use TCP to connect to mbed connector

Fork of mbedConnectorInterfaceWithDM by Doug Anson

Committer:
ansond
Date:
Wed Jun 15 19:40:08 2016 +0000
Revision:
44:7c73baf9f4c1
Parent:
38:bb6d2be4d54c
Child:
45:db754b994deb
updates

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