Final 350 project

Dependencies:   uzair Camera_LS_Y201 F7_Ethernet LCD_DISCO_F746NG NetworkAPI SDFileSystem mbed

Committer:
shoaib_ahmed
Date:
Mon Jul 31 09:16:35 2017 +0000
Revision:
0:791a779d6220
final project;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shoaib_ahmed 0:791a779d6220 1 /* NTPClient.cpp */
shoaib_ahmed 0:791a779d6220 2 /* Copyright (C) 2012 mbed.org, MIT License
shoaib_ahmed 0:791a779d6220 3 *
shoaib_ahmed 0:791a779d6220 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
shoaib_ahmed 0:791a779d6220 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
shoaib_ahmed 0:791a779d6220 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
shoaib_ahmed 0:791a779d6220 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
shoaib_ahmed 0:791a779d6220 8 * furnished to do so, subject to the following conditions:
shoaib_ahmed 0:791a779d6220 9 *
shoaib_ahmed 0:791a779d6220 10 * The above copyright notice and this permission notice shall be included in all copies or
shoaib_ahmed 0:791a779d6220 11 * substantial portions of the Software.
shoaib_ahmed 0:791a779d6220 12 *
shoaib_ahmed 0:791a779d6220 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
shoaib_ahmed 0:791a779d6220 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
shoaib_ahmed 0:791a779d6220 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
shoaib_ahmed 0:791a779d6220 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
shoaib_ahmed 0:791a779d6220 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
shoaib_ahmed 0:791a779d6220 18 */
shoaib_ahmed 0:791a779d6220 19
shoaib_ahmed 0:791a779d6220 20 //Debug is disabled by default
shoaib_ahmed 0:791a779d6220 21 #if 0
shoaib_ahmed 0:791a779d6220 22 //Enable debug
shoaib_ahmed 0:791a779d6220 23 #define __DEBUG__
shoaib_ahmed 0:791a779d6220 24 #include <cstdio>
shoaib_ahmed 0:791a779d6220 25 #define DBG(x, ...) std::printf("[NTPClient : DBG]"x"\r\n", ##__VA_ARGS__);
shoaib_ahmed 0:791a779d6220 26 #define WARN(x, ...) std::printf("[NTPClient : WARN]"x"\r\n", ##__VA_ARGS__);
shoaib_ahmed 0:791a779d6220 27 #define ERR(x, ...) std::printf("[NTPClient : ERR]"x"\r\n", ##__VA_ARGS__);
shoaib_ahmed 0:791a779d6220 28
shoaib_ahmed 0:791a779d6220 29 #else
shoaib_ahmed 0:791a779d6220 30 //Disable debug
shoaib_ahmed 0:791a779d6220 31 #define DBG(x, ...)
shoaib_ahmed 0:791a779d6220 32 #define WARN(x, ...)
shoaib_ahmed 0:791a779d6220 33 #define ERR(x, ...)
shoaib_ahmed 0:791a779d6220 34
shoaib_ahmed 0:791a779d6220 35 #endif
shoaib_ahmed 0:791a779d6220 36
shoaib_ahmed 0:791a779d6220 37 #include "NTPClient.h"
shoaib_ahmed 0:791a779d6220 38
shoaib_ahmed 0:791a779d6220 39 #include "UDPSocket.h"
shoaib_ahmed 0:791a779d6220 40
shoaib_ahmed 0:791a779d6220 41 #include "mbed.h" //time() and set_time()
shoaib_ahmed 0:791a779d6220 42
shoaib_ahmed 0:791a779d6220 43 #define NTP_PORT 123
shoaib_ahmed 0:791a779d6220 44 #define NTP_CLIENT_PORT 0 //Random port
shoaib_ahmed 0:791a779d6220 45 #define NTP_TIMESTAMP_DELTA 2208988800ull //Diff btw a UNIX timestamp (Starting Jan, 1st 1970) and a NTP timestamp (Starting Jan, 1st 1900)
shoaib_ahmed 0:791a779d6220 46
shoaib_ahmed 0:791a779d6220 47 NTPClient::NTPClient() : m_sock()
shoaib_ahmed 0:791a779d6220 48 {
shoaib_ahmed 0:791a779d6220 49
shoaib_ahmed 0:791a779d6220 50
shoaib_ahmed 0:791a779d6220 51 }
shoaib_ahmed 0:791a779d6220 52
shoaib_ahmed 0:791a779d6220 53 NTPResult NTPClient::setTime(const char* host, uint16_t port, uint32_t timeout)
shoaib_ahmed 0:791a779d6220 54 {
shoaib_ahmed 0:791a779d6220 55 #ifdef __DEBUG__
shoaib_ahmed 0:791a779d6220 56 time_t ctTime;
shoaib_ahmed 0:791a779d6220 57 ctTime = time(NULL);
shoaib_ahmed 0:791a779d6220 58 DBG("Time is set to (UTC): %s", ctime(&ctTime));
shoaib_ahmed 0:791a779d6220 59 #endif
shoaib_ahmed 0:791a779d6220 60
shoaib_ahmed 0:791a779d6220 61 //Create & bind socket
shoaib_ahmed 0:791a779d6220 62 DBG("Binding socket");
shoaib_ahmed 0:791a779d6220 63 m_sock.bind(0); //Bind to a random port
shoaib_ahmed 0:791a779d6220 64
shoaib_ahmed 0:791a779d6220 65 m_sock.set_blocking(false, timeout); //Set not blocking
shoaib_ahmed 0:791a779d6220 66
shoaib_ahmed 0:791a779d6220 67 struct NTPPacket pkt;
shoaib_ahmed 0:791a779d6220 68
shoaib_ahmed 0:791a779d6220 69 //Now ping the server and wait for response
shoaib_ahmed 0:791a779d6220 70 DBG("Ping");
shoaib_ahmed 0:791a779d6220 71 //Prepare NTP Packet:
shoaib_ahmed 0:791a779d6220 72 pkt.li = 0; //Leap Indicator : No warning
shoaib_ahmed 0:791a779d6220 73 pkt.vn = 4; //Version Number : 4
shoaib_ahmed 0:791a779d6220 74 pkt.mode = 3; //Client mode
shoaib_ahmed 0:791a779d6220 75 pkt.stratum = 0; //Not relevant here
shoaib_ahmed 0:791a779d6220 76 pkt.poll = 0; //Not significant as well
shoaib_ahmed 0:791a779d6220 77 pkt.precision = 0; //Neither this one is
shoaib_ahmed 0:791a779d6220 78
shoaib_ahmed 0:791a779d6220 79 pkt.rootDelay = 0; //Or this one
shoaib_ahmed 0:791a779d6220 80 pkt.rootDispersion = 0; //Or that one
shoaib_ahmed 0:791a779d6220 81 pkt.refId = 0; //...
shoaib_ahmed 0:791a779d6220 82
shoaib_ahmed 0:791a779d6220 83 pkt.refTm_s = 0;
shoaib_ahmed 0:791a779d6220 84 pkt.origTm_s = 0;
shoaib_ahmed 0:791a779d6220 85 pkt.rxTm_s = 0;
shoaib_ahmed 0:791a779d6220 86 pkt.txTm_s = htonl( NTP_TIMESTAMP_DELTA + time(NULL) ); //WARN: We are in LE format, network byte order is BE
shoaib_ahmed 0:791a779d6220 87
shoaib_ahmed 0:791a779d6220 88 pkt.refTm_f = pkt.origTm_f = pkt.rxTm_f = pkt.txTm_f = 0;
shoaib_ahmed 0:791a779d6220 89
shoaib_ahmed 0:791a779d6220 90 Endpoint outEndpoint;
shoaib_ahmed 0:791a779d6220 91
shoaib_ahmed 0:791a779d6220 92 if( outEndpoint.set_address(host, port) < 0)
shoaib_ahmed 0:791a779d6220 93 {
shoaib_ahmed 0:791a779d6220 94 m_sock.close();
shoaib_ahmed 0:791a779d6220 95 return NTP_DNS;
shoaib_ahmed 0:791a779d6220 96 }
shoaib_ahmed 0:791a779d6220 97
shoaib_ahmed 0:791a779d6220 98 //Set timeout, non-blocking and wait using select
shoaib_ahmed 0:791a779d6220 99 int ret = m_sock.sendTo( outEndpoint, (char*)&pkt, sizeof(NTPPacket) );
shoaib_ahmed 0:791a779d6220 100 if (ret < 0 )
shoaib_ahmed 0:791a779d6220 101 {
shoaib_ahmed 0:791a779d6220 102 ERR("Could not send packet");
shoaib_ahmed 0:791a779d6220 103 m_sock.close();
shoaib_ahmed 0:791a779d6220 104 return NTP_CONN;
shoaib_ahmed 0:791a779d6220 105 }
shoaib_ahmed 0:791a779d6220 106
shoaib_ahmed 0:791a779d6220 107 //Read response
shoaib_ahmed 0:791a779d6220 108 Endpoint inEndpoint;
shoaib_ahmed 0:791a779d6220 109 // Set the inEndpoint address property
shoaib_ahmed 0:791a779d6220 110 inEndpoint.set_address(outEndpoint.get_address(), 0);
shoaib_ahmed 0:791a779d6220 111 DBG("Pong");
shoaib_ahmed 0:791a779d6220 112 do
shoaib_ahmed 0:791a779d6220 113 {
shoaib_ahmed 0:791a779d6220 114 ret = m_sock.receiveFrom( inEndpoint, (char*)&pkt, sizeof(NTPPacket) ); //FIXME need a DNS Resolver to actually compare the incoming address with the DNS name
shoaib_ahmed 0:791a779d6220 115 if(ret < 0)
shoaib_ahmed 0:791a779d6220 116 {
shoaib_ahmed 0:791a779d6220 117 ERR("Could not receive packet");
shoaib_ahmed 0:791a779d6220 118 m_sock.close();
shoaib_ahmed 0:791a779d6220 119 return NTP_CONN;
shoaib_ahmed 0:791a779d6220 120 }
shoaib_ahmed 0:791a779d6220 121 } while( strcmp(outEndpoint.get_address(), inEndpoint.get_address()) != 0 );
shoaib_ahmed 0:791a779d6220 122
shoaib_ahmed 0:791a779d6220 123 if(ret < sizeof(NTPPacket)) //TODO: Accept chunks
shoaib_ahmed 0:791a779d6220 124 {
shoaib_ahmed 0:791a779d6220 125 ERR("Receive packet size does not match");
shoaib_ahmed 0:791a779d6220 126 m_sock.close();
shoaib_ahmed 0:791a779d6220 127 return NTP_PRTCL;
shoaib_ahmed 0:791a779d6220 128 }
shoaib_ahmed 0:791a779d6220 129
shoaib_ahmed 0:791a779d6220 130 if( pkt.stratum == 0) //Kiss of death message : Not good !
shoaib_ahmed 0:791a779d6220 131 {
shoaib_ahmed 0:791a779d6220 132 ERR("Kissed to death!");
shoaib_ahmed 0:791a779d6220 133 m_sock.close();
shoaib_ahmed 0:791a779d6220 134 return NTP_PRTCL;
shoaib_ahmed 0:791a779d6220 135 }
shoaib_ahmed 0:791a779d6220 136
shoaib_ahmed 0:791a779d6220 137 //Correct Endianness
shoaib_ahmed 0:791a779d6220 138 pkt.refTm_s = ntohl( pkt.refTm_s );
shoaib_ahmed 0:791a779d6220 139 pkt.refTm_f = ntohl( pkt.refTm_f );
shoaib_ahmed 0:791a779d6220 140 pkt.origTm_s = ntohl( pkt.origTm_s );
shoaib_ahmed 0:791a779d6220 141 pkt.origTm_f = ntohl( pkt.origTm_f );
shoaib_ahmed 0:791a779d6220 142 pkt.rxTm_s = ntohl( pkt.rxTm_s );
shoaib_ahmed 0:791a779d6220 143 pkt.rxTm_f = ntohl( pkt.rxTm_f );
shoaib_ahmed 0:791a779d6220 144 pkt.txTm_s = ntohl( pkt.txTm_s );
shoaib_ahmed 0:791a779d6220 145 pkt.txTm_f = ntohl( pkt.txTm_f );
shoaib_ahmed 0:791a779d6220 146
shoaib_ahmed 0:791a779d6220 147 //Compute offset, see RFC 4330 p.13
shoaib_ahmed 0:791a779d6220 148 uint32_t destTm_s = (NTP_TIMESTAMP_DELTA + time(NULL));
shoaib_ahmed 0:791a779d6220 149 int64_t offset = ( (int64_t)( pkt.rxTm_s - pkt.origTm_s ) + (int64_t) ( pkt.txTm_s - destTm_s ) ) / 2; //Avoid overflow
shoaib_ahmed 0:791a779d6220 150 DBG("Sent @%ul", pkt.txTm_s);
shoaib_ahmed 0:791a779d6220 151 DBG("Offset: %lld", offset);
shoaib_ahmed 0:791a779d6220 152 //Set time accordingly
shoaib_ahmed 0:791a779d6220 153 set_time( time(NULL) + offset );
shoaib_ahmed 0:791a779d6220 154
shoaib_ahmed 0:791a779d6220 155 #ifdef __DEBUG__
shoaib_ahmed 0:791a779d6220 156 ctTime = time(NULL);
shoaib_ahmed 0:791a779d6220 157 DBG("Time is now (UTC): %s", ctime(&ctTime));
shoaib_ahmed 0:791a779d6220 158 #endif
shoaib_ahmed 0:791a779d6220 159
shoaib_ahmed 0:791a779d6220 160 m_sock.close();
shoaib_ahmed 0:791a779d6220 161
shoaib_ahmed 0:791a779d6220 162 return NTP_OK;
shoaib_ahmed 0:791a779d6220 163 }
shoaib_ahmed 0:791a779d6220 164