From c20ee2aa85469a86e943d46db96e3a2cdb2ac885 Mon Sep 17 00:00:00 2001 From: Arto Kinnunen Date: Wed, 9 Jun 2021 09:00:26 +0300 Subject: [PATCH] Enable nanostack system time read/write Allow Nanostack to read and write system time to synchronise time in the mesh network. By default feature is enabled in the mesh json- configuration. --- .../nanostack/mbed-mesh-api/mbed_lib.json | 4 ++++ .../source/MeshInterfaceNanostack.cpp | 19 +++++++++++++++++++ .../source/include/mesh_system.h | 7 +++++++ .../mbed-mesh-api/source/mesh_system.c | 7 +++++++ 4 files changed, 37 insertions(+) diff --git a/features/nanostack/mbed-mesh-api/mbed_lib.json b/features/nanostack/mbed-mesh-api/mbed_lib.json index 4278e60141c..f0c855733c1 100644 --- a/features/nanostack/mbed-mesh-api/mbed_lib.json +++ b/features/nanostack/mbed-mesh-api/mbed_lib.json @@ -24,6 +24,10 @@ "help": "Definition of heap statistics `mem_stat_t` storage.", "value": null }, + "system-time-update-from-nanostack": { + "help": "Allow nanostack to read and write device system time to synchronise time in the network. Feature enabled when set to true, false otherwise.", + "value": true + }, "6lowpan-nd-channel-mask": { "help": "Channel mask, bit-mask of channels to use. [0-0x07fff800]", "value": "0x7fff800" diff --git a/features/nanostack/mbed-mesh-api/source/MeshInterfaceNanostack.cpp b/features/nanostack/mbed-mesh-api/source/MeshInterfaceNanostack.cpp index f09f2175efb..2ac81462966 100644 --- a/features/nanostack/mbed-mesh-api/source/MeshInterfaceNanostack.cpp +++ b/features/nanostack/mbed-mesh-api/source/MeshInterfaceNanostack.cpp @@ -22,6 +22,21 @@ #include "thread_management_if.h" #include "ip6string.h" #include "mbed_error.h" +#include "mbed_rtc_time.h" + +#if (MBED_CONF_MBED_MESH_API_SYSTEM_TIME_UPDATE_FROM_NANOSTACK == true) +static uint64_t time_read_callback(void) +{ + time_t seconds = time(NULL); + + return (uint64_t)seconds; +} + +static void time_write_callback(uint64_t time_write) +{ + set_time((time_t)time_write); +} +#endif /* MBED_CONF_MBED_MESH_API_SYSTEM_TIME_UPDATE_FROM_NANOSTACK */ nsapi_error_t Nanostack::Interface::get_ip_address(SocketAddress *address) { @@ -142,6 +157,10 @@ int InterfaceNanostack::connect() return error; } +#if (MBED_CONF_MBED_MESH_API_SYSTEM_TIME_UPDATE_FROM_NANOSTACK == true) + mesh_system_time_callback_set(time_read_callback, time_write_callback); +#endif /* MBED_CONF_MBED_MESH_API_SYSTEM_TIME_UPDATE_FROM_NANOSTACK */ + return _interface->bringup(false, NULL, NULL, NULL, IPV6_STACK, _blocking); } diff --git a/features/nanostack/mbed-mesh-api/source/include/mesh_system.h b/features/nanostack/mbed-mesh-api/source/include/mesh_system.h index b075ec60042..a6525c508cd 100644 --- a/features/nanostack/mbed-mesh-api/source/include/mesh_system.h +++ b/features/nanostack/mbed-mesh-api/source/include/mesh_system.h @@ -32,6 +32,11 @@ enum { APPL_BACKHAUL_LINK_UP }; + +typedef uint64_t ns_time_read_cb(void); +typedef void ns_time_write_cb(uint64_t); + + /* * \brief Send application connect event to receiver tasklet to * ensure that connection is made in right tasklet. @@ -40,6 +45,8 @@ void mesh_system_send_connect_event(uint8_t receiver); int mesh_system_set_file_system_root_path(const char *root_path); +void mesh_system_time_callback_set(ns_time_read_cb, ns_time_write_cb); + /* * \brief Initialize mesh system. * Memory pool, timers, traces and support are initialized. diff --git a/features/nanostack/mbed-mesh-api/source/mesh_system.c b/features/nanostack/mbed-mesh-api/source/mesh_system.c index 59fa78582fa..28bdabef084 100644 --- a/features/nanostack/mbed-mesh-api/source/mesh_system.c +++ b/features/nanostack/mbed-mesh-api/source/mesh_system.c @@ -26,6 +26,7 @@ #include "mbed_assert.h" #include "mbed_error.h" #include "ns_file_system.h" +#include "ns_time_api.h" // For tracing we need to define flag, have include and define group #define HAVE_DEBUG 1 #include "ns_trace.h" @@ -83,3 +84,9 @@ int mesh_system_set_file_system_root_path(const char *root_path) { return ns_file_system_set_root_path(root_path); } + +void mesh_system_time_callback_set(ns_time_read_cb read_cb, ns_time_write_cb write_cb) +{ + ns_time_api_system_time_callback_set(read_cb); + ns_time_api_system_time_write_callback_set(write_cb); +}