Skip to content

Commit 100435d

Browse files
Damian-Nordicnordicjm
authored andcommitted
net: openthread: rpc: extrapolate radio time
Enhance otLinkRawGetRadioTime() serialization to extrapolate the radio time from the last radio time reading and the current local time, if the sync has occurred recently. This allows to reduce the RPC traffic given that radio time may need to be read frequently by an application. Signed-off-by: Damian Krolik <[email protected]>
1 parent 6ddb12b commit 100435d

File tree

3 files changed

+45
-9
lines changed

3 files changed

+45
-9
lines changed

subsys/net/openthread/rpc/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ config OPENTHREAD_RPC_CLIENT_NUM_SENT_COAP_REQUESTS
6565
Defines the number of slots for storing a callback along with its context
6666
for ongoing CoAP requests awaiting a response.
6767

68+
config OPENTHREAD_RPC_CLIENT_RADIO_TIME_REFRESH_PERIOD
69+
int "Radio time sync period [s]"
70+
default 30
71+
help
72+
Defines how frequently the RPC client shall read the RPC server's
73+
radio time instead of extrapolating it from the last reading and the
74+
current local time. The purpose of this setting is to reduce the RPC
75+
traffic given that otLinkRawGetRadioTime() may be used extensively by
76+
the application.
77+
6878
endmenu # "OpenThread over RPC client configuration"
6979

7080
menu "OpenThread over RPC client configuration"

subsys/net/openthread/rpc/client/ot_rpc_link_raw.c

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,46 @@
1212

1313
#include <openthread/link_raw.h>
1414

15+
#include <zephyr/kernel.h>
16+
17+
#define RADIO_TIME_REFRESH_PERIOD (CONFIG_OPENTHREAD_RPC_CLIENT_RADIO_TIME_REFRESH_PERIOD * 1000000)
18+
19+
static bool sync_radio_time_valid;
20+
static uint64_t sync_local_time;
21+
static uint64_t sync_radio_time;
22+
23+
static inline uint64_t get_local_time(void)
24+
{
25+
return k_ticks_to_us_floor64(k_uptime_ticks());
26+
}
27+
1528
uint64_t otLinkRawGetRadioTime(otInstance *aInstance)
1629
{
1730
struct nrf_rpc_cbor_ctx ctx;
18-
uint64_t time;
31+
uint64_t now;
1932

2033
ARG_UNUSED(aInstance);
2134

22-
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, 0);
23-
nrf_rpc_cbor_cmd_rsp_no_err(&ot_group, OT_RPC_CMD_LINK_RAW_GET_RADIO_TIME, &ctx);
24-
25-
time = nrf_rpc_decode_uint64(&ctx);
26-
27-
if (!nrf_rpc_decoding_done_and_check(&ot_group, &ctx)) {
28-
ot_rpc_report_rsp_decoding_error(OT_RPC_CMD_LINK_RAW_GET_RADIO_TIME);
35+
now = get_local_time();
36+
37+
if (!sync_radio_time_valid || now >= sync_local_time + RADIO_TIME_REFRESH_PERIOD) {
38+
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, 0);
39+
nrf_rpc_cbor_cmd_rsp_no_err(&ot_group, OT_RPC_CMD_LINK_RAW_GET_RADIO_TIME, &ctx);
40+
41+
/*
42+
* Calculate sync_local_time as if the radio time was read by the peer exactly
43+
* halfway between constructing a request and receiving the response.
44+
*/
45+
sync_radio_time_valid = true;
46+
sync_local_time = now / 2;
47+
now = get_local_time();
48+
sync_local_time += now / 2;
49+
sync_radio_time = nrf_rpc_decode_uint64(&ctx);
50+
51+
if (!nrf_rpc_decoding_done_and_check(&ot_group, &ctx)) {
52+
ot_rpc_report_rsp_decoding_error(OT_RPC_CMD_LINK_RAW_GET_RADIO_TIME);
53+
}
2954
}
3055

31-
return time;
56+
return sync_radio_time + (now - sync_local_time);
3257
}

tests/subsys/net/openthread/rpc/client/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CONFIG_TEST_RANDOM_GENERATOR=y
1010

1111
CONFIG_OPENTHREAD_RPC=y
1212
CONFIG_OPENTHREAD_RPC_CLIENT=y
13+
CONFIG_OPENTHREAD_RPC_CLIENT_RADIO_TIME_REFRESH_PERIOD=0
1314
CONFIG_NETWORKING=y
1415
CONFIG_NRF_RPC_ZCBOR_BACKUPS=1
1516
CONFIG_NRF_RPC_CBKPROXY_OUT_SLOTS=0

0 commit comments

Comments
 (0)