From 6425fda812b06a9261a53ddbbee78ed296362067 Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Fri, 10 Nov 2023 16:00:00 +0100 Subject: [PATCH 1/2] gcoap: Use millisecond timer for observe option values --- sys/Makefile.dep | 1 - sys/include/net/gcoap.h | 6 +++--- sys/net/application_layer/gcoap/gcoap.c | 4 ++-- 3 files changed, 5 insertions(+), 6 deletions(-) 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..d52ccdf596 100644 --- a/sys/net/application_layer/gcoap/gcoap.c +++ b/sys/net/application_layer/gcoap/gcoap.c @@ -1645,7 +1645,7 @@ int gcoap_resp_init(coap_pkt_t *pdu, uint8_t *buf, size_t len, unsigned code) if (coap_get_observe(pdu) == COAP_OBS_REGISTER) { /* generate initial notification value */ - uint32_t now = ztimer_now(ZTIMER_USEC); + 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); } @@ -1672,7 +1672,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); + 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); From a6a8f6e1cdd2afed48be1d71ba849f2fd760e4d3 Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Fri, 10 Nov 2023 16:01:10 +0100 Subject: [PATCH 2/2] gcoap: Separate out observe option generation in own function --- sys/net/application_layer/gcoap/gcoap.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/sys/net/application_layer/gcoap/gcoap.c b/sys/net/application_layer/gcoap/gcoap.c index d52ccdf596..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_MSEC); - 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_MSEC); - 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; }