System time read/write callbacks (#2637)

- Add new API for setting system time read and write callbacks.
- Update ws_pae to use the new time service.
pull/14818/head
Arto Kinnunen 2021-06-09 13:14:54 +03:00 committed by Arto Kinnunen
parent 32ecdb34b3
commit bad78ee326
6 changed files with 147 additions and 24 deletions

View File

@ -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");
@ -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.
*
@ -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
*
*/
void ns_time_api_system_time_callback_set(ns_time_api_system_time_callback callback);
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_write_callback_set(ns_time_api_system_time_write_callback callback_wr);
#endif /* NS_TIME_API_H_ */

View File

@ -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;

View File

@ -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");
@ -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
@ -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)
{
@ -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;
@ -162,15 +163,15 @@ 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;
}
}
@ -178,10 +179,5 @@ int8_t ws_pae_current_time_set(uint64_t time)
return 0;
}
void ns_time_api_system_time_callback_set(ns_time_api_system_time_callback callback)
{
system_time_callback = callback;
}
#endif /* HAVE_WS */

View File

@ -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;
}

View File

@ -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_ */

View File

@ -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 \