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
Diff: api/DynamicResource.cpp
- Revision:
- 31:bacc63106754
- Parent:
- 30:113c2a1d8db2
- Child:
- 36:1c6c45584c13
--- a/api/DynamicResource.cpp Tue Apr 07 21:30:03 2015 +0000 +++ b/api/DynamicResource.cpp Wed Apr 08 15:49:56 2015 +0000 @@ -25,6 +25,13 @@ // InstancePointerTable Helper #include "InstancePointerTableHelper.h" +// ResourceObserver help +#include "ResourceObserver.h" + +// Start/Stop Observations... +#define START_OBS 0 +#define STOP_OBS 1 + // default constructor DynamicResource::DynamicResource(const Logger *logger,const char *name,const char *res_type,uint8_t res_mask,const bool observable) : Resource<string>(logger,string(name),string("")) { @@ -35,6 +42,7 @@ this->m_obs_token_ptr = NULL; this->m_obs_token_len = 0; this->m_data_wrapper = NULL; + this->m_observer = NULL; } // constructor (input initial value) @@ -103,7 +111,7 @@ uint8_t DynamicResource::process(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s *proto) { sn_coap_hdr_s *coap_res_ptr = 0; - + // create our key for debugging output... DataWrapper *hold = this->getDataWrapper(); this->setDataWrapper(NULL); @@ -157,11 +165,24 @@ // Observation handling... if(received_coap_ptr->options_list_ptr && received_coap_ptr->options_list_ptr->observe) { + // ResourceObserver + ResourceObserver *observer = (ResourceObserver *)this->m_observer; + + // create the options list pointer coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s)); memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s)); - coap_res_ptr->options_list_ptr->observe_ptr = &this->m_obs_number; // see nullify note below... - coap_res_ptr->options_list_ptr->observe_len = 1; - this->m_obs_number++; + + // get observe start/stop value from received options list pointer + uint8_t OBS_command = *received_coap_ptr->options_list_ptr->observe_ptr; + if (OBS_command == START_OBS) { + coap_res_ptr->options_list_ptr->observe_ptr = &this->m_obs_number; // see nullify note below... + coap_res_ptr->options_list_ptr->observe_len = 1; + this->m_obs_number++; + if (observer != NULL) observer->beginObservation(); + } + if (OBS_command == STOP_OBS) { + if (observer != NULL) observer->stopObservation(); + } } // build out the response and send... @@ -317,6 +338,11 @@ } } +// set the observer pointer +void DynamicResource::setObserver(void *observer) { + this->m_observer = observer; +} + // convenience method to get the URI from its buffer field... string DynamicResource::coapDataToString(uint8_t *coap_data_ptr,int coap_data_ptr_length) {