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.
Dependencies: C027 C12832 EthernetInterface StatusReporter LM75B MQTT-ansond endpoint_core endpoint_mqtt mbed-rtos mbed
HTTPTransport.cpp@10:748fc7052e61, 2014-02-26 (annotated)
- Committer:
- ansond
- Date:
- Wed Feb 26 22:27:04 2014 +0000
- Revision:
- 10:748fc7052e61
- Parent:
- 9:ff877db53cfd
- Child:
- 11:59ee476fda24
updates
Who changed what in which revision?
| User | Revision | Line number | New 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 | 10:748fc7052e61 | 40 | // build out the Payload |
| ansond | 10:748fc7052e61 | 41 | char *HTTPTransport::buildIOCPayload() { |
| ansond | 10:748fc7052e61 | 42 | MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint(); |
| ansond | 10:748fc7052e61 | 43 | memset(this->m_payload_buffer,0,IOC_PAYLOAD_LEN+1); |
| ansond | 10:748fc7052e61 | 44 | |
| ansond | 10:748fc7052e61 | 45 | // construct the payload for Load/Updates |
| ansond | 10:748fc7052e61 | 46 | |
| ansond | 10:748fc7052e61 | 47 | // return the payload |
| ansond | 10:748fc7052e61 | 48 | return this->m_payload_buffer; |
| ansond | 10:748fc7052e61 | 49 | } |
| ansond | 10:748fc7052e61 | 50 | |
| ansond | 10:748fc7052e61 | 51 | // save the IOC ID |
| ansond | 10:748fc7052e61 | 52 | void HTTPTransport::saveIOCID(char *data) { |
| ansond | 10:748fc7052e61 | 53 | MBEDEndpoint *endpoint = (MBEDEndpoint *)this->getEndpoint(); |
| ansond | 10:748fc7052e61 | 54 | int ioc_id = -1; |
| ansond | 10:748fc7052e61 | 55 | |
| ansond | 10:748fc7052e61 | 56 | // parse the IOC ID from the data result |
| ansond | 10:748fc7052e61 | 57 | |
| ansond | 10:748fc7052e61 | 58 | // save the IOC ID |
| ansond | 10:748fc7052e61 | 59 | if (ioc_id > 0) endpoint->setIOCID(ioc_id); |
| ansond | 10:748fc7052e61 | 60 | } |
| ansond | 10:748fc7052e61 | 61 | |
| ansond | 9:ff877db53cfd | 62 | // Load up our endpoint into the IOC |
| ansond | 9:ff877db53cfd | 63 | bool HTTPTransport::loadEndpoint() { |
| ansond | 9:ff877db53cfd | 64 | bool success = false; |
| ansond | 9:ff877db53cfd | 65 | |
| ansond | 10:748fc7052e61 | 66 | // create the URL |
| ansond | 10:748fc7052e61 | 67 | char *url = this->createIOCLoadURL(); |
| ansond | 10:748fc7052e61 | 68 | |
| ansond | 10:748fc7052e61 | 69 | // build the payload |
| ansond | 10:748fc7052e61 | 70 | char *data = this->buildIOCPayload(); |
| ansond | 10:748fc7052e61 | 71 | |
| ansond | 10:748fc7052e61 | 72 | // issue the POST |
| ansond | 10:748fc7052e61 | 73 | memset(this->m_result_buffer,0,IOC_RESULT_LEN+1); |
| ansond | 10:748fc7052e61 | 74 | success = this->httpPost(url,data,strlen(data),(char *)this->m_result_buffer,IOC_RESULT_LEN); |
| ansond | 10:748fc7052e61 | 75 | |
| ansond | 10:748fc7052e61 | 76 | // update the IOC ID if found |
| ansond | 10:748fc7052e61 | 77 | if (success) this->saveIOCID(this->m_result_buffer); |
| ansond | 10:748fc7052e61 | 78 | |
| ansond | 10:748fc7052e61 | 79 | // return our status |
| ansond | 9:ff877db53cfd | 80 | return success; |
| ansond | 9:ff877db53cfd | 81 | } |
| ansond | 9:ff877db53cfd | 82 | |
| ansond | 9:ff877db53cfd | 83 | // update our endpoint with the IOC |
| ansond | 9:ff877db53cfd | 84 | bool HTTPTransport::updateEndpoint() { |
| ansond | 9:ff877db53cfd | 85 | bool success = false; |
| ansond | 9:ff877db53cfd | 86 | |
| ansond | 10:748fc7052e61 | 87 | // create the URL |
| ansond | 10:748fc7052e61 | 88 | char *url = this->createIOCUpdateURL(endpoint->getIOCID()); |
| ansond | 10:748fc7052e61 | 89 | |
| ansond | 10:748fc7052e61 | 90 | // build the payload |
| ansond | 10:748fc7052e61 | 91 | char *data = this->buildIOCPayload(); |
| ansond | 10:748fc7052e61 | 92 | |
| ansond | 10:748fc7052e61 | 93 | // issue the PUT |
| ansond | 10:748fc7052e61 | 94 | memset(this->m_result_buffer,0,IOC_RESULT_LEN+1); |
| ansond | 10:748fc7052e61 | 95 | success = this->httpPut(url,data,strlen(data),(char *)this->m_result_buffer,IOC_RESULT_LEN); |
| ansond | 10:748fc7052e61 | 96 | |
| ansond | 10:748fc7052e61 | 97 | // return our status |
| ansond | 9:ff877db53cfd | 98 | return success; |
| ansond | 10:748fc7052e61 | 99 | } |
| ansond | 10:748fc7052e61 | 100 | |
| ansond | 10:748fc7052e61 | 101 | // create the IOC load URL |
| ansond | 10:748fc7052e61 | 102 | char *HTTPTransport::createIOCLoadURL() { return this->createIOCUpdateURL(0); } |
| ansond | 10:748fc7052e61 | 103 | |
| ansond | 10:748fc7052e61 | 104 | // create the IOC update URL |
| ansond | 10:748fc7052e61 | 105 | char *HTTPTransport::createIOCUpdateURL(int ioc_id) { |
| ansond | 10:748fc7052e61 | 106 | char ioc_id_str[16]; |
| ansond | 10:748fc7052e61 | 107 | memset(ioc_id_str,0,16); |
| ansond | 10:748fc7052e61 | 108 | if (ioc_id > 0) sprintf(ioc_id_str,"/%d",ioc_id); |
| ansond | 10:748fc7052e61 | 109 | memset(this->m_url_buffer,0,IOC_REST_URL_LEN+1); |
| ansond | 10:748fc7052e61 | 110 | sprintf(this->m_url_buffer,IOC_REST_URL,IOC_HOST_NAME,IOC_DATASOURCE_ID,ioc_id_str); |
| ansond | 10:748fc7052e61 | 111 | return this->m_url_buffer; |
| ansond | 10:748fc7052e61 | 112 | } |
| ansond | 9:ff877db53cfd | 113 | |
| ansond | 0:ae2a45502448 | 114 | // connect up HTTP |
| ansond | 0:ae2a45502448 | 115 | bool HTTPTransport::connect() { return this->m_connected; } |
| ansond | 0:ae2a45502448 | 116 | |
| ansond | 0:ae2a45502448 | 117 | // disconnect from HTTP |
| ansond | 0:ae2a45502448 | 118 | bool HTTPTransport::disconnect() { return true; } |
| ansond | 0:ae2a45502448 | 119 | |
| ansond | 0:ae2a45502448 | 120 | // HTTP Get |
| ansond | 0:ae2a45502448 | 121 | bool HTTPTransport::httpGet(char *url,char *result,int result_length) { |
| ansond | 0:ae2a45502448 | 122 | return (this->m_http->get(url,result,result_length) == 0); |
| ansond | 0:ae2a45502448 | 123 | } |
| ansond | 0:ae2a45502448 | 124 | |
| ansond | 0:ae2a45502448 | 125 | // HTTP Put |
| ansond | 0:ae2a45502448 | 126 | bool HTTPTransport::httpPut(char *url,char *data,int data_length,char *result,int result_length) { |
| ansond | 0:ae2a45502448 | 127 | HTTPText input(data,data_length); |
| ansond | 0:ae2a45502448 | 128 | HTTPText output(result,result_length); |
| ansond | 0:ae2a45502448 | 129 | return (this->m_http->put(url,output,&input) != 0); |
| ansond | 0:ae2a45502448 | 130 | } |
| ansond | 0:ae2a45502448 | 131 | |
| ansond | 0:ae2a45502448 | 132 | // HTTP Post |
| ansond | 0:ae2a45502448 | 133 | bool HTTPTransport::httpPost(char *url,char *data,int data_length,char *result,int result_length) { |
| ansond | 0:ae2a45502448 | 134 | HTTPText input(data,data_length); |
| ansond | 0:ae2a45502448 | 135 | HTTPText output(result,result_length); |
| ansond | 0:ae2a45502448 | 136 | return (this->m_http->post(url,output,&input) != 0); |
| ansond | 0:ae2a45502448 | 137 | } |
| ansond | 0:ae2a45502448 | 138 | |
| ansond | 0:ae2a45502448 | 139 | // HTTP Delete |
| ansond | 0:ae2a45502448 | 140 | bool HTTPTransport::httpDelete(char *url,char *data,int data_length) { |
| ansond | 0:ae2a45502448 | 141 | HTTPText input(data,data_length); |
| ansond | 0:ae2a45502448 | 142 | return (this->m_http->del(url,&input) != 0); |
| ansond | 0:ae2a45502448 | 143 | } |