mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-12-17 18:43:50 +01:00
nanocoap_sock: don't store entire sock in coap_block_request_t
This commit is contained in:
parent
3877a92ca4
commit
63c9dde3a4
@ -150,7 +150,7 @@ typedef sock_udp_t nanocoap_sock_t;
|
|||||||
* @brief Blockwise request helper struct
|
* @brief Blockwise request helper struct
|
||||||
*/
|
*/
|
||||||
typedef 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 */
|
const char *path; /**< path on the server */
|
||||||
uint32_t blknum; /**< current block number */
|
uint32_t blknum; /**< current block number */
|
||||||
uint8_t method; /**< request method (GET, POST, PUT) */
|
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);
|
void *buf, size_t len);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Initialize block request context
|
* @brief Initialize block request context by URL and connect a socket
|
||||||
*
|
|
||||||
* @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
|
|
||||||
*
|
*
|
||||||
* @param[out] ctx The block request context to initialize
|
* @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] url The request URL
|
||||||
* @param[in] method Request method (`COAP_METHOD_{GET|PUT|POST}`)
|
* @param[in] method Request method (`COAP_METHOD_{GET|PUT|POST}`)
|
||||||
* @param[in] blksize Request blocksize exponent
|
* @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 Success
|
||||||
* @retval <0 Error (see @ref nanocoap_sock_url_connect for details)
|
* @retval <0 Error (see @ref nanocoap_sock_url_connect for details)
|
||||||
*/
|
*/
|
||||||
static inline int nanocoap_block_request_init_url(coap_block_request_t *ctx,
|
static inline int nanocoap_block_request_connect_url(coap_block_request_t *ctx,
|
||||||
const char *url,
|
nanocoap_sock_t *sock,
|
||||||
uint8_t method,
|
const char *url,
|
||||||
coap_blksize_t blksize)
|
uint8_t method,
|
||||||
|
coap_blksize_t blksize)
|
||||||
{
|
{
|
||||||
|
ctx->sock = sock;
|
||||||
ctx->path = sock_urlpath(url);
|
ctx->path = sock_urlpath(url);
|
||||||
ctx->blknum = 0;
|
ctx->blknum = 0;
|
||||||
ctx->method = method;
|
ctx->method = method;
|
||||||
ctx->blksize = blksize;
|
ctx->blksize = blksize;
|
||||||
return nanocoap_sock_url_connect(url, &ctx->sock);
|
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -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
|
* This method is expected to be called in a loop until all
|
||||||
* payload blocks have been transferred.
|
* payload blocks have been transferred.
|
||||||
*
|
*
|
||||||
* @pre @p ctx was initialized with @ref nanocoap_block_request_init or
|
* @pre @p ctx was initialized with @ref nanocoap_block_request_connect_url
|
||||||
* @ref nanocoap_block_request_init_url
|
* or manually.
|
||||||
*
|
*
|
||||||
* @param[in] ctx blockwise request context
|
* @param[in] ctx blockwise request context
|
||||||
* @param[in] data payload to send
|
* @param[in] data payload to send
|
||||||
|
|||||||
@ -527,7 +527,7 @@ int nanocoap_sock_block_request(coap_block_request_t *req,
|
|||||||
pkt.payload = pktpos;
|
pkt.payload = pktpos;
|
||||||
pkt.payload_len = 0;
|
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) {
|
if (res < 0) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -131,7 +131,7 @@ int nanocoap_vfs_put(nanocoap_sock_t *sock, const char *path, const char *src,
|
|||||||
.path = path,
|
.path = path,
|
||||||
.method = COAP_METHOD_PUT,
|
.method = COAP_METHOD_PUT,
|
||||||
.blksize = coap_size2szx(work_buf_len - 1),
|
.blksize = coap_size2szx(work_buf_len - 1),
|
||||||
.sock = *sock,
|
.sock = sock,
|
||||||
};
|
};
|
||||||
|
|
||||||
return _vfs_put(&ctx, src, work_buf);
|
return _vfs_put(&ctx, src, work_buf);
|
||||||
@ -146,12 +146,13 @@ int nanocoap_vfs_put_url(const char *url, const char *src,
|
|||||||
return -ENOBUFS;
|
return -ENOBUFS;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
nanocoap_sock_t sock;
|
||||||
coap_block_request_t ctx;
|
coap_block_request_t ctx;
|
||||||
int res = nanocoap_block_request_init_url(&ctx, url, COAP_METHOD_PUT,
|
int res = nanocoap_block_request_connect_url(&ctx, &sock, url, COAP_METHOD_PUT,
|
||||||
coap_size2szx(work_buf_len - 1));
|
coap_size2szx(work_buf_len - 1));
|
||||||
if (res == 0) {
|
if (res == 0) {
|
||||||
res = _vfs_put(&ctx, src, work_buf);
|
res = _vfs_put(&ctx, src, work_buf);
|
||||||
nanocoap_block_request_done(&ctx);
|
nanocoap_sock_close(&sock);
|
||||||
}
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
|
|||||||
@ -268,6 +268,7 @@ static const char song[] =
|
|||||||
int nanotest_client_put_cmd(int argc, char **argv)
|
int nanotest_client_put_cmd(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
|
nanocoap_sock_t sock;
|
||||||
coap_block_request_t ctx;
|
coap_block_request_t ctx;
|
||||||
|
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
@ -275,8 +276,8 @@ int nanotest_client_put_cmd(int argc, char **argv)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
res = nanocoap_block_request_init_url(&ctx, argv[1],
|
res = nanocoap_block_request_connect_url(&ctx, &sock, argv[1],
|
||||||
COAP_METHOD_PUT, COAP_BLOCKSIZE_32);
|
COAP_METHOD_PUT, COAP_BLOCKSIZE_32);
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
printf("error: %d\n", res);
|
printf("error: %d\n", res);
|
||||||
return res;
|
return res;
|
||||||
@ -295,7 +296,7 @@ int nanotest_client_put_cmd(int argc, char **argv)
|
|||||||
pos += res;
|
pos += res;
|
||||||
}
|
}
|
||||||
|
|
||||||
nanocoap_block_request_done(&ctx);
|
nanocoap_sock_close(&sock);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user