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

Merge pull request #20076 from bergzand/pr/gcoap_msec

gcoap: Use millisecond timer for observe option values
This commit is contained in:
benpicco 2023-11-14 10:26:18 +00:00 committed by GitHub
commit 81fd126d3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 11 deletions

View File

@ -517,7 +517,6 @@ ifneq (,$(filter gcoap,$(USEMODULE)))
USEMODULE += sock_udp
USEMODULE += sock_util
USEMODULE += ztimer_msec
USEMODULE += ztimer_usec
USEMODULE += event_callback
USEMODULE += event_timeout_ztimer
USEMODULE += random

View File

@ -600,11 +600,11 @@ extern "C" {
* @brief See CONFIG_GCOAP_OBS_VALUE_WIDTH
*/
#if (CONFIG_GCOAP_OBS_VALUE_WIDTH == 3)
#define GCOAP_OBS_TICK_EXPONENT (5)
#define GCOAP_OBS_TICK_EXPONENT (0)
#elif (CONFIG_GCOAP_OBS_VALUE_WIDTH == 2)
#define GCOAP_OBS_TICK_EXPONENT (16)
#define GCOAP_OBS_TICK_EXPONENT (6)
#elif (CONFIG_GCOAP_OBS_VALUE_WIDTH == 1)
#define GCOAP_OBS_TICK_EXPONENT (24)
#define GCOAP_OBS_TICK_EXPONENT (14)
#endif
/**

View File

@ -1630,6 +1630,14 @@ ssize_t gcoap_req_send_tl(const uint8_t *buf, size_t len,
return ((res > 0 || res == -ENOTCONN) ? res : 0);
}
static void _add_generated_observe_option(coap_pkt_t *pdu)
{
/* generate initial notification value */
uint32_t now = ztimer_now(ZTIMER_MSEC);
pdu->observe_value = (now >> GCOAP_OBS_TICK_EXPONENT) & 0xFFFFFF;
coap_opt_add_uint(pdu, COAP_OPT_OBSERVE, pdu->observe_value);
}
int gcoap_resp_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code)
{
int header_len = coap_build_reply(pdu, code, buf, len, 0);
@ -1644,10 +1652,7 @@ int gcoap_resp_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code)
pdu->payload_len = len - header_len;
if (coap_get_observe(pdu) == COAP_OBS_REGISTER) {
/* generate initial notification value */
uint32_t now = ztimer_now(ZTIMER_USEC);
pdu->observe_value = (now >> GCOAP_OBS_TICK_EXPONENT) & 0xFFFFFF;
coap_opt_add_uint(pdu, COAP_OPT_OBSERVE, pdu->observe_value);
_add_generated_observe_option(pdu);
}
return 0;
@ -1672,9 +1677,7 @@ int gcoap_obs_init(coap_pkt_t *pdu, uint8_t *buf, size_t len,
if (hdrlen > 0) {
coap_pkt_init(pdu, buf, len, hdrlen);
uint32_t now = ztimer_now(ZTIMER_USEC);
pdu->observe_value = (now >> GCOAP_OBS_TICK_EXPONENT) & 0xFFFFFF;
coap_opt_add_uint(pdu, COAP_OPT_OBSERVE, pdu->observe_value);
_add_generated_observe_option(pdu);
return GCOAP_OBS_INIT_OK;
}