Skip to content

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

Open
carl-tud wants to merge 178 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 178 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

I'm mainly concerned about memory usage once the server has state... And that could easily be turned off by not importing it.

So yes, go for it!

@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...

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 CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR 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