From 1cfcb307f2f16a3bcc03bb6e34e6de6ac31bff4c Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Wed, 28 Sep 2022 12:15:58 +0200 Subject: [PATCH] nanocoap_sock: add nanocoap_sock_{put, post}_non() --- sys/include/net/nanocoap_sock.h | 38 +++++++++++++++++++++++ sys/net/application_layer/nanocoap/sock.c | 35 ++++++++++++++++++--- 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/sys/include/net/nanocoap_sock.h b/sys/include/net/nanocoap_sock.h index 610b69db57..5bb0710042 100644 --- a/sys/include/net/nanocoap_sock.h +++ b/sys/include/net/nanocoap_sock.h @@ -240,6 +240,25 @@ 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 non-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 if the request was sent and no response buffer was provided, + * independently of success (because no response is requested in that case) + * @returns <0 on error + */ +ssize_t nanocoap_sock_put_non(nanocoap_sock_t *sock, const char *path, + const void *request, size_t len, + void *response, size_t len_max); + /** * @brief Simple synchronous CoAP (confirmable) PUT to URL * @@ -273,6 +292,25 @@ 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 Simple non-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 if the request was sent and no response buffer was provided, + * independently of success (because no response is requested in that case) + * @returns <0 on error + */ +ssize_t nanocoap_sock_post_non(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 to URL * diff --git a/sys/net/application_layer/nanocoap/sock.c b/sys/net/application_layer/nanocoap/sock.c index ff7667adcf..eecda7fe59 100644 --- a/sys/net/application_layer/nanocoap/sock.c +++ b/sys/net/application_layer/nanocoap/sock.c @@ -311,7 +311,7 @@ ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf, si } ssize_t _sock_put_post(nanocoap_sock_t *sock, const char *path, unsigned code, - const void *request, size_t len, + uint8_t type, const void *request, size_t len, void *response, size_t max_len) { /* buffer for CoAP header */ @@ -333,9 +333,15 @@ ssize_t _sock_put_post(nanocoap_sock_t *sock, const char *path, unsigned code, .iov_len = max_len, }; - pktpos += coap_build_hdr(pkt.hdr, COAP_TYPE_CON, NULL, 0, code, _get_id()); + pktpos += coap_build_hdr(pkt.hdr, type, NULL, 0, code, _get_id()); pktpos += coap_opt_put_uri_path(pktpos, 0, path); + if (response == NULL && type == COAP_TYPE_NON) { + /* all responses (2.xx, 4.xx and 5.xx) are ignored */ + pktpos += coap_opt_put_uint(pktpos, COAP_OPT_URI_PATH, + COAP_OPT_NO_RESPONSE, 26); + } + if (len) { /* set payload marker */ *pktpos++ = 0xFF; @@ -351,14 +357,32 @@ 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); + return _sock_put_post(sock, path, COAP_METHOD_PUT, COAP_TYPE_CON, 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); + return _sock_put_post(sock, path, COAP_METHOD_POST, COAP_TYPE_CON, request, len, + response, len_max); +} + +ssize_t nanocoap_sock_put_non(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, COAP_TYPE_NON, request, len, + response, len_max); +} + +ssize_t nanocoap_sock_post_non(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, COAP_TYPE_NON, request, len, + response, len_max); } static ssize_t _sock_put_post_url(const char *url, unsigned code, @@ -371,7 +395,8 @@ static ssize_t _sock_put_post_url(const char *url, unsigned code, return res; } - res = _sock_put_post(&sock, sock_urlpath(url), code, request, len, response, len_max); + res = _sock_put_post(&sock, sock_urlpath(url), code, COAP_TYPE_CON, + request, len, response, len_max); nanocoap_sock_close(&sock); return res;