diff --git a/src/NimBLEMeshModel.cpp b/src/NimBLEMeshModel.cpp index 79a2a07..ea534dc 100644 --- a/src/NimBLEMeshModel.cpp +++ b/src/NimBLEMeshModel.cpp @@ -20,7 +20,7 @@ static NimBLEMeshModelCallbacks defaultCallbacks; * @brief base model constructor * @param [in] pCallbacks, a pointer to a callback instance for model operations */ -NimBLEMeshModel::NimBLEMeshModel(NimBLEMeshModelCallbacks* pCallbacks) { +NimBLEMeshModel::NimBLEMeshModel(NimBLEMeshModelCallbacks *pCallbacks) { if(pCallbacks == nullptr) { m_callbacks = &defaultCallbacks; } else { @@ -117,16 +117,25 @@ void NimBLEMeshModel::publish() { ble_npl_callout_reset(&m_pubTimer, 1); } -void NimBLEMeshModel::setPubMsg() { - m_opPub.msg = NULL; +uint32_t NimBLEMeshModel::getTransTime() { + return (m_transTime & 0x3F) * NimBLEUtils::meshTransTimeMs(m_transTime); } +uint16_t NimBLEMeshModel::getDelayTime() { + return m_delayTime * 5; +} + +/* +void NimBLEMeshModel::setValue() { + NIMBLE_LOGE(LOG_TAG,"DEFAULT SET VALUE"); +} +*/ /** * @brief Generic on/off server model constructor * @param [in] pCallbacks, a pointer to a callback instance for model operations */ -NimBLEGenOnOffSrvModel::NimBLEGenOnOffSrvModel(NimBLEMeshModelCallbacks* pCallbacks) +NimBLEGenOnOffSrvModel::NimBLEGenOnOffSrvModel(NimBLEMeshModelCallbacks *pCallbacks) :NimBLEMeshModel(pCallbacks) { // Register the opcodes for this model with the required callbacks @@ -154,6 +163,9 @@ void NimBLEGenOnOffSrvModel::getOnOff(bt_mesh_model *model, { NimBLEMeshModel *pModel = (NimBLEMeshModel*)model->user_data; //pModel->m_onOffValue = pModel->m_callbacks->getOnOff(); + if(pModel->m_callbacks != &defaultCallbacks) { + pModel->setValue(pModel->m_callbacks->getOnOff(pModel)); + } pModel->setPubMsg(); /* struct os_mbuf *msg = NET_BUF_SIMPLE(2 + 3); @@ -242,7 +254,7 @@ void NimBLEGenOnOffSrvModel::setOnOffUnack(bt_mesh_model *model, //pModel->m_onOffValue = pModel->m_onOffTarget; pModel->m_value = pModel->m_targetValue; //pModel->m_callbacks->setOnOff(pModel->m_onOffValue); - pModel->m_callbacks->setOnOff(*(uint8_t*)pModel->m_value.data()); + pModel->m_callbacks->setOnOff(pModel, *(uint8_t*)pModel->m_value.data()); } } @@ -263,8 +275,8 @@ void NimBLEGenOnOffSrvModel::tdTimerCb(ble_npl_event *event) { //pModel->m_onOffValue = pModel->m_onOffTarget; //pModel->m_callbacks->setOnOff(pModel->m_onOffValue); pModel->m_value = pModel->m_targetValue; - pModel->m_callbacks->setOnOff(*(uint8_t*)pModel->m_value.data()); - pModel->publish(); + pModel->m_callbacks->setOnOff(pModel, *(uint8_t*)pModel->m_value.data()); +// pModel->publish(); } @@ -292,11 +304,27 @@ void NimBLEGenOnOffSrvModel::setPubMsg() { } } +void NimBLEGenOnOffSrvModel::setValue(uint8_t *val, size_t len) { + if(len != sizeof(uint8_t)) { + NIMBLE_LOGE(LOG_TAG, "NimBLEGenOnOffSrvModel: Incorrect value length"); + return; + } + m_value = std::string((char*)val, len); +} + +void NimBLEGenOnOffSrvModel::setTargetValue(uint8_t *val, size_t len) { + if(len != sizeof(uint8_t)) { + NIMBLE_LOGE(LOG_TAG, "NimBLEGenOnOffSrvModel: Incorrect target value length"); + return; + } + m_targetValue = std::string((char*)val, len); +} + /** * @brief Generic level server model constructor * @param [in] pCallbacks, a pointer to a callback instance for model operations */ -NimBLEGenLevelSrvModel::NimBLEGenLevelSrvModel(NimBLEMeshModelCallbacks* pCallbacks) +NimBLEGenLevelSrvModel::NimBLEGenLevelSrvModel(NimBLEMeshModelCallbacks *pCallbacks) :NimBLEMeshModel(pCallbacks) { // Register the opcodes for this model with the required callbacks @@ -397,8 +425,8 @@ void NimBLEGenLevelSrvModel::setLevelUnack(bt_mesh_model *model, if(pModel->m_transTime & 0x3F) { /* pModel->m_transStep = -1 *((pModel->m_levelValue - pModel->m_levelTarget) / (pModel->m_transTime & 0x3F));*/ - pModel->m_transStep = -1 *((*(int16_t*)pModel->m_value.data() - *(int16_t*)pModel->m_targetValue.data()) / - (pModel->m_transTime & 0x3F)); + pModel->m_transStep = -1 * ((*(int16_t*)pModel->m_value.data() - *(int16_t*)pModel->m_targetValue.data()) / + (pModel->m_transTime & 0x3F)); if(timerMs == 0) { timerMs = NimBLEUtils::meshTransTimeMs(pModel->m_transTime); pModel->m_transTime -= 1; @@ -412,7 +440,7 @@ void NimBLEGenLevelSrvModel::setLevelUnack(bt_mesh_model *model, pModel->m_levelValue = pModel->m_levelTarget; pModel->m_callbacks->setLevel(pModel->m_levelValue);*/ pModel->m_value = pModel->m_targetValue; - pModel->m_callbacks->setLevel(*(int16_t*)pModel->m_value.data()); + pModel->m_callbacks->setLevel(pModel, *(int16_t*)pModel->m_value.data()); } } @@ -486,10 +514,10 @@ void NimBLEGenLevelSrvModel::tdTimerCb(ble_npl_event *event) { //pModel->m_callbacks->setLevel(pModel->m_levelValue); int16_t newval = *(int16_t*)pModel->m_value.data() += pModel->m_transStep; pModel->m_value = std::string((char*)&newval, sizeof(newval)); - pModel->m_callbacks->setLevel(newval); + pModel->m_callbacks->setLevel(pModel, newval); pModel->startTdTimer(NimBLEUtils::meshTransTimeMs(pModel->m_transTime)); pModel->m_transTime -= 1; - pModel->publish(); + //pModel->publish(); return; } @@ -497,8 +525,8 @@ void NimBLEGenLevelSrvModel::tdTimerCb(ble_npl_event *event) { /* pModel->m_levelValue = pModel->m_levelTarget; pModel->m_callbacks->setLevel(pModel->m_levelValue);*/ pModel->m_value = pModel->m_targetValue; - pModel->m_callbacks->setLevel(*(int16_t*)pModel->m_value.data()); - pModel->publish(); + pModel->m_callbacks->setLevel(pModel, *(int16_t*)pModel->m_value.data()); + //pModel->publish(); } @@ -525,25 +553,41 @@ void NimBLEGenLevelSrvModel::setPubMsg() { } } +void NimBLEGenLevelSrvModel::setValue(uint8_t *val, size_t len) { + if(len != sizeof(int16_t)) { + NIMBLE_LOGE(LOG_TAG, "NimBLEGenLevelSrvModel: Incorrect value length"); + return; + } + m_value = std::string((char*)val, len); +} + +void NimBLEGenLevelSrvModel::setTargetValue(uint8_t *val, size_t len) { + if(len != sizeof(int16_t)) { + NIMBLE_LOGE(LOG_TAG, "NimBLEGenLevelSrvModel: Incorrect target value length"); + return; + } + m_targetValue = std::string((char*)val, len); +} + /** * Default model callbacks */ NimBLEMeshModelCallbacks::~NimBLEMeshModelCallbacks() {} -void NimBLEMeshModelCallbacks::setOnOff(uint8_t val) { +void NimBLEMeshModelCallbacks::setOnOff(NimBLEMeshModel *pModel, uint8_t val) { NIMBLE_LOGD(LOG_TAG, "Gen On/Off set val: %d", val); } -uint8_t NimBLEMeshModelCallbacks::getOnOff() { +uint8_t NimBLEMeshModelCallbacks::getOnOff(NimBLEMeshModel *pModel) { NIMBLE_LOGD(LOG_TAG, "Gen On/Off get"); return 0; } -void NimBLEMeshModelCallbacks::setLevel(int16_t val) { +void NimBLEMeshModelCallbacks::setLevel(NimBLEMeshModel *pModel, int16_t val) { NIMBLE_LOGD(LOG_TAG, "Gen Level set val: %d", val); } -int16_t NimBLEMeshModelCallbacks::getLevel() { +int16_t NimBLEMeshModelCallbacks::getLevel(NimBLEMeshModel *pModel) { NIMBLE_LOGD(LOG_TAG, "Gen Level get"); return 0; } diff --git a/src/NimBLEMeshModel.h b/src/NimBLEMeshModel.h index 4214bc0..c92116d 100644 --- a/src/NimBLEMeshModel.h +++ b/src/NimBLEMeshModel.h @@ -21,41 +21,63 @@ class NimBLEMeshModel { public: NimBLEMeshModel(NimBLEMeshModelCallbacks* pCallbacks); virtual ~NimBLEMeshModel(); - - bt_mesh_model_op* m_opList; - - bt_mesh_model_pub m_opPub; - NimBLEMeshModelCallbacks* m_callbacks; int extractTransTimeDelay(os_mbuf *buf); bool checkRetransmit(uint8_t tid, bt_mesh_msg_ctx *ctx); void sendMessage(bt_mesh_model *model, bt_mesh_msg_ctx *ctx, os_mbuf *msg); void startTdTimer(ble_npl_time_t timerMs); void publish(); - virtual void setPubMsg(); + uint32_t getTransTime(); + uint16_t getDelayTime(); + virtual void setPubMsg(){}; + virtual void setValue(uint8_t *val, size_t len){}; + virtual void setTargetValue(uint8_t *val, size_t len){}; - uint8_t m_lastTid; - uint16_t m_lastSrcAddr; - uint16_t m_lastDstAddr; - time_t m_lastMsgTime; - uint8_t m_transTime; - uint8_t m_delayTime; - std::string m_value; - std::string m_targetValue; + template + void setValue(const T &s) { + setValue((uint8_t*)&s, sizeof(T)); + } + + template + void setTargetValue(const T &s) { + setTargetValue((uint8_t*)&s, sizeof(T)); + } + + template + void getValue(T &s) { + s = *(T*)m_value.data(); + } + + template + void getTargetValue(T &s) { + s = *(T*)m_targetValue.data(); + } + + bt_mesh_model_op* m_opList; + bt_mesh_model_pub m_opPub; + NimBLEMeshModelCallbacks* m_callbacks; + uint8_t m_lastTid; + uint16_t m_lastSrcAddr; + uint16_t m_lastDstAddr; + time_t m_lastMsgTime; + uint8_t m_transTime; + uint8_t m_delayTime; + std::string m_value; + std::string m_targetValue; /*uint8_t m_onOffValue; uint8_t m_onOffTarget; int16_t m_levelValue; int16_t m_levelTarget;*/ - int16_t m_transStep; + int16_t m_transStep; - ble_npl_callout m_tdTimer; - ble_npl_callout m_pubTimer; + ble_npl_callout m_tdTimer; + ble_npl_callout m_pubTimer; }; class NimBLEGenOnOffSrvModel : NimBLEMeshModel { friend class NimBLEMeshElement; friend class NimBLEMeshNode; - NimBLEGenOnOffSrvModel(NimBLEMeshModelCallbacks* pCallbacks); + NimBLEGenOnOffSrvModel(NimBLEMeshModelCallbacks *pCallbacks); ~NimBLEGenOnOffSrvModel(){}; static void getOnOff(bt_mesh_model *model, @@ -68,17 +90,18 @@ class NimBLEGenOnOffSrvModel : NimBLEMeshModel { bt_mesh_msg_ctx *ctx, os_mbuf *buf); static void tdTimerCb(ble_npl_event *event); - static void pubTimerCb(ble_npl_event *event); void setPubMsg() override; + void setValue(uint8_t *val, size_t len) override; + void setTargetValue(uint8_t *val, size_t len) override; }; class NimBLEGenLevelSrvModel : NimBLEMeshModel { friend class NimBLEMeshElement; friend class NimBLEMeshNode; - NimBLEGenLevelSrvModel(NimBLEMeshModelCallbacks* pCallbacks); + NimBLEGenLevelSrvModel(NimBLEMeshModelCallbacks *pCallbacks); ~NimBLEGenLevelSrvModel(){}; static void getLevel(bt_mesh_model *model, @@ -103,19 +126,20 @@ class NimBLEGenLevelSrvModel : NimBLEMeshModel { bt_mesh_msg_ctx *ctx, os_mbuf *buf); static void tdTimerCb(ble_npl_event *event); - static void pubTimerCb(ble_npl_event *event); void setPubMsg() override; + void setValue(uint8_t *val, size_t len) override; + void setTargetValue(uint8_t *val, size_t len) override; }; class NimBLEMeshModelCallbacks { public: virtual ~NimBLEMeshModelCallbacks(); - virtual void setOnOff(uint8_t); - virtual uint8_t getOnOff(); - virtual void setLevel(int16_t); - virtual int16_t getLevel(); + virtual void setOnOff(NimBLEMeshModel *pModel, uint8_t val); + virtual uint8_t getOnOff(NimBLEMeshModel *pModel); + virtual void setLevel(NimBLEMeshModel *pModel, int16_t val); + virtual int16_t getLevel(NimBLEMeshModel *pModel); }; #endif // CONFIG_BT_ENABLED