use TCP to connect to mbed connector
Fork of mbedConnectorInterfaceWithDM by
source/ObjectInstanceManager.cpp@27:b8aaf7dc7023, 2016-06-14 (annotated)
- Committer:
- ansond
- Date:
- Tue Jun 14 04:01:34 2016 +0000
- Revision:
- 27:b8aaf7dc7023
- Child:
- 33:1d0b855df5a5
Initial merge and update of device and firmware objects
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 | 27:b8aaf7dc7023 | 23 | // Class support |
ansond | 27:b8aaf7dc7023 | 24 | #include "mbed-connector-interface/ObjectInstanceManager.h" |
ansond | 27:b8aaf7dc7023 | 25 | |
ansond | 27:b8aaf7dc7023 | 26 | // mbed-client support |
ansond | 27:b8aaf7dc7023 | 27 | #include "mbed-client/m2minterfacefactory.h" |
ansond | 27:b8aaf7dc7023 | 28 | #include "mbed-client/m2mdevice.h" |
ansond | 27:b8aaf7dc7023 | 29 | #include "mbed-client/m2minterfaceobserver.h" |
ansond | 27:b8aaf7dc7023 | 30 | #include "mbed-client/m2minterface.h" |
ansond | 27:b8aaf7dc7023 | 31 | #include "mbed-client/m2mobjectinstance.h" |
ansond | 27:b8aaf7dc7023 | 32 | #include "mbed-client/m2mresource.h" |
ansond | 27:b8aaf7dc7023 | 33 | |
ansond | 27:b8aaf7dc7023 | 34 | // string support |
ansond | 27:b8aaf7dc7023 | 35 | #include <string> |
ansond | 27:b8aaf7dc7023 | 36 | |
ansond | 27:b8aaf7dc7023 | 37 | // constructor |
ansond | 27:b8aaf7dc7023 | 38 | ObjectInstanceManager::ObjectInstanceManager(const Logger *logger,const void *ep) { |
ansond | 27:b8aaf7dc7023 | 39 | this->m_logger = (Logger *)logger; |
ansond | 27:b8aaf7dc7023 | 40 | this->m_ep = (void *)ep; |
ansond | 27:b8aaf7dc7023 | 41 | this->m_object_list.clear(); |
ansond | 27:b8aaf7dc7023 | 42 | } |
ansond | 27:b8aaf7dc7023 | 43 | |
ansond | 27:b8aaf7dc7023 | 44 | // copy constructor |
ansond | 27:b8aaf7dc7023 | 45 | ObjectInstanceManager::ObjectInstanceManager(const ObjectInstanceManager &oim) { |
ansond | 27:b8aaf7dc7023 | 46 | this->m_logger = oim.m_logger; |
ansond | 27:b8aaf7dc7023 | 47 | this->m_ep = oim.m_ep; |
ansond | 27:b8aaf7dc7023 | 48 | this->m_object_list = oim.m_object_list; |
ansond | 27:b8aaf7dc7023 | 49 | } |
ansond | 27:b8aaf7dc7023 | 50 | |
ansond | 27:b8aaf7dc7023 | 51 | // destructor |
ansond | 27:b8aaf7dc7023 | 52 | ObjectInstanceManager::~ObjectInstanceManager() { |
ansond | 27:b8aaf7dc7023 | 53 | this->m_object_list.clear(); |
ansond | 27:b8aaf7dc7023 | 54 | } |
ansond | 27:b8aaf7dc7023 | 55 | |
ansond | 27:b8aaf7dc7023 | 56 | // create a Dynamic Resource Instance |
ansond | 27:b8aaf7dc7023 | 57 | void *ObjectInstanceManager::createDynamicResourceInstance(char *objID,char *resID,char *resName,int resType,bool observable) { |
ansond | 27:b8aaf7dc7023 | 58 | void *res = NULL; |
ansond | 27:b8aaf7dc7023 | 59 | M2MObjectInstance *instance = (M2MObjectInstance *)this->getOrCreateInstance(objID,resID); |
ansond | 27:b8aaf7dc7023 | 60 | if (instance != NULL) { |
ansond | 27:b8aaf7dc7023 | 61 | // DEBUG |
ansond | 27:b8aaf7dc7023 | 62 | 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 | 63 | |
ansond | 27:b8aaf7dc7023 | 64 | // create the resource |
ansond | 27:b8aaf7dc7023 | 65 | res = (void *)instance->create_dynamic_resource(resID,resName,(M2MResourceInstance::ResourceType)resType,observable); |
ansond | 27:b8aaf7dc7023 | 66 | } |
ansond | 27:b8aaf7dc7023 | 67 | return res; |
ansond | 27:b8aaf7dc7023 | 68 | } |
ansond | 27:b8aaf7dc7023 | 69 | |
ansond | 27:b8aaf7dc7023 | 70 | // create a Static Resource Instance |
ansond | 27:b8aaf7dc7023 | 71 | void *ObjectInstanceManager::createStaticResourceInstance(char *objID,char *resID,char *resName,int resType,void *data,int data_length) { |
ansond | 27:b8aaf7dc7023 | 72 | void *res = NULL; |
ansond | 27:b8aaf7dc7023 | 73 | M2MObjectInstance *instance = (M2MObjectInstance *)this->getOrCreateInstance(objID,resID); |
ansond | 27:b8aaf7dc7023 | 74 | if (instance != NULL) { |
ansond | 27:b8aaf7dc7023 | 75 | // DEBUG |
ansond | 27:b8aaf7dc7023 | 76 | 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 | 77 | |
ansond | 27:b8aaf7dc7023 | 78 | // create the resource |
ansond | 27:b8aaf7dc7023 | 79 | res = (void *)instance->create_static_resource(resID,resName,(M2MResourceInstance::ResourceType)resType,(uint8_t *)data,(uint8_t)data_length); |
ansond | 27:b8aaf7dc7023 | 80 | } |
ansond | 27:b8aaf7dc7023 | 81 | return res; |
ansond | 27:b8aaf7dc7023 | 82 | } |
ansond | 27:b8aaf7dc7023 | 83 | |
ansond | 27:b8aaf7dc7023 | 84 | // create and/or retrieve a given instance |
ansond | 27:b8aaf7dc7023 | 85 | void *ObjectInstanceManager::getOrCreateInstance(char *objID,char *resID) { |
ansond | 27:b8aaf7dc7023 | 86 | void *instance = NULL; |
ansond | 27:b8aaf7dc7023 | 87 | NamedPointer *obj_np = this->getOrCreateObject(objID); |
ansond | 27:b8aaf7dc7023 | 88 | if (obj_np != NULL) { |
ansond | 27:b8aaf7dc7023 | 89 | NamedPointerList *list = (NamedPointerList *)obj_np->list(); |
ansond | 27:b8aaf7dc7023 | 90 | NamedPointer *inst_np = this->getNamedPointer(resID,list); |
ansond | 27:b8aaf7dc7023 | 91 | if (inst_np != NULL) { |
ansond | 27:b8aaf7dc7023 | 92 | // instance already exists... so create another one (n)... |
ansond | 27:b8aaf7dc7023 | 93 | M2MObject *obj = (M2MObject *)(obj_np->ptr()); |
ansond | 27:b8aaf7dc7023 | 94 | if (obj != NULL) { |
ansond | 27:b8aaf7dc7023 | 95 | instance = (void *)obj->create_object_instance(); |
ansond | 27:b8aaf7dc7023 | 96 | NamedPointer new_inst_np(string(resID),instance); |
ansond | 27:b8aaf7dc7023 | 97 | list->push_back(new_inst_np); |
ansond | 27:b8aaf7dc7023 | 98 | } |
ansond | 27:b8aaf7dc7023 | 99 | } |
ansond | 27:b8aaf7dc7023 | 100 | else if (list->size() > 0) { |
ansond | 27:b8aaf7dc7023 | 101 | // 0th instance exists... parent the resource to it... |
ansond | 27:b8aaf7dc7023 | 102 | instance = (void *)(list->at(0).ptr()); |
ansond | 27:b8aaf7dc7023 | 103 | } |
ansond | 27:b8aaf7dc7023 | 104 | else { |
ansond | 27:b8aaf7dc7023 | 105 | // no instance does not exist so create one (0).. |
ansond | 27:b8aaf7dc7023 | 106 | M2MObject *obj = (M2MObject *)(obj_np->ptr()); |
ansond | 27:b8aaf7dc7023 | 107 | if (obj != NULL) { |
ansond | 27:b8aaf7dc7023 | 108 | instance = (void *)obj->create_object_instance(); |
ansond | 27:b8aaf7dc7023 | 109 | NamedPointer new_inst_np(string(resID),instance); |
ansond | 27:b8aaf7dc7023 | 110 | list->push_back(new_inst_np); |
ansond | 27:b8aaf7dc7023 | 111 | } |
ansond | 27:b8aaf7dc7023 | 112 | } |
ansond | 27:b8aaf7dc7023 | 113 | } |
ansond | 27:b8aaf7dc7023 | 114 | else { |
ansond | 27:b8aaf7dc7023 | 115 | // DEBUG |
ansond | 27:b8aaf7dc7023 | 116 | this->logger()->log("getOrCreateInstance: unable to create object instance for objID:%s",objID); |
ansond | 27:b8aaf7dc7023 | 117 | } |
ansond | 27:b8aaf7dc7023 | 118 | return instance; |
ansond | 27:b8aaf7dc7023 | 119 | } |
ansond | 27:b8aaf7dc7023 | 120 | |
ansond | 27:b8aaf7dc7023 | 121 | // create and/or retrieve a given objectID |
ansond | 27:b8aaf7dc7023 | 122 | NamedPointer *ObjectInstanceManager::getOrCreateObject(char *objID) { |
ansond | 27:b8aaf7dc7023 | 123 | NamedPointer *result = NULL; |
ansond | 27:b8aaf7dc7023 | 124 | if (objID != NULL) { |
ansond | 27:b8aaf7dc7023 | 125 | result = this->getNamedPointer(objID,&(this->m_object_list)); |
ansond | 27:b8aaf7dc7023 | 126 | } |
ansond | 27:b8aaf7dc7023 | 127 | if (result == NULL) { |
ansond | 27:b8aaf7dc7023 | 128 | void *obj = (void *)M2MInterfaceFactory::create_object(objID); |
ansond | 27:b8aaf7dc7023 | 129 | NamedPointer new_np(string(objID),obj); |
ansond | 27:b8aaf7dc7023 | 130 | this->m_object_list.push_back(new_np); |
ansond | 27:b8aaf7dc7023 | 131 | result = this->getNamedPointer(objID,&(this->m_object_list)); |
ansond | 27:b8aaf7dc7023 | 132 | } |
ansond | 27:b8aaf7dc7023 | 133 | return result; |
ansond | 27:b8aaf7dc7023 | 134 | } |
ansond | 27:b8aaf7dc7023 | 135 | |
ansond | 27:b8aaf7dc7023 | 136 | // get the named pointer for a given id |
ansond | 27:b8aaf7dc7023 | 137 | NamedPointer *ObjectInstanceManager::getNamedPointer(const char *id,NamedPointerList *list) { |
ansond | 27:b8aaf7dc7023 | 138 | NamedPointer *result = NULL; |
ansond | 27:b8aaf7dc7023 | 139 | bool found = false; |
ansond | 27:b8aaf7dc7023 | 140 | for(int i=0;list != NULL && i<list->size() && !found;++i) { |
ansond | 27:b8aaf7dc7023 | 141 | char *tmp_id = (char *)list->at(i).name().c_str(); |
ansond | 27:b8aaf7dc7023 | 142 | if (id != NULL && tmp_id != NULL && strcmp(id,tmp_id) == 0) { |
ansond | 27:b8aaf7dc7023 | 143 | found = true; |
ansond | 27:b8aaf7dc7023 | 144 | result = &(list->at(i)); |
ansond | 27:b8aaf7dc7023 | 145 | } |
ansond | 27:b8aaf7dc7023 | 146 | } |
ansond | 27:b8aaf7dc7023 | 147 | return result; |
ansond | 27:b8aaf7dc7023 | 148 | } |
ansond | 27:b8aaf7dc7023 | 149 | |
ansond | 27:b8aaf7dc7023 | 150 | // Get our Object List |
ansond | 27:b8aaf7dc7023 | 151 | NamedPointerList ObjectInstanceManager::getObjectList() { |
ansond | 27:b8aaf7dc7023 | 152 | return this->m_object_list; |
ansond | 27:b8aaf7dc7023 | 153 | } |
ansond | 27:b8aaf7dc7023 | 154 | |
ansond | 27:b8aaf7dc7023 | 155 | // Logger |
ansond | 27:b8aaf7dc7023 | 156 | Logger *ObjectInstanceManager::logger() { |
ansond | 27:b8aaf7dc7023 | 157 | return this->m_logger; |
ansond | 27:b8aaf7dc7023 | 158 | } |
ansond | 27:b8aaf7dc7023 | 159 |