mbed Connector Endpoint interface. This interface permits a mbed endpoint to easily setup MDS resources and emit those resources to an MDS server.

Dependents:   IoT_LED_demo ServoTest uWater_Project hackathon ... more

Revision:
25:1fc958ac14d1
Parent:
24:a6915e19814e
Child:
27:eb6818ad257a
--- a/api/DataWrapper.cpp	Fri Mar 20 04:08:59 2015 +0000
+++ b/api/DataWrapper.cpp	Fri Mar 20 05:32:16 2015 +0000
@@ -25,17 +25,32 @@
  // constructor
  DataWrapper::DataWrapper(uint8_t *data,int data_length) {
      this->m_data = data;
-     this->m_data_length = data_length;
+     this->m_data_length = 0;
+     this->m_data_length_max = data_length;
+     this->m_alloced = false;
+     this->reset();
+ }
+ 
+ // constructor (alloc)
+ DataWrapper::DataWrapper(int data_length) {
+     this->m_data = (uint8_t *)malloc(data_length+1);
+     memset(this->m_data,0,data_length+1);
+     this->m_data_length = 0;
+     this->m_data_length_max = data_length;
+     this->m_alloced = true;
  }
  
  // copy constructor
  DataWrapper::DataWrapper(const DataWrapper &data) {
      this->m_data = data.m_data;
      this->m_data_length = data.m_data_length;
+     this->m_data_length_max = data.m_data_length_max;
+     this->m_alloced = data.m_alloced;
  }
  
  // destructor
  DataWrapper::~DataWrapper() {
+     if (this->m_alloced && this->m_data != NULL) free(this->m_data);
  }
  
  // wrap
@@ -43,7 +58,7 @@
      this->reset();
      if (data != NULL && data_length > 0) {
         int length = data_length;
-        if (length > this->m_data_length) length = this->m_data_length;
+        if (length > this->m_data_length_max) length = this->m_data_length_max;
         memcpy(this->m_data,data,length);
         this->m_data_length = length;
      }
@@ -54,14 +69,14 @@
      this->reset();
      if (data != NULL && data_length > 0) {
         int length = data_length;
-        if (length > this->m_data_length) length = this->m_data_length;
+        if (length > this->m_data_length_max) length = this->m_data_length_max;
         memcpy(this->m_data,data,length);
         this->m_data_length = length;
      }
  }
  
  void DataWrapper::reset() {
-     if (this->m_data != NULL && this->m_data_length > 0)
-        memset(this->m_data,0,this->m_data_length);
+     if (this->m_data != NULL && this->m_data_length_max > 0)
+        memset(this->m_data,0,this->m_data_length_max);
      this->m_data_length = 0;
 }
\ No newline at end of file