Commit 149b457a authored by Marko Kiiskila's avatar Marko Kiiskila Committed by mkiiskila
Browse files

boot_serial; improve platform abstraction.

Pass function pointers to do reads/writes from uart.

Signed-off-by: default avatarMarko Kiiskila <marko@runtime.io>
parent c0011585
Loading
Loading
Loading
Loading
+16 −4
Changes for boot/boot_serial/include/boot_serial/boot_serial.h: 16 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -24,12 +24,24 @@
extern "C" {
#endif

/*
/**
 * Function pointers to read/write data from uart.
 * read returns the number of bytes read, str points to buffer to fill,
 *  cnt is the number of bytes to fill within buffer, *newline will be
 *  set if newline is the last character.
 * write takes as it's arguments pointer to data to write, and the count
 *  of bytes.
 */
struct boot_uart_funcs {
    int (*read)(char *str, int cnt, int *newline);
    void (*write)(const char *ptr, int cnt);
};

/**
 * Start processing newtmgr commands for uploading image0 over serial.
 *
 * Open console serial port and wait for download command.
 * Assumes serial port is open and waits for download command.
 */
void boot_serial_start(int max_input);
void boot_serial_start(const struct boot_uart_funcs *f);

#ifdef __cplusplus
}
−1
Changes for boot/boot_serial/pkg.yml: 0 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -29,7 +29,6 @@ pkg.deps:
    - "@apache-mynewt-core/hw/hal"
    - "@apache-mynewt-core/kernel/os"
    - "@apache-mynewt-core/encoding/tinycbor"
    - "@apache-mynewt-core/encoding/cborattr"
    - "@apache-mynewt-core/encoding/base64"
    - "@mcuboot/boot/mynewt/flash_map_backend"
    - "@mcuboot/boot/mynewt/boot_uart"
−3
Changes for boot/boot_serial/src/CMakeLists.txt: 0 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -2,7 +2,4 @@ zephyr_sources(boot_serial.c)

zephyr_include_directories(${BOOT_DIR}/bootutil/include)

zephyr_include_directories($ENV{ZEPHYR_BASE}/ext/lib/mgmt/mcumgr/cborattr/include)
zephyr_sources($ENV{ZEPHYR_BASE}/ext/lib/mgmt/mcumgr/cborattr/src/cborattr.c)

zephyr_link_libraries_ifdef(CONFIG_TINYCBOR TINYCBOR)
+12 −26
Changes for boot/boot_serial/src/boot_serial.c: 12 added lines, 26 removed lines.
Original line number Diff line number Diff line
@@ -32,7 +32,6 @@
#include <misc/__assert.h>
#include <flash.h>
#include <crc16.h>
#include <serial_adapter/serial_adapter.h>
#include <base64.h>
#include <cbor.h>
#else
@@ -40,18 +39,12 @@
#include <hal/hal_system.h>
#include <os/endian.h>
#include <os/os_cputime.h>
#include <boot_uart/boot_uart.h>
#include <crc/crc16.h>
#include <base64/base64.h>
#include <tinycbor/cbor.h>
#include <tinycbor/cbor_buf_reader.h>

#define console_write boot_uart_write
#define console_read  boot_uart_read

#endif /* __ZEPHYR__ */

#include <cborattr/cborattr.h>
#include <flash_map_backend/flash_map_backend.h>
#include <hal/hal_flash.h>
#include <os/os.h>
@@ -62,6 +55,7 @@
#include "boot_serial/boot_serial.h"
#include "boot_serial_priv.h"

#define BOOT_SERIAL_INPUT_MAX   512
#define BOOT_SERIAL_OUT_MAX	80

#ifdef __ZEPHYR__
@@ -74,10 +68,10 @@
#define ntohs(x) sys_be16_to_cpu(x)
#define htons(x) sys_cpu_to_be16(x)

static char in_buf[CONFIG_BOOT_MAX_LINE_INPUT_LEN + 1];
static char dec_buf[CONFIG_BOOT_MAX_LINE_INPUT_LEN + 1];
#endif

static char in_buf[BOOT_SERIAL_INPUT_MAX + 1];
static char dec_buf[BOOT_SERIAL_INPUT_MAX + 1];
static const struct boot_uart_funcs *boot_uf;
static uint32_t curr_off;
static uint32_t img_size;
static struct nmgr_hdr *bs_hdr;
@@ -484,7 +478,7 @@ boot_serial_output(void)
#endif
    crc = htons(crc);

    console_write(pkt_start, sizeof(pkt_start));
    boot_uf->write(pkt_start, sizeof(pkt_start));

    totlen = len + sizeof(*bs_hdr) + sizeof(crc);
    totlen = htons(totlen);
@@ -504,8 +498,8 @@ boot_serial_output(void)
#else
    totlen = base64_encode(buf, totlen, encoded_buf, 1);
#endif
    console_write(encoded_buf, totlen);
    console_write("\n\r", 2);
    boot_uf->write(encoded_buf, totlen);
    boot_uf->write("\n\r", 2);
    BOOT_LOG_INF("TX");
}

@@ -561,7 +555,7 @@ boot_serial_in_dec(char *in, int inlen, char *out, int *out_off, int maxout)
 * serial port.
 */
void
boot_serial_start(int max_input)
boot_serial_start(const struct boot_uart_funcs *f)
{
    int rc;
    int off;
@@ -569,24 +563,16 @@ boot_serial_start(int max_input)
    char *dec;
    int dec_off;
    int full_line;
    int max_input;

#ifdef __ZEPHYR__
    rc = boot_console_init();
    boot_uf = f;
    buf = in_buf;
    dec = dec_buf;
    assert(max_input <= sizeof(in_buf) && max_input <= sizeof(dec_buf));
#else
    rc = boot_uart_open();
    assert(rc == 0);

    buf = os_malloc(max_input);
    dec = os_malloc(max_input);
    assert(buf && dec);
#endif
    max_input = sizeof(in_buf);

    off = 0;
    while (1) {
        rc = console_read(buf + off, max_input - off, &full_line);
        rc = f->read(buf + off, sizeof(in_buf) - off, &full_line);
        if (rc <= 0 && !full_line) {
            continue;
        }
+1 −1
Changes for boot/mynewt/boot_uart/include/boot_uart/boot_uart.h: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -23,6 +23,6 @@
int boot_uart_open(void);
void boot_uart_close(void);
int boot_uart_read(char *str, int cnt, int *newline);
void boot_uart_write(char *ptr, int cnt);
void boot_uart_write(const char *ptr, int cnt);

#endif /* _BOOT_UART_H_ */
Loading