diff --git a/CMakeLists.txt b/CMakeLists.txt index b8a2a1d..ebcaf94 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,7 @@ idf_component_register( "src/NimBLEEddystoneURL.cpp" "src/NimBLEExtAdvertising.cpp" "src/NimBLEHIDDevice.cpp" + "src/NimBLEMeshCreateModel.c" "src/NimBLEMeshElement.cpp" "src/NimBLEMeshModel.cpp" "src/NimBLEMeshNode.cpp" diff --git a/src/NimBLEMeshCreateModel.c b/src/NimBLEMeshCreateModel.c new file mode 100644 index 0000000..ee8c2b3 --- /dev/null +++ b/src/NimBLEMeshCreateModel.c @@ -0,0 +1,30 @@ +/* + * NimBLEMeshCreateModel.cpp + * + * Created: on April 27 2022 + * Author H2zero + * + */ + +#include "NimBLEMeshCreateModel.h" + +static struct bt_mesh_model_cb mod_cb = { + //.init = modelInitCallback +}; + +struct bt_mesh_model createConfigSrvModel(struct bt_mesh_cfg_srv* cfg) { + struct bt_mesh_model cmod = BT_MESH_MODEL_CFG_SRV(cfg); + return cmod; +} + +struct bt_mesh_model createHealthModel(struct bt_mesh_health_srv* hsrv, + struct bt_mesh_model_pub* hpub) { + struct bt_mesh_model hmod = BT_MESH_MODEL_HEALTH_SRV(hsrv, hpub); + return hmod; +} + +struct bt_mesh_model createGenModel(int16_t _id, struct bt_mesh_model_op* op, + struct bt_mesh_model_pub* pub, void* udata) { + struct bt_mesh_model mod = BT_MESH_MODEL_CB(_id, op, pub, udata, &mod_cb); + return mod; +} diff --git a/src/NimBLEMeshCreateModel.h b/src/NimBLEMeshCreateModel.h new file mode 100644 index 0000000..b73f682 --- /dev/null +++ b/src/NimBLEMeshCreateModel.h @@ -0,0 +1,35 @@ +/* + * NimBLEMeshCreateModel.h + * + * Created: on April 27 2022 + * Author H2zero + * + */ + +#ifndef __NIMBLE_MESH_CREATE_MODEL_H +#define __NIMBLE_MESH_CREATE_MODEL_H + +#include "nimconfig.h" +#if defined(CONFIG_NIMBLE_CPP_IDF) +# include "mesh/mesh.h" +# include "mesh/cfg_srv.h" +#else +# include "nimble/nimble/host/mesh/include/mesh/mesh.h" +# include "nimble/nimble/host/mesh/include/mesh/cfg_srv.h" +#endif + + +#ifdef __cplusplus +extern "C" { +#endif +//int modelInitCallback(struct bt_mesh_model *model); +struct bt_mesh_model createConfigSrvModel(struct bt_mesh_cfg_srv* cfg); +struct bt_mesh_model createHealthModel(struct bt_mesh_health_srv* hsrv, + struct bt_mesh_model_pub* hpub); +struct bt_mesh_model createGenModel(int16_t _id, struct bt_mesh_model_op* op, + struct bt_mesh_model_pub* pub, void* udata); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/src/NimBLEMeshElement.cpp b/src/NimBLEMeshElement.cpp index 3fba746..d96cc51 100644 --- a/src/NimBLEMeshElement.cpp +++ b/src/NimBLEMeshElement.cpp @@ -8,18 +8,19 @@ #include "NimBLEMeshElement.h" #include "NimBLELog.h" +#include "NimBLEMeshCreateModel.h" static const char* LOG_TAG = "NimBLEMeshElement"; NimBLEMeshElement::NimBLEMeshElement() { - m_pElem_t = nullptr; + m_pElem = nullptr; m_pHealthModel = nullptr; } NimBLEMeshElement::~NimBLEMeshElement() { - if(m_pElem_t != nullptr) { - delete m_pElem_t; + if(m_pElem != nullptr) { + delete m_pElem; } if(m_pHealthModel != nullptr) { @@ -64,7 +65,7 @@ NimBLEMeshModel* NimBLEMeshElement::createModel(uint16_t type, NimBLEMeshModelCa case BT_MESH_MODEL_ID_HEALTH_SRV: m_pHealthModel = new NimBLEHealthSrvModel(pCallbacks); pModel = m_pHealthModel; - m_modelsVec.push_back(bt_mesh_model{{type},0,0,0,&pModel->m_opPub,{0},{0},bt_mesh_health_srv_op,&bt_mesh_health_cli_cb,&m_pHealthModel->m_healthSrv}); + m_modelsVec.push_back(createHealthModel(&m_pHealthModel->m_healthSrv, &pModel->m_opPub)); return pModel; default: @@ -72,7 +73,7 @@ NimBLEMeshModel* NimBLEMeshElement::createModel(uint16_t type, NimBLEMeshModelCa return nullptr; } - m_modelsVec.push_back(bt_mesh_model{{type},0,0,0, &pModel->m_opPub,{0},{0},pModel->m_opList,nullptr,pModel}); + m_modelsVec.push_back(createGenModel(type, pModel->m_opList, &pModel->m_opPub, pModel)); return pModel; } @@ -81,8 +82,8 @@ NimBLEMeshModel* NimBLEMeshElement::createModel(uint16_t type, NimBLEMeshModelCa * @brief Adds a model created outside of element context to the elements model vector. * @param [in] model A pointer to the model instance to add. */ -void NimBLEMeshElement::addModel(bt_mesh_model *model) { - m_modelsVec.push_back(*model); +void NimBLEMeshElement::addModel(const bt_mesh_model & model) { + m_modelsVec.push_back(model); } @@ -134,8 +135,8 @@ NimBLEMeshModel* NimBLEMeshElement::getModelByIdx(uint8_t eidx, uint8_t midx, ui * @details Must not be called until all models have been added. */ bt_mesh_elem* NimBLEMeshElement::start() { - m_pElem_t = new bt_mesh_elem{0, 0, uint8_t(m_modelsVec.size()), 0, &m_modelsVec[0], NULL}; - return m_pElem_t; + m_pElem = new bt_mesh_elem{0, 0, uint8_t(m_modelsVec.size()), 0, &m_modelsVec[0], NULL}; + return m_pElem; } diff --git a/src/NimBLEMeshElement.h b/src/NimBLEMeshElement.h index 6543b58..1c85f2e 100644 --- a/src/NimBLEMeshElement.h +++ b/src/NimBLEMeshElement.h @@ -33,10 +33,10 @@ private: NimBLEMeshElement(); ~NimBLEMeshElement(); - void addModel(bt_mesh_model* model); + void addModel(const bt_mesh_model & model); bt_mesh_elem* start(); - bt_mesh_elem *m_pElem_t; + bt_mesh_elem *m_pElem; NimBLEHealthSrvModel* m_pHealthModel; std::vector m_modelsVec; }; diff --git a/src/NimBLEMeshNode.cpp b/src/NimBLEMeshNode.cpp index d72bbaa..8313f25 100644 --- a/src/NimBLEMeshNode.cpp +++ b/src/NimBLEMeshNode.cpp @@ -12,6 +12,7 @@ #include "NimBLEMeshNode.h" #include "NimBLELog.h" #include "NimBLEDevice.h" +#include "NimBLEMeshCreateModel.h" #if defined(CONFIG_NIMBLE_CPP_IDF) # include "services/gap/ble_svc_gap.h" @@ -66,8 +67,6 @@ NimBLEMeshNode::NimBLEMeshNode(const NimBLEUUID &uuid, uint8_t type) { m_prov.complete = NimBLEMeshNode::provComplete; m_prov.reset = NimBLEMeshNode::provReset; - m_configSrvModel = nullptr; - // Create the primary element m_elemVec.push_back(new NimBLEMeshElement()); } @@ -77,10 +76,6 @@ NimBLEMeshNode::NimBLEMeshNode(const NimBLEUUID &uuid, uint8_t type) { * @brief Destructor, cleanup any resources created. */ NimBLEMeshNode::~NimBLEMeshNode() { - if(m_configSrvModel != nullptr) { - delete m_configSrvModel; - } - if(m_comp.elem != nullptr) { free (m_comp.elem); } @@ -168,16 +163,7 @@ bool NimBLEMeshNode::start() { // Config server and primary health models are required in the primary element // create them here and add them as the first models. - m_configSrvModel = new bt_mesh_model{{BT_MESH_MODEL_ID_CFG_SRV},0,0,0,nullptr,{0},{0},bt_mesh_cfg_srv_op,&bt_mesh_cfg_srv_cb,&m_serverConfig}; - for(int i = 0; i < CONFIG_BT_MESH_MODEL_KEY_COUNT; i++) { - m_configSrvModel->keys[i] = BT_MESH_KEY_UNUSED; - } - - for(int i = 0; i < CONFIG_BT_MESH_MODEL_GROUP_COUNT; i++) { - m_configSrvModel->groups[i] = BT_MESH_ADDR_UNASSIGNED; - } - - m_elemVec[0]->addModel(m_configSrvModel); + m_elemVec[0]->addModel(createConfigSrvModel(&m_serverConfig)); if(m_elemVec[0]->getModel(BT_MESH_MODEL_ID_HEALTH_SRV) == nullptr) { m_elemVec[0]->createModel(BT_MESH_MODEL_ID_HEALTH_SRV); @@ -193,7 +179,7 @@ bool NimBLEMeshNode::start() { } for(size_t i = 0; i < m_elemVec.size(); i++) { - memcpy((void*)&m_comp.elem[i], (void*)m_elemVec[i]->start(),sizeof(bt_mesh_elem)); + memcpy((void*)&m_comp.elem[i], (void*)m_elemVec[i]->start(), sizeof(bt_mesh_elem)); } m_comp.elem_count = (uint8_t)m_elemVec.size(); diff --git a/src/NimBLEMeshNode.h b/src/NimBLEMeshNode.h index b6d56c7..9e0138b 100644 --- a/src/NimBLEMeshNode.h +++ b/src/NimBLEMeshNode.h @@ -66,7 +66,6 @@ private: bt_mesh_comp m_comp; uint16_t m_primAddr; uint16_t m_primNetIdx; - bt_mesh_model* m_configSrvModel; NimBLEUUID m_uuid; std::vector m_elemVec;