Benjamin Cabé

Zephyr Weekly Update – New CoAP service

Zephyr Weekly Update - November 10, 2023

Zephyr Weekly Update - November 10, 2023

Hi everyone! In case you missed it, the Eclipse Foundation just released the results of their 2023 IoT Developer Survey. It is always a challenge to understand the trends in adoption of open source software as there is no obligation on the adopters’ side to tell when and where they are using open source projects 🙂

This survey is very helpful in shedding some light on the technology stacks people are using in their IoT solutions, and it’s nice to see Zephyr is on their radar.

In other news, here are some of the things that have kept the Zephyr community busy this week!

CoAP “servlets”

When building an IoT device, one typically wants to spend time writing their actual application logic, not reinventing the wheel regarding how they should implement the “Internet” aspect of their “Thing”

A merged pull request from this week, PR #64265, is introducing a CoAP server API that allows to easily register CoAP resources against a CoAP “service”, effectively getting rid of most of the boilerplate one would have to come up with if building on lower layer APIs.

In a nutshell, and in a slightly simplified way, a minimal CoAP server + /hello resource would not require much more code than:

COAP_SERVICE_DEFINE(coap_server, 
			"0.0.0.0",
			&coap_port, 
			COAP_SERVICE_AUTOSTART);

static int my_get(struct coap_resource *resource, struct coap_packet *request,
                  struct sockaddr *addr, socklen_t addr_len)
{
    static const char *msg = "Hello, world!";
    uint8_t data[CONFIG_COAP_SERVER_MESSAGE_SIZE];
    struct coap_packet response;
    
    /** ... **/
    
    coap_packet_append_payload(&response, (uint8_t *)msg, sizeof(msg));

    return coap_resource_send(resource, &response, addr, addr_len);
}


static const char * const my_resource_path[] = { "hello", NULL };
COAP_RESOURCE_DEFINE(my_resource, coap_server, {
    .path = my_resource_path,
    .get = my_get
});

The CoAP service can then be started/stopped using coap_service_start()/coap_service_stop() (in the example above it’s set to start automatically) or using shell commands, and things like the magic ./well-known/core endpoint, retransmissions, etc. are automatically taken care of by the service.

I love it when Zephyr gets new features like this. This new service feels very much like what you would expect to find in a full-blown operating system, and yet we’re still talking about super constrained devices here.

You should definitely check out the code sample to get more familiar with this new API.

Boards & SoCs

Lolin S2 Mini dev kit

Drivers

Miscellaneous


A big thank you to the 7 individuals who had their first pull request accepted this week, 💙 🙌: @cocoeoli, @mgolu, @topisani, @samueltardieu, @p9n, @BenjaminDeuter, and @raffi-g.

As always, I very much welcome your thoughts and feedback in the comments below!

If you enjoyed this article, don’t forget to subscribe to this blog to be notified of upcoming publications! And of course, you can also always find me on Twitter and Mastodon.

Catch up on all previous issues of the Zephyr Weekly Update:

Exit mobile version