From 88d0b5753c020a69d18a763fae09bab3acd27cbb Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 6 Jan 2022 00:08:15 +0100 Subject: [PATCH] suit/transport/coap: let caller allocate blockwise work buffer --- sys/include/net/nanocoap.h | 15 +++++++++++++++ sys/include/suit/transport/coap.h | 3 ++- sys/suit/handlers_command_seq.c | 3 ++- sys/suit/transport/coap.c | 31 +++++++++++++++---------------- 4 files changed, 34 insertions(+), 18 deletions(-) diff --git a/sys/include/net/nanocoap.h b/sys/include/net/nanocoap.h index dcefeb7617..13f17d19a1 100644 --- a/sys/include/net/nanocoap.h +++ b/sys/include/net/nanocoap.h @@ -150,6 +150,21 @@ extern "C" { #endif /** @} */ +/** + * @brief Maximum length of a CoAP header for a blockwise message + */ +#ifndef CONFIG_NANOCOAP_BLOCK_HEADER_MAX +#define CONFIG_NANOCOAP_BLOCK_HEADER_MAX (64) +#endif + +/** + * @brief Work buffer size for blockwise operation + * + * @param[in] blksize CoAP blocksize + */ +#define NANOCOAP_BLOCKWISE_BUF(blksize) (CONFIG_NANOCOAP_BLOCK_HEADER_MAX \ + + (0x1 << (blksize + 4))) + /** * @name coap_opt_finish() flag parameter values * diff --git a/sys/include/suit/transport/coap.h b/sys/include/suit/transport/coap.h index db88d0ca34..79e66e460d 100644 --- a/sys/include/suit/transport/coap.h +++ b/sys/include/suit/transport/coap.h @@ -137,6 +137,7 @@ typedef enum { * * @param[in] url url pointer to source path * @param[in] blksize sender suggested SZX for the COAP block request + * @param[in] work_buf Work buffer, must be NANOCOAP_BLOCKWISE_BUF(blksize) bytes * @param[in] callback callback to be executed on each received block * @param[in] arg optional function arguments * @@ -145,7 +146,7 @@ typedef enum { * @returns 0 on success */ int suit_coap_get_blockwise_url(const char *url, - coap_blksize_t blksize, + coap_blksize_t blksize, void *work_buf, coap_blockwise_cb_t callback, void *arg); /** diff --git a/sys/suit/handlers_command_seq.c b/sys/suit/handlers_command_seq.c index 818bad2752..0d0c755255 100644 --- a/sys/suit/handlers_command_seq.c +++ b/sys/suit/handlers_command_seq.c @@ -358,8 +358,9 @@ static int _dtv_fetch(suit_manifest_t *manifest, int key, if (0) {} #ifdef MODULE_SUIT_TRANSPORT_COAP else if (strncmp(manifest->urlbuf, "coap://", 7) == 0) { + uint8_t buffer[NANOCOAP_BLOCKWISE_BUF(CONFIG_SUIT_COAP_BLOCKSIZE)]; res = suit_coap_get_blockwise_url(manifest->urlbuf, CONFIG_SUIT_COAP_BLOCKSIZE, - suit_storage_helper, + buffer, suit_storage_helper, manifest); } #endif diff --git a/sys/suit/transport/coap.c b/sys/suit/transport/coap.c index 73e0fd957b..1852f6b97a 100644 --- a/sys/suit/transport/coap.c +++ b/sys/suit/transport/coap.c @@ -144,14 +144,13 @@ static int _fetch_block(coap_pkt_t *pkt, uint8_t *buf, sock_udp_t *sock, pktpos += coap_build_hdr(pkt->hdr, COAP_TYPE_CON, NULL, 0, COAP_METHOD_GET, num); pktpos += coap_opt_put_uri_pathquery(pktpos, &lastonum, path); - pktpos += - coap_opt_put_uint(pktpos, lastonum, COAP_OPT_BLOCK2, - (num << 4) | blksize); + pktpos += coap_opt_put_uint(pktpos, lastonum, COAP_OPT_BLOCK2, + (num << 4) | blksize); pkt->payload = pktpos; pkt->payload_len = 0; - int res = nanocoap_request(sock, pkt, 64 + (0x1 << (blksize + 4))); + int res = nanocoap_request(sock, pkt, NANOCOAP_BLOCKWISE_BUF(blksize)); if (res < 0) { return res; } @@ -166,11 +165,9 @@ static int _fetch_block(coap_pkt_t *pkt, uint8_t *buf, sock_udp_t *sock, } int suit_coap_get_blockwise(sock_udp_ep_t *remote, const char *path, - coap_blksize_t blksize, + coap_blksize_t blksize, void *buf, coap_blockwise_cb_t callback, void *arg) { - /* mmmmh dynamically sized array */ - uint8_t buf[64 + (0x1 << (blksize + 4))]; sock_udp_ep_t local = SOCK_IPV6_EP_ANY; coap_pkt_t pkt; @@ -218,7 +215,7 @@ out: } int suit_coap_get_blockwise_url(const char *url, - coap_blksize_t blksize, + coap_blksize_t blksize, void *buf, coap_blockwise_cb_t callback, void *arg) { char hostport[CONFIG_SOCK_HOSTPORT_MAXLEN]; @@ -244,7 +241,7 @@ int suit_coap_get_blockwise_url(const char *url, remote.port = COAP_PORT; } - return suit_coap_get_blockwise(&remote, urlpath, blksize, callback, arg); + return suit_coap_get_blockwise(&remote, urlpath, blksize, buf, callback, arg); } typedef struct { @@ -273,20 +270,20 @@ static int _2buf(void *arg, size_t offset, uint8_t *buf, size_t len, int more) } } -ssize_t suit_coap_get_blockwise_url_buf(const char *url, - coap_blksize_t blksize, - uint8_t *buf, size_t len) +static ssize_t suit_coap_get_blockwise_url_buf(const char *url, + coap_blksize_t blksize, void *work_buf, + uint8_t *buf, size_t len) { _buf_t _buf = { .ptr = buf, .len = len }; - int res = suit_coap_get_blockwise_url(url, blksize, _2buf, &_buf); + int res = suit_coap_get_blockwise_url(url, blksize, work_buf, _2buf, &_buf); return (res < 0) ? (ssize_t)res : (ssize_t)_buf.offset; } -static void _suit_handle_url(const char *url) +static void _suit_handle_url(const char *url, coap_blksize_t blksize, void *work_buf) { LOG_INFO("suit_coap: downloading \"%s\"\n", url); - ssize_t size = suit_coap_get_blockwise_url_buf(url, CONFIG_SUIT_COAP_BLOCKSIZE, + ssize_t size = suit_coap_get_blockwise_url_buf(url, blksize, work_buf, _manifest_buf, SUIT_MANIFEST_BUFSIZE); if (size >= 0) { @@ -375,6 +372,8 @@ static void *_suit_coap_thread(void *arg) { (void)arg; + uint8_t buffer[NANOCOAP_BLOCKWISE_BUF(CONFIG_SUIT_COAP_BLOCKSIZE)]; + LOG_INFO("suit_coap: started.\n"); msg_t msg_queue[4]; msg_init_queue(msg_queue, 4); @@ -388,7 +387,7 @@ static void *_suit_coap_thread(void *arg) switch (m.content.value) { case SUIT_MSG_TRIGGER: LOG_INFO("suit_coap: trigger received\n"); - _suit_handle_url(_url); + _suit_handle_url(_url, CONFIG_SUIT_COAP_BLOCKSIZE, buffer); break; default: LOG_WARNING("suit_coap: warning: unhandled msg\n");