Geiger counter http://geigermaps.jp/Create/Tutorials/mbed_geiger/ja

Dependencies:   mbed

Committer:
okini3939
Date:
Fri Apr 15 16:51:30 2011 +0000
Revision:
0:5b2e60110d9b

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okini3939 0:5b2e60110d9b 1
okini3939 0:5b2e60110d9b 2 /*
okini3939 0:5b2e60110d9b 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
okini3939 0:5b2e60110d9b 4
okini3939 0:5b2e60110d9b 5 Permission is hereby granted, free of charge, to any person obtaining a copy
okini3939 0:5b2e60110d9b 6 of this software and associated documentation files (the "Software"), to deal
okini3939 0:5b2e60110d9b 7 in the Software without restriction, including without limitation the rights
okini3939 0:5b2e60110d9b 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
okini3939 0:5b2e60110d9b 9 copies of the Software, and to permit persons to whom the Software is
okini3939 0:5b2e60110d9b 10 furnished to do so, subject to the following conditions:
okini3939 0:5b2e60110d9b 11
okini3939 0:5b2e60110d9b 12 The above copyright notice and this permission notice shall be included in
okini3939 0:5b2e60110d9b 13 all copies or substantial portions of the Software.
okini3939 0:5b2e60110d9b 14
okini3939 0:5b2e60110d9b 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
okini3939 0:5b2e60110d9b 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
okini3939 0:5b2e60110d9b 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
okini3939 0:5b2e60110d9b 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
okini3939 0:5b2e60110d9b 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
okini3939 0:5b2e60110d9b 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
okini3939 0:5b2e60110d9b 21 THE SOFTWARE.
okini3939 0:5b2e60110d9b 22 */
okini3939 0:5b2e60110d9b 23
okini3939 0:5b2e60110d9b 24 /** \file
okini3939 0:5b2e60110d9b 25 HTTP Client header file
okini3939 0:5b2e60110d9b 26 */
okini3939 0:5b2e60110d9b 27
okini3939 0:5b2e60110d9b 28 #ifndef HTTP_CLIENT_H
okini3939 0:5b2e60110d9b 29 #define HTTP_CLIENT_H
okini3939 0:5b2e60110d9b 30
okini3939 0:5b2e60110d9b 31 class HTTPData;
okini3939 0:5b2e60110d9b 32
okini3939 0:5b2e60110d9b 33 #include "core/net.h"
okini3939 0:5b2e60110d9b 34 #include "api/TCPSocket.h"
okini3939 0:5b2e60110d9b 35 #include "api/DNSRequest.h"
okini3939 0:5b2e60110d9b 36 #include "HTTPData.h"
okini3939 0:5b2e60110d9b 37 #include "mbed.h"
okini3939 0:5b2e60110d9b 38
okini3939 0:5b2e60110d9b 39 #include <string>
okini3939 0:5b2e60110d9b 40 using std::string;
okini3939 0:5b2e60110d9b 41
okini3939 0:5b2e60110d9b 42 #include <map>
okini3939 0:5b2e60110d9b 43 using std::map;
okini3939 0:5b2e60110d9b 44
okini3939 0:5b2e60110d9b 45 ///HTTP client results
okini3939 0:5b2e60110d9b 46 enum HTTPResult
okini3939 0:5b2e60110d9b 47 {
okini3939 0:5b2e60110d9b 48 HTTP_OK, ///<Success
okini3939 0:5b2e60110d9b 49 HTTP_PROCESSING, ///<Processing
okini3939 0:5b2e60110d9b 50 HTTP_PARSE, ///<URI Parse error
okini3939 0:5b2e60110d9b 51 HTTP_DNS, ///<Could not resolve name
okini3939 0:5b2e60110d9b 52 HTTP_PRTCL, ///<Protocol error
okini3939 0:5b2e60110d9b 53 HTTP_NOTFOUND, ///<HTTP 404 Error
okini3939 0:5b2e60110d9b 54 HTTP_REFUSED, ///<HTTP 403 Error
okini3939 0:5b2e60110d9b 55 HTTP_ERROR, ///<HTTP xxx error
okini3939 0:5b2e60110d9b 56 HTTP_TIMEOUT, ///<Connection timeout
okini3939 0:5b2e60110d9b 57 HTTP_CONN ///<Connection error
okini3939 0:5b2e60110d9b 58 };
okini3939 0:5b2e60110d9b 59
okini3939 0:5b2e60110d9b 60 #include "core/netservice.h"
okini3939 0:5b2e60110d9b 61
okini3939 0:5b2e60110d9b 62 ///A simple HTTP Client
okini3939 0:5b2e60110d9b 63 /**
okini3939 0:5b2e60110d9b 64 The HTTPClient is composed of:
okini3939 0:5b2e60110d9b 65 - The actual client (HTTPClient)
okini3939 0:5b2e60110d9b 66 - Classes that act as a data repository, each of which deriving from the HTTPData class (HTTPText for short text content, HTTPFile for file I/O, HTTPMap for key/value pairs, and HTTPStream for streaming purposes)
okini3939 0:5b2e60110d9b 67 */
okini3939 0:5b2e60110d9b 68 class HTTPClient : protected NetService
okini3939 0:5b2e60110d9b 69 {
okini3939 0:5b2e60110d9b 70 public:
okini3939 0:5b2e60110d9b 71 ///Instantiates the HTTP client
okini3939 0:5b2e60110d9b 72 HTTPClient();
okini3939 0:5b2e60110d9b 73 virtual ~HTTPClient();
okini3939 0:5b2e60110d9b 74
okini3939 0:5b2e60110d9b 75 ///Provides a basic authentification feature (Base64 encoded username and password)
okini3939 0:5b2e60110d9b 76 void basicAuth(const char* user, const char* password); //Basic Authentification
okini3939 0:5b2e60110d9b 77
okini3939 0:5b2e60110d9b 78 //High Level setup functions
okini3939 0:5b2e60110d9b 79 ///Executes a GET Request (blocking)
okini3939 0:5b2e60110d9b 80 /**
okini3939 0:5b2e60110d9b 81 Executes a GET request on the URI uri
okini3939 0:5b2e60110d9b 82 @param uri : URI on which to execute the request
okini3939 0:5b2e60110d9b 83 @param pDataIn : pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
okini3939 0:5b2e60110d9b 84 Blocks until completion
okini3939 0:5b2e60110d9b 85 */
okini3939 0:5b2e60110d9b 86 HTTPResult get(const char* uri, HTTPData* pDataIn); //Blocking
okini3939 0:5b2e60110d9b 87
okini3939 0:5b2e60110d9b 88 ///Executes a GET Request (non blocking)
okini3939 0:5b2e60110d9b 89 /**
okini3939 0:5b2e60110d9b 90 Executes a GET request on the URI uri
okini3939 0:5b2e60110d9b 91 @param uri : URI on which to execute the request
okini3939 0:5b2e60110d9b 92 @param pDataIn : pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
okini3939 0:5b2e60110d9b 93 @param pMethod : callback function
okini3939 0:5b2e60110d9b 94 The function returns immediately and calls the callback on completion or error
okini3939 0:5b2e60110d9b 95 */
okini3939 0:5b2e60110d9b 96 HTTPResult get(const char* uri, HTTPData* pDataIn, void (*pMethod)(HTTPResult)); //Non blocking
okini3939 0:5b2e60110d9b 97
okini3939 0:5b2e60110d9b 98 ///Executes a GET Request (non blocking)
okini3939 0:5b2e60110d9b 99 /**
okini3939 0:5b2e60110d9b 100 Executes a GET request on the URI uri
okini3939 0:5b2e60110d9b 101 @param uri : URI on which to execute the request
okini3939 0:5b2e60110d9b 102 @param pDataIn : pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
okini3939 0:5b2e60110d9b 103 @param pItem : instance of class on which to execute the callback method
okini3939 0:5b2e60110d9b 104 @param pMethod : callback method
okini3939 0:5b2e60110d9b 105 The function returns immediately and calls the callback on completion or error
okini3939 0:5b2e60110d9b 106 */
okini3939 0:5b2e60110d9b 107 template<class T>
okini3939 0:5b2e60110d9b 108 HTTPResult get(const char* uri, HTTPData* pDataIn, T* pItem, void (T::*pMethod)(HTTPResult)) //Non blocking
okini3939 0:5b2e60110d9b 109 {
okini3939 0:5b2e60110d9b 110 setOnResult(pItem, pMethod);
okini3939 0:5b2e60110d9b 111 doGet(uri, pDataIn);
okini3939 0:5b2e60110d9b 112 return HTTP_PROCESSING;
okini3939 0:5b2e60110d9b 113 }
okini3939 0:5b2e60110d9b 114
okini3939 0:5b2e60110d9b 115 ///Executes a POST Request (blocking)
okini3939 0:5b2e60110d9b 116 /**
okini3939 0:5b2e60110d9b 117 Executes a POST request on the URI uri
okini3939 0:5b2e60110d9b 118 @param uri : URI on which to execute the request
okini3939 0:5b2e60110d9b 119 @param dataOut : a HTTPData instance that contains the data that will be posted
okini3939 0:5b2e60110d9b 120 @param pDataIn : pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
okini3939 0:5b2e60110d9b 121 Blocks until completion
okini3939 0:5b2e60110d9b 122 */
okini3939 0:5b2e60110d9b 123 HTTPResult post(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn); //Blocking
okini3939 0:5b2e60110d9b 124
okini3939 0:5b2e60110d9b 125 ///Executes a POST Request (non blocking)
okini3939 0:5b2e60110d9b 126 /**
okini3939 0:5b2e60110d9b 127 Executes a POST request on the URI uri
okini3939 0:5b2e60110d9b 128 @param uri : URI on which to execute the request
okini3939 0:5b2e60110d9b 129 @param dataOut : a HTTPData instance that contains the data that will be posted
okini3939 0:5b2e60110d9b 130 @param pDataIn : pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
okini3939 0:5b2e60110d9b 131 @param pMethod : callback function
okini3939 0:5b2e60110d9b 132 The function returns immediately and calls the callback on completion or error
okini3939 0:5b2e60110d9b 133 */
okini3939 0:5b2e60110d9b 134 HTTPResult post(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn, void (*pMethod)(HTTPResult)); //Non blocking
okini3939 0:5b2e60110d9b 135
okini3939 0:5b2e60110d9b 136 ///Executes a POST Request (non blocking)
okini3939 0:5b2e60110d9b 137 /**
okini3939 0:5b2e60110d9b 138 Executes a POST request on the URI uri
okini3939 0:5b2e60110d9b 139 @param uri : URI on which to execute the request
okini3939 0:5b2e60110d9b 140 @param dataOut : a HTTPData instance that contains the data that will be posted
okini3939 0:5b2e60110d9b 141 @param pDataIn : pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
okini3939 0:5b2e60110d9b 142 @param pItem : instance of class on which to execute the callback method
okini3939 0:5b2e60110d9b 143 @param pMethod : callback method
okini3939 0:5b2e60110d9b 144 The function returns immediately and calls the callback on completion or error
okini3939 0:5b2e60110d9b 145 */
okini3939 0:5b2e60110d9b 146 template<class T>
okini3939 0:5b2e60110d9b 147 HTTPResult post(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn, T* pItem, void (T::*pMethod)(HTTPResult)) //Non blocking
okini3939 0:5b2e60110d9b 148 {
okini3939 0:5b2e60110d9b 149 setOnResult(pItem, pMethod);
okini3939 0:5b2e60110d9b 150 doPost(uri, dataOut, pDataIn);
okini3939 0:5b2e60110d9b 151 return HTTP_PROCESSING;
okini3939 0:5b2e60110d9b 152 }
okini3939 0:5b2e60110d9b 153
okini3939 0:5b2e60110d9b 154 ///Executes a GET Request (non blocking)
okini3939 0:5b2e60110d9b 155 /**
okini3939 0:5b2e60110d9b 156 Executes a GET request on the URI uri
okini3939 0:5b2e60110d9b 157 @param uri : URI on which to execute the request
okini3939 0:5b2e60110d9b 158 @param pDataIn : pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
okini3939 0:5b2e60110d9b 159 The function returns immediately and calls the previously set callback on completion or error
okini3939 0:5b2e60110d9b 160 */
okini3939 0:5b2e60110d9b 161 void doGet(const char* uri, HTTPData* pDataIn);
okini3939 0:5b2e60110d9b 162
okini3939 0:5b2e60110d9b 163 ///Executes a POST Request (non blocking)
okini3939 0:5b2e60110d9b 164 /**
okini3939 0:5b2e60110d9b 165 Executes a POST request on the URI uri
okini3939 0:5b2e60110d9b 166 @param uri : URI on which to execute the request
okini3939 0:5b2e60110d9b 167 @param dataOut : a HTTPData instance that contains the data that will be posted
okini3939 0:5b2e60110d9b 168 @param pDataIn : pointer to an HTTPData instance that will collect the data returned by the request, can be NULL
okini3939 0:5b2e60110d9b 169 @param pMethod : callback function
okini3939 0:5b2e60110d9b 170 The function returns immediately and calls the previously set callback on completion or error
okini3939 0:5b2e60110d9b 171 */
okini3939 0:5b2e60110d9b 172 void doPost(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn);
okini3939 0:5b2e60110d9b 173
okini3939 0:5b2e60110d9b 174 ///Setups the result callback
okini3939 0:5b2e60110d9b 175 /**
okini3939 0:5b2e60110d9b 176 @param pMethod : callback function
okini3939 0:5b2e60110d9b 177 */
okini3939 0:5b2e60110d9b 178 void setOnResult( void (*pMethod)(HTTPResult) );
okini3939 0:5b2e60110d9b 179
okini3939 0:5b2e60110d9b 180 ///Setups the result callback
okini3939 0:5b2e60110d9b 181 /**
okini3939 0:5b2e60110d9b 182 @param pItem : instance of class on which to execute the callback method
okini3939 0:5b2e60110d9b 183 @param pMethod : callback method
okini3939 0:5b2e60110d9b 184 */
okini3939 0:5b2e60110d9b 185 class CDummy;
okini3939 0:5b2e60110d9b 186 template<class T>
okini3939 0:5b2e60110d9b 187 void setOnResult( T* pItem, void (T::*pMethod)(HTTPResult) )
okini3939 0:5b2e60110d9b 188 {
okini3939 0:5b2e60110d9b 189 m_pCb = NULL;
okini3939 0:5b2e60110d9b 190 m_pCbItem = (CDummy*) pItem;
okini3939 0:5b2e60110d9b 191 m_pCbMeth = (void (CDummy::*)(HTTPResult)) pMethod;
okini3939 0:5b2e60110d9b 192 }
okini3939 0:5b2e60110d9b 193
okini3939 0:5b2e60110d9b 194 ///Setups timeout
okini3939 0:5b2e60110d9b 195 /**
okini3939 0:5b2e60110d9b 196 @param ms : time of connection inactivity in ms after which the request should timeout
okini3939 0:5b2e60110d9b 197 */
okini3939 0:5b2e60110d9b 198 void setTimeout(int ms);
okini3939 0:5b2e60110d9b 199
okini3939 0:5b2e60110d9b 200 virtual void poll(); //Called by NetServices
okini3939 0:5b2e60110d9b 201
okini3939 0:5b2e60110d9b 202 ///Gets last request's HTTP response code
okini3939 0:5b2e60110d9b 203 /**
okini3939 0:5b2e60110d9b 204 @return The HTTP response code of the last request
okini3939 0:5b2e60110d9b 205 */
okini3939 0:5b2e60110d9b 206 int getHTTPResponseCode();
okini3939 0:5b2e60110d9b 207
okini3939 0:5b2e60110d9b 208 ///Sets a specific request header
okini3939 0:5b2e60110d9b 209 void setRequestHeader(const string& header, const string& value);
okini3939 0:5b2e60110d9b 210
okini3939 0:5b2e60110d9b 211 ///Gets a response header
okini3939 0:5b2e60110d9b 212 string& getResponseHeader(const string& header);
okini3939 0:5b2e60110d9b 213
okini3939 0:5b2e60110d9b 214 ///Clears request headers
okini3939 0:5b2e60110d9b 215 void resetRequestHeaders();
okini3939 0:5b2e60110d9b 216
okini3939 0:5b2e60110d9b 217 protected:
okini3939 0:5b2e60110d9b 218 void resetTimeout();
okini3939 0:5b2e60110d9b 219
okini3939 0:5b2e60110d9b 220 void init();
okini3939 0:5b2e60110d9b 221 void close();
okini3939 0:5b2e60110d9b 222
okini3939 0:5b2e60110d9b 223 void setup(const char* uri, HTTPData* pDataOut, HTTPData* pDataIn); //Setup request, make DNS Req if necessary
okini3939 0:5b2e60110d9b 224 void connect(); //Start Connection
okini3939 0:5b2e60110d9b 225
okini3939 0:5b2e60110d9b 226 int tryRead(); //Read data and try to feed output
okini3939 0:5b2e60110d9b 227 void readData(); //Data has been read
okini3939 0:5b2e60110d9b 228 void writeData(); //Data has been written & buf is free
okini3939 0:5b2e60110d9b 229
okini3939 0:5b2e60110d9b 230 void onTCPSocketEvent(TCPSocketEvent e);
okini3939 0:5b2e60110d9b 231 void onDNSReply(DNSReply r);
okini3939 0:5b2e60110d9b 232 void onResult(HTTPResult r); //Called when exchange completed or on failure
okini3939 0:5b2e60110d9b 233 void onTimeout(); //Connection has timed out
okini3939 0:5b2e60110d9b 234
okini3939 0:5b2e60110d9b 235 private:
okini3939 0:5b2e60110d9b 236 HTTPResult blockingProcess(); //Called in blocking mode, calls Net::poll() until return code is available
okini3939 0:5b2e60110d9b 237
okini3939 0:5b2e60110d9b 238 bool readHeaders(); //Called first when receiving data
okini3939 0:5b2e60110d9b 239 bool writeHeaders(); //Called to create req
okini3939 0:5b2e60110d9b 240 int readLine(char* str, int maxLen, bool* pIncomplete = NULL);
okini3939 0:5b2e60110d9b 241
okini3939 0:5b2e60110d9b 242 enum HTTP_METH
okini3939 0:5b2e60110d9b 243 {
okini3939 0:5b2e60110d9b 244 HTTP_GET,
okini3939 0:5b2e60110d9b 245 HTTP_POST,
okini3939 0:5b2e60110d9b 246 HTTP_HEAD
okini3939 0:5b2e60110d9b 247 };
okini3939 0:5b2e60110d9b 248
okini3939 0:5b2e60110d9b 249 HTTP_METH m_meth;
okini3939 0:5b2e60110d9b 250
okini3939 0:5b2e60110d9b 251 CDummy* m_pCbItem;
okini3939 0:5b2e60110d9b 252 void (CDummy::*m_pCbMeth)(HTTPResult);
okini3939 0:5b2e60110d9b 253
okini3939 0:5b2e60110d9b 254 void (*m_pCb)(HTTPResult);
okini3939 0:5b2e60110d9b 255
okini3939 0:5b2e60110d9b 256 TCPSocket* m_pTCPSocket;
okini3939 0:5b2e60110d9b 257 map<string, string> m_reqHeaders;
okini3939 0:5b2e60110d9b 258 map<string, string> m_respHeaders;
okini3939 0:5b2e60110d9b 259
okini3939 0:5b2e60110d9b 260 Timer m_watchdog;
okini3939 0:5b2e60110d9b 261 int m_timeout;
okini3939 0:5b2e60110d9b 262
okini3939 0:5b2e60110d9b 263 DNSRequest* m_pDnsReq;
okini3939 0:5b2e60110d9b 264
okini3939 0:5b2e60110d9b 265 Host m_server;
okini3939 0:5b2e60110d9b 266 string m_path;
okini3939 0:5b2e60110d9b 267
okini3939 0:5b2e60110d9b 268 bool m_closed;
okini3939 0:5b2e60110d9b 269
okini3939 0:5b2e60110d9b 270 enum HTTPStep
okini3939 0:5b2e60110d9b 271 {
okini3939 0:5b2e60110d9b 272 // HTTP_INIT,
okini3939 0:5b2e60110d9b 273 HTTP_WRITE_HEADERS,
okini3939 0:5b2e60110d9b 274 HTTP_WRITE_DATA,
okini3939 0:5b2e60110d9b 275 HTTP_READ_HEADERS,
okini3939 0:5b2e60110d9b 276 HTTP_READ_DATA,
okini3939 0:5b2e60110d9b 277 HTTP_READ_DATA_INCOMPLETE,
okini3939 0:5b2e60110d9b 278 HTTP_DONE,
okini3939 0:5b2e60110d9b 279 HTTP_CLOSED
okini3939 0:5b2e60110d9b 280 };
okini3939 0:5b2e60110d9b 281
okini3939 0:5b2e60110d9b 282 HTTPStep m_state;
okini3939 0:5b2e60110d9b 283
okini3939 0:5b2e60110d9b 284 HTTPData* m_pDataOut;
okini3939 0:5b2e60110d9b 285 HTTPData* m_pDataIn;
okini3939 0:5b2e60110d9b 286
okini3939 0:5b2e60110d9b 287 bool m_dataChunked; //Data is encoded as chunks
okini3939 0:5b2e60110d9b 288 int m_dataPos; //Position in data
okini3939 0:5b2e60110d9b 289 int m_dataLen; //Data length
okini3939 0:5b2e60110d9b 290 char* m_buf;
okini3939 0:5b2e60110d9b 291 char* m_pBufRemaining; //Remaining
okini3939 0:5b2e60110d9b 292 int m_bufRemainingLen; //Data length in m_pBufRemaining
okini3939 0:5b2e60110d9b 293
okini3939 0:5b2e60110d9b 294 int m_httpResponseCode;
okini3939 0:5b2e60110d9b 295
okini3939 0:5b2e60110d9b 296 HTTPResult m_blockingResult; //Result if blocking mode
okini3939 0:5b2e60110d9b 297
okini3939 0:5b2e60110d9b 298 };
okini3939 0:5b2e60110d9b 299
okini3939 0:5b2e60110d9b 300 //Including data containers here for more convenience
okini3939 0:5b2e60110d9b 301 #include "data/HTTPFile.h"
okini3939 0:5b2e60110d9b 302 #include "data/HTTPStream.h"
okini3939 0:5b2e60110d9b 303 #include "data/HTTPText.h"
okini3939 0:5b2e60110d9b 304 #include "data/HTTPMap.h"
okini3939 0:5b2e60110d9b 305
okini3939 0:5b2e60110d9b 306 #endif