Core Base Classes for the Light Endpoints

Dependencies:   BufferedSerial

Dependents:   mbed_mqtt_endpoint_ublox_ethernet mbed_mqtt_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_cellular mbed_nsp_endpoint_ublox_ethernet ... more

Committer:
ansond
Date:
Tue Jul 01 17:12:18 2014 +0000
Revision:
160:24337c83dd1d
Parent:
111:dac439bf902b
Child:
161:a85cb6cba5ed
switches for rtos support

Who changed what in which revision?

UserRevisionLine numberNew 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 160:24337c83dd1d 19
ansond 160:24337c83dd1d 20 #ifdef NETWORK_MUTEX
ansond 111:dac439bf902b 21 #include "rtos.h"
ansond 160:24337c83dd1d 22 extern Mutex *network_mutex;
ansond 160:24337c83dd1d 23 #endif
ansond 30:bf56ef794ba6 24
ansond 30:bf56ef794ba6 25 #include "HTTPTransport.h"
ansond 30:bf56ef794ba6 26
ansond 30:bf56ef794ba6 27 // Endpoint Support
ansond 30:bf56ef794ba6 28 #include "MBEDEndpoint.h"
ansond 31:7a4bb4742e69 29
ansond 30:bf56ef794ba6 30 // HTTP Client
ansond 30:bf56ef794ba6 31 HTTPClient _http;
ansond 30:bf56ef794ba6 32
ansond 30:bf56ef794ba6 33 // default constructor
ansond 30:bf56ef794ba6 34 HTTPTransport::HTTPTransport(ErrorHandler *error_handler,void *endpoint) : Transport(error_handler,endpoint) {
ansond 30:bf56ef794ba6 35 this->m_http = &_http;
ansond 30:bf56ef794ba6 36 this->m_connected = true;
ansond 30:bf56ef794ba6 37 }
ansond 30:bf56ef794ba6 38
ansond 96:686ec39400dc 39 // constructor with custom HTTPClient binding
ansond 96:686ec39400dc 40 HTTPTransport::HTTPTransport(HTTPClient *http,ErrorHandler *error_handler,void *endpoint) : Transport(error_handler,endpoint) {
ansond 96:686ec39400dc 41 this->m_http = http;
ansond 96:686ec39400dc 42 this->m_connected = true;
ansond 96:686ec39400dc 43 }
ansond 96:686ec39400dc 44
ansond 30:bf56ef794ba6 45 // default destructor
ansond 30:bf56ef794ba6 46 HTTPTransport::~HTTPTransport() {
ansond 30:bf56ef794ba6 47 }
ansond 37:1588ba3af6d1 48
ansond 30:bf56ef794ba6 49 // HTTP Get
ansond 30:bf56ef794ba6 50 bool HTTPTransport::httpGet(char *url,char *result,int result_length) {
ansond 36:73e343ddca7f 51 this->logger()->blinkTransportTxLED();
ansond 160:24337c83dd1d 52 #ifdef NETWORK_MUTEX
ansond 111:dac439bf902b 53 if (network_mutex != NULL) network_mutex->lock();
ansond 160:24337c83dd1d 54 #endif
ansond 30:bf56ef794ba6 55 int status = this->m_http->get(url,result,result_length);
ansond 160:24337c83dd1d 56 #ifdef NETWORK_MUTEX
ansond 111:dac439bf902b 57 if (network_mutex != NULL) network_mutex->unlock();
ansond 160:24337c83dd1d 58 #endif
ansond 36:73e343ddca7f 59 this->logger()->blinkTransportRxLED();
ansond 30:bf56ef794ba6 60 this->logger()->log("httpGet: Status: %d",status);
ansond 30:bf56ef794ba6 61 return (status == 0);
ansond 30:bf56ef794ba6 62 }
ansond 30:bf56ef794ba6 63
ansond 30:bf56ef794ba6 64 // HTTP Put
ansond 30:bf56ef794ba6 65 bool HTTPTransport::httpPut(char *url,char *data,int data_length,char *result,int result_length) {
ansond 30:bf56ef794ba6 66 HTTPText output(data,data_length);
ansond 30:bf56ef794ba6 67 HTTPText input(result,result_length);
ansond 36:73e343ddca7f 68 this->logger()->blinkTransportTxLED();
ansond 160:24337c83dd1d 69 #ifdef NETWORK_MUTEX
ansond 111:dac439bf902b 70 if (network_mutex != NULL) network_mutex->lock();
ansond 160:24337c83dd1d 71 #endif
ansond 30:bf56ef794ba6 72 int status = this->m_http->put(url,output,&input);
ansond 160:24337c83dd1d 73 #ifdef NETWORK_MUTEX
ansond 111:dac439bf902b 74 if (network_mutex != NULL) network_mutex->unlock();
ansond 160:24337c83dd1d 75 #endif
ansond 36:73e343ddca7f 76 this->logger()->blinkTransportRxLED();
ansond 30:bf56ef794ba6 77 this->logger()->log("httpPut: Status: %d",status);
ansond 30:bf56ef794ba6 78 return (status == 0);
ansond 30:bf56ef794ba6 79 }
ansond 30:bf56ef794ba6 80
ansond 30:bf56ef794ba6 81 // HTTP Post
ansond 30:bf56ef794ba6 82 bool HTTPTransport::httpPost(char *url,char *data,int data_length,char *result,int result_length) {
ansond 30:bf56ef794ba6 83 HTTPText output(data,data_length);
ansond 30:bf56ef794ba6 84 HTTPText input(result,result_length);
ansond 36:73e343ddca7f 85 this->logger()->blinkTransportTxLED();
ansond 160:24337c83dd1d 86 #ifdef NETWORK_MUTEX
ansond 111:dac439bf902b 87 if (network_mutex != NULL) network_mutex->lock();
ansond 160:24337c83dd1d 88 #endif
ansond 30:bf56ef794ba6 89 int status = this->m_http->post(url,output,&input);
ansond 160:24337c83dd1d 90 #ifdef NETWORK_MUTEX
ansond 111:dac439bf902b 91 if (network_mutex != NULL) network_mutex->unlock();
ansond 160:24337c83dd1d 92 #endif
ansond 36:73e343ddca7f 93 this->logger()->blinkTransportRxLED();
ansond 30:bf56ef794ba6 94 this->logger()->log("httpPost: Status: %d",status);
ansond 30:bf56ef794ba6 95 return (status == 0);
ansond 30:bf56ef794ba6 96 }
ansond 30:bf56ef794ba6 97
ansond 30:bf56ef794ba6 98 // HTTP Delete
ansond 30:bf56ef794ba6 99 bool HTTPTransport::httpDelete(char *url,char *data,int data_length) {
ansond 30:bf56ef794ba6 100 HTTPText input(data,data_length);
ansond 36:73e343ddca7f 101 this->logger()->blinkTransportTxLED();
ansond 160:24337c83dd1d 102 #ifdef NETWORK_MUTEX
ansond 111:dac439bf902b 103 if (network_mutex != NULL) network_mutex->lock();
ansond 160:24337c83dd1d 104 #endif
ansond 30:bf56ef794ba6 105 int status = this->m_http->del(url,&input);
ansond 160:24337c83dd1d 106 #ifdef NETWORK_MUTEX
ansond 111:dac439bf902b 107 if (network_mutex != NULL) network_mutex->unlock();
ansond 160:24337c83dd1d 108 #endif
ansond 36:73e343ddca7f 109 this->logger()->blinkTransportRxLED();
ansond 30:bf56ef794ba6 110 this->logger()->log("httpDelete: Status: %d",status);
ansond 30:bf56ef794ba6 111 return (status == 0);
ansond 30:bf56ef794ba6 112 }