EnigmaIOT  0.9.3
Secure sensor and gateway platform based on ESP8266 and ESP32
timeManager.cpp
Go to the documentation of this file.
1 
9 #include "timeManager.h"
10 #include "debug.h"
11 
13  //Serial.printf ("millis: %u\n", millis ());
14  //Serial.printf ("offset: %d\n", offset);
15  //Serial.printf ("clock: %u %u\n", millis () + offset, clock());
16 
17  return clock ();
18 }
19 
21  if (timeIsAdjusted) {
22  int64_t clk = offset + (time_t)(millis ());
23  DEBUG_DBG ("Clock: %lld", clk);
24  return clk;
25  } else {
26  return millis ();
27  }
28 }
29 
30 int64_t TimeManagerClass::adjustTime (int64_t t1r, int64_t t2r, int64_t t3r, int64_t t4r) {
31  int64_t t1 = t1r;
32  int64_t t2 = t2r;
33  int64_t t3 = t3r;
34  int64_t t4 = t4r;
35 
36  DEBUG_DBG ("T1: %lld, T2: %lld, T3: %lld, T4: %lld", t1, t2, t3, t4);
37  int64_t delay = ((t2 - t1) + (t3 - t4)) / 2;
38  DEBUG_DBG ("Delay: %lld", delay);
39  offset += delay;
40  DEBUG_DBG ("New offset: %lld", offset);
41  roundTripDelay = (t4 - t1) - (t3 - t2);
42  DEBUG_DBG ("Round trip delay: %lld", roundTripDelay);
43 
44  timeIsAdjusted = true;
45 
46  return delay;
47 }
48 
49 
50 
52 
timeManager.h
Clock synchronisation calculations.
TimeManager
TimeManagerClass TimeManager
Definition: timeManager.cpp:51
TimeManagerClass::adjustTime
int64_t adjustTime(int64_t t1r, int64_t t2r, int64_t t3r, int64_t t4r)
Gets delay between Gateway time and local clock and adjust local clock accordingly....
Definition: timeManager.cpp:30
TimeManagerClass::roundTripDelay
int64_t roundTripDelay
Propagation delay between Node and Gateway.
Definition: timeManager.h:19
TimeManagerClass::timeIsAdjusted
bool timeIsAdjusted
Indicates if time has been synchronized.
Definition: timeManager.h:17
TimeManagerClass::setOrigin
int64_t setOrigin()
Inits time synchronization getting local clock to be assigned to T1.
Definition: timeManager.cpp:12
TimeManagerClass::clock
int64_t clock()
Gets local clock. It returns millis() if not synchronized, local clock otherwise.
Definition: timeManager.cpp:20
TimeManagerClass
Definition: timeManager.h:14
TimeManagerClass::offset
int64_t offset
Offet between node millis() and gateway time.
Definition: timeManager.h:18
debug.h
Auxiliary functions for debugging over Serial.