Experimental HTTPClient with proxy support

Committer:
igorsk
Date:
Wed Jun 29 16:01:58 2011 +0000
Revision:
0:b56b6a05cad4

        

Who changed what in which revision?

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