Skip to content

Commit

Permalink
Enable nanostack system time read/write
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Arto Kinnunen committed Jun 9, 2021
1 parent b822013 commit c20ee2a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions features/nanostack/mbed-mesh-api/mbed_lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
19 changes: 19 additions & 0 deletions features/nanostack/mbed-mesh-api/source/MeshInterfaceNanostack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}

Expand Down
7 changes: 7 additions & 0 deletions features/nanostack/mbed-mesh-api/source/include/mesh_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions features/nanostack/mbed-mesh-api/source/mesh_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
}

0 comments on commit c20ee2a

Please sign in to comment.