EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
timeManager.cpp
Go to the documentation of this file.
1 
9 #include "timeManager.h"
10 #include "EnigmaIOTdebug.h"
11 
13  timeval currentime;
14 
15  gettimeofday (&currentime, NULL);
16  int64_t clk = currentime.tv_sec;
17  clk *= 1000000L;
18  clk += currentime.tv_usec;
19  // DEBUG_DBG ("Clock: %lld", clk/1000L);
20  return clk/1000L;
21 }
22 
24  timeval currentime;
25 
26  gettimeofday (&currentime, NULL);
27  int64_t clk = currentime.tv_sec;
28  clk *= 1000000L;
29  clk += currentime.tv_usec;
30  // DEBUG_DBG ("Clock: %lld", clk);
31  return clk;
32 }
33 
34 int64_t TimeManagerClass::adjustTime (int64_t t1r, int64_t t2r, int64_t t3r, int64_t t4r) {
35  int64_t t1 = t1r;
36  int64_t t2 = t2r;
37  int64_t t3 = t3r;
38  int64_t t4 = t4r;
39  timeval currenttime;
40  int64_t currenttime_us;
41  timeval newtime;
42  int64_t newtime_us;
43 
44  DEBUG_DBG ("T1: %lld, T2: %lld, T3: %lld, T4: %lld", t1, t2, t3, t4);
45  offset = ((t2 - t1) + (t3 - t4)) / 2L;
46  DEBUG_DBG ("New offset: %lld", offset);
47  roundTripDelay = (t4 - t1) - (t3 - t2);
48  DEBUG_DBG ("Round trip delay: %lld", roundTripDelay);
49 
50  gettimeofday (&currenttime, NULL);
51  currenttime_us = (int64_t)currenttime.tv_sec * 1000000LL + (int64_t)currenttime.tv_usec;
52  newtime_us = currenttime_us + offset;
53  newtime.tv_sec = newtime_us / 1000000LL;
54  newtime.tv_usec = newtime_us - ((int64_t)(newtime.tv_sec) * 1000000LL);
55 
56  settimeofday (&newtime, NULL); // hard adjustment
57 
58  timeIsAdjusted = true;
59 
60  return offset;
61 }
62 
63 
64 
66 
timeManager.h
Clock synchronisation calculations.
TimeManagerClass::clock_us
int64_t clock_us()
Gets local clock.
Definition: timeManager.cpp:23
TimeManager
TimeManagerClass TimeManager
Definition: timeManager.cpp:65
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:34
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::clock
int64_t clock()
Gets local clock.
Definition: timeManager.cpp:12
TimeManagerClass
Definition: timeManager.h:15
EnigmaIOTdebug.h
Auxiliary functions for debugging over Serial.
TimeManagerClass::offset
int64_t offset
Offet between node millis() and gateway time.
Definition: timeManager.h:18