From 63c9dde3a4c970c3e8fde9d1d01118dfa21aee82 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Tue, 1 Nov 2022 00:02:31 +0100 Subject: [PATCH] nanocoap_sock: don't store entire sock in coap_block_request_t --- sys/include/net/nanocoap_sock.h | 56 +++++------------------ sys/net/application_layer/nanocoap/sock.c | 2 +- sys/net/application_layer/nanocoap/vfs.c | 9 ++-- tests/nanocoap_cli/nanocli_client.c | 7 +-- 4 files changed, 22 insertions(+), 52 deletions(-) diff --git a/sys/include/net/nanocoap_sock.h b/sys/include/net/nanocoap_sock.h index 5bb0710042af..77044f71c2b6 100644 --- a/sys/include/net/nanocoap_sock.h +++ b/sys/include/net/nanocoap_sock.h @@ -150,7 +150,7 @@ typedef sock_udp_t nanocoap_sock_t; * @brief Blockwise request helper struct */ typedef struct { - nanocoap_sock_t sock; /**< socket used for the request */ + nanocoap_sock_t *sock; /**< socket used for the request */ const char *path; /**< path on the server */ uint32_t blknum; /**< current block number */ uint8_t method; /**< request method (GET, POST, PUT) */ @@ -454,34 +454,10 @@ ssize_t nanocoap_get(const sock_udp_ep_t *remote, const char *path, void *buf, size_t len); /** - * @brief Initialize block request context - * - * @param[out] ctx The block request context to initialize - * @param[in] remote Server endpoint - * @param[in] path Server path for request - * @param[in] method Request method (`COAP_METHOD_{GET|PUT|POST}`) - * @param[in] blksize Request blocksize exponent - * - * @retval 0 Success - * @retval <0 Error (see @ref nanocoap_sock_connect for details) - */ -static inline int nanocoap_block_request_init(coap_block_request_t *ctx, - const sock_udp_ep_t *remote, - const char *path, - uint8_t method, - coap_blksize_t blksize) -{ - ctx->path = path; - ctx->blknum = 0; - ctx->method = method; - ctx->blksize = blksize; - return nanocoap_sock_connect(&ctx->sock, NULL, remote); -} - -/** - * @brief Initialize block request context by URL + * @brief Initialize block request context by URL and connect a socket * * @param[out] ctx The block request context to initialize + * @param[out] sock Socket to initialize and use for the request * @param[in] url The request URL * @param[in] method Request method (`COAP_METHOD_{GET|PUT|POST}`) * @param[in] blksize Request blocksize exponent @@ -489,26 +465,18 @@ static inline int nanocoap_block_request_init(coap_block_request_t *ctx, * @retval 0 Success * @retval <0 Error (see @ref nanocoap_sock_url_connect for details) */ -static inline int nanocoap_block_request_init_url(coap_block_request_t *ctx, - const char *url, - uint8_t method, - coap_blksize_t blksize) +static inline int nanocoap_block_request_connect_url(coap_block_request_t *ctx, + nanocoap_sock_t *sock, + const char *url, + uint8_t method, + coap_blksize_t blksize) { + ctx->sock = sock; ctx->path = sock_urlpath(url); ctx->blknum = 0; ctx->method = method; ctx->blksize = blksize; - return nanocoap_sock_url_connect(url, &ctx->sock); -} - -/** - * @brief Free block request context - * - * @param[out] ctx The block request context to finalize - */ -static inline void nanocoap_block_request_done(coap_block_request_t *ctx) -{ - nanocoap_sock_close(&ctx->sock); + return nanocoap_sock_url_connect(url, ctx->sock); } /** @@ -517,8 +485,8 @@ static inline void nanocoap_block_request_done(coap_block_request_t *ctx) * This method is expected to be called in a loop until all * payload blocks have been transferred. * - * @pre @p ctx was initialized with @ref nanocoap_block_request_init or - * @ref nanocoap_block_request_init_url + * @pre @p ctx was initialized with @ref nanocoap_block_request_connect_url + * or manually. * * @param[in] ctx blockwise request context * @param[in] data payload to send diff --git a/sys/net/application_layer/nanocoap/sock.c b/sys/net/application_layer/nanocoap/sock.c index d440d2fff504..7f9c9d62eb55 100644 --- a/sys/net/application_layer/nanocoap/sock.c +++ b/sys/net/application_layer/nanocoap/sock.c @@ -527,7 +527,7 @@ int nanocoap_sock_block_request(coap_block_request_t *req, pkt.payload = pktpos; pkt.payload_len = 0; - res = nanocoap_sock_request_cb(&req->sock, &pkt, callback, arg); + res = nanocoap_sock_request_cb(req->sock, &pkt, callback, arg); if (res < 0) { return res; } diff --git a/sys/net/application_layer/nanocoap/vfs.c b/sys/net/application_layer/nanocoap/vfs.c index 31b951b787a6..c26af368eb31 100644 --- a/sys/net/application_layer/nanocoap/vfs.c +++ b/sys/net/application_layer/nanocoap/vfs.c @@ -131,7 +131,7 @@ int nanocoap_vfs_put(nanocoap_sock_t *sock, const char *path, const char *src, .path = path, .method = COAP_METHOD_PUT, .blksize = coap_size2szx(work_buf_len - 1), - .sock = *sock, + .sock = sock, }; return _vfs_put(&ctx, src, work_buf); @@ -146,12 +146,13 @@ int nanocoap_vfs_put_url(const char *url, const char *src, return -ENOBUFS; } + nanocoap_sock_t sock; coap_block_request_t ctx; - int res = nanocoap_block_request_init_url(&ctx, url, COAP_METHOD_PUT, - coap_size2szx(work_buf_len - 1)); + int res = nanocoap_block_request_connect_url(&ctx, &sock, url, COAP_METHOD_PUT, + coap_size2szx(work_buf_len - 1)); if (res == 0) { res = _vfs_put(&ctx, src, work_buf); - nanocoap_block_request_done(&ctx); + nanocoap_sock_close(&sock); } return res; diff --git a/tests/nanocoap_cli/nanocli_client.c b/tests/nanocoap_cli/nanocli_client.c index 7cf40ffcabd2..b406d8e3d285 100644 --- a/tests/nanocoap_cli/nanocli_client.c +++ b/tests/nanocoap_cli/nanocli_client.c @@ -268,6 +268,7 @@ static const char song[] = int nanotest_client_put_cmd(int argc, char **argv) { int res; + nanocoap_sock_t sock; coap_block_request_t ctx; if (argc < 2) { @@ -275,8 +276,8 @@ int nanotest_client_put_cmd(int argc, char **argv) return 1; } - res = nanocoap_block_request_init_url(&ctx, argv[1], - COAP_METHOD_PUT, COAP_BLOCKSIZE_32); + res = nanocoap_block_request_connect_url(&ctx, &sock, argv[1], + COAP_METHOD_PUT, COAP_BLOCKSIZE_32); if (res < 0) { printf("error: %d\n", res); return res; @@ -295,7 +296,7 @@ int nanotest_client_put_cmd(int argc, char **argv) pos += res; } - nanocoap_block_request_done(&ctx); + nanocoap_sock_close(&sock); return res; }