RIOT icon indicating copy to clipboard operation
RIOT copied to clipboard

pkg/libcsp: add sock_udp [demonstrator]

Open fjmolinas opened this issue 3 years ago • 0 comments

Contribution description

For another project, I wanted to use CoAP and many applications depending on it on top of a non-IP stack (libCSP). Since all those are basically using the sock api, and sock is IP based, I implemented sock_udp ep that basically just ignores everything in the IP address but the last hex part, This is just meant to be able to have drop-in network stack replacements, and the same usage through the present CLI tools, but nothing related to IP addressing is actually used. I'm tagging it as demonstrator as this feels a bit like a hack, but might be useful for others.

Testing procedure

There are some incomplete unittests, not sure it's worth the effort right now to pursue further.

Use gcoap as a CoAP server or a as a CoAP client, to do it must be patched a little:

diff --git a/examples/gcoap/Makefile b/examples/gcoap/Makefile
index bcb3c4731e..2b622d0e86 100644
--- a/examples/gcoap/Makefile
+++ b/examples/gcoap/Makefile
@@ -9,25 +9,23 @@ BOARD ?= native
 # This has to be the absolute path to the RIOT base directory:
 RIOTBASE ?= $(CURDIR)/../..

-# 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
-
-# use GNRC by default
-LWIP ?= 0
-
-ifeq (0,$(LWIP))
-  USEMODULE += auto_init_gnrc_netif
-  # Specify the mandatory networking modules
-  USEMODULE += gnrc_ipv6_default
-  # Additional networking modules that can be dropped if not needed
-  USEMODULE += gnrc_icmpv6_echo
-else
-  USEMODULE += lwip_ipv6
-  USEMODULE += lwip_netdev
-endif
-
+# Use CSP network stack
+USEPKG += libcsp
+# libCSP ports must be <64, so arbitrarily pick port 2 as the default
+# CoAP port instead of 5683
+COAP_PORT ?= 2
+CFLAGS += -DCOAP_PORT=$(COAP_PORT)
+CFLAGS += -DCONFIG_GCOAP_PORT=$(COAP_PORT)
+# Include gcoap
 USEMODULE += gcoap
+ifneq (,$(filter libcsp,$(USEPKG)))
+  ifeq ($(BOARD),native)
+    FEATURES_REQUIRED += periph_can
+  else
+    # Default to MCP2515 for non native BOARDs to cover larger BOARD scpectrum
+    USEMODULE += mcp2515
+  endif
+endif

 # Required by gcoap example
 USEMODULE += od

The gcoap examples in master expects IPv6 addressing. But CSP is used here which does not use or support IP. To work around this, destination addresses for requests should be formatted as IPv6 addresses with trailing zeros. Internal the CSP sock_udp module ignores all but the first 2 bytes.

e.g.:

> csp
csp
can0       addr: 39:E5 netmask: 4 mtu: 247
           tx: 00000 rx: 00000 txe: 00000 rxe: 00000
           drop: 00000 autherr: 00000 frame: 00000
           txb: 0 (0B) rxb: 0 (0B)

The CSP address 39:E5 or 0x39E5 would become ::39e5 Ipv6 address.

Follow the instructions in README.native.can.md to setup two virtual can interfaces.

Verify setup from RIOT terminal

> coap info

Expected response:

CoAP server is listening on port 2
 CLI requests sent: 0
CoAP open requests: 0

Send query between RIOT instances

> coap get ::39e5 2 /.well-known/core
coap get ::39e5 2 /.well-known/core
gcoap_cli: sending msg ID 56189, 23 bytes
> gcoap: response Success, code 2.05, 46 bytes
</cli/stats>;ct=0;rt="count";obs,</riot/board>

Issues/PRs references

Depends on #18009

fjmolinas avatar Apr 26 '22 07:04 fjmolinas