From 6350d22bc9cc9d49aa226f023d65bdcb0f73c41c Mon Sep 17 00:00:00 2001 From: chrysn Date: Fri, 2 Oct 2020 21:00:40 +0200 Subject: [PATCH] net/gcoap: Register additional resources head-first This simplifies (written and compiled) code by doing a head rather than a tail insertion of the new listener into gcoap's list. As handling of listeners without a link_encoder is now fixed, gcoap_get_resource_list can handles this now without having to manually skip over the .well-known/core handler (which is not the first entry any more now). Incidentally, this allows the user to install a custom handler for .well-known/core, as the default handler is now evaluated last. --- sys/net/application_layer/gcoap/gcoap.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/sys/net/application_layer/gcoap/gcoap.c b/sys/net/application_layer/gcoap/gcoap.c index f745f519b0..56d8d6d5d6 100644 --- a/sys/net/application_layer/gcoap/gcoap.c +++ b/sys/net/application_layer/gcoap/gcoap.c @@ -636,17 +636,12 @@ kernel_pid_t gcoap_init(void) void gcoap_register_listener(gcoap_listener_t *listener) { - /* Add the listener to the end of the linked list. */ - gcoap_listener_t *_last = _coap_state.listeners; - while (_last->next) { - _last = _last->next; - } - - listener->next = NULL; if (!listener->link_encoder) { listener->link_encoder = gcoap_encode_link; } - _last->next = listener; + + listener->next = _coap_state.listeners; + _coap_state.listeners = listener; } int gcoap_req_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, @@ -875,8 +870,7 @@ int gcoap_get_resource_list(void *buf, size_t maxlen, uint8_t cf) { assert(cf == COAP_FORMAT_LINK); - /* skip the first listener, gcoap itself (we skip /.well-known/core) */ - gcoap_listener_t *listener = _coap_state.listeners->next; + gcoap_listener_t *listener = _coap_state.listeners; char *out = (char *)buf; size_t pos = 0;