SNMP agent attached to SPI slave

Dependencies:   mbed

Committer:
lorcansmith
Date:
Mon Aug 13 15:07:40 2012 +0000
Revision:
0:2a53a4c3238c
v1.1 release includes ioAlarm traps

Who changed what in which revision?

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