diff --git a/lib/libesp32/berry_tasmota/src/be_MI32_lib.c b/lib/libesp32/berry_tasmota/src/be_MI32_lib.c index 652002239..fd4e6bb06 100644 --- a/lib/libesp32/berry_tasmota/src/be_MI32_lib.c +++ b/lib/libesp32/berry_tasmota/src/be_MI32_lib.c @@ -64,8 +64,8 @@ BE_FUNC_CTYPE_DECLARE(be_BLE_reg_adv_cb, "", "c[c]"); extern void be_BLE_reg_server_cb(void* function, uint8_t *buffer); BE_FUNC_CTYPE_DECLARE(be_BLE_reg_server_cb, "", "c[c]"); -extern void be_BLE_set_MAC(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type); -BE_FUNC_CTYPE_DECLARE(be_BLE_set_MAC, "", "@(bytes)~[i]"); +extern void be_BLE_set_MAC(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type, uint32_t pin); +BE_FUNC_CTYPE_DECLARE(be_BLE_set_MAC, "", "@(bytes)~[ii]"); extern void be_BLE_set_characteristic(struct bvm *vm, const char *Chr); BE_FUNC_CTYPE_DECLARE(be_BLE_set_characteristic, "", "@s"); diff --git a/tasmota/include/xsns_62_esp32_mi.h b/tasmota/include/xsns_62_esp32_mi.h index adecf392e..f4b959ea3 100644 --- a/tasmota/include/xsns_62_esp32_mi.h +++ b/tasmota/include/xsns_62_esp32_mi.h @@ -178,6 +178,7 @@ struct MI32connectionContextBerry_t{ uint8_t addrType; int error; int32_t arg1; + uint32_t pin; bool hasArg1; bool oneOp; bool response; 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 13374943e..da95c1640 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_MI32.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_MI32.ino @@ -110,7 +110,7 @@ extern "C" { extern bool MI32runBerryConnection(uint8_t operation, bbool response, int32_t *arg1); extern bool MI32setBerryCtxSvc(const char *Svc, bbool discoverAttributes); extern bool MI32setBerryCtxChr(const char *Chr); - extern bool MI32setBerryCtxMAC(uint8_t *MAC, uint8_t type); + extern bool MI32setBerryCtxMAC(uint8_t *MAC, uint8_t type, uint32_t pin); extern bool MI32addMACtoWatchList(uint8_t *MAC, uint8_t type); extern void MI32setBerryStoreRec(uint8_t *buffer, size_t size); @@ -159,8 +159,8 @@ extern "C" { return true; } - void be_BLE_set_MAC(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type); - void be_BLE_set_MAC(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type){ + void be_BLE_set_MAC(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type, uint32_t pin); + void be_BLE_set_MAC(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type, uint32_t pin){ if(!be_BLE_MAC_size(vm, size)){ return; } @@ -168,7 +168,11 @@ extern "C" { if(type){ _type = type; } - if (MI32setBerryCtxMAC(buf,_type)) return; + uint32_t _pin = 0; + if (pin){ + _pin = pin; + } + if (MI32setBerryCtxMAC(buf,_type, _pin)) return; be_raisef(vm, "ble_error", "BLE: could not set MAC"); } diff --git a/tasmota/tasmota_xsns_sensor/xsns_62_esp32_mi.ino b/tasmota/tasmota_xsns_sensor/xsns_62_esp32_mi.ino index 54207f673..461594c7d 100644 --- a/tasmota/tasmota_xsns_sensor/xsns_62_esp32_mi.ino +++ b/tasmota/tasmota_xsns_sensor/xsns_62_esp32_mi.ino @@ -68,6 +68,13 @@ #include "include/xsns_62_esp32_mi.h" #include "services/gap/ble_svc_gap.h" +#undef LOG_LEVEL_DEBUG +#undef LOG_LEVEL_INFO +#undef LOG_LEVEL_WARN +#undef LOG_LEVEL_ERROR +#undef LOG_LEVEL_CRITICAL +#undef LOG_LEVEL_NONE + void MI32notifyCB(NimBLERemoteCharacteristic* pRemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify); void MI32AddKey(mi_bindKey_t keyMAC); void MI32HandleEveryDevice(const NimBLEAdvertisedDevice* advertisedDevice, uint8_t addr[6], int RSSI); @@ -100,6 +107,9 @@ class MI32SensorCallback : public NimBLEClientCallbacks { MI32.mode.triggerBerryConnCB = 1; //AddLog(LOG_LEVEL_DEBUG,PSTR("disconnected")); } + void onPassKeyEntry(NimBLEConnInfo& connInfo) { + NimBLEDevice::injectPassKey(connInfo, MI32.conCtx->pin); + } }; class MI32AdvCallbacks: public NimBLEScanCallbacks { @@ -210,7 +220,7 @@ class MI32CharacteristicCallbacks: public NimBLECharacteristicCallbacks { } item; item.header.length = 0; item.header.type = BLE_OP_ON_READ; - item.header.returnCharUUID = *(uint16_t*)pCharacteristic->getUUID().getValue(); + item.header.returnCharUUID = *reinterpret_cast(pCharacteristic->getUUID().getValue() + 12); item.header.handle = pCharacteristic->getHandle(); xRingbufferSend(BLERingBufferQueue, (const void*)&item, sizeof(BLERingBufferItem_t), pdMS_TO_TICKS(1)); }; @@ -222,7 +232,7 @@ class MI32CharacteristicCallbacks: public NimBLECharacteristicCallbacks { } item; item.header.length = pCharacteristic->getValue().size(); item.header.type = BLE_OP_ON_WRITE; - item.header.returnCharUUID = *(uint16_t*)pCharacteristic->getUUID().getValue(); + item.header.returnCharUUID = *reinterpret_cast(pCharacteristic->getUUID().getValue() + 12); item.header.handle = pCharacteristic->getHandle(); memcpy(item.buffer,pCharacteristic->getValue().data(),pCharacteristic->getValue().size()); xRingbufferSend(BLERingBufferQueue, (const void*)&item, sizeof(BLERingBufferItem_t) + item.header.length , pdMS_TO_TICKS(1)); @@ -238,7 +248,7 @@ class MI32CharacteristicCallbacks: public NimBLECharacteristicCallbacks { } item; item.header.length = sizeof(item.buffer); item.header.type = BLE_OP_ON_STATUS; - item.header.returnCharUUID = *(uint16_t*)pCharacteristic->getUUID().getValue(); + item.header.returnCharUUID = *reinterpret_cast(pCharacteristic->getUUID().getValue() + 12); item.header.handle = pCharacteristic->getHandle(); memcpy(item.buffer,&code,item.header.length); xRingbufferSend(BLERingBufferQueue, (const void*)&item, sizeof(BLERingBufferItem_t) + item.header.length, pdMS_TO_TICKS(1)); @@ -250,7 +260,7 @@ class MI32CharacteristicCallbacks: public NimBLECharacteristicCallbacks { } item; item.header.length = 0; item.header.type = BLE_OP_ON_UNSUBSCRIBE + subValue;; - item.header.returnCharUUID = *(uint16_t*)pCharacteristic->getUUID().getValue(); + item.header.returnCharUUID = *reinterpret_cast(pCharacteristic->getUUID().getValue() + 12); item.header.handle = pCharacteristic->getHandle(); xRingbufferSend(BLERingBufferQueue, (const void*)&item, sizeof(BLERingBufferItem_t), pdMS_TO_TICKS(1)); }; @@ -266,7 +276,7 @@ void MI32notifyCB(NimBLERemoteCharacteristic* pRemoteCharacteristic, uint8_t* pD item.header.length = length; // item.header.type = 103; does not matter for now memcpy(item.buffer,pData,length); - item.header.returnCharUUID = *(uint16_t*)pRemoteCharacteristic->getUUID().getValue(); + item.header.returnCharUUID = *reinterpret_cast(pRemoteCharacteristic->getUUID().getValue() + 12); item.header.handle = pRemoteCharacteristic->getHandle(); xRingbufferSend(BLERingBufferQueue, (const void*)&item, sizeof(BLERingBufferItem_t) + length , pdMS_TO_TICKS(5)); MI32.mode.readingDone = 1; @@ -710,6 +720,7 @@ void MI32Init(void) { NimBLEDevice::setSecurityAuth(true, true, true); #else NimBLEDevice::setSecurityAuth(false, true, true); + NimBLEDevice::setSecurityIOCap(BLE_HS_IO_KEYBOARD_DISPLAY); #endif AddLog(LOG_LEVEL_INFO,PSTR("M32: Init BLE device: %s"),TasmotaGlobal.hostname); @@ -854,7 +865,7 @@ extern "C" { return; } if(MI32.conCtx == nullptr){ - MI32.conCtx = new MI32connectionContextBerry_t; + MI32.conCtx = new MI32connectionContextBerry_t{}; } MI32.conCtx->buffer = buffer; MI32.beServerCB = function; @@ -876,7 +887,7 @@ extern "C" { if(MI32.conCtx != nullptr){ MI32.conCtx->charUUID = NimBLEUUID(Chr); AddLog(LOG_LEVEL_DEBUG,PSTR("M32: CHR: %s"),MI32.conCtx->charUUID.toString().c_str()); - uint16_t _uuid = *(uint16_t*)MI32.conCtx->charUUID.getValue(); //if not "notify op" -> present requested characteristic as return UUID + uint16_t _uuid = *reinterpret_cast(MI32.conCtx->charUUID.getValue() + 12); //if not "notify op" -> present requested characteristic as return UUID MI32.conCtx->returnCharUUID = _uuid; AddLog(LOG_LEVEL_DEBUG,PSTR("M32: return 16-bit UUID: %04x"),MI32.conCtx->returnCharUUID); return true; @@ -884,11 +895,13 @@ extern "C" { return false; } - bool MI32setBerryCtxMAC(uint8_t *MAC, uint8_t type){ + bool MI32setBerryCtxMAC(uint8_t *MAC, uint8_t type, uint32_t pin){ if(MI32.conCtx != nullptr){ memcpy(MI32.conCtx->MAC,MAC,6); if(type<4) MI32.conCtx->addrType = type; - else MI32.conCtx->addrType = 0; + else MI32.conCtx->addrType = 0; + MI32.conCtx->pin = pin; + return true; } return false; @@ -1349,6 +1362,9 @@ void MI32ConnectionTask(void *pvParameters){ #endif //CONFIG_IDF_TARGET_ESP32C3 MI32.conCtx->error = MI32_CONN_NO_ERROR; if (MI32ConnectActiveSensor()){ + if (BLERingBufferQueue == nullptr){ + BLERingBufferQueue = xRingbufferCreate(2048, RINGBUF_TYPE_NOSPLIT); + } MI32.mode.readingDone = 0; uint32_t timer = 0; while (MI32.mode.connected == 0){ @@ -1450,12 +1466,9 @@ void MI32ConnectionTask(void *pvParameters){ MI32.mode.readingDone = 1; break; case 3: - if (BLERingBufferQueue == nullptr){ - BLERingBufferQueue = xRingbufferCreate(2048, RINGBUF_TYPE_NOSPLIT); - if(!BLERingBufferQueue) { - MI32.conCtx->error = MI32_CONN_CAN_NOT_NOTIFY; - break; - } + if(!BLERingBufferQueue) { + MI32.conCtx->error = MI32_CONN_CAN_NOT_NOTIFY; + break; } if(MI32.conCtx->hasArg1){ // characteristic selected by handle if (pChr->canNotify()) { @@ -1698,7 +1711,7 @@ void MI32ServerSetCharacteristic(NimBLEServer *pServer, std::vectorgetUUID().getValue(); + item.header.returnCharUUID = *reinterpret_cast(pCharacteristic->getUUID().getValue() + 12); item.header.handle = pCharacteristic->getHandle(); xRingbufferSend(BLERingBufferQueue, (const void*)&item, sizeof(BLERingBufferItem_t), pdMS_TO_TICKS(1)); }