Skip to content

Commit f5e7753

Browse files
pepe2kde-nordic
authored andcommitted
boot_serial: support fragmentation for outgoing SMP packets
The mcumgr packet sent over serial should be fragmented into frames of up to 127 bytes, including 2-bytes frame start header and 1-byte for terminating newline [1], resulting in up to 124 bytes for the base64 encoded payload. Current implementation doesn't fulfill above requirement and transmits single frames, without considering their length. This change introduces support for fragmenting as defined in 'SMP over console' specification. [1] github.com/apache/mynewt-mcumgr/blob/master/transport/smp-console.md Signed-off-by: Piotr Dymacz <pepe2k@gmail.com>
1 parent 569b1d6 commit f5e7753

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

boot/boot_serial/src/boot_serial.c

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ BOOT_LOG_MODULE_DECLARE(mcuboot);
8383
#endif
8484

8585
#define BOOT_SERIAL_OUT_MAX (128 * BOOT_IMAGE_NUMBER)
86+
#define BOOT_SERIAL_FRAME_MTU 124 /* 127 - pkt start (2 bytes) and stop (1 byte) */
8687

8788
#ifdef __ZEPHYR__
8889
/* base64 lib encodes data to null-terminated string */
@@ -758,9 +759,10 @@ static void
758759
boot_serial_output(void)
759760
{
760761
char *data;
761-
int len;
762+
int len, out;
762763
uint16_t crc;
763764
uint16_t totlen;
765+
char pkt_cont[2] = { SHELL_NLIP_DATA_START1, SHELL_NLIP_DATA_START2 };
764766
char pkt_start[2] = { SHELL_NLIP_PKT_START1, SHELL_NLIP_PKT_START2 };
765767
char buf[BOOT_SERIAL_OUT_MAX + sizeof(*bs_hdr) + sizeof(crc) + sizeof(totlen)];
766768
char encoded_buf[BASE64_ENCODE_SIZE(sizeof(buf))];
@@ -786,8 +788,6 @@ boot_serial_output(void)
786788
#endif
787789
crc = htons(crc);
788790

789-
boot_uf->write(pkt_start, sizeof(pkt_start));
790-
791791
totlen = len + sizeof(*bs_hdr) + sizeof(crc);
792792
totlen = htons(totlen);
793793

@@ -810,8 +810,23 @@ boot_serial_output(void)
810810
#else
811811
totlen = base64_encode(buf, totlen, encoded_buf, 1);
812812
#endif
813-
boot_uf->write(encoded_buf, totlen);
814-
boot_uf->write("\n", 1);
813+
814+
out = 0;
815+
while (out < totlen) {
816+
if (out == 0) {
817+
boot_uf->write(pkt_start, sizeof(pkt_start));
818+
} else {
819+
boot_uf->write(pkt_cont, sizeof(pkt_cont));
820+
}
821+
822+
len = MIN(BOOT_SERIAL_FRAME_MTU, totlen - out);
823+
boot_uf->write(&encoded_buf[out], len);
824+
825+
out += len;
826+
827+
boot_uf->write("\n", 1);
828+
}
829+
815830
BOOT_LOG_INF("TX");
816831
}
817832

0 commit comments

Comments
 (0)