bunch of tesitng for F746NG

Dependencies:   BSP_DISCO_F746NG F746_GUI F7_Ethernet LCD_DISCO_F746NG SimpleSocket TMP36 GZ TS_DISCO_F746NG TextLCD WebSocketClient mbed-rtos mbed sMotor

Committer:
Maricius
Date:
Mon Jun 18 13:16:23 2018 +0000
Revision:
1:1f4543ea364d
Parent:
0:45610c4af223
initial commit

Who changed what in which revision?

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