forked from nrfconnect/sdk-nrf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.c
182 lines (138 loc) · 4.95 KB
/
clock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
* Copyright (c) 2024-2025 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#include <zephyr/kernel.h>
#include <zephyr/settings/settings.h>
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(fp_fmdn_clock, CONFIG_BT_FAST_PAIR_LOG_LEVEL);
#include <bluetooth/services/fast_pair/fast_pair.h>
#include "fp_activation.h"
#include "fp_fmdn_clock.h"
#include "fp_storage_clock.h"
static uint32_t storage_clock_boot_checkpoint;
static int64_t uptime_on_boot;
static void fmdn_clock_storage_work_handle(struct k_work *work);
static K_WORK_DELAYABLE_DEFINE(fmdn_clock_storage_work,
fmdn_clock_storage_work_handle);
static bool is_enabled;
static uint32_t fmdn_clock_read(void)
{
int64_t sys_uptime;
uint32_t fmdn_clock;
/* Calculate elapsed time since bootup. */
sys_uptime = k_uptime_get();
/* Ensure that uptime starts from zero after a system reset by subtracting
* the timestamp during the system initialization.
*/
if (IS_ENABLED(CONFIG_BT_FAST_PAIR_FMDN_CLOCK_UPTIME_PERSISTENCE)) {
__ASSERT_NO_MSG(sys_uptime >= uptime_on_boot);
sys_uptime -= uptime_on_boot;
}
/* Convert from milliseconds to seconds. */
sys_uptime /= MSEC_PER_SEC;
/* Cast the uptime value in seconds onto the uint32_t type.
* The uint32_t type of clock value should allow over a hundred
* years of operation without the overflow:
* 2^32 / 3600 / 24 / 365 ~ 136 years
*/
fmdn_clock = sys_uptime;
/* Calculate the absolute time using the checkpoint from NVM. */
fmdn_clock += storage_clock_boot_checkpoint;
return fmdn_clock;
}
uint32_t fp_fmdn_clock_read(void)
{
uint32_t fmdn_clock;
__ASSERT_NO_MSG(is_enabled);
fmdn_clock = fmdn_clock_read();
LOG_DBG("FMDN Clock: reading the last value: %u [s]", fmdn_clock);
return fmdn_clock;
}
static void fmdn_clock_storage_work_handle(struct k_work *work)
{
int err;
uint32_t fmdn_clock;
__ASSERT_NO_MSG(is_enabled);
ARG_UNUSED(work);
fmdn_clock = fmdn_clock_read();
/* Set up the subsequent work handler for updating the clock value in NVM. */
(void) k_work_reschedule(&fmdn_clock_storage_work,
K_MINUTES(CONFIG_BT_FAST_PAIR_FMDN_CLOCK_NVM_UPDATE_TIME));
err = fp_storage_clock_checkpoint_update(fmdn_clock);
if (err) {
/* Retry the storage operation on error. */
(void) k_work_reschedule(&fmdn_clock_storage_work,
K_SECONDS(CONFIG_BT_FAST_PAIR_FMDN_CLOCK_NVM_UPDATE_RETRY_TIME));
LOG_ERR("FMDN Clock: fp_storage_clock_checkpoint_update failed: %d", err);
} else {
LOG_DBG("FMDN Clock: storing the last value: %u [s]", fmdn_clock);
}
}
static int fp_fmdn_clock_init(void)
{
int err;
uint32_t fmdn_clock;
uint32_t elapsed_time;
uint32_t current_checkpoint = 0;
static const uint32_t update_period =
CONFIG_BT_FAST_PAIR_FMDN_CLOCK_NVM_UPDATE_TIME * 60;
if (is_enabled) {
LOG_WRN("FMDN Clock: module already initialized");
return 0;
}
err = fp_storage_clock_boot_checkpoint_get(&storage_clock_boot_checkpoint);
if (err) {
LOG_ERR("FMDN Clock: fp_storage_clock_boot_checkpoint_get failed: %d", err);
return err;
}
err = fp_storage_clock_current_checkpoint_get(¤t_checkpoint);
if (err) {
LOG_ERR("FMDN Clock: fp_storage_clock_current_checkpoint_get failed: %d", err);
return err;
}
__ASSERT_NO_MSG(current_checkpoint >= storage_clock_boot_checkpoint);
/* Calculate the time from the last update. */
fmdn_clock = fmdn_clock_read();
__ASSERT_NO_MSG(fmdn_clock >= current_checkpoint);
/* Set up the first work handler for updating the clock value in NVM. */
elapsed_time = (fmdn_clock - current_checkpoint);
if (elapsed_time < update_period) {
const uint32_t update_time = (update_period - elapsed_time);
(void) k_work_reschedule(&fmdn_clock_storage_work, K_SECONDS(update_time));
} else {
(void) k_work_reschedule(&fmdn_clock_storage_work, K_NO_WAIT);
}
is_enabled = true;
return 0;
}
static int fp_fmdn_clock_uninit(void)
{
if (!is_enabled) {
LOG_WRN("FMDN Clock: module already uninitialized");
return 0;
}
is_enabled = false;
(void) k_work_cancel_delayable(&fmdn_clock_storage_work);
return 0;
}
/* The clock module requires clock storage module for initialization. */
BUILD_ASSERT(CONFIG_BT_FAST_PAIR_STORAGE_INTEGRATION_INIT_PRIORITY <
FP_ACTIVATION_INIT_PRIORITY_DEFAULT);
FP_ACTIVATION_MODULE_REGISTER(fp_fmdn_clock,
FP_ACTIVATION_INIT_PRIORITY_DEFAULT,
fp_fmdn_clock_init,
fp_fmdn_clock_uninit);
#if defined(CONFIG_BT_FAST_PAIR_FMDN_CLOCK_UPTIME_PERSISTENCE)
static int bt_fast_pair_fmdn_clock_uptime_persistence_workaround_init(void)
{
uptime_on_boot = k_uptime_get();
return 0;
}
/* Define the system initialization hook with the priority level that is executed as closely
* as possible to the kernel initialization. The kernel uptime value should be read as soon
* as possible after the kernel starts to increase accuracy of the FMDN clock.
*/
SYS_INIT(bt_fast_pair_fmdn_clock_uptime_persistence_workaround_init, POST_KERNEL, 0);
#endif /* defined(CONFIG_BT_FAST_PAIR_FMDN_CLOCK_UPTIME_PERSISTENCE) */