Core Base Classes for the Light Endpoints
Dependents: mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_ethernet ... more
HTTPTransport.cpp@111:dac439bf902b, 2014-03-17 (annotated)
- Committer:
- ansond
- Date:
- Mon Mar 17 16:56:41 2014 +0000
- Revision:
- 111:dac439bf902b
- Parent:
- 96:686ec39400dc
- Child:
- 160:24337c83dd1d
updates
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 30:bf56ef794ba6 | 1 | /* Copyright C2013 Doug Anson, MIT License |
ansond | 30:bf56ef794ba6 | 2 | * |
ansond | 30:bf56ef794ba6 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
ansond | 30:bf56ef794ba6 | 4 | * and associated documentation files the "Software", to deal in the Software without restriction, |
ansond | 30:bf56ef794ba6 | 5 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
ansond | 30:bf56ef794ba6 | 6 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
ansond | 30:bf56ef794ba6 | 7 | * furnished to do so, subject to the following conditions: |
ansond | 30:bf56ef794ba6 | 8 | * |
ansond | 30:bf56ef794ba6 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
ansond | 30:bf56ef794ba6 | 10 | * substantial portions of the Software. |
ansond | 30:bf56ef794ba6 | 11 | * |
ansond | 30:bf56ef794ba6 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
ansond | 30:bf56ef794ba6 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
ansond | 30:bf56ef794ba6 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
ansond | 30:bf56ef794ba6 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
ansond | 30:bf56ef794ba6 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
ansond | 30:bf56ef794ba6 | 17 | */ |
ansond | 111:dac439bf902b | 18 | #include "mbed.h" |
ansond | 111:dac439bf902b | 19 | #include "rtos.h" |
ansond | 30:bf56ef794ba6 | 20 | |
ansond | 30:bf56ef794ba6 | 21 | #include "HTTPTransport.h" |
ansond | 30:bf56ef794ba6 | 22 | |
ansond | 30:bf56ef794ba6 | 23 | // Endpoint Support |
ansond | 30:bf56ef794ba6 | 24 | #include "MBEDEndpoint.h" |
ansond | 111:dac439bf902b | 25 | |
ansond | 111:dac439bf902b | 26 | // Network mutex |
ansond | 111:dac439bf902b | 27 | extern Mutex *network_mutex; |
ansond | 31:7a4bb4742e69 | 28 | |
ansond | 30:bf56ef794ba6 | 29 | // HTTP Client |
ansond | 30:bf56ef794ba6 | 30 | HTTPClient _http; |
ansond | 30:bf56ef794ba6 | 31 | |
ansond | 30:bf56ef794ba6 | 32 | // default constructor |
ansond | 30:bf56ef794ba6 | 33 | HTTPTransport::HTTPTransport(ErrorHandler *error_handler,void *endpoint) : Transport(error_handler,endpoint) { |
ansond | 30:bf56ef794ba6 | 34 | this->m_http = &_http; |
ansond | 30:bf56ef794ba6 | 35 | this->m_connected = true; |
ansond | 30:bf56ef794ba6 | 36 | } |
ansond | 30:bf56ef794ba6 | 37 | |
ansond | 96:686ec39400dc | 38 | // constructor with custom HTTPClient binding |
ansond | 96:686ec39400dc | 39 | HTTPTransport::HTTPTransport(HTTPClient *http,ErrorHandler *error_handler,void *endpoint) : Transport(error_handler,endpoint) { |
ansond | 96:686ec39400dc | 40 | this->m_http = http; |
ansond | 96:686ec39400dc | 41 | this->m_connected = true; |
ansond | 96:686ec39400dc | 42 | } |
ansond | 96:686ec39400dc | 43 | |
ansond | 30:bf56ef794ba6 | 44 | // default destructor |
ansond | 30:bf56ef794ba6 | 45 | HTTPTransport::~HTTPTransport() { |
ansond | 30:bf56ef794ba6 | 46 | } |
ansond | 37:1588ba3af6d1 | 47 | |
ansond | 30:bf56ef794ba6 | 48 | // HTTP Get |
ansond | 30:bf56ef794ba6 | 49 | bool HTTPTransport::httpGet(char *url,char *result,int result_length) { |
ansond | 36:73e343ddca7f | 50 | this->logger()->blinkTransportTxLED(); |
ansond | 111:dac439bf902b | 51 | if (network_mutex != NULL) network_mutex->lock(); |
ansond | 30:bf56ef794ba6 | 52 | int status = this->m_http->get(url,result,result_length); |
ansond | 111:dac439bf902b | 53 | if (network_mutex != NULL) network_mutex->unlock(); |
ansond | 36:73e343ddca7f | 54 | this->logger()->blinkTransportRxLED(); |
ansond | 30:bf56ef794ba6 | 55 | this->logger()->log("httpGet: Status: %d",status); |
ansond | 30:bf56ef794ba6 | 56 | return (status == 0); |
ansond | 30:bf56ef794ba6 | 57 | } |
ansond | 30:bf56ef794ba6 | 58 | |
ansond | 30:bf56ef794ba6 | 59 | // HTTP Put |
ansond | 30:bf56ef794ba6 | 60 | bool HTTPTransport::httpPut(char *url,char *data,int data_length,char *result,int result_length) { |
ansond | 30:bf56ef794ba6 | 61 | HTTPText output(data,data_length); |
ansond | 30:bf56ef794ba6 | 62 | HTTPText input(result,result_length); |
ansond | 36:73e343ddca7f | 63 | this->logger()->blinkTransportTxLED(); |
ansond | 111:dac439bf902b | 64 | if (network_mutex != NULL) network_mutex->lock(); |
ansond | 30:bf56ef794ba6 | 65 | int status = this->m_http->put(url,output,&input); |
ansond | 111:dac439bf902b | 66 | if (network_mutex != NULL) network_mutex->unlock(); |
ansond | 36:73e343ddca7f | 67 | this->logger()->blinkTransportRxLED(); |
ansond | 30:bf56ef794ba6 | 68 | this->logger()->log("httpPut: Status: %d",status); |
ansond | 30:bf56ef794ba6 | 69 | return (status == 0); |
ansond | 30:bf56ef794ba6 | 70 | } |
ansond | 30:bf56ef794ba6 | 71 | |
ansond | 30:bf56ef794ba6 | 72 | // HTTP Post |
ansond | 30:bf56ef794ba6 | 73 | bool HTTPTransport::httpPost(char *url,char *data,int data_length,char *result,int result_length) { |
ansond | 30:bf56ef794ba6 | 74 | HTTPText output(data,data_length); |
ansond | 30:bf56ef794ba6 | 75 | HTTPText input(result,result_length); |
ansond | 36:73e343ddca7f | 76 | this->logger()->blinkTransportTxLED(); |
ansond | 111:dac439bf902b | 77 | if (network_mutex != NULL) network_mutex->lock(); |
ansond | 30:bf56ef794ba6 | 78 | int status = this->m_http->post(url,output,&input); |
ansond | 111:dac439bf902b | 79 | if (network_mutex != NULL) network_mutex->unlock(); |
ansond | 36:73e343ddca7f | 80 | this->logger()->blinkTransportRxLED(); |
ansond | 30:bf56ef794ba6 | 81 | this->logger()->log("httpPost: Status: %d",status); |
ansond | 30:bf56ef794ba6 | 82 | return (status == 0); |
ansond | 30:bf56ef794ba6 | 83 | } |
ansond | 30:bf56ef794ba6 | 84 | |
ansond | 30:bf56ef794ba6 | 85 | // HTTP Delete |
ansond | 30:bf56ef794ba6 | 86 | bool HTTPTransport::httpDelete(char *url,char *data,int data_length) { |
ansond | 30:bf56ef794ba6 | 87 | HTTPText input(data,data_length); |
ansond | 36:73e343ddca7f | 88 | this->logger()->blinkTransportTxLED(); |
ansond | 111:dac439bf902b | 89 | if (network_mutex != NULL) network_mutex->lock(); |
ansond | 30:bf56ef794ba6 | 90 | int status = this->m_http->del(url,&input); |
ansond | 111:dac439bf902b | 91 | if (network_mutex != NULL) network_mutex->unlock(); |
ansond | 36:73e343ddca7f | 92 | this->logger()->blinkTransportRxLED(); |
ansond | 30:bf56ef794ba6 | 93 | this->logger()->log("httpDelete: Status: %d",status); |
ansond | 30:bf56ef794ba6 | 94 | return (status == 0); |
ansond | 30:bf56ef794ba6 | 95 | } |