Skip to content

Commit

Permalink
Merge pull request #18827 from benpicco/nanocoap_block-fix
Browse files Browse the repository at this point in the history
nanocoap_sock: don't store entire sock in coap_block_request_t
  • Loading branch information
benpicco committed Nov 8, 2022
2 parents 7e0af3c + 63c9dde commit f8964c0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 56 deletions.
56 changes: 12 additions & 44 deletions sys/include/net/nanocoap_sock.h
Expand Up @@ -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) */
Expand Down Expand Up @@ -454,61 +454,29 @@ 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
*
* @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);
}

/**
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sys/net/application_layer/nanocoap/sock.c
Expand Up @@ -528,7 +528,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;
}
Expand Down
16 changes: 8 additions & 8 deletions sys/net/application_layer/nanocoap/vfs.c
Expand Up @@ -114,8 +114,6 @@ static int _vfs_put(coap_block_request_t *ctx, const char *file, void *buffer)
vfs_lseek(fd, -1, SEEK_CUR);
}

nanocoap_block_request_done(ctx);

vfs_close(fd);
return res;
}
Expand All @@ -133,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);
Expand All @@ -148,12 +146,14 @@ 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));
if (res) {
return res;
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_sock_close(&sock);
}

return _vfs_put(&ctx, src, work_buf);
return res;
}
7 changes: 4 additions & 3 deletions tests/nanocoap_cli/nanocli_client.c
Expand Up @@ -268,15 +268,16 @@ 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) {
printf("usage: %s <url>\n", argv[0]);
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;
Expand All @@ -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;
}

Expand Down

0 comments on commit f8964c0

Please sign in to comment.