mirror of
https://github.com/h2zero/esp-nimble-cpp.git
synced 2024-11-23 13:40:55 +01:00
Implement set/get values and add templates.
This commit is contained in:
parent
dc7d9d5b73
commit
6de87945e7
2 changed files with 112 additions and 44 deletions
|
@ -20,7 +20,7 @@ static NimBLEMeshModelCallbacks defaultCallbacks;
|
||||||
* @brief base model constructor
|
* @brief base model constructor
|
||||||
* @param [in] pCallbacks, a pointer to a callback instance for model operations
|
* @param [in] pCallbacks, a pointer to a callback instance for model operations
|
||||||
*/
|
*/
|
||||||
NimBLEMeshModel::NimBLEMeshModel(NimBLEMeshModelCallbacks* pCallbacks) {
|
NimBLEMeshModel::NimBLEMeshModel(NimBLEMeshModelCallbacks *pCallbacks) {
|
||||||
if(pCallbacks == nullptr) {
|
if(pCallbacks == nullptr) {
|
||||||
m_callbacks = &defaultCallbacks;
|
m_callbacks = &defaultCallbacks;
|
||||||
} else {
|
} else {
|
||||||
|
@ -117,16 +117,25 @@ void NimBLEMeshModel::publish() {
|
||||||
ble_npl_callout_reset(&m_pubTimer, 1);
|
ble_npl_callout_reset(&m_pubTimer, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void NimBLEMeshModel::setPubMsg() {
|
uint32_t NimBLEMeshModel::getTransTime() {
|
||||||
m_opPub.msg = NULL;
|
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
|
* @brief Generic on/off server model constructor
|
||||||
* @param [in] pCallbacks, a pointer to a callback instance for model operations
|
* @param [in] pCallbacks, a pointer to a callback instance for model operations
|
||||||
*/
|
*/
|
||||||
NimBLEGenOnOffSrvModel::NimBLEGenOnOffSrvModel(NimBLEMeshModelCallbacks* pCallbacks)
|
NimBLEGenOnOffSrvModel::NimBLEGenOnOffSrvModel(NimBLEMeshModelCallbacks *pCallbacks)
|
||||||
:NimBLEMeshModel(pCallbacks)
|
:NimBLEMeshModel(pCallbacks)
|
||||||
{
|
{
|
||||||
// Register the opcodes for this model with the required callbacks
|
// 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;
|
NimBLEMeshModel *pModel = (NimBLEMeshModel*)model->user_data;
|
||||||
//pModel->m_onOffValue = pModel->m_callbacks->getOnOff();
|
//pModel->m_onOffValue = pModel->m_callbacks->getOnOff();
|
||||||
|
if(pModel->m_callbacks != &defaultCallbacks) {
|
||||||
|
pModel->setValue(pModel->m_callbacks->getOnOff(pModel));
|
||||||
|
}
|
||||||
pModel->setPubMsg();
|
pModel->setPubMsg();
|
||||||
/*
|
/*
|
||||||
struct os_mbuf *msg = NET_BUF_SIMPLE(2 + 3);
|
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_onOffValue = pModel->m_onOffTarget;
|
||||||
pModel->m_value = pModel->m_targetValue;
|
pModel->m_value = pModel->m_targetValue;
|
||||||
//pModel->m_callbacks->setOnOff(pModel->m_onOffValue);
|
//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_onOffValue = pModel->m_onOffTarget;
|
||||||
//pModel->m_callbacks->setOnOff(pModel->m_onOffValue);
|
//pModel->m_callbacks->setOnOff(pModel->m_onOffValue);
|
||||||
pModel->m_value = pModel->m_targetValue;
|
pModel->m_value = pModel->m_targetValue;
|
||||||
pModel->m_callbacks->setOnOff(*(uint8_t*)pModel->m_value.data());
|
pModel->m_callbacks->setOnOff(pModel, *(uint8_t*)pModel->m_value.data());
|
||||||
pModel->publish();
|
// 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
|
* @brief Generic level server model constructor
|
||||||
* @param [in] pCallbacks, a pointer to a callback instance for model operations
|
* @param [in] pCallbacks, a pointer to a callback instance for model operations
|
||||||
*/
|
*/
|
||||||
NimBLEGenLevelSrvModel::NimBLEGenLevelSrvModel(NimBLEMeshModelCallbacks* pCallbacks)
|
NimBLEGenLevelSrvModel::NimBLEGenLevelSrvModel(NimBLEMeshModelCallbacks *pCallbacks)
|
||||||
:NimBLEMeshModel(pCallbacks)
|
:NimBLEMeshModel(pCallbacks)
|
||||||
{
|
{
|
||||||
// Register the opcodes for this model with the required callbacks
|
// 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) {
|
if(pModel->m_transTime & 0x3F) {
|
||||||
/* pModel->m_transStep = -1 *((pModel->m_levelValue - pModel->m_levelTarget) /
|
/* pModel->m_transStep = -1 *((pModel->m_levelValue - pModel->m_levelTarget) /
|
||||||
(pModel->m_transTime & 0x3F));*/
|
(pModel->m_transTime & 0x3F));*/
|
||||||
pModel->m_transStep = -1 *((*(int16_t*)pModel->m_value.data() - *(int16_t*)pModel->m_targetValue.data()) /
|
pModel->m_transStep = -1 * ((*(int16_t*)pModel->m_value.data() - *(int16_t*)pModel->m_targetValue.data()) /
|
||||||
(pModel->m_transTime & 0x3F));
|
(pModel->m_transTime & 0x3F));
|
||||||
if(timerMs == 0) {
|
if(timerMs == 0) {
|
||||||
timerMs = NimBLEUtils::meshTransTimeMs(pModel->m_transTime);
|
timerMs = NimBLEUtils::meshTransTimeMs(pModel->m_transTime);
|
||||||
pModel->m_transTime -= 1;
|
pModel->m_transTime -= 1;
|
||||||
|
@ -412,7 +440,7 @@ void NimBLEGenLevelSrvModel::setLevelUnack(bt_mesh_model *model,
|
||||||
pModel->m_levelValue = pModel->m_levelTarget;
|
pModel->m_levelValue = pModel->m_levelTarget;
|
||||||
pModel->m_callbacks->setLevel(pModel->m_levelValue);*/
|
pModel->m_callbacks->setLevel(pModel->m_levelValue);*/
|
||||||
pModel->m_value = pModel->m_targetValue;
|
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);
|
//pModel->m_callbacks->setLevel(pModel->m_levelValue);
|
||||||
int16_t newval = *(int16_t*)pModel->m_value.data() += pModel->m_transStep;
|
int16_t newval = *(int16_t*)pModel->m_value.data() += pModel->m_transStep;
|
||||||
pModel->m_value = std::string((char*)&newval, sizeof(newval));
|
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->startTdTimer(NimBLEUtils::meshTransTimeMs(pModel->m_transTime));
|
||||||
pModel->m_transTime -= 1;
|
pModel->m_transTime -= 1;
|
||||||
pModel->publish();
|
//pModel->publish();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -497,8 +525,8 @@ void NimBLEGenLevelSrvModel::tdTimerCb(ble_npl_event *event) {
|
||||||
/* pModel->m_levelValue = pModel->m_levelTarget;
|
/* pModel->m_levelValue = pModel->m_levelTarget;
|
||||||
pModel->m_callbacks->setLevel(pModel->m_levelValue);*/
|
pModel->m_callbacks->setLevel(pModel->m_levelValue);*/
|
||||||
pModel->m_value = pModel->m_targetValue;
|
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());
|
||||||
pModel->publish();
|
//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
|
* Default model callbacks
|
||||||
*/
|
*/
|
||||||
NimBLEMeshModelCallbacks::~NimBLEMeshModelCallbacks() {}
|
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);
|
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");
|
NIMBLE_LOGD(LOG_TAG, "Gen On/Off get");
|
||||||
return 0;
|
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);
|
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");
|
NIMBLE_LOGD(LOG_TAG, "Gen Level get");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,41 +21,63 @@ class NimBLEMeshModel {
|
||||||
public:
|
public:
|
||||||
NimBLEMeshModel(NimBLEMeshModelCallbacks* pCallbacks);
|
NimBLEMeshModel(NimBLEMeshModelCallbacks* pCallbacks);
|
||||||
virtual ~NimBLEMeshModel();
|
virtual ~NimBLEMeshModel();
|
||||||
|
|
||||||
bt_mesh_model_op* m_opList;
|
|
||||||
|
|
||||||
bt_mesh_model_pub m_opPub;
|
|
||||||
NimBLEMeshModelCallbacks* m_callbacks;
|
|
||||||
int extractTransTimeDelay(os_mbuf *buf);
|
int extractTransTimeDelay(os_mbuf *buf);
|
||||||
bool checkRetransmit(uint8_t tid, bt_mesh_msg_ctx *ctx);
|
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 sendMessage(bt_mesh_model *model, bt_mesh_msg_ctx *ctx, os_mbuf *msg);
|
||||||
void startTdTimer(ble_npl_time_t timerMs);
|
void startTdTimer(ble_npl_time_t timerMs);
|
||||||
void publish();
|
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;
|
template<typename T>
|
||||||
uint16_t m_lastSrcAddr;
|
void setValue(const T &s) {
|
||||||
uint16_t m_lastDstAddr;
|
setValue((uint8_t*)&s, sizeof(T));
|
||||||
time_t m_lastMsgTime;
|
}
|
||||||
uint8_t m_transTime;
|
|
||||||
uint8_t m_delayTime;
|
template<typename T>
|
||||||
std::string m_value;
|
void setTargetValue(const T &s) {
|
||||||
std::string m_targetValue;
|
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;
|
||||||
|
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_onOffValue;
|
||||||
uint8_t m_onOffTarget;
|
uint8_t m_onOffTarget;
|
||||||
int16_t m_levelValue;
|
int16_t m_levelValue;
|
||||||
int16_t m_levelTarget;*/
|
int16_t m_levelTarget;*/
|
||||||
int16_t m_transStep;
|
int16_t m_transStep;
|
||||||
|
|
||||||
ble_npl_callout m_tdTimer;
|
ble_npl_callout m_tdTimer;
|
||||||
ble_npl_callout m_pubTimer;
|
ble_npl_callout m_pubTimer;
|
||||||
};
|
};
|
||||||
|
|
||||||
class NimBLEGenOnOffSrvModel : NimBLEMeshModel {
|
class NimBLEGenOnOffSrvModel : NimBLEMeshModel {
|
||||||
friend class NimBLEMeshElement;
|
friend class NimBLEMeshElement;
|
||||||
friend class NimBLEMeshNode;
|
friend class NimBLEMeshNode;
|
||||||
|
|
||||||
NimBLEGenOnOffSrvModel(NimBLEMeshModelCallbacks* pCallbacks);
|
NimBLEGenOnOffSrvModel(NimBLEMeshModelCallbacks *pCallbacks);
|
||||||
~NimBLEGenOnOffSrvModel(){};
|
~NimBLEGenOnOffSrvModel(){};
|
||||||
|
|
||||||
static void getOnOff(bt_mesh_model *model,
|
static void getOnOff(bt_mesh_model *model,
|
||||||
|
@ -68,17 +90,18 @@ class NimBLEGenOnOffSrvModel : NimBLEMeshModel {
|
||||||
bt_mesh_msg_ctx *ctx,
|
bt_mesh_msg_ctx *ctx,
|
||||||
os_mbuf *buf);
|
os_mbuf *buf);
|
||||||
static void tdTimerCb(ble_npl_event *event);
|
static void tdTimerCb(ble_npl_event *event);
|
||||||
|
|
||||||
static void pubTimerCb(ble_npl_event *event);
|
static void pubTimerCb(ble_npl_event *event);
|
||||||
|
|
||||||
void setPubMsg() override;
|
void setPubMsg() override;
|
||||||
|
void setValue(uint8_t *val, size_t len) override;
|
||||||
|
void setTargetValue(uint8_t *val, size_t len) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
class NimBLEGenLevelSrvModel : NimBLEMeshModel {
|
class NimBLEGenLevelSrvModel : NimBLEMeshModel {
|
||||||
friend class NimBLEMeshElement;
|
friend class NimBLEMeshElement;
|
||||||
friend class NimBLEMeshNode;
|
friend class NimBLEMeshNode;
|
||||||
|
|
||||||
NimBLEGenLevelSrvModel(NimBLEMeshModelCallbacks* pCallbacks);
|
NimBLEGenLevelSrvModel(NimBLEMeshModelCallbacks *pCallbacks);
|
||||||
~NimBLEGenLevelSrvModel(){};
|
~NimBLEGenLevelSrvModel(){};
|
||||||
|
|
||||||
static void getLevel(bt_mesh_model *model,
|
static void getLevel(bt_mesh_model *model,
|
||||||
|
@ -103,19 +126,20 @@ class NimBLEGenLevelSrvModel : NimBLEMeshModel {
|
||||||
bt_mesh_msg_ctx *ctx,
|
bt_mesh_msg_ctx *ctx,
|
||||||
os_mbuf *buf);
|
os_mbuf *buf);
|
||||||
static void tdTimerCb(ble_npl_event *event);
|
static void tdTimerCb(ble_npl_event *event);
|
||||||
|
|
||||||
static void pubTimerCb(ble_npl_event *event);
|
static void pubTimerCb(ble_npl_event *event);
|
||||||
|
|
||||||
void setPubMsg() override;
|
void setPubMsg() override;
|
||||||
|
void setValue(uint8_t *val, size_t len) override;
|
||||||
|
void setTargetValue(uint8_t *val, size_t len) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
class NimBLEMeshModelCallbacks {
|
class NimBLEMeshModelCallbacks {
|
||||||
public:
|
public:
|
||||||
virtual ~NimBLEMeshModelCallbacks();
|
virtual ~NimBLEMeshModelCallbacks();
|
||||||
virtual void setOnOff(uint8_t);
|
virtual void setOnOff(NimBLEMeshModel *pModel, uint8_t val);
|
||||||
virtual uint8_t getOnOff();
|
virtual uint8_t getOnOff(NimBLEMeshModel *pModel);
|
||||||
virtual void setLevel(int16_t);
|
virtual void setLevel(NimBLEMeshModel *pModel, int16_t val);
|
||||||
virtual int16_t getLevel();
|
virtual int16_t getLevel(NimBLEMeshModel *pModel);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // CONFIG_BT_ENABLED
|
#endif // CONFIG_BT_ENABLED
|
||||||
|
|
Loading…
Reference in a new issue