Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:41:36 2011 +0000
Revision:
0:8f5825f330b0
setDataLen hacked to 180bytes

Who changed what in which revision?

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