Vandan Patel / ntp-client

Dependents:   UDP_RoundTripDelay_OS6_H743ZI advancedIO_1wifiMessing_copy advancedIO_Assignment_Program UDP_RoundTripDelay_OS6_K64F

Committer:
nzvandan
Date:
Thu Sep 10 00:31:53 2020 +0000
Revision:
0:d4145a1a189a
2020 NTP Server Communication

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nzvandan 0:d4145a1a189a 1 /* Copyright (c) 2019 ARM, Arm Limited and affiliates.
nzvandan 0:d4145a1a189a 2 * SPDX-License-Identifier: Apache-2.0
nzvandan 0:d4145a1a189a 3 *
nzvandan 0:d4145a1a189a 4 * Licensed under the Apache License, Version 2.0 (the "License");
nzvandan 0:d4145a1a189a 5 * you may not use this file except in compliance with the License.
nzvandan 0:d4145a1a189a 6 * You may obtain a copy of the License at
nzvandan 0:d4145a1a189a 7 *
nzvandan 0:d4145a1a189a 8 * http://www.apache.org/licenses/LICENSE-2.0
nzvandan 0:d4145a1a189a 9 *
nzvandan 0:d4145a1a189a 10 * Unless required by applicable law or agreed to in writing, software
nzvandan 0:d4145a1a189a 11 * distributed under the License is distributed on an "AS IS" BASIS,
nzvandan 0:d4145a1a189a 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
nzvandan 0:d4145a1a189a 13 * See the License for the specific language governing permissions and
nzvandan 0:d4145a1a189a 14 * limitations under the License.
nzvandan 0:d4145a1a189a 15 */
nzvandan 0:d4145a1a189a 16
nzvandan 0:d4145a1a189a 17 #include "ntp-client/NTPClient.h"
nzvandan 0:d4145a1a189a 18 #include "mbed.h"
nzvandan 0:d4145a1a189a 19
nzvandan 0:d4145a1a189a 20 NTPClient::NTPClient(NetworkInterface *interface)
nzvandan 0:d4145a1a189a 21 : iface(interface), nist_server_address(NTP_DEFULT_NIST_SERVER_ADDRESS), nist_server_port(NTP_DEFULT_NIST_SERVER_PORT) {
nzvandan 0:d4145a1a189a 22 }
nzvandan 0:d4145a1a189a 23
nzvandan 0:d4145a1a189a 24 void NTPClient::set_server(const char* server, int port) {
nzvandan 0:d4145a1a189a 25 nist_server_address = server;
nzvandan 0:d4145a1a189a 26 nist_server_port = port;
nzvandan 0:d4145a1a189a 27 }
nzvandan 0:d4145a1a189a 28
nzvandan 0:d4145a1a189a 29 time_t NTPClient::get_timestamp(int timeout) {
nzvandan 0:d4145a1a189a 30 const time_t TIME1970 = (time_t)2208988800UL;
nzvandan 0:d4145a1a189a 31 int ntp_send_values[12] = {0};
nzvandan 0:d4145a1a189a 32 int ntp_recv_values[12] = {0};
nzvandan 0:d4145a1a189a 33
nzvandan 0:d4145a1a189a 34 SocketAddress nist;
nzvandan 0:d4145a1a189a 35
nzvandan 0:d4145a1a189a 36 if (iface) {
nzvandan 0:d4145a1a189a 37 int ret_gethostbyname = iface->gethostbyname(nist_server_address, &nist);
nzvandan 0:d4145a1a189a 38
nzvandan 0:d4145a1a189a 39 if (ret_gethostbyname < 0) {
nzvandan 0:d4145a1a189a 40 // Network error on DNS lookup
nzvandan 0:d4145a1a189a 41 return ret_gethostbyname;
nzvandan 0:d4145a1a189a 42 }
nzvandan 0:d4145a1a189a 43
nzvandan 0:d4145a1a189a 44 nist.set_port(nist_server_port);
nzvandan 0:d4145a1a189a 45
nzvandan 0:d4145a1a189a 46 memset(ntp_send_values, 0x00, sizeof(ntp_send_values));
nzvandan 0:d4145a1a189a 47 ntp_send_values[0] = '\x1b';
nzvandan 0:d4145a1a189a 48
nzvandan 0:d4145a1a189a 49 memset(ntp_recv_values, 0x00, sizeof(ntp_recv_values));
nzvandan 0:d4145a1a189a 50
nzvandan 0:d4145a1a189a 51 UDPSocket sock;
nzvandan 0:d4145a1a189a 52 sock.open(iface);
nzvandan 0:d4145a1a189a 53 sock.set_timeout(timeout);
nzvandan 0:d4145a1a189a 54
nzvandan 0:d4145a1a189a 55 sock.sendto(nist, (void*)ntp_send_values, sizeof(ntp_send_values));
nzvandan 0:d4145a1a189a 56
nzvandan 0:d4145a1a189a 57 SocketAddress source;
nzvandan 0:d4145a1a189a 58 const int n = sock.recvfrom(&source, (void*)ntp_recv_values, sizeof(ntp_recv_values));
nzvandan 0:d4145a1a189a 59
nzvandan 0:d4145a1a189a 60 if (n > 10) {
nzvandan 0:d4145a1a189a 61 return ntohl(ntp_recv_values[10]) - TIME1970;
nzvandan 0:d4145a1a189a 62
nzvandan 0:d4145a1a189a 63 } else {
nzvandan 0:d4145a1a189a 64 if (n < 0) {
nzvandan 0:d4145a1a189a 65 // Network error
nzvandan 0:d4145a1a189a 66 return n;
nzvandan 0:d4145a1a189a 67
nzvandan 0:d4145a1a189a 68 } else {
nzvandan 0:d4145a1a189a 69 // No or partial data returned
nzvandan 0:d4145a1a189a 70 return -1;
nzvandan 0:d4145a1a189a 71 }
nzvandan 0:d4145a1a189a 72 }
nzvandan 0:d4145a1a189a 73
nzvandan 0:d4145a1a189a 74 } else {
nzvandan 0:d4145a1a189a 75 // No network interface
nzvandan 0:d4145a1a189a 76 return -2;
nzvandan 0:d4145a1a189a 77 }
nzvandan 0:d4145a1a189a 78 }
nzvandan 0:d4145a1a189a 79
nzvandan 0:d4145a1a189a 80 void NTPClient::network(NetworkInterface *interface) {
nzvandan 0:d4145a1a189a 81 iface = interface;
nzvandan 0:d4145a1a189a 82 }
nzvandan 0:d4145a1a189a 83
nzvandan 0:d4145a1a189a 84 uint32_t NTPClient::ntohl(uint32_t x) {
nzvandan 0:d4145a1a189a 85 uint32_t ret = (x & 0xff) << 24;
nzvandan 0:d4145a1a189a 86 ret |= (x & 0xff00) << 8;
nzvandan 0:d4145a1a189a 87 ret |= (x & 0xff0000UL) >> 8;
nzvandan 0:d4145a1a189a 88 ret |= (x & 0xff000000UL) >> 24;
nzvandan 0:d4145a1a189a 89 return ret;
nzvandan 0:d4145a1a189a 90 }