Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sys/net/*coap: Make APIs (more) transport agnostic #20900

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
10 changes: 5 additions & 5 deletions examples/gcoap/client.c
Original file line number Diff line number Diff line change
@@ -134,16 +134,16 @@ static void _resp_handler(const gcoap_request_memo_t *memo, coap_pkt_t* pdu,
uri_parser_result_t urip;
uri_parser_process(&urip, _last_req_uri, strlen(_last_req_uri));
if (*_proxy_uri) {
gcoap_req_init(pdu, (uint8_t *)pdu->hdr, CONFIG_GCOAP_PDU_BUF_SIZE,
gcoap_req_init(pdu, pdu->buf, CONFIG_GCOAP_PDU_BUF_SIZE,
COAP_METHOD_GET, NULL);
}
else {
gcoap_req_init(pdu, (uint8_t *)pdu->hdr, CONFIG_GCOAP_PDU_BUF_SIZE,
gcoap_req_init(pdu, pdu->buf, CONFIG_GCOAP_PDU_BUF_SIZE,
COAP_METHOD_GET, urip.path);
}

if (msg_type == COAP_TYPE_ACK) {
coap_hdr_set_type(pdu->hdr, COAP_TYPE_CON);
coap_pkt_set_type(pdu, COAP_TYPE_CON);
}
block.blknum++;
coap_opt_add_block2_control(pdu, &block);
@@ -154,7 +154,7 @@ static void _resp_handler(const gcoap_request_memo_t *memo, coap_pkt_t* pdu,

int len = coap_opt_finish(pdu, COAP_OPT_FINISH_NONE);
gcoap_socket_type_t tl = _get_tl(*_proxy_uri ? _proxy_uri : _last_req_uri);
_send((uint8_t *)pdu->hdr, len, remote, memo->context, tl);
_send(pdu->buf, len, remote, memo->context, tl);
}
else {
puts("--- blockwise complete ---");
@@ -341,7 +341,7 @@ static int _cli_cmd(int argc, char **argv)
}
}

coap_hdr_set_type(pdu.hdr, msg_type);
coap_pkt_set_type(&pdu, msg_type);

size_t paylen = 0;
if (apos < argc) {
2 changes: 1 addition & 1 deletion examples/gcoap/server.c
Original file line number Diff line number Diff line change
@@ -65,7 +65,7 @@
static ssize_t _encode_link(const coap_resource_t *resource, char *buf,
size_t maxlen, coap_link_encoder_ctx_t *context);
static ssize_t _stats_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, coap_request_ctx_t *ctx);
static ssize_t _riot_board_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, coap_request_ctx_t *ctx);

Check warning on line 68 in examples/gcoap/server.c

GitHub Actions / static-tests

line is longer than 100 characters
#if IS_USED(MODULE_PERIPH_RTC)
static ssize_t _rtc_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, coap_request_ctx_t *ctx);
#endif
@@ -93,7 +93,7 @@
NULL
};


Check warning on line 96 in examples/gcoap/server.c

GitHub Actions / static-tests

too many consecutive empty lines
/* Adds link format params to resource list */
static ssize_t _encode_link(const coap_resource_t *resource, char *buf,
size_t maxlen, coap_link_encoder_ctx_t *context) {
@@ -123,7 +123,7 @@
}
size_t len;
char str_time[20] = "";
uint8_t buf[sizeof(coap_hdr_t) + COAP_TOKEN_LENGTH_MAX + 1 + sizeof(str_time)];
uint8_t buf[sizeof(coap_udp_hdr_t) + COAP_TOKEN_LENGTH_MAX + 1 + sizeof(str_time)];
coap_pkt_t pdu;
const coap_resource_t *rtc_resource = NULL;
const gcoap_listener_t *listener = NULL;
@@ -136,7 +136,7 @@
switch (gcoap_obs_init(&pdu, buf, sizeof(buf), rtc_resource)) {
case GCOAP_OBS_INIT_OK:
len = coap_opt_finish(&pdu, COAP_OPT_FINISH_PAYLOAD);
memcpy(pdu.payload, str_time, strftime(str_time, sizeof(str_time), "%Y-%m-%d %H:%M:%S", &tm_now));

Check warning on line 139 in examples/gcoap/server.c

GitHub Actions / static-tests

line is longer than 100 characters
pdu.payload_len = strlen(str_time);
len += pdu.payload_len;
if (!gcoap_obs_send(buf, len, rtc_resource)) {
@@ -161,7 +161,7 @@
gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
size_t resp_len = coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD);
char str_time[20] = "";
memcpy(pdu->payload, str_time, strftime(str_time, sizeof(str_time), "%Y-%m-%d %H:%M:%S", &tm_now));

Check warning on line 164 in examples/gcoap/server.c

GitHub Actions / static-tests

line is longer than 100 characters
pdu->payload_len = strlen(str_time);
resp_len += pdu->payload_len;
return resp_len;
@@ -210,7 +210,7 @@
return 0;
}

static ssize_t _riot_board_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len, coap_request_ctx_t *ctx)

Check warning on line 213 in examples/gcoap/server.c

GitHub Actions / static-tests

line is longer than 100 characters
{
(void)ctx;
gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
@@ -272,7 +272,7 @@
gcoap_register_listener(&_listener);
#if IS_USED(MODULE_PERIPH_RTC)
static event_periodic_callback_t _ev_pcb_rtc;
event_periodic_callback_init(&_ev_pcb_rtc, ZTIMER_MSEC, EVENT_PRIO_MEDIUM, _rtc_notify_observers, NULL);

Check warning on line 275 in examples/gcoap/server.c

GitHub Actions / static-tests

line is longer than 100 characters
event_periodic_callback_start(&_ev_pcb_rtc, 10 * MS_PER_SEC);
#endif
}
6 changes: 3 additions & 3 deletions examples/nanocoap_server/coap_handler.c
Original file line number Diff line number Diff line change
@@ -15,9 +15,9 @@
#include "event/thread.h"
#include "event/timeout.h"
#include "fmt.h"
#include "hashes/sha256.h"
#include "net/nanocoap.h"
#include "net/nanocoap_sock.h"
#include "hashes/sha256.h"

/* internal value that can be read/written via CoAP */
static uint8_t internal_value = 0;
@@ -41,14 +41,14 @@
(uint8_t *)sub_uri, sub_uri_len);
}

static ssize_t _riot_board_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, coap_request_ctx_t *context)

Check warning on line 44 in examples/nanocoap_server/coap_handler.c

GitHub Actions / static-tests

line is longer than 100 characters
{
(void)context;
return coap_reply_simple(pkt, COAP_CODE_205, buf, len,
COAP_FORMAT_TEXT, (uint8_t*)RIOT_BOARD, strlen(RIOT_BOARD));
}

static ssize_t _riot_block2_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, coap_request_ctx_t *context)

Check warning on line 51 in examples/nanocoap_server/coap_handler.c

GitHub Actions / static-tests

line is longer than 100 characters
{
(void)context;
coap_block_slicer_t slicer;
@@ -158,7 +158,7 @@
return reply_len;
}

uint8_t *pkt_pos = (uint8_t*)pkt->hdr + reply_len;
uint8_t *pkt_pos = pkt->buf + reply_len;
if (blockwise) {
pkt_pos += coap_opt_put_block1_control(pkt_pos, 0, &block1);
}
@@ -167,7 +167,7 @@
pkt_pos += fmt_bytes_hex((char *)pkt_pos, digest, sizeof(digest));
}

return pkt_pos - (uint8_t*)pkt->hdr;
return pkt_pos - pkt->buf;
}

NANOCOAP_RESOURCE(echo) {
@@ -177,7 +177,7 @@
.path = "/riot/board", .methods = COAP_GET, .handler = _riot_board_handler
};
NANOCOAP_RESOURCE(value) {
.path = "/riot/value", .methods = COAP_GET | COAP_PUT | COAP_POST, .handler = _riot_value_handler

Check warning on line 180 in examples/nanocoap_server/coap_handler.c

GitHub Actions / static-tests

line is longer than 100 characters
};
NANOCOAP_RESOURCE(ver) {
.path = "/riot/ver", .methods = COAP_GET, .handler = _riot_block2_handler
@@ -199,7 +199,7 @@
response, sizeof(response));
}

static ssize_t _separate_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, coap_request_ctx_t *context)

Check warning on line 202 in examples/nanocoap_server/coap_handler.c

GitHub Actions / static-tests

line is longer than 100 characters
{
static event_timeout_t event_timeout;
static event_callback_t event_timed = EVENT_CALLBACK_INIT(_send_response, &_separate_ctx);
1 change: 1 addition & 0 deletions sys/include/net/coap.h
Original file line number Diff line number Diff line change
@@ -540,6 +540,7 @@ typedef enum {
* @brief Marks the boundary between header and payload
*/
#define COAP_PAYLOAD_MARKER (0xFF)
#define COAP_PAYLOAD_MARKER_SIZE (1U) /**< Size of the payload marker */

/**
* @defgroup net_coap_conf CoAP compile configurations
38 changes: 27 additions & 11 deletions sys/include/net/gcoap.h
Original file line number Diff line number Diff line change
@@ -401,14 +401,15 @@

#include "event/callback.h"
#include "event/timeout.h"
#include "net/ipv6/addr.h"
#include "net/sock/udp.h"
#include "net/nanocoap.h"

#if IS_USED(MODULE_GCOAP_DTLS)
#include "net/sock/dtls.h"
# include "net/sock/dtls.h"
#endif
#if IS_USED(MODULE_NANOCOAP_CACHE)
# include "net/nanocoap/cache.h"
#endif
#include "net/nanocoap.h"
#include "net/nanocoap/cache.h"
#include "timex.h"

#ifdef __cplusplus
extern "C" {
@@ -480,7 +481,7 @@ extern "C" {
/**
* @brief Maximum length in bytes for a header, including the token
*/
#define GCOAP_HEADER_MAXLEN (sizeof(coap_hdr_t) + GCOAP_TOKENLEN_MAX)
#define GCOAP_HEADER_MAXLEN (sizeof(coap_udp_hdr_t) + GCOAP_TOKENLEN_MAX)

/**
* @ingroup net_gcoap_conf
@@ -1217,22 +1218,37 @@ sock_dtls_t *gcoap_get_sock_dtls(void);
#endif

/**
* @brief Get the header of a request from a @ref gcoap_request_memo_t
* @brief Get the buffer from a @ref gcoap_request_memo_t
*
* @param[in] memo A request memo. Must not be NULL.
*
* @return The request header for the given request memo.
* @return The buffer storing the message
*/
static inline coap_hdr_t *gcoap_request_memo_get_hdr(const gcoap_request_memo_t *memo)
static inline uint8_t *gcoap_request_memo_get_buf(gcoap_request_memo_t *memo)
{
if (memo->send_limit == GCOAP_SEND_LIMIT_NON) {
return (coap_hdr_t *)&memo->msg.hdr_buf[0];
return &memo->msg.hdr_buf[0];
}
else {
return (coap_hdr_t *)memo->msg.data.pdu_buf;
return memo->msg.data.pdu_buf;
}
}

/**
* @brief Get the header of a request from a @ref gcoap_request_memo_t
*
* @param[in] memo A request memo. Must not be NULL.
*
* @return The request header for the given request memo.
*
* @deprecated Use @ref gcoap_request_memo_get_buf instead
*/
static inline coap_udp_hdr_t *gcoap_request_memo_get_hdr(const gcoap_request_memo_t *memo)
{
gcoap_request_memo_t *evil_cast_is_evil = (gcoap_request_memo_t *)memo;
return (coap_udp_hdr_t *)gcoap_request_memo_get_buf(evil_cast_is_evil);
}

#ifdef __cplusplus
}
#endif
Loading