Skip to content

Commit b822013

Browse files
author
Arto Kinnunen
committed
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.
1 parent 9c6fe22 commit b822013

File tree

6 files changed

+147
-24
lines changed

6 files changed

+147
-24
lines changed

features/nanostack/sal-stack-nanostack/nanostack/ns_time_api.h

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Arm Limited and affiliates.
2+
* Copyright (c) 2020-2021, Pelion and affiliates.
33
* SPDX-License-Identifier: Apache-2.0
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -29,7 +29,7 @@
2929
#include "ns_types.h"
3030

3131
/**
32-
* System time callback.
32+
* System time read callback.
3333
*
3434
* Callback shall return the system time in seconds after 1970.
3535
*
@@ -39,13 +39,33 @@
3939
typedef uint64_t ns_time_api_system_time_callback(void);
4040

4141
/**
42-
* System time callback set.
42+
* System time write callback.
4343
*
44-
* Sets callback for the system time.
44+
* Callback will write the time in seconds after 1970.
4545
*
46-
* \param callback system time callback
46+
* \param seconds system time in seconds
47+
*
48+
*/
49+
typedef void ns_time_api_system_time_write_callback(uint64_t write_time);
50+
51+
/**
52+
* System time read callback set.
53+
*
54+
* Sets callback for the system time read.
55+
*
56+
* \param callback_rd system time read callback
57+
*
58+
*/
59+
void ns_time_api_system_time_callback_set(ns_time_api_system_time_callback callback_rd);
60+
61+
/**
62+
* Set system time write callback.
63+
*
64+
* Sets system time write callback.
65+
*
66+
* \param callback_wr system time write callback.
4767
*
4868
*/
49-
void ns_time_api_system_time_callback_set(ns_time_api_system_time_callback callback);
69+
void ns_time_api_system_time_write_callback_set(ns_time_api_system_time_write_callback callback_wr);
5070

5171
#endif /* NS_TIME_API_H_ */

features/nanostack/sal-stack-nanostack/source/6LoWPAN/ws/ws_empty_functions.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -440,11 +440,6 @@ int ws_statistics_stop(int8_t interface_id)
440440
return -1;
441441
}
442442

443-
void ns_time_api_system_time_callback_set(ns_time_api_system_time_callback callback)
444-
{
445-
(void) callback;
446-
}
447-
448443
int ws_stack_info_get(int8_t interface_id, ws_stack_info_t *info_ptr)
449444
{
450445
(void) interface_id;

features/nanostack/sal-stack-nanostack/source/6LoWPAN/ws/ws_pae_time.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, Arm Limited and affiliates.
2+
* Copyright (c) 2020-2021, Pelion and affiliates.
33
* SPDX-License-Identifier: Apache-2.0
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -25,6 +25,7 @@
2525
#include "6LoWPAN/ws/ws_pae_time.h"
2626
#include "Security/protocols/sec_prot_certs.h"
2727
#include "Security/protocols/sec_prot_keys.h"
28+
#include "Service_Libs/utils/ns_time.h"
2829

2930
#ifdef HAVE_WS
3031

@@ -34,7 +35,6 @@
3435
#define CURRENT_TIME_INIT_VALUE 1577836800
3536

3637
static uint64_t current_time = CURRENT_TIME_INIT_VALUE;
37-
static ns_time_api_system_time_callback *system_time_callback = NULL;
3838

3939
uint16_t ws_pae_time_to_short_convert(uint32_t time)
4040
{
@@ -148,8 +148,9 @@ int8_t ws_pae_time_diff_calc(uint64_t curr_time, uint64_t comp_time, uint32_t *t
148148

149149
uint64_t ws_pae_current_time_get(void)
150150
{
151-
if (system_time_callback) {
152-
return system_time_callback();
151+
uint64_t new_time;
152+
if (ns_time_system_time_read(&new_time) == 0) {
153+
return new_time;
153154
}
154155

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

163164
int8_t ws_pae_current_time_set(uint64_t time)
164165
{
166+
uint64_t new_system_time;
165167
current_time = time;
166168

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

169-
if (system_time_callback) {
170-
uint64_t system_time = system_time_callback();
171+
if (ns_time_system_time_read(&new_system_time) == 0) {
171172
// System time has gone backwards
172-
if (system_time < current_time || system_time > current_time + SYSTEM_TIME_MAXIMUM_DIFF) {
173-
tr_error("FATAL: system time less than reference time or more than 12 months in future: %"PRIi64" reference time: %"PRIi64, system_time, current_time);
173+
if (new_system_time < current_time || new_system_time > current_time + SYSTEM_TIME_MAXIMUM_DIFF) {
174+
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);
174175
return -1;
175176
}
176177
}
177178

178179
return 0;
179180
}
180181

181-
void ns_time_api_system_time_callback_set(ns_time_api_system_time_callback callback)
182-
{
183-
system_time_callback = callback;
184-
}
185-
186182
#endif /* HAVE_WS */
187183

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2021, Pelion and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include <string.h>
19+
#include <stdio.h>
20+
#include "ns_types.h"
21+
#include "ns_time_api.h" //ns_time_api_system_time_callback
22+
23+
static ns_time_api_system_time_callback *system_time_read_callback = NULL;
24+
static ns_time_api_system_time_write_callback *system_time_write_callback = NULL;
25+
26+
void ns_time_api_system_time_callback_set(ns_time_api_system_time_callback callback_rd)
27+
{
28+
system_time_read_callback = callback_rd;
29+
}
30+
31+
void ns_time_api_system_time_write_callback_set(ns_time_api_system_time_write_callback callback_wr)
32+
{
33+
system_time_write_callback = callback_wr;
34+
}
35+
36+
int ns_time_system_time_write(uint64_t time_write)
37+
{
38+
if (system_time_write_callback) {
39+
system_time_write_callback(time_write);
40+
return 0;
41+
}
42+
43+
return -1;
44+
}
45+
46+
int ns_time_system_time_read(uint64_t *time_read)
47+
{
48+
if (system_time_read_callback && time_read) {
49+
uint64_t new_time = system_time_read_callback();
50+
*time_read = new_time;
51+
return 0;
52+
}
53+
54+
return -1;
55+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (c) 2021, Pelion and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#ifndef _NS_TIME_H_
19+
#define _NS_TIME_H_
20+
21+
/**
22+
* \file ns_time.h
23+
* \brief Nanostack internal time handling API.
24+
*/
25+
26+
/**
27+
* Write new time as a platform time
28+
*
29+
* Write a new time to platform provided time system.
30+
* Platform time callbacks must be set by using method ns_time_api_system_time_callbacks_set.
31+
*
32+
* \param time_write time to be written as a new system time.
33+
*
34+
* \return 0 in success.
35+
* \return <0 in case of errors.
36+
*
37+
*/
38+
int ns_time_system_time_write(uint64_t time_write);
39+
40+
/**
41+
* Read platform time from a time callback
42+
*
43+
* Read a new time from time system provided by the platform.
44+
* Platform time callbacks must be set by using the method ns_time_api_system_time_callbacks_set.
45+
*
46+
* \param time_read Address to variable where new time will be written.
47+
*
48+
* \return 0 in success.
49+
* \return <0 in case of errors.
50+
*
51+
*/
52+
int ns_time_system_time_read(uint64_t *time_read);
53+
54+
55+
56+
#endif /* _NS_TIME_H_ */

features/nanostack/sal-stack-nanostack/sources.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ SRCS += \
211211
source/Service_Libs/utils/ns_crc.c \
212212
source/Service_Libs/utils/isqrt.c \
213213
source/Service_Libs/utils/ns_file_system.c \
214+
source/Service_Libs/utils/ns_time.c \
214215
source/Service_Libs/utils/ns_conf.c \
215216
source/Service_Libs/mdns/ns_mdns_api.c \
216217
source/Service_Libs/mdns/ns_fnet_port.c \

0 commit comments

Comments
 (0)