Bluenet  5.7.0
Bluenet, firmware for nRF52 smart home devices
Loading...
Searching...
No Matches
cs_SystemTime.h
Go to the documentation of this file.
8#pragma once
9
10#include <drivers/cs_Timer.h>
13#include <stdint.h>
14#include <test/cs_TestAccess.h>
15#include <time/cs_Time.h>
16#include <time/cs_TimeOfDay.h>
18#include <util/cs_Coroutine.h>
19
49class SystemTime : public EventListener {
50 friend class TestAccess<SystemTime>;
51
52public:
56 void init();
57
58 virtual void handleEvent(event_t& event);
59 virtual ~SystemTime() = default;
60
61 // ======================== Utility functions ========================
62
68 static uint32_t posix();
69
73 static Time now();
74
78 static DayOfWeek day();
79
83 static uint32_t up();
84
91
92 // ======================== Setters ========================
93
107 static cs_ret_code_t setTime(uint32_t time, bool throttled, bool sendToMesh);
108
117 static cs_ret_code_t setSunTimes(const sun_time_t& sunTimes, bool throttled = true);
118
119private:
120 // ========================== Run time data and constants ============================
121 // state data
122 static uint32_t upTimeSec;
123
124 // throttling: when not 0, block command
127
128 // settings
129 static constexpr auto TICK_TIME_MS = 500;
130
131 // Time stamp will be updated at a low rate, to maintain precision. Must be lower than RTC overflow time.
132 static constexpr uint16_t TIME_UPDATE_PERIOD_MS = (60 * 1000);
133
134 // Time shouldn't differ more than 1 minute.
135 static constexpr uint16_t THROTTLE_SET_TIME_TICKS = (60 * 1000 / TICK_TIME_MS);
136
137 // Sun time shouldn't differ more than 30 minutes.
138 static constexpr uint16_t THROTTLE_SET_SUN_TIMES_TICKS = (30 * 60 * 1000 / TICK_TIME_MS);
139
140 // timing features
141 static app_timer_t appTimerData;
142 static app_timer_id_t appTimerId;
143
144 static void scheduleNextTick();
145
149 static void tick(void* unused);
150
151 // ===================== mesh posix time sync implementation =====================
152
153 // ---------------- constants ----------------
154
158 static constexpr uint32_t reboot_sync_timeout_ms();
159
163 static constexpr uint32_t root_clock_update_period_ms();
164
168 static constexpr uint32_t root_clock_reelection_timeout_ms();
169
173 static constexpr stone_id_t stone_id_init();
174
178 static constexpr uint8_t timestamp_version_lollipop_max();
179
183 static constexpr uint8_t timestamp_version_min_valid();
184
185 // -------------- runtime variables ----------------
186
191
196
203
208
213
218
220
221 // ------------------ Method definitions ------------------
222
223 static uint32_t syncTimeCoroutineAction();
224
225 static uint8_t timeStampVersion();
226
228 static void setRootTimeStamp(high_resolution_time_stamp_t stamp, stone_id_t id, uint32_t rtcCount);
229
236 static void updateRootTimeStamp(uint32_t rtcCount);
237
241 static void sendTimeSyncMessage(high_resolution_time_stamp_t stamp, stone_id_t id, bool reliable = false);
242
246 static bool meIsRootClock();
247
251 static bool isRootClock(stone_id_t candidate);
252
257
263
267 static bool rebootTimedOut();
268
269 // ==========================================
270 // =========== Debug functions ==============
271 // ==========================================
272
273 static constexpr uint32_t debugSyncTimeMessagePeriodMs();
275
281 static void initDebug();
282
285};
A coroutine essentially is a throttling mechanism: it takes in a tick-event or tick count and execute...
Definition: cs_Coroutine.h:40
Event listener.
Definition: cs_EventListener.h:17
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Sep 24,...
Definition: cs_SystemTime.h:49
static bool meIsRootClock()
Returns true if the id of this stone can considered to be root clock.
static uint16_t throttleSetTimeCountdownTicks
Definition: cs_SystemTime.h:125
void init()
Creates and starts the first tick timer.
static bool rebootTimedOut()
Returns true when reboot period is passed.
static high_resolution_time_stamp_t getSynchronizedStamp()
Get the synchronized timestamp, updated to current time.
static uint32_t up()
Uptime in seconds.
static uint16_t throttleSetSunTimesCountdownTicks
Definition: cs_SystemTime.h:126
static constexpr uint8_t timestamp_version_lollipop_max()
The time version is lollipop versioned.
bool isOnlyReceiveByThisDevice(cmd_source_with_counter_t counted_source)
Returns true if the command source is uart or ble through connection.
static uint32_t posix()
Get the current time as posix timestamp in seconds.
static constexpr uint16_t TIME_UPDATE_PERIOD_MS
Definition: cs_SystemTime.h:132
static uint32_t rtcCountOfLastSecondIncrement
RTC count of last time a second had passed.
Definition: cs_SystemTime.h:190
static uint32_t syncTimeCoroutineAction()
static cs_ret_code_t setSunTimes(const sun_time_t &sunTimes, bool throttled=true)
Set the sunrise and sunset times.
static bool reelectionPeriodTimedOut()
Returns true if onTimeSyncMessageReceive hasn't received any sync messages from a clock authority in ...
static void onTimeSyncMessageReceive(time_sync_message_t syncmessage)
static app_timer_t appTimerData
Definition: cs_SystemTime.h:141
static void sendTimeSyncMessage(high_resolution_time_stamp_t stamp, stone_id_t id, bool reliable=false)
Send a sync message for given stamp/id combo to the mesh.
static constexpr uint8_t timestamp_version_min_valid()
Lowest time version at which a valid posix time is set.
static void pushSyncMessageToTestSuite(time_sync_message_t &syncmessage)
static constexpr uint32_t root_clock_update_period_ms()
Time between sync messages from the root clock.
static void scheduleNextTick()
static constexpr uint16_t THROTTLE_SET_SUN_TIMES_TICKS
Definition: cs_SystemTime.h:138
static Coroutine syncTimeCoroutine
Definition: cs_SystemTime.h:219
static DayOfWeek day()
Get the current weekday.
static void publishSyncMessageForTesting()
virtual void handleEvent(event_t &event)
Handle events.
static Coroutine debugSyncTimeCoroutine
Definition: cs_SystemTime.h:274
static constexpr uint16_t THROTTLE_SET_TIME_TICKS
Definition: cs_SystemTime.h:135
static void tick(void *unused)
Must be called at least once per second to update upTimeSec.
static uint32_t upTimeSec
Definition: cs_SystemTime.h:122
static void initDebug()
Sets up the debug symc time coroutine when DEBUG_SYSTEM_TIME is defined in order to broadcast sync me...
static Time now()
Get the current time as Time object.
static high_resolution_time_stamp_t rootTime
Root clock.
Definition: cs_SystemTime.h:202
static stone_id_t rootClockId
Stone ID of the root clock.
Definition: cs_SystemTime.h:217
static uint32_t rtcCountOfLastRootTimeUpdate
RTC count of last time the root clock was set.
Definition: cs_SystemTime.h:207
static constexpr uint32_t reboot_sync_timeout_ms()
Timeout period before considering this stone to be root clock.
static uint8_t timeStampVersion()
static constexpr uint32_t debugSyncTimeMessagePeriodMs()
static constexpr stone_id_t stone_id_init()
Stone id to use for initialization.
static bool isRootClock(stone_id_t candidate)
Returns true if the candidate can considered to be root clock.
static stone_id_t myId
Cached ID of this stone.
Definition: cs_SystemTime.h:195
virtual ~SystemTime()=default
static cs_ret_code_t setTime(uint32_t time, bool throttled, bool sendToMesh)
Set the system wide time to given posix timestamp.
static app_timer_id_t appTimerId
Definition: cs_SystemTime.h:142
static uint32_t uptimeOfLastTimeSyncMessage
Uptime of last time a sync message was received.
Definition: cs_SystemTime.h:212
static void setRootTimeStamp(high_resolution_time_stamp_t stamp, stone_id_t id, uint32_t rtcCount)
static constexpr uint32_t root_clock_reelection_timeout_ms()
If no sync message has been received from the root clock for this time, a new root clock will be sele...
static constexpr auto TICK_TIME_MS
Definition: cs_SystemTime.h:129
static void updateRootTimeStamp(uint32_t rtcCount)
Keep up the root clock time.
Definition: cs_TestAccess.h:11
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Sep 24,...
Definition: cs_Time.h:14
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Oct 9,...
Definition: cs_Event.h:26
DayOfWeek
Author: Crownstone Team Copyright: Crownstone (https://crownstone.rocks) Date: Sep 24,...
Definition: cs_DayOfWeek.h:13
uint8_t stone_id_t
Definition: cs_Typedefs.h:23
uint16_t cs_ret_code_t
Definition: cs_Typedefs.h:21
Struct that tells where a command originated from.
Definition: cs_CmdSource.h:54
Versioned timestamp with milliseconds precision.
Definition: cs_TimeSyncMessage.h:16
Definition: cs_Packets.h:300
event sent over the internal event bus upon reception of new mesh time stamp msg.
Definition: cs_TimeSyncMessage.h:26