use TCP to connect to mbed connector

Fork of mbedConnectorInterfaceWithDM by Doug Anson

Committer:
ansond
Date:
Tue Jun 14 04:01:34 2016 +0000
Revision:
27:b8aaf7dc7023
Parent:
24:c92984bede9c
Child:
33:1d0b855df5a5
Initial merge and update of device and firmware objects

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 27:b8aaf7dc7023 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,const ResourceType type) : 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 27:b8aaf7dc7023 48 this->m_type = type;
ansond 13:9edad7677211 49 this->m_observable = observable;
ansond 13:9edad7677211 50 this->m_res_mask = res_mask;
ansond 13:9edad7677211 51 this->m_obs_number = 0;
ansond 13:9edad7677211 52 this->m_data_wrapper = NULL;
ansond 13:9edad7677211 53 this->m_observer = NULL;
ansond 13:9edad7677211 54 this->m_maxage = DEFAULT_MAXAGE;
ansond 13:9edad7677211 55 this->m_content_format = DEFAULT_CONTENT_FORMAT;
ansond 27:b8aaf7dc7023 56 this->m_ep = NULL;
ansond 27:b8aaf7dc7023 57 this->m_res = NULL;
ansond 13:9edad7677211 58 }
ansond 13:9edad7677211 59
ansond 13:9edad7677211 60 // constructor (input initial value)
ansond 27:b8aaf7dc7023 61 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,const ResourceType type) : Resource<string>(logger,string(obj_name),string(res_name),value)
ansond 13:9edad7677211 62 {
ansond 13:9edad7677211 63 this->m_res_type = string(res_type);
ansond 27:b8aaf7dc7023 64 this->m_type = type;
ansond 13:9edad7677211 65 this->m_observable = observable;
ansond 13:9edad7677211 66 this->m_res_mask = res_mask;
ansond 13:9edad7677211 67 this->m_obs_number = 0;
ansond 13:9edad7677211 68 this->m_data_wrapper = NULL;
ansond 13:9edad7677211 69 this->m_observer = NULL;
ansond 13:9edad7677211 70 this->m_maxage = DEFAULT_MAXAGE;
ansond 13:9edad7677211 71 this->m_content_format = DEFAULT_CONTENT_FORMAT;
ansond 27:b8aaf7dc7023 72 this->m_ep = NULL;
ansond 27:b8aaf7dc7023 73 this->m_res = NULL;
ansond 13:9edad7677211 74 }
ansond 13:9edad7677211 75
ansond 13:9edad7677211 76 // constructor (strings)
ansond 27:b8aaf7dc7023 77 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,const ResourceType type) : Resource<string>(logger,obj_name,res_name,value)
ansond 13:9edad7677211 78 {
ansond 13:9edad7677211 79 this->m_res_type = res_type;
ansond 27:b8aaf7dc7023 80 this->m_type = type;
ansond 13:9edad7677211 81 this->m_observable = observable;
ansond 13:9edad7677211 82 this->m_res_mask = res_mask;
ansond 13:9edad7677211 83 this->m_obs_number = 0;
ansond 13:9edad7677211 84 this->m_data_wrapper = NULL;
ansond 13:9edad7677211 85 this->m_observer = NULL;
ansond 13:9edad7677211 86 this->m_maxage = DEFAULT_MAXAGE;
ansond 13:9edad7677211 87 this->m_content_format = DEFAULT_CONTENT_FORMAT;
ansond 27:b8aaf7dc7023 88 this->m_ep = NULL;
ansond 27:b8aaf7dc7023 89 this->m_res = NULL;
ansond 13:9edad7677211 90 }
ansond 13:9edad7677211 91
ansond 13:9edad7677211 92 // copy constructor
ansond 13:9edad7677211 93 DynamicResource::DynamicResource(const DynamicResource &resource) : Resource<string>((const Resource<string> &)resource)
ansond 13:9edad7677211 94 {
ansond 13:9edad7677211 95 this->m_res_type = resource.m_res_type;
ansond 27:b8aaf7dc7023 96 this->m_type = resource.m_type;
ansond 13:9edad7677211 97 this->m_observable = resource.m_observable;
ansond 13:9edad7677211 98 this->m_res_mask = resource.m_res_mask;
ansond 13:9edad7677211 99 this->m_obs_number = resource.m_obs_number;
ansond 13:9edad7677211 100 this->m_data_wrapper = resource.m_data_wrapper;
ansond 13:9edad7677211 101 this->m_observer = resource.m_observer;
ansond 13:9edad7677211 102 this->m_maxage = resource.m_maxage;
ansond 13:9edad7677211 103 this->m_content_format = resource.m_content_format;
ansond 27:b8aaf7dc7023 104 this->m_ep = resource.m_ep;
ansond 27:b8aaf7dc7023 105 this->m_res = resource.m_res;
ansond 13:9edad7677211 106 }
ansond 13:9edad7677211 107
ansond 13:9edad7677211 108 // destructor
ansond 13:9edad7677211 109 DynamicResource::~DynamicResource() {
ansond 13:9edad7677211 110 }
ansond 13:9edad7677211 111
ansond 13:9edad7677211 112 // bind CoAP Resource...
ansond 27:b8aaf7dc7023 113 void DynamicResource::bind(void *ep) {
ansond 27:b8aaf7dc7023 114 if (ep != NULL) {
ansond 27:b8aaf7dc7023 115 // cast
ansond 27:b8aaf7dc7023 116 Connector::Endpoint *endpoint = (Connector::Endpoint *)ep;
ansond 27:b8aaf7dc7023 117
ansond 27:b8aaf7dc7023 118 // get our ObjectInstanceManager
ansond 27:b8aaf7dc7023 119 ObjectInstanceManager *oim = endpoint->getObjectInstanceManager();
ansond 27:b8aaf7dc7023 120
ansond 27:b8aaf7dc7023 121 // Create our Resource
ansond 27:b8aaf7dc7023 122 this->m_res = (M2MResource *)oim->createDynamicResourceInstance((char *)this->getObjName().c_str(),(char *)this->getResName().c_str(),(char *)this->m_res_type.c_str(),(int)this->m_type,this->m_observable);
ansond 27:b8aaf7dc7023 123 if (this->m_res != NULL) {
ansond 27:b8aaf7dc7023 124 // perform an initial get() to initialize our data value
ansond 27:b8aaf7dc7023 125 this->setValue(this->get());
ansond 27:b8aaf7dc7023 126
ansond 27:b8aaf7dc7023 127 // now record the data value
ansond 27:b8aaf7dc7023 128 if (this->getDataWrapper() != NULL) {
ansond 27:b8aaf7dc7023 129 // wrap the data...
ansond 27:b8aaf7dc7023 130 this->getDataWrapper()->wrap((uint8_t *)this->getValue().c_str(),(int)this->getValue().size());
ansond 27:b8aaf7dc7023 131 this->m_res->set_operation((M2MBase::Operation)this->m_res_mask);
ansond 27:b8aaf7dc7023 132 this->m_res->set_value( this->getDataWrapper()->get(),(uint8_t)this->getDataWrapper()->length());
ansond 27:b8aaf7dc7023 133 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 27:b8aaf7dc7023 134 }
ansond 27:b8aaf7dc7023 135 else {
ansond 27:b8aaf7dc7023 136 // do not wrap the data...
ansond 27:b8aaf7dc7023 137 this->m_res->set_operation((M2MBase::Operation)this->m_res_mask);
ansond 27:b8aaf7dc7023 138 this->m_res->set_value((uint8_t *)this->getValue().c_str(),(uint8_t)this->getValue().size());
ansond 27:b8aaf7dc7023 139 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 27:b8aaf7dc7023 140 }
ansond 27:b8aaf7dc7023 141
ansond 27:b8aaf7dc7023 142 // set our endpoint instance
ansond 27:b8aaf7dc7023 143 this->m_ep = (void *)ep;
ansond 27:b8aaf7dc7023 144
ansond 27:b8aaf7dc7023 145 // For POST-enabled RESOURCES (only...), we must add a callback
ansond 27:b8aaf7dc7023 146 if ((this->m_res_mask & M2MBase::POST_ALLOWED) != 0) {
ansond 27:b8aaf7dc7023 147 // add a callback for the execute function...we will just direct through process()...
ansond 27:b8aaf7dc7023 148 //this->logger()->log("DynamicResource::bind(): Setting up POST execute callback function");
ansond 27:b8aaf7dc7023 149 this->m_res->set_execute_function(execute_callback(this, &DynamicResource::process_resource_post));
ansond 27:b8aaf7dc7023 150 }
ansond 27:b8aaf7dc7023 151 }
ansond 13:9edad7677211 152 }
ansond 13:9edad7677211 153 else {
ansond 27:b8aaf7dc7023 154 // no instance pointer to our endpoint
ansond 27:b8aaf7dc7023 155 this->logger()->log("%s: NULL endpoint instance pointer in bind() request...",this->m_res_type.c_str());
ansond 13:9edad7677211 156 }
ansond 13:9edad7677211 157 }
ansond 13:9edad7677211 158
ansond 13:9edad7677211 159 // get our M2MBase representation
ansond 27:b8aaf7dc7023 160 M2MResource *DynamicResource::getResource() {
ansond 27:b8aaf7dc7023 161 return this->m_res;
ansond 13:9edad7677211 162 }
ansond 13:9edad7677211 163
ansond 13:9edad7677211 164 // process inbound mbed-client POST message for a Resource
ansond 13:9edad7677211 165 void DynamicResource::process_resource_post(void *args) {
ansond 13:9edad7677211 166 // just call process() for POST and Resources...
ansond 13:9edad7677211 167 //this->logger()->log("DynamicResource::process_resource_post(): calling process(POST)");
ansond 13:9edad7677211 168 (void)this->process(M2MBase::POST_ALLOWED,this->m_res->base_type(),args);
ansond 13:9edad7677211 169 }
ansond 13:9edad7677211 170
ansond 13:9edad7677211 171 // process inbound mbed-client message
ansond 13:9edad7677211 172 uint8_t DynamicResource::process(M2MBase::Operation op,M2MBase::BaseType type,void *args) {
ansond 13:9edad7677211 173 #if defined (HAS_EXECUTE_PARAMS)
ansond 13:9edad7677211 174 M2MResource::M2MExecuteParameter* param = NULL;
ansond 13:9edad7677211 175
ansond 13:9edad7677211 176 // cast args if present...
ansond 13:9edad7677211 177 if (args != NULL) {
ansond 13:9edad7677211 178 param = (M2MResource::M2MExecuteParameter*)args;
ansond 13:9edad7677211 179 }
ansond 13:9edad7677211 180 #endif
ansond 13:9edad7677211 181 // DEBUG
ansond 13:9edad7677211 182 //this->logger()->log("in %s::process() Operation=0x0%x Type=%x%x",this->m_res_type.c_str(),op,type);
ansond 13:9edad7677211 183
ansond 13:9edad7677211 184 // PUT() check
ansond 13:9edad7677211 185 if ((op & M2MBase::PUT_ALLOWED) != 0) {
ansond 13:9edad7677211 186 string value = this->coapDataToString(this->m_res->value(),this->m_res->value_length());
ansond 13:9edad7677211 187 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 188 this->put(value.c_str());
ansond 13:9edad7677211 189 return 0;
ansond 13:9edad7677211 190 }
ansond 13:9edad7677211 191
ansond 13:9edad7677211 192 #if defined (HAS_EXECUTE_PARAMS)
ansond 13:9edad7677211 193 // POST() check
ansond 13:9edad7677211 194 if ((op & M2MBase::POST_ALLOWED) != 0) {
ansond 13:9edad7677211 195 string value;
ansond 13:9edad7677211 196 if (param != NULL) {
ansond 13:9edad7677211 197 // use parameters
ansond 13:9edad7677211 198 String object_name = param->get_argument_object_name();
ansond 13:9edad7677211 199 int instance_id = (int)param->get_argument_object_instance_id();
ansond 13:9edad7677211 200 String resource_name = param->get_argument_resource_name();
ansond 13:9edad7677211 201 value = this->coapDataToString(param->get_argument_value(),param->get_argument_value_length());
ansond 13:9edad7677211 202 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 203 }
ansond 13:9edad7677211 204 else {
ansond 13:9edad7677211 205 // use the resource value itself
ansond 13:9edad7677211 206 value = this->coapDataToString(this->m_res->value(),this->m_res->value_length());
ansond 13:9edad7677211 207 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 208 }
ansond 13:9edad7677211 209
ansond 13:9edad7677211 210 // invoke
ansond 13:9edad7677211 211 this->post(args);
ansond 13:9edad7677211 212 return 0;
ansond 13:9edad7677211 213 }
ansond 13:9edad7677211 214 #else
ansond 13:9edad7677211 215 // POST() check
ansond 13:9edad7677211 216 if ((op & M2MBase::POST_ALLOWED) != 0) {
ansond 24:c92984bede9c 217 if (args != NULL) {
ansond 24:c92984bede9c 218 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 219 this->post(args);
ansond 24:c92984bede9c 220 }
ansond 24:c92984bede9c 221 else {
ansond 24:c92984bede9c 222 string value = this->coapDataToString(this->m_res->value(),this->m_res->value_length());
ansond 24:c92984bede9c 223 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 224 this->post((void *)value.c_str());
ansond 24:c92984bede9c 225 }
ansond 13:9edad7677211 226 return 0;
ansond 13:9edad7677211 227 }
ansond 13:9edad7677211 228 #endif
ansond 13:9edad7677211 229
ansond 13:9edad7677211 230 #if defined (HAS_EXECUTE_PARAMS)
ansond 13:9edad7677211 231 // DELETE() check
ansond 13:9edad7677211 232 if ((op & M2MBase::DELETE_ALLOWED) != 0) {
ansond 13:9edad7677211 233 if (param != NULL) {
ansond 13:9edad7677211 234 // use parameters
ansond 13:9edad7677211 235 String object_name = param->get_argument_object_name();
ansond 13:9edad7677211 236 int instance_id = (int)param->get_argument_object_instance_id();
ansond 13:9edad7677211 237 String resource_name = param->get_argument_resource_name();
ansond 13:9edad7677211 238 string value = this->coapDataToString(param->get_argument_value(),param->get_argument_value_length());
ansond 13:9edad7677211 239 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 240 }
ansond 13:9edad7677211 241 else {
ansond 13:9edad7677211 242 // use the resource value itself
ansond 13:9edad7677211 243 string value = this->coapDataToString(this->m_res->value(),this->m_res->value_length());
ansond 13:9edad7677211 244 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 245 }
ansond 13:9edad7677211 246
ansond 13:9edad7677211 247 // invoke
ansond 13:9edad7677211 248 this->del(args);
ansond 13:9edad7677211 249 return 0;
ansond 13:9edad7677211 250 }
ansond 13:9edad7677211 251 #else
ansond 13:9edad7677211 252 // DELETE() check
ansond 13:9edad7677211 253 if ((op & M2MBase::DELETE_ALLOWED) != 0) {
ansond 24:c92984bede9c 254 if (args != NULL) {
ansond 24:c92984bede9c 255 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 256 this->del(args);
ansond 24:c92984bede9c 257 }
ansond 24:c92984bede9c 258 else {
ansond 24:c92984bede9c 259 string value = this->coapDataToString(this->m_res->value(),this->m_res->value_length());
ansond 24:c92984bede9c 260 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 261 this->del((void *)value.c_str());
ansond 24:c92984bede9c 262 }
ansond 13:9edad7677211 263 }
ansond 13:9edad7677211 264 #endif
ansond 13:9edad7677211 265
ansond 13:9edad7677211 266 // unknown type...
ansond 13:9edad7677211 267 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 268 return 1;
ansond 13:9edad7677211 269 }
ansond 13:9edad7677211 270
ansond 13:9edad7677211 271 // send the notification
ansond 13:9edad7677211 272 int DynamicResource::notify(const string data) {
ansond 13:9edad7677211 273 return this->notify((uint8_t *)data.c_str(),(int)data.length());
ansond 13:9edad7677211 274 }
ansond 13:9edad7677211 275
ansond 13:9edad7677211 276 // send the notification
ansond 13:9edad7677211 277 int DynamicResource::notify(uint8_t *data,int data_length) {
ansond 13:9edad7677211 278 uint8_t *notify_data = NULL;
ansond 13:9edad7677211 279 int notify_data_length = 0;
ansond 13:9edad7677211 280 int status = 0;
ansond 13:9edad7677211 281
ansond 13:9edad7677211 282 // convert the string from the GET to something suitable for CoAP payloads
ansond 13:9edad7677211 283 if (this->getDataWrapper() != NULL) {
ansond 13:9edad7677211 284 // wrap the data...
ansond 13:9edad7677211 285 this->getDataWrapper()->wrap((uint8_t *)data,data_length);
ansond 13:9edad7677211 286
ansond 13:9edad7677211 287 // announce (after wrap)
ansond 13:9edad7677211 288 //this->logger()->log("Notify payload [%s]...",this->getDataWrapper()->get());
ansond 13:9edad7677211 289
ansond 13:9edad7677211 290 // fill notify
ansond 13:9edad7677211 291 notify_data_length = this->getDataWrapper()->length();
ansond 13:9edad7677211 292 notify_data = this->getDataWrapper()->get();
ansond 13:9edad7677211 293 }
ansond 13:9edad7677211 294 else {
ansond 13:9edad7677211 295 // announce (no wrap)
ansond 13:9edad7677211 296 //this->logger()->log("Notify payload [%s]...",data);
ansond 13:9edad7677211 297
ansond 13:9edad7677211 298 // do not wrap the data...
ansond 13:9edad7677211 299 notify_data_length = data_length;
ansond 13:9edad7677211 300 notify_data = data;
ansond 13:9edad7677211 301 }
ansond 13:9edad7677211 302
ansond 13:9edad7677211 303 // update the resource
ansond 13:9edad7677211 304 this->m_res->set_value((uint8_t *)notify_data,(uint8_t)notify_data_length);
ansond 13:9edad7677211 305
ansond 13:9edad7677211 306 // return our status
ansond 13:9edad7677211 307 return status;
ansond 13:9edad7677211 308 }
ansond 13:9edad7677211 309
ansond 27:b8aaf7dc7023 310 // default GET (does nothing)
ansond 27:b8aaf7dc7023 311 string DynamicResource::get()
ansond 27:b8aaf7dc7023 312 {
ansond 27:b8aaf7dc7023 313 // not used by default
ansond 27:b8aaf7dc7023 314 this->logger()->log("DynamicResource::get() invoked (NOOP)");
ansond 27:b8aaf7dc7023 315 return string("");
ansond 27:b8aaf7dc7023 316 }
ansond 27:b8aaf7dc7023 317
ansond 13:9edad7677211 318 // default PUT (does nothing)
ansond 13:9edad7677211 319 void DynamicResource::put(const string value)
ansond 13:9edad7677211 320 {
ansond 13:9edad7677211 321 // not used by default
ansond 13:9edad7677211 322 this->logger()->log("DynamicResource::put() invoked (NOOP)");
ansond 13:9edad7677211 323 }
ansond 13:9edad7677211 324
ansond 13:9edad7677211 325 // default POST (does nothing)
ansond 13:9edad7677211 326 void DynamicResource::post(void *args)
ansond 13:9edad7677211 327 {
ansond 13:9edad7677211 328 // not used by default
ansond 13:9edad7677211 329 this->logger()->log("DynamicResource::post() invoked (NOOP)");
ansond 13:9edad7677211 330 }
ansond 13:9edad7677211 331
ansond 13:9edad7677211 332 // default DELETE (does nothing)
ansond 13:9edad7677211 333 void DynamicResource::del(void *args)
ansond 13:9edad7677211 334 {
ansond 13:9edad7677211 335 // not used by default
ansond 13:9edad7677211 336 this->logger()->log("DynamicResource::del() invoked (NOOP)");
ansond 13:9edad7677211 337 }
ansond 13:9edad7677211 338
ansond 13:9edad7677211 339 // default observe behavior
ansond 13:9edad7677211 340 void DynamicResource::observe() {
ansond 16:dffa38c3340f 341 if (this->m_observable == true && this->isRegistered() == true) {
ansond 13:9edad7677211 342 this->notify(this->get());
ansond 13:9edad7677211 343 }
ansond 13:9edad7677211 344 }
ansond 13:9edad7677211 345
ansond 13:9edad7677211 346 // set the observer pointer
ansond 13:9edad7677211 347 void DynamicResource::setObserver(void *observer) {
ansond 13:9edad7677211 348 this->m_observer = observer;
ansond 13:9edad7677211 349 }
ansond 13:9edad7677211 350
ansond 13:9edad7677211 351 // set the content-format in responses
ansond 13:9edad7677211 352 void DynamicResource::setContentFormat(uint8_t content_format) {
ansond 13:9edad7677211 353 this->m_content_format = content_format;
ansond 13:9edad7677211 354 }
ansond 13:9edad7677211 355
ansond 13:9edad7677211 356 // set the max-age of responses
ansond 13:9edad7677211 357 void DynamicResource::setMaxAge(uint8_t maxage) {
ansond 13:9edad7677211 358 this->m_maxage = maxage;
ansond 13:9edad7677211 359 }
ansond 13:9edad7677211 360
ansond 27:b8aaf7dc7023 361 // convert the CoAP data pointer to a string type
ansond 13:9edad7677211 362 string DynamicResource::coapDataToString(uint8_t *coap_data_ptr,int coap_data_ptr_length)
ansond 13:9edad7677211 363 {
ansond 13:9edad7677211 364 if (coap_data_ptr != NULL && coap_data_ptr_length > 0) {
ansond 13:9edad7677211 365 if (this->getDataWrapper() != NULL) {
ansond 13:9edad7677211 366 // unwrap the data...
ansond 13:9edad7677211 367 this->getDataWrapper()->unwrap(coap_data_ptr,coap_data_ptr_length);
ansond 13:9edad7677211 368 char *buf = (char *)this->getDataWrapper()->get(); // assumes data is null terminated in DataWrapper...
ansond 13:9edad7677211 369 return string(buf);
ansond 13:9edad7677211 370 }
ansond 13:9edad7677211 371 else {
ansond 13:9edad7677211 372 // no unwrap of the data...
ansond 13:9edad7677211 373 char buf[MAX_VALUE_BUFFER_LENGTH+1];
ansond 13:9edad7677211 374 memset(buf,0,MAX_VALUE_BUFFER_LENGTH+1);
ansond 13:9edad7677211 375 memcpy(buf,(char *)coap_data_ptr,coap_data_ptr_length);
ansond 13:9edad7677211 376 return string(buf);
ansond 13:9edad7677211 377 }
ansond 13:9edad7677211 378 }
ansond 13:9edad7677211 379 return string("");
ansond 13:9edad7677211 380 }
ansond 13:9edad7677211 381
ansond 27:b8aaf7dc7023 382 // convert the CoAP data pointer to an integer type
ansond 27:b8aaf7dc7023 383 int DynamicResource::coapDataToInteger(uint8_t *coap_data_ptr,int coap_data_ptr_length) {
ansond 27:b8aaf7dc7023 384 int value = 0;
ansond 27:b8aaf7dc7023 385 if (coap_data_ptr != NULL && coap_data_ptr_length > 0) {
ansond 27:b8aaf7dc7023 386 if (this->getDataWrapper() != NULL) {
ansond 27:b8aaf7dc7023 387 // unwrap the data...
ansond 27:b8aaf7dc7023 388 this->getDataWrapper()->unwrap(coap_data_ptr,coap_data_ptr_length);
ansond 27:b8aaf7dc7023 389 //value = (int)this->getDataWrapper()->get(); // assumes data is null terminated in DataWrapper...
ansond 27:b8aaf7dc7023 390 }
ansond 27:b8aaf7dc7023 391 else {
ansond 27:b8aaf7dc7023 392 // no unwrap of the data...
ansond 27:b8aaf7dc7023 393 //value = (int)coap_data_ptr;
ansond 27:b8aaf7dc7023 394 }
ansond 27:b8aaf7dc7023 395 }
ansond 27:b8aaf7dc7023 396 return value;
ansond 27:b8aaf7dc7023 397 }
ansond 27:b8aaf7dc7023 398
ansond 27:b8aaf7dc7023 399 // convert the CoAP data pointer to a float type
ansond 27:b8aaf7dc7023 400 float DynamicResource::coapDataToFloat(uint8_t *coap_data_ptr,int coap_data_ptr_length) {
ansond 27:b8aaf7dc7023 401 float value = 0.0;
ansond 27:b8aaf7dc7023 402 if (coap_data_ptr != NULL && coap_data_ptr_length > 0) {
ansond 27:b8aaf7dc7023 403 if (this->getDataWrapper() != NULL) {
ansond 27:b8aaf7dc7023 404 // unwrap the data...
ansond 27:b8aaf7dc7023 405 this->getDataWrapper()->unwrap(coap_data_ptr,coap_data_ptr_length);
ansond 27:b8aaf7dc7023 406 //value = (float)this->getDataWrapper()->get(); // assumes data is null terminated in DataWrapper...
ansond 27:b8aaf7dc7023 407 }
ansond 27:b8aaf7dc7023 408 else {
ansond 27:b8aaf7dc7023 409 // no unwrap of the data...
ansond 27:b8aaf7dc7023 410 //value = (float)coap_data_ptr;
ansond 27:b8aaf7dc7023 411 }
ansond 27:b8aaf7dc7023 412 }
ansond 27:b8aaf7dc7023 413 return value;
ansond 27:b8aaf7dc7023 414 }
ansond 27:b8aaf7dc7023 415
ansond 27:b8aaf7dc7023 416 // convert the CoAP data pointer to an opaque type
ansond 27:b8aaf7dc7023 417 void *DynamicResource::coapDataToOpaque(uint8_t *coap_data_ptr,int coap_data_ptr_length) {
ansond 27:b8aaf7dc7023 418 if (coap_data_ptr != NULL && coap_data_ptr_length > 0) {
ansond 27:b8aaf7dc7023 419 if (this->getDataWrapper() != NULL) {
ansond 27:b8aaf7dc7023 420 // unwrap the data...
ansond 27:b8aaf7dc7023 421 this->getDataWrapper()->unwrap(coap_data_ptr,coap_data_ptr_length);
ansond 27:b8aaf7dc7023 422 char *buf = (char *)this->getDataWrapper()->get(); // assumes data is null terminated in DataWrapper...
ansond 27:b8aaf7dc7023 423 return (void *)buf;
ansond 27:b8aaf7dc7023 424 }
ansond 27:b8aaf7dc7023 425 }
ansond 27:b8aaf7dc7023 426 return (void *)coap_data_ptr;
ansond 27:b8aaf7dc7023 427 }
ansond 27:b8aaf7dc7023 428
ansond 13:9edad7677211 429 // Determine if we are connected or not
ansond 13:9edad7677211 430 bool DynamicResource::isConnected() {
ansond 13:9edad7677211 431 bool is_connected = false;
ansond 13:9edad7677211 432
ansond 13:9edad7677211 433 // get our Endpoint
ansond 13:9edad7677211 434 Connector::Endpoint *ep = (Connector::Endpoint *)this->m_endpoint;
ansond 13:9edad7677211 435 if (ep != NULL) {
ansond 13:9edad7677211 436 is_connected = ep->isConnected();
ansond 17:defb680f8fce 437 if (is_connected) {
ansond 17:defb680f8fce 438 //this->logger()->log("DynamicResource::isConnected = true");
ansond 17:defb680f8fce 439 }
ansond 17:defb680f8fce 440 else {
ansond 17:defb680f8fce 441 //this->logger()->log("DynamicResource::isConnected = false");
ansond 17:defb680f8fce 442 }
ansond 17:defb680f8fce 443 }
ansond 17:defb680f8fce 444 else {
ansond 21:0bbe9057e7b1 445 this->logger()->log("DynamicResource::isConnected = false (no endpoint)");
ansond 13:9edad7677211 446 }
ansond 13:9edad7677211 447
ansond 13:9edad7677211 448 // return our endpoint connection state
ansond 13:9edad7677211 449 return is_connected;
ansond 13:9edad7677211 450 }
ansond 13:9edad7677211 451
ansond 15:c11dbe4d354c 452 // Determine if we are registered or not
ansond 15:c11dbe4d354c 453 bool DynamicResource::isRegistered() {
ansond 15:c11dbe4d354c 454 bool is_registered = false;
ansond 15:c11dbe4d354c 455
ansond 15:c11dbe4d354c 456 if (this->isConnected() == true) {
ansond 15:c11dbe4d354c 457 // get our Endpoint
ansond 15:c11dbe4d354c 458 Connector::Endpoint *ep = (Connector::Endpoint *)this->m_endpoint;
ansond 15:c11dbe4d354c 459 if (ep != NULL) {
ansond 15:c11dbe4d354c 460 is_registered = ep->isRegistered();
ansond 17:defb680f8fce 461 if (is_registered) {
ansond 17:defb680f8fce 462 //this->logger()->log("DynamicResource::isRegistered = true");
ansond 17:defb680f8fce 463 }
ansond 17:defb680f8fce 464 else {
ansond 17:defb680f8fce 465 //this->logger()->log("DynamicResource::isRegistered = false");
ansond 17:defb680f8fce 466 }
ansond 17:defb680f8fce 467 }
ansond 17:defb680f8fce 468 else {
ansond 21:0bbe9057e7b1 469 this->logger()->log("DynamicResource::isRegistered = false (no endpoint)");
ansond 15:c11dbe4d354c 470 }
ansond 15:c11dbe4d354c 471 }
ansond 15:c11dbe4d354c 472
ansond 15:c11dbe4d354c 473 // return our endpoint registration state
ansond 15:c11dbe4d354c 474 return is_registered;
ansond 17:defb680f8fce 475 }