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:
Thu Feb 27 22:07:45 2014 +0000
Revision:
21:d1ce325d1d32
Parent:
20:4d7321f7579d
Child:
23:793b2898522e
updates

Who changed what in which revision?

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