1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-17 05:12:57 +01:00

examples/gcoap: use resource by path lookup

This commit is contained in:
Fabian Hüßler 2024-08-01 10:33:20 +02:00
parent a181c7b543
commit e858f77ba9

View File

@ -124,22 +124,31 @@ static void _rtc_notify_observers(void *arg)
char str_time[20] = ""; char str_time[20] = "";
uint8_t buf[sizeof(coap_hdr_t) + COAP_TOKEN_LENGTH_MAX + 1 + sizeof(str_time)]; uint8_t buf[sizeof(coap_hdr_t) + COAP_TOKEN_LENGTH_MAX + 1 + sizeof(str_time)];
coap_pkt_t pdu; coap_pkt_t pdu;
switch (gcoap_obs_init(&pdu, buf, sizeof(buf), &_resources[2])) { const coap_resource_t *rtc_resource = NULL;
case GCOAP_OBS_INIT_OK: const gcoap_listener_t *listener = NULL;
len = coap_opt_finish(&pdu, COAP_OPT_FINISH_PAYLOAD); while ((rtc_resource = gcoap_get_resource_by_path_iterator(&listener, rtc_resource, "/rtc"))) {
memcpy(pdu.payload, str_time, strftime(str_time, sizeof(str_time), "%Y-%m-%d %H:%M:%S", &tm_now)); if (!strcmp(rtc_resource->path, "/rtc")) {
pdu.payload_len = strlen(str_time); break; /* exact match */
len += pdu.payload_len; }
if (!gcoap_obs_send(buf, len, &_resources[2])) { }
DEBUG_PUTS("gcoap_server: cannot send /rtc notification"); if (rtc_resource) {
} switch (gcoap_obs_init(&pdu, buf, sizeof(buf), rtc_resource)) {
break; case GCOAP_OBS_INIT_OK:
case GCOAP_OBS_INIT_UNUSED: len = coap_opt_finish(&pdu, COAP_OPT_FINISH_PAYLOAD);
DEBUG_PUTS("gcoap_server: no observer for /rtc"); memcpy(pdu.payload, str_time, strftime(str_time, sizeof(str_time), "%Y-%m-%d %H:%M:%S", &tm_now));
break; pdu.payload_len = strlen(str_time);
case GCOAP_OBS_INIT_ERR: len += pdu.payload_len;
DEBUG_PUTS("gcoap_server: error initializing /rtc notification"); if (!gcoap_obs_send(buf, len, rtc_resource)) {
break; DEBUG_PUTS("gcoap_server: cannot send /rtc notification");
}
break;
case GCOAP_OBS_INIT_UNUSED:
DEBUG_PUTS("gcoap_server: no observer for /rtc");
break;
case GCOAP_OBS_INIT_ERR:
DEBUG_PUTS("gcoap_server: error initializing /rtc notification");
break;
}
} }
} }