diff --git a/sys/include/net/nanocoap_sock.h b/sys/include/net/nanocoap_sock.h index e930eac14668a..c57c2146126c5 100644 --- a/sys/include/net/nanocoap_sock.h +++ b/sys/include/net/nanocoap_sock.h @@ -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,21 @@ 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, out] buf buffer containing the payload, will be used for response + * @param[in] len length of the payload to send + * @param[in] len_max length of @p buffer + * + * @returns length of response payload on success + * @returns <0 on error + */ +ssize_t nanocoap_sock_put(nanocoap_sock_t *sock, const char *path, void *buf, + size_t len, size_t len_max); + /** * @brief Performs a blockwise coap get request on a socket. * diff --git a/sys/net/application_layer/nanocoap/sock.c b/sys/net/application_layer/nanocoap/sock.c index f7d8cb171e58a..dd31c2714c89d 100644 --- a/sys/net/application_layer/nanocoap/sock.c +++ b/sys/net/application_layer/nanocoap/sock.c @@ -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; @@ -298,25 +298,48 @@ static int _get_cb(void *arg, coap_pkt_t *pkt) return pkt->payload_len; } +static ssize_t _sock_req_simple(nanocoap_sock_t *sock, const char *path, unsigned code, + coap_pkt_t *pkt, size_t buffer_len) +{ + uint8_t *pktpos = (void *)pkt->hdr; + + struct iovec ctx = { + .iov_base = &pkt->hdr, + .iov_len = buffer_len, + }; + + pktpos += coap_build_hdr(pkt->hdr, COAP_TYPE_CON, NULL, 0, code, _get_id()); + pktpos += coap_opt_put_uri_path(pktpos, 0, path); + + pkt->payload = pktpos; + pkt->payload_len = 0; + + 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) { - uint8_t *pktpos = buf; coap_pkt_t pkt = { .hdr = buf, }; - struct iovec ctx = { - .iov_base = buf, - .iov_len = len, - }; + return _sock_req_simple(sock, path, COAP_METHOD_GET, &pkt, len); +} - pktpos += coap_build_hdr(pkt.hdr, COAP_TYPE_CON, NULL, 0, COAP_METHOD_GET, _get_id()); - pktpos += coap_opt_put_uri_path(pktpos, 0, path); +ssize_t nanocoap_sock_put(nanocoap_sock_t *sock, const char *path, + void *buf, size_t len, size_t len_max) +{ + iolist_t payload = { + .iol_base = buf, + .iol_len = len, + }; - pkt.payload = pktpos; - pkt.payload_len = 0; + coap_pkt_t pkt = { + .hdr = buf, + .snips = &payload, + }; - return nanocoap_sock_request_cb(sock, &pkt, _get_cb, &ctx); + return _sock_req_simple(sock, path, COAP_METHOD_PUT, &pkt, len_max); } ssize_t nanocoap_request(coap_pkt_t *pkt, sock_udp_ep_t *local,