MBED_DEMOS / Mbed 2 deprecated mbed_mqtt_endpoint_ublox_ethernet

Dependencies:   C027 C12832 EthernetInterface StatusReporter LM75B MQTT-ansond endpoint_core endpoint_mqtt mbed-rtos mbed

Committer:
ansond
Date:
Tue Mar 11 17:23:02 2014 +0000
Revision:
94:0159f907859b
Parent:
36:210f3956038c
Child:
95:24e9557e13d6
updates

Who changed what in which revision?

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