Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add system time read/write callbacks to mbed-mesh-api #14817

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions connectivity/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
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 @@ -110,6 +125,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
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 connectivity/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);
}
32 changes: 26 additions & 6 deletions connectivity/nanostack/sal-stack-nanostack/nanostack/ns_time_api.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Arm Limited and affiliates.
* Copyright (c) 2020-2021, Pelion and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -29,7 +29,7 @@
#include "ns_types.h"

/**
* System time callback.
* System time read callback.
*
* Callback shall return the system time in seconds after 1970.
*
Expand All @@ -39,13 +39,33 @@
typedef uint64_t ns_time_api_system_time_callback(void);

/**
* System time callback set.
* System time write callback.
*
* Sets callback for the system time.
* Callback will write the time in seconds after 1970.
*
* \param callback system time callback
* \param seconds system time in seconds
*
*/
typedef void ns_time_api_system_time_write_callback(uint64_t write_time);

/**
* System time read callback set.
*
* Sets callback for the system time read.
*
* \param callback_rd system time read callback
*
*/
void ns_time_api_system_time_callback_set(ns_time_api_system_time_callback callback_rd);

/**
* Set system time write callback.
*
* Sets system time write callback.
*
* \param callback_wr system time write callback.
*
*/
void ns_time_api_system_time_callback_set(ns_time_api_system_time_callback callback);
void ns_time_api_system_time_write_callback_set(ns_time_api_system_time_write_callback callback_wr);

#endif /* NS_TIME_API_H_ */
Original file line number Diff line number Diff line change
Expand Up @@ -440,11 +440,6 @@ int ws_statistics_stop(int8_t interface_id)
return -1;
}

void ns_time_api_system_time_callback_set(ns_time_api_system_time_callback callback)
{
(void) callback;
}

int ws_stack_info_get(int8_t interface_id, ws_stack_info_t *info_ptr)
{
(void) interface_id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Arm Limited and affiliates.
* Copyright (c) 2020-2021, Pelion and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -25,6 +25,7 @@
#include "6LoWPAN/ws/ws_pae_time.h"
#include "Security/protocols/sec_prot_certs.h"
#include "Security/protocols/sec_prot_keys.h"
#include "Service_Libs/utils/ns_time.h"

#ifdef HAVE_WS

Expand All @@ -34,7 +35,6 @@
#define CURRENT_TIME_INIT_VALUE 1577836800

static uint64_t current_time = CURRENT_TIME_INIT_VALUE;
static ns_time_api_system_time_callback *system_time_callback = NULL;

uint16_t ws_pae_time_to_short_convert(uint32_t time)
{
Expand Down Expand Up @@ -148,8 +148,9 @@ int8_t ws_pae_time_diff_calc(uint64_t curr_time, uint64_t comp_time, uint32_t *t

uint64_t ws_pae_current_time_get(void)
{
if (system_time_callback) {
return system_time_callback();
uint64_t new_time;
if (ns_time_system_time_read(&new_time) == 0) {
return new_time;
}

return current_time;
Expand All @@ -162,26 +163,21 @@ void ws_pae_current_time_update(uint16_t seconds)

int8_t ws_pae_current_time_set(uint64_t time)
{
uint64_t new_system_time;
current_time = time;

tr_debug("Current time set: %"PRIi64, time);

if (system_time_callback) {
uint64_t system_time = system_time_callback();
if (ns_time_system_time_read(&new_system_time) == 0) {
// System time has gone backwards
if (system_time < current_time || system_time > current_time + SYSTEM_TIME_MAXIMUM_DIFF) {
tr_error("FATAL: system time less than reference time or more than 12 months in future: %"PRIi64" reference time: %"PRIi64, system_time, current_time);
if (new_system_time < current_time || new_system_time > current_time + SYSTEM_TIME_MAXIMUM_DIFF) {
tr_error("FATAL: system time less than reference time or more than 12 months in future: %"PRIi64" reference time: %"PRIi64, new_system_time, current_time);
return -1;
}
}

return 0;
}

void ns_time_api_system_time_callback_set(ns_time_api_system_time_callback callback)
{
system_time_callback = callback;
}

#endif /* HAVE_WS */

Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ target_sources(mbed-nanostack-sal_stack
utils/ns_conf.c
utils/ns_crc.c
utils/ns_file_system.c
utils/ns_time.c

whiteboard/whiteboard.c
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2021, Pelion and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <string.h>
#include <stdio.h>
#include "ns_types.h"
#include "ns_time_api.h" //ns_time_api_system_time_callback

static ns_time_api_system_time_callback *system_time_read_callback = NULL;
static ns_time_api_system_time_write_callback *system_time_write_callback = NULL;

void ns_time_api_system_time_callback_set(ns_time_api_system_time_callback callback_rd)
{
system_time_read_callback = callback_rd;
}

void ns_time_api_system_time_write_callback_set(ns_time_api_system_time_write_callback callback_wr)
{
system_time_write_callback = callback_wr;
}

int ns_time_system_time_write(uint64_t time_write)
{
if (system_time_write_callback) {
system_time_write_callback(time_write);
return 0;
}

return -1;
}

int ns_time_system_time_read(uint64_t *time_read)
{
if (system_time_read_callback && time_read) {
uint64_t new_time = system_time_read_callback();
*time_read = new_time;
return 0;
}

return -1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2021, Pelion and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef _NS_TIME_H_
#define _NS_TIME_H_

/**
* \file ns_time.h
* \brief Nanostack internal time handling API.
*/

/**
* Write new time as a platform time
*
* Write a new time to platform provided time system.
* Platform time callbacks must be set by using method ns_time_api_system_time_callbacks_set.
*
* \param time_write time to be written as a new system time.
*
* \return 0 in success.
* \return <0 in case of errors.
*
*/
int ns_time_system_time_write(uint64_t time_write);

/**
* Read platform time from a time callback
*
* Read a new time from time system provided by the platform.
* Platform time callbacks must be set by using the method ns_time_api_system_time_callbacks_set.
*
* \param time_read Address to variable where new time will be written.
*
* \return 0 in success.
* \return <0 in case of errors.
*
*/
int ns_time_system_time_read(uint64_t *time_read);



#endif /* _NS_TIME_H_ */
1 change: 1 addition & 0 deletions connectivity/nanostack/sal-stack-nanostack/sources.mk
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ SRCS += \
source/Service_Libs/utils/ns_crc.c \
source/Service_Libs/utils/isqrt.c \
source/Service_Libs/utils/ns_file_system.c \
source/Service_Libs/utils/ns_time.c \
source/Service_Libs/utils/ns_conf.c \
source/Service_Libs/mdns/ns_mdns_api.c \
source/Service_Libs/mdns/ns_fnet_port.c \
Expand Down