-
Notifications
You must be signed in to change notification settings - Fork 0
/
timesyncronizer.cpp
172 lines (156 loc) · 5.43 KB
/
timesyncronizer.cpp
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
#include "timesyncronizer.h"
#include <QDateTime>
#include <QProcess>
#include <QTimer>
#include <QtDebug>
#include <arpa/inet.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <error.h>
#include <iostream>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/timex.h>
#define NTP_PORT 123
/* This program uses an NTP mode 6 control message, which is the
same as that used by the ntpq command. The ntpdc command uses
NTP mode 7, details of which are elusive.
For details on the format of NTP control message, see
http://www.eecis.udel.edu/~mills/database/rfc/rfc1305/rfc1305b.ps.
This document covers NTP version 2, however the control message
format works with ntpd version 4 and earlier.
Section 3.2 (pp 9 ff) of RFC1305b describes the data formats used by
this program.
This returns:
0: if clock is synchronised.
1: if clock is not synchronised.
2: if clock state cannot be determined, eg if
ntpd is not contactable. */
/* ------------------------------------------------------------------------*/
/* value of Byte1 and Byte2 in the ntpmsg */
#define B1VAL 0x16 /* VN = 2, Mode = 6 */
#define B2VAL \
2 /* Response = 0; ( this makes the packet a command ) \
Error = 0; \
More = 0; \
Op Code = 2 (read variables command) */
#define RMASK 0x80 /* bit mask for the response bit in Status Byte2 */
#define EMASK 0x40 /* bit mask for the error bit in Status Byte2 */
#define MMASK 0x20 /* bit mask for the more bit in Status Byte2 */
#define PAYLOADSIZE 468 /* size in bytes of the message payload string */
#define NTPSTATUSPERIOD 3000
TimeSyncronizer::TimeSyncronizer(QObject *parent) : QObject(parent)
{
}
TimeSyncronizer::~TimeSyncronizer()
{
}
void TimeSyncronizer::init()
{
m_timeCounter = 0;
emit ntpStatusChanged(ntpStatus()); // first time we must emit ntpStatus
QTimer *timer = new QTimer(this);
timer->setInterval(NTPSTATUSPERIOD);
connect(timer, &QTimer::timeout, this, &TimeSyncronizer::checkNtpAndSetTime);
timer->start();
}
void printts(const timespec &st)
{
auto datetime = QDateTime::fromMSecsSinceEpoch(((st.tv_sec * 1000) + (st.tv_nsec / 1.0e6)));
qDebug() << "Setting datetime: " << datetime;
}
void TimeSyncronizer::printAndSetSystemTime(const timespec time)
{
printts(time);
setSystemTime(time);
}
timespec TimeSyncronizer::systemTime() const
{
timespec time;
struct timeval timeToGet;
gettimeofday(&timeToGet, NULL);
// clock_gettime(CLOCK_REALTIME, &time);
time.tv_sec = timeToGet.tv_sec;
time.tv_nsec = timeToGet.tv_usec * 1000;
return time;
}
void TimeSyncronizer::setSystemTime(const timespec &systemTime)
{
QString program = "/usr/sbin/hwclock";
QStringList arguments { "-w" };
struct timeval timeToSet;
timeToSet.tv_sec = systemTime.tv_sec;
timeToSet.tv_usec = 0;
settimeofday(&timeToSet, NULL);
// clock_settime(CLOCK_REALTIME, &systemTime); // set current datetime
QProcess *myProcess = new QProcess(this); // set datetime to RTC
qInfo() << "Set hwclock time: " << systemTime.tv_sec;
myProcess->start(program, arguments);
myProcess->waitForFinished();
qInfo() << "HWClock exited with code: " << myProcess->exitCode() << " and status: " << myProcess->exitStatus();
}
void TimeSyncronizer::checkNtpAndSetTime()
{
bool status = ntpStatus();
++m_timeCounter;
if (m_timeCounter >= 20) // one time per minute
{
if (status)
{
m_timeCounter = 0;
emit setTime(systemTime());
}
emit ntpStatusChanged(status);
}
}
bool TimeSyncronizer::ntpStatus() const
{
QString output;
#if defined(Q_OS_LINUX)
QProcess process;
QString program = "/usr/bin/ntpq";
QStringList arguments { "-pn" };
process.start(program, arguments);
if (!process.waitForFinished(1000))
{
qWarning() << "ntpq start error: " << process.errorString();
return false;
}
output = process.readAllStandardOutput();
#else
QFile file(qApp->applicationDirPath() + "/ntpq_output.txt");
if (file.exists() && file.open(QIODevice::ReadOnly))
{
output = file.readAll();
file.close();
}
#endif
// qDebug() << "Ntpq -pn output: " << output;
if (output.isEmpty())
{
qWarning() << "ntpq output is empty!";
return false;
}
if (output.contains("Connection refused"))
{
qWarning() << "ntpq error: connection refused";
return false;
}
// Split ntpq output
QStringList lines = output.split('\n');
if (lines.size() < 2)
{
qWarning() << "ntpq output is wrong!";
return false;
}
// Removing ntpq table header
lines.removeFirst(); // remote refid st t when poll reach delay offset jitter
lines.removeFirst(); //==============================================================================
foreach (QString str, lines)
{
if (str.startsWith("*")) // ntp is synchronized
return true;
}
return false;
}