A sprinkler controller that takes HTTP command for zone and duration.

Dependencies:   EthernetNetIf mbed HTTPServer

Committer:
dminear
Date:
Wed Mar 09 03:44:43 2011 +0000
Revision:
1:91c2c52b4691
Parent:
0:810ec1936452
Fixed constant setting of time via NTP.

Who changed what in which revision?

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