From 23e94c1aca2d81e0264a3707127f3d7a436fae13 Mon Sep 17 00:00:00 2001 From: Christian Baars Date: Thu, 9 Jun 2022 21:00:00 +0200 Subject: [PATCH 1/2] Use a notifiaction queue --- tasmota/include/xsns_62_esp32_mi.h | 5 +++ .../tasmota_xsns_sensor/xsns_62_esp32_mi.ino | 37 +++++++++++++------ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/tasmota/include/xsns_62_esp32_mi.h b/tasmota/include/xsns_62_esp32_mi.h index 3b5a2e000..edbf0b2db 100644 --- a/tasmota/include/xsns_62_esp32_mi.h +++ b/tasmota/include/xsns_62_esp32_mi.h @@ -157,6 +157,11 @@ struct MI32connectionContextBerry_t{ bool response; }; +struct MI32notificationBuffer_t{ + uint8_t buffer[256]; + uint16_t returnCharUUID; +}; + struct { // uint32_t period; // set manually in addition to TELE-period, is set to TELE-period after start TaskHandle_t ScanTask = nullptr; diff --git a/tasmota/tasmota_xsns_sensor/xsns_62_esp32_mi.ino b/tasmota/tasmota_xsns_sensor/xsns_62_esp32_mi.ino index 82bd6b24b..a84106b15 100644 --- a/tasmota/tasmota_xsns_sensor/xsns_62_esp32_mi.ino +++ b/tasmota/tasmota_xsns_sensor/xsns_62_esp32_mi.ino @@ -60,6 +60,7 @@ #include #include +#include #include @@ -164,6 +165,7 @@ class MI32AdvCallbacks: public NimBLEAdvertisedDeviceCallbacks { static MI32AdvCallbacks MI32ScanCallbacks; static MI32SensorCallback MI32SensorCB; static NimBLEClient* MI32Client; +static std::queue MI32NotificationQueue; /*********************************************************************************************\ * BLE callback functions @@ -175,16 +177,17 @@ void MI32scanEndedCB(NimBLEScanResults results){ } void MI32notifyCB(NimBLERemoteCharacteristic* pRemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify){ - if(MI32.mode.triggerBerryConnCB) return; //discard data, if we did not pass the old to Berry yet - if(isNotify){ - MI32.infoMsg = MI32_GOT_NOTIFICATION; - MI32.conCtx->buffer[0] = (uint8_t)length; - memcpy(MI32.conCtx->buffer + 1, pData, length); - MI32.conCtx->returnCharUUID = pRemoteCharacteristic->getUUID().getNative()->u16.value; - MI32.conCtx->operation = 103; - MI32.mode.triggerBerryConnCB = 1; - MI32.mode.readingDone = 1; - } + if(isNotify){ + MI32notificationBuffer_t _buf; + _buf.buffer[0] = (uint8_t)length; + memcpy(_buf.buffer+1, pData, length); + _buf.returnCharUUID = pRemoteCharacteristic->getUUID().getNative()->u16.value; + MI32NotificationQueue.push(_buf); + MI32.mode.readingDone = 1; + MI32.mode.triggerBerryConnCB = 1; + MI32.infoMsg = MI32_GOT_NOTIFICATION; + return; // do not discard data + } } /*********************************************************************************************\ * Helper functions @@ -1141,6 +1144,7 @@ bool MI32ConnectActiveSensor(){ // only use inside a task !! bool MI32StartConnectionTask(){ if(MI32.conCtx == nullptr) return false; if(MI32.conCtx->buffer == nullptr) return false; + MI32NotificationQueue = {}; MI32.mode.willConnect = 1; MI32Scan->stop(); MI32suspendScanTask(); @@ -1178,7 +1182,8 @@ void MI32ConnectionTask(void *pvParameters){ timer++; vTaskDelay(10/ portTICK_PERIOD_MS); } - MI32Client->discoverAttributes(); // solves connection problems on i.e. yeelight dimmer + // TODO: make next line optional + // MI32Client->discoverAttributes(); // solves connection problems on i.e. yeelight dimmer NimBLERemoteService* pSvc = nullptr; NimBLERemoteCharacteristic* pChr = nullptr; @@ -1672,7 +1677,15 @@ void MI32Every50mSecond(){ } MI32.mode.triggerBerryAdvCB = 0; } - if(MI32.mode.triggerBerryConnCB == 1){ + if(MI32.mode.triggerBerryConnCB || !MI32NotificationQueue.empty()){ + if(!MI32NotificationQueue.empty()){ + MI32notificationBuffer_t _buf = MI32NotificationQueue.front(); + MI32NotificationQueue.pop(); + memcpy(MI32.conCtx->buffer,_buf.buffer,_buf.buffer[0]+1); + MI32.conCtx->returnCharUUID = _buf.returnCharUUID; + MI32.conCtx->operation = 103; + MI32.conCtx->error = 0; + } if(MI32.beConnCB != nullptr){ void (*func_ptr)(int, int, int) = (void (*)(int, int, int))MI32.beConnCB; char _message[32]; From f9c31bd8b8a8329615f5dd9b9fc4c217003a2b2f Mon Sep 17 00:00:00 2001 From: Christian Baars Date: Thu, 9 Jun 2022 22:00:19 +0200 Subject: [PATCH 2/2] make discoverAttributes optional --- tasmota/include/xsns_62_esp32_mi.h | 1 + tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_MI32.ino | 10 +++++++--- tasmota/tasmota_xsns_sensor/xsns_62_esp32_mi.ino | 8 +++++--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/tasmota/include/xsns_62_esp32_mi.h b/tasmota/include/xsns_62_esp32_mi.h index edbf0b2db..dd404ab14 100644 --- a/tasmota/include/xsns_62_esp32_mi.h +++ b/tasmota/include/xsns_62_esp32_mi.h @@ -190,6 +190,7 @@ struct { uint32_t triggerBerryConnCB:1; uint32_t triggerNextConnJob:1; uint32_t readyForNextConnJob:1; + uint32_t discoverAttributes:1; }; uint32_t all = 0; } mode; diff --git a/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_MI32.ino b/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_MI32.ino index 032e2e2ed..e247ee01a 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_MI32.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_MI32.ino @@ -119,7 +119,7 @@ extern "C" { extern void MI32setBerryAdvCB(void* function, uint8_t *buffer); extern void MI32setBerryConnCB(void* function, uint8_t *buffer); extern bool MI32runBerryConnection(uint8_t operation, bool response); - extern bool MI32setBerryCtxSvc(const char *Svc); + extern bool MI32setBerryCtxSvc(const char *Svc, bool discoverAttributes); extern bool MI32setBerryCtxChr(const char *Chr); extern bool MI32setBerryCtxMAC(uint8_t *MAC, uint8_t type); extern bool MI32addMACtoBlockList(uint8_t *MAC, uint8_t type); @@ -175,8 +175,12 @@ extern "C" { int be_BLE_set_service(bvm *vm); int be_BLE_set_service(bvm *vm){ int32_t argc = be_top(vm); // Get the number of arguments - if (argc == 2 && be_isstring(vm, 2)) { - if (MI32setBerryCtxSvc(be_tostring(vm, 2))) be_return(vm); + if (argc > 1 && be_isstring(vm, 2)) { + bool discoverAttributes = false; + if(argc == 3 && be_isint(vm, 3)){ + discoverAttributes = be_toint(vm,3)>0; + } + if (MI32setBerryCtxSvc(be_tostring(vm, 2),discoverAttributes)) be_return(vm); } be_raise(vm, kTypeError, nullptr); } diff --git a/tasmota/tasmota_xsns_sensor/xsns_62_esp32_mi.ino b/tasmota/tasmota_xsns_sensor/xsns_62_esp32_mi.ino index a84106b15..55d84db7a 100644 --- a/tasmota/tasmota_xsns_sensor/xsns_62_esp32_mi.ino +++ b/tasmota/tasmota_xsns_sensor/xsns_62_esp32_mi.ino @@ -702,10 +702,11 @@ extern "C" { AddLog(LOG_LEVEL_INFO,PSTR("M32: Connection Ctx created")); } - bool MI32setBerryCtxSvc(const char *Svc){ + bool MI32setBerryCtxSvc(const char *Svc, bool discoverAttributes){ if(MI32.conCtx != nullptr){ MI32.conCtx->serviceUUID = NimBLEUUID(Svc); AddLog(LOG_LEVEL_DEBUG,PSTR("M32: SVC: %s"),MI32.conCtx->serviceUUID.toString().c_str()); + MI32.mode.discoverAttributes = discoverAttributes; return true; } return false; @@ -1182,8 +1183,9 @@ void MI32ConnectionTask(void *pvParameters){ timer++; vTaskDelay(10/ portTICK_PERIOD_MS); } - // TODO: make next line optional - // MI32Client->discoverAttributes(); // solves connection problems on i.e. yeelight dimmer + if(MI32.mode.discoverAttributes){ + MI32Client->discoverAttributes(); // solves connection problems on i.e. yeelight dimmer + } NimBLERemoteService* pSvc = nullptr; NimBLERemoteCharacteristic* pChr = nullptr;