Skip to content

Commit

Permalink
examples/nanocoap_server: add CoAP over TCP support
Browse files Browse the repository at this point in the history
This changes the Makefile to easily setup lwIP as network stack, while
keeping GNRC as the default.

If lwIP is used on one of the larger boards, the app will be build with
CoAP over TCP support enabled.
  • Loading branch information
maribu committed Dec 17, 2024
1 parent 9be1835 commit e930e7a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 20 deletions.
36 changes: 27 additions & 9 deletions examples/nanocoap_server/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,25 @@ BOARD ?= native
# This has to be the absolute path to the RIOT base directory:
RIOTBASE ?= $(CURDIR)/../..

NETWORK_STACK ?= gnrc

# Include packages that pull up and auto-init the link layer.
# NOTE: 6LoWPAN will be included if IEEE802.15.4 devices are present
USEMODULE += netdev_default
USEMODULE += auto_init_gnrc_netif
# Specify the mandatory networking modules for IPv6 and UDP
USEMODULE += gnrc_ipv6_default
USEMODULE += sock_udp
# Additional networking modules that can be dropped if not needed
USEMODULE += gnrc_icmpv6_echo

USEMODULE += nanocoap_sock
USEMODULE += nanocoap_resources
USEMODULE += ipv6_addr

ifeq ($(NETWORK_STACK),gnrc)
USEMODULE += auto_init_gnrc_netif
# Specify the mandatory networking modules for IPv6
USEMODULE += gnrc_ipv6_default
# Additional networking modules that can be dropped if not needed
USEMODULE += gnrc_icmpv6_echo
endif
ifeq ($(NETWORK_STACK),lwip)
USEMODULE += auto_init_lwip_netif
USEMODULE += lwip_ipv6 lwip_ipv6_autoconfig
endif

USEMODULE += xtimer

Expand All @@ -43,7 +50,7 @@ ifneq (,$(filter $(BOARD),$(LOW_MEMORY_BOARDS)))
USEMODULE += prng_minstd
endif

# Enable fileserver for boards with plenty of memory
# Enable fileserver and TCP for boards with plenty of memory
HIGH_MEMORY_BOARDS := native native64 same54-xpro mcb2388

ifneq (,$(filter $(BOARD),$(HIGH_MEMORY_BOARDS)))
Expand All @@ -61,6 +68,17 @@ ifneq (,$(filter $(BOARD),$(HIGH_MEMORY_BOARDS)))
ifneq (,$(filter native native64,$(BOARD)))
USEMODULE += vfs_auto_format
endif

# async TCP is not supported on GNRC yet
ifeq ($(NETWORK_STACK),lwip)
USEMODULE += nanocoap_server_tcp
endif
endif

# if nanocaop_server_tcp is used: This app makes use of event_thread
# to run the TCP server
ifneq (,$(filter nanocoap_server_tcp,$(USEMODULE)))
USEMODULE += event_thread
endif

# Change this to 0 show compiler invocation lines by default:
Expand Down
14 changes: 4 additions & 10 deletions examples/nanocoap_server/coap_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
#include "net/nanocoap_sock.h"
#include "hashes/sha256.h"

#if MODULE_EVENT_THREAD
# include "event.h"
#if MODULE_NANOCOAP_SERVER_SEPARATE
# include "event/thread.h"
# include "event/timeout.h"
# include "event/callback.h"
Expand Down Expand Up @@ -210,8 +209,8 @@ static ssize_t _separate_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, coap
if (event_timeout_is_pending(&event_timeout)) {
if (nanocoap_is_duplicate_in_separate_ctx(&_separate_ctx, pkt, context)) {
/* no need to check transport: Only UDP can have duplicates */
puts("_separate_handler(): duplicate --> ACK");
return coap_build_empty_ack(pkt, (void *)buf);
puts("_separate_handler(): duplicate");
return coap_reply_empty_ack(pkt, buf, len);
}
puts("_separate_handler(): response already scheduled");
return coap_build_reply(pkt, COAP_CODE_SERVICE_UNAVAILABLE, buf, len, 0);
Expand All @@ -229,12 +228,7 @@ static ssize_t _separate_handler(coap_pkt_t *pkt, uint8_t *buf, size_t len, coap
&event_timed.super);
event_timeout_set(&event_timeout, 1 * MS_PER_SEC);

if (coap_get_transport(pkt) == COAP_TRANSPORT_TCP) {
/* no empty ACK in TCP needed */
return 0;
}

return coap_build_empty_ack(pkt, (void *)buf);
return coap_reply_empty_ack(pkt, buf, len);
}

NANOCOAP_RESOURCE(separate) {
Expand Down
16 changes: 15 additions & 1 deletion examples/nanocoap_server/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@
#include "net/nanocoap_sock.h"
#include "xtimer.h"

#if MODULE_NANOCOAP_SERVER_TCP
# include "event/thread.h"
#endif

#define COAP_INBUF_SIZE (256U)

#define MAIN_QUEUE_SIZE (8)
static msg_t _main_msg_queue[MAIN_QUEUE_SIZE];

#if MODULE_NANOCOAP_SERVER_TCP
static nanocoap_tcp_server_ctx_t tcp_ctx;
#endif

int main(void)
{
puts("RIOT nanocoap example application");
Expand All @@ -42,10 +50,16 @@ int main(void)
netifs_print_ipv6("\", \"");
puts("\"]}");

#if MODULE_NANOCOAP_SERVER_TCP
nanocoap_server_tcp(&tcp_ctx, EVENT_PRIO_MEDIUM, NULL);
#endif

#if MODULE_NANOCOAP_UDP
/* initialize nanocoap server instance */
uint8_t buf[COAP_INBUF_SIZE];
sock_udp_ep_t local = { .port=COAP_PORT, .family=AF_INET6 };
nanocoap_server(&local, buf, sizeof(buf));
nanocoap_server_udp(&local, buf, sizeof(buf));
#endif

/* should be never reached */
return 0;
Expand Down

0 comments on commit e930e7a

Please sign in to comment.