Skip to content

Commit

Permalink
System time read/write callbacks (ARMmbed#2637)
Browse files Browse the repository at this point in the history
- Add new API for setting system time read and write callbacks.
- Update ws_pae to use the new time service.
  • Loading branch information
Arto Kinnunen authored Jun 9, 2021
1 parent 7905df6 commit 6d290dc
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Filter EAPOL parents based on DEVICE_MIN_SENS configuration with Threshold and Hysteresis to prevent EAPOL failures caused by bad signal levels
* Adaptation layer removes oldest packets first when stack needs to release memory or reduce traffic
* Use QoS specific timeout to drop old packets from TX queue to prioritize limited bandwidth.
* Added network time syncronisation support. Network node can update device current time if accurate time was received from the network.

### Changes
* Statistics for MAC data request latencies. Separated to adaptation layer and MAC queueing delays.
Expand Down
32 changes: 26 additions & 6 deletions nanostack/ns_time_api.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Pelion 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_ */
5 changes: 0 additions & 5 deletions source/6LoWPAN/ws/ws_empty_functions.c
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
22 changes: 9 additions & 13 deletions source/6LoWPAN/ws/ws_pae_time.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Pelion 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 */

55 changes: 55 additions & 0 deletions source/Service_Libs/utils/ns_time.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;
}
56 changes: 56 additions & 0 deletions source/Service_Libs/utils/ns_time.h
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 sources.mk
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,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

0 comments on commit 6d290dc

Please sign in to comment.