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.
This commit is contained in:
chrysn 2020-10-02 21:00:40 +02:00
parent e3db58f013
commit 6350d22bc9

View File

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