Trial code integration web page update with analogue data and ntp support

Dependencies:   NTPClient_NetServices mbed

Committer:
pmr1
Date:
Fri Aug 06 17:57:45 2010 +0000
Revision:
0:8cc2035bebfc

        

Who changed what in which revision?

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