NTP client to get UTC time from the internet time servers
Dependents: NTPClient_HelloWorld
Fork of NTPClient by
NTPClient.cpp@5:fe36718063a5, 2016-12-14 (annotated)
- Committer:
- mapellil
- Date:
- Wed Dec 14 16:23:38 2016 +0000
- Revision:
- 5:fe36718063a5
- Parent:
- 4:881559865a93
- Child:
- 6:510bffa8b3d9
Porting to Nucleo F401RE over WiFi
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:04a82df0f587 | 1 | /* NTPClient.cpp */ |
donatien | 2:9a64a50df235 | 2 | /* Copyright (C) 2012 mbed.org, MIT License |
donatien | 2:9a64a50df235 | 3 | * |
donatien | 2:9a64a50df235 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
donatien | 2:9a64a50df235 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
donatien | 2:9a64a50df235 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
donatien | 2:9a64a50df235 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
donatien | 2:9a64a50df235 | 8 | * furnished to do so, subject to the following conditions: |
donatien | 2:9a64a50df235 | 9 | * |
donatien | 2:9a64a50df235 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
donatien | 2:9a64a50df235 | 11 | * substantial portions of the Software. |
donatien | 2:9a64a50df235 | 12 | * |
donatien | 2:9a64a50df235 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
donatien | 2:9a64a50df235 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
donatien | 2:9a64a50df235 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
donatien | 2:9a64a50df235 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 2:9a64a50df235 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
donatien | 2:9a64a50df235 | 18 | */ |
donatien | 0:04a82df0f587 | 19 | |
donatien | 2:9a64a50df235 | 20 | //Debug is disabled by default |
donatien | 4:881559865a93 | 21 | #if 0 |
donatien | 2:9a64a50df235 | 22 | //Enable debug |
donatien | 2:9a64a50df235 | 23 | #define __DEBUG__ |
donatien | 2:9a64a50df235 | 24 | #include <cstdio> |
donatien | 2:9a64a50df235 | 25 | #define DBG(x, ...) std::printf("[NTPClient : DBG]"x"\r\n", ##__VA_ARGS__); |
donatien | 2:9a64a50df235 | 26 | #define WARN(x, ...) std::printf("[NTPClient : WARN]"x"\r\n", ##__VA_ARGS__); |
donatien | 2:9a64a50df235 | 27 | #define ERR(x, ...) std::printf("[NTPClient : ERR]"x"\r\n", ##__VA_ARGS__); |
donatien | 0:04a82df0f587 | 28 | |
donatien | 2:9a64a50df235 | 29 | #else |
donatien | 2:9a64a50df235 | 30 | //Disable debug |
donatien | 2:9a64a50df235 | 31 | #define DBG(x, ...) |
donatien | 2:9a64a50df235 | 32 | #define WARN(x, ...) |
donatien | 2:9a64a50df235 | 33 | #define ERR(x, ...) |
donatien | 2:9a64a50df235 | 34 | |
donatien | 0:04a82df0f587 | 35 | #endif |
mapellil | 5:fe36718063a5 | 36 | #include "NetworkStack.h" |
donatien | 0:04a82df0f587 | 37 | #include "NTPClient.h" |
donatien | 0:04a82df0f587 | 38 | |
donatien | 1:b221a8765b3f | 39 | #include "UDPSocket.h" |
donatien | 1:b221a8765b3f | 40 | |
donatien | 0:04a82df0f587 | 41 | #include "mbed.h" //time() and set_time() |
donatien | 0:04a82df0f587 | 42 | |
donatien | 0:04a82df0f587 | 43 | #define NTP_PORT 123 |
donatien | 0:04a82df0f587 | 44 | #define NTP_CLIENT_PORT 0 //Random port |
donatien | 0:04a82df0f587 | 45 | #define NTP_TIMESTAMP_DELTA 2208988800ull //Diff btw a UNIX timestamp (Starting Jan, 1st 1970) and a NTP timestamp (Starting Jan, 1st 1900) |
donatien | 0:04a82df0f587 | 46 | |
mapellil | 5:fe36718063a5 | 47 | /*NTPClient::NTPClient() : m_sock() |
donatien | 0:04a82df0f587 | 48 | { |
donatien | 0:04a82df0f587 | 49 | |
mapellil | 5:fe36718063a5 | 50 | }*/ |
donatien | 0:04a82df0f587 | 51 | |
mapellil | 5:fe36718063a5 | 52 | NTPClient::NTPClient(NetworkStack & _m_intf) : m_intf(_m_intf) |
mapellil | 5:fe36718063a5 | 53 | { |
donatien | 0:04a82df0f587 | 54 | } |
donatien | 0:04a82df0f587 | 55 | |
mapellil | 5:fe36718063a5 | 56 | #ifdef htons |
mapellil | 5:fe36718063a5 | 57 | #undef htons |
mapellil | 5:fe36718063a5 | 58 | #endif /* htons */ |
mapellil | 5:fe36718063a5 | 59 | #ifdef htonl |
mapellil | 5:fe36718063a5 | 60 | #undef htonl |
mapellil | 5:fe36718063a5 | 61 | #endif /* htonl */ |
mapellil | 5:fe36718063a5 | 62 | #ifdef ntohs |
mapellil | 5:fe36718063a5 | 63 | #undef ntohs |
mapellil | 5:fe36718063a5 | 64 | #endif /* ntohs */ |
mapellil | 5:fe36718063a5 | 65 | #ifdef ntohl |
mapellil | 5:fe36718063a5 | 66 | #undef ntohl |
mapellil | 5:fe36718063a5 | 67 | #endif /* ntohl */ |
mapellil | 5:fe36718063a5 | 68 | |
mapellil | 5:fe36718063a5 | 69 | |
mapellil | 5:fe36718063a5 | 70 | #if ((__BYTE_ORDER__) == (__ORDER_LITTLE_ENDIAN__)) |
mapellil | 5:fe36718063a5 | 71 | |
mapellil | 5:fe36718063a5 | 72 | #define htons(x) ((((x) & 0xff) << 8) | (((x) & 0xff00) >> 8)) |
mapellil | 5:fe36718063a5 | 73 | #define ntohs(x) htons(x) |
mapellil | 5:fe36718063a5 | 74 | #define htonl(x) ((((x) & 0xff) << 24) | \ |
mapellil | 5:fe36718063a5 | 75 | (((x) & 0xff00) << 8) | \ |
mapellil | 5:fe36718063a5 | 76 | (((x) & 0xff0000UL) >> 8) | \ |
mapellil | 5:fe36718063a5 | 77 | (((x) & 0xff000000UL) >> 24)) |
mapellil | 5:fe36718063a5 | 78 | #define ntohl(x) htonl(x) |
mapellil | 5:fe36718063a5 | 79 | |
mapellil | 5:fe36718063a5 | 80 | #else |
mapellil | 5:fe36718063a5 | 81 | |
mapellil | 5:fe36718063a5 | 82 | #define htons(x) (x) |
mapellil | 5:fe36718063a5 | 83 | #define htonl(x) (x) |
mapellil | 5:fe36718063a5 | 84 | #define ntohl(x) (x) |
mapellil | 5:fe36718063a5 | 85 | #define ntohs(x) (x) |
mapellil | 5:fe36718063a5 | 86 | |
mapellil | 5:fe36718063a5 | 87 | #endif |
mapellil | 5:fe36718063a5 | 88 | |
mapellil | 5:fe36718063a5 | 89 | |
donatien | 2:9a64a50df235 | 90 | NTPResult NTPClient::setTime(const char* host, uint16_t port, uint32_t timeout) |
donatien | 0:04a82df0f587 | 91 | { |
donatien | 2:9a64a50df235 | 92 | #ifdef __DEBUG__ |
donatien | 0:04a82df0f587 | 93 | time_t ctTime; |
donatien | 0:04a82df0f587 | 94 | ctTime = time(NULL); |
mapellil | 5:fe36718063a5 | 95 | set_time(ctTime); |
donatien | 2:9a64a50df235 | 96 | DBG("Time is set to (UTC): %s", ctime(&ctTime)); |
donatien | 0:04a82df0f587 | 97 | #endif |
donatien | 0:04a82df0f587 | 98 | |
mapellil | 5:fe36718063a5 | 99 | SocketAddress address(0, port); |
mapellil | 5:fe36718063a5 | 100 | int r = m_intf.gethostbyname(&address, host); |
mapellil | 5:fe36718063a5 | 101 | if (r) { |
mapellil | 5:fe36718063a5 | 102 | printf("error: 'gethostbyname(\"%s\")' failed with code %d\r\n", host, r); |
mapellil | 5:fe36718063a5 | 103 | } else if (!address) { |
mapellil | 5:fe36718063a5 | 104 | printf("error: 'gethostbyname(\"%s\")' returned null IP address\r\n", host); |
mapellil | 5:fe36718063a5 | 105 | } |
mapellil | 5:fe36718063a5 | 106 | //printf ("address: %s\n\r",address.get_ip_address()); |
mapellil | 5:fe36718063a5 | 107 | |
donatien | 1:b221a8765b3f | 108 | //Create & bind socket |
mapellil | 5:fe36718063a5 | 109 | if (m_sock.open(&m_intf) < 0) printf ("ERROR sock open \n\r"); |
mapellil | 5:fe36718063a5 | 110 | m_sock.set_blocking(true); //Set blocking LICIO |
donatien | 0:04a82df0f587 | 111 | |
mapellil | 5:fe36718063a5 | 112 | struct NTPPacket pkt; |
mapellil | 5:fe36718063a5 | 113 | memset (&pkt, 0, sizeof(NTPPacket)); |
donatien | 0:04a82df0f587 | 114 | |
donatien | 0:04a82df0f587 | 115 | //Now ping the server and wait for response |
donatien | 0:04a82df0f587 | 116 | DBG("Ping"); |
donatien | 0:04a82df0f587 | 117 | //Prepare NTP Packet: |
donatien | 0:04a82df0f587 | 118 | pkt.li = 0; //Leap Indicator : No warning |
donatien | 0:04a82df0f587 | 119 | pkt.vn = 4; //Version Number : 4 |
donatien | 0:04a82df0f587 | 120 | pkt.mode = 3; //Client mode |
donatien | 0:04a82df0f587 | 121 | pkt.stratum = 0; //Not relevant here |
donatien | 0:04a82df0f587 | 122 | pkt.poll = 0; //Not significant as well |
donatien | 0:04a82df0f587 | 123 | pkt.precision = 0; //Neither this one is |
donatien | 0:04a82df0f587 | 124 | |
mapellil | 5:fe36718063a5 | 125 | int ret = m_sock.sendto(address, (char*)&pkt, sizeof(NTPPacket) ); |
donatien | 2:9a64a50df235 | 126 | if (ret < 0 ) |
donatien | 0:04a82df0f587 | 127 | { |
mapellil | 5:fe36718063a5 | 128 | ERR("Could not send packet %d", ret); |
donatien | 2:9a64a50df235 | 129 | m_sock.close(); |
donatien | 2:9a64a50df235 | 130 | return NTP_CONN; |
donatien | 0:04a82df0f587 | 131 | } |
donatien | 0:04a82df0f587 | 132 | |
donatien | 0:04a82df0f587 | 133 | //Read response |
mapellil | 5:fe36718063a5 | 134 | DBG("Pong"); |
donatien | 2:9a64a50df235 | 135 | |
mapellil | 5:fe36718063a5 | 136 | ret = m_sock.recvfrom(&address, (char*)&pkt, sizeof(NTPPacket) ); // LICIO |
mapellil | 5:fe36718063a5 | 137 | if(ret < 0) |
donatien | 0:04a82df0f587 | 138 | { |
mapellil | 5:fe36718063a5 | 139 | ERR("Could not receive packet %d", ret); |
mapellil | 5:fe36718063a5 | 140 | m_sock.close(); |
mapellil | 5:fe36718063a5 | 141 | return NTP_CONN; |
mapellil | 5:fe36718063a5 | 142 | } |
donatien | 0:04a82df0f587 | 143 | |
donatien | 0:04a82df0f587 | 144 | if(ret < sizeof(NTPPacket)) //TODO: Accept chunks |
donatien | 0:04a82df0f587 | 145 | { |
donatien | 0:04a82df0f587 | 146 | ERR("Receive packet size does not match"); |
donatien | 2:9a64a50df235 | 147 | m_sock.close(); |
donatien | 2:9a64a50df235 | 148 | return NTP_PRTCL; |
donatien | 0:04a82df0f587 | 149 | } |
donatien | 0:04a82df0f587 | 150 | |
donatien | 0:04a82df0f587 | 151 | if( pkt.stratum == 0) //Kiss of death message : Not good ! |
donatien | 0:04a82df0f587 | 152 | { |
donatien | 0:04a82df0f587 | 153 | ERR("Kissed to death!"); |
donatien | 2:9a64a50df235 | 154 | m_sock.close(); |
donatien | 2:9a64a50df235 | 155 | return NTP_PRTCL; |
donatien | 0:04a82df0f587 | 156 | } |
donatien | 0:04a82df0f587 | 157 | |
donatien | 0:04a82df0f587 | 158 | //Correct Endianness |
mapellil | 5:fe36718063a5 | 159 | pkt.refTm_s = ntohl( pkt.refTm_s ); |
donatien | 0:04a82df0f587 | 160 | pkt.refTm_f = ntohl( pkt.refTm_f ); |
donatien | 0:04a82df0f587 | 161 | pkt.origTm_s = ntohl( pkt.origTm_s ); |
donatien | 0:04a82df0f587 | 162 | pkt.origTm_f = ntohl( pkt.origTm_f ); |
donatien | 0:04a82df0f587 | 163 | pkt.rxTm_s = ntohl( pkt.rxTm_s ); |
donatien | 0:04a82df0f587 | 164 | pkt.rxTm_f = ntohl( pkt.rxTm_f ); |
donatien | 0:04a82df0f587 | 165 | pkt.txTm_s = ntohl( pkt.txTm_s ); |
donatien | 0:04a82df0f587 | 166 | pkt.txTm_f = ntohl( pkt.txTm_f ); |
donatien | 0:04a82df0f587 | 167 | |
mapellil | 5:fe36718063a5 | 168 | // see RFC 4330 p.13 |
mapellil | 5:fe36718063a5 | 169 | time_t txTm = (time_t)(pkt.txTm_s - NTP_TIMESTAMP_DELTA); |
mapellil | 5:fe36718063a5 | 170 | set_time(txTm); |
donatien | 0:04a82df0f587 | 171 | |
donatien | 2:9a64a50df235 | 172 | #ifdef __DEBUG__ |
donatien | 0:04a82df0f587 | 173 | ctTime = time(NULL); |
donatien | 2:9a64a50df235 | 174 | DBG("Time is now (UTC): %s", ctime(&ctTime)); |
donatien | 0:04a82df0f587 | 175 | #endif |
donatien | 2:9a64a50df235 | 176 | m_sock.close(); |
donatien | 0:04a82df0f587 | 177 | |
donatien | 2:9a64a50df235 | 178 | return NTP_OK; |
donatien | 0:04a82df0f587 | 179 | } |
donatien | 0:04a82df0f587 | 180 |