1#ifndef _PAINLESS_MESH_NTP_HPP_
2#define _PAINLESS_MESH_NTP_HPP_
4#ifndef TIME_SYNC_INTERVAL
5#define TIME_SYNC_INTERVAL 1 * TASK_MINUTE
8#ifndef TIME_SYNC_ACCURACY
9#define TIME_SYNC_ACCURACY 5000
12#ifndef MIN_LARGE_OFFSET_STEP
13#define MIN_LARGE_OFFSET_STEP 500
16#ifndef MAX_CPU_TRIP_RATIO
17#define MAX_CPU_TRIP_RATIO \
54inline int32_t
clockOffset(uint32_t time0, uint32_t time1, uint32_t time2,
57 ((int32_t)(time1 - time0) / 2) + ((int32_t)(time2 - time3) / 2);
60 int32_t signedOffset = (int32_t)offset;
62 signedOffset = signedOffset / 4;
73inline int32_t
tripDelay(uint32_t time0, uint32_t time1, uint32_t time2,
75 return ((time3 - time0) - (time2 - time1)) / 2;
98 if (mySubCount > remoteSubCount)
return false;
99 if (mySubCount == remoteSubCount) {
100 if (connection.
nodeId == 0)
115 if (
adopt(mesh, (*connection))) {
118 Log(S_TIME,
"initTimeSync(): Requesting time from %u\n",
123 Log(S_TIME,
"initTimeSync(): Requesting %u to adopt our time\n",
127 router::send<protocol::TimeSync, T>(timeSync, connection,
true);
130template <
class T,
class U>
132 std::shared_ptr<U> conn, uint32_t receivedAt) {
136 "handleTimeSync(): Received time sync error. Restarting time "
138 conn->timeSyncTask.forceNextIteration();
143 "handleTimeSync(): Received requesto to start TimeSync with "
146 timeSync.
reply(mesh.getNodeTime());
147 router::send<painlessmesh::protocol::TimeSync>(timeSync, conn,
true);
151 timeSync.
reply(receivedAt, mesh.getNodeTime());
152 router::send<painlessmesh::protocol::TimeSync>(timeSync, conn,
true);
155 "handleTimeSync(): timeSyncStatus with %u completed\n", conn->nodeId);
163 "handleTimeSync(): %u adopting TIME_RESPONSE from %u\n", mesh.nodeId,
167 mesh.timeOffset += offset;
170 if (mesh.nodeTimeAdjustedCallback) {
171 mesh.nodeTimeAdjustedCallback(offset);
179 "handleTimeSync(): timeSyncStatus with %u completed\n",
183 for (
auto&& connection : mesh.subs) {
184 if (connection->nodeId != conn->nodeId) {
185 connection->timeSyncTask.forceNextIteration();
187 "handleTimeSync(): timeSyncStatus with %u brought forward\n",
193 conn->timeSyncTask.delay(200 * TASK_MILLISECOND);
195 "handleTimeSync(): timeSyncStatus with %u needs further tries\n",
208template <
class T,
class U>
210 std::shared_ptr<U> conn, uint32_t receivedAt) {
217 "handleTimeDelay(): Error in requesting time delay. Please try "
226 timeDelay.
reply(receivedAt, mesh.getNodeTime());
227 router::send<protocol::TimeDelay, U>(timeDelay, conn);
238 if (mesh.nodeDelayReceivedCallback)
239 mesh.nodeDelayReceivedCallback(timeDelay.
from, delay);
244 "handleTimeDelay(): Unknown timeSyncMessageType received. Ignoring "
251template <
class T,
typename U>
258 uint32_t receivedAt) {
260 handleTimeSync<T, U>(mesh, timeSync, connection, receivedAt);
268 uint32_t receivedAt) {
270 handleTimeDelay<T, U>(mesh, timeDelay, connection, receivedAt);
Definition callback.hpp:57
void onPackage(int id, std::function< void(Args...)> func)
Definition callback.hpp:62
uint32_t getNodeTime()
Definition ntp.hpp:43
uint32_t timeOffset
Definition ntp.hpp:46
Definition protocol.hpp:167
bool hasTimeAuthority
Definition protocol.hpp:171
uint32_t nodeId
Definition protocol.hpp:169
Definition protocol.hpp:463
Definition protocol.hpp:355
void reply(uint32_t newT0)
Definition protocol.hpp:437
uint32_t from
Definition protocol.hpp:359
time_sync_msg_t msg
Definition protocol.hpp:360
Definition protocol.hpp:481
T to()
Definition protocol.hpp:743
protocol::NodeTree excludeRoute(protocol::NodeTree &&tree, uint32_t exclude)
Definition layout.hpp:87
uint32_t size(protocol::NodeTree nodeTree)
Definition layout.hpp:232
@ S_TIME
Definition logger.hpp:24
@ ERROR
Definition logger.hpp:19
int32_t clockOffset(uint32_t time0, uint32_t time1, uint32_t time2, uint32_t time3)
Definition ntp.hpp:54
void handleTimeDelay(T &mesh, painlessmesh::protocol::TimeDelay timeDelay, std::shared_ptr< U > conn, uint32_t receivedAt)
Definition ntp.hpp:209
void handleTimeSync(T &mesh, painlessmesh::protocol::TimeSync timeSync, std::shared_ptr< U > conn, uint32_t receivedAt)
Definition ntp.hpp:131
void initTimeSync(protocol::NodeTree mesh, std::shared_ptr< T > connection, uint32_t nodeTime)
Definition ntp.hpp:111
void addPackageCallback(callback::MeshPackageCallbackList< U > &callbackList, T &mesh)
Definition ntp.hpp:252
int32_t tripDelay(uint32_t time0, uint32_t time1, uint32_t time2, uint32_t time3)
Definition ntp.hpp:73
bool adopt(protocol::NodeTree mesh, protocol::NodeTree connection)
Definition ntp.hpp:78
@ TIME_REPLY
Definition protocol.hpp:49
@ TIME_SYNC_ERROR
Definition protocol.hpp:46
@ TIME_REQUEST
Definition protocol.hpp:48
@ TIME_SYNC_REQUEST
Definition protocol.hpp:47
@ TIME_SYNC
Definition protocol.hpp:37
@ TIME_DELAY
Definition protocol.hpp:36
painlessmesh::logger::LogClass Log
Definition wifi.cpp:2
#define TIME_SYNC_ACCURACY
Definition ntp.hpp:9
#define MAX_CPU_TRIP_RATIO
Definition ntp.hpp:17
#define MIN_LARGE_OFFSET_STEP
Definition ntp.hpp:13
#define TIME_SYNC_INTERVAL
Definition ntp.hpp:5
uint32_t t0
Definition protocol.hpp:347
uint32_t t1
Definition protocol.hpp:348
int type
Definition protocol.hpp:346
uint32_t t2
Definition protocol.hpp:349
painlessmesh::logger::LogClass Log
Definition wifi.cpp:2