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:
Tue Sep 26 16:01:31 2017 +0000
Revision:
127:b4a661ff6fb9
Parent:
118:bee40e9f18ff
minor re-ordering of FCC init

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