Implement set/get values and add templates.

This commit is contained in:
h2zero 2020-09-07 10:21:44 -06:00
parent dc7d9d5b73
commit 6de87945e7
2 changed files with 112 additions and 44 deletions

View file

@ -117,10 +117,19 @@ 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
@ -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,6 +304,22 @@ 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
@ -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;
}

View file

@ -21,18 +21,40 @@ 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){};
template<typename T>
void setValue(const T &s) {
setValue((uint8_t*)&s, sizeof(T));
}
template<typename T>
void setTargetValue(const T &s) {
setTargetValue((uint8_t*)&s, sizeof(T));
}
template<typename T>
void getValue(T &s) {
s = *(T*)m_value.data();
}
template<typename T>
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;
@ -68,10 +90,11 @@ 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 {
@ -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