custom for >5 resources

Fork of mbedConnectorInterface by Doug Anson

Committer:
ansond
Date:
Fri Apr 10 06:09:34 2015 +0000
Revision:
37:5de33aed0cac
Parent:
36:1c6c45584c13
Child:
48:4b9ee3e32f93
removed logging in notify as it was messing up ISR based endpoints

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 36:1c6c45584c13 31 // Leaving this disabled until the CoAP draft spec is further along...
ansond 36:1c6c45584c13 32 #ifdef GET_ENABLE_DISABLE_OBS
ansond 36:1c6c45584c13 33 // GET verb can Start/Stop Observations...
ansond 36:1c6c45584c13 34 #define START_OBS 0
ansond 36:1c6c45584c13 35 #define STOP_OBS 1
ansond 36:1c6c45584c13 36 #endif
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 36:1c6c45584c13 178 // Leaving this disabled until the CoAP draft spec is further along...
ansond 36:1c6c45584c13 179 #ifdef GET_ENABLE_DISABLE_OBS
ansond 36:1c6c45584c13 180 // ResourceObserver
ansond 36:1c6c45584c13 181 ResourceObserver *observer = (ResourceObserver *)this->m_observer;
ansond 36:1c6c45584c13 182
ansond 31:bacc63106754 183 // get observe start/stop value from received options list pointer
ansond 31:bacc63106754 184 uint8_t OBS_command = *received_coap_ptr->options_list_ptr->observe_ptr;
ansond 31:bacc63106754 185 if (OBS_command == START_OBS) {
ansond 36:1c6c45584c13 186 coap_res_ptr->options_list_ptr->observe_ptr = &this->m_obs_number; // see nullify note below...
ansond 31:bacc63106754 187 coap_res_ptr->options_list_ptr->observe_len = 1;
ansond 31:bacc63106754 188 this->m_obs_number++;
ansond 31:bacc63106754 189 if (observer != NULL) observer->beginObservation();
ansond 31:bacc63106754 190 }
ansond 31:bacc63106754 191 if (OBS_command == STOP_OBS) {
ansond 31:bacc63106754 192 if (observer != NULL) observer->stopObservation();
ansond 31:bacc63106754 193 }
ansond 36:1c6c45584c13 194 #else
ansond 36:1c6c45584c13 195 // simply fill in the observation requirements...
ansond 36:1c6c45584c13 196 coap_res_ptr->options_list_ptr->observe_ptr = &this->m_obs_number; // see nullify note below...
ansond 36:1c6c45584c13 197 coap_res_ptr->options_list_ptr->observe_len = 1;
ansond 36:1c6c45584c13 198 this->m_obs_number++;
ansond 36:1c6c45584c13 199 #endif
ansond 21:8487990a3baa 200 }
sam_grove 2:853f9ecc12df 201
sam_grove 2:853f9ecc12df 202 // build out the response and send...
ansond 21:8487990a3baa 203 sn_nsdl_send_coap_message(address,coap_res_ptr);
ansond 22:192b598ba389 204
ansond 22:192b598ba389 205 //
ansond 22:192b598ba389 206 // nullify note:
ansond 22:192b598ba389 207 //
ansond 22:192b598ba389 208 // because our obs_number (assigned to observe_ptr) is part of this object instance, we dont
ansond 22:192b598ba389 209 // want to have the underlying free() try to free it... to just nullify here
ansond 22:192b598ba389 210 //
ansond 22:192b598ba389 211 if (coap_res_ptr->options_list_ptr) coap_res_ptr->options_list_ptr->observe_ptr = 0;
ansond 21:8487990a3baa 212 }
ansond 21:8487990a3baa 213 else {
ansond 5:a929d65eb385 214 this->logger()->log("ERROR: resource(GET) mask is munged (mask: 0x%x)",this->m_res_mask);
ansond 0:b438482ebbfc 215 }
sam_grove 2:853f9ecc12df 216 } else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_PUT) {
sam_grove 2:853f9ecc12df 217 if(received_coap_ptr->payload_len > 0) {
ansond 0:b438482ebbfc 218 // process the PUT if we have registered a callback for it...
ansond 0:b438482ebbfc 219 if ((this->m_res_mask&SN_GRS_PUT_ALLOWED) != 0) {
ansond 28:8fd9336edeb9 220 // put() delivers values as std::string
ansond 0:b438482ebbfc 221 string value = this->coapDataToString(received_coap_ptr->payload_ptr,received_coap_ptr->payload_len);
sam_grove 2:853f9ecc12df 222
ansond 0:b438482ebbfc 223 // call the resource put() to set the resource value
ansond 5:a929d65eb385 224 this->logger()->log("Calling resource(PUT) with [%s]=[%s]...",key.c_str(),value.c_str());
ansond 0:b438482ebbfc 225 this->put(value);
sam_grove 2:853f9ecc12df 226
ansond 0:b438482ebbfc 227 // build out the response and send...
ansond 5:a929d65eb385 228 this->logger()->log("resource(PUT) completed for [%s]...",key.c_str());
ansond 21:8487990a3baa 229 coap_res_ptr = sn_coap_build_response(received_coap_ptr,COAP_MSG_CODE_RESPONSE_CHANGED);
ansond 21:8487990a3baa 230 sn_nsdl_send_coap_message(address,coap_res_ptr);
sam_grove 2:853f9ecc12df 231 } else {
ansond 5:a929d65eb385 232 this->logger()->log("ERROR: resource(PUT) mask is munged (mask: 0x%x)",this->m_res_mask);
ansond 0:b438482ebbfc 233 }
sam_grove 2:853f9ecc12df 234 } else {
ansond 5:a929d65eb385 235 this->logger()->log("ERROR: Binder(PUT) payload is NULL...");
ansond 0:b438482ebbfc 236 }
ansond 21:8487990a3baa 237 } else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_POST) {
ansond 21:8487990a3baa 238 if(received_coap_ptr->payload_len > 0) {
ansond 21:8487990a3baa 239 // process the POST if we have registered a callback for it...
ansond 21:8487990a3baa 240 if ((this->m_res_mask&SN_GRS_POST_ALLOWED) != 0) {
ansond 28:8fd9336edeb9 241 // post() delivers values as std::string
ansond 21:8487990a3baa 242 string value = this->coapDataToString(received_coap_ptr->payload_ptr,received_coap_ptr->payload_len);
ansond 21:8487990a3baa 243
ansond 21:8487990a3baa 244 // call the resource post() to set the resource value
ansond 21:8487990a3baa 245 this->logger()->log("Calling resource(POST) with [%s]=[%s]...",key.c_str(),value.c_str());
ansond 21:8487990a3baa 246 this->post(value);
ansond 21:8487990a3baa 247
ansond 21:8487990a3baa 248 // build out the response and send...
ansond 21:8487990a3baa 249 this->logger()->log("resource(POST) completed for [%s]...",key.c_str());
ansond 21:8487990a3baa 250 coap_res_ptr = sn_coap_build_response(received_coap_ptr,COAP_MSG_CODE_RESPONSE_CHANGED);
ansond 21:8487990a3baa 251 sn_nsdl_send_coap_message(address,coap_res_ptr);
ansond 21:8487990a3baa 252 } else {
ansond 21:8487990a3baa 253 this->logger()->log("ERROR: resource(POST) mask is munged (mask: 0x%x)",this->m_res_mask);
ansond 21:8487990a3baa 254 }
ansond 21:8487990a3baa 255 } else {
ansond 21:8487990a3baa 256 this->logger()->log("ERROR: Binder(POST) payload is NULL...");
ansond 21:8487990a3baa 257 }
ansond 21:8487990a3baa 258 } else if(received_coap_ptr->msg_code == COAP_MSG_CODE_REQUEST_DELETE) {
ansond 21:8487990a3baa 259 if(received_coap_ptr->payload_len > 0) {
ansond 21:8487990a3baa 260 // process the DELETE if we have registered a callback for it...
ansond 21:8487990a3baa 261 if ((this->m_res_mask&SN_GRS_DELETE_ALLOWED) != 0) {
ansond 28:8fd9336edeb9 262 // del() delivers values as std::string
ansond 21:8487990a3baa 263 string value = this->coapDataToString(received_coap_ptr->payload_ptr,received_coap_ptr->payload_len);
ansond 21:8487990a3baa 264
ansond 21:8487990a3baa 265 // call the resource del() to set the resource value
ansond 21:8487990a3baa 266 this->logger()->log("Calling resource(DELETE) with [%s]=[%s]...",key.c_str(),value.c_str());
ansond 21:8487990a3baa 267 this->del(value);
ansond 21:8487990a3baa 268
ansond 21:8487990a3baa 269 // build out the response and send...
ansond 21:8487990a3baa 270 this->logger()->log("resource(DELETE) completed for [%s]...",key.c_str());
ansond 21:8487990a3baa 271 coap_res_ptr = sn_coap_build_response(received_coap_ptr,COAP_MSG_CODE_RESPONSE_CHANGED);
ansond 21:8487990a3baa 272 sn_nsdl_send_coap_message(address,coap_res_ptr);
ansond 21:8487990a3baa 273 } else {
ansond 21:8487990a3baa 274 this->logger()->log("ERROR: resource(DELETE) mask is munged (mask: 0x%x)",this->m_res_mask);
ansond 21:8487990a3baa 275 }
ansond 21:8487990a3baa 276 } else {
ansond 21:8487990a3baa 277 this->logger()->log("ERROR: Binder(DELETE) payload is NULL...");
ansond 21:8487990a3baa 278 }
ansond 0:b438482ebbfc 279 }
ansond 0:b438482ebbfc 280
ansond 0:b438482ebbfc 281 sn_coap_parser_release_allocated_coap_msg_mem(coap_res_ptr);
ansond 22:192b598ba389 282
ansond 0:b438482ebbfc 283 return 0;
sam_grove 2:853f9ecc12df 284 }
sam_grove 2:853f9ecc12df 285
ansond 21:8487990a3baa 286 // send the notification
ansond 21:8487990a3baa 287 int DynamicResource::notify(const string data) {
ansond 21:8487990a3baa 288 return this->notify((uint8_t *)data.c_str(),(int)data.length());
ansond 21:8487990a3baa 289 }
ansond 21:8487990a3baa 290
ansond 21:8487990a3baa 291 // send the notification
ansond 21:8487990a3baa 292 int DynamicResource::notify(uint8_t *data,int data_length) {
ansond 27:eb6818ad257a 293 uint8_t *notify_data = NULL;
ansond 27:eb6818ad257a 294 int notify_data_length = 0;
ansond 27:eb6818ad257a 295
ansond 27:eb6818ad257a 296 // convert the string from the GET to something suitable for CoAP payloads
ansond 27:eb6818ad257a 297 if (this->getDataWrapper() != NULL) {
ansond 27:eb6818ad257a 298 // wrap the data...
ansond 27:eb6818ad257a 299 this->getDataWrapper()->wrap((uint8_t *)data,data_length);
ansond 27:eb6818ad257a 300
ansond 27:eb6818ad257a 301 // announce (after wrap)
ansond 37:5de33aed0cac 302 //this->logger()->log("Notify payload [%s]...",this->getDataWrapper()->get());
ansond 27:eb6818ad257a 303
ansond 27:eb6818ad257a 304 // fill notify
ansond 27:eb6818ad257a 305 notify_data_length = this->getDataWrapper()->length();
ansond 27:eb6818ad257a 306 notify_data = this->getDataWrapper()->get();
ansond 27:eb6818ad257a 307 }
ansond 27:eb6818ad257a 308 else {
ansond 27:eb6818ad257a 309 // announce (no wrap)
ansond 37:5de33aed0cac 310 //this->logger()->log("Notify payload [%s]...",data);
ansond 27:eb6818ad257a 311
ansond 27:eb6818ad257a 312 // do not wrap the data...
ansond 27:eb6818ad257a 313 notify_data_length = data_length;
ansond 27:eb6818ad257a 314 notify_data = data;
ansond 27:eb6818ad257a 315 }
ansond 27:eb6818ad257a 316
ansond 27:eb6818ad257a 317 // send the observation...
ansond 27:eb6818ad257a 318 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 319 if (status == 0) {
ansond 21:8487990a3baa 320 this->logger()->log("ERROR: resource(NOTIFY) send failed...");
ansond 21:8487990a3baa 321 }
ansond 27:eb6818ad257a 322
ansond 27:eb6818ad257a 323 // return our status
ansond 21:8487990a3baa 324 return status;
ansond 21:8487990a3baa 325 }
ansond 21:8487990a3baa 326
sam_grove 2:853f9ecc12df 327 // default PUT (does nothing)
sam_grove 2:853f9ecc12df 328 void DynamicResource::put(const string value)
sam_grove 2:853f9ecc12df 329 {
sam_grove 2:853f9ecc12df 330 // not used by default
sam_grove 2:853f9ecc12df 331 ;
sam_grove 2:853f9ecc12df 332 }
sam_grove 2:853f9ecc12df 333
ansond 21:8487990a3baa 334 // default POST (does nothing)
ansond 21:8487990a3baa 335 void DynamicResource::post(const string value)
ansond 21:8487990a3baa 336 {
ansond 21:8487990a3baa 337 // not used by default
ansond 21:8487990a3baa 338 ;
ansond 21:8487990a3baa 339 }
ansond 21:8487990a3baa 340
ansond 21:8487990a3baa 341 // default DELETE (does nothing)
ansond 21:8487990a3baa 342 void DynamicResource::del(const string value)
ansond 21:8487990a3baa 343 {
ansond 21:8487990a3baa 344 // not used by default
ansond 21:8487990a3baa 345 ;
ansond 21:8487990a3baa 346 }
ansond 21:8487990a3baa 347
ansond 30:113c2a1d8db2 348 // default observe behavior
ansond 30:113c2a1d8db2 349 void DynamicResource::observe() {
ansond 30:113c2a1d8db2 350 if (this->m_observable == true) {
ansond 30:113c2a1d8db2 351 this->notify(this->get());
ansond 30:113c2a1d8db2 352 }
ansond 30:113c2a1d8db2 353 }
ansond 30:113c2a1d8db2 354
ansond 31:bacc63106754 355 // set the observer pointer
ansond 31:bacc63106754 356 void DynamicResource::setObserver(void *observer) {
ansond 31:bacc63106754 357 this->m_observer = observer;
ansond 31:bacc63106754 358 }
ansond 31:bacc63106754 359
sam_grove 2:853f9ecc12df 360 // convenience method to get the URI from its buffer field...
sam_grove 2:853f9ecc12df 361 string DynamicResource::coapDataToString(uint8_t *coap_data_ptr,int coap_data_ptr_length)
sam_grove 2:853f9ecc12df 362 {
ansond 0:b438482ebbfc 363 if (coap_data_ptr != NULL && coap_data_ptr_length > 0) {
ansond 24:a6915e19814e 364 if (this->getDataWrapper() != NULL) {
ansond 24:a6915e19814e 365 // unwrap the data...
ansond 24:a6915e19814e 366 this->getDataWrapper()->unwrap(coap_data_ptr,coap_data_ptr_length);
ansond 24:a6915e19814e 367 char *buf = (char *)this->getDataWrapper()->get(); // assumes data is null terminated in DataWrapper...
ansond 24:a6915e19814e 368 return string(buf);
ansond 24:a6915e19814e 369 }
ansond 24:a6915e19814e 370 else {
ansond 24:a6915e19814e 371 // no unwrap of the data...
ansond 24:a6915e19814e 372 char buf[MAX_VALUE_BUFFER_LENGTH+1];
ansond 24:a6915e19814e 373 memset(buf,0,MAX_VALUE_BUFFER_LENGTH+1);
ansond 24:a6915e19814e 374 memcpy(buf,(char *)coap_data_ptr,coap_data_ptr_length);
ansond 24:a6915e19814e 375 return string(buf);
ansond 24:a6915e19814e 376 }
ansond 0:b438482ebbfc 377 }
ansond 0:b438482ebbfc 378 return string("");
sam_grove 2:853f9ecc12df 379 }