This short program illustrates how to use the DS130x_I2C library. My objective is to share the same RTC with Microchip 18F MCU.

Dependencies:   mbed DebugLibrary

Committer:
Yann
Date:
Wed Feb 09 13:57:49 2011 +0000
Revision:
0:f30e2135b0db
V0.0.0.1

Who changed what in which revision?

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