mbed Connector Interface simplification API on top of mbed-client

Fork of mbedConnectorInterfaceV3 by Doug Anson

NOTE:

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

Committer:
ansond
Date:
Fri Feb 19 17:32:14 2016 +0000
Revision:
0:1f1f55e73248
Child:
27:b8aaf7dc7023
initial checkin

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ansond 0:1f1f55e73248 1 /**
ansond 0:1f1f55e73248 2 * @file StaticResource.h
ansond 0:1f1f55e73248 3 * @brief mbed CoAP Endpoint Static Resource class
ansond 0:1f1f55e73248 4 * @author Doug Anson/Chris Paola
ansond 0:1f1f55e73248 5 * @version 1.0
ansond 0:1f1f55e73248 6 * @see
ansond 0:1f1f55e73248 7 *
ansond 0:1f1f55e73248 8 * Copyright (c) 2014
ansond 0:1f1f55e73248 9 *
ansond 0:1f1f55e73248 10 * Licensed under the Apache License, Version 2.0 (the "License");
ansond 0:1f1f55e73248 11 * you may not use this file except in compliance with the License.
ansond 0:1f1f55e73248 12 * You may obtain a copy of the License at
ansond 0:1f1f55e73248 13 *
ansond 0:1f1f55e73248 14 * http://www.apache.org/licenses/LICENSE-2.0
ansond 0:1f1f55e73248 15 *
ansond 0:1f1f55e73248 16 * Unless required by applicable law or agreed to in writing, software
ansond 0:1f1f55e73248 17 * distributed under the License is distributed on an "AS IS" BASIS,
ansond 0:1f1f55e73248 18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ansond 0:1f1f55e73248 19 * See the License for the specific language governing permissions and
ansond 0:1f1f55e73248 20 * limitations under the License.
ansond 0:1f1f55e73248 21 */
ansond 0:1f1f55e73248 22
ansond 0:1f1f55e73248 23 #include "mbed-connector-interface/StaticResource.h"
ansond 0:1f1f55e73248 24
ansond 0:1f1f55e73248 25 // Constructor
ansond 0:1f1f55e73248 26 StaticResource::StaticResource(const Logger *logger,const char *obj_name,const char *res_name, const char *value) : Resource<string>(logger,string(obj_name),string(res_name),string(value)), m_data_wrapper()
ansond 0:1f1f55e73248 27 {
ansond 0:1f1f55e73248 28 this->m_object = NULL;
ansond 0:1f1f55e73248 29 }
ansond 0:1f1f55e73248 30
ansond 0:1f1f55e73248 31 // Constructor
ansond 0:1f1f55e73248 32 StaticResource::StaticResource(const Logger *logger,const char *obj_name,const char *res_name,const string value) : Resource<string>(logger,string(obj_name),string(res_name),string(value))
ansond 0:1f1f55e73248 33 {
ansond 0:1f1f55e73248 34 this->m_data_wrapper = NULL;
ansond 0:1f1f55e73248 35 this->m_object = NULL;
ansond 0:1f1f55e73248 36 }
ansond 0:1f1f55e73248 37
ansond 0:1f1f55e73248 38 // Constructor with buffer lengths
ansond 0:1f1f55e73248 39 StaticResource::StaticResource(const Logger *logger,const string obj_name,const string res_name,const string value) : Resource<string>(logger,string(obj_name),string(res_name),string(value))
ansond 0:1f1f55e73248 40 {
ansond 0:1f1f55e73248 41 this->m_data_wrapper = NULL;
ansond 0:1f1f55e73248 42 this->m_object = NULL;
ansond 0:1f1f55e73248 43 }
ansond 0:1f1f55e73248 44
ansond 0:1f1f55e73248 45 // Copy constructor
ansond 0:1f1f55e73248 46 StaticResource::StaticResource(const StaticResource &resource) : Resource<string>((const Resource<string> &)resource)
ansond 0:1f1f55e73248 47 {
ansond 0:1f1f55e73248 48 this->m_data_wrapper = resource.m_data_wrapper;
ansond 0:1f1f55e73248 49 this->m_object = resource.m_object;
ansond 0:1f1f55e73248 50 }
ansond 0:1f1f55e73248 51
ansond 0:1f1f55e73248 52 // Destructor
ansond 0:1f1f55e73248 53 StaticResource::~StaticResource() {
ansond 0:1f1f55e73248 54 }
ansond 0:1f1f55e73248 55
ansond 0:1f1f55e73248 56 // bind CoAP Resource..
ansond 0:1f1f55e73248 57 M2MObject *StaticResource::bind(void *p) {
ansond 0:1f1f55e73248 58 if (p != NULL) {
ansond 0:1f1f55e73248 59 this->m_object = M2MInterfaceFactory::create_object(this->getObjName().c_str());
ansond 0:1f1f55e73248 60 if (this->m_object != NULL) {
ansond 0:1f1f55e73248 61 M2MObjectInstance* inst = this->m_object->create_object_instance();
ansond 0:1f1f55e73248 62 if (inst != NULL) {
ansond 0:1f1f55e73248 63 if (this->getDataWrapper() != NULL) {
ansond 0:1f1f55e73248 64 // wrap the data...
ansond 0:1f1f55e73248 65 this->getDataWrapper()->wrap((uint8_t *)this->getValue().c_str(),(int)this->getValue().size());
ansond 0:1f1f55e73248 66 inst->create_static_resource(this->getResName().c_str(),"StaticResource",M2MResourceInstance::STRING,this->getDataWrapper()->get(),(uint8_t)this->getDataWrapper()->length());
ansond 0:1f1f55e73248 67 this->logger()->log("StaticResource: [%s] value: [%s] bound",this->getFullName().c_str(),this->getDataWrapper()->get());
ansond 0:1f1f55e73248 68 }
ansond 0:1f1f55e73248 69 else {
ansond 0:1f1f55e73248 70 // do not wrap the data...
ansond 0:1f1f55e73248 71 inst->create_static_resource(this->getResName().c_str(),"StaticResource",M2MResourceInstance::STRING,(uint8_t *)this->getValue().c_str(),(uint8_t)this->getValue().size());
ansond 0:1f1f55e73248 72 this->logger()->log("StaticResource: [%s] value: [%s] bound",this->getFullName().c_str(),this->getValue().c_str());
ansond 0:1f1f55e73248 73 }
ansond 0:1f1f55e73248 74 }
ansond 0:1f1f55e73248 75 else {
ansond 0:1f1f55e73248 76 // create_object_instance() failed...
ansond 0:1f1f55e73248 77 this->logger()->log("StaticResource: Unable to create object instance...");
ansond 0:1f1f55e73248 78 delete this->m_object;
ansond 0:1f1f55e73248 79 this->m_object = NULL;
ansond 0:1f1f55e73248 80 }
ansond 0:1f1f55e73248 81 }
ansond 0:1f1f55e73248 82 else {
ansond 0:1f1f55e73248 83 // create_object() failed
ansond 0:1f1f55e73248 84 this->logger()->log("StaticResource: Unable to create object...");
ansond 0:1f1f55e73248 85 }
ansond 0:1f1f55e73248 86 }
ansond 0:1f1f55e73248 87 else {
ansond 0:1f1f55e73248 88 this->logger()->log("StaticResource: NULL value parameter in bind() request...");
ansond 0:1f1f55e73248 89 }
ansond 0:1f1f55e73248 90 return this->m_object;
ansond 0:1f1f55e73248 91 }
ansond 0:1f1f55e73248 92