mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2024-12-29 04:50:03 +01:00
examples/gcoap: use resource by path lookup
This commit is contained in:
parent
a181c7b543
commit
e858f77ba9
@ -124,13 +124,21 @@ 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;
|
||||||
|
const gcoap_listener_t *listener = NULL;
|
||||||
|
while ((rtc_resource = gcoap_get_resource_by_path_iterator(&listener, rtc_resource, "/rtc"))) {
|
||||||
|
if (!strcmp(rtc_resource->path, "/rtc")) {
|
||||||
|
break; /* exact match */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (rtc_resource) {
|
||||||
|
switch (gcoap_obs_init(&pdu, buf, sizeof(buf), rtc_resource)) {
|
||||||
case GCOAP_OBS_INIT_OK:
|
case GCOAP_OBS_INIT_OK:
|
||||||
len = coap_opt_finish(&pdu, COAP_OPT_FINISH_PAYLOAD);
|
len = coap_opt_finish(&pdu, COAP_OPT_FINISH_PAYLOAD);
|
||||||
memcpy(pdu.payload, str_time, strftime(str_time, sizeof(str_time), "%Y-%m-%d %H:%M:%S", &tm_now));
|
memcpy(pdu.payload, str_time, strftime(str_time, sizeof(str_time), "%Y-%m-%d %H:%M:%S", &tm_now));
|
||||||
pdu.payload_len = strlen(str_time);
|
pdu.payload_len = strlen(str_time);
|
||||||
len += pdu.payload_len;
|
len += pdu.payload_len;
|
||||||
if (!gcoap_obs_send(buf, len, &_resources[2])) {
|
if (!gcoap_obs_send(buf, len, rtc_resource)) {
|
||||||
DEBUG_PUTS("gcoap_server: cannot send /rtc notification");
|
DEBUG_PUTS("gcoap_server: cannot send /rtc notification");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -141,6 +149,7 @@ static void _rtc_notify_observers(void *arg)
|
|||||||
DEBUG_PUTS("gcoap_server: error initializing /rtc notification");
|
DEBUG_PUTS("gcoap_server: error initializing /rtc notification");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static ssize_t _rtc_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, coap_request_ctx_t *ctx)
|
static ssize_t _rtc_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, coap_request_ctx_t *ctx)
|
||||||
|
Loading…
Reference in New Issue
Block a user