NTP Client for the mbed networking libraries
Dependents: NTPClient_HelloWorld temp_FIAP NTPClient_Wifly_HelloWorld RTOSTest ... more
Fork of NTPClientLib by
NTPClient.cpp@3:17d8bad407cc, 2012-08-05 (annotated)
- Committer:
- donatien
- Date:
- Sun Aug 05 16:10:33 2012 +0000
- Revision:
- 3:17d8bad407cc
- Parent:
- 2:9a64a50df235
- Child:
- 4:881559865a93
Fixed blocking mode
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 | 2:9a64a50df235 | 21 | #if |
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 |
donatien | 0:04a82df0f587 | 36 | |
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 | |
donatien | 2:9a64a50df235 | 47 | NTPClient::NTPClient() : m_sock() |
donatien | 0:04a82df0f587 | 48 | { |
donatien | 0:04a82df0f587 | 49 | |
donatien | 0:04a82df0f587 | 50 | |
donatien | 0:04a82df0f587 | 51 | } |
donatien | 0:04a82df0f587 | 52 | |
donatien | 2:9a64a50df235 | 53 | NTPResult NTPClient::setTime(const char* host, uint16_t port, uint32_t timeout) |
donatien | 0:04a82df0f587 | 54 | { |
donatien | 2:9a64a50df235 | 55 | #ifdef __DEBUG__ |
donatien | 0:04a82df0f587 | 56 | time_t ctTime; |
donatien | 0:04a82df0f587 | 57 | ctTime = time(NULL); |
donatien | 2:9a64a50df235 | 58 | DBG("Time is set to (UTC): %s", ctime(&ctTime)); |
donatien | 0:04a82df0f587 | 59 | #endif |
donatien | 0:04a82df0f587 | 60 | |
donatien | 1:b221a8765b3f | 61 | //Create & bind socket |
donatien | 2:9a64a50df235 | 62 | DBG("Binding socket"); |
donatien | 2:9a64a50df235 | 63 | m_sock.bind(0); //Bind to a random port |
donatien | 2:9a64a50df235 | 64 | |
donatien | 3:17d8bad407cc | 65 | m_sock.set_blocking(false, timeout); //Set not blocking |
donatien | 0:04a82df0f587 | 66 | |
donatien | 0:04a82df0f587 | 67 | struct NTPPacket pkt; |
donatien | 0:04a82df0f587 | 68 | |
donatien | 0:04a82df0f587 | 69 | //Now ping the server and wait for response |
donatien | 0:04a82df0f587 | 70 | DBG("Ping"); |
donatien | 0:04a82df0f587 | 71 | //Prepare NTP Packet: |
donatien | 0:04a82df0f587 | 72 | pkt.li = 0; //Leap Indicator : No warning |
donatien | 0:04a82df0f587 | 73 | pkt.vn = 4; //Version Number : 4 |
donatien | 0:04a82df0f587 | 74 | pkt.mode = 3; //Client mode |
donatien | 0:04a82df0f587 | 75 | pkt.stratum = 0; //Not relevant here |
donatien | 0:04a82df0f587 | 76 | pkt.poll = 0; //Not significant as well |
donatien | 0:04a82df0f587 | 77 | pkt.precision = 0; //Neither this one is |
donatien | 0:04a82df0f587 | 78 | |
donatien | 0:04a82df0f587 | 79 | pkt.rootDelay = 0; //Or this one |
donatien | 0:04a82df0f587 | 80 | pkt.rootDispersion = 0; //Or that one |
donatien | 0:04a82df0f587 | 81 | pkt.refId = 0; //... |
donatien | 0:04a82df0f587 | 82 | |
donatien | 0:04a82df0f587 | 83 | pkt.refTm_s = 0; |
donatien | 0:04a82df0f587 | 84 | pkt.origTm_s = 0; |
donatien | 0:04a82df0f587 | 85 | pkt.rxTm_s = 0; |
donatien | 0:04a82df0f587 | 86 | pkt.txTm_s = htonl( NTP_TIMESTAMP_DELTA + time(NULL) ); //WARN: We are in LE format, network byte order is BE |
donatien | 0:04a82df0f587 | 87 | |
donatien | 0:04a82df0f587 | 88 | pkt.refTm_f = pkt.origTm_f = pkt.rxTm_f = pkt.txTm_f = 0; |
donatien | 0:04a82df0f587 | 89 | |
donatien | 2:9a64a50df235 | 90 | Endpoint outEndpoint; |
donatien | 2:9a64a50df235 | 91 | |
donatien | 2:9a64a50df235 | 92 | if( outEndpoint.set_address(host, port) < 0) |
donatien | 2:9a64a50df235 | 93 | { |
donatien | 2:9a64a50df235 | 94 | m_sock.close(); |
donatien | 2:9a64a50df235 | 95 | return NTP_DNS; |
donatien | 2:9a64a50df235 | 96 | } |
donatien | 2:9a64a50df235 | 97 | |
donatien | 0:04a82df0f587 | 98 | //Set timeout, non-blocking and wait using select |
donatien | 2:9a64a50df235 | 99 | int ret = m_sock.sendTo( outEndpoint, (char*)&pkt, sizeof(NTPPacket) ); |
donatien | 2:9a64a50df235 | 100 | if (ret < 0 ) |
donatien | 0:04a82df0f587 | 101 | { |
donatien | 0:04a82df0f587 | 102 | ERR("Could not send packet"); |
donatien | 2:9a64a50df235 | 103 | m_sock.close(); |
donatien | 2:9a64a50df235 | 104 | return NTP_CONN; |
donatien | 0:04a82df0f587 | 105 | } |
donatien | 0:04a82df0f587 | 106 | |
donatien | 0:04a82df0f587 | 107 | //Read response |
donatien | 2:9a64a50df235 | 108 | Endpoint inEndpoint; |
donatien | 2:9a64a50df235 | 109 | |
donatien | 0:04a82df0f587 | 110 | DBG("Pong"); |
donatien | 0:04a82df0f587 | 111 | do |
donatien | 0:04a82df0f587 | 112 | { |
donatien | 2:9a64a50df235 | 113 | ret = m_sock.receiveFrom( inEndpoint, (char*)&pkt, sizeof(NTPPacket) ); //FIXME need a DNS Resolver to actually compare the incoming address with the DNS name |
donatien | 0:04a82df0f587 | 114 | if(ret < 0) |
donatien | 0:04a82df0f587 | 115 | { |
donatien | 0:04a82df0f587 | 116 | ERR("Could not receive packet"); |
donatien | 2:9a64a50df235 | 117 | m_sock.close(); |
donatien | 2:9a64a50df235 | 118 | return NTP_CONN; |
donatien | 0:04a82df0f587 | 119 | } |
donatien | 2:9a64a50df235 | 120 | } while( strcmp(outEndpoint.get_address(), inEndpoint.get_address()) != 0 ); |
donatien | 0:04a82df0f587 | 121 | |
donatien | 0:04a82df0f587 | 122 | if(ret < sizeof(NTPPacket)) //TODO: Accept chunks |
donatien | 0:04a82df0f587 | 123 | { |
donatien | 0:04a82df0f587 | 124 | ERR("Receive packet size does not match"); |
donatien | 2:9a64a50df235 | 125 | m_sock.close(); |
donatien | 2:9a64a50df235 | 126 | return NTP_PRTCL; |
donatien | 0:04a82df0f587 | 127 | } |
donatien | 0:04a82df0f587 | 128 | |
donatien | 0:04a82df0f587 | 129 | if( pkt.stratum == 0) //Kiss of death message : Not good ! |
donatien | 0:04a82df0f587 | 130 | { |
donatien | 0:04a82df0f587 | 131 | ERR("Kissed to death!"); |
donatien | 2:9a64a50df235 | 132 | m_sock.close(); |
donatien | 2:9a64a50df235 | 133 | return NTP_PRTCL; |
donatien | 0:04a82df0f587 | 134 | } |
donatien | 0:04a82df0f587 | 135 | |
donatien | 0:04a82df0f587 | 136 | //Correct Endianness |
donatien | 0:04a82df0f587 | 137 | pkt.refTm_s = ntohl( pkt.refTm_s ); |
donatien | 0:04a82df0f587 | 138 | pkt.refTm_f = ntohl( pkt.refTm_f ); |
donatien | 0:04a82df0f587 | 139 | pkt.origTm_s = ntohl( pkt.origTm_s ); |
donatien | 0:04a82df0f587 | 140 | pkt.origTm_f = ntohl( pkt.origTm_f ); |
donatien | 0:04a82df0f587 | 141 | pkt.rxTm_s = ntohl( pkt.rxTm_s ); |
donatien | 0:04a82df0f587 | 142 | pkt.rxTm_f = ntohl( pkt.rxTm_f ); |
donatien | 0:04a82df0f587 | 143 | pkt.txTm_s = ntohl( pkt.txTm_s ); |
donatien | 0:04a82df0f587 | 144 | pkt.txTm_f = ntohl( pkt.txTm_f ); |
donatien | 0:04a82df0f587 | 145 | |
donatien | 0:04a82df0f587 | 146 | //Compute offset, see RFC 4330 p.13 |
donatien | 0:04a82df0f587 | 147 | uint32_t destTm_s = (NTP_TIMESTAMP_DELTA + time(NULL)); |
donatien | 0:04a82df0f587 | 148 | int64_t offset = ( (int64_t)( pkt.rxTm_s - pkt.origTm_s ) + (int64_t) ( pkt.txTm_s - destTm_s ) ) / 2; //Avoid overflow |
donatien | 0:04a82df0f587 | 149 | DBG("Sent @%ul", pkt.txTm_s); |
donatien | 2:9a64a50df235 | 150 | DBG("Offset: %lld", offset); |
donatien | 0:04a82df0f587 | 151 | //Set time accordingly |
donatien | 0:04a82df0f587 | 152 | set_time( time(NULL) + offset ); |
donatien | 0:04a82df0f587 | 153 | |
donatien | 2:9a64a50df235 | 154 | #ifdef __DEBUG__ |
donatien | 0:04a82df0f587 | 155 | ctTime = time(NULL); |
donatien | 2:9a64a50df235 | 156 | DBG("Time is now (UTC): %s", ctime(&ctTime)); |
donatien | 0:04a82df0f587 | 157 | #endif |
donatien | 0:04a82df0f587 | 158 | |
donatien | 2:9a64a50df235 | 159 | m_sock.close(); |
donatien | 0:04a82df0f587 | 160 | |
donatien | 2:9a64a50df235 | 161 | return NTP_OK; |
donatien | 0:04a82df0f587 | 162 | } |
donatien | 0:04a82df0f587 | 163 |