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@0:a3fc1c6ef150, 2014-03-26 (annotated)
- Committer:
- ansond
- Date:
- Wed Mar 26 19:48:35 2014 +0000
- Revision:
- 0:a3fc1c6ef150
- Child:
- 33:7e7f39fad51a
updates
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 | 0:a3fc1c6ef150 | 20 | |
ansond | 0:a3fc1c6ef150 | 21 | // default constructor |
ansond | 0:a3fc1c6ef150 | 22 | IOCHTTPTransport::IOCHTTPTransport(ErrorHandler *error_handler,void *endpoint) : HTTPTransport(error_handler,endpoint) { |
ansond | 0:a3fc1c6ef150 | 23 | memset(this->m_url_buffer,0,IOC_REST_URL_LEN+1); |
ansond | 0:a3fc1c6ef150 | 24 | } |
ansond | 0:a3fc1c6ef150 | 25 | |
ansond | 0:a3fc1c6ef150 | 26 | // default destructor |
ansond | 0:a3fc1c6ef150 | 27 | IOCHTTPTransport::~IOCHTTPTransport() { |
ansond | 0:a3fc1c6ef150 | 28 | } |
ansond | 0:a3fc1c6ef150 | 29 | |
ansond | 0:a3fc1c6ef150 | 30 | // package up data |
ansond | 0:a3fc1c6ef150 | 31 | char *IOCHTTPTransport::packageData(char *verb,char *data,int ioc_id) { |
ansond | 0:a3fc1c6ef150 | 32 | char buffer[IOC_PAYLOAD_LEN+1]; |
ansond | 0:a3fc1c6ef150 | 33 | memset(buffer,0,IOC_PAYLOAD_LEN+1); |
ansond | 0:a3fc1c6ef150 | 34 | if (USE_GW_HTTP_REDIRECTOR == false) { |
ansond | 0:a3fc1c6ef150 | 35 | // just use the data |
ansond | 0:a3fc1c6ef150 | 36 | return data; |
ansond | 0:a3fc1c6ef150 | 37 | } |
ansond | 0:a3fc1c6ef150 | 38 | else { |
ansond | 0:a3fc1c6ef150 | 39 | // repackage into format: VERB;USER;PASS;AUTHDOMAIN;CONTENTTYPE;JSON;URL |
ansond | 0:a3fc1c6ef150 | 40 | 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 | 41 | memcpy(data,buffer,strlen(buffer)); |
ansond | 0:a3fc1c6ef150 | 42 | return data; |
ansond | 0:a3fc1c6ef150 | 43 | } |
ansond | 0:a3fc1c6ef150 | 44 | } |
ansond | 0:a3fc1c6ef150 | 45 | |
ansond | 0:a3fc1c6ef150 | 46 | // Load up our endpoint into the IOC |
ansond | 0:a3fc1c6ef150 | 47 | bool IOCHTTPTransport::loadEndpoint(char *data,int data_length,char *result,int result_length) { |
ansond | 0:a3fc1c6ef150 | 48 | data = this->packageData("POST",data,0); // must be first as it will first write into the common URL buffer |
ansond | 0:a3fc1c6ef150 | 49 | char *url = this->createIOCLoadURL(); // resets the common URL buffer to point to the IOC GW |
ansond | 0:a3fc1c6ef150 | 50 | return this->httpPost(url,data,strlen(data),result,result_length); |
ansond | 0:a3fc1c6ef150 | 51 | } |
ansond | 0:a3fc1c6ef150 | 52 | |
ansond | 0:a3fc1c6ef150 | 53 | // update our endpoint with the IOC |
ansond | 0:a3fc1c6ef150 | 54 | bool IOCHTTPTransport::updateEndpoint(int ioc_id,char *data,int data_length,char *result,int result_length) { |
ansond | 0:a3fc1c6ef150 | 55 | //this->logger()->log("updateEndpoint: %s",data); |
ansond | 0:a3fc1c6ef150 | 56 | data = this->packageData("PUT",data,ioc_id); |
ansond | 0:a3fc1c6ef150 | 57 | return this->httpPut(this->createIOCUpdateURL(ioc_id),data,strlen(data),result,result_length); |
ansond | 0:a3fc1c6ef150 | 58 | } |
ansond | 0:a3fc1c6ef150 | 59 | |
ansond | 0:a3fc1c6ef150 | 60 | // create the IOC load URL |
ansond | 0:a3fc1c6ef150 | 61 | char *IOCHTTPTransport::createIOCLoadURL() { return this->createIOCUpdateURL(0); } |
ansond | 0:a3fc1c6ef150 | 62 | |
ansond | 0:a3fc1c6ef150 | 63 | // create the IOC update URL |
ansond | 0:a3fc1c6ef150 | 64 | char *IOCHTTPTransport::createIOCUpdateURL(int ioc_id) { return this->createIOCUpdateURL(ioc_id,USE_GW_HTTP_REDIRECTOR); } |
ansond | 0:a3fc1c6ef150 | 65 | char *IOCHTTPTransport::createIOCUpdateURL(int ioc_id,bool useRedirector) { |
ansond | 0:a3fc1c6ef150 | 66 | if (useRedirector == false) { |
ansond | 0:a3fc1c6ef150 | 67 | // make HTTP calls directly |
ansond | 0:a3fc1c6ef150 | 68 | char ioc_id_str[IOC_IOC_ID_LEN+1]; |
ansond | 0:a3fc1c6ef150 | 69 | memset(ioc_id_str,0,IOC_IOC_ID_LEN+1); |
ansond | 0:a3fc1c6ef150 | 70 | if (ioc_id > 0) sprintf(ioc_id_str,"/%d",ioc_id); |
ansond | 0:a3fc1c6ef150 | 71 | memset(this->m_url_buffer,0,IOC_REST_URL_LEN+1); |
ansond | 0:a3fc1c6ef150 | 72 | sprintf(this->m_url_buffer,IOC_REST_URL,IOC_HOST_NAME,IOC_DATASOURCE_ID,ioc_id_str); |
ansond | 0:a3fc1c6ef150 | 73 | } |
ansond | 0:a3fc1c6ef150 | 74 | else { |
ansond | 0:a3fc1c6ef150 | 75 | // use the GW HTTP redirector |
ansond | 0:a3fc1c6ef150 | 76 | memset(this->m_url_buffer,0,IOC_REST_URL_LEN+1); |
ansond | 0:a3fc1c6ef150 | 77 | sprintf(this->m_url_buffer,GW_REDIRECT_URL,GW_IPADDRESS); |
ansond | 0:a3fc1c6ef150 | 78 | } |
ansond | 0:a3fc1c6ef150 | 79 | return this->m_url_buffer; |
ansond | 0:a3fc1c6ef150 | 80 | } |
ansond | 0:a3fc1c6ef150 | 81 | |
ansond | 0:a3fc1c6ef150 | 82 | // HTTP Get |
ansond | 0:a3fc1c6ef150 | 83 | bool IOCHTTPTransport::httpGet(char *url,char *result,int result_length) { |
ansond | 0:a3fc1c6ef150 | 84 | this->m_http->basicAuth(IOC_USERNAME,IOC_PASSWORD); |
ansond | 0:a3fc1c6ef150 | 85 | return HTTPTransport::httpGet(url,result,result_length); |
ansond | 0:a3fc1c6ef150 | 86 | } |
ansond | 0:a3fc1c6ef150 | 87 | |
ansond | 0:a3fc1c6ef150 | 88 | // HTTP Put |
ansond | 0:a3fc1c6ef150 | 89 | bool IOCHTTPTransport::httpPut(char *url,char *data,int data_length,char *result,int result_length) { |
ansond | 0:a3fc1c6ef150 | 90 | this->m_http->basicAuth(IOC_USERNAME,IOC_PASSWORD); |
ansond | 0:a3fc1c6ef150 | 91 | return HTTPTransport::httpPut(url,data,data_length,result,result_length); |
ansond | 0:a3fc1c6ef150 | 92 | } |
ansond | 0:a3fc1c6ef150 | 93 | |
ansond | 0:a3fc1c6ef150 | 94 | // HTTP Post |
ansond | 0:a3fc1c6ef150 | 95 | bool IOCHTTPTransport::httpPost(char *url,char *data,int data_length,char *result,int result_length) { |
ansond | 0:a3fc1c6ef150 | 96 | this->m_http->basicAuth(IOC_USERNAME,IOC_PASSWORD); |
ansond | 0:a3fc1c6ef150 | 97 | return HTTPTransport::httpPost(url,data,data_length,result,result_length); |
ansond | 0:a3fc1c6ef150 | 98 | } |
ansond | 0:a3fc1c6ef150 | 99 | |
ansond | 0:a3fc1c6ef150 | 100 | // HTTP Delete |
ansond | 0:a3fc1c6ef150 | 101 | bool IOCHTTPTransport::httpDelete(char *url,char *data,int data_length) { |
ansond | 0:a3fc1c6ef150 | 102 | this->m_http->basicAuth(IOC_USERNAME,IOC_PASSWORD); |
ansond | 0:a3fc1c6ef150 | 103 | return HTTPTransport::httpDelete(url,data,data_length); |
ansond | 0:a3fc1c6ef150 | 104 | } |