Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbedConnectorInterfaceWithDM by
ObjectInstanceManager.cpp
00001 /** 00002 * @file ObjectInstanceManager.cpp 00003 * @brief mbed CoAP Endpoint Device Management Object Instance Manager 00004 * @author Doug Anson 00005 * @version 1.0 00006 * @see 00007 * 00008 * Copyright (c) 2016 00009 * 00010 * Licensed under the Apache License, Version 2.0 (the "License"); 00011 * you may not use this file except in compliance with the License. 00012 * You may obtain a copy of the License at 00013 * 00014 * http://www.apache.org/licenses/LICENSE-2.0 00015 * 00016 * Unless required by applicable law or agreed to in writing, software 00017 * distributed under the License is distributed on an "AS IS" BASIS, 00018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00019 * See the License for the specific language governing permissions and 00020 * limitations under the License. 00021 */ 00022 00023 // Class support 00024 #include "mbed-connector-interface/ObjectInstanceManager.h" 00025 00026 // mbed-client support 00027 #include "mbed-client/m2minterfacefactory.h" 00028 #include "mbed-client/m2minterfaceobserver.h" 00029 #include "mbed-client/m2minterface.h" 00030 #include "mbed-client/m2mobjectinstance.h" 00031 #include "mbed-client/m2mresource.h" 00032 #include "mbed-client/m2mdevice.h" 00033 #include "mbed-client/m2mfirmware.h" 00034 00035 // string support 00036 #include <string> 00037 00038 // constructor 00039 ObjectInstanceManager::ObjectInstanceManager(const Logger *logger,const void *ep) { 00040 this->m_logger = (Logger *)logger; 00041 this->m_ep = (void *)ep; 00042 this->m_object_list.clear(); 00043 this->m_instance_number = 0; 00044 } 00045 00046 // copy constructor 00047 ObjectInstanceManager::ObjectInstanceManager(const ObjectInstanceManager &oim) { 00048 this->m_logger = oim.m_logger; 00049 this->m_ep = oim.m_ep; 00050 this->m_object_list = oim.m_object_list; 00051 this->m_instance_number = oim.m_instance_number; 00052 } 00053 00054 // destructor 00055 ObjectInstanceManager::~ObjectInstanceManager() { 00056 this->m_object_list.clear(); 00057 } 00058 00059 // create a Dynamic Resource Instance 00060 void *ObjectInstanceManager::createDynamicResourceInstance(char *objID,char *resID,char *resName,int resType,bool observable) { 00061 void *res = NULL; 00062 M2MObjectInstance *instance = (M2MObjectInstance *)this->getOrCreateInstance(objID,resID); 00063 if (instance != NULL) { 00064 // DEBUG 00065 //this->logger()->log("ObjectInstanceManager: Creating Dynamic Resource: ObjID:%s ResID:%s ResName:%s Type:%d Observable: %d",objID,resID,resName,resType,observable); 00066 00067 // create the resource 00068 res = (void *)instance->create_dynamic_resource(resID,resName,(M2MResourceInstance::ResourceType)resType,observable); 00069 } 00070 return res; 00071 } 00072 00073 // create a Static Resource Instance 00074 void *ObjectInstanceManager::createStaticResourceInstance(char *objID,char *resID,char *resName,int resType,void *data,int data_length) { 00075 void *res = NULL; 00076 M2MObjectInstance *instance = (M2MObjectInstance *)this->getOrCreateInstance(objID,resID); 00077 if (instance != NULL) { 00078 // DEBUG 00079 //this->logger()->log("ObjectInstanceManager: Creating Static Resource: ObjID:%s ResID:%s ResName:%s Type:%d DataLength: %d",objID,resID,resName,resType,data_length); 00080 00081 // create the resource 00082 res = (void *)instance->create_static_resource(resID,resName,(M2MResourceInstance::ResourceType)resType,(uint8_t *)data,(uint8_t)data_length); 00083 } 00084 return res; 00085 } 00086 00087 // Get the instance number of the resource just created 00088 int ObjectInstanceManager::getLastCreatedInstanceNumber() { 00089 return this->m_instance_number; 00090 } 00091 00092 // create and/or retrieve a given instance 00093 void *ObjectInstanceManager::getOrCreateInstance(char *objID,char *resID) { 00094 void *instance = NULL; 00095 NamedPointer *obj_np = this->getOrCreateObject(objID); 00096 if (obj_np != NULL) { 00097 NamedPointerList *list = (NamedPointerList *)obj_np->list(); 00098 NamedPointer *inst_np = this->getNamedPointer(resID,list); 00099 if (inst_np != NULL) { 00100 // instance already exists... so create another one (n)... 00101 M2MObject *obj = (M2MObject *)(obj_np->ptr()); 00102 if (obj != NULL) { 00103 instance = (void *)obj->create_object_instance(); 00104 this->m_instance_number = inst_np->index()+1; 00105 NamedPointer new_inst_np(string(resID),instance,this->m_instance_number); 00106 list->push_back(new_inst_np); 00107 } 00108 } 00109 else if (list->size() > 0) { 00110 // 0th instance exists... parent the resource to it... 00111 this->m_instance_number = 0; 00112 instance = (void *)(list->at(0).ptr()); 00113 } 00114 else { 00115 // no instance does not exist so create one (0).. 00116 M2MObject *obj = (M2MObject *)(obj_np->ptr()); 00117 if (obj != NULL) { 00118 instance = (void *)obj->create_object_instance(); 00119 this->m_instance_number = 0; 00120 NamedPointer new_inst_np(string(resID),instance,this->m_instance_number); 00121 list->push_back(new_inst_np); 00122 } 00123 } 00124 } 00125 else { 00126 // DEBUG 00127 this->logger()->log("getOrCreateInstance: unable to create object instance for objID:%s",objID); 00128 } 00129 return instance; 00130 } 00131 00132 // create and/or retrieve a given objectID 00133 NamedPointer *ObjectInstanceManager::getOrCreateObject(char *objID) { 00134 NamedPointer *result = NULL; 00135 if (objID != NULL) { 00136 result = this->getNamedPointer(objID,&(this->m_object_list)); 00137 } 00138 if (result == NULL) { 00139 void *obj = (void *)M2MInterfaceFactory::create_object(objID); 00140 NamedPointer new_np(string(objID),obj,0); 00141 this->m_object_list.push_back(new_np); 00142 result = this->getNamedPointer(objID,&(this->m_object_list)); 00143 } 00144 return result; 00145 } 00146 00147 // get the named pointer for a given id 00148 NamedPointer *ObjectInstanceManager::getNamedPointer(const char *id,NamedPointerList *list) { 00149 NamedPointer *result = NULL; 00150 bool found = false; 00151 for(int i=0;list != NULL && i<(int)list->size() && !found;++i) { 00152 char *tmp_id = (char *)list->at(i).name().c_str(); 00153 if (id != NULL && tmp_id != NULL && strcmp(id,tmp_id) == 0) { 00154 found = true; 00155 result = &(list->at(i)); 00156 } 00157 } 00158 return result; 00159 } 00160 00161 // Get our Object List 00162 NamedPointerList ObjectInstanceManager::getObjectList() { 00163 return this->m_object_list; 00164 } 00165 00166 // Logger 00167 Logger *ObjectInstanceManager::logger() { 00168 return this->m_logger; 00169 } 00170
Generated on Wed Jul 13 2022 01:24:02 by
