Integrating the ublox LISA C200 modem

Fork of SprintUSBModemHTTPClientTest by Donatien Garnier

Committer:
sam_grove
Date:
Tue Oct 08 00:08:22 2013 +0000
Revision:
21:3f45e53afe4f
Parent:
5:3f93dd1d4cb3
Added http client test. Return from post seems to be a bit wonky but haven't looked closely at this

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sam_grove 5:3f93dd1d4cb3 1 /* HTTPClient.h */
sam_grove 5:3f93dd1d4cb3 2 /* Copyright (C) 2012 mbed.org, MIT License
sam_grove 5:3f93dd1d4cb3 3 *
sam_grove 5:3f93dd1d4cb3 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
sam_grove 5:3f93dd1d4cb3 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
sam_grove 5:3f93dd1d4cb3 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
sam_grove 5:3f93dd1d4cb3 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
sam_grove 5:3f93dd1d4cb3 8 * furnished to do so, subject to the following conditions:
sam_grove 5:3f93dd1d4cb3 9 *
sam_grove 5:3f93dd1d4cb3 10 * The above copyright notice and this permission notice shall be included in all copies or
sam_grove 5:3f93dd1d4cb3 11 * substantial portions of the Software.
sam_grove 5:3f93dd1d4cb3 12 *
sam_grove 5:3f93dd1d4cb3 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
sam_grove 5:3f93dd1d4cb3 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
sam_grove 5:3f93dd1d4cb3 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
sam_grove 5:3f93dd1d4cb3 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
sam_grove 5:3f93dd1d4cb3 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
sam_grove 5:3f93dd1d4cb3 18 */
sam_grove 5:3f93dd1d4cb3 19
sam_grove 5:3f93dd1d4cb3 20 /** \file
sam_grove 5:3f93dd1d4cb3 21 HTTP Client header file
sam_grove 5:3f93dd1d4cb3 22 */
sam_grove 5:3f93dd1d4cb3 23
sam_grove 5:3f93dd1d4cb3 24 #ifndef HTTP_CLIENT_H
sam_grove 5:3f93dd1d4cb3 25 #define HTTP_CLIENT_H
sam_grove 5:3f93dd1d4cb3 26
sam_grove 5:3f93dd1d4cb3 27 #include "TCPSocketConnection.h"
sam_grove 5:3f93dd1d4cb3 28
sam_grove 5:3f93dd1d4cb3 29 #define HTTP_CLIENT_DEFAULT_TIMEOUT 15000
sam_grove 5:3f93dd1d4cb3 30
sam_grove 5:3f93dd1d4cb3 31 class HTTPData;
sam_grove 5:3f93dd1d4cb3 32
sam_grove 5:3f93dd1d4cb3 33 #include "IHTTPData.h"
sam_grove 5:3f93dd1d4cb3 34 #include "mbed.h"
sam_grove 5:3f93dd1d4cb3 35
sam_grove 5:3f93dd1d4cb3 36 ///HTTP client results
sam_grove 5:3f93dd1d4cb3 37 enum HTTPResult
sam_grove 5:3f93dd1d4cb3 38 {
sam_grove 5:3f93dd1d4cb3 39 HTTP_PROCESSING, ///<Processing
sam_grove 5:3f93dd1d4cb3 40 HTTP_PARSE, ///<url Parse error
sam_grove 5:3f93dd1d4cb3 41 HTTP_DNS, ///<Could not resolve name
sam_grove 5:3f93dd1d4cb3 42 HTTP_PRTCL, ///<Protocol error
sam_grove 5:3f93dd1d4cb3 43 HTTP_NOTFOUND, ///<HTTP 404 Error
sam_grove 5:3f93dd1d4cb3 44 HTTP_REFUSED, ///<HTTP 403 Error
sam_grove 5:3f93dd1d4cb3 45 HTTP_ERROR, ///<HTTP xxx error
sam_grove 5:3f93dd1d4cb3 46 HTTP_TIMEOUT, ///<Connection timeout
sam_grove 5:3f93dd1d4cb3 47 HTTP_CONN, ///<Connection error
sam_grove 5:3f93dd1d4cb3 48 HTTP_CLOSED, ///<Connection was closed by remote host
sam_grove 5:3f93dd1d4cb3 49 HTTP_OK = 0, ///<Success
sam_grove 5:3f93dd1d4cb3 50 };
sam_grove 5:3f93dd1d4cb3 51
sam_grove 5:3f93dd1d4cb3 52 /**A simple HTTP Client
sam_grove 5:3f93dd1d4cb3 53 The HTTPClient is composed of:
sam_grove 5:3f93dd1d4cb3 54 - The actual client (HTTPClient)
sam_grove 5:3f93dd1d4cb3 55 - 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)
sam_grove 5:3f93dd1d4cb3 56 */
sam_grove 5:3f93dd1d4cb3 57 class HTTPClient
sam_grove 5:3f93dd1d4cb3 58 {
sam_grove 5:3f93dd1d4cb3 59 public:
sam_grove 5:3f93dd1d4cb3 60 ///Instantiate the HTTP client
sam_grove 5:3f93dd1d4cb3 61 HTTPClient();
sam_grove 5:3f93dd1d4cb3 62 ~HTTPClient();
sam_grove 5:3f93dd1d4cb3 63
sam_grove 5:3f93dd1d4cb3 64 #if 0 //TODO add header handlers
sam_grove 5:3f93dd1d4cb3 65 /**
sam_grove 5:3f93dd1d4cb3 66 Provides a basic authentification feature (Base64 encoded username and password)
sam_grove 5:3f93dd1d4cb3 67 Pass two NULL pointers to switch back to no authentication
sam_grove 5:3f93dd1d4cb3 68 @param user username to use for authentication, must remain valid durlng the whole HTTP session
sam_grove 5:3f93dd1d4cb3 69 @param user password to use for authentication, must remain valid durlng the whole HTTP session
sam_grove 5:3f93dd1d4cb3 70 */
sam_grove 5:3f93dd1d4cb3 71 void basicAuth(const char* user, const char* password); //Basic Authentification
sam_grove 5:3f93dd1d4cb3 72 #endif
sam_grove 5:3f93dd1d4cb3 73
sam_grove 5:3f93dd1d4cb3 74 //High Level setup functions
sam_grove 5:3f93dd1d4cb3 75 /** Execute a GET request on the URL
sam_grove 5:3f93dd1d4cb3 76 Blocks until completion
sam_grove 5:3f93dd1d4cb3 77 @param url : url on which to execute the request
sam_grove 5:3f93dd1d4cb3 78 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
sam_grove 5:3f93dd1d4cb3 79 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
sam_grove 5:3f93dd1d4cb3 80 @return 0 on success, HTTP error (<0) on failure
sam_grove 5:3f93dd1d4cb3 81 */
sam_grove 5:3f93dd1d4cb3 82 HTTPResult get(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
sam_grove 5:3f93dd1d4cb3 83
sam_grove 5:3f93dd1d4cb3 84 /** Execute a GET request on the URL
sam_grove 5:3f93dd1d4cb3 85 Blocks until completion
sam_grove 5:3f93dd1d4cb3 86 This is a helper to directly get a piece of text from a HTTP result
sam_grove 5:3f93dd1d4cb3 87 @param url : url on which to execute the request
sam_grove 5:3f93dd1d4cb3 88 @param result : pointer to a char array in which the result will be stored
sam_grove 5:3f93dd1d4cb3 89 @param maxResultLen : length of the char array (including space for the NULL-terminating char)
sam_grove 5:3f93dd1d4cb3 90 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
sam_grove 5:3f93dd1d4cb3 91 @return 0 on success, HTTP error (<0) on failure
sam_grove 5:3f93dd1d4cb3 92 */
sam_grove 5:3f93dd1d4cb3 93 HTTPResult get(const char* url, char* result, size_t maxResultLen, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
sam_grove 5:3f93dd1d4cb3 94
sam_grove 5:3f93dd1d4cb3 95 /** Execute a POST request on the URL
sam_grove 5:3f93dd1d4cb3 96 Blocks until completion
sam_grove 5:3f93dd1d4cb3 97 @param url : url on which to execute the request
sam_grove 5:3f93dd1d4cb3 98 @param dataOut : a IHTTPDataOut instance that contains the data that will be posted
sam_grove 5:3f93dd1d4cb3 99 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
sam_grove 5:3f93dd1d4cb3 100 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
sam_grove 5:3f93dd1d4cb3 101 @return 0 on success, HTTP error (<0) on failure
sam_grove 5:3f93dd1d4cb3 102 */
sam_grove 5:3f93dd1d4cb3 103 HTTPResult post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
sam_grove 5:3f93dd1d4cb3 104
sam_grove 5:3f93dd1d4cb3 105 /** Execute a PUT request on the URL
sam_grove 5:3f93dd1d4cb3 106 Blocks until completion
sam_grove 5:3f93dd1d4cb3 107 @param url : url on which to execute the request
sam_grove 5:3f93dd1d4cb3 108 @param dataOut : a IHTTPDataOut instance that contains the data that will be put
sam_grove 5:3f93dd1d4cb3 109 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
sam_grove 5:3f93dd1d4cb3 110 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
sam_grove 5:3f93dd1d4cb3 111 @return 0 on success, HTTP error (<0) on failure
sam_grove 5:3f93dd1d4cb3 112 */
sam_grove 5:3f93dd1d4cb3 113 HTTPResult put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
sam_grove 5:3f93dd1d4cb3 114
sam_grove 5:3f93dd1d4cb3 115 /** Execute a DELETE request on the URL
sam_grove 5:3f93dd1d4cb3 116 Blocks until completion
sam_grove 5:3f93dd1d4cb3 117 @param url : url on which to execute the request
sam_grove 5:3f93dd1d4cb3 118 @param pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
sam_grove 5:3f93dd1d4cb3 119 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
sam_grove 5:3f93dd1d4cb3 120 @return 0 on success, HTTP error (<0) on failure
sam_grove 5:3f93dd1d4cb3 121 */
sam_grove 5:3f93dd1d4cb3 122 HTTPResult del(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
sam_grove 5:3f93dd1d4cb3 123
sam_grove 5:3f93dd1d4cb3 124 /** Get last request's HTTP response code
sam_grove 5:3f93dd1d4cb3 125 @return The HTTP response code of the last request
sam_grove 5:3f93dd1d4cb3 126 */
sam_grove 5:3f93dd1d4cb3 127 int getHTTPResponseCode();
sam_grove 5:3f93dd1d4cb3 128
sam_grove 5:3f93dd1d4cb3 129 private:
sam_grove 5:3f93dd1d4cb3 130 enum HTTP_METH
sam_grove 5:3f93dd1d4cb3 131 {
sam_grove 5:3f93dd1d4cb3 132 HTTP_GET,
sam_grove 5:3f93dd1d4cb3 133 HTTP_POST,
sam_grove 5:3f93dd1d4cb3 134 HTTP_PUT,
sam_grove 5:3f93dd1d4cb3 135 HTTP_DELETE,
sam_grove 5:3f93dd1d4cb3 136 HTTP_HEAD
sam_grove 5:3f93dd1d4cb3 137 };
sam_grove 5:3f93dd1d4cb3 138
sam_grove 5:3f93dd1d4cb3 139 HTTPResult connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout); //Execute request
sam_grove 5:3f93dd1d4cb3 140 HTTPResult recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen); //0 on success, err code on failure
sam_grove 5:3f93dd1d4cb3 141 HTTPResult send(char* buf, size_t len = 0); //0 on success, err code on failure
sam_grove 5:3f93dd1d4cb3 142 HTTPResult parseURL(const char* url, char* scheme, size_t maxSchemeLen, char* host, size_t maxHostLen, uint16_t* port, char* path, size_t maxPathLen); //Parse URL
sam_grove 5:3f93dd1d4cb3 143
sam_grove 5:3f93dd1d4cb3 144 //Parameters
sam_grove 5:3f93dd1d4cb3 145 TCPSocketConnection m_sock;
sam_grove 5:3f93dd1d4cb3 146
sam_grove 5:3f93dd1d4cb3 147 int m_timeout;
sam_grove 5:3f93dd1d4cb3 148
sam_grove 5:3f93dd1d4cb3 149 const char* m_basicAuthUser;
sam_grove 5:3f93dd1d4cb3 150 const char* m_basicAuthPassword;
sam_grove 5:3f93dd1d4cb3 151 int m_httpResponseCode;
sam_grove 5:3f93dd1d4cb3 152
sam_grove 5:3f93dd1d4cb3 153 };
sam_grove 5:3f93dd1d4cb3 154
sam_grove 5:3f93dd1d4cb3 155 //Including data containers here for more convenience
sam_grove 5:3f93dd1d4cb3 156 #include "data/HTTPText.h"
sam_grove 5:3f93dd1d4cb3 157 #include "data/HTTPMap.h"
sam_grove 5:3f93dd1d4cb3 158
sam_grove 5:3f93dd1d4cb3 159 #endif