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@45:db754b994deb, 2016-06-15 (annotated)
- Committer:
- ansond
- Date:
- Wed Jun 15 20:51:13 2016 +0000
- Revision:
- 45:db754b994deb
- Parent:
- 38:bb6d2be4d54c
updates to instance number reporting
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 | 45:db754b994deb | 43 | this->m_instance_number = 0; |
ansond | 27:b8aaf7dc7023 | 44 | } |
ansond | 27:b8aaf7dc7023 | 45 | |
ansond | 27:b8aaf7dc7023 | 46 | // copy constructor |
ansond | 27:b8aaf7dc7023 | 47 | ObjectInstanceManager::ObjectInstanceManager(const ObjectInstanceManager &oim) { |
ansond | 27:b8aaf7dc7023 | 48 | this->m_logger = oim.m_logger; |
ansond | 27:b8aaf7dc7023 | 49 | this->m_ep = oim.m_ep; |
ansond | 27:b8aaf7dc7023 | 50 | this->m_object_list = oim.m_object_list; |
ansond | 45:db754b994deb | 51 | this->m_instance_number = oim.m_instance_number; |
ansond | 27:b8aaf7dc7023 | 52 | } |
ansond | 27:b8aaf7dc7023 | 53 | |
ansond | 27:b8aaf7dc7023 | 54 | // destructor |
ansond | 27:b8aaf7dc7023 | 55 | ObjectInstanceManager::~ObjectInstanceManager() { |
ansond | 27:b8aaf7dc7023 | 56 | this->m_object_list.clear(); |
ansond | 27:b8aaf7dc7023 | 57 | } |
ansond | 27:b8aaf7dc7023 | 58 | |
ansond | 27:b8aaf7dc7023 | 59 | // create a Dynamic Resource Instance |
ansond | 27:b8aaf7dc7023 | 60 | void *ObjectInstanceManager::createDynamicResourceInstance(char *objID,char *resID,char *resName,int resType,bool observable) { |
ansond | 27:b8aaf7dc7023 | 61 | void *res = NULL; |
ansond | 27:b8aaf7dc7023 | 62 | M2MObjectInstance *instance = (M2MObjectInstance *)this->getOrCreateInstance(objID,resID); |
ansond | 27:b8aaf7dc7023 | 63 | if (instance != NULL) { |
ansond | 27:b8aaf7dc7023 | 64 | // DEBUG |
ansond | 45:db754b994deb | 65 | //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 | 66 | |
ansond | 27:b8aaf7dc7023 | 67 | // create the resource |
ansond | 27:b8aaf7dc7023 | 68 | res = (void *)instance->create_dynamic_resource(resID,resName,(M2MResourceInstance::ResourceType)resType,observable); |
ansond | 27:b8aaf7dc7023 | 69 | } |
ansond | 27:b8aaf7dc7023 | 70 | return res; |
ansond | 27:b8aaf7dc7023 | 71 | } |
ansond | 27:b8aaf7dc7023 | 72 | |
ansond | 27:b8aaf7dc7023 | 73 | // create a Static Resource Instance |
ansond | 27:b8aaf7dc7023 | 74 | void *ObjectInstanceManager::createStaticResourceInstance(char *objID,char *resID,char *resName,int resType,void *data,int data_length) { |
ansond | 27:b8aaf7dc7023 | 75 | void *res = NULL; |
ansond | 27:b8aaf7dc7023 | 76 | M2MObjectInstance *instance = (M2MObjectInstance *)this->getOrCreateInstance(objID,resID); |
ansond | 27:b8aaf7dc7023 | 77 | if (instance != NULL) { |
ansond | 27:b8aaf7dc7023 | 78 | // DEBUG |
ansond | 45:db754b994deb | 79 | //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 | 80 | |
ansond | 27:b8aaf7dc7023 | 81 | // create the resource |
ansond | 27:b8aaf7dc7023 | 82 | res = (void *)instance->create_static_resource(resID,resName,(M2MResourceInstance::ResourceType)resType,(uint8_t *)data,(uint8_t)data_length); |
ansond | 27:b8aaf7dc7023 | 83 | } |
ansond | 27:b8aaf7dc7023 | 84 | return res; |
ansond | 27:b8aaf7dc7023 | 85 | } |
ansond | 27:b8aaf7dc7023 | 86 | |
ansond | 45:db754b994deb | 87 | // Get the instance number of the resource just created |
ansond | 45:db754b994deb | 88 | int ObjectInstanceManager::getLastCreatedInstanceNumber() { |
ansond | 45:db754b994deb | 89 | return this->m_instance_number; |
ansond | 45:db754b994deb | 90 | } |
ansond | 45:db754b994deb | 91 | |
ansond | 27:b8aaf7dc7023 | 92 | // create and/or retrieve a given instance |
ansond | 27:b8aaf7dc7023 | 93 | void *ObjectInstanceManager::getOrCreateInstance(char *objID,char *resID) { |
ansond | 27:b8aaf7dc7023 | 94 | void *instance = NULL; |
ansond | 27:b8aaf7dc7023 | 95 | NamedPointer *obj_np = this->getOrCreateObject(objID); |
ansond | 27:b8aaf7dc7023 | 96 | if (obj_np != NULL) { |
ansond | 27:b8aaf7dc7023 | 97 | NamedPointerList *list = (NamedPointerList *)obj_np->list(); |
ansond | 27:b8aaf7dc7023 | 98 | NamedPointer *inst_np = this->getNamedPointer(resID,list); |
ansond | 27:b8aaf7dc7023 | 99 | if (inst_np != NULL) { |
ansond | 27:b8aaf7dc7023 | 100 | // instance already exists... so create another one (n)... |
ansond | 27:b8aaf7dc7023 | 101 | M2MObject *obj = (M2MObject *)(obj_np->ptr()); |
ansond | 27:b8aaf7dc7023 | 102 | if (obj != NULL) { |
ansond | 27:b8aaf7dc7023 | 103 | instance = (void *)obj->create_object_instance(); |
ansond | 45:db754b994deb | 104 | this->m_instance_number = inst_np->index()+1; |
ansond | 45:db754b994deb | 105 | NamedPointer new_inst_np(string(resID),instance,this->m_instance_number); |
ansond | 27:b8aaf7dc7023 | 106 | list->push_back(new_inst_np); |
ansond | 27:b8aaf7dc7023 | 107 | } |
ansond | 27:b8aaf7dc7023 | 108 | } |
ansond | 27:b8aaf7dc7023 | 109 | else if (list->size() > 0) { |
ansond | 27:b8aaf7dc7023 | 110 | // 0th instance exists... parent the resource to it... |
ansond | 45:db754b994deb | 111 | this->m_instance_number = 0; |
ansond | 27:b8aaf7dc7023 | 112 | instance = (void *)(list->at(0).ptr()); |
ansond | 27:b8aaf7dc7023 | 113 | } |
ansond | 27:b8aaf7dc7023 | 114 | else { |
ansond | 27:b8aaf7dc7023 | 115 | // no instance does not exist so create one (0).. |
ansond | 27:b8aaf7dc7023 | 116 | M2MObject *obj = (M2MObject *)(obj_np->ptr()); |
ansond | 27:b8aaf7dc7023 | 117 | if (obj != NULL) { |
ansond | 27:b8aaf7dc7023 | 118 | instance = (void *)obj->create_object_instance(); |
ansond | 45:db754b994deb | 119 | this->m_instance_number = 0; |
ansond | 45:db754b994deb | 120 | NamedPointer new_inst_np(string(resID),instance,this->m_instance_number); |
ansond | 27:b8aaf7dc7023 | 121 | list->push_back(new_inst_np); |
ansond | 27:b8aaf7dc7023 | 122 | } |
ansond | 27:b8aaf7dc7023 | 123 | } |
ansond | 27:b8aaf7dc7023 | 124 | } |
ansond | 27:b8aaf7dc7023 | 125 | else { |
ansond | 27:b8aaf7dc7023 | 126 | // DEBUG |
ansond | 27:b8aaf7dc7023 | 127 | this->logger()->log("getOrCreateInstance: unable to create object instance for objID:%s",objID); |
ansond | 27:b8aaf7dc7023 | 128 | } |
ansond | 27:b8aaf7dc7023 | 129 | return instance; |
ansond | 27:b8aaf7dc7023 | 130 | } |
ansond | 27:b8aaf7dc7023 | 131 | |
ansond | 27:b8aaf7dc7023 | 132 | // create and/or retrieve a given objectID |
ansond | 27:b8aaf7dc7023 | 133 | NamedPointer *ObjectInstanceManager::getOrCreateObject(char *objID) { |
ansond | 27:b8aaf7dc7023 | 134 | NamedPointer *result = NULL; |
ansond | 27:b8aaf7dc7023 | 135 | if (objID != NULL) { |
ansond | 27:b8aaf7dc7023 | 136 | result = this->getNamedPointer(objID,&(this->m_object_list)); |
ansond | 27:b8aaf7dc7023 | 137 | } |
ansond | 27:b8aaf7dc7023 | 138 | if (result == NULL) { |
ansond | 27:b8aaf7dc7023 | 139 | void *obj = (void *)M2MInterfaceFactory::create_object(objID); |
ansond | 45:db754b994deb | 140 | NamedPointer new_np(string(objID),obj,0); |
ansond | 27:b8aaf7dc7023 | 141 | this->m_object_list.push_back(new_np); |
ansond | 27:b8aaf7dc7023 | 142 | result = this->getNamedPointer(objID,&(this->m_object_list)); |
ansond | 27:b8aaf7dc7023 | 143 | } |
ansond | 27:b8aaf7dc7023 | 144 | return result; |
ansond | 27:b8aaf7dc7023 | 145 | } |
ansond | 27:b8aaf7dc7023 | 146 | |
ansond | 27:b8aaf7dc7023 | 147 | // get the named pointer for a given id |
ansond | 27:b8aaf7dc7023 | 148 | NamedPointer *ObjectInstanceManager::getNamedPointer(const char *id,NamedPointerList *list) { |
ansond | 27:b8aaf7dc7023 | 149 | NamedPointer *result = NULL; |
ansond | 27:b8aaf7dc7023 | 150 | bool found = false; |
ansond | 38:bb6d2be4d54c | 151 | for(int i=0;list != NULL && i<(int)list->size() && !found;++i) { |
ansond | 27:b8aaf7dc7023 | 152 | char *tmp_id = (char *)list->at(i).name().c_str(); |
ansond | 27:b8aaf7dc7023 | 153 | if (id != NULL && tmp_id != NULL && strcmp(id,tmp_id) == 0) { |
ansond | 27:b8aaf7dc7023 | 154 | found = true; |
ansond | 27:b8aaf7dc7023 | 155 | result = &(list->at(i)); |
ansond | 27:b8aaf7dc7023 | 156 | } |
ansond | 27:b8aaf7dc7023 | 157 | } |
ansond | 27:b8aaf7dc7023 | 158 | return result; |
ansond | 27:b8aaf7dc7023 | 159 | } |
ansond | 27:b8aaf7dc7023 | 160 | |
ansond | 27:b8aaf7dc7023 | 161 | // Get our Object List |
ansond | 27:b8aaf7dc7023 | 162 | NamedPointerList ObjectInstanceManager::getObjectList() { |
ansond | 27:b8aaf7dc7023 | 163 | return this->m_object_list; |
ansond | 27:b8aaf7dc7023 | 164 | } |
ansond | 27:b8aaf7dc7023 | 165 | |
ansond | 27:b8aaf7dc7023 | 166 | // Logger |
ansond | 27:b8aaf7dc7023 | 167 | Logger *ObjectInstanceManager::logger() { |
ansond | 27:b8aaf7dc7023 | 168 | return this->m_logger; |
ansond | 27:b8aaf7dc7023 | 169 | } |
ansond | 27:b8aaf7dc7023 | 170 |