custom for >5 resources

Fork of mbedConnectorInterface by Doug Anson

Committer:
michaeljkoster
Date:
Tue Apr 14 02:51:23 2015 +0000
Revision:
50:1a9e2184945e
Parent:
48:4b9ee3e32f93
Child:
51:b308658817e5
add max-age internal support with default 60s

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