Return NimBLEMeshModel pointer from NimBLEMeshElement::createModel

This commit is contained in:
h2zero 2020-09-07 10:16:49 -06:00
parent 4d78c3013b
commit 06037f8bf6
3 changed files with 23 additions and 9 deletions

View file

@ -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.

View file

@ -19,10 +19,12 @@
#include <vector>
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;

View file

@ -118,7 +118,6 @@ void NimBLEMeshModel::publish() {
}
void NimBLEMeshModel::setPubMsg() {
NIMBLE_LOGD(LOG_TAG,"Base setPubMsg");
m_opPub.msg = NULL;
}