Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
18134: nanocoap_link_format: add helper function to parse Link Format r=kaspar030 a=benpicco



18738: nanocoap_sock: implement nanocoap_sock_delete() r=maribu a=benpicco



18939: gnrc_ipv6_nib: clean up _resolve_addr() r=maribu a=benpicco



19118: sys/ztimer: ztimer_mock: guard ztimer_ondemand static functions r=kaspar030 a=kaspar030



Co-authored-by: Benjamin Valentin <[email protected]>
Co-authored-by: Benjamin Valentin <[email protected]>
Co-authored-by: Benjamin Valentin <[email protected]>
Co-authored-by: Kaspar Schleiser <[email protected]>
  • Loading branch information
5 people authored Jan 10, 2023
5 parents de0bd97 + f06c763 + 794fa43 + 64a2fbc + ccc3c93 commit 53176f7
Show file tree
Hide file tree
Showing 11 changed files with 376 additions and 136 deletions.
4 changes: 4 additions & 0 deletions sys/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,10 @@ ifneq (,$(filter nanocoap_cache,$(USEMODULE)))
USEMODULE += hashes
endif

ifneq (,$(filter nanocoap_link_format,$(USEMODULE)))
USEMODULE += fmt
endif

ifneq (,$(filter nanocoap_vfs,$(USEMODULE)))
USEMODULE += nanocoap_sock
USEMODULE += vfs
Expand Down
72 changes: 72 additions & 0 deletions sys/include/net/nanocoap/link_format.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (C) 2022 ML!PA Consulting GmbH
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @ingroup net_nanosock
* @brief NanoCoAP Link Format helper functions
*
* @{
*
* @file
* @brief NanoCoAP Link Format ([RFC 6690](https://www.rfc-editor.org/rfc/rfc6690.html))
* helper functions
*
* @author Benjamin Valentin <[email protected]>
*/
#ifndef NET_NANOCOAP_LINK_FORMAT_H
#define NET_NANOCOAP_LINK_FORMAT_H

#include "net/nanocoap_sock.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief Callback function called for each resource on the directory
*
* @param[in] entry Resource entry from the server
* @param[in] ctx Optional function context
*
* @returns 0 on success
* @returns <0 on error
*/
typedef int (*coap_link_format_handler_t)(char *entry, void *ctx);

/**
* @brief Downloads the resource behind @p path via blockwise GET
*
* @param[in] sock Connection to the server
* @param[in] path path of the resource
* @param[in] cb Callback to execute for each resource entry
* @param[in] arg Optional callback argument
*
* @returns 0 on success
* @returns <0 on error
*/
int nanocoap_link_format_get(nanocoap_sock_t *sock, const char *path,
coap_link_format_handler_t cb, void *arg);

/**
* @brief Downloads the resource behind @p url via blockwise GET
*
* @param[in] url URL to the resource
* @param[in] cb Callback to execute for each resource entry
* @param[in] arg Optional callback argument
*
* @returns 0 on success
* @returns <0 on error
*/
int nanocoap_link_format_get_url(const char *url,
coap_link_format_handler_t cb, void *arg);

#ifdef __cplusplus
}
#endif
#endif /* NET_NANOCOAP_LINK_FORMAT_H */
/** @} */
21 changes: 21 additions & 0 deletions sys/include/net/nanocoap_sock.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,27 @@ ssize_t nanocoap_sock_post_url(const char *url,
const void *request, size_t len,
void *response, size_t len_max);

/**
* @brief Simple synchronous CoAP (confirmable) DELETE
*
* @param[in] sock socket to use for the request
* @param[in] path remote path to delete
*
* @returns 0 on success
* @returns <0 on error
*/
ssize_t nanocoap_sock_delete(nanocoap_sock_t *sock, const char *path);

/**
* @brief Simple synchronous CoAP (confirmable) DELETE for URL
*
* @param[in] url URL of the resource that should be deleted
*
* @returns 0 on success
* @returns <0 on error
*/
ssize_t nanocoap_sock_delete_url(const char *url);

/**
* @brief Performs a blockwise coap get request on a socket.
*
Expand Down
109 changes: 109 additions & 0 deletions sys/net/application_layer/nanocoap/link_format.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright (C) 2022 ML!PA Consulting GmbH
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @ingroup net_nanocoap
* @{
*
* @file
* @brief NanoCoAP Link Format parser
*
* @author Benjamin Valentin <[email protected]>
*
* @}
*/

#include "fmt.h"
#include "net/nanocoap/link_format.h"
#include "net/nanocoap_sock.h"
#include "net/sock/util.h"

struct dir_list_ctx {
char *buf;
char *cur;
char *end;
coap_link_format_handler_t cb;
void *ctx;
char esc_buf[2];
uint8_t esc_idx;
};

static int _dirlist_cb(void *arg, size_t offset, uint8_t *buf, size_t len, int more)
{
(void)offset;

struct dir_list_ctx *ctx = arg;

char *end = (char *)buf + len;
for (char *c = (char *)buf; c != end; ++c) {

/* start of escape sequence */
if (*c == '%') {
ctx->esc_idx = 1;
continue;
}
if (ctx->esc_idx) {
/* fill escape buffer */
ctx->esc_buf[ctx->esc_idx - 1] = *c;
if (++ctx->esc_idx == 3) {
ctx->esc_idx = 0;
*c = scn_u32_hex(ctx->esc_buf, 2);
} else {
continue;
}
}

if (*c == ',' || ctx->cur == ctx->end) {
int res;
*ctx->cur = 0;
res = ctx->cb(ctx->buf, ctx->ctx);
ctx->cur = ctx->buf;
if (res < 0) {
return res;
}
} else {
*ctx->cur++ = *c;
}
}

if (!more) {
*ctx->cur = 0;
return ctx->cb(ctx->buf, ctx->ctx);
}

return 0;
}

int nanocoap_link_format_get(nanocoap_sock_t *sock, const char *path,
coap_link_format_handler_t cb, void *arg)
{
char buffer[CONFIG_NANOCOAP_QS_MAX];
struct dir_list_ctx ctx = {
.buf = buffer,
.end = buffer + sizeof(buffer),
.cur = buffer,
.cb = cb,
.ctx = arg,
};
return nanocoap_sock_get_blockwise(sock, path, CONFIG_NANOCOAP_BLOCKSIZE_DEFAULT,
_dirlist_cb, &ctx);
}

int nanocoap_link_format_get_url(const char *url, coap_link_format_handler_t cb, void *arg)
{
nanocoap_sock_t sock;
int res = nanocoap_sock_url_connect(url, &sock);
if (res) {
return res;
}

res = nanocoap_link_format_get(&sock, sock_urlpath(url), cb, arg);
nanocoap_sock_close(&sock);

return res;
}
32 changes: 32 additions & 0 deletions sys/net/application_layer/nanocoap/sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,38 @@ ssize_t nanocoap_sock_post_url(const char *url,
return _sock_put_post_url(url, COAP_METHOD_POST, request, len, response, len_max);
}

ssize_t nanocoap_sock_delete(nanocoap_sock_t *sock, const char *path)
{
/* buffer for CoAP header */
uint8_t buffer[CONFIG_NANOCOAP_BLOCK_HEADER_MAX];
uint8_t *pktpos = buffer;

coap_pkt_t pkt = {
.hdr = (void *)pktpos,
};

pktpos += coap_build_hdr(pkt.hdr, COAP_TYPE_CON, NULL, 0, COAP_METHOD_DELETE, _get_id());
pktpos += coap_opt_put_uri_path(pktpos, 0, path);

pkt.payload = pktpos;

return nanocoap_sock_request_cb(sock, &pkt, NULL, NULL);
}

ssize_t nanocoap_sock_delete_url(const char *url)
{
nanocoap_sock_t sock;
int res = nanocoap_sock_url_connect(url, &sock);
if (res) {
return res;
}

res = nanocoap_sock_delete(&sock, sock_urlpath(url));
nanocoap_sock_close(&sock);

return res;
}

ssize_t nanocoap_request(coap_pkt_t *pkt, const sock_udp_ep_t *local,
const sock_udp_ep_t *remote, size_t len)
{
Expand Down
2 changes: 1 addition & 1 deletion sys/net/gnrc/network_layer/ipv6/nib/_nib-arsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ bool _is_reachable(_nib_onl_entry_t *entry);
#define _set_reachable(netif, nce) (void)netif; (void)nce

#define _get_nud_state(nbr) (GNRC_IPV6_NIB_NC_INFO_NUD_STATE_UNMANAGED)
#define _set_nud_state(netif, nce, state) (void)netif; (void)nbr; (void)state
#define _set_nud_state(netif, nce, state) (void)netif; (void)nce; (void)state
#define _is_reachable(entry) (true)
#endif /* CONFIG_GNRC_IPV6_NIB_ARSM || defined(DOXYGEN) */

Expand Down
Loading

0 comments on commit 53176f7

Please sign in to comment.