Michiel Berckvens / Mbed 2 deprecated ProjectHTTP

Dependencies:   DS1307 TextLCD mbed

Committer:
Michielber
Date:
Thu Dec 04 10:36:40 2014 +0000
Revision:
0:f615d151a72c
Berckvens Michiel & Basteyns Jonas 4/12/2014

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Michielber 0:f615d151a72c 1
Michielber 0:f615d151a72c 2 /*
Michielber 0:f615d151a72c 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
Michielber 0:f615d151a72c 4
Michielber 0:f615d151a72c 5 Permission is hereby granted, free of charge, to any person obtaining a copy
Michielber 0:f615d151a72c 6 of this software and associated documentation files (the "Software"), to deal
Michielber 0:f615d151a72c 7 in the Software without restriction, including without limitation the rights
Michielber 0:f615d151a72c 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Michielber 0:f615d151a72c 9 copies of the Software, and to permit persons to whom the Software is
Michielber 0:f615d151a72c 10 furnished to do so, subject to the following conditions:
Michielber 0:f615d151a72c 11
Michielber 0:f615d151a72c 12 The above copyright notice and this permission notice shall be included in
Michielber 0:f615d151a72c 13 all copies or substantial portions of the Software.
Michielber 0:f615d151a72c 14
Michielber 0:f615d151a72c 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Michielber 0:f615d151a72c 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Michielber 0:f615d151a72c 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Michielber 0:f615d151a72c 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Michielber 0:f615d151a72c 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Michielber 0:f615d151a72c 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Michielber 0:f615d151a72c 21 THE SOFTWARE.
Michielber 0:f615d151a72c 22 */
Michielber 0:f615d151a72c 23
Michielber 0:f615d151a72c 24 /** \file
Michielber 0:f615d151a72c 25 NTP Client header file
Michielber 0:f615d151a72c 26 */
Michielber 0:f615d151a72c 27
Michielber 0:f615d151a72c 28 #ifndef NTP_CLIENT_H
Michielber 0:f615d151a72c 29 #define NTP_CLIENT_H
Michielber 0:f615d151a72c 30
Michielber 0:f615d151a72c 31 #include "core/net.h"
Michielber 0:f615d151a72c 32 #include "core/netservice.h"
Michielber 0:f615d151a72c 33 #include "api/UDPSocket.h"
Michielber 0:f615d151a72c 34 #include "api/DNSRequest.h"
Michielber 0:f615d151a72c 35 #include "mbed.h"
Michielber 0:f615d151a72c 36
Michielber 0:f615d151a72c 37 ///NTP Client results
Michielber 0:f615d151a72c 38 enum NTPResult
Michielber 0:f615d151a72c 39 {
Michielber 0:f615d151a72c 40 NTP_OK, ///<Success
Michielber 0:f615d151a72c 41 NTP_PROCESSING, ///<Processing
Michielber 0:f615d151a72c 42 NTP_PRTCL, ///<Protocol error
Michielber 0:f615d151a72c 43 NTP_TIMEOUT, ///<Connection timeout
Michielber 0:f615d151a72c 44 NTP_DNS ///<Could not resolve DNS hostname
Michielber 0:f615d151a72c 45 };
Michielber 0:f615d151a72c 46
Michielber 0:f615d151a72c 47 ///A NTP Client
Michielber 0:f615d151a72c 48 /**
Michielber 0:f615d151a72c 49 The NTP client is a simple UDP client that will update the mbed's RTC
Michielber 0:f615d151a72c 50 */
Michielber 0:f615d151a72c 51 class NTPClient : protected NetService
Michielber 0:f615d151a72c 52 {
Michielber 0:f615d151a72c 53 public:
Michielber 0:f615d151a72c 54 /**
Michielber 0:f615d151a72c 55 Instantiates the NTP client
Michielber 0:f615d151a72c 56 */
Michielber 0:f615d151a72c 57 NTPClient();
Michielber 0:f615d151a72c 58 virtual ~NTPClient();
Michielber 0:f615d151a72c 59
Michielber 0:f615d151a72c 60 //High level setup functions
Michielber 0:f615d151a72c 61
Michielber 0:f615d151a72c 62 ///Gets current time (blocking)
Michielber 0:f615d151a72c 63 /**
Michielber 0:f615d151a72c 64 Updates the time using the server host
Michielber 0:f615d151a72c 65 Blocks until completion
Michielber 0:f615d151a72c 66 @param host : NTP server
Michielber 0:f615d151a72c 67 */
Michielber 0:f615d151a72c 68 NTPResult setTime(const Host& host); //Blocking
Michielber 0:f615d151a72c 69
Michielber 0:f615d151a72c 70 ///Gets current time (non-blocking)
Michielber 0:f615d151a72c 71 /**
Michielber 0:f615d151a72c 72 Updates the time using the server host
Michielber 0:f615d151a72c 73 The function returns immediately and calls the callback on completion or error
Michielber 0:f615d151a72c 74 @param host : NTP server
Michielber 0:f615d151a72c 75 @param pMethod : callback function
Michielber 0:f615d151a72c 76 */
Michielber 0:f615d151a72c 77 NTPResult setTime(const Host& host, void (*pMethod)(NTPResult)); //Non blocking
Michielber 0:f615d151a72c 78
Michielber 0:f615d151a72c 79 ///Gets current time (non-blocking)
Michielber 0:f615d151a72c 80 /**
Michielber 0:f615d151a72c 81 Updates the time
Michielber 0:f615d151a72c 82 @param host : NTP server
Michielber 0:f615d151a72c 83 @param pItem : instance of class on which to execute the callback method
Michielber 0:f615d151a72c 84 @param pMethod : callback method
Michielber 0:f615d151a72c 85 The function returns immediately and calls the callback on completion or error
Michielber 0:f615d151a72c 86 */
Michielber 0:f615d151a72c 87 template<class T>
Michielber 0:f615d151a72c 88 NTPResult setTime(const Host& host, T* pItem, void (T::*pMethod)(NTPResult)) //Non blocking
Michielber 0:f615d151a72c 89 {
Michielber 0:f615d151a72c 90 setOnResult(pItem, pMethod);
Michielber 0:f615d151a72c 91 doSetTime(host);
Michielber 0:f615d151a72c 92 return NTP_PROCESSING;
Michielber 0:f615d151a72c 93 }
Michielber 0:f615d151a72c 94
Michielber 0:f615d151a72c 95 ///Gets current time (non-blocking)
Michielber 0:f615d151a72c 96 /**
Michielber 0:f615d151a72c 97 Updates the time using the server host
Michielber 0:f615d151a72c 98 The function returns immediately and calls the previously set callback on completion or error
Michielber 0:f615d151a72c 99 @param host : NTP server
Michielber 0:f615d151a72c 100 */
Michielber 0:f615d151a72c 101 void doSetTime(const Host& host);
Michielber 0:f615d151a72c 102
Michielber 0:f615d151a72c 103 ///Setups the result callback
Michielber 0:f615d151a72c 104 /**
Michielber 0:f615d151a72c 105 @param pMethod : callback function
Michielber 0:f615d151a72c 106 */
Michielber 0:f615d151a72c 107 void setOnResult( void (*pMethod)(NTPResult) );
Michielber 0:f615d151a72c 108
Michielber 0:f615d151a72c 109 ///Setups the result callback
Michielber 0:f615d151a72c 110 /**
Michielber 0:f615d151a72c 111 @param pItem : instance of class on which to execute the callback method
Michielber 0:f615d151a72c 112 @param pMethod : callback method
Michielber 0:f615d151a72c 113 */
Michielber 0:f615d151a72c 114 class CDummy;
Michielber 0:f615d151a72c 115 template<class T>
Michielber 0:f615d151a72c 116 void setOnResult( T* pItem, void (T::*pMethod)(NTPResult) )
Michielber 0:f615d151a72c 117 {
Michielber 0:f615d151a72c 118 m_pCbItem = (CDummy*) pItem;
Michielber 0:f615d151a72c 119 m_pCbMeth = (void (CDummy::*)(NTPResult)) pMethod;
Michielber 0:f615d151a72c 120 }
Michielber 0:f615d151a72c 121
Michielber 0:f615d151a72c 122 void close();
Michielber 0:f615d151a72c 123
Michielber 0:f615d151a72c 124 protected:
Michielber 0:f615d151a72c 125 virtual void poll(); //Called by NetServices
Michielber 0:f615d151a72c 126
Michielber 0:f615d151a72c 127 private:
Michielber 0:f615d151a72c 128 void init();
Michielber 0:f615d151a72c 129 void open();
Michielber 0:f615d151a72c 130
Michielber 0:f615d151a72c 131 __packed struct NTPPacket //See RFC 4330 for Simple NTP
Michielber 0:f615d151a72c 132 {
Michielber 0:f615d151a72c 133 //WARN: We are in LE! Network is BE!
Michielber 0:f615d151a72c 134 //LSb first
Michielber 0:f615d151a72c 135 unsigned mode : 3;
Michielber 0:f615d151a72c 136 unsigned vn : 3;
Michielber 0:f615d151a72c 137 unsigned li : 2;
Michielber 0:f615d151a72c 138
Michielber 0:f615d151a72c 139 uint8_t stratum;
Michielber 0:f615d151a72c 140 uint8_t poll;
Michielber 0:f615d151a72c 141 uint8_t precision;
Michielber 0:f615d151a72c 142 //32 bits header
Michielber 0:f615d151a72c 143
Michielber 0:f615d151a72c 144 uint32_t rootDelay;
Michielber 0:f615d151a72c 145 uint32_t rootDispersion;
Michielber 0:f615d151a72c 146 uint32_t refId;
Michielber 0:f615d151a72c 147
Michielber 0:f615d151a72c 148 uint32_t refTm_s;
Michielber 0:f615d151a72c 149 uint32_t refTm_f;
Michielber 0:f615d151a72c 150 uint32_t origTm_s;
Michielber 0:f615d151a72c 151 uint32_t origTm_f;
Michielber 0:f615d151a72c 152 uint32_t rxTm_s;
Michielber 0:f615d151a72c 153 uint32_t rxTm_f;
Michielber 0:f615d151a72c 154 uint32_t txTm_s;
Michielber 0:f615d151a72c 155 uint32_t txTm_f;
Michielber 0:f615d151a72c 156 };
Michielber 0:f615d151a72c 157
Michielber 0:f615d151a72c 158 void process(); //Main state-machine
Michielber 0:f615d151a72c 159
Michielber 0:f615d151a72c 160 void setTimeout(int ms);
Michielber 0:f615d151a72c 161 void resetTimeout();
Michielber 0:f615d151a72c 162
Michielber 0:f615d151a72c 163 void onTimeout(); //Connection has timed out
Michielber 0:f615d151a72c 164 void onDNSReply(DNSReply r);
Michielber 0:f615d151a72c 165 void onUDPSocketEvent(UDPSocketEvent e);
Michielber 0:f615d151a72c 166 void onResult(NTPResult r); //Called when exchange completed or on failure
Michielber 0:f615d151a72c 167
Michielber 0:f615d151a72c 168 NTPResult blockingProcess(); //Called in blocking mode, calls Net::poll() until return code is available
Michielber 0:f615d151a72c 169
Michielber 0:f615d151a72c 170 UDPSocket* m_pUDPSocket;
Michielber 0:f615d151a72c 171
Michielber 0:f615d151a72c 172 enum NTPStep
Michielber 0:f615d151a72c 173 {
Michielber 0:f615d151a72c 174 NTP_PING,
Michielber 0:f615d151a72c 175 NTP_PONG
Michielber 0:f615d151a72c 176 };
Michielber 0:f615d151a72c 177
Michielber 0:f615d151a72c 178 NTPStep m_state;
Michielber 0:f615d151a72c 179
Michielber 0:f615d151a72c 180 NTPPacket m_pkt;
Michielber 0:f615d151a72c 181
Michielber 0:f615d151a72c 182 CDummy* m_pCbItem;
Michielber 0:f615d151a72c 183 void (CDummy::*m_pCbMeth)(NTPResult);
Michielber 0:f615d151a72c 184
Michielber 0:f615d151a72c 185 void (*m_pCb)(NTPResult);
Michielber 0:f615d151a72c 186
Michielber 0:f615d151a72c 187 Timer m_watchdog;
Michielber 0:f615d151a72c 188 int m_timeout;
Michielber 0:f615d151a72c 189
Michielber 0:f615d151a72c 190 bool m_closed;
Michielber 0:f615d151a72c 191
Michielber 0:f615d151a72c 192 Host m_host;
Michielber 0:f615d151a72c 193
Michielber 0:f615d151a72c 194 DNSRequest* m_pDnsReq;
Michielber 0:f615d151a72c 195
Michielber 0:f615d151a72c 196 NTPResult m_blockingResult; //Result if blocking mode
Michielber 0:f615d151a72c 197
Michielber 0:f615d151a72c 198 };
Michielber 0:f615d151a72c 199
Michielber 0:f615d151a72c 200 #endif