Skip to content

net/unicoap: Unified and Modular CoAP stack: Messaging and Minimal Server (pt 2)#21582

Open
carl-tud wants to merge 183 commits intoRIOT-OS:masterfrom
carl-tud:unicoap-02-server-minimal
Open

net/unicoap: Unified and Modular CoAP stack: Messaging and Minimal Server (pt 2)#21582
carl-tud wants to merge 183 commits intoRIOT-OS:masterfrom
carl-tud:unicoap-02-server-minimal

Conversation

@carl-tud
Copy link
Copy Markdown
Contributor

@carl-tud carl-tud commented Jul 7, 2025

This PR is the second in a series to introduce unicoap, a unified and modular CoAP implementation for RIOT. An overview of all PRs related to unicoap is presented in #21389, including reasons why unicoap is needed and a performance analysis.

What does this PR include?

  • RFC 7252 messaging implementation
  • Implementation of the CoAP over UDP and CoAP over DTLS drivers, plus an additional zero-copy optimization for UDP
  • Basic server functionality, including XFA resource definitions and helpful debug logs
  • A sample server application
  • Structured documentation, including a tutorial from the first line of code to running the example and testing it using the included client script
  • Support for running unicoap on a thread of your choice, e.g., the main thread.

The new API is more flexible. CoAP endpoints are abstracted into a unicoap_endpoint_t structure and transport-specific settings are controlled by flags. For example, this is a simple resource that responds with "Hello, World!".

// Define request handler for /hello
static int handle_hello_request(unicoap_message_t* message, 
    const unicoap_aux_t* aux, unicoap_request_context_t* ctx, void* arg) {
    // The aux parameter provides access to auxiliary information, such as local and remote endpoints,
    // transport-specific data and internal properties such as the message token.

    // Retrieve remote (client) endpoint and log string description of protocol number.
    printf("/hello/world resource invoked over %s\n", unicoap_string_from_proto(aux->remote->proto));

    // Craft response using convenience initializer. Vectored payloads are supported, too.
    unicoap_response_init_string(message, UNICOAP_STATUS_CONTENT, "Hello, World!");

    // Send response.
    return unicoap_send_response(message, ctx);
}

// Statically define resource, but dynamic registrations are also possible.
UNICOAP_RESOURCE(helloworld) {
    // When you specify a path, you now need to specify each component separately.
    // The UNICOAP_PATH macro checks if you passed garbage at compile time.
    .path = UNICOAP_PATH("hello", "world"), // /hello/world,
    
    // Instruct unicoap to send confirmable messages when communicating over UDP or DTLS.
    // This flag abstracts the transport-specific message type.
    .flags = UNICOAP_RESOURCE_FLAG_RELIABLE,
    
    // Specify what methods to allow. In unicoap, there are no duplicate defines for method flags.
    .methods = UNICOAP_METHODS(UNICOAP_METHOD_GET, UNICOAP_METHOD_PUT),
    
    // Optionally, you can also restrict the resource to a set of transports.
    .protocols = UNICOAP_PROTOCOLS(UNICOAP_PROTO_DTLS, UNICOAP_PROTO_UDP),
    
    .handler = handle_hello_request,
    .handler_arg = NULL
};

More in the documentation (CI build sometimes not available, e.g., due to failing tests).


Open discussion points

why does Github not allow to see all unresolved comments and/or to pin comments?!?

listed in #21582 (comment)

@github-actions github-actions bot added Area: network Area: Networking Area: doc Area: Documentation Area: tests Area: tests and testing framework Area: build system Area: Build system Area: CoAP Area: Constrained Application Protocol implementations Area: sys Area: System Area: examples Area: Example Applications Area: Kconfig Area: Kconfig integration labels Jul 7, 2025
@carl-tud carl-tud changed the title net/unicoap: Messaging and Minimal Server (pt 2) net/unicoap: Unified and Modular CoAP stack: Messaging and Minimal Server (pt 2) Jul 7, 2025
@crasbe crasbe added Type: new feature The issue requests / The PR implemements a new feature for RIOT CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Jul 7, 2025
@riot-ci
Copy link
Copy Markdown

riot-ci commented Jul 7, 2025

Murdock results

✔️ PASSED

6e21e25 use TEST_ASSERT_NULL

Success Failures Total Runtime
11043 0 11044 10m:38s

Artifacts

@carl-tud carl-tud force-pushed the unicoap-02-server-minimal branch from da014c7 to 4d00949 Compare July 9, 2025 14:37
@github-actions github-actions bot removed the Area: tests Area: tests and testing framework label Jul 9, 2025
@carl-tud carl-tud marked this pull request as ready for review July 9, 2025 14:42
@mguetschow
Copy link
Copy Markdown
Contributor

What compiler does atmega256rfr2-xpro use and what's up with macro evaluation there? Some very weird errors...

ci.riot-os.org/details/6ff04d08730f4039800eebc5de39bd4f/builds/examples:networking:coap:unicoap_server

Very strange, cannot reproduce with BUILD_IN_DOCKER=1 make -C examples/networking/coap/unicoap_server BOARD=atmega256rfr2-xpro clean all

@carl-tud
Copy link
Copy Markdown
Contributor Author

carl-tud commented Apr 2, 2026

What compiler does atmega256rfr2-xpro use and what's up with macro evaluation there? Some very weird errors...
ci.riot-os.org/details/6ff04d08730f4039800eebc5de39bd4f/builds/examples:networking:coap:unicoap_server

Very strange, cannot reproduce with BUILD_IN_DOCKER=1 make -C examples/networking/coap/unicoap_server BOARD=atmega256rfr2-xpro clean all

@mguetschow Do you have any suspicion why this might be happening?

Copy link
Copy Markdown
Contributor

@mguetschow mguetschow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nits again :)

@carl-tud
Copy link
Copy Markdown
Contributor Author

carl-tud commented Apr 2, 2026

What compiler does atmega256rfr2-xpro use and what's up with macro evaluation there? Some very weird errors...
ci.riot-os.org/details/6ff04d08730f4039800eebc5de39bd4f/builds/examples:networking:coap:unicoap_server

Very strange, cannot reproduce with BUILD_IN_DOCKER=1 make -C examples/networking/coap/unicoap_server BOARD=atmega256rfr2-xpro clean all

@mguetschow Do you have any suspicion why this might be happening?

So if use my own CONCAT macro (a ## b), it works?! But if I import CONCAT (also a ## b) from macros/utils.h, then it fails with this CONCAT_NOEXPAND error...

@carl-tud carl-tud removed the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Apr 3, 2026
@carl-tud
Copy link
Copy Markdown
Contributor Author

carl-tud commented Apr 3, 2026

@mguetschow The gcoap_dtls error seems to be on gcoap's side, I guess. The aiocoap client script works on native64 through the tap interface. But gcoap_dtls fails regardless whether it's a real 802.15.4 network or one setup using tapsetup -c 0 on native64.

So the only question I have left is whether we're assuming something about DTLS connection establishment that aiocoap supports but gcoap/gcoap_dtls don't.

native64 tapsetup -c 2
# gcoap: authentication timed out
# gcoap: sock send failed: -107 Transport endpoint is not connected
# gcoap_cli: msg send failed
adafruit-feather-nrf52840-sense 802.15.4
# gcoap: authentication timed out
# gcoap: sock send failed: -128 Socket is not connected
# gcoap_cli: msg send failed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: build system Area: Build system Area: CoAP Area: Constrained Application Protocol implementations Area: doc Area: Documentation Area: examples Area: Example Applications Area: Kconfig Area: Kconfig integration Area: network Area: Networking Area: sys Area: System Area: tests Area: tests and testing framework Type: new feature The issue requests / The PR implemements a new feature for RIOT

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants