mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
Merge pull request #18827 from benpicco/nanocoap_block-fix
nanocoap_sock: don't store entire sock in coap_block_request_t
This commit is contained in:
commit
f8964c0149
@ -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
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
@ -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);
|
||||
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user