From 0b768e560245c0daae9c2e0dcd80f5ef0642f1e0 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Fri, 28 Jan 2022 17:57:45 +0100 Subject: [PATCH] nanocoap: add nanocoap_request_simple() --- sys/include/net/nanocoap_sock.h | 18 +++++++++++++++++- sys/net/application_layer/nanocoap/sock.c | 23 ++++++++++++++++++++--- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/sys/include/net/nanocoap_sock.h b/sys/include/net/nanocoap_sock.h index 70cfc250e5..f54a5e672a 100644 --- a/sys/include/net/nanocoap_sock.h +++ b/sys/include/net/nanocoap_sock.h @@ -245,7 +245,23 @@ int nanocoap_get_blockwise_url(const char *url, * @returns length of response on success * @returns <0 on error */ -ssize_t nanocoap_request(sock_udp_t *sock, coap_pkt_t *pkt, size_t len); +ssize_t nanocoap_sock_request(sock_udp_t *sock, coap_pkt_t *pkt, size_t len); + +/** + * @brief Simple synchronous CoAP request + * + * @param[in,out] pkt Packet struct containing the request. Is reused for + * the response + * @param[in] local Local UDP endpoint, may be NULL + * @param[in] remote remote UDP endpoint + * @param[in] len Total length of the buffer associated with the + * request + * + * @returns length of response on success + * @returns <0 on error + */ +ssize_t nanocoap_request(coap_pkt_t *pkt, sock_udp_ep_t *local, + sock_udp_ep_t *remote, size_t len); #ifdef __cplusplus } diff --git a/sys/net/application_layer/nanocoap/sock.c b/sys/net/application_layer/nanocoap/sock.c index a3f50f696f..cc18fd906d 100644 --- a/sys/net/application_layer/nanocoap/sock.c +++ b/sys/net/application_layer/nanocoap/sock.c @@ -41,7 +41,7 @@ int nanocoap_connect(sock_udp_t *sock, sock_udp_ep_t *local, sock_udp_ep_t *remo return sock_udp_create(sock, local, remote, 0); } -ssize_t nanocoap_request(sock_udp_t *sock, coap_pkt_t *pkt, size_t len) +ssize_t nanocoap_sock_request(sock_udp_t *sock, coap_pkt_t *pkt, size_t len) { ssize_t res = -EAGAIN; size_t pdu_len = (pkt->payload - (uint8_t *)pkt->hdr) + pkt->payload_len; @@ -110,6 +110,23 @@ ssize_t nanocoap_request(sock_udp_t *sock, coap_pkt_t *pkt, size_t len) return res; } +ssize_t nanocoap_request(coap_pkt_t *pkt, sock_udp_ep_t *local, + sock_udp_ep_t *remote, size_t len) +{ + int res; + sock_udp_t sock; + + res = nanocoap_connect(&sock, local, remote); + if (res) { + return res; + } + + res = nanocoap_sock_request(&sock, pkt, len); + nanocoap_close(&sock); + + return res; +} + ssize_t nanocoap_get(sock_udp_t *sock, const char *path, void *buf, size_t len) { ssize_t res; @@ -122,7 +139,7 @@ ssize_t nanocoap_get(sock_udp_t *sock, const char *path, void *buf, size_t len) pkt.payload = pktpos; pkt.payload_len = 0; - res = nanocoap_request(sock, &pkt, len); + res = nanocoap_sock_request(sock, &pkt, len); if (res < 0) { return res; } @@ -158,7 +175,7 @@ static int _fetch_block(coap_pkt_t *pkt, uint8_t *buf, sock_udp_t *sock, pkt->payload = pktpos; pkt->payload_len = 0; - int res = nanocoap_request(sock, pkt, NANOCOAP_BLOCKWISE_BUF(blksize)); + int res = nanocoap_sock_request(sock, pkt, NANOCOAP_BLOCKWISE_BUF(blksize)); if (res < 0) { return res; }