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.
Fork of mbedConnectorInterface by
Diff: api/DynamicResource.cpp
- Revision:
- 24:a6915e19814e
- Parent:
- 23:caa0260acc21
- Child:
- 25:1fc958ac14d1
--- a/api/DynamicResource.cpp Thu Mar 19 04:05:08 2015 +0000 +++ b/api/DynamicResource.cpp Fri Mar 20 04:08:59 2015 +0000 @@ -34,6 +34,7 @@ this->m_obs_number = 0; this->m_obs_token_ptr = NULL; this->m_obs_token_len = 0; + this->m_data_wrapper = NULL; } // constructor (input initial value) @@ -45,6 +46,7 @@ this->m_obs_number = 0; this->m_obs_token_ptr = NULL; this->m_obs_token_len = 0; + this->m_data_wrapper = NULL; } // constructor (strings) @@ -56,6 +58,7 @@ this->m_obs_number = 0; this->m_obs_token_ptr = NULL; this->m_obs_token_len = 0; + this->m_data_wrapper = NULL; } // copy constructor @@ -67,6 +70,7 @@ this->m_obs_number = resource.m_obs_number; this->m_obs_token_ptr = resource.m_obs_token_ptr; this->m_obs_token_len = resource.m_obs_token_len; + this->m_data_wrapper = resource.m_data_wrapper; } // destructor @@ -112,12 +116,26 @@ this->logger()->log("Calling resource(GET) for [%s]...",key.c_str()); string value = this->get(); - // convert the string from the GET to something suitable for CoAP payloads - this->logger()->log("Building payload for [%s]=[%s]...",key.c_str(),value.c_str()); - - // fill in the CoAP response payload - coap_res_ptr->payload_len = value.size(); - coap_res_ptr->payload_ptr = (uint8_t *)value.c_str(); + // convert the string from the GET to something suitable for CoAP payloads + if (this->getDataWrapper() != NULL) { + // wrap the data... + this->getDataWrapper()->wrap((uint8_t *)value.c_str(),(int)value.size()); + + // announce (after wrap) + this->logger()->log("Building payload for [%s]=[%s]...",key.c_str(),this->getDataWrapper()->get()); + + // fill in the CoAP response payload + coap_res_ptr->payload_len = this->getDataWrapper()->length(); + coap_res_ptr->payload_ptr = this->getDataWrapper()->get(); + } + else { + // announce (no wrap) + this->logger()->log("Building payload for [%s]=[%s]...",key.c_str(),value.c_str()); + + // do not wrap the data... + coap_res_ptr->payload_len = value.size(); + coap_res_ptr->payload_ptr = (uint8_t *)value.c_str(); + } // Observation handling... if(received_coap_ptr->token_ptr) { @@ -266,10 +284,19 @@ string DynamicResource::coapDataToString(uint8_t *coap_data_ptr,int coap_data_ptr_length) { if (coap_data_ptr != NULL && coap_data_ptr_length > 0) { - char buf[MAX_VALUE_BUFFER_LENGTH+1]; - memset(buf,0,MAX_VALUE_BUFFER_LENGTH+1); - memcpy(buf,(char *)coap_data_ptr,coap_data_ptr_length); - return string(buf); + if (this->getDataWrapper() != NULL) { + // unwrap the data... + this->getDataWrapper()->unwrap(coap_data_ptr,coap_data_ptr_length); + char *buf = (char *)this->getDataWrapper()->get(); // assumes data is null terminated in DataWrapper... + return string(buf); + } + else { + // no unwrap of the data... + char buf[MAX_VALUE_BUFFER_LENGTH+1]; + memset(buf,0,MAX_VALUE_BUFFER_LENGTH+1); + memcpy(buf,(char *)coap_data_ptr,coap_data_ptr_length); + return string(buf); + } } return string(""); }