Skip to content

Commit

Permalink
examples,tests: update users of deprecated nanocoap APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
maribu committed Nov 5, 2024
1 parent 83e3bf9 commit b2c9504
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 25 deletions.
15 changes: 9 additions & 6 deletions examples/nanocoap_server/coap_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
#include <stdio.h>
#include <string.h>

#include "event/callback.h"
#include "event/timeout.h"
#include "event/thread.h"
#include "fmt.h"
#include "net/nanocoap.h"
#include "net/nanocoap_sock.h"
#include "hashes/sha256.h"
#include "kernel_defines.h"

#if MODULE_EVENT_THREAD
# include "event.h"
# include "event/thread.h"
# include "event/timeout.h"
# include "event/callback.h"
#endif

/* internal value that can be read/written via CoAP */
static uint8_t internal_value = 0;
Expand Down Expand Up @@ -158,7 +161,7 @@ ssize_t _sha256_handler(coap_pkt_t* pkt, uint8_t *buf, size_t len, coap_request_
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);
}
Expand All @@ -167,7 +170,7 @@ ssize_t _sha256_handler(coap_pkt_t* pkt, uint8_t *buf, size_t len, coap_request_
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) {
Expand Down
12 changes: 5 additions & 7 deletions tests/net/nanocoap_cli/nanocli_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

#include "net/coap.h"
#include "net/gnrc/netif.h"
#include "net/ipv6.h"
#include "net/nanocoap.h"
#include "net/nanocoap_sock.h"
#include "net/sock/udp.h"
Expand Down Expand Up @@ -85,8 +84,7 @@ int nanotest_client_cmd(int argc, char **argv)
{
/* Ordered like the RFC method code numbers, but off by 1. GET is code 0. */
const char *method_codes[] = {"get", "post", "put"};
unsigned buflen = 128;
uint8_t buf[buflen];
uint8_t buf[128];
coap_pkt_t pkt;
size_t len;
uint8_t token[2] = {0xDA, 0xEC};
Expand All @@ -106,13 +104,13 @@ int nanotest_client_cmd(int argc, char **argv)
goto end;
}

pkt.hdr = (coap_hdr_t *)buf;
pkt.buf = buf;

/* parse options */
if (argc == 5 || argc == 6) {
ssize_t hdrlen = coap_build_hdr(pkt.hdr, COAP_TYPE_CON, &token[0], 2,
ssize_t hdrlen = coap_build_udp_hdr(buf, sizeof(buf), COAP_TYPE_CON, &token[0], 2,
code_pos+1, 1);
coap_pkt_init(&pkt, &buf[0], buflen, hdrlen);
coap_pkt_init(&pkt, &buf[0], sizeof(buf), hdrlen);
coap_opt_add_string(&pkt, COAP_OPT_URI_PATH, argv[4], '/');
if (argc == 6) {
coap_opt_add_uint(&pkt, COAP_OPT_CONTENT_FORMAT, COAP_FORMAT_TEXT);
Expand All @@ -129,7 +127,7 @@ int nanotest_client_cmd(int argc, char **argv)
printf("nanocli: sending msg ID %u, %" PRIuSIZE " bytes\n", coap_get_id(&pkt),
len);

ssize_t res = _send(&pkt, buflen, argv[2], argv[3]);
ssize_t res = _send(&pkt, sizeof(buf), argv[2], argv[3]);
if (res < 0) {
printf("nanocli: msg send failed: %" PRIdSIZE "\n", res);
}
Expand Down
11 changes: 2 additions & 9 deletions tests/pkg/edhoc_c/initiator.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
#include <stdio.h>

#include "net/gnrc/netif.h"
#include "net/ipv6.h"
#include "net/nanocoap_sock.h"
#include "shell.h"

#include "edhoc/edhoc.h"
#include "edhoc_keys.h"
Expand All @@ -33,9 +31,6 @@
#include "tinycrypt/sha256.h"
#endif

#define ENABLE_DEBUG 0
#include "debug.h"

#define COAP_BUF_SIZE (256U)

#if IS_ACTIVE(CONFIG_INITIATOR)
Expand Down Expand Up @@ -114,11 +109,9 @@ static ssize_t _build_coap_pkt(coap_pkt_t *pkt, uint8_t *buf, ssize_t buflen,
uint8_t token[2] = { 0xDA, 0xEC };
ssize_t len = 0;

/* set pkt buffer */
pkt->hdr = (coap_hdr_t *)buf;
/* build header, confirmed message always post */
ssize_t hdrlen = coap_build_hdr(pkt->hdr, COAP_TYPE_CON, token,
sizeof(token), COAP_METHOD_POST, 1);
ssize_t hdrlen = coap_build_udp_hdr(buf, buflen, COAP_TYPE_CON, token,
sizeof(token), COAP_METHOD_POST, 1);

coap_pkt_init(pkt, buf, buflen, hdrlen);
coap_opt_add_string(pkt, COAP_OPT_URI_PATH, "/.well-known/edhoc", '/');
Expand Down
5 changes: 2 additions & 3 deletions tests/riotboot_flashwrite/coap_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "net/nanocoap.h"
#include "riotboot/flashwrite.h"
Expand Down Expand Up @@ -72,10 +71,10 @@ ssize_t _flashwrite_handler(coap_pkt_t* pkt, uint8_t *buf, size_t len, coap_requ
return reply_len;
}

uint8_t *pkt_pos = (uint8_t*)pkt->hdr + reply_len;
uint8_t *pkt_pos = pkt->buf + reply_len;
pkt_pos += coap_put_block1_ok(pkt_pos, &block1, 0);

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

NANOCOAP_RESOURCE(flashwrite) {
Expand Down

0 comments on commit b2c9504

Please sign in to comment.