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

Committer:
ansond
Date:
Mon Apr 13 23:41:34 2015 +0000
Revision:
48:4b9ee3e32f93
Parent:
37:5de33aed0cac
Child:
50:1a9e2184945e
added configuration options for GET based observation control and the ability to have immediate observationing if desired

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:b438482ebbfc 1 /**
ansond 0:b438482ebbfc 2 * @file DynamicResource.cpp
ansond 0:b438482ebbfc 3 * @brief mbed CoAP Endpoint Dynamic Resource class
ansond 0:b438482ebbfc 4 * @author Doug Anson/Chris Paola
ansond 0:b438482ebbfc 5 * @version 1.0
sam_grove 2:853f9ecc12df 6 * @see
ansond 0:b438482ebbfc 7 *
ansond 0:b438482ebbfc 8 * Copyright (c) 2014
ansond 0:b438482ebbfc 9 *
ansond 0:b438482ebbfc 10 * Licensed under the Apache License, Version 2.0 (the "License");
ansond 0:b438482ebbfc 11 * you may not use this file except in compliance with the License.
ansond 0:b438482ebbfc 12 * You may obtain a copy of the License at
ansond 0:b438482ebbfc 13 *
ansond 0:b438482ebbfc 14 * http://www.apache.org/licenses/LICENSE-2.0
ansond 0:b438482ebbfc 15 *
ansond 0:b438482ebbfc 16 * Unless required by applicable law or agreed to in writing, software
ansond 0:b438482ebbfc 17 * distributed under the License is distributed on an "AS IS" BASIS,
ansond 0:b438482ebbfc 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ansond 0:b438482ebbfc 19 * See the License for the specific language governing permissions and
ansond 0:b438482ebbfc 20 * limitations under the License.
ansond 0:b438482ebbfc 21 */
sam_grove 2:853f9ecc12df 22
sam_grove 2:853f9ecc12df 23 #include "DynamicResource.h"
sam_grove 2:853f9ecc12df 24
sam_grove 2:853f9ecc12df 25 // InstancePointerTable Helper
sam_grove 2:853f9ecc12df 26 #include "InstancePointerTableHelper.h"
sam_grove 2:853f9ecc12df 27
ansond 31:bacc63106754 28 // ResourceObserver help
ansond 31:bacc63106754 29 #include "ResourceObserver.h"
ansond 31:bacc63106754 30
ansond 48:4b9ee3e32f93 31 // Options enablement
ansond 48:4b9ee3e32f93 32 #include "Options.h"
ansond 48:4b9ee3e32f93 33
ansond 48:4b9ee3e32f93 34 // GET verb can Start/Stop Observations...
ansond 48:4b9ee3e32f93 35 #define START_OBS 0
ansond 48:4b9ee3e32f93 36 #define STOP_OBS 1
ansond 31:bacc63106754 37
sam_grove 2:853f9ecc12df 38 // default constructor
sam_grove 2:853f9ecc12df 39 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(""))
sam_grove 2:853f9ecc12df 40 {
ansond 0:b438482ebbfc 41 this->m_res_type = string(res_type);
ansond 0:b438482ebbfc 42 this->m_observable = observable;
sam_grove 2:853f9ecc12df 43 this->m_res_mask = res_mask;
ansond 21:8487990a3baa 44 this->m_obs_number = 0;
ansond 21:8487990a3baa 45 this->m_obs_token_ptr = NULL;
ansond 21:8487990a3baa 46 this->m_obs_token_len = 0;
ansond 24:a6915e19814e 47 this->m_data_wrapper = NULL;
ansond 31:bacc63106754 48 this->m_observer = NULL;
sam_grove 2:853f9ecc12df 49 }
sam_grove 2:853f9ecc12df 50
sam_grove 2:853f9ecc12df 51 // constructor (input initial value)
sam_grove 2:853f9ecc12df 52 DynamicResource::DynamicResource(const Logger *logger,const char *name,const char *res_type,const string value,uint8_t res_mask,const bool observable) : Resource<string>(logger,string(name),value)
sam_grove 2:853f9ecc12df 53 {
ansond 0:b438482ebbfc 54 this->m_res_type = string(res_type);
ansond 0:b438482ebbfc 55 this->m_observable = observable;
sam_grove 2:853f9ecc12df 56 this->m_res_mask = res_mask;
ansond 21:8487990a3baa 57 this->m_obs_number = 0;
ansond 21:8487990a3baa 58 this->m_obs_token_ptr = NULL;
ansond 21:8487990a3baa 59 this->m_obs_token_len = 0;
ansond 24:a6915e19814e 60 this->m_data_wrapper = NULL;
ansond 36:1c6c45584c13 61 this->m_observer = NULL;
sam_grove 2:853f9ecc12df 62 }
sam_grove 2:853f9ecc12df 63
sam_grove 2:853f9ecc12df 64 // constructor (strings)
sam_grove 2:853f9ecc12df 65 DynamicResource::DynamicResource(const Logger *logger,const string name,const string res_type,const string value,uint8_t res_mask,const bool observable) : Resource<string>(logger,name,value)
sam_grove 2:853f9ecc12df 66 {
ansond 0:b438482ebbfc 67 this->m_res_type = res_type;
ansond 0:b438482ebbfc 68 this->m_observable = observable;
sam_grove 2:853f9ecc12df 69 this->m_res_mask = res_mask;
ansond 21:8487990a3baa 70 this->m_obs_number = 0;
ansond 21:8487990a3baa 71 this->m_obs_token_ptr = NULL;
ansond 21:8487990a3baa 72 this->m_obs_token_len = 0;
ansond 24:a6915e19814e 73 this->m_data_wrapper = NULL;
ansond 36:1c6c45584c13 74 this->m_observer = NULL;
sam_grove 2:853f9ecc12df 75 }
sam_grove 2:853f9ecc12df 76
sam_grove 2:853f9ecc12df 77 // copy constructor
sam_grove 2:853f9ecc12df 78 DynamicResource::DynamicResource(const DynamicResource &resource) : Resource<string>((const Resource<string> &)resource)
sam_grove 2:853f9ecc12df 79 {
ansond 0:b438482ebbfc 80 this->m_res_type = resource.m_res_type;
ansond 0:b438482ebbfc 81 this->m_observable = resource.m_observable;
ansond 0:b438482ebbfc 82 this->m_res_mask = resource.m_res_mask;
ansond 21:8487990a3baa 83 this->m_obs_number = resource.m_obs_number;
ansond 21:8487990a3baa 84 this->m_obs_token_ptr = resource.m_obs_token_ptr;
ansond 21:8487990a3baa 85 this->m_obs_token_len = resource.m_obs_token_len;
ansond 24:a6915e19814e 86 this->m_data_wrapper = resource.m_data_wrapper;
ansond 36:1c6c45584c13 87 this->m_observer = resource.m_observer;
sam_grove 2:853f9ecc12df 88 }
sam_grove 2:853f9ecc12df 89
sam_grove 2:853f9ecc12df 90 // destructor
sam_grove 2:853f9ecc12df 91 DynamicResource::~DynamicResource()
sam_grove 2:853f9ecc12df 92 {
ansond 21:8487990a3baa 93 if(this->m_obs_token_ptr) free(this->m_obs_token_ptr);
sam_grove 2:853f9ecc12df 94 }
sam_grove 2:853f9ecc12df 95
sam_grove 2:853f9ecc12df 96 // bind resource to NSDL
sam_grove 2:853f9ecc12df 97 void DynamicResource::bind(void *p)
sam_grove 2:853f9ecc12df 98 {
ansond 0:b438482ebbfc 99 if (p != NULL) {
ansond 0:b438482ebbfc 100 sn_nsdl_resource_info_s *resource_ptr = (sn_nsdl_resource_info_s *)p;
ansond 0:b438482ebbfc 101 const uint8_t *name = (const uint8_t *)(this->getName().c_str());
ansond 0:b438482ebbfc 102 const uint8_t *res_type = (const uint8_t *)this->m_res_type.c_str();
ansond 0:b438482ebbfc 103 int name_length = this->getName().size();
ansond 0:b438482ebbfc 104 int res_type_length = this->m_res_type.size();
sam_grove 2:853f9ecc12df 105 int is_observable = 0;
sam_grove 2:853f9ecc12df 106 if (this->m_observable == true) is_observable = 1;
ansond 0:b438482ebbfc 107 const string *key = new string(this->getName());
ansond 0:b438482ebbfc 108 ipt_helper_add_instance_pointer(key,this);
ansond 0:b438482ebbfc 109 nsdl_create_dynamic_resource(resource_ptr,name_length,(uint8_t *)name,res_type_length,(uint8_t *)res_type,is_observable,&ipt_helper_nsdl_callback_stub,this->m_res_mask);
ansond 20:abfbaf524192 110 this->logger()->log("DynamicResource: [%s] type: [%s] bound (observable: %d)",name,res_type,is_observable);
sam_grove 2:853f9ecc12df 111 } else {
ansond 5:a929d65eb385 112 this->logger()->log("DynamicResource: NULL parameter in bind()");
ansond 0:b438482ebbfc 113 }
sam_grove 2:853f9ecc12df 114 }
ansond 0:b438482ebbfc 115
sam_grove 2:853f9ecc12df 116 // process NSDL message
sam_grove 2:853f9ecc12df 117 uint8_t DynamicResource::process(sn_coap_hdr_s *received_coap_ptr, sn_nsdl_addr_s *address, sn_proto_info_s *proto)
sam_grove 2:853f9ecc12df 118 {
ansond 0:b438482ebbfc 119 sn_coap_hdr_s *coap_res_ptr = 0;
ansond 31:bacc63106754 120
ansond 0:b438482ebbfc 121 // create our key for debugging output...
ansond 25:1fc958ac14d1 122 DataWrapper *hold = this->getDataWrapper();
ansond 25:1fc958ac14d1 123 this->setDataWrapper(NULL);
ansond 0:b438482ebbfc 124 string key = this->coapDataToString(received_coap_ptr->uri_path_ptr,received_coap_ptr->uri_path_len);
ansond 25:1fc958ac14d1 125 this->setDataWrapper(hold);
sam_grove 2:853f9ecc12df 126
sam_grove 2:853f9ecc12df 127 if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_GET) {
ansond 0:b438482ebbfc 128 coap_res_ptr = sn_coap_build_response(received_coap_ptr, COAP_MSG_CODE_RESPONSE_CONTENT);
sam_grove 2:853f9ecc12df 129
sam_grove 2:853f9ecc12df 130 // process the GET if we have registered a callback for it...
ansond 0:b438482ebbfc 131 if ((this->m_res_mask&SN_GRS_GET_ALLOWED) != 0) {
ansond 0:b438482ebbfc 132 // call the resource get() to get the resource value
ansond 5:a929d65eb385 133 this->logger()->log("Calling resource(GET) for [%s]...",key.c_str());
ansond 0:b438482ebbfc 134 string value = this->get();
sam_grove 2:853f9ecc12df 135
ansond 24:a6915e19814e 136 // convert the string from the GET to something suitable for CoAP payloads
ansond 24:a6915e19814e 137 if (this->getDataWrapper() != NULL) {
ansond 24:a6915e19814e 138 // wrap the data...
ansond 24:a6915e19814e 139 this->getDataWrapper()->wrap((uint8_t *)value.c_str(),(int)value.size());
ansond 24:a6915e19814e 140
ansond 24:a6915e19814e 141 // announce (after wrap)
ansond 24:a6915e19814e 142 this->logger()->log("Building payload for [%s]=[%s]...",key.c_str(),this->getDataWrapper()->get());
ansond 24:a6915e19814e 143
ansond 24:a6915e19814e 144 // fill in the CoAP response payload
ansond 24:a6915e19814e 145 coap_res_ptr->payload_len = this->getDataWrapper()->length();
ansond 24:a6915e19814e 146 coap_res_ptr->payload_ptr = this->getDataWrapper()->get();
ansond 24:a6915e19814e 147 }
ansond 24:a6915e19814e 148 else {
ansond 24:a6915e19814e 149 // announce (no wrap)
ansond 24:a6915e19814e 150 this->logger()->log("Building payload for [%s]=[%s]...",key.c_str(),value.c_str());
ansond 24:a6915e19814e 151
ansond 24:a6915e19814e 152 // do not wrap the data...
ansond 24:a6915e19814e 153 coap_res_ptr->payload_len = value.size();
ansond 24:a6915e19814e 154 coap_res_ptr->payload_ptr = (uint8_t *)value.c_str();
ansond 24:a6915e19814e 155 }
ansond 21:8487990a3baa 156
ansond 21:8487990a3baa 157 // Observation handling...
ansond 21:8487990a3baa 158 if(received_coap_ptr->token_ptr) {
ansond 21:8487990a3baa 159 if(this->m_obs_token_ptr) {
ansond 21:8487990a3baa 160 free(this->m_obs_token_ptr);
ansond 21:8487990a3baa 161 this->m_obs_token_ptr = NULL;
ansond 23:caa0260acc21 162 this->m_obs_token_len = 0;
ansond 21:8487990a3baa 163 }
ansond 22:192b598ba389 164
ansond 21:8487990a3baa 165 this->m_obs_token_ptr = (uint8_t*)malloc(received_coap_ptr->token_len);
ansond 21:8487990a3baa 166 if(this->m_obs_token_ptr) {
ansond 21:8487990a3baa 167 memcpy(this->m_obs_token_ptr, received_coap_ptr->token_ptr,received_coap_ptr->token_len);
ansond 21:8487990a3baa 168 this->m_obs_token_len = received_coap_ptr->token_len;
ansond 21:8487990a3baa 169 }
ansond 21:8487990a3baa 170 }
ansond 21:8487990a3baa 171
ansond 22:192b598ba389 172 // Observation handling...
ansond 36:1c6c45584c13 173 if(received_coap_ptr->options_list_ptr && received_coap_ptr->options_list_ptr->observe) {
ansond 31:bacc63106754 174 // create the options list pointer
ansond 21:8487990a3baa 175 coap_res_ptr->options_list_ptr = (sn_coap_options_list_s*)malloc(sizeof(sn_coap_options_list_s));
ansond 21:8487990a3baa 176 memset(coap_res_ptr->options_list_ptr, 0, sizeof(sn_coap_options_list_s));
ansond 36:1c6c45584c13 177
ansond 48:4b9ee3e32f93 178 // if GET controlled observation is enabled, perform it here...
ansond 48:4b9ee3e32f93 179 if (((Connector::Options *)this->getOptions())->enableGETObservationControl()) {
ansond 48:4b9ee3e32f93 180 // ResourceObserver
ansond 48:4b9ee3e32f93 181 ResourceObserver *observer = (ResourceObserver *)this->m_observer;
ansond 48:4b9ee3e32f93 182
ansond 48:4b9ee3e32f93 183 // get observe start/stop value from received options list pointer
ansond 48:4b9ee3e32f93 184 uint8_t OBS_command = *received_coap_ptr->options_list_ptr->observe_ptr;
ansond 48:4b9ee3e32f93 185 if (OBS_command == START_OBS) {
ansond 48:4b9ee3e32f93 186 coap_res_ptr->options_list_ptr->observe_ptr = &this->m_obs_number; // see nullify note below...
ansond 48:4b9ee3e32f93 187 coap_res_ptr->options_list_ptr->observe_len = 1;
ansond 48:4b9ee3e32f93 188 this->m_obs_number++;
ansond 48:4b9ee3e32f93 189 if (observer != NULL) observer->beginObservation();
ansond 48:4b9ee3e32f93 190 }
ansond 48:4b9ee3e32f93 191 if (OBS_command == STOP_OBS) {
ansond 48:4b9ee3e32f93 192 if (observer != NULL) observer->stopObservation();
ansond 48:4b9ee3e32f93 193 }
ansond 48:4b9ee3e32f93 194 }
ansond 48:4b9ee3e32f93 195 else {
ansond 48:4b9ee3e32f93 196 // non-GET controlled observationing: simply fill in the observation requirements...
ansond 48:4b9ee3e32f93 197 coap_res_ptr->options_list_ptr->observe_ptr = &this->m_obs_number; // see nullify note below...
ansond 31:bacc63106754 198 coap_res_ptr->options_list_ptr->observe_len = 1;
ansond 31:bacc63106754 199 this->m_obs_number++;
ansond 31:bacc63106754 200 }
ansond 21:8487990a3baa 201 }
sam_grove 2:853f9ecc12df 202
sam_grove 2:853f9ecc12df 203 // build out the response and send...
ansond 21:8487990a3baa 204 sn_nsdl_send_coap_message(address,coap_res_ptr);
ansond 22:192b598ba389 205
ansond 22:192b598ba389 206 //
ansond 22:192b598ba389 207 // nullify note:
ansond 22:192b598ba389 208 //
ansond 22:192b598ba389 209 // because our obs_number (assigned to observe_ptr) is part of this object instance, we dont
ansond 22:192b598ba389 210 // want to have the underlying free() try to free it... to just nullify here
ansond 22:192b598ba389 211 //
ansond 22:192b598ba389 212 if (coap_res_ptr->options_list_ptr) coap_res_ptr->options_list_ptr->observe_ptr = 0;
ansond 21:8487990a3baa 213 }
ansond 21:8487990a3baa 214 else {
ansond 5:a929d65eb385 215 this->logger()->log("ERROR: resource(GET) mask is munged (mask: 0x%x)",this->m_res_mask);
ansond 0:b438482ebbfc 216 }
sam_grove 2:853f9ecc12df 217 } else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) {
sam_grove 2:853f9ecc12df 218 if(received_coap_ptr->payload_len > 0) {
ansond 0:b438482ebbfc 219 // process the PUT if we have registered a callback for it...
ansond 0:b438482ebbfc 220 if ((this->m_res_mask&SN_GRS_PUT_ALLOWED) != 0) {
ansond 28:8fd9336edeb9 221 // put() delivers values as std::string
ansond 0:b438482ebbfc 222 string value = this->coapDataToString(received_coap_ptr->payload_ptr,received_coap_ptr->payload_len);
sam_grove 2:853f9ecc12df 223
ansond 0:b438482ebbfc 224 // call the resource put() to set the resource value
ansond 5:a929d65eb385 225 this->logger()->log("Calling resource(PUT) with [%s]=[%s]...",key.c_str(),value.c_str());
ansond 0:b438482ebbfc 226 this->put(value);
sam_grove 2:853f9ecc12df 227
ansond 0:b438482ebbfc 228 // build out the response and send...
ansond 5:a929d65eb385 229 this->logger()->log("resource(PUT) completed for [%s]...",key.c_str());
ansond 21:8487990a3baa 230 coap_res_ptr = sn_coap_build_response(received_coap_ptr,COAP_MSG_CODE_RESPONSE_CHANGED);
ansond 21:8487990a3baa 231 sn_nsdl_send_coap_message(address,coap_res_ptr);
sam_grove 2:853f9ecc12df 232 } else {
ansond 5:a929d65eb385 233 this->logger()->log("ERROR: resource(PUT) mask is munged (mask: 0x%x)",this->m_res_mask);
ansond 0:b438482ebbfc 234 }
sam_grove 2:853f9ecc12df 235 } else {
ansond 5:a929d65eb385 236 this->logger()->log("ERROR: Binder(PUT) payload is NULL...");
ansond 0:b438482ebbfc 237 }
ansond 21:8487990a3baa 238 } else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_POST) {
ansond 21:8487990a3baa 239 if(received_coap_ptr->payload_len > 0) {
ansond 21:8487990a3baa 240 // process the POST if we have registered a callback for it...
ansond 21:8487990a3baa 241 if ((this->m_res_mask&SN_GRS_POST_ALLOWED) != 0) {
ansond 28:8fd9336edeb9 242 // post() delivers values as std::string
ansond 21:8487990a3baa 243 string value = this->coapDataToString(received_coap_ptr->payload_ptr,received_coap_ptr->payload_len);
ansond 21:8487990a3baa 244
ansond 21:8487990a3baa 245 // call the resource post() to set the resource value
ansond 21:8487990a3baa 246 this->logger()->log("Calling resource(POST) with [%s]=[%s]...",key.c_str(),value.c_str());
ansond 21:8487990a3baa 247 this->post(value);
ansond 21:8487990a3baa 248
ansond 21:8487990a3baa 249 // build out the response and send...
ansond 21:8487990a3baa 250 this->logger()->log("resource(POST) completed for [%s]...",key.c_str());
ansond 21:8487990a3baa 251 coap_res_ptr = sn_coap_build_response(received_coap_ptr,COAP_MSG_CODE_RESPONSE_CHANGED);
ansond 21:8487990a3baa 252 sn_nsdl_send_coap_message(address,coap_res_ptr);
ansond 21:8487990a3baa 253 } else {
ansond 21:8487990a3baa 254 this->logger()->log("ERROR: resource(POST) mask is munged (mask: 0x%x)",this->m_res_mask);
ansond 21:8487990a3baa 255 }
ansond 21:8487990a3baa 256 } else {
ansond 21:8487990a3baa 257 this->logger()->log("ERROR: Binder(POST) payload is NULL...");
ansond 21:8487990a3baa 258 }
ansond 21:8487990a3baa 259 } else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_DELETE) {
ansond 21:8487990a3baa 260 if(received_coap_ptr->payload_len > 0) {
ansond 21:8487990a3baa 261 // process the DELETE if we have registered a callback for it...
ansond 21:8487990a3baa 262 if ((this->m_res_mask&SN_GRS_DELETE_ALLOWED) != 0) {
ansond 28:8fd9336edeb9 263 // del() delivers values as std::string
ansond 21:8487990a3baa 264 string value = this->coapDataToString(received_coap_ptr->payload_ptr,received_coap_ptr->payload_len);
ansond 21:8487990a3baa 265
ansond 21:8487990a3baa 266 // call the resource del() to set the resource value
ansond 21:8487990a3baa 267 this->logger()->log("Calling resource(DELETE) with [%s]=[%s]...",key.c_str(),value.c_str());
ansond 21:8487990a3baa 268 this->del(value);
ansond 21:8487990a3baa 269
ansond 21:8487990a3baa 270 // build out the response and send...
ansond 21:8487990a3baa 271 this->logger()->log("resource(DELETE) completed for [%s]...",key.c_str());
ansond 21:8487990a3baa 272 coap_res_ptr = sn_coap_build_response(received_coap_ptr,COAP_MSG_CODE_RESPONSE_CHANGED);
ansond 21:8487990a3baa 273 sn_nsdl_send_coap_message(address,coap_res_ptr);
ansond 21:8487990a3baa 274 } else {
ansond 21:8487990a3baa 275 this->logger()->log("ERROR: resource(DELETE) mask is munged (mask: 0x%x)",this->m_res_mask);
ansond 21:8487990a3baa 276 }
ansond 21:8487990a3baa 277 } else {
ansond 21:8487990a3baa 278 this->logger()->log("ERROR: Binder(DELETE) payload is NULL...");
ansond 21:8487990a3baa 279 }
ansond 0:b438482ebbfc 280 }
ansond 0:b438482ebbfc 281
ansond 0:b438482ebbfc 282 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
ansond 22:192b598ba389 283
ansond 0:b438482ebbfc 284 return 0;
sam_grove 2:853f9ecc12df 285 }
sam_grove 2:853f9ecc12df 286
ansond 21:8487990a3baa 287 // send the notification
ansond 21:8487990a3baa 288 int DynamicResource::notify(const string data) {
ansond 21:8487990a3baa 289 return this->notify((uint8_t *)data.c_str(),(int)data.length());
ansond 21:8487990a3baa 290 }
ansond 21:8487990a3baa 291
ansond 21:8487990a3baa 292 // send the notification
ansond 21:8487990a3baa 293 int DynamicResource::notify(uint8_t *data,int data_length) {
ansond 27:eb6818ad257a 294 uint8_t *notify_data = NULL;
ansond 27:eb6818ad257a 295 int notify_data_length = 0;
ansond 27:eb6818ad257a 296
ansond 27:eb6818ad257a 297 // convert the string from the GET to something suitable for CoAP payloads
ansond 27:eb6818ad257a 298 if (this->getDataWrapper() != NULL) {
ansond 27:eb6818ad257a 299 // wrap the data...
ansond 27:eb6818ad257a 300 this->getDataWrapper()->wrap((uint8_t *)data,data_length);
ansond 27:eb6818ad257a 301
ansond 27:eb6818ad257a 302 // announce (after wrap)
ansond 37:5de33aed0cac 303 //this->logger()->log("Notify payload [%s]...",this->getDataWrapper()->get());
ansond 27:eb6818ad257a 304
ansond 27:eb6818ad257a 305 // fill notify
ansond 27:eb6818ad257a 306 notify_data_length = this->getDataWrapper()->length();
ansond 27:eb6818ad257a 307 notify_data = this->getDataWrapper()->get();
ansond 27:eb6818ad257a 308 }
ansond 27:eb6818ad257a 309 else {
ansond 27:eb6818ad257a 310 // announce (no wrap)
ansond 37:5de33aed0cac 311 //this->logger()->log("Notify payload [%s]...",data);
ansond 27:eb6818ad257a 312
ansond 27:eb6818ad257a 313 // do not wrap the data...
ansond 27:eb6818ad257a 314 notify_data_length = data_length;
ansond 27:eb6818ad257a 315 notify_data = data;
ansond 27:eb6818ad257a 316 }
ansond 27:eb6818ad257a 317
ansond 27:eb6818ad257a 318 // send the observation...
ansond 27:eb6818ad257a 319 int status = sn_nsdl_send_observation_notification(this->m_obs_token_ptr,this->m_obs_token_len,notify_data,notify_data_length,&this->m_obs_number,1,COAP_MSG_TYPE_NON_CONFIRMABLE,0);
ansond 21:8487990a3baa 320 if (status == 0) {
ansond 21:8487990a3baa 321 this->logger()->log("ERROR: resource(NOTIFY) send failed...");
ansond 21:8487990a3baa 322 }
ansond 48:4b9ee3e32f93 323 else {
ansond 48:4b9ee3e32f93 324 ++(this->m_obs_number);
ansond 48:4b9ee3e32f93 325 }
ansond 27:eb6818ad257a 326
ansond 27:eb6818ad257a 327 // return our status
ansond 21:8487990a3baa 328 return status;
ansond 21:8487990a3baa 329 }
ansond 21:8487990a3baa 330
sam_grove 2:853f9ecc12df 331 // default PUT (does nothing)
sam_grove 2:853f9ecc12df 332 void DynamicResource::put(const string value)
sam_grove 2:853f9ecc12df 333 {
sam_grove 2:853f9ecc12df 334 // not used by default
sam_grove 2:853f9ecc12df 335 ;
sam_grove 2:853f9ecc12df 336 }
sam_grove 2:853f9ecc12df 337
ansond 21:8487990a3baa 338 // default POST (does nothing)
ansond 21:8487990a3baa 339 void DynamicResource::post(const string value)
ansond 21:8487990a3baa 340 {
ansond 21:8487990a3baa 341 // not used by default
ansond 21:8487990a3baa 342 ;
ansond 21:8487990a3baa 343 }
ansond 21:8487990a3baa 344
ansond 21:8487990a3baa 345 // default DELETE (does nothing)
ansond 21:8487990a3baa 346 void DynamicResource::del(const string value)
ansond 21:8487990a3baa 347 {
ansond 21:8487990a3baa 348 // not used by default
ansond 21:8487990a3baa 349 ;
ansond 21:8487990a3baa 350 }
ansond 21:8487990a3baa 351
ansond 30:113c2a1d8db2 352 // default observe behavior
ansond 30:113c2a1d8db2 353 void DynamicResource::observe() {
ansond 30:113c2a1d8db2 354 if (this->m_observable == true) {
ansond 30:113c2a1d8db2 355 this->notify(this->get());
ansond 30:113c2a1d8db2 356 }
ansond 30:113c2a1d8db2 357 }
ansond 30:113c2a1d8db2 358
ansond 31:bacc63106754 359 // set the observer pointer
ansond 31:bacc63106754 360 void DynamicResource::setObserver(void *observer) {
ansond 31:bacc63106754 361 this->m_observer = observer;
ansond 31:bacc63106754 362 }
ansond 31:bacc63106754 363
sam_grove 2:853f9ecc12df 364 // convenience method to get the URI from its buffer field...
sam_grove 2:853f9ecc12df 365 string DynamicResource::coapDataToString(uint8_t *coap_data_ptr,int coap_data_ptr_length)
sam_grove 2:853f9ecc12df 366 {
ansond 0:b438482ebbfc 367 if (coap_data_ptr != NULL && coap_data_ptr_length > 0) {
ansond 24:a6915e19814e 368 if (this->getDataWrapper() != NULL) {
ansond 24:a6915e19814e 369 // unwrap the data...
ansond 24:a6915e19814e 370 this->getDataWrapper()->unwrap(coap_data_ptr,coap_data_ptr_length);
ansond 24:a6915e19814e 371 char *buf = (char *)this->getDataWrapper()->get(); // assumes data is null terminated in DataWrapper...
ansond 24:a6915e19814e 372 return string(buf);
ansond 24:a6915e19814e 373 }
ansond 24:a6915e19814e 374 else {
ansond 24:a6915e19814e 375 // no unwrap of the data...
ansond 24:a6915e19814e 376 char buf[MAX_VALUE_BUFFER_LENGTH+1];
ansond 24:a6915e19814e 377 memset(buf,0,MAX_VALUE_BUFFER_LENGTH+1);
ansond 24:a6915e19814e 378 memcpy(buf,(char *)coap_data_ptr,coap_data_ptr_length);
ansond 24:a6915e19814e 379 return string(buf);
ansond 24:a6915e19814e 380 }
ansond 0:b438482ebbfc 381 }
ansond 0:b438482ebbfc 382 return string("");
sam_grove 2:853f9ecc12df 383 }