Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nanocoap_sock: implement nanocoap_sock_delete() #18738

Merged
merged 2 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions sys/include/net/nanocoap_sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,27 @@ ssize_t nanocoap_sock_post_url(const char *url,
const void *request, size_t len,
void *response, size_t len_max);

/**
* @brief Simple synchronous CoAP (confirmable) DELETE
*
* @param[in] sock socket to use for the request
* @param[in] path remote path to delete
*
* @returns 0 on success
* @returns <0 on error
*/
ssize_t nanocoap_sock_delete(nanocoap_sock_t *sock, const char *path);

/**
* @brief Simple synchronous CoAP (confirmable) DELETE for URL
*
* @param[in] url URL of the resource that should be deleted
*
* @returns 0 on success
* @returns <0 on error
*/
ssize_t nanocoap_sock_delete_url(const char *url);

/**
* @brief Performs a blockwise coap get request on a socket.
*
Expand Down
32 changes: 32 additions & 0 deletions sys/net/application_layer/nanocoap/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,38 @@ ssize_t nanocoap_sock_post_url(const char *url,
return _sock_put_post_url(url, COAP_METHOD_POST, request, len, response, len_max);
}

ssize_t nanocoap_sock_delete(nanocoap_sock_t *sock, const char *path)
{
/* buffer for CoAP header */
uint8_t buffer[CONFIG_NANOCOAP_BLOCK_HEADER_MAX];
uint8_t *pktpos = buffer;

coap_pkt_t pkt = {
.hdr = (void *)pktpos,
};

pktpos += coap_build_hdr(pkt.hdr, COAP_TYPE_CON, NULL, 0, COAP_METHOD_DELETE, _get_id());
pktpos += coap_opt_put_uri_path(pktpos, 0, path);

pkt.payload = pktpos;

return nanocoap_sock_request_cb(sock, &pkt, NULL, NULL);
}

ssize_t nanocoap_sock_delete_url(const char *url)
{
nanocoap_sock_t sock;
int res = nanocoap_sock_url_connect(url, &sock);
if (res) {
return res;
}

res = nanocoap_sock_delete(&sock, sock_urlpath(url));
nanocoap_sock_close(&sock);

return res;
}

ssize_t nanocoap_request(coap_pkt_t *pkt, const sock_udp_ep_t *local,
const sock_udp_ep_t *remote, size_t len)
{
Expand Down
18 changes: 11 additions & 7 deletions tests/nanocoap_cli/nanocli_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ static int _blockwise_cb(void *arg, size_t offset, uint8_t *buf, size_t len, int
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"};
const char *method_codes[] = { "get", "post", "put", "delete" };
int res;

if (argc < 3) {
Expand All @@ -202,11 +202,12 @@ int nanotest_client_url_cmd(int argc, char **argv)
}

switch (code_pos) {
case 0:
return nanocoap_get_blockwise_url(argv[2], COAP_BLOCKSIZE_32,
_blockwise_cb, NULL);
case 1:
case 2:
case COAP_METHOD_GET - 1:
res = nanocoap_get_blockwise_url(argv[2], COAP_BLOCKSIZE_32,
_blockwise_cb, NULL);
break;
case COAP_METHOD_POST - 1:
case COAP_METHOD_PUT - 1:
;
char response[32];
nanocoap_sock_t sock;
Expand All @@ -229,6 +230,9 @@ int nanotest_client_url_cmd(int argc, char **argv)
printf("response: %s\n", response);
}
break;
case COAP_METHOD_DELETE - 1:
res = nanocoap_sock_delete_url(argv[2]);
break;
default:
printf("TODO: implement %s request\n", method_codes[code_pos]);
return -1;
Expand All @@ -240,7 +244,7 @@ int nanotest_client_url_cmd(int argc, char **argv)
return res;

error:
printf("usage: %s <get|post|put> <url> [data]\n", argv[0]);
printf("usage: %s <get|post|put|delete> <url> [data]\n", argv[0]);
return -1;
}

Expand Down