I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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