EthernetNetIf Compatibility.

Dependents:   XBeeWiFi_SPI_example

Fork of NetServicesSource by Donatien Garnier

Committer:
donatien
Date:
Tue Jul 27 15:59:42 2010 +0000
Revision:
5:dd63a1e02b1b
Parent:
2:a4f97773c90f
Child:
6:b7dd7cde8ad2

        

Who changed what in which revision?

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