use TCP to connect to mbed connector

Fork of mbedConnectorInterfaceWithDM by Doug Anson

Committer:
ansond
Date:
Sun Jun 12 02:49:50 2016 +0000
Revision:
24:c92984bede9c
Parent:
21:0bbe9057e7b1
Child:
27:b8aaf7dc7023
updated

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 24:c92984bede9c 244 if (args != NULL) {
ansond 24:c92984bede9c 245 this->logger()->log("%s: Calling resource(POST) with [%s]=[%s]",this->m_res_type.c_str(),this->getFullName().c_str(),(char *)args);
ansond 24:c92984bede9c 246 this->post(args);
ansond 24:c92984bede9c 247 }
ansond 24:c92984bede9c 248 else {
ansond 24:c92984bede9c 249 string value = this->coapDataToString(this->m_res->value(),this->m_res->value_length());
ansond 24:c92984bede9c 250 this->logger()->log("%s: Calling resource(POST) with [%s]=[%s]",this->m_res_type.c_str(),this->getFullName().c_str(),value.c_str());
ansond 24:c92984bede9c 251 this->post((void *)value.c_str());
ansond 24:c92984bede9c 252 }
ansond 13:9edad7677211 253 return 0;
ansond 13:9edad7677211 254 }
ansond 13:9edad7677211 255 #endif
ansond 13:9edad7677211 256
ansond 13:9edad7677211 257 #if defined (HAS_EXECUTE_PARAMS)
ansond 13:9edad7677211 258 // DELETE() check
ansond 13:9edad7677211 259 if ((op & M2MBase::DELETE_ALLOWED) != 0) {
ansond 13:9edad7677211 260 if (param != NULL) {
ansond 13:9edad7677211 261 // use parameters
ansond 13:9edad7677211 262 String object_name = param->get_argument_object_name();
ansond 13:9edad7677211 263 int instance_id = (int)param->get_argument_object_instance_id();
ansond 13:9edad7677211 264 String resource_name = param->get_argument_resource_name();
ansond 13:9edad7677211 265 string value = this->coapDataToString(param->get_argument_value(),param->get_argument_value_length());
ansond 13:9edad7677211 266 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 267 }
ansond 13:9edad7677211 268 else {
ansond 13:9edad7677211 269 // use the resource value itself
ansond 13:9edad7677211 270 string value = this->coapDataToString(this->m_res->value(),this->m_res->value_length());
ansond 13:9edad7677211 271 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 272 }
ansond 13:9edad7677211 273
ansond 13:9edad7677211 274 // invoke
ansond 13:9edad7677211 275 this->del(args);
ansond 13:9edad7677211 276 return 0;
ansond 13:9edad7677211 277 }
ansond 13:9edad7677211 278 #else
ansond 13:9edad7677211 279 // DELETE() check
ansond 13:9edad7677211 280 if ((op & M2MBase::DELETE_ALLOWED) != 0) {
ansond 24:c92984bede9c 281 if (args != NULL) {
ansond 24:c92984bede9c 282 this->logger()->log("%s: Calling resource(DELETE) with [%s]=[%s]",this->m_res_type.c_str(),this->getFullName().c_str(),(char *)args);
ansond 24:c92984bede9c 283 this->del(args);
ansond 24:c92984bede9c 284 }
ansond 24:c92984bede9c 285 else {
ansond 24:c92984bede9c 286 string value = this->coapDataToString(this->m_res->value(),this->m_res->value_length());
ansond 24:c92984bede9c 287 this->logger()->log("%s: Calling resource(DELETE) with [%s]=[%s]",this->m_res_type.c_str(),this->getFullName().c_str(),value.c_str());
ansond 24:c92984bede9c 288 this->del((void *)value.c_str());
ansond 24:c92984bede9c 289 }
ansond 13:9edad7677211 290 }
ansond 13:9edad7677211 291 #endif
ansond 13:9edad7677211 292
ansond 13:9edad7677211 293 // unknown type...
ansond 13:9edad7677211 294 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 295 return 1;
ansond 13:9edad7677211 296 }
ansond 13:9edad7677211 297
ansond 13:9edad7677211 298 // send the notification
ansond 13:9edad7677211 299 int DynamicResource::notify(const string data) {
ansond 13:9edad7677211 300 return this->notify((uint8_t *)data.c_str(),(int)data.length());
ansond 13:9edad7677211 301 }
ansond 13:9edad7677211 302
ansond 13:9edad7677211 303 // send the notification
ansond 13:9edad7677211 304 int DynamicResource::notify(uint8_t *data,int data_length) {
ansond 13:9edad7677211 305 uint8_t *notify_data = NULL;
ansond 13:9edad7677211 306 int notify_data_length = 0;
ansond 13:9edad7677211 307 int status = 0;
ansond 13:9edad7677211 308
ansond 13:9edad7677211 309 // convert the string from the GET to something suitable for CoAP payloads
ansond 13:9edad7677211 310 if (this->getDataWrapper() != NULL) {
ansond 13:9edad7677211 311 // wrap the data...
ansond 13:9edad7677211 312 this->getDataWrapper()->wrap((uint8_t *)data,data_length);
ansond 13:9edad7677211 313
ansond 13:9edad7677211 314 // announce (after wrap)
ansond 13:9edad7677211 315 //this->logger()->log("Notify payload [%s]...",this->getDataWrapper()->get());
ansond 13:9edad7677211 316
ansond 13:9edad7677211 317 // fill notify
ansond 13:9edad7677211 318 notify_data_length = this->getDataWrapper()->length();
ansond 13:9edad7677211 319 notify_data = this->getDataWrapper()->get();
ansond 13:9edad7677211 320 }
ansond 13:9edad7677211 321 else {
ansond 13:9edad7677211 322 // announce (no wrap)
ansond 13:9edad7677211 323 //this->logger()->log("Notify payload [%s]...",data);
ansond 13:9edad7677211 324
ansond 13:9edad7677211 325 // do not wrap the data...
ansond 13:9edad7677211 326 notify_data_length = data_length;
ansond 13:9edad7677211 327 notify_data = data;
ansond 13:9edad7677211 328 }
ansond 13:9edad7677211 329
ansond 13:9edad7677211 330 // update the resource
ansond 13:9edad7677211 331 this->m_res->set_value((uint8_t *)notify_data,(uint8_t)notify_data_length);
ansond 13:9edad7677211 332
ansond 13:9edad7677211 333 // return our status
ansond 13:9edad7677211 334 return status;
ansond 13:9edad7677211 335 }
ansond 13:9edad7677211 336
ansond 13:9edad7677211 337 // default PUT (does nothing)
ansond 13:9edad7677211 338 void DynamicResource::put(const string value)
ansond 13:9edad7677211 339 {
ansond 13:9edad7677211 340 // not used by default
ansond 13:9edad7677211 341 this->logger()->log("DynamicResource::put() invoked (NOOP)");
ansond 13:9edad7677211 342 }
ansond 13:9edad7677211 343
ansond 13:9edad7677211 344 // default POST (does nothing)
ansond 13:9edad7677211 345 void DynamicResource::post(void *args)
ansond 13:9edad7677211 346 {
ansond 13:9edad7677211 347 // not used by default
ansond 13:9edad7677211 348 this->logger()->log("DynamicResource::post() invoked (NOOP)");
ansond 13:9edad7677211 349 }
ansond 13:9edad7677211 350
ansond 13:9edad7677211 351 // default DELETE (does nothing)
ansond 13:9edad7677211 352 void DynamicResource::del(void *args)
ansond 13:9edad7677211 353 {
ansond 13:9edad7677211 354 // not used by default
ansond 13:9edad7677211 355 this->logger()->log("DynamicResource::del() invoked (NOOP)");
ansond 13:9edad7677211 356 }
ansond 13:9edad7677211 357
ansond 13:9edad7677211 358 // default observe behavior
ansond 13:9edad7677211 359 void DynamicResource::observe() {
ansond 16:dffa38c3340f 360 if (this->m_observable == true && this->isRegistered() == true) {
ansond 13:9edad7677211 361 this->notify(this->get());
ansond 13:9edad7677211 362 }
ansond 13:9edad7677211 363 }
ansond 13:9edad7677211 364
ansond 13:9edad7677211 365 // set the observer pointer
ansond 13:9edad7677211 366 void DynamicResource::setObserver(void *observer) {
ansond 13:9edad7677211 367 this->m_observer = observer;
ansond 13:9edad7677211 368 }
ansond 13:9edad7677211 369
ansond 13:9edad7677211 370 // set the content-format in responses
ansond 13:9edad7677211 371 void DynamicResource::setContentFormat(uint8_t content_format) {
ansond 13:9edad7677211 372 this->m_content_format = content_format;
ansond 13:9edad7677211 373 }
ansond 13:9edad7677211 374
ansond 13:9edad7677211 375 // set the max-age of responses
ansond 13:9edad7677211 376 void DynamicResource::setMaxAge(uint8_t maxage) {
ansond 13:9edad7677211 377 this->m_maxage = maxage;
ansond 13:9edad7677211 378 }
ansond 13:9edad7677211 379
ansond 13:9edad7677211 380 // convenience method to get the URI from its buffer field...
ansond 13:9edad7677211 381 string DynamicResource::coapDataToString(uint8_t *coap_data_ptr,int coap_data_ptr_length)
ansond 13:9edad7677211 382 {
ansond 13:9edad7677211 383 if (coap_data_ptr != NULL && coap_data_ptr_length > 0) {
ansond 13:9edad7677211 384 if (this->getDataWrapper() != NULL) {
ansond 13:9edad7677211 385 // unwrap the data...
ansond 13:9edad7677211 386 this->getDataWrapper()->unwrap(coap_data_ptr,coap_data_ptr_length);
ansond 13:9edad7677211 387 char *buf = (char *)this->getDataWrapper()->get(); // assumes data is null terminated in DataWrapper...
ansond 13:9edad7677211 388 return string(buf);
ansond 13:9edad7677211 389 }
ansond 13:9edad7677211 390 else {
ansond 13:9edad7677211 391 // no unwrap of the data...
ansond 13:9edad7677211 392 char buf[MAX_VALUE_BUFFER_LENGTH+1];
ansond 13:9edad7677211 393 memset(buf,0,MAX_VALUE_BUFFER_LENGTH+1);
ansond 13:9edad7677211 394 memcpy(buf,(char *)coap_data_ptr,coap_data_ptr_length);
ansond 13:9edad7677211 395 return string(buf);
ansond 13:9edad7677211 396 }
ansond 13:9edad7677211 397 }
ansond 13:9edad7677211 398 return string("");
ansond 13:9edad7677211 399 }
ansond 13:9edad7677211 400
ansond 13:9edad7677211 401 // Determine if we are connected or not
ansond 13:9edad7677211 402 bool DynamicResource::isConnected() {
ansond 13:9edad7677211 403 bool is_connected = false;
ansond 13:9edad7677211 404
ansond 13:9edad7677211 405 // get our Endpoint
ansond 13:9edad7677211 406 Connector::Endpoint *ep = (Connector::Endpoint *)this->m_endpoint;
ansond 13:9edad7677211 407 if (ep != NULL) {
ansond 13:9edad7677211 408 is_connected = ep->isConnected();
ansond 17:defb680f8fce 409 if (is_connected) {
ansond 17:defb680f8fce 410 //this->logger()->log("DynamicResource::isConnected = true");
ansond 17:defb680f8fce 411 }
ansond 17:defb680f8fce 412 else {
ansond 17:defb680f8fce 413 //this->logger()->log("DynamicResource::isConnected = false");
ansond 17:defb680f8fce 414 }
ansond 17:defb680f8fce 415 }
ansond 17:defb680f8fce 416 else {
ansond 21:0bbe9057e7b1 417 this->logger()->log("DynamicResource::isConnected = false (no endpoint)");
ansond 13:9edad7677211 418 }
ansond 13:9edad7677211 419
ansond 13:9edad7677211 420 // return our endpoint connection state
ansond 13:9edad7677211 421 return is_connected;
ansond 13:9edad7677211 422 }
ansond 13:9edad7677211 423
ansond 15:c11dbe4d354c 424 // Determine if we are registered or not
ansond 15:c11dbe4d354c 425 bool DynamicResource::isRegistered() {
ansond 15:c11dbe4d354c 426 bool is_registered = false;
ansond 15:c11dbe4d354c 427
ansond 15:c11dbe4d354c 428 if (this->isConnected() == true) {
ansond 15:c11dbe4d354c 429 // get our Endpoint
ansond 15:c11dbe4d354c 430 Connector::Endpoint *ep = (Connector::Endpoint *)this->m_endpoint;
ansond 15:c11dbe4d354c 431 if (ep != NULL) {
ansond 15:c11dbe4d354c 432 is_registered = ep->isRegistered();
ansond 17:defb680f8fce 433 if (is_registered) {
ansond 17:defb680f8fce 434 //this->logger()->log("DynamicResource::isRegistered = true");
ansond 17:defb680f8fce 435 }
ansond 17:defb680f8fce 436 else {
ansond 17:defb680f8fce 437 //this->logger()->log("DynamicResource::isRegistered = false");
ansond 17:defb680f8fce 438 }
ansond 17:defb680f8fce 439 }
ansond 17:defb680f8fce 440 else {
ansond 21:0bbe9057e7b1 441 this->logger()->log("DynamicResource::isRegistered = false (no endpoint)");
ansond 15:c11dbe4d354c 442 }
ansond 15:c11dbe4d354c 443 }
ansond 15:c11dbe4d354c 444
ansond 15:c11dbe4d354c 445 // return our endpoint registration state
ansond 15:c11dbe4d354c 446 return is_registered;
ansond 17:defb680f8fce 447 }