1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

sys/net/nanocoap: introduce nanocoap_sock_*()

Co-authored-by: Jan Romann <jan.romann@uni-bremen.de>
This commit is contained in:
benpicco 2022-02-10 18:47:05 +01:00 committed by Benjamin Valentin
parent e56d744069
commit 67b98fd741
3 changed files with 75 additions and 39 deletions

View File

@ -888,7 +888,7 @@ int coap_get_blockopt(coap_pkt_t *pkt, uint16_t option, uint32_t *blknum, unsign
* @brief Check whether any of the packet's options that are critical
*
* (i.e must be understood by the receiver, indicated by a 1 in the option number's least
* significant bit) were not accessed since the packet was parsed.)
* significant bit) were not accessed since the packet was parsed.
*
* Call this in a server on requests after all their option processing has happened,
* and stop processing the request if it returns true, returning a 4.02 Bad Option response.

View File

@ -139,6 +139,12 @@
extern "C" {
#endif
/**
* @brief nanocoap socket type
*
*/
typedef sock_udp_t nanocoap_sock_t;
/**
* @brief Start a nanocoap server instance
*
@ -163,15 +169,15 @@ int nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize);
* @returns 0 on success
* @returns <0 on error
*/
int nanocoap_connect(sock_udp_t *sock, sock_udp_ep_t *local,
sock_udp_ep_t *remote);
int nanocoap_sock_connect(nanocoap_sock_t *sock, sock_udp_ep_t *local,
sock_udp_ep_t *remote);
/**
* @brief Close a CoAP client socket
*
* @param[in] sock CoAP UDP socket
*/
static inline void nanocoap_close(sock_udp_t *sock)
static inline void nanocoap_sock_close(nanocoap_sock_t *sock)
{
sock_udp_close(sock);
}
@ -187,8 +193,8 @@ static inline void nanocoap_close(sock_udp_t *sock)
* @returns length of response payload on success
* @returns <0 on error
*/
ssize_t nanocoap_get(sock_udp_t *sock, const char *path, void *buf,
size_t len);
ssize_t nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf,
size_t len);
/**
* @brief Performs a blockwise coap get request on a socket.
@ -208,9 +214,9 @@ ssize_t nanocoap_get(sock_udp_t *sock, const char *path, void *buf,
* @returns -1 if failed to fetch the url content
* @returns 0 on success
*/
int nanocoap_get_blockwise(sock_udp_t *sock, const char *path,
coap_blksize_t blksize, void *work_buf,
coap_blockwise_cb_t callback, void *arg);
int nanocoap_sock_get_blockwise(nanocoap_sock_t *sock, const char *path,
coap_blksize_t blksize, void *work_buf,
coap_blockwise_cb_t callback, void *arg);
/**
* @brief Performs a blockwise coap get request to the specified url.
@ -219,7 +225,8 @@ int nanocoap_get_blockwise(sock_udp_t *sock, const char *path,
* block-wise-transfer. A coap_blockwise_cb_t will be called on each received
* block.
*
* @param[in] url Absolute URL pointer to source path (i.e. not containing a fragment identifier)
* @param[in] url Absolute URL pointer to source path (i.e. not containing
* a fragment identifier)
* @param[in] blksize sender suggested SZX for the COAP block request
* @param[in] work_buf Work buffer, must be `NANOCOAP_BLOCKWISE_BUF(blksize)` bytes
* @param[in] callback callback to be executed on each received block
@ -245,7 +252,7 @@ int nanocoap_get_blockwise_url(const char *url,
* @returns length of response on success
* @returns <0 on error
*/
ssize_t nanocoap_sock_request(sock_udp_t *sock, coap_pkt_t *pkt, size_t len);
ssize_t nanocoap_sock_request(nanocoap_sock_t *sock, coap_pkt_t *pkt, size_t len);
/**
* @brief Simple synchronous CoAP request
@ -263,6 +270,20 @@ ssize_t nanocoap_sock_request(sock_udp_t *sock, coap_pkt_t *pkt, size_t len);
ssize_t nanocoap_request(coap_pkt_t *pkt, sock_udp_ep_t *local,
sock_udp_ep_t *remote, size_t len);
/**
* @brief Simple synchronous CoAP (confirmable) get
*
* @param[in] remote remote UDP endpoint
* @param[in] path remote path
* @param[out] buf buffer to write response to
* @param[in] len length of @p buffer
*
* @returns length of response payload on success
* @returns <0 on error
*/
ssize_t nanocoap_get(sock_udp_ep_t *remote, const char *path, void *buf,
size_t len);
#ifdef __cplusplus
}
#endif

View File

@ -32,7 +32,7 @@
#define ENABLE_DEBUG 0
#include "debug.h"
int nanocoap_connect(sock_udp_t *sock, sock_udp_ep_t *local, sock_udp_ep_t *remote)
int nanocoap_sock_connect(nanocoap_sock_t *sock, sock_udp_ep_t *local, sock_udp_ep_t *remote)
{
if (!remote->port) {
remote->port = COAP_PORT;
@ -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_sock_request(sock_udp_t *sock, coap_pkt_t *pkt, size_t len)
ssize_t nanocoap_sock_request(nanocoap_sock_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;
@ -78,7 +78,6 @@ ssize_t nanocoap_sock_request(sock_udp_t *sock, coap_pkt_t *pkt, size_t len)
if (res == -ETIMEDOUT) {
DEBUG("nanocoap: timeout\n");
timeout *= 2;
tries_left--;
if (!tries_left) {
DEBUG("nanocoap: maximum retries reached\n");
@ -110,24 +109,7 @@ ssize_t nanocoap_sock_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 nanocoap_sock_get(nanocoap_sock_t *sock, const char *path, void *buf, size_t len)
{
ssize_t res;
coap_pkt_t pkt;
@ -158,7 +140,40 @@ ssize_t nanocoap_get(sock_udp_t *sock, const char *path, void *buf, size_t len)
return res;
}
static int _fetch_block(coap_pkt_t *pkt, uint8_t *buf, sock_udp_t *sock,
ssize_t nanocoap_request(coap_pkt_t *pkt, sock_udp_ep_t *local,
sock_udp_ep_t *remote, size_t len)
{
int res;
nanocoap_sock_t sock;
res = nanocoap_sock_connect(&sock, local, remote);
if (res) {
return res;
}
res = nanocoap_sock_request(&sock, pkt, len);
nanocoap_sock_close(&sock);
return res;
}
ssize_t nanocoap_get(sock_udp_ep_t *remote, const char *path, void *buf, size_t len)
{
int res;
nanocoap_sock_t sock;
res = nanocoap_sock_connect(&sock, NULL, remote);
if (res) {
return res;
}
res = nanocoap_sock_get(&sock, path, buf, len);
nanocoap_sock_close(&sock);
return res;
}
static int _fetch_block(coap_pkt_t *pkt, uint8_t *buf, nanocoap_sock_t *sock,
const char *path, coap_blksize_t blksize, size_t num)
{
uint8_t *pktpos = buf;
@ -189,7 +204,7 @@ static int _fetch_block(coap_pkt_t *pkt, uint8_t *buf, sock_udp_t *sock,
return 0;
}
int nanocoap_get_blockwise(sock_udp_t *sock, const char *path,
int nanocoap_sock_get_blockwise(nanocoap_sock_t *sock, const char *path,
coap_blksize_t blksize, void *buf,
coap_blockwise_cb_t callback, void *arg)
{
@ -235,7 +250,7 @@ int nanocoap_get_blockwise_url(const char *url,
char hostport[CONFIG_SOCK_HOSTPORT_MAXLEN];
char urlpath[CONFIG_SOCK_URLPATH_MAXLEN];
sock_udp_ep_t remote;
sock_udp_t sock;
nanocoap_sock_t sock;
int res;
if (strncmp(url, "coap://", 7)) {
@ -253,20 +268,20 @@ int nanocoap_get_blockwise_url(const char *url,
return -EINVAL;
}
res = nanocoap_connect(&sock, NULL, &remote);
res = nanocoap_sock_connect(&sock, NULL, &remote);
if (res) {
return res;
}
res = nanocoap_get_blockwise(&sock, urlpath, blksize, buf, callback, arg);
nanocoap_close(&sock);
res = nanocoap_sock_get_blockwise(&sock, urlpath, blksize, buf, callback, arg);
nanocoap_sock_close(&sock);
return res;
}
int nanocoap_server(sock_udp_ep_t *local, uint8_t *buf, size_t bufsize)
{
sock_udp_t sock;
nanocoap_sock_t sock;
sock_udp_ep_t remote;
if (!local->port) {