-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathNTPUtils.cpp
161 lines (137 loc) · 4.63 KB
/
NTPUtils.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
/*
This file is part of ArduinoIoTCloud.
Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
This software is released under the GNU General Public License version 3,
which covers the main part of arduino-cli.
The terms of this license can be found at:
https://www.gnu.org/licenses/gpl-3.0.en.html
You can be released from the requirements of the above licenses by purchasing
a commercial license. Buying such a license is mandatory if you want to modify or
otherwise use the software for commercial activities involving the Arduino
software without disclosing the source code of your own applications. To purchase
a commercial license, send an email to [email protected].
*/
/**************************************************************************************
* INCLUDE
**************************************************************************************/
#include "../../AIoTC_Config.h"
#ifndef HAS_LORA
#include "NTPUtils.h"
#include <Arduino.h>
#ifdef BOARD_HAS_ECCX08
#include <ArduinoECCX08.h>
#endif
/**************************************************************************************
* PUBLIC MEMBER FUNCTIONS
**************************************************************************************/
unsigned long NTPUtils::getTime(UDP & udp)
{
#ifdef NTP_USE_RANDOM_PORT
udp.begin(NTPUtils::getRandomPort(MIN_NTP_PORT, MAX_NTP_PORT));
#else
udp.begin(NTP_LOCAL_PORT);
#endif
sendNTPpacket(udp);
bool is_timeout = false;
unsigned long const start = millis();
do
{
is_timeout = (millis() - start) >= NTP_TIMEOUT_MS;
} while(!is_timeout && !udp.parsePacket());
if(is_timeout) {
udp.stop();
return 0;
}
uint8_t ntp_packet_buf[NTP_PACKET_SIZE];
udp.read(ntp_packet_buf, NTP_PACKET_SIZE);
udp.stop();
unsigned long const highWord = word(ntp_packet_buf[40], ntp_packet_buf[41]);
unsigned long const lowWord = word(ntp_packet_buf[42], ntp_packet_buf[43]);
unsigned long const secsSince1900 = highWord << 16 | lowWord;
unsigned long const seventyYears = 2208988800UL;
unsigned long const epoch = secsSince1900 - seventyYears;
return epoch;
}
/**************************************************************************************
* PRIVATE MEMBER FUNCTIONS
**************************************************************************************/
void NTPUtils::sendNTPpacket(UDP & udp)
{
uint8_t ntp_packet_buf[NTP_PACKET_SIZE] = {0};
ntp_packet_buf[0] = 0b11100011;
ntp_packet_buf[1] = 0;
ntp_packet_buf[2] = 6;
ntp_packet_buf[3] = 0xEC;
ntp_packet_buf[12] = 49;
ntp_packet_buf[13] = 0x4E;
ntp_packet_buf[14] = 49;
ntp_packet_buf[15] = 52;
udp.beginPacket(NTP_TIME_SERVER, NTP_TIME_SERVER_PORT);
udp.write(ntp_packet_buf, NTP_PACKET_SIZE);
udp.endPacket();
}
#ifdef NTP_USE_RANDOM_PORT
int NTPUtils::getRandomPort(int const min_port, int const max_port)
{
#if defined (ARDUINO_ARCH_ESP8266) || (ARDUINO_ARCH_ESP32) || \
(ARDUINO_ARCH_RENESAS) || (ARDUINO_ARCH_MBED)
/* Uses HW Random Number Generator */
#elif defined (ARDUINO_ARCH_SAMD)
/* Use ADC to generate a seed */
randomSeed(adcSeed());
#else
randomSeed(analogRead(0));
#endif
return random(min_port, max_port);
}
#if defined (ARDUINO_ARCH_SAMD)
unsigned long NTPUtils::adcSeed()
{
uint32_t seed = 0;
uint32_t bitCount = 0;
uint16_t sampctlr = ADC->SAMPCTRL.reg;
// Use lowest sampling time
ADC->SAMPCTRL.reg = 0;
// Enable ADC
ADC->CTRLA.bit.ENABLE = 1;
while (ADC->STATUS.bit.SYNCBUSY == 1);
do {
uint16_t adcReading;
// Start ADC conversion
ADC->SWTRIG.bit.START = 1;
// Wait until ADC conversion is done
while (!(ADC->INTFLAG.bit.RESRDY));
while (ADC->STATUS.bit.SYNCBUSY == 1);
// Get result
adcReading = ADC->RESULT.reg;
// Clear result ready flag
ADC->INTFLAG.reg = ADC_INTFLAG_RESRDY;
while (ADC->STATUS.bit.SYNCBUSY == 1);
// Take least significant bit
uint8_t b0 = adcReading & 0x0001;
ADC->SWTRIG.bit.START = 1;
// Wait until ADC conversion is done
while (!(ADC->INTFLAG.bit.RESRDY));
while (ADC->STATUS.bit.SYNCBUSY == 1);
// Get result
adcReading = ADC->RESULT.reg;
// Clear result ready flag
ADC->INTFLAG.reg = ADC_INTFLAG_RESRDY;
while (ADC->STATUS.bit.SYNCBUSY == 1);
// Take least significant nibble
uint8_t b1 = adcReading & 0x0001;
if (b0 == b1) {
continue;
}
seed |= b0 << bitCount;
bitCount++;
} while(bitCount < 32);
// Disable ADC
ADC->CTRLA.bit.ENABLE = 0;
// restore original sampling time
ADC->SAMPCTRL.reg = sampctlr;
return seed;
}
#endif /* ARDUINO_ARCH_SAMD */
#endif /* NTP_USE_RANDOM_PORT */
#endif /* !HAS_LORA */