mqtt specific components for the impact mbed endpoint library
Dependents: mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_mqtt_endpoint_nxp
IOCHTTPTransport.cpp@48:7192a7a4edcd, 2014-07-01 (annotated)
- Committer:
- ansond
- Date:
- Tue Jul 01 22:01:09 2014 +0000
- Revision:
- 48:7192a7a4edcd
- Parent:
- 46:32a7545dde45
- Child:
- 56:789a1a8c5ebe
fixed gw redirector url
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 0:a3fc1c6ef150 | 1 | /* Copyright C2013 Doug Anson, MIT License |
ansond | 0:a3fc1c6ef150 | 2 | * |
ansond | 0:a3fc1c6ef150 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
ansond | 0:a3fc1c6ef150 | 4 | * and associated documentation files the "Software", to deal in the Software without restriction, |
ansond | 0:a3fc1c6ef150 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
ansond | 0:a3fc1c6ef150 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
ansond | 0:a3fc1c6ef150 | 7 | * furnished to do so, subject to the following conditions: |
ansond | 0:a3fc1c6ef150 | 8 | * |
ansond | 0:a3fc1c6ef150 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
ansond | 0:a3fc1c6ef150 | 10 | * substantial portions of the Software. |
ansond | 0:a3fc1c6ef150 | 11 | * |
ansond | 0:a3fc1c6ef150 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
ansond | 0:a3fc1c6ef150 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
ansond | 0:a3fc1c6ef150 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
ansond | 0:a3fc1c6ef150 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ansond | 0:a3fc1c6ef150 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
ansond | 0:a3fc1c6ef150 | 17 | */ |
ansond | 0:a3fc1c6ef150 | 18 | |
ansond | 0:a3fc1c6ef150 | 19 | #include "IOCHTTPTransport.h" |
ansond | 33:7e7f39fad51a | 20 | #include "MBEDEndpoint.h" |
ansond | 0:a3fc1c6ef150 | 21 | |
ansond | 0:a3fc1c6ef150 | 22 | // default constructor |
ansond | 0:a3fc1c6ef150 | 23 | IOCHTTPTransport::IOCHTTPTransport(ErrorHandler *error_handler,void *endpoint) : HTTPTransport(error_handler,endpoint) { |
ansond | 0:a3fc1c6ef150 | 24 | memset(this->m_url_buffer,0,IOC_REST_URL_LEN+1); |
ansond | 0:a3fc1c6ef150 | 25 | } |
ansond | 0:a3fc1c6ef150 | 26 | |
ansond | 0:a3fc1c6ef150 | 27 | // default destructor |
ansond | 0:a3fc1c6ef150 | 28 | IOCHTTPTransport::~IOCHTTPTransport() { |
ansond | 0:a3fc1c6ef150 | 29 | } |
ansond | 0:a3fc1c6ef150 | 30 | |
ansond | 0:a3fc1c6ef150 | 31 | // package up data |
ansond | 0:a3fc1c6ef150 | 32 | char *IOCHTTPTransport::packageData(char *verb,char *data,int ioc_id) { |
ansond | 0:a3fc1c6ef150 | 33 | char buffer[IOC_PAYLOAD_LEN+1]; |
ansond | 0:a3fc1c6ef150 | 34 | memset(buffer,0,IOC_PAYLOAD_LEN+1); |
ansond | 0:a3fc1c6ef150 | 35 | if (USE_GW_HTTP_REDIRECTOR == false) { |
ansond | 0:a3fc1c6ef150 | 36 | // just use the data |
ansond | 0:a3fc1c6ef150 | 37 | return data; |
ansond | 0:a3fc1c6ef150 | 38 | } |
ansond | 0:a3fc1c6ef150 | 39 | else { |
ansond | 0:a3fc1c6ef150 | 40 | // repackage into format: VERB;USER;PASS;AUTHDOMAIN;CONTENTTYPE;JSON;URL |
ansond | 0:a3fc1c6ef150 | 41 | sprintf(buffer,"%s;%s;%s;_none_;%s;%s;%s ",verb,IOC_USERNAME,IOC_PASSWORD,"application/json",data,this->createIOCUpdateURL(ioc_id,false)); |
ansond | 0:a3fc1c6ef150 | 42 | memcpy(data,buffer,strlen(buffer)); |
ansond | 0:a3fc1c6ef150 | 43 | return data; |
ansond | 0:a3fc1c6ef150 | 44 | } |
ansond | 0:a3fc1c6ef150 | 45 | } |
ansond | 0:a3fc1c6ef150 | 46 | |
ansond | 0:a3fc1c6ef150 | 47 | // Load up our endpoint into the IOC |
ansond | 0:a3fc1c6ef150 | 48 | bool IOCHTTPTransport::loadEndpoint(char *data,int data_length,char *result,int result_length) { |
ansond | 0:a3fc1c6ef150 | 49 | data = this->packageData("POST",data,0); // must be first as it will first write into the common URL buffer |
ansond | 0:a3fc1c6ef150 | 50 | char *url = this->createIOCLoadURL(); // resets the common URL buffer to point to the IOC GW |
ansond | 0:a3fc1c6ef150 | 51 | return this->httpPost(url,data,strlen(data),result,result_length); |
ansond | 0:a3fc1c6ef150 | 52 | } |
ansond | 0:a3fc1c6ef150 | 53 | |
ansond | 0:a3fc1c6ef150 | 54 | // update our endpoint with the IOC |
ansond | 0:a3fc1c6ef150 | 55 | bool IOCHTTPTransport::updateEndpoint(int ioc_id,char *data,int data_length,char *result,int result_length) { |
ansond | 0:a3fc1c6ef150 | 56 | //this->logger()->log("updateEndpoint: %s",data); |
ansond | 0:a3fc1c6ef150 | 57 | data = this->packageData("PUT",data,ioc_id); |
ansond | 0:a3fc1c6ef150 | 58 | return this->httpPut(this->createIOCUpdateURL(ioc_id),data,strlen(data),result,result_length); |
ansond | 0:a3fc1c6ef150 | 59 | } |
ansond | 0:a3fc1c6ef150 | 60 | |
ansond | 0:a3fc1c6ef150 | 61 | // create the IOC load URL |
ansond | 0:a3fc1c6ef150 | 62 | char *IOCHTTPTransport::createIOCLoadURL() { return this->createIOCUpdateURL(0); } |
ansond | 0:a3fc1c6ef150 | 63 | |
ansond | 0:a3fc1c6ef150 | 64 | // create the IOC update URL |
ansond | 0:a3fc1c6ef150 | 65 | char *IOCHTTPTransport::createIOCUpdateURL(int ioc_id) { return this->createIOCUpdateURL(ioc_id,USE_GW_HTTP_REDIRECTOR); } |
ansond | 0:a3fc1c6ef150 | 66 | char *IOCHTTPTransport::createIOCUpdateURL(int ioc_id,bool useRedirector) { |
ansond | 0:a3fc1c6ef150 | 67 | if (useRedirector == false) { |
ansond | 0:a3fc1c6ef150 | 68 | // make HTTP calls directly |
ansond | 0:a3fc1c6ef150 | 69 | char ioc_id_str[IOC_IOC_ID_LEN+1]; |
ansond | 0:a3fc1c6ef150 | 70 | memset(ioc_id_str,0,IOC_IOC_ID_LEN+1); |
ansond | 0:a3fc1c6ef150 | 71 | if (ioc_id > 0) sprintf(ioc_id_str,"/%d",ioc_id); |
ansond | 0:a3fc1c6ef150 | 72 | memset(this->m_url_buffer,0,IOC_REST_URL_LEN+1); |
ansond | 0:a3fc1c6ef150 | 73 | sprintf(this->m_url_buffer,IOC_REST_URL,IOC_HOST_NAME,IOC_DATASOURCE_ID,ioc_id_str); |
ansond | 0:a3fc1c6ef150 | 74 | } |
ansond | 0:a3fc1c6ef150 | 75 | else { |
ansond | 33:7e7f39fad51a | 76 | // get our endpoint |
ansond | 33:7e7f39fad51a | 77 | MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint(); |
ansond | 33:7e7f39fad51a | 78 | |
ansond | 0:a3fc1c6ef150 | 79 | // use the GW HTTP redirector |
ansond | 0:a3fc1c6ef150 | 80 | memset(this->m_url_buffer,0,IOC_REST_URL_LEN+1); |
ansond | 48:7192a7a4edcd | 81 | sprintf(this->m_url_buffer,GW_REDIRECT_URL,endpoint->getGWAddress(),endpoint->getGWPort()); |
ansond | 0:a3fc1c6ef150 | 82 | } |
ansond | 0:a3fc1c6ef150 | 83 | return this->m_url_buffer; |
ansond | 0:a3fc1c6ef150 | 84 | } |
ansond | 0:a3fc1c6ef150 | 85 | |
ansond | 0:a3fc1c6ef150 | 86 | // HTTP Get |
ansond | 0:a3fc1c6ef150 | 87 | bool IOCHTTPTransport::httpGet(char *url,char *result,int result_length) { |
ansond | 0:a3fc1c6ef150 | 88 | this->m_http->basicAuth(IOC_USERNAME,IOC_PASSWORD); |
ansond | 0:a3fc1c6ef150 | 89 | return HTTPTransport::httpGet(url,result,result_length); |
ansond | 0:a3fc1c6ef150 | 90 | } |
ansond | 0:a3fc1c6ef150 | 91 | |
ansond | 0:a3fc1c6ef150 | 92 | // HTTP Put |
ansond | 0:a3fc1c6ef150 | 93 | bool IOCHTTPTransport::httpPut(char *url,char *data,int data_length,char *result,int result_length) { |
ansond | 0:a3fc1c6ef150 | 94 | this->m_http->basicAuth(IOC_USERNAME,IOC_PASSWORD); |
ansond | 36:23a1b2dde4d9 | 95 | //this->logger()->log("httpPut: URL: %s DATA: %s",url,data); |
ansond | 0:a3fc1c6ef150 | 96 | return HTTPTransport::httpPut(url,data,data_length,result,result_length); |
ansond | 0:a3fc1c6ef150 | 97 | } |
ansond | 0:a3fc1c6ef150 | 98 | |
ansond | 0:a3fc1c6ef150 | 99 | // HTTP Post |
ansond | 0:a3fc1c6ef150 | 100 | bool IOCHTTPTransport::httpPost(char *url,char *data,int data_length,char *result,int result_length) { |
ansond | 0:a3fc1c6ef150 | 101 | this->m_http->basicAuth(IOC_USERNAME,IOC_PASSWORD); |
ansond | 36:23a1b2dde4d9 | 102 | //this->logger()->log("httpPost: URL: %s DATA: %s",url,data); |
ansond | 0:a3fc1c6ef150 | 103 | return HTTPTransport::httpPost(url,data,data_length,result,result_length); |
ansond | 0:a3fc1c6ef150 | 104 | } |
ansond | 0:a3fc1c6ef150 | 105 | |
ansond | 0:a3fc1c6ef150 | 106 | // HTTP Delete |
ansond | 0:a3fc1c6ef150 | 107 | bool IOCHTTPTransport::httpDelete(char *url,char *data,int data_length) { |
ansond | 0:a3fc1c6ef150 | 108 | this->m_http->basicAuth(IOC_USERNAME,IOC_PASSWORD); |
ansond | 0:a3fc1c6ef150 | 109 | return HTTPTransport::httpDelete(url,data,data_length); |
ansond | 0:a3fc1c6ef150 | 110 | } |