mbed Connector Interface simplification API on top of mbed-client

Fork of mbedConnectorInterfaceV3 by Doug Anson

NOTE:

This repo has been replaced with https://github.com/ARMmbed/mbedConnectorInterface. No further updates will occur with this repo. Please use the github repo instead. Thanks!

Committer:
ansond
Date:
Fri Jun 10 17:09:56 2016 +0000
Revision:
15:c11dbe4d354c
Parent:
13:9edad7677211
Child:
16:dffa38c3340f
updates and sync with v3

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 13:9edad7677211 1 /**
ansond 13:9edad7677211 2 * @file DynamicResource.cpp
ansond 13:9edad7677211 3 * @brief mbed CoAP Endpoint Dynamic Resource class
ansond 13:9edad7677211 4 * @author Doug Anson/Chris Paola
ansond 13:9edad7677211 5 * @version 1.0
ansond 13:9edad7677211 6 * @see
ansond 13:9edad7677211 7 *
ansond 13:9edad7677211 8 * Copyright (c) 2014
ansond 13:9edad7677211 9 *
ansond 13:9edad7677211 10 * Licensed under the Apache License, Version 2.0 (the "License");
ansond 13:9edad7677211 11 * you may not use this file except in compliance with the License.
ansond 13:9edad7677211 12 * You may obtain a copy of the License at
ansond 13:9edad7677211 13 *
ansond 13:9edad7677211 14 * http://www.apache.org/licenses/LICENSE-2.0
ansond 13:9edad7677211 15 *
ansond 13:9edad7677211 16 * Unless required by applicable law or agreed to in writing, software
ansond 13:9edad7677211 17 * distributed under the License is distributed on an "AS IS" BASIS,
ansond 13:9edad7677211 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ansond 13:9edad7677211 19 * See the License for the specific language governing permissions and
ansond 13:9edad7677211 20 * limitations under the License.
ansond 13:9edad7677211 21 */
ansond 13:9edad7677211 22
ansond 13:9edad7677211 23 #include "mbed-connector-interface/DynamicResource.h"
ansond 13:9edad7677211 24
ansond 13:9edad7677211 25 // ResourceObserver help
ansond 13:9edad7677211 26 #include "mbed-connector-interface/ResourceObserver.h"
ansond 13:9edad7677211 27
ansond 13:9edad7677211 28 // Options enablement
ansond 13:9edad7677211 29 #include "mbed-connector-interface/Options.h"
ansond 13:9edad7677211 30
ansond 13:9edad7677211 31 // Endpoint
ansond 13:9edad7677211 32 #include "mbed-connector-interface/ConnectorEndpoint.h"
ansond 13:9edad7677211 33
ansond 13:9edad7677211 34 // GET option that can be used to Start/Stop Observations...
ansond 13:9edad7677211 35 #define START_OBS 0
ansond 13:9edad7677211 36 #define STOP_OBS 1
ansond 13:9edad7677211 37
ansond 13:9edad7677211 38 // MaxAge support for each DynamicResource
ansond 13:9edad7677211 39 #define DEFAULT_MAXAGE 60
ansond 13:9edad7677211 40
ansond 13:9edad7677211 41 // ContentFormat defaults for each DynamicResource
ansond 13:9edad7677211 42 #define DEFAULT_CONTENT_FORMAT 0
ansond 13:9edad7677211 43
ansond 13:9edad7677211 44 // default constructor
ansond 13:9edad7677211 45 DynamicResource::DynamicResource(const Logger *logger,const char *obj_name,const char *res_name,const char *res_type,uint8_t res_mask,const bool observable) : Resource<string>(logger,string(obj_name),string(res_name),string(""))
ansond 13:9edad7677211 46 {
ansond 13:9edad7677211 47 this->m_res_type = string(res_type);
ansond 13:9edad7677211 48 this->m_observable = observable;
ansond 13:9edad7677211 49 this->m_res_mask = res_mask;
ansond 13:9edad7677211 50 this->m_obs_number = 0;
ansond 13:9edad7677211 51 this->m_data_wrapper = NULL;
ansond 13:9edad7677211 52 this->m_observer = NULL;
ansond 13:9edad7677211 53 this->m_maxage = DEFAULT_MAXAGE;
ansond 13:9edad7677211 54 this->m_content_format = DEFAULT_CONTENT_FORMAT;
ansond 13:9edad7677211 55 this->m_object = NULL;
ansond 13:9edad7677211 56 this->m_op_processor = NULL;
ansond 13:9edad7677211 57 }
ansond 13:9edad7677211 58
ansond 13:9edad7677211 59 // constructor (input initial value)
ansond 13:9edad7677211 60 DynamicResource::DynamicResource(const Logger *logger,const char *obj_name,const char *res_name,const char *res_type,const string value,uint8_t res_mask,const bool observable) : Resource<string>(logger,string(obj_name),string(res_name),value)
ansond 13:9edad7677211 61 {
ansond 13:9edad7677211 62 this->m_res_type = string(res_type);
ansond 13:9edad7677211 63 this->m_observable = observable;
ansond 13:9edad7677211 64 this->m_res_mask = res_mask;
ansond 13:9edad7677211 65 this->m_obs_number = 0;
ansond 13:9edad7677211 66 this->m_data_wrapper = NULL;
ansond 13:9edad7677211 67 this->m_observer = NULL;
ansond 13:9edad7677211 68 this->m_maxage = DEFAULT_MAXAGE;
ansond 13:9edad7677211 69 this->m_content_format = DEFAULT_CONTENT_FORMAT;
ansond 13:9edad7677211 70 this->m_object = NULL;
ansond 13:9edad7677211 71 this->m_op_processor = NULL;
ansond 13:9edad7677211 72 }
ansond 13:9edad7677211 73
ansond 13:9edad7677211 74 // constructor (strings)
ansond 13:9edad7677211 75 DynamicResource::DynamicResource(const Logger *logger,const string obj_name,const string res_name,const string res_type,const string value,uint8_t res_mask,const bool observable) : Resource<string>(logger,obj_name,res_name,value)
ansond 13:9edad7677211 76 {
ansond 13:9edad7677211 77 this->m_res_type = res_type;
ansond 13:9edad7677211 78 this->m_observable = observable;
ansond 13:9edad7677211 79 this->m_res_mask = res_mask;
ansond 13:9edad7677211 80 this->m_obs_number = 0;
ansond 13:9edad7677211 81 this->m_data_wrapper = NULL;
ansond 13:9edad7677211 82 this->m_observer = NULL;
ansond 13:9edad7677211 83 this->m_maxage = DEFAULT_MAXAGE;
ansond 13:9edad7677211 84 this->m_content_format = DEFAULT_CONTENT_FORMAT;
ansond 13:9edad7677211 85 this->m_object = NULL;
ansond 13:9edad7677211 86 this->m_op_processor = NULL;
ansond 13:9edad7677211 87 }
ansond 13:9edad7677211 88
ansond 13:9edad7677211 89 // copy constructor
ansond 13:9edad7677211 90 DynamicResource::DynamicResource(const DynamicResource &resource) : Resource<string>((const Resource<string> &)resource)
ansond 13:9edad7677211 91 {
ansond 13:9edad7677211 92 this->m_res_type = resource.m_res_type;
ansond 13:9edad7677211 93 this->m_observable = resource.m_observable;
ansond 13:9edad7677211 94 this->m_res_mask = resource.m_res_mask;
ansond 13:9edad7677211 95 this->m_obs_number = resource.m_obs_number;
ansond 13:9edad7677211 96 this->m_data_wrapper = resource.m_data_wrapper;
ansond 13:9edad7677211 97 this->m_observer = resource.m_observer;
ansond 13:9edad7677211 98 this->m_maxage = resource.m_maxage;
ansond 13:9edad7677211 99 this->m_content_format = resource.m_content_format;
ansond 13:9edad7677211 100 this->m_object = resource.m_object;
ansond 13:9edad7677211 101 this->m_op_processor = resource.m_op_processor;
ansond 13:9edad7677211 102 }
ansond 13:9edad7677211 103
ansond 13:9edad7677211 104 // destructor
ansond 13:9edad7677211 105 DynamicResource::~DynamicResource() {
ansond 13:9edad7677211 106 }
ansond 13:9edad7677211 107
ansond 13:9edad7677211 108 // bind CoAP Resource...
ansond 13:9edad7677211 109 M2MObject *DynamicResource::bind(void *p) {
ansond 13:9edad7677211 110 if (p != NULL) {
ansond 13:9edad7677211 111 this->m_object = M2MInterfaceFactory::create_object(this->getObjName().c_str());
ansond 13:9edad7677211 112 if (this->m_object != NULL) {
ansond 13:9edad7677211 113 this->m_obj_instance = this->m_object->create_object_instance();
ansond 13:9edad7677211 114 if (this->m_obj_instance != NULL) {
ansond 13:9edad7677211 115 this->m_res = this->m_obj_instance->create_dynamic_resource(this->getResName().c_str(),this->m_res_type.c_str(),M2MResourceInstance::STRING,this->m_observable);
ansond 13:9edad7677211 116 if (this->m_res != NULL) {
ansond 13:9edad7677211 117 // perform an initial get() to initialize our data value
ansond 13:9edad7677211 118 this->setValue(this->get());
ansond 13:9edad7677211 119
ansond 13:9edad7677211 120 // now record the data value
ansond 13:9edad7677211 121 if (this->getDataWrapper() != NULL) {
ansond 13:9edad7677211 122 // wrap the data...
ansond 13:9edad7677211 123 this->getDataWrapper()->wrap((uint8_t *)this->getValue().c_str(),(int)this->getValue().size());
ansond 13:9edad7677211 124 this->m_res->set_operation((M2MBase::Operation)this->m_res_mask);
ansond 13:9edad7677211 125 this->m_res->set_value( this->getDataWrapper()->get(),(uint8_t)this->getDataWrapper()->length());
ansond 13:9edad7677211 126 this->logger()->log("%s: [%s] value: [%s] bound (observable: %d)",this->m_res_type.c_str(),this->getFullName().c_str(),this->getDataWrapper()->get(),this->m_observable);
ansond 13:9edad7677211 127 this->m_op_processor = (void *)p;
ansond 13:9edad7677211 128 }
ansond 13:9edad7677211 129 else {
ansond 13:9edad7677211 130 // do not wrap the data...
ansond 13:9edad7677211 131 this->m_res->set_operation((M2MBase::Operation)this->m_res_mask);
ansond 13:9edad7677211 132 this->m_res->set_value((uint8_t *)this->getValue().c_str(),(uint8_t)this->getValue().size());
ansond 13:9edad7677211 133 this->logger()->log("%s: [%s] value: [%s] bound (observable: %d)",this->m_res_type.c_str(),this->getFullName().c_str(),this->getValue().c_str(),this->m_observable);
ansond 13:9edad7677211 134 this->m_op_processor = (void *)p;
ansond 13:9edad7677211 135 }
ansond 13:9edad7677211 136
ansond 13:9edad7677211 137 // For POST-enabled RESOURCES (only...), we must add a callback
ansond 13:9edad7677211 138 if ((this->m_res_mask & M2MBase::POST_ALLOWED) != 0) {
ansond 15:c11dbe4d354c 139 // add a callback for the execute function...we will just direct through process()...
ansond 15:c11dbe4d354c 140 //this->logger()->log("DynamicResource::bind(): Setting up POST execute callback function");
ansond 15:c11dbe4d354c 141 this->m_res->set_execute_function(execute_callback(this, &DynamicResource::process_resource_post));
ansond 13:9edad7677211 142 }
ansond 13:9edad7677211 143
ansond 13:9edad7677211 144 // DISABLE for now...
ansond 13:9edad7677211 145 #if 0
ansond 13:9edad7677211 146 // For DELETE-enabled RESOURCES (only...), we must add a callback
ansond 13:9edad7677211 147 if ((this->m_res_mask & M2MBase::DELETE_ALLOWED) != 0) {
ansond 15:c11dbe4d354c 148 // add a callback for the execute function...we will just direct through process()...
ansond 15:c11dbe4d354c 149 //this->logger()->log("DynamicResource::bind(): Setting up DELETE execute callback function");
ansond 15:c11dbe4d354c 150 this->m_res->set_execute_function(execute_callback(this, &DynamicResource::process_resource_delete));
ansond 13:9edad7677211 151 }
ansond 13:9edad7677211 152 #endif
ansond 13:9edad7677211 153 }
ansond 13:9edad7677211 154 else {
ansond 13:9edad7677211 155 // create_dynamic_resource() failed
ansond 13:9edad7677211 156 this->logger()->log("%s: Unable to create dynamic resource...",this->m_res_type.c_str());
ansond 13:9edad7677211 157 delete this->m_object;
ansond 13:9edad7677211 158 this->m_object = NULL;
ansond 13:9edad7677211 159 }
ansond 13:9edad7677211 160 }
ansond 13:9edad7677211 161 else {
ansond 13:9edad7677211 162 // create_object_instance() failed...
ansond 13:9edad7677211 163 this->logger()->log("%s: Unable to create object instance...",this->m_res_type.c_str());
ansond 13:9edad7677211 164 delete this->m_object;
ansond 13:9edad7677211 165 this->m_object = NULL;
ansond 13:9edad7677211 166 }
ansond 13:9edad7677211 167 }
ansond 13:9edad7677211 168 else {
ansond 13:9edad7677211 169 // create_object() failed
ansond 13:9edad7677211 170 this->logger()->log("%s: Unable to create object...",this->m_res_type.c_str());
ansond 13:9edad7677211 171 }
ansond 13:9edad7677211 172 }
ansond 13:9edad7677211 173 else {
ansond 13:9edad7677211 174 this->logger()->log("%s: NULL value parameter in bind() request...",this->m_res_type.c_str());
ansond 13:9edad7677211 175 }
ansond 13:9edad7677211 176 return this->m_object;
ansond 13:9edad7677211 177 }
ansond 13:9edad7677211 178
ansond 13:9edad7677211 179 // get our M2MBase representation
ansond 13:9edad7677211 180 M2MBase *DynamicResource::getResource() {
ansond 13:9edad7677211 181 return (M2MBase *)this->m_res;
ansond 13:9edad7677211 182 }
ansond 13:9edad7677211 183
ansond 13:9edad7677211 184 // process inbound mbed-client POST message for a Resource
ansond 13:9edad7677211 185 void DynamicResource::process_resource_post(void *args) {
ansond 13:9edad7677211 186 // just call process() for POST and Resources...
ansond 13:9edad7677211 187 //this->logger()->log("DynamicResource::process_resource_post(): calling process(POST)");
ansond 13:9edad7677211 188 (void)this->process(M2MBase::POST_ALLOWED,this->m_res->base_type(),args);
ansond 13:9edad7677211 189 }
ansond 13:9edad7677211 190
ansond 13:9edad7677211 191 // process inbound mbed-client DELETE message for a Resource
ansond 13:9edad7677211 192 void DynamicResource::process_resource_delete(void *args) {
ansond 13:9edad7677211 193 // just call process() for DELETE and Resources...
ansond 13:9edad7677211 194 //this->logger()->log("DynamicResource::process_resource_delete(): calling process(DELETE)");
ansond 13:9edad7677211 195 (void)this->process(M2MBase::DELETE_ALLOWED,this->m_res->base_type(),args);
ansond 13:9edad7677211 196 }
ansond 0:1f1f55e73248 197
ansond 13:9edad7677211 198 // process inbound mbed-client message
ansond 13:9edad7677211 199 uint8_t DynamicResource::process(M2MBase::Operation op,M2MBase::BaseType type,void *args) {
ansond 13:9edad7677211 200 #if defined (HAS_EXECUTE_PARAMS)
ansond 13:9edad7677211 201 M2MResource::M2MExecuteParameter* param = NULL;
ansond 13:9edad7677211 202
ansond 13:9edad7677211 203 // cast args if present...
ansond 13:9edad7677211 204 if (args != NULL) {
ansond 13:9edad7677211 205 param = (M2MResource::M2MExecuteParameter*)args;
ansond 13:9edad7677211 206 }
ansond 13:9edad7677211 207 #endif
ansond 13:9edad7677211 208 // DEBUG
ansond 13:9edad7677211 209 //this->logger()->log("in %s::process() Operation=0x0%x Type=%x%x",this->m_res_type.c_str(),op,type);
ansond 13:9edad7677211 210
ansond 13:9edad7677211 211 // PUT() check
ansond 13:9edad7677211 212 if ((op & M2MBase::PUT_ALLOWED) != 0) {
ansond 13:9edad7677211 213 string value = this->coapDataToString(this->m_res->value(),this->m_res->value_length());
ansond 13:9edad7677211 214 this->logger()->log("%s: Calling resource(PUT) with [%s]=[%s]",this->m_res_type.c_str(),this->getFullName().c_str(),value.c_str());
ansond 13:9edad7677211 215 this->put(value.c_str());
ansond 13:9edad7677211 216 return 0;
ansond 13:9edad7677211 217 }
ansond 13:9edad7677211 218
ansond 13:9edad7677211 219 #if defined (HAS_EXECUTE_PARAMS)
ansond 13:9edad7677211 220 // POST() check
ansond 13:9edad7677211 221 if ((op & M2MBase::POST_ALLOWED) != 0) {
ansond 13:9edad7677211 222 string value;
ansond 13:9edad7677211 223 if (param != NULL) {
ansond 13:9edad7677211 224 // use parameters
ansond 13:9edad7677211 225 String object_name = param->get_argument_object_name();
ansond 13:9edad7677211 226 int instance_id = (int)param->get_argument_object_instance_id();
ansond 13:9edad7677211 227 String resource_name = param->get_argument_resource_name();
ansond 13:9edad7677211 228 value = this->coapDataToString(param->get_argument_value(),param->get_argument_value_length());
ansond 13:9edad7677211 229 this->logger()->log("%s: post() (resource: [%s/%d/%s] value: [%s]) invoked",this->m_res_type.c_str(),object_name.c_str(),instance_id,resource_name.c_str(),value.c_str());
ansond 13:9edad7677211 230 }
ansond 13:9edad7677211 231 else {
ansond 13:9edad7677211 232 // use the resource value itself
ansond 13:9edad7677211 233 value = this->coapDataToString(this->m_res->value(),this->m_res->value_length());
ansond 13:9edad7677211 234 this->logger()->log("%s: post() (resource: [%s] value: [%s] invoked",this->m_res_type.c_str(),this->getFullName().c_str(),value.c_str());
ansond 13:9edad7677211 235 }
ansond 13:9edad7677211 236
ansond 13:9edad7677211 237 // invoke
ansond 13:9edad7677211 238 this->post(args);
ansond 13:9edad7677211 239 return 0;
ansond 13:9edad7677211 240 }
ansond 13:9edad7677211 241 #else
ansond 13:9edad7677211 242 // POST() check
ansond 13:9edad7677211 243 if ((op & M2MBase::POST_ALLOWED) != 0) {
ansond 13:9edad7677211 244 string value = this->coapDataToString(this->m_res->value(),this->m_res->value_length());
ansond 13:9edad7677211 245 this->logger()->log("%s: Calling resource(POST) with [%s]=[%s]",this->m_res_type.c_str(),this->getFullName().c_str(),value.c_str());
ansond 13:9edad7677211 246 this->post((void *)value.c_str());
ansond 13:9edad7677211 247 return 0;
ansond 13:9edad7677211 248 }
ansond 13:9edad7677211 249 #endif
ansond 13:9edad7677211 250
ansond 13:9edad7677211 251 #if defined (HAS_EXECUTE_PARAMS)
ansond 13:9edad7677211 252 // DELETE() check
ansond 13:9edad7677211 253 if ((op & M2MBase::DELETE_ALLOWED) != 0) {
ansond 13:9edad7677211 254 if (param != NULL) {
ansond 13:9edad7677211 255 // use parameters
ansond 13:9edad7677211 256 String object_name = param->get_argument_object_name();
ansond 13:9edad7677211 257 int instance_id = (int)param->get_argument_object_instance_id();
ansond 13:9edad7677211 258 String resource_name = param->get_argument_resource_name();
ansond 13:9edad7677211 259 string value = this->coapDataToString(param->get_argument_value(),param->get_argument_value_length());
ansond 13:9edad7677211 260 this->logger()->log("%s: delete() (resource: [%s/%d/%s] value: [%s]) invoked",this->m_res_type.c_str(),object_name.c_str(),instance_id,resource_name.c_str(),value.c_str());
ansond 13:9edad7677211 261 }
ansond 13:9edad7677211 262 else {
ansond 13:9edad7677211 263 // use the resource value itself
ansond 13:9edad7677211 264 string value = this->coapDataToString(this->m_res->value(),this->m_res->value_length());
ansond 13:9edad7677211 265 this->logger()->log("%s: delete() (resource: [%s] value: [%s] invoked",this->m_res_type.c_str(),this->getFullName().c_str(),value.c_str());
ansond 13:9edad7677211 266 }
ansond 13:9edad7677211 267
ansond 13:9edad7677211 268 // invoke
ansond 13:9edad7677211 269 this->del(args);
ansond 13:9edad7677211 270 return 0;
ansond 13:9edad7677211 271 }
ansond 13:9edad7677211 272 #else
ansond 13:9edad7677211 273 // DELETE() check
ansond 13:9edad7677211 274 if ((op & M2MBase::DELETE_ALLOWED) != 0) {
ansond 13:9edad7677211 275 string value = this->coapDataToString(this->m_res->value(),this->m_res->value_length());
ansond 13:9edad7677211 276 this->logger()->log("%s: Calling resource(DELETE) with [%s]=[%s]",this->m_res_type.c_str(),this->getFullName().c_str(),value.c_str());
ansond 13:9edad7677211 277 this->del((void *)value.c_str());
ansond 13:9edad7677211 278 return 0;
ansond 13:9edad7677211 279 }
ansond 13:9edad7677211 280 #endif
ansond 13:9edad7677211 281
ansond 13:9edad7677211 282 // unknown type...
ansond 13:9edad7677211 283 this->logger()->log("%s: Unknown Operation (0x%x) for [%s]=[%s]... FAILED.",op,this->m_res_type.c_str(),this->getFullName().c_str(),this->m_res->value());
ansond 13:9edad7677211 284 return 1;
ansond 13:9edad7677211 285 }
ansond 13:9edad7677211 286
ansond 13:9edad7677211 287 // send the notification
ansond 13:9edad7677211 288 int DynamicResource::notify(const string data) {
ansond 13:9edad7677211 289 return this->notify((uint8_t *)data.c_str(),(int)data.length());
ansond 13:9edad7677211 290 }
ansond 13:9edad7677211 291
ansond 13:9edad7677211 292 // send the notification
ansond 13:9edad7677211 293 int DynamicResource::notify(uint8_t *data,int data_length) {
ansond 13:9edad7677211 294 uint8_t *notify_data = NULL;
ansond 13:9edad7677211 295 int notify_data_length = 0;
ansond 13:9edad7677211 296 int status = 0;
ansond 13:9edad7677211 297
ansond 13:9edad7677211 298 // convert the string from the GET to something suitable for CoAP payloads
ansond 13:9edad7677211 299 if (this->getDataWrapper() != NULL) {
ansond 13:9edad7677211 300 // wrap the data...
ansond 13:9edad7677211 301 this->getDataWrapper()->wrap((uint8_t *)data,data_length);
ansond 13:9edad7677211 302
ansond 13:9edad7677211 303 // announce (after wrap)
ansond 13:9edad7677211 304 //this->logger()->log("Notify payload [%s]...",this->getDataWrapper()->get());
ansond 13:9edad7677211 305
ansond 13:9edad7677211 306 // fill notify
ansond 13:9edad7677211 307 notify_data_length = this->getDataWrapper()->length();
ansond 13:9edad7677211 308 notify_data = this->getDataWrapper()->get();
ansond 13:9edad7677211 309 }
ansond 13:9edad7677211 310 else {
ansond 13:9edad7677211 311 // announce (no wrap)
ansond 13:9edad7677211 312 //this->logger()->log("Notify payload [%s]...",data);
ansond 13:9edad7677211 313
ansond 13:9edad7677211 314 // do not wrap the data...
ansond 13:9edad7677211 315 notify_data_length = data_length;
ansond 13:9edad7677211 316 notify_data = data;
ansond 13:9edad7677211 317 }
ansond 13:9edad7677211 318
ansond 13:9edad7677211 319 // update the resource
ansond 13:9edad7677211 320 this->m_res->set_value((uint8_t *)notify_data,(uint8_t)notify_data_length);
ansond 13:9edad7677211 321
ansond 13:9edad7677211 322 // return our status
ansond 13:9edad7677211 323 return status;
ansond 13:9edad7677211 324 }
ansond 13:9edad7677211 325
ansond 13:9edad7677211 326 // default PUT (does nothing)
ansond 13:9edad7677211 327 void DynamicResource::put(const string value)
ansond 13:9edad7677211 328 {
ansond 13:9edad7677211 329 // not used by default
ansond 13:9edad7677211 330 this->logger()->log("DynamicResource::put() invoked (NOOP)");
ansond 13:9edad7677211 331 }
ansond 13:9edad7677211 332
ansond 13:9edad7677211 333 // default POST (does nothing)
ansond 13:9edad7677211 334 void DynamicResource::post(void *args)
ansond 13:9edad7677211 335 {
ansond 13:9edad7677211 336 // not used by default
ansond 13:9edad7677211 337 this->logger()->log("DynamicResource::post() invoked (NOOP)");
ansond 13:9edad7677211 338 }
ansond 13:9edad7677211 339
ansond 13:9edad7677211 340 // default DELETE (does nothing)
ansond 13:9edad7677211 341 void DynamicResource::del(void *args)
ansond 13:9edad7677211 342 {
ansond 13:9edad7677211 343 // not used by default
ansond 13:9edad7677211 344 this->logger()->log("DynamicResource::del() invoked (NOOP)");
ansond 13:9edad7677211 345 }
ansond 13:9edad7677211 346
ansond 13:9edad7677211 347 // default observe behavior
ansond 13:9edad7677211 348 void DynamicResource::observe() {
ansond 15:c11dbe4d354c 349 if (this->m_observable == true /* && this->isRegistered() == true */) {
ansond 13:9edad7677211 350 this->notify(this->get());
ansond 13:9edad7677211 351 }
ansond 13:9edad7677211 352 }
ansond 13:9edad7677211 353
ansond 13:9edad7677211 354 // set the observer pointer
ansond 13:9edad7677211 355 void DynamicResource::setObserver(void *observer) {
ansond 13:9edad7677211 356 this->m_observer = observer;
ansond 13:9edad7677211 357 }
ansond 13:9edad7677211 358
ansond 13:9edad7677211 359 // set the content-format in responses
ansond 13:9edad7677211 360 void DynamicResource::setContentFormat(uint8_t content_format) {
ansond 13:9edad7677211 361 this->m_content_format = content_format;
ansond 13:9edad7677211 362 }
ansond 13:9edad7677211 363
ansond 13:9edad7677211 364 // set the max-age of responses
ansond 13:9edad7677211 365 void DynamicResource::setMaxAge(uint8_t maxage) {
ansond 13:9edad7677211 366 this->m_maxage = maxage;
ansond 13:9edad7677211 367 }
ansond 13:9edad7677211 368
ansond 13:9edad7677211 369 // convenience method to get the URI from its buffer field...
ansond 13:9edad7677211 370 string DynamicResource::coapDataToString(uint8_t *coap_data_ptr,int coap_data_ptr_length)
ansond 13:9edad7677211 371 {
ansond 13:9edad7677211 372 if (coap_data_ptr != NULL && coap_data_ptr_length > 0) {
ansond 13:9edad7677211 373 if (this->getDataWrapper() != NULL) {
ansond 13:9edad7677211 374 // unwrap the data...
ansond 13:9edad7677211 375 this->getDataWrapper()->unwrap(coap_data_ptr,coap_data_ptr_length);
ansond 13:9edad7677211 376 char *buf = (char *)this->getDataWrapper()->get(); // assumes data is null terminated in DataWrapper...
ansond 13:9edad7677211 377 return string(buf);
ansond 13:9edad7677211 378 }
ansond 13:9edad7677211 379 else {
ansond 13:9edad7677211 380 // no unwrap of the data...
ansond 13:9edad7677211 381 char buf[MAX_VALUE_BUFFER_LENGTH+1];
ansond 13:9edad7677211 382 memset(buf,0,MAX_VALUE_BUFFER_LENGTH+1);
ansond 13:9edad7677211 383 memcpy(buf,(char *)coap_data_ptr,coap_data_ptr_length);
ansond 13:9edad7677211 384 return string(buf);
ansond 13:9edad7677211 385 }
ansond 13:9edad7677211 386 }
ansond 13:9edad7677211 387 return string("");
ansond 13:9edad7677211 388 }
ansond 13:9edad7677211 389
ansond 13:9edad7677211 390 // Determine if we are connected or not
ansond 13:9edad7677211 391 bool DynamicResource::isConnected() {
ansond 13:9edad7677211 392 bool is_connected = false;
ansond 13:9edad7677211 393
ansond 13:9edad7677211 394 // get our Endpoint
ansond 13:9edad7677211 395 Connector::Endpoint *ep = (Connector::Endpoint *)this->m_endpoint;
ansond 13:9edad7677211 396 if (ep != NULL) {
ansond 13:9edad7677211 397 is_connected = ep->isConnected();
ansond 13:9edad7677211 398 }
ansond 13:9edad7677211 399
ansond 13:9edad7677211 400 // return our endpoint connection state
ansond 13:9edad7677211 401 return is_connected;
ansond 13:9edad7677211 402 }
ansond 13:9edad7677211 403
ansond 15:c11dbe4d354c 404 // Determine if we are registered or not
ansond 15:c11dbe4d354c 405 bool DynamicResource::isRegistered() {
ansond 15:c11dbe4d354c 406 bool is_registered = false;
ansond 15:c11dbe4d354c 407
ansond 15:c11dbe4d354c 408 if (this->isConnected() == true) {
ansond 15:c11dbe4d354c 409 // get our Endpoint
ansond 15:c11dbe4d354c 410 Connector::Endpoint *ep = (Connector::Endpoint *)this->m_endpoint;
ansond 15:c11dbe4d354c 411 if (ep != NULL) {
ansond 15:c11dbe4d354c 412 is_registered = ep->isRegistered();
ansond 15:c11dbe4d354c 413 }
ansond 15:c11dbe4d354c 414 }
ansond 15:c11dbe4d354c 415
ansond 15:c11dbe4d354c 416 // return our endpoint registration state
ansond 15:c11dbe4d354c 417 return is_registered;
ansond 15:c11dbe4d354c 418 }
ansond 15:c11dbe4d354c 419