mbed Connector Interface simplification API on top of mbed-client
Fork of mbedConnectorInterfaceV3 by
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!
source/ObjectInstanceManager.cpp@38:bb6d2be4d54c, 2016-06-14 (annotated)
- Committer:
- ansond
- Date:
- Tue Jun 14 20:18:30 2016 +0000
- Revision:
- 38:bb6d2be4d54c
- Parent:
- 33:1d0b855df5a5
- Child:
- 45:db754b994deb
cleaned up compile warnings
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ansond | 27:b8aaf7dc7023 | 1 | /** |
ansond | 27:b8aaf7dc7023 | 2 | * @file ObjectInstanceManager.cpp |
ansond | 27:b8aaf7dc7023 | 3 | * @brief mbed CoAP Endpoint Device Management Object Instance Manager |
ansond | 27:b8aaf7dc7023 | 4 | * @author Doug Anson |
ansond | 27:b8aaf7dc7023 | 5 | * @version 1.0 |
ansond | 27:b8aaf7dc7023 | 6 | * @see |
ansond | 27:b8aaf7dc7023 | 7 | * |
ansond | 27:b8aaf7dc7023 | 8 | * Copyright (c) 2016 |
ansond | 27:b8aaf7dc7023 | 9 | * |
ansond | 27:b8aaf7dc7023 | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ansond | 27:b8aaf7dc7023 | 11 | * you may not use this file except in compliance with the License. |
ansond | 27:b8aaf7dc7023 | 12 | * You may obtain a copy of the License at |
ansond | 27:b8aaf7dc7023 | 13 | * |
ansond | 27:b8aaf7dc7023 | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
ansond | 27:b8aaf7dc7023 | 15 | * |
ansond | 27:b8aaf7dc7023 | 16 | * Unless required by applicable law or agreed to in writing, software |
ansond | 27:b8aaf7dc7023 | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
ansond | 27:b8aaf7dc7023 | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ansond | 27:b8aaf7dc7023 | 19 | * See the License for the specific language governing permissions and |
ansond | 27:b8aaf7dc7023 | 20 | * limitations under the License. |
ansond | 27:b8aaf7dc7023 | 21 | */ |
ansond | 27:b8aaf7dc7023 | 22 | |
ansond | 33:1d0b855df5a5 | 23 | // Class support |
ansond | 33:1d0b855df5a5 | 24 | #include "mbed-connector-interface/ObjectInstanceManager.h" |
ansond | 27:b8aaf7dc7023 | 25 | |
ansond | 33:1d0b855df5a5 | 26 | // mbed-client support |
ansond | 27:b8aaf7dc7023 | 27 | #include "mbed-client/m2minterfacefactory.h" |
ansond | 27:b8aaf7dc7023 | 28 | #include "mbed-client/m2minterfaceobserver.h" |
ansond | 27:b8aaf7dc7023 | 29 | #include "mbed-client/m2minterface.h" |
ansond | 27:b8aaf7dc7023 | 30 | #include "mbed-client/m2mobjectinstance.h" |
ansond | 27:b8aaf7dc7023 | 31 | #include "mbed-client/m2mresource.h" |
ansond | 33:1d0b855df5a5 | 32 | #include "mbed-client/m2mdevice.h" |
ansond | 33:1d0b855df5a5 | 33 | #include "mbed-client/m2mfirmware.h" |
ansond | 27:b8aaf7dc7023 | 34 | |
ansond | 27:b8aaf7dc7023 | 35 | // string support |
ansond | 27:b8aaf7dc7023 | 36 | #include <string> |
ansond | 27:b8aaf7dc7023 | 37 | |
ansond | 27:b8aaf7dc7023 | 38 | // constructor |
ansond | 27:b8aaf7dc7023 | 39 | ObjectInstanceManager::ObjectInstanceManager(const Logger *logger,const void *ep) { |
ansond | 27:b8aaf7dc7023 | 40 | this->m_logger = (Logger *)logger; |
ansond | 27:b8aaf7dc7023 | 41 | this->m_ep = (void *)ep; |
ansond | 27:b8aaf7dc7023 | 42 | this->m_object_list.clear(); |
ansond | 27:b8aaf7dc7023 | 43 | } |
ansond | 27:b8aaf7dc7023 | 44 | |
ansond | 27:b8aaf7dc7023 | 45 | // copy constructor |
ansond | 27:b8aaf7dc7023 | 46 | ObjectInstanceManager::ObjectInstanceManager(const ObjectInstanceManager &oim) { |
ansond | 27:b8aaf7dc7023 | 47 | this->m_logger = oim.m_logger; |
ansond | 27:b8aaf7dc7023 | 48 | this->m_ep = oim.m_ep; |
ansond | 27:b8aaf7dc7023 | 49 | this->m_object_list = oim.m_object_list; |
ansond | 27:b8aaf7dc7023 | 50 | } |
ansond | 27:b8aaf7dc7023 | 51 | |
ansond | 27:b8aaf7dc7023 | 52 | // destructor |
ansond | 27:b8aaf7dc7023 | 53 | ObjectInstanceManager::~ObjectInstanceManager() { |
ansond | 27:b8aaf7dc7023 | 54 | this->m_object_list.clear(); |
ansond | 27:b8aaf7dc7023 | 55 | } |
ansond | 27:b8aaf7dc7023 | 56 | |
ansond | 27:b8aaf7dc7023 | 57 | // create a Dynamic Resource Instance |
ansond | 27:b8aaf7dc7023 | 58 | void *ObjectInstanceManager::createDynamicResourceInstance(char *objID,char *resID,char *resName,int resType,bool observable) { |
ansond | 27:b8aaf7dc7023 | 59 | void *res = NULL; |
ansond | 27:b8aaf7dc7023 | 60 | M2MObjectInstance *instance = (M2MObjectInstance *)this->getOrCreateInstance(objID,resID); |
ansond | 27:b8aaf7dc7023 | 61 | if (instance != NULL) { |
ansond | 27:b8aaf7dc7023 | 62 | // DEBUG |
ansond | 27:b8aaf7dc7023 | 63 | this->logger()->log("ObjectInstanceManager: Creating Dynamic Resource: ObjID:%s ResID:%s ResName:%s Type:%d Observable: %d",objID,resID,resName,resType,observable); |
ansond | 27:b8aaf7dc7023 | 64 | |
ansond | 27:b8aaf7dc7023 | 65 | // create the resource |
ansond | 27:b8aaf7dc7023 | 66 | res = (void *)instance->create_dynamic_resource(resID,resName,(M2MResourceInstance::ResourceType)resType,observable); |
ansond | 27:b8aaf7dc7023 | 67 | } |
ansond | 27:b8aaf7dc7023 | 68 | return res; |
ansond | 27:b8aaf7dc7023 | 69 | } |
ansond | 27:b8aaf7dc7023 | 70 | |
ansond | 27:b8aaf7dc7023 | 71 | // create a Static Resource Instance |
ansond | 27:b8aaf7dc7023 | 72 | void *ObjectInstanceManager::createStaticResourceInstance(char *objID,char *resID,char *resName,int resType,void *data,int data_length) { |
ansond | 27:b8aaf7dc7023 | 73 | void *res = NULL; |
ansond | 27:b8aaf7dc7023 | 74 | M2MObjectInstance *instance = (M2MObjectInstance *)this->getOrCreateInstance(objID,resID); |
ansond | 27:b8aaf7dc7023 | 75 | if (instance != NULL) { |
ansond | 27:b8aaf7dc7023 | 76 | // DEBUG |
ansond | 27:b8aaf7dc7023 | 77 | this->logger()->log("ObjectInstanceManager: Creating Static Resource: ObjID:%s ResID:%s ResName:%s Type:%d DataLength: %d",objID,resID,resName,resType,data_length); |
ansond | 27:b8aaf7dc7023 | 78 | |
ansond | 27:b8aaf7dc7023 | 79 | // create the resource |
ansond | 27:b8aaf7dc7023 | 80 | res = (void *)instance->create_static_resource(resID,resName,(M2MResourceInstance::ResourceType)resType,(uint8_t *)data,(uint8_t)data_length); |
ansond | 27:b8aaf7dc7023 | 81 | } |
ansond | 27:b8aaf7dc7023 | 82 | return res; |
ansond | 27:b8aaf7dc7023 | 83 | } |
ansond | 27:b8aaf7dc7023 | 84 | |
ansond | 27:b8aaf7dc7023 | 85 | // create and/or retrieve a given instance |
ansond | 27:b8aaf7dc7023 | 86 | void *ObjectInstanceManager::getOrCreateInstance(char *objID,char *resID) { |
ansond | 27:b8aaf7dc7023 | 87 | void *instance = NULL; |
ansond | 27:b8aaf7dc7023 | 88 | NamedPointer *obj_np = this->getOrCreateObject(objID); |
ansond | 27:b8aaf7dc7023 | 89 | if (obj_np != NULL) { |
ansond | 27:b8aaf7dc7023 | 90 | NamedPointerList *list = (NamedPointerList *)obj_np->list(); |
ansond | 27:b8aaf7dc7023 | 91 | NamedPointer *inst_np = this->getNamedPointer(resID,list); |
ansond | 27:b8aaf7dc7023 | 92 | if (inst_np != NULL) { |
ansond | 27:b8aaf7dc7023 | 93 | // instance already exists... so create another one (n)... |
ansond | 27:b8aaf7dc7023 | 94 | M2MObject *obj = (M2MObject *)(obj_np->ptr()); |
ansond | 27:b8aaf7dc7023 | 95 | if (obj != NULL) { |
ansond | 27:b8aaf7dc7023 | 96 | instance = (void *)obj->create_object_instance(); |
ansond | 27:b8aaf7dc7023 | 97 | NamedPointer new_inst_np(string(resID),instance); |
ansond | 27:b8aaf7dc7023 | 98 | list->push_back(new_inst_np); |
ansond | 27:b8aaf7dc7023 | 99 | } |
ansond | 27:b8aaf7dc7023 | 100 | } |
ansond | 27:b8aaf7dc7023 | 101 | else if (list->size() > 0) { |
ansond | 27:b8aaf7dc7023 | 102 | // 0th instance exists... parent the resource to it... |
ansond | 27:b8aaf7dc7023 | 103 | instance = (void *)(list->at(0).ptr()); |
ansond | 27:b8aaf7dc7023 | 104 | } |
ansond | 27:b8aaf7dc7023 | 105 | else { |
ansond | 27:b8aaf7dc7023 | 106 | // no instance does not exist so create one (0).. |
ansond | 27:b8aaf7dc7023 | 107 | M2MObject *obj = (M2MObject *)(obj_np->ptr()); |
ansond | 27:b8aaf7dc7023 | 108 | if (obj != NULL) { |
ansond | 27:b8aaf7dc7023 | 109 | instance = (void *)obj->create_object_instance(); |
ansond | 27:b8aaf7dc7023 | 110 | NamedPointer new_inst_np(string(resID),instance); |
ansond | 27:b8aaf7dc7023 | 111 | list->push_back(new_inst_np); |
ansond | 27:b8aaf7dc7023 | 112 | } |
ansond | 27:b8aaf7dc7023 | 113 | } |
ansond | 27:b8aaf7dc7023 | 114 | } |
ansond | 27:b8aaf7dc7023 | 115 | else { |
ansond | 27:b8aaf7dc7023 | 116 | // DEBUG |
ansond | 27:b8aaf7dc7023 | 117 | this->logger()->log("getOrCreateInstance: unable to create object instance for objID:%s",objID); |
ansond | 27:b8aaf7dc7023 | 118 | } |
ansond | 27:b8aaf7dc7023 | 119 | return instance; |
ansond | 27:b8aaf7dc7023 | 120 | } |
ansond | 27:b8aaf7dc7023 | 121 | |
ansond | 27:b8aaf7dc7023 | 122 | // create and/or retrieve a given objectID |
ansond | 27:b8aaf7dc7023 | 123 | NamedPointer *ObjectInstanceManager::getOrCreateObject(char *objID) { |
ansond | 27:b8aaf7dc7023 | 124 | NamedPointer *result = NULL; |
ansond | 27:b8aaf7dc7023 | 125 | if (objID != NULL) { |
ansond | 27:b8aaf7dc7023 | 126 | result = this->getNamedPointer(objID,&(this->m_object_list)); |
ansond | 27:b8aaf7dc7023 | 127 | } |
ansond | 27:b8aaf7dc7023 | 128 | if (result == NULL) { |
ansond | 27:b8aaf7dc7023 | 129 | void *obj = (void *)M2MInterfaceFactory::create_object(objID); |
ansond | 27:b8aaf7dc7023 | 130 | NamedPointer new_np(string(objID),obj); |
ansond | 27:b8aaf7dc7023 | 131 | this->m_object_list.push_back(new_np); |
ansond | 27:b8aaf7dc7023 | 132 | result = this->getNamedPointer(objID,&(this->m_object_list)); |
ansond | 27:b8aaf7dc7023 | 133 | } |
ansond | 27:b8aaf7dc7023 | 134 | return result; |
ansond | 27:b8aaf7dc7023 | 135 | } |
ansond | 27:b8aaf7dc7023 | 136 | |
ansond | 27:b8aaf7dc7023 | 137 | // get the named pointer for a given id |
ansond | 27:b8aaf7dc7023 | 138 | NamedPointer *ObjectInstanceManager::getNamedPointer(const char *id,NamedPointerList *list) { |
ansond | 27:b8aaf7dc7023 | 139 | NamedPointer *result = NULL; |
ansond | 27:b8aaf7dc7023 | 140 | bool found = false; |
ansond | 38:bb6d2be4d54c | 141 | for(int i=0;list != NULL && i<(int)list->size() && !found;++i) { |
ansond | 27:b8aaf7dc7023 | 142 | char *tmp_id = (char *)list->at(i).name().c_str(); |
ansond | 27:b8aaf7dc7023 | 143 | if (id != NULL && tmp_id != NULL && strcmp(id,tmp_id) == 0) { |
ansond | 27:b8aaf7dc7023 | 144 | found = true; |
ansond | 27:b8aaf7dc7023 | 145 | result = &(list->at(i)); |
ansond | 27:b8aaf7dc7023 | 146 | } |
ansond | 27:b8aaf7dc7023 | 147 | } |
ansond | 27:b8aaf7dc7023 | 148 | return result; |
ansond | 27:b8aaf7dc7023 | 149 | } |
ansond | 27:b8aaf7dc7023 | 150 | |
ansond | 27:b8aaf7dc7023 | 151 | // Get our Object List |
ansond | 27:b8aaf7dc7023 | 152 | NamedPointerList ObjectInstanceManager::getObjectList() { |
ansond | 27:b8aaf7dc7023 | 153 | return this->m_object_list; |
ansond | 27:b8aaf7dc7023 | 154 | } |
ansond | 27:b8aaf7dc7023 | 155 | |
ansond | 27:b8aaf7dc7023 | 156 | // Logger |
ansond | 27:b8aaf7dc7023 | 157 | Logger *ObjectInstanceManager::logger() { |
ansond | 27:b8aaf7dc7023 | 158 | return this->m_logger; |
ansond | 27:b8aaf7dc7023 | 159 | } |
ansond | 27:b8aaf7dc7023 | 160 |