|
1 |
| -#include "NTPUtils.h" |
2 |
| -#include "Arduino.h" |
3 | 1 | /*
|
4 |
| - This Utility Class is derived from the example code found here https://www.arduino.cc/en/Tutorial/UdpNTPClient |
5 |
| - For more information on NTP (Network Time Protocol) you can refer to this Wikipedia article https://en.wikipedia.org/wiki/Network_Time_Protocol |
6 |
| -*/ |
7 |
| - |
| 2 | + This file is part of ArduinoIoTCloud. |
8 | 3 |
|
9 |
| -// could be a constexpr in C++14 |
10 |
| -static time_t cvt_TIME(char const *time) { |
11 |
| - char s_month[5]; |
12 |
| - int month, day, year; |
13 |
| - struct tm t = {0 /* tm_sec */, |
14 |
| - 0 /* tm_min */, |
15 |
| - 0 /* tm_hour */, |
16 |
| - 0 /* tm_mday */, |
17 |
| - 0 /* tm_mon */, |
18 |
| - 0 /* tm_year */, |
19 |
| - 0 /* tm_wday */, |
20 |
| - 0 /* tm_yday */, |
21 |
| - 0 /* tm_isdst */ |
22 |
| - }; |
23 |
| - static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec"; |
| 4 | + Copyright 2020 ARDUINO SA (http://www.arduino.cc/) |
24 | 5 |
|
25 |
| - sscanf(time, "%s %d %d", s_month, &day, &year); |
26 |
| - |
27 |
| - month = (strstr(month_names, s_month) - month_names) / 3; |
28 |
| - |
29 |
| - t.tm_mon = month; |
30 |
| - t.tm_mday = day; |
31 |
| - t.tm_year = year - 1900; |
32 |
| - t.tm_isdst = -1; |
33 |
| - |
34 |
| - return mktime(&t); |
35 |
| -} |
| 6 | + This software is released under the GNU General Public License version 3, |
| 7 | + which covers the main part of arduino-cli. |
| 8 | + The terms of this license can be found at: |
| 9 | + https://www.gnu.org/licenses/gpl-3.0.en.html |
36 | 10 |
|
| 11 | + You can be released from the requirements of the above licenses by purchasing |
| 12 | + a commercial license. Buying such a license is mandatory if you want to modify or |
| 13 | + otherwise use the software for commercial activities involving the Arduino |
| 14 | + software without disclosing the source code of your own applications. To purchase |
| 15 | + a commercial license, send an email to [email protected]. |
| 16 | +*/ |
37 | 17 |
|
38 |
| -NTPUtils::NTPUtils(UDP& Udp) : Udp(Udp) {} |
| 18 | +/************************************************************************************** |
| 19 | + * INCLUDE |
| 20 | + **************************************************************************************/ |
39 | 21 |
|
40 |
| -bool NTPUtils::isTimeValid(unsigned long time) { |
41 |
| - return (time > static_cast<unsigned long>(cvt_TIME(__DATE__))); |
42 |
| -} |
| 22 | +#include "NTPUtils.h" |
| 23 | +#include "Arduino.h" |
43 | 24 |
|
44 |
| -void NTPUtils::sendNTPpacket(uint8_t* packetBuffer) { |
45 |
| - memset(packetBuffer, 0, NTP_PACKET_SIZE); |
46 |
| - packetBuffer[0] = 0b11100011; |
47 |
| - packetBuffer[1] = 0; |
48 |
| - packetBuffer[2] = 6; |
49 |
| - packetBuffer[3] = 0xEC; |
50 |
| - packetBuffer[12] = 49; |
51 |
| - packetBuffer[13] = 0x4E; |
52 |
| - packetBuffer[14] = 49; |
53 |
| - packetBuffer[15] = 52; |
54 |
| - Udp.beginPacket(timeServer, 123); |
55 |
| - Udp.write(packetBuffer, NTP_PACKET_SIZE); |
56 |
| - Udp.endPacket(); |
57 |
| -} |
| 25 | +/************************************************************************************** |
| 26 | + * PUBLIC MEMBER FUNCTIONS |
| 27 | + **************************************************************************************/ |
58 | 28 |
|
59 |
| -unsigned long NTPUtils::getTime() { |
| 29 | +unsigned long NTPUtils::getTime(UDP & udp, const char * time_server) |
| 30 | +{ |
| 31 | + udp.begin(NTP_LOCAL_PORT); |
| 32 | + |
| 33 | + sendNTPpacket(udp, time_server); |
60 | 34 |
|
61 |
| - unsigned int localPort = 8888; |
62 |
| - uint8_t packetBuffer[NTP_PACKET_SIZE]; |
| 35 | + bool is_timeout = false; |
| 36 | + unsigned long const start = millis(); |
| 37 | + do |
| 38 | + { |
| 39 | + is_timeout = (millis() - start) >= NTP_TIMEOUT_MS; |
| 40 | + } while(!is_timeout && !udp.parsePacket()); |
63 | 41 |
|
64 |
| - Udp.begin(localPort); |
65 |
| - sendNTPpacket(packetBuffer); |
66 |
| - long start = millis(); |
67 |
| - while (!Udp.parsePacket() && (millis() - start < 10000)) {} |
68 |
| - if (millis() - start >= 1000) { |
69 |
| - //timeout reached |
70 |
| - Udp.stop(); |
| 42 | + if(is_timeout) { |
| 43 | + udp.stop(); |
71 | 44 | return 0;
|
72 | 45 | }
|
73 |
| - Udp.read(packetBuffer, NTP_PACKET_SIZE); |
| 46 | + |
| 47 | + uint8_t ntp_packet_buf[NTP_PACKET_SIZE]; |
| 48 | + udp.read(ntp_packet_buf, NTP_PACKET_SIZE); |
| 49 | + udp.stop(); |
74 | 50 |
|
75 |
| - unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); |
76 |
| - unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); |
77 |
| - unsigned long secsSince1900 = highWord << 16 | lowWord; |
78 |
| - const unsigned long seventyYears = 2208988800UL; |
79 |
| - unsigned long epoch = secsSince1900 - seventyYears; |
| 51 | + unsigned long const highWord = word(ntp_packet_buf[40], ntp_packet_buf[41]); |
| 52 | + unsigned long const lowWord = word(ntp_packet_buf[42], ntp_packet_buf[43]); |
| 53 | + unsigned long const secsSince1900 = highWord << 16 | lowWord; |
| 54 | + unsigned long const seventyYears = 2208988800UL; |
| 55 | + unsigned long const epoch = secsSince1900 - seventyYears; |
80 | 56 |
|
81 |
| - Udp.stop(); |
82 | 57 | return epoch;
|
83 | 58 | }
|
| 59 | + |
| 60 | +/************************************************************************************** |
| 61 | + * PRIVATE MEMBER FUNCTIONS |
| 62 | + **************************************************************************************/ |
| 63 | + |
| 64 | +void NTPUtils::sendNTPpacket(UDP & udp, const char * time_server) |
| 65 | +{ |
| 66 | + uint8_t ntp_packet_buf[NTP_PACKET_SIZE] = {0}; |
| 67 | + |
| 68 | + ntp_packet_buf[0] = 0b11100011; |
| 69 | + ntp_packet_buf[1] = 0; |
| 70 | + ntp_packet_buf[2] = 6; |
| 71 | + ntp_packet_buf[3] = 0xEC; |
| 72 | + ntp_packet_buf[12] = 49; |
| 73 | + ntp_packet_buf[13] = 0x4E; |
| 74 | + ntp_packet_buf[14] = 49; |
| 75 | + ntp_packet_buf[15] = 52; |
| 76 | + |
| 77 | + udp.beginPacket(time_server, NTP_TIME_SERVER_PORT); |
| 78 | + udp.write(ntp_packet_buf, NTP_PACKET_SIZE); |
| 79 | + udp.endPacket(); |
| 80 | +} |
0 commit comments