diff --git a/sys/include/net/nanocoap_sock.h b/sys/include/net/nanocoap_sock.h index 65ef78cdb4..b75b91b52f 100644 --- a/sys/include/net/nanocoap_sock.h +++ b/sys/include/net/nanocoap_sock.h @@ -389,6 +389,20 @@ 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 non-confirmable GET + * + * @param[in] sock socket to use for the request + * @param[in] path remote path and query + * @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_get_non(nanocoap_sock_t *sock, const char *path, + void *response, size_t len_max); + /** * @brief Simple synchronous CoAP (confirmable) PUT * diff --git a/sys/net/application_layer/nanocoap/sock.c b/sys/net/application_layer/nanocoap/sock.c index 9101b0b13c..0b8343523e 100644 --- a/sys/net/application_layer/nanocoap/sock.c +++ b/sys/net/application_layer/nanocoap/sock.c @@ -390,7 +390,9 @@ static int _get_put_cb(void *arg, coap_pkt_t *pkt) return pkt->payload_len; } -ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf, size_t len) +static ssize_t _sock_get(nanocoap_sock_t *sock, const char *path, + uint8_t type, + void *response, size_t max_len) { /* buffer for CoAP header */ uint8_t buffer[CONFIG_NANOCOAP_BLOCK_HEADER_MAX]; @@ -401,11 +403,11 @@ ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf, si }; struct iovec ctx = { - .iov_base = buf, - .iov_len = len, + .iov_base = response, + .iov_len = max_len, }; - pktpos += coap_build_hdr(pkt.hdr, COAP_TYPE_CON, NULL, 0, COAP_METHOD_GET, + pktpos += coap_build_hdr(pkt.hdr, type, NULL, 0, COAP_METHOD_GET, nanocoap_sock_next_msg_id(sock)); pktpos += coap_opt_put_uri_pathquery(pktpos, NULL, path); @@ -415,6 +417,11 @@ ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf, si return nanocoap_sock_request_cb(sock, &pkt, _get_put_cb, &ctx); } +ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf, size_t len) +{ + return _sock_get(sock, path, COAP_TYPE_CON, buf, len); +} + ssize_t _sock_put_post(nanocoap_sock_t *sock, const char *path, unsigned code, uint8_t type, const void *request, size_t len, void *response, size_t max_len) @@ -507,6 +514,12 @@ ssize_t nanocoap_sock_fetch_non(nanocoap_sock_t *sock, const char *path, response, len_max); } +ssize_t nanocoap_sock_get_non(nanocoap_sock_t *sock, const char *path, + void *response, size_t len_max) +{ + return _sock_get(sock, path, COAP_TYPE_NON, response, len_max); +} + static ssize_t _sock_put_post_url(const char *url, unsigned code, const void *request, size_t len, void *response, size_t len_max)