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