From 06037f8bf6c54eb592a5fbd8b5a90f490171dbf5 Mon Sep 17 00:00:00 2001 From: h2zero Date: Mon, 7 Sep 2020 10:16:49 -0600 Subject: [PATCH] Return NimBLEMeshModel pointer from NimBLEMeshElement::createModel --- src/NimBLEMeshElement.cpp | 27 ++++++++++++++++++++------- src/NimBLEMeshElement.h | 4 +++- src/NimBLEMeshModel.cpp | 1 - 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/NimBLEMeshElement.cpp b/src/NimBLEMeshElement.cpp index 92d08b5..ef40f67 100644 --- a/src/NimBLEMeshElement.cpp +++ b/src/NimBLEMeshElement.cpp @@ -22,6 +22,8 @@ NimBLEMeshElement::~NimBLEMeshElement() { for(auto &it : m_modelsVec) { delete (NimBLEMeshModel*)it.user_data; } + + m_modelsVec.clear(); } /** @@ -29,12 +31,10 @@ NimBLEMeshElement::~NimBLEMeshElement() { * @param [in] type The type of model to create. * @param [in] pCallbacks a pointer to a callback instance for this model. */ -void NimBLEMeshElement::createModel(uint16_t type, NimBLEMeshModelCallbacks *pCallbacks) { - for(auto &it : m_modelsVec) { - if(it.id == type) { - NIMBLE_LOGE(LOG_TAG, "Error: element already has a type %04x model", type); - return; - } +NimBLEMeshModel* NimBLEMeshElement::createModel(uint16_t type, NimBLEMeshModelCallbacks *pCallbacks) { + if(getModel(type) != nullptr) { + NIMBLE_LOGE(LOG_TAG, "Error: element already has a type %04x model", type); + return nullptr; } NIMBLE_LOGD(LOG_TAG, "Creating model type: %04x", type); @@ -53,10 +53,11 @@ void NimBLEMeshElement::createModel(uint16_t type, NimBLEMeshModelCallbacks *pCa default: NIMBLE_LOGE(LOG_TAG, "Error: model type %04x not supported", type); - return; + return nullptr; } m_modelsVec.push_back(bt_mesh_model{{type},0,0,0, &pModel->m_opPub,{0},{0},pModel->m_opList, pModel}); + return pModel; } /** @@ -67,6 +68,18 @@ void NimBLEMeshElement::addModel(bt_mesh_model* model) { m_modelsVec.push_back(*model); } + +NimBLEMeshModel* NimBLEMeshElement::getModel(uint16_t type) { + for(auto &it : m_modelsVec) { + if(it.id == type) { + return (NimBLEMeshModel*)it.user_data; + } + } + + return nullptr; +} + + /** * @brief Creates a bt_mesh_elem for registering with the nimble stack. * @returns A pointer to the bt_mesh_elem created. diff --git a/src/NimBLEMeshElement.h b/src/NimBLEMeshElement.h index d88d90f..d276d76 100644 --- a/src/NimBLEMeshElement.h +++ b/src/NimBLEMeshElement.h @@ -19,10 +19,12 @@ #include class NimBLEMeshModelCallbacks; +class NimBLEMeshModel; class NimBLEMeshElement { public: - void createModel(uint16_t type, NimBLEMeshModelCallbacks* pCallbacks=nullptr); + NimBLEMeshModel* createModel(uint16_t type, NimBLEMeshModelCallbacks* pCallbacks=nullptr); + NimBLEMeshModel* getModel(uint16_t type); private: friend class NimBLEMeshNode; diff --git a/src/NimBLEMeshModel.cpp b/src/NimBLEMeshModel.cpp index 578b857..c2b725c 100644 --- a/src/NimBLEMeshModel.cpp +++ b/src/NimBLEMeshModel.cpp @@ -118,7 +118,6 @@ void NimBLEMeshModel::publish() { } void NimBLEMeshModel::setPubMsg() { - NIMBLE_LOGD(LOG_TAG,"Base setPubMsg"); m_opPub.msg = NULL; }