1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2026-01-01 01:41:18 +01:00

Merge pull request #18514 from benpicco/nanocoap_sock_put

nanocoap_sock: add nanocoap_sock_put()
This commit is contained in:
benpicco 2022-08-30 14:00:42 +02:00 committed by GitHub
commit 346c7336eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 123 additions and 6 deletions

View File

@ -206,7 +206,7 @@ static inline void nanocoap_sock_close(nanocoap_sock_t *sock)
}
/**
* @brief Simple synchronous CoAP (confirmable) get
* @brief Simple synchronous CoAP (confirmable) GET
*
* @param[in] sock socket to use for the request
* @param[in] path remote path
@ -219,6 +219,40 @@ static inline void nanocoap_sock_close(nanocoap_sock_t *sock)
ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf,
size_t len);
/**
* @brief Simple synchronous CoAP (confirmable) PUT
*
* @param[in] sock socket to use for the request
* @param[in] path remote path
* @param[in] request buffer containing the payload
* @param[in] len length of the payload to send
* @param[out] response buffer for the response, may be NULL
* @param[in] len_max length of @p response
*
* @returns length of response payload on success
* @returns <0 on error
*/
ssize_t nanocoap_sock_put(nanocoap_sock_t *sock, const char *path,
const void *request, size_t len,
void *response, size_t len_max);
/**
* @brief Simple synchronous CoAP (confirmable) POST
*
* @param[in] sock socket to use for the request
* @param[in] path remote path
* @param[in] request buffer containing the payload
* @param[in] len length of the payload to send
* @param[out] response buffer for the response, may be NULL
* @param[in] len_max length of @p response
*
* @returns length of response payload on success
* @returns <0 on error
*/
ssize_t nanocoap_sock_post(nanocoap_sock_t *sock, const char *path,
const void *request, size_t len,
void *response, size_t len_max);
/**
* @brief Performs a blockwise coap get request on a socket.
*

View File

@ -280,7 +280,7 @@ ssize_t nanocoap_sock_request(sock_udp_t *sock, coap_pkt_t *pkt, size_t len)
return nanocoap_sock_request_cb(sock, pkt, _request_cb, &buf);
}
static int _get_cb(void *arg, coap_pkt_t *pkt)
static int _get_put_cb(void *arg, coap_pkt_t *pkt)
{
struct iovec *buf = arg;
@ -316,7 +316,58 @@ ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf, si
pkt.payload = pktpos;
pkt.payload_len = 0;
return nanocoap_sock_request_cb(sock, &pkt, _get_cb, &ctx);
return nanocoap_sock_request_cb(sock, &pkt, _get_put_cb, &ctx);
}
ssize_t _sock_put_post(nanocoap_sock_t *sock, const char *path, unsigned code,
const void *request, size_t len,
void *response, size_t max_len)
{
/* buffer for CoAP header */
uint8_t buffer[CONFIG_NANOCOAP_BLOCK_HEADER_MAX];
uint8_t *pktpos = buffer;
iolist_t payload = {
.iol_base = (void *)request,
.iol_len = len,
};
coap_pkt_t pkt = {
.hdr = (void *)buffer,
.snips = &payload,
};
struct iovec ctx = {
.iov_base = response,
.iov_len = max_len,
};
pktpos += coap_build_hdr(pkt.hdr, COAP_TYPE_CON, NULL, 0, code, _get_id());
pktpos += coap_opt_put_uri_path(pktpos, 0, path);
if (len) {
/* set payload marker */
*pktpos++ = 0xFF;
}
pkt.payload = pktpos;
pkt.payload_len = 0;
return nanocoap_sock_request_cb(sock, &pkt, response ? _get_put_cb : NULL, &ctx);
}
ssize_t nanocoap_sock_put(nanocoap_sock_t *sock, const char *path,
const void *request, size_t len,
void *response, size_t len_max)
{
return _sock_put_post(sock, path, COAP_METHOD_PUT, request, len, response, len_max);
}
ssize_t nanocoap_sock_post(nanocoap_sock_t *sock, const char *path,
const void *request, size_t len,
void *response, size_t len_max)
{
return _sock_put_post(sock, path, COAP_METHOD_POST, request, len, response, len_max);
}
ssize_t nanocoap_request(coap_pkt_t *pkt, sock_udp_ep_t *local,

View File

@ -184,6 +184,7 @@ int nanotest_client_url_cmd(int argc, char **argv)
{
/* Ordered like the RFC method code numbers, but off by 1. GET is code 0. */
const char *method_codes[] = {"get", "post", "put"};
int res;
if (argc < 3) {
goto error;
@ -200,13 +201,44 @@ int nanotest_client_url_cmd(int argc, char **argv)
goto error;
}
if (code_pos != 0) {
switch (code_pos) {
case 0:
return nanocoap_get_blockwise_url(argv[2], COAP_BLOCKSIZE_32,
_blockwise_cb, NULL);
case 1:
case 2:
;
char response[32];
nanocoap_sock_t sock;
res = nanocoap_sock_url_connect(argv[2], &sock);
if (res) {
break;
}
if (code_pos == 1) {
res = nanocoap_sock_post(&sock, sock_urlpath(argv[2]),
argv[3], strlen(argv[3]),
response, sizeof(response));
} else {
res = nanocoap_sock_put(&sock, sock_urlpath(argv[2]),
argv[3], strlen(argv[3]),
response, sizeof(response));
}
nanocoap_sock_close(&sock);
if (res > 0) {
response[res] = 0;
printf("response: %s\n", response);
}
break;
default:
printf("TODO: implement %s request\n", method_codes[code_pos]);
return -1;
}
return nanocoap_get_blockwise_url(argv[2], COAP_BLOCKSIZE_32,
_blockwise_cb, NULL);
if (res) {
printf("res: %d\n", res);
}
return res;
error:
printf("usage: %s <get|post|put> <url> [data]\n", argv[0]);
return -1;