boot_serial; improve platform abstraction.
Pass function pointers to do reads/writes from uart.
Signed-off-by: Marko Kiiskila <marko@runtime.io>
diff --git a/boot/boot_serial/include/boot_serial/boot_serial.h b/boot/boot_serial/include/boot_serial/boot_serial.h
index b93c28f..3393213 100644
--- a/boot/boot_serial/include/boot_serial/boot_serial.h
+++ b/boot/boot_serial/include/boot_serial/boot_serial.h
@@ -24,12 +24,24 @@
extern "C" {
#endif
-/*
- * Start processing newtmgr commands for uploading image0 over serial.
- *
- * Open console serial port and wait for download command.
+/**
+ * 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.
*/
-void boot_serial_start(int max_input);
+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.
+ * Assumes serial port is open and waits for download command.
+ */
+void boot_serial_start(const struct boot_uart_funcs *f);
#ifdef __cplusplus
}
diff --git a/boot/boot_serial/pkg.yml b/boot/boot_serial/pkg.yml
index 69d90e1..f1431c7 100644
--- a/boot/boot_serial/pkg.yml
+++ b/boot/boot_serial/pkg.yml
@@ -29,7 +29,6 @@
- "@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"
diff --git a/boot/boot_serial/src/CMakeLists.txt b/boot/boot_serial/src/CMakeLists.txt
index 7b0bf99..20feaa1 100644
--- a/boot/boot_serial/src/CMakeLists.txt
+++ b/boot/boot_serial/src/CMakeLists.txt
@@ -2,7 +2,4 @@
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)
diff --git a/boot/boot_serial/src/boot_serial.c b/boot/boot_serial/src/boot_serial.c
index e67e68d..9b63043 100644
--- a/boot/boot_serial/src/boot_serial.c
+++ b/boot/boot_serial/src/boot_serial.c
@@ -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 @@
#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 @@
#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 @@
* 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 @@
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;
}