Trial code integration web page update with analogue data and ntp support

Dependencies:   NTPClient_NetServices mbed

Committer:
pmr1
Date:
Fri Aug 06 17:57:45 2010 +0000
Revision:
0:8cc2035bebfc

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pmr1 0:8cc2035bebfc 1
pmr1 0:8cc2035bebfc 2 /*
pmr1 0:8cc2035bebfc 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
pmr1 0:8cc2035bebfc 4
pmr1 0:8cc2035bebfc 5 Permission is hereby granted, free of charge, to any person obtaining a copy
pmr1 0:8cc2035bebfc 6 of this software and associated documentation files (the "Software"), to deal
pmr1 0:8cc2035bebfc 7 in the Software without restriction, including without limitation the rights
pmr1 0:8cc2035bebfc 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
pmr1 0:8cc2035bebfc 9 copies of the Software, and to permit persons to whom the Software is
pmr1 0:8cc2035bebfc 10 furnished to do so, subject to the following conditions:
pmr1 0:8cc2035bebfc 11
pmr1 0:8cc2035bebfc 12 The above copyright notice and this permission notice shall be included in
pmr1 0:8cc2035bebfc 13 all copies or substantial portions of the Software.
pmr1 0:8cc2035bebfc 14
pmr1 0:8cc2035bebfc 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
pmr1 0:8cc2035bebfc 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
pmr1 0:8cc2035bebfc 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
pmr1 0:8cc2035bebfc 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
pmr1 0:8cc2035bebfc 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
pmr1 0:8cc2035bebfc 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
pmr1 0:8cc2035bebfc 21 THE SOFTWARE.
pmr1 0:8cc2035bebfc 22 */
pmr1 0:8cc2035bebfc 23
pmr1 0:8cc2035bebfc 24 #ifndef HTTP_CLIENT_H
pmr1 0:8cc2035bebfc 25 #define HTTP_CLIENT_H
pmr1 0:8cc2035bebfc 26
pmr1 0:8cc2035bebfc 27 class HTTPData;
pmr1 0:8cc2035bebfc 28
pmr1 0:8cc2035bebfc 29 #include "if/net/net.h"
pmr1 0:8cc2035bebfc 30 #include "api/TCPSocket.h"
pmr1 0:8cc2035bebfc 31 #include "api/DNSRequest.h"
pmr1 0:8cc2035bebfc 32 #include "HTTPData.h"
pmr1 0:8cc2035bebfc 33 #include "mbed.h"
pmr1 0:8cc2035bebfc 34
pmr1 0:8cc2035bebfc 35 #include <string>
pmr1 0:8cc2035bebfc 36 using std::string;
pmr1 0:8cc2035bebfc 37
pmr1 0:8cc2035bebfc 38 #include <map>
pmr1 0:8cc2035bebfc 39 using std::map;
pmr1 0:8cc2035bebfc 40
pmr1 0:8cc2035bebfc 41 enum HTTPResult
pmr1 0:8cc2035bebfc 42 {
pmr1 0:8cc2035bebfc 43 HTTP_OK,
pmr1 0:8cc2035bebfc 44 HTTP_PROCESSING,
pmr1 0:8cc2035bebfc 45 HTTP_PARSE, //URI Parse error
pmr1 0:8cc2035bebfc 46 HTTP_DNS, //Could not resolve name
pmr1 0:8cc2035bebfc 47 HTTP_PRTCL, //Protocol error
pmr1 0:8cc2035bebfc 48 HTTP_NOTFOUND, //404 Error
pmr1 0:8cc2035bebfc 49 HTTP_REFUSED, //403 Error
pmr1 0:8cc2035bebfc 50 HTTP_ERROR, //xxx error
pmr1 0:8cc2035bebfc 51 HTTP_TIMEOUT, //Connection timeout
pmr1 0:8cc2035bebfc 52 HTTP_CONN //Connection error
pmr1 0:8cc2035bebfc 53 };
pmr1 0:8cc2035bebfc 54
pmr1 0:8cc2035bebfc 55
pmr1 0:8cc2035bebfc 56
pmr1 0:8cc2035bebfc 57 class HTTPClient : protected NetService
pmr1 0:8cc2035bebfc 58 {
pmr1 0:8cc2035bebfc 59 public:
pmr1 0:8cc2035bebfc 60 HTTPClient();
pmr1 0:8cc2035bebfc 61 virtual ~HTTPClient();
pmr1 0:8cc2035bebfc 62
pmr1 0:8cc2035bebfc 63 void basicAuth(const char* user, const char* password); //Basic Authentification
pmr1 0:8cc2035bebfc 64
pmr1 0:8cc2035bebfc 65 //High Level setup functions
pmr1 0:8cc2035bebfc 66 HTTPResult get(const char* uri, HTTPData* pDataIn); //Blocking
pmr1 0:8cc2035bebfc 67 HTTPResult get(const char* uri, HTTPData* pDataIn, void (*pMethod)(HTTPResult)); //Non blocking
pmr1 0:8cc2035bebfc 68 template<class T>
pmr1 0:8cc2035bebfc 69 HTTPResult get(const char* uri, HTTPData* pDataIn, T* pItem, void (T::*pMethod)(HTTPResult)) //Non blocking
pmr1 0:8cc2035bebfc 70 {
pmr1 0:8cc2035bebfc 71 setOnResult(pItem, pMethod);
pmr1 0:8cc2035bebfc 72 doGet(uri, pDataIn);
pmr1 0:8cc2035bebfc 73 return HTTP_PROCESSING;
pmr1 0:8cc2035bebfc 74 }
pmr1 0:8cc2035bebfc 75
pmr1 0:8cc2035bebfc 76 HTTPResult post(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn); //Blocking
pmr1 0:8cc2035bebfc 77 HTTPResult post(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn, void (*pMethod)(HTTPResult)); //Non blocking
pmr1 0:8cc2035bebfc 78 template<class T>
pmr1 0:8cc2035bebfc 79 HTTPResult post(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn, T* pItem, void (T::*pMethod)(HTTPResult)) //Non blocking
pmr1 0:8cc2035bebfc 80 {
pmr1 0:8cc2035bebfc 81 setOnResult(pItem, pMethod);
pmr1 0:8cc2035bebfc 82 doPost(uri, dataOut, pDataIn);
pmr1 0:8cc2035bebfc 83 return HTTP_PROCESSING;
pmr1 0:8cc2035bebfc 84 }
pmr1 0:8cc2035bebfc 85
pmr1 0:8cc2035bebfc 86 void doGet(const char* uri, HTTPData* pDataIn);
pmr1 0:8cc2035bebfc 87 void doPost(const char* uri, const HTTPData& dataOut, HTTPData* pDataIn);
pmr1 0:8cc2035bebfc 88
pmr1 0:8cc2035bebfc 89 void setOnResult( void (*pMethod)(HTTPResult) );
pmr1 0:8cc2035bebfc 90 class CDummy;
pmr1 0:8cc2035bebfc 91 template<class T>
pmr1 0:8cc2035bebfc 92 //Linker bug : Must be defined here :(
pmr1 0:8cc2035bebfc 93 void setOnResult( T* pItem, void (T::*pMethod)(HTTPResult) )
pmr1 0:8cc2035bebfc 94 {
pmr1 0:8cc2035bebfc 95 m_pCb = NULL;
pmr1 0:8cc2035bebfc 96 m_pCbItem = (CDummy*) pItem;
pmr1 0:8cc2035bebfc 97 m_pCbMeth = (void (CDummy::*)(HTTPResult)) pMethod;
pmr1 0:8cc2035bebfc 98 }
pmr1 0:8cc2035bebfc 99
pmr1 0:8cc2035bebfc 100 void setTimeout(int ms);
pmr1 0:8cc2035bebfc 101
pmr1 0:8cc2035bebfc 102 virtual void poll(); //Called by NetServices
pmr1 0:8cc2035bebfc 103
pmr1 0:8cc2035bebfc 104 int getHTTPResponseCode();
pmr1 0:8cc2035bebfc 105 void setRequestHeader(const string& header, const string& value);
pmr1 0:8cc2035bebfc 106 string& getResponseHeader(const string& header);
pmr1 0:8cc2035bebfc 107 void resetRequestHeaders();
pmr1 0:8cc2035bebfc 108
pmr1 0:8cc2035bebfc 109 protected:
pmr1 0:8cc2035bebfc 110 void resetTimeout();
pmr1 0:8cc2035bebfc 111
pmr1 0:8cc2035bebfc 112 void init();
pmr1 0:8cc2035bebfc 113 void close();
pmr1 0:8cc2035bebfc 114
pmr1 0:8cc2035bebfc 115 void setup(const char* uri, HTTPData* pDataOut, HTTPData* pDataIn); //Setup request, make DNS Req if necessary
pmr1 0:8cc2035bebfc 116 void connect(); //Start Connection
pmr1 0:8cc2035bebfc 117
pmr1 0:8cc2035bebfc 118 int tryRead(); //Read data and try to feed output
pmr1 0:8cc2035bebfc 119 void readData(); //Data has been read
pmr1 0:8cc2035bebfc 120 void writeData(); //Data has been written & buf is free
pmr1 0:8cc2035bebfc 121
pmr1 0:8cc2035bebfc 122 void onTCPSocketEvent(TCPSocketEvent e);
pmr1 0:8cc2035bebfc 123 void onDNSReply(DNSReply r);
pmr1 0:8cc2035bebfc 124 void onResult(HTTPResult r); //Called when exchange completed or on failure
pmr1 0:8cc2035bebfc 125 void onTimeout(); //Connection has timed out
pmr1 0:8cc2035bebfc 126
pmr1 0:8cc2035bebfc 127 private:
pmr1 0:8cc2035bebfc 128 HTTPResult blockingProcess(); //Called in blocking mode, calls Net::poll() until return code is available
pmr1 0:8cc2035bebfc 129
pmr1 0:8cc2035bebfc 130 bool readHeaders(); //Called first when receiving data
pmr1 0:8cc2035bebfc 131 bool writeHeaders(); //Called to create req
pmr1 0:8cc2035bebfc 132 int readLine(char* str, int maxLen, bool* pIncomplete = NULL);
pmr1 0:8cc2035bebfc 133
pmr1 0:8cc2035bebfc 134 enum HTTP_METH
pmr1 0:8cc2035bebfc 135 {
pmr1 0:8cc2035bebfc 136 HTTP_GET,
pmr1 0:8cc2035bebfc 137 HTTP_POST,
pmr1 0:8cc2035bebfc 138 HTTP_HEAD
pmr1 0:8cc2035bebfc 139 };
pmr1 0:8cc2035bebfc 140
pmr1 0:8cc2035bebfc 141 HTTP_METH m_meth;
pmr1 0:8cc2035bebfc 142
pmr1 0:8cc2035bebfc 143 CDummy* m_pCbItem;
pmr1 0:8cc2035bebfc 144 void (CDummy::*m_pCbMeth)(HTTPResult);
pmr1 0:8cc2035bebfc 145
pmr1 0:8cc2035bebfc 146 void (*m_pCb)(HTTPResult);
pmr1 0:8cc2035bebfc 147
pmr1 0:8cc2035bebfc 148 TCPSocket* m_pTCPSocket;
pmr1 0:8cc2035bebfc 149 map<string, string> m_reqHeaders;
pmr1 0:8cc2035bebfc 150 map<string, string> m_respHeaders;
pmr1 0:8cc2035bebfc 151
pmr1 0:8cc2035bebfc 152 Timer m_watchdog;
pmr1 0:8cc2035bebfc 153 int m_timeout;
pmr1 0:8cc2035bebfc 154
pmr1 0:8cc2035bebfc 155 DNSRequest* m_pDnsReq;
pmr1 0:8cc2035bebfc 156
pmr1 0:8cc2035bebfc 157 Host m_server;
pmr1 0:8cc2035bebfc 158 string m_path;
pmr1 0:8cc2035bebfc 159
pmr1 0:8cc2035bebfc 160 bool m_closed;
pmr1 0:8cc2035bebfc 161
pmr1 0:8cc2035bebfc 162 enum HTTPStep
pmr1 0:8cc2035bebfc 163 {
pmr1 0:8cc2035bebfc 164 // HTTP_INIT,
pmr1 0:8cc2035bebfc 165 HTTP_WRITE_HEADERS,
pmr1 0:8cc2035bebfc 166 HTTP_WRITE_DATA,
pmr1 0:8cc2035bebfc 167 HTTP_READ_HEADERS,
pmr1 0:8cc2035bebfc 168 HTTP_READ_DATA,
pmr1 0:8cc2035bebfc 169 HTTP_READ_DATA_INCOMPLETE,
pmr1 0:8cc2035bebfc 170 HTTP_DONE,
pmr1 0:8cc2035bebfc 171 HTTP_CLOSED
pmr1 0:8cc2035bebfc 172 };
pmr1 0:8cc2035bebfc 173
pmr1 0:8cc2035bebfc 174 HTTPStep m_state;
pmr1 0:8cc2035bebfc 175
pmr1 0:8cc2035bebfc 176 HTTPData* m_pDataOut;
pmr1 0:8cc2035bebfc 177 HTTPData* m_pDataIn;
pmr1 0:8cc2035bebfc 178
pmr1 0:8cc2035bebfc 179 bool m_dataChunked; //Data is encoded as chunks
pmr1 0:8cc2035bebfc 180 int m_dataPos; //Position in data
pmr1 0:8cc2035bebfc 181 int m_dataLen; //Data length
pmr1 0:8cc2035bebfc 182 char* m_buf;
pmr1 0:8cc2035bebfc 183 char* m_pBufRemaining; //Remaining
pmr1 0:8cc2035bebfc 184 int m_bufRemainingLen; //Data length in m_pBufRemaining
pmr1 0:8cc2035bebfc 185
pmr1 0:8cc2035bebfc 186 int m_httpResponseCode;
pmr1 0:8cc2035bebfc 187
pmr1 0:8cc2035bebfc 188 HTTPResult m_blockingResult; //Result if blocking mode
pmr1 0:8cc2035bebfc 189
pmr1 0:8cc2035bebfc 190 };
pmr1 0:8cc2035bebfc 191
pmr1 0:8cc2035bebfc 192 //Including data containers here for more convenience
pmr1 0:8cc2035bebfc 193 #include "data/HTTPFile.h"
pmr1 0:8cc2035bebfc 194 #include "data/HTTPStream.h"
pmr1 0:8cc2035bebfc 195 #include "data/HTTPText.h"
pmr1 0:8cc2035bebfc 196 #include "data/HTTPMap.h"
pmr1 0:8cc2035bebfc 197
pmr1 0:8cc2035bebfc 198 #endif