Skip to content

Commit

Permalink
WIP: net/gcoap: adapt to new nanocoap API
Browse files Browse the repository at this point in the history
  • Loading branch information
benpicco committed Jan 21, 2022
1 parent 867540e commit f232e81
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 42 deletions.
8 changes: 4 additions & 4 deletions examples/cord_ep/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,25 @@ static void _on_ep_event(cord_ep_standalone_event_t event)

/* define some dummy CoAP resources */
static ssize_t _handler_dummy(coap_pkt_t *pdu,
uint8_t *buf, size_t len, void *ctx)
coap_rsp_pkt_t *response, void *ctx)
{
(void)ctx;

/* get random data */
int16_t val = 23;

gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
gcoap_resp_init(pdu, response, COAP_CODE_CONTENT);
size_t resp_len = coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD);
resp_len += fmt_s16_dec((char *)pdu->payload, val);
return resp_len;
}

static ssize_t _handler_info(coap_pkt_t *pdu,
uint8_t *buf, size_t len, void *ctx)
coap_rsp_pkt_t *response, void *ctx)
{
(void)ctx;

gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
gcoap_resp_init(pdu, response, COAP_CODE_CONTENT);
size_t resp_len = coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD);
size_t slen = sizeof(NODE_INFO);
memcpy(pdu->payload, NODE_INFO, slen);
Expand Down
12 changes: 6 additions & 6 deletions examples/cord_epsim/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,26 @@
static char riot_info[BUFSIZE];

/* define some dummy CoAP resources */
static ssize_t text_resp(coap_pkt_t *pdu, uint8_t *buf, size_t len,
static ssize_t text_resp(coap_pkt_t *pdu, coap_rsp_pkt_t *response,
const char *text, unsigned format)
{
gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
gcoap_resp_init(pdu, response, COAP_CODE_CONTENT);
coap_opt_add_format(pdu, format);
ssize_t resp_len = coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD);
size_t slen = strlen(text);
memcpy(pdu->payload, text, slen);
return resp_len + slen;
}

static ssize_t handler_info(coap_pkt_t *pdu, uint8_t *buf, size_t len, void *ctx)
static ssize_t handler_info(coap_pkt_t *pdu, coap_rsp_pkt_t *response, void *ctx)
{
(void)ctx;
return text_resp(pdu, buf, len, riot_info, COAP_FORMAT_JSON);
return text_resp(pdu, response, riot_info, COAP_FORMAT_JSON);
}

static ssize_t handler_text(coap_pkt_t *pdu, uint8_t *buf, size_t len, void *ctx)
static ssize_t handler_text(coap_pkt_t *pdu, coap_rsp_pkt_t *response, void *ctx)
{
return text_resp(pdu, buf, len, (char *)ctx, COAP_FORMAT_TEXT);
return text_resp(pdu, response, (char *)ctx, COAP_FORMAT_TEXT);
}

static const coap_resource_t resources[] = {
Expand Down
18 changes: 9 additions & 9 deletions examples/gcoap/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ static const credman_credential_t credential = {

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, void *ctx);
static ssize_t _riot_board_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx);
static ssize_t _stats_handler(coap_pkt_t* pdu, coap_rsp_pkt_t *response, void *ctx);
static ssize_t _riot_board_handler(coap_pkt_t* pdu, coap_rsp_pkt_t *response, void *ctx);

/* CoAP resources. Must be sorted by path (ASCII order). */
static const coap_resource_t _resources[] = {
Expand Down Expand Up @@ -107,7 +107,7 @@ static ssize_t _encode_link(const coap_resource_t *resource, char *buf,
* allows any two byte value for example purposes. Semantically, the only
* valid action is to set the value to 0.
*/
static ssize_t _stats_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx)
static ssize_t _stats_handler(coap_pkt_t* pdu, coap_rsp_pkt_t *response, void *ctx)
{
(void)ctx;

Expand All @@ -116,7 +116,7 @@ static ssize_t _stats_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *c

switch (method_flag) {
case COAP_GET:
gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
gcoap_resp_init(pdu, response, COAP_CODE_CONTENT);
coap_opt_add_format(pdu, COAP_FORMAT_TEXT);
size_t resp_len = coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD);

Expand All @@ -131,20 +131,20 @@ static ssize_t _stats_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *c
char payload[6] = { 0 };
memcpy(payload, (char *)pdu->payload, pdu->payload_len);
req_count = (uint16_t)strtoul(payload, NULL, 10);
return gcoap_response(pdu, buf, len, COAP_CODE_CHANGED);
return gcoap_response(pdu, response, COAP_CODE_CHANGED);
}
else {
return gcoap_response(pdu, buf, len, COAP_CODE_BAD_REQUEST);
return gcoap_response(pdu, response, COAP_CODE_BAD_REQUEST);
}
}

return 0;
}

static ssize_t _riot_board_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len, void *ctx)
static ssize_t _riot_board_handler(coap_pkt_t *pdu, coap_rsp_pkt_t *response, void *ctx)
{
(void)ctx;
gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
gcoap_resp_init(pdu, response, COAP_CODE_CONTENT);
coap_opt_add_format(pdu, COAP_FORMAT_TEXT);
size_t resp_len = coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD);

Expand All @@ -155,7 +155,7 @@ static ssize_t _riot_board_handler(coap_pkt_t *pdu, uint8_t *buf, size_t len, vo
}
else {
puts("gcoap_cli: msg buffer too small");
return gcoap_response(pdu, buf, len, COAP_CODE_INTERNAL_SERVER_ERROR);
return gcoap_response(pdu, response, COAP_CODE_INTERNAL_SERVER_ERROR);
}
}

Expand Down
8 changes: 4 additions & 4 deletions sys/include/net/gcoap.h
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@ ssize_t gcoap_req_send(const uint8_t *buf, size_t len,
* @return 0 on success
* @return < 0 on error
*/
int gcoap_resp_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code);
int gcoap_resp_init(coap_pkt_t *pdu, coap_rsp_pkt_t *response, unsigned code);

/**
* @brief Writes a complete CoAP response PDU when there is no payload
Expand All @@ -947,10 +947,10 @@ int gcoap_resp_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code);
* @return size of the PDU within the buffer
* @return < 0 on error
*/
static inline ssize_t gcoap_response(coap_pkt_t *pdu, uint8_t *buf,
size_t len, unsigned code)
static inline ssize_t gcoap_response(coap_pkt_t *pdu, coap_rsp_pkt_t *response,
unsigned code)
{
return (gcoap_resp_init(pdu, buf, len, code) == 0)
return (gcoap_resp_init(pdu, response, code) == 0)
? coap_opt_finish(pdu, COAP_OPT_FINISH_NONE)
: -1;
}
Expand Down
35 changes: 18 additions & 17 deletions sys/net/application_layer/gcoap/gcoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ static ssize_t _tl_send(gcoap_socket_t *sock, const void *data, size_t len,
const sock_udp_ep_t *remote);
static ssize_t _tl_authenticate(gcoap_socket_t *sock, const sock_udp_ep_t *remote,
uint32_t timeout);
static ssize_t _well_known_core_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx);
static ssize_t _well_known_core_handler(coap_pkt_t* pdu, coap_rsp_pkt_t *response, void *ctx);
static void _cease_retransmission(gcoap_request_memo_t *memo);
static size_t _handle_req(coap_pkt_t *pdu, uint8_t *buf, size_t len,
static size_t _handle_req(coap_pkt_t *pdu, coap_rsp_pkt_t *response,
sock_udp_ep_t *remote);
static void _expire_request(gcoap_request_memo_t *memo);
static void _find_req_memo(gcoap_request_memo_t **memo_ptr, coap_pkt_t *pdu,
Expand Down Expand Up @@ -327,6 +327,7 @@ static void _process_coap_pdu(gcoap_socket_t *sock, sock_udp_ep_t *remote,
uint8_t *buf, size_t len, bool truncated)
{
coap_pkt_t pdu;
coap_rsp_pkt_t response;
gcoap_request_memo_t *memo = NULL;
/* Code paths that necessitate a response on the message layer can set a
* response type here (COAP_TYPE_RST or COAP_TYPE_ACK). If set, at the end
Expand All @@ -352,6 +353,8 @@ static void _process_coap_pdu(gcoap_socket_t *sock, sock_udp_ep_t *remote,
return;
}

coap_init_response(&pdu, &response, _listen_buf, sizeof(_listen_buf));

/* validate class and type for incoming */
switch (coap_get_code_class(&pdu)) {
/* incoming request or empty */
Expand Down Expand Up @@ -380,10 +383,10 @@ static void _process_coap_pdu(gcoap_socket_t *sock, sock_udp_ep_t *remote,

if (truncated) {
/* TBD: Set a Size1 */
pdu_len = gcoap_response(&pdu, _listen_buf, sizeof(_listen_buf),
pdu_len = gcoap_response(&pdu, &response,
COAP_CODE_REQUEST_ENTITY_TOO_LARGE);
} else {
pdu_len = _handle_req(&pdu, _listen_buf, sizeof(_listen_buf), remote);
pdu_len = _handle_req(&pdu, &response, remote);
}

if (pdu_len > 0) {
Expand Down Expand Up @@ -529,7 +532,7 @@ static void _cease_retransmission(gcoap_request_memo_t *memo) {
*
* return length of response pdu, or < 0 if can't handle
*/
static size_t _handle_req(coap_pkt_t *pdu, uint8_t *buf, size_t len,
static size_t _handle_req(coap_pkt_t *pdu, coap_rsp_pkt_t *response,
sock_udp_ep_t *remote)
{
const coap_resource_t *resource = NULL;
Expand All @@ -540,16 +543,16 @@ static size_t _handle_req(coap_pkt_t *pdu, uint8_t *buf, size_t len,

switch (_find_resource((const coap_pkt_t *)pdu, &resource, &listener)) {
case GCOAP_RESOURCE_WRONG_METHOD:
return gcoap_response(pdu, buf, len, COAP_CODE_METHOD_NOT_ALLOWED);
return gcoap_response(pdu, response, COAP_CODE_METHOD_NOT_ALLOWED);
case GCOAP_RESOURCE_NO_PATH:
return gcoap_response(pdu, buf, len, COAP_CODE_PATH_NOT_FOUND);
return gcoap_response(pdu, response, COAP_CODE_PATH_NOT_FOUND);
case GCOAP_RESOURCE_FOUND:
/* find observe registration for resource */
_find_obs_memo_resource(&resource_memo, resource);
break;
case GCOAP_RESOURCE_ERROR:
default:
return gcoap_response(pdu, buf, len, COAP_CODE_INTERNAL_SERVER_ERROR);
return gcoap_response(pdu, response, COAP_CODE_INTERNAL_SERVER_ERROR);
break;
}

Expand Down Expand Up @@ -630,9 +633,9 @@ static size_t _handle_req(coap_pkt_t *pdu, uint8_t *buf, size_t len,
return -1;
}

ssize_t pdu_len = resource->handler(pdu, buf, len, resource->context);
ssize_t pdu_len = resource->handler(pdu, response, resource->context);
if (pdu_len < 0) {
pdu_len = gcoap_response(pdu, buf, len,
pdu_len = gcoap_response(pdu, response,
COAP_CODE_INTERNAL_SERVER_ERROR);
}
return pdu_len;
Expand Down Expand Up @@ -816,12 +819,12 @@ static void _expire_request(gcoap_request_memo_t *memo)
* Handler for /.well-known/core. Lists registered handlers, except for
* /.well-known/core itself.
*/
static ssize_t _well_known_core_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len,
static ssize_t _well_known_core_handler(coap_pkt_t* pdu, coap_rsp_pkt_t *response,
void *ctx)
{
(void)ctx;

gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
gcoap_resp_init(pdu, response, COAP_CODE_CONTENT);
coap_opt_add_format(pdu, COAP_FORMAT_LINK);
ssize_t plen = coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD);

Expand Down Expand Up @@ -1225,18 +1228,16 @@ ssize_t gcoap_req_send(const uint8_t *buf, size_t len,
return ((res > 0 || res == -ENOTCONN) ? res : 0);
}

int gcoap_resp_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code)
int gcoap_resp_init(coap_pkt_t *pdu, coap_rsp_pkt_t *response, unsigned code)
{
if (coap_get_type(pdu) == COAP_TYPE_CON) {
coap_hdr_set_type(pdu->hdr, COAP_TYPE_ACK);
}
coap_hdr_set_code(pdu->hdr, code);

unsigned header_len = coap_get_total_hdr_len(pdu);

pdu->options_len = 0;
pdu->payload = buf + header_len;
pdu->payload_len = len - header_len;
pdu->payload = response->cur;
pdu->payload_len = response->end - response->cur;

if (coap_get_observe(pdu) == COAP_OBS_REGISTER) {
/* generate initial notification value */
Expand Down
8 changes: 6 additions & 2 deletions tests/unittests/tests-gcoap/tests-gcoap.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,12 +273,14 @@ static void test_gcoap__server_get_resp(void)
{
uint8_t buf[CONFIG_GCOAP_PDU_BUF_SIZE];
coap_pkt_t pdu;
coap_rsp_pkt_t response;

/* read request */
_read_cli_stats_req(&pdu, &buf[0]);

/* generate response */
gcoap_resp_init(&pdu, &buf[0], sizeof(buf), COAP_CODE_CONTENT);
coap_init_response(&pdu, &response, buf, sizeof(buf));
gcoap_resp_init(&pdu, &response, COAP_CODE_CONTENT);
coap_opt_add_format(&pdu, COAP_FORMAT_TEXT);
ssize_t res = coap_opt_finish(&pdu, COAP_OPT_FINISH_PAYLOAD);

Expand Down Expand Up @@ -338,12 +340,14 @@ static void test_gcoap__server_con_resp(void)
{
uint8_t buf[CONFIG_GCOAP_PDU_BUF_SIZE];
coap_pkt_t pdu;
coap_rsp_pkt_t response;

/* read request */
_read_cli_stats_req_con(&pdu, &buf[0]);

/* generate response */
gcoap_resp_init(&pdu, &buf[0], sizeof(buf), COAP_CODE_CONTENT);
coap_init_response(&pdu, &response, buf, sizeof(buf));
gcoap_resp_init(&pdu, &response, COAP_CODE_CONTENT);
coap_opt_add_format(&pdu, COAP_FORMAT_TEXT);
ssize_t res = coap_opt_finish(&pdu, COAP_OPT_FINISH_PAYLOAD);

Expand Down

0 comments on commit f232e81

Please sign in to comment.