Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_mqtt_endpoint_nxp
IOCHTTPTransport.cpp
00001 /* Copyright C2013 Doug Anson, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files the "Software", to deal in the Software without restriction, 00005 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00006 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00007 * furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 #include "IOCHTTPTransport.h" 00020 #include "MBEDEndpoint.h" 00021 00022 // default constructor 00023 IOCHTTPTransport::IOCHTTPTransport(Logger *logger,void *endpoint) : HTTPTransport(logger,endpoint) { 00024 memset(this->m_url_buffer,0,IOC_REST_URL_LEN+1); 00025 } 00026 00027 // default destructor 00028 IOCHTTPTransport::~IOCHTTPTransport() { 00029 } 00030 00031 // package up data 00032 char *IOCHTTPTransport::packageData(char *verb,char *data,int ioc_id) { 00033 char buffer[IOC_PAYLOAD_LEN+1]; 00034 memset(buffer,0,IOC_PAYLOAD_LEN+1); 00035 if (USE_GW_HTTP_REDIRECTOR == false) { 00036 // just use the data 00037 return data; 00038 } 00039 else { 00040 // repackage into format: VERB;USER;PASS;AUTHDOMAIN;CONTENTTYPE;JSON;URL 00041 sprintf(buffer,"%s;%s;%s;_none_;%s;%s;%s ",verb,IOC_USERNAME,IOC_PASSWORD,"application/json",data,this->createIOCUpdateURL(ioc_id,false)); 00042 memcpy(data,buffer,strlen(buffer)); 00043 return data; 00044 } 00045 } 00046 00047 // Load up our endpoint into the IOC 00048 bool IOCHTTPTransport::loadEndpoint(char *data,int data_length,char *result,int result_length) { 00049 data = this->packageData("POST",data,0); // must be first as it will first write into the common URL buffer 00050 char *url = this->createIOCLoadURL(); // resets the common URL buffer to point to the IOC GW 00051 return this->httpPost(url,data,strlen(data),result,result_length); 00052 } 00053 00054 // update our endpoint with the IOC 00055 bool IOCHTTPTransport::updateEndpoint(int ioc_id,char *data,int data_length,char *result,int result_length) { 00056 //this->logger()->log("updateEndpoint: %s",data); 00057 data = this->packageData("PUT",data,ioc_id); 00058 return this->httpPut(this->createIOCUpdateURL(ioc_id),data,strlen(data),result,result_length); 00059 } 00060 00061 // create the IOC load URL 00062 char *IOCHTTPTransport::createIOCLoadURL() { return this->createIOCUpdateURL(0); } 00063 00064 // create the IOC update URL 00065 char *IOCHTTPTransport::createIOCUpdateURL(int ioc_id) { return this->createIOCUpdateURL(ioc_id,USE_GW_HTTP_REDIRECTOR); } 00066 char *IOCHTTPTransport::createIOCUpdateURL(int ioc_id,bool useRedirector) { 00067 if (useRedirector == false) { 00068 // make HTTP calls directly 00069 char ioc_id_str[IOC_IOC_ID_LEN+1]; 00070 memset(ioc_id_str,0,IOC_IOC_ID_LEN+1); 00071 if (ioc_id > 0) sprintf(ioc_id_str,"/%d",ioc_id); 00072 memset(this->m_url_buffer,0,IOC_REST_URL_LEN+1); 00073 sprintf(this->m_url_buffer,IOC_REST_URL,IOC_HOST_NAME,IOC_DATASOURCE_ID,ioc_id_str); 00074 } 00075 else { 00076 // get our endpoint 00077 MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint(); 00078 00079 // use the GW HTTP redirector 00080 memset(this->m_url_buffer,0,IOC_REST_URL_LEN+1); 00081 sprintf(this->m_url_buffer,GW_REDIRECT_URL,endpoint->getGWAddress(),endpoint->getGWPort()); 00082 } 00083 return this->m_url_buffer; 00084 } 00085 00086 // HTTP Get 00087 bool IOCHTTPTransport::httpGet(char *url,char *result,int result_length) { 00088 this->m_http->basicAuth(IOC_USERNAME,IOC_PASSWORD); 00089 return HTTPTransport::httpGet(url,result,result_length); 00090 } 00091 00092 // HTTP Put 00093 bool IOCHTTPTransport::httpPut(char *url,char *data,int data_length,char *result,int result_length) { 00094 this->m_http->basicAuth(IOC_USERNAME,IOC_PASSWORD); 00095 //this->logger()->log("httpPut: URL: %s DATA: %s",url,data); 00096 return HTTPTransport::httpPut(url,data,data_length,result,result_length); 00097 } 00098 00099 // HTTP Post 00100 bool IOCHTTPTransport::httpPost(char *url,char *data,int data_length,char *result,int result_length) { 00101 this->m_http->basicAuth(IOC_USERNAME,IOC_PASSWORD); 00102 //this->logger()->log("httpPost: URL: %s DATA: %s",url,data); 00103 return HTTPTransport::httpPost(url,data,data_length,result,result_length); 00104 } 00105 00106 // HTTP Delete 00107 bool IOCHTTPTransport::httpDelete(char *url,char *data,int data_length) { 00108 this->m_http->basicAuth(IOC_USERNAME,IOC_PASSWORD); 00109 return HTTPTransport::httpDelete(url,data,data_length); 00110 }
Generated on Thu Jul 14 2022 16:49:25 by
1.7.2