|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Arduino |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MPL-2.0 |
| 5 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | + */ |
| 9 | + |
| 10 | +/************************************************************************************** |
| 11 | + * INCLUDE |
| 12 | + **************************************************************************************/ |
| 13 | + |
| 14 | +#include "NTPUtils.h" |
| 15 | + |
| 16 | + |
| 17 | +/************************************************************************************** |
| 18 | + * NAMESPACE |
| 19 | + **************************************************************************************/ |
| 20 | + |
| 21 | +namespace opcua |
| 22 | +{ |
| 23 | + |
| 24 | +/************************************************************************************** |
| 25 | + * PUBLIC MEMBER FUNCTIONS |
| 26 | + **************************************************************************************/ |
| 27 | + |
| 28 | +unsigned long NTPUtils::getTime(UDP &udp) |
| 29 | +{ |
| 30 | + udp.begin(NTP_LOCAL_PORT); |
| 31 | + |
| 32 | + sendNTPpacket(udp); |
| 33 | + |
| 34 | + bool is_timeout = false; |
| 35 | + unsigned long const start = millis(); |
| 36 | + do |
| 37 | + { |
| 38 | + is_timeout = (millis() - start) >= NTP_TIMEOUT_MS; |
| 39 | + } while (!is_timeout && !udp.parsePacket()); |
| 40 | + |
| 41 | + if (is_timeout) |
| 42 | + { |
| 43 | + udp.stop(); |
| 44 | + return 0; |
| 45 | + } |
| 46 | + |
| 47 | + uint8_t ntp_packet_buf[NTP_PACKET_SIZE]; |
| 48 | + udp.read(ntp_packet_buf, NTP_PACKET_SIZE); |
| 49 | + udp.stop(); |
| 50 | + |
| 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; |
| 56 | + |
| 57 | + return epoch; |
| 58 | +} |
| 59 | + |
| 60 | +/************************************************************************************** |
| 61 | + * PRIVATE MEMBER FUNCTIONS |
| 62 | + **************************************************************************************/ |
| 63 | + |
| 64 | +void NTPUtils::sendNTPpacket(UDP &udp) |
| 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(NTP_TIME_SERVER, NTP_TIME_SERVER_PORT); |
| 78 | + udp.write(ntp_packet_buf, NTP_PACKET_SIZE); |
| 79 | + udp.endPacket(); |
| 80 | +} |
| 81 | + |
| 82 | +/************************************************************************************** |
| 83 | + * NAMESPACE |
| 84 | + **************************************************************************************/ |
| 85 | + |
| 86 | +} /* opcua */ |
0 commit comments