diff --git a/sys/Makefile.dep b/sys/Makefile.dep index 8a7771accc..5339c1d8d3 100644 --- a/sys/Makefile.dep +++ b/sys/Makefile.dep @@ -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 diff --git a/sys/include/net/gcoap.h b/sys/include/net/gcoap.h index 89070169d9..96f7229312 100644 --- a/sys/include/net/gcoap.h +++ b/sys/include/net/gcoap.h @@ -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 /** diff --git a/sys/net/application_layer/gcoap/gcoap.c b/sys/net/application_layer/gcoap/gcoap.c index 6b4641da12..04954fcfb7 100644 --- a/sys/net/application_layer/gcoap/gcoap.c +++ b/sys/net/application_layer/gcoap/gcoap.c @@ -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; }