modded version Dirk-Willem van Gulik's Bonjour/Zerconf library http://mbed.org/users/dirkx/code/Bonjour/

Dependents:   OSCtoCVConverter

Fork of Bonjour by Dirk-Willem van Gulik (NXP/mbed)

Committer:
casiotone401
Date:
Thu Oct 16 14:13:21 2014 +0000
Revision:
8:275256b5d807
Parent:
0:355018f44c9f
minor change

Who changed what in which revision?

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