NetBurner 3.5.7
PDF Version
NTPServer.h
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
5#ifndef NTPSERVER_H
6#define NTPSERVER_H
7
8#include <nbrtos.h>
9#include <udp.h>
10#include <cstdint>
11#include "IEEE1588Timer.h"
12#include "GPSChip.h"
13
22{
23 public:
31 {
32 private:
33 void SetFutureTime(uint32_t cap_1588, uint32_t cur_time, uint32_t interval, uint32_t From1588);
34
35 public:
36 beuint32_t sec;
37 beuint32_t frac;
38
45 bool SetFrom(uint32_t tt, uint32_t From1588);
46
50 NtpTimeStamp() : sec(0), frac(0) {}
51
56 static NtpTimeStamp Now();
57
65
77bool operator<(const NtpTimeStamp &other) const
78{
79 uint32_t this_sec = sec;
80 uint32_t other_sec = other.sec;
81
82 if (this_sec != other_sec)
83 return this_sec < other_sec;
84
85 // Seconds are equal, compare fractional part
86 uint32_t this_frac = frac;
87 uint32_t other_frac = other.frac;
88 return this_frac < other_frac;
89}
90
96bool operator>(const NtpTimeStamp &other) const
97{
98 return other < *this;
99}
100
106bool operator<=(const NtpTimeStamp &other) const
107{
108 return !(other < *this);
109}
110
116bool operator>=(const NtpTimeStamp &other) const
117{
118 return !(*this < other);
119}
120
126bool operator==(const NtpTimeStamp &other) const
127{
128 return (sec == other.sec) && (frac == other.frac);
129}
130
136bool operator!=(const NtpTimeStamp &other) const
137{
138 return !(*this == other);
139}
140
141
142 };
143
144 private:
148 struct NTP_Packet
149 {
150 uint8_t LV_VN_Mode;
151 uint8_t Stratum;
152 uint8_t Poll;
153 int8_t Precision;
154
155 beuint32_t Root_Delay;
156 beuint32_t Root_Dispersion;
157 uint8_t Ref_Id[4];
158 NtpTimeStamp Ref_Time;
159 NtpTimeStamp Org_Time;
160 NtpTimeStamp Rx_Time;
161 NtpTimeStamp Tx_Time;
162 };
163
164 // Constants for NTP server operation
165 static const uint32_t DISPERSION = 0;
166 static const int8_t PRECISION = 0xE9;
167
168 // References to external components
169 IEEE1588Timer &timer;
170 GPSChip &gps;
171 uint16_t port;
172
173 // Server task management
174 OS_FIFO rxFifo;
175 bool running;
176 uint32_t taskStack[USER_TASK_STK_SIZE];
177
182 void serverTaskImpl();
183
184
185/* struct TxTimeMeasure
186 {
187 uint32_t now_1588;
188 uint32_t now_tt;
189 PoolPtr packet;
190 };
191
192 TxTimeMeasure TxRecords[256];
193 uint8_t txmeas_put_index;
194 uint8_t txmeas_get_index;
195 uint32_t nTxMeas;
196 uint32_t nTxMeasSum;
197*/
198 void CleanupTxRecords();
199 void InitTxRecords();
200 void SaveTxRecord(PoolPtr,uint32_t now);
201 uint32_t GetTxTimeOffset();
202
203public:
204
205
212 NTPServer(IEEE1588Timer &timer, GPSChip &gps, uint16_t port = 123);
213
219 bool start();
220
225 void stop();
226
231 static void serverTask(void *param);
232
237 bool isRunning() const { return running; }
238
243 uint16_t getPort() const { return port; }
244};
245
246#endif // NTPSERVER_H
Definition IEEE1588Timer.h:28
Definition NTPServer.h:31
bool operator<=(const NtpTimeStamp &other) const
Definition NTPServer.h:106
static NtpTimeStamp Now()
Definition NTPServer.cpp:358
bool operator!=(const NtpTimeStamp &other) const
Definition NTPServer.h:136
bool operator>=(const NtpTimeStamp &other) const
Definition NTPServer.h:116
bool SetFrom(uint32_t tt, uint32_t From1588)
Definition NTPServer.cpp:310
NtpTimeStamp & operator=(UDPPacket &upkt)
Definition NTPServer.cpp:365
bool operator>(const NtpTimeStamp &other) const
Definition NTPServer.h:96
beuint32_t frac
Fractional seconds (big endian)
Definition NTPServer.h:37
beuint32_t sec
Seconds since NTP epoch (big endian)
Definition NTPServer.h:36
bool operator<(const NtpTimeStamp &other) const
Definition NTPServer.h:77
bool operator==(const NtpTimeStamp &other) const
Definition NTPServer.h:126
NtpTimeStamp()
Definition NTPServer.h:50
Definition NTPServer.h:22
bool start()
Definition NTPServer.cpp:26
void stop()
Definition NTPServer.cpp:46
NTPServer(IEEE1588Timer &timer, GPSChip &gps, uint16_t port=123)
Definition NTPServer.cpp:19
uint16_t getPort() const
Definition NTPServer.h:243
static void serverTask(void *param)
Definition NTPServer.cpp:53
bool isRunning() const
Definition NTPServer.h:237
UDP Packet Class - Complete UDP packet management.
Definition udp.h:602
Main buffer structure for network and serial communication.
Definition buffers.h:90