From c5cfecb31108e119e27b4b6d4674b717fd59f847 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Thu, 23 May 2024 15:58:29 +0200 Subject: [PATCH] nanocoap_sock: add nanocoap_get_blockwise_to_buf() --- sys/include/net/nanocoap_sock.h | 23 +++++++++++++++++++++++ sys/net/application_layer/nanocoap/sock.c | 11 +++++++++++ 2 files changed, 34 insertions(+) diff --git a/sys/include/net/nanocoap_sock.h b/sys/include/net/nanocoap_sock.h index e079df79cc5c..422cbf149078 100644 --- a/sys/include/net/nanocoap_sock.h +++ b/sys/include/net/nanocoap_sock.h @@ -584,6 +584,29 @@ ssize_t nanocoap_get_blockwise_url_to_buf(const char *url, coap_blksize_t blksize, void *buf, size_t len); +/** + * @brief Performs a blockwise CoAP GET request, store the response + * in a buffer. + * + * This function will fetch the content of the specified resource path via + * block-wise-transfer. + * The blocks will be re-assembled into @p buf + * + * @param[in] sock socket to use for the request + * @param[in] path pointer to source path + * @param[in] blksize sender suggested SZX for the COAP block request + * @param[in] buf Target buffer + * @param[in] len Target buffer length + * + * @returns <0 on error + * @returns -EINVAL if an invalid url is provided + * @returns -ENOBUFS if the provided buffer was too small + * @returns size of the response payload on success + */ +ssize_t nanocoap_get_blockwise_to_buf(nanocoap_sock_t *sock, const char *path, + coap_blksize_t blksize, + void *buf, size_t len); + /** * @brief Simple synchronous CoAP request * diff --git a/sys/net/application_layer/nanocoap/sock.c b/sys/net/application_layer/nanocoap/sock.c index 1802ad26cdcf..dd7a185f35cd 100644 --- a/sys/net/application_layer/nanocoap/sock.c +++ b/sys/net/application_layer/nanocoap/sock.c @@ -811,6 +811,17 @@ ssize_t nanocoap_get_blockwise_url_to_buf(const char *url, return (res < 0) ? (ssize_t)res : (ssize_t)_buf.len; } +ssize_t nanocoap_get_blockwise_to_buf(nanocoap_sock_t *sock, const char *path, + coap_blksize_t blksize, + void *buf, size_t len) +{ + _buf_t _buf = { .ptr = buf, .len = len }; + + int res = nanocoap_sock_get_blockwise(sock, path, blksize, _2buf, &_buf); + + return (res < 0) ? (ssize_t)res : (ssize_t)_buf.len; +} + int nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize) { nanocoap_sock_t sock;