Skip to content

Commit

Permalink
System time read/write callbacks (#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 committed Jun 9, 2021
1 parent 9c6fe22 commit b822013
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 24 deletions.
32 changes: 26 additions & 6 deletions features/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
@@ -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 features/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

0 comments on commit b822013

Please sign in to comment.