6868#include " include/xsns_62_esp32_mi.h"
6969#include " services/gap/ble_svc_gap.h"
7070
71+ #undef LOG_LEVEL_DEBUG
72+ #undef LOG_LEVEL_INFO
73+ #undef LOG_LEVEL_WARN
74+ #undef LOG_LEVEL_ERROR
75+ #undef LOG_LEVEL_CRITICAL
76+ #undef LOG_LEVEL_NONE
77+
7178void MI32notifyCB (NimBLERemoteCharacteristic* pRemoteCharacteristic, uint8_t * pData, size_t length, bool isNotify);
7279void MI32AddKey (mi_bindKey_t keyMAC);
7380void MI32HandleEveryDevice (const NimBLEAdvertisedDevice* advertisedDevice, uint8_t addr[6 ], int RSSI);
@@ -100,6 +107,9 @@ class MI32SensorCallback : public NimBLEClientCallbacks {
100107 MI32.mode .triggerBerryConnCB = 1 ;
101108 // AddLog(LOG_LEVEL_DEBUG,PSTR("disconnected"));
102109 }
110+ void onPassKeyEntry (NimBLEConnInfo& connInfo) {
111+ NimBLEDevice::injectPassKey (connInfo, MI32.conCtx ->pin );
112+ }
103113};
104114
105115class MI32AdvCallbacks : public NimBLEScanCallbacks {
@@ -210,7 +220,7 @@ class MI32CharacteristicCallbacks: public NimBLECharacteristicCallbacks {
210220 } item;
211221 item.header .length = 0 ;
212222 item.header .type = BLE_OP_ON_READ;
213- item.header .returnCharUUID = *( uint16_t *) pCharacteristic->getUUID ().getValue ();
223+ item.header .returnCharUUID = *reinterpret_cast < const uint16_t *>( pCharacteristic->getUUID ().getValue () + 12 );
214224 item.header .handle = pCharacteristic->getHandle ();
215225 xRingbufferSend (BLERingBufferQueue, (const void *)&item, sizeof (BLERingBufferItem_t), pdMS_TO_TICKS (1 ));
216226 };
@@ -222,7 +232,7 @@ class MI32CharacteristicCallbacks: public NimBLECharacteristicCallbacks {
222232 } item;
223233 item.header .length = pCharacteristic->getValue ().size ();
224234 item.header .type = BLE_OP_ON_WRITE;
225- item.header .returnCharUUID = *( uint16_t *) pCharacteristic->getUUID ().getValue ();
235+ item.header .returnCharUUID = *reinterpret_cast < const uint16_t *>( pCharacteristic->getUUID ().getValue () + 12 );
226236 item.header .handle = pCharacteristic->getHandle ();
227237 memcpy (item.buffer ,pCharacteristic->getValue ().data (),pCharacteristic->getValue ().size ());
228238 xRingbufferSend (BLERingBufferQueue, (const void *)&item, sizeof (BLERingBufferItem_t) + item.header .length , pdMS_TO_TICKS (1 ));
@@ -238,7 +248,7 @@ class MI32CharacteristicCallbacks: public NimBLECharacteristicCallbacks {
238248 } item;
239249 item.header .length = sizeof (item.buffer );
240250 item.header .type = BLE_OP_ON_STATUS;
241- item.header .returnCharUUID = *( uint16_t *) pCharacteristic->getUUID ().getValue ();
251+ item.header .returnCharUUID = *reinterpret_cast < const uint16_t *>( pCharacteristic->getUUID ().getValue () + 12 );
242252 item.header .handle = pCharacteristic->getHandle ();
243253 memcpy (item.buffer ,&code,item.header .length );
244254 xRingbufferSend (BLERingBufferQueue, (const void *)&item, sizeof (BLERingBufferItem_t) + item.header .length , pdMS_TO_TICKS (1 ));
@@ -250,7 +260,7 @@ class MI32CharacteristicCallbacks: public NimBLECharacteristicCallbacks {
250260 } item;
251261 item.header .length = 0 ;
252262 item.header .type = BLE_OP_ON_UNSUBSCRIBE + subValue;;
253- item.header .returnCharUUID = *( uint16_t *) pCharacteristic->getUUID ().getValue ();
263+ item.header .returnCharUUID = *reinterpret_cast < const uint16_t *>( pCharacteristic->getUUID ().getValue () + 12 );
254264 item.header .handle = pCharacteristic->getHandle ();
255265 xRingbufferSend (BLERingBufferQueue, (const void *)&item, sizeof (BLERingBufferItem_t), pdMS_TO_TICKS (1 ));
256266 };
@@ -266,7 +276,7 @@ void MI32notifyCB(NimBLERemoteCharacteristic* pRemoteCharacteristic, uint8_t* pD
266276 item.header .length = length;
267277 // item.header.type = 103; does not matter for now
268278 memcpy (item.buffer ,pData,length);
269- item.header .returnCharUUID = *( uint16_t *) pRemoteCharacteristic->getUUID ().getValue ();
279+ item.header .returnCharUUID = *reinterpret_cast < const uint16_t *>( pRemoteCharacteristic->getUUID ().getValue () + 12 );
270280 item.header .handle = pRemoteCharacteristic->getHandle ();
271281 xRingbufferSend (BLERingBufferQueue, (const void *)&item, sizeof (BLERingBufferItem_t) + length , pdMS_TO_TICKS (5 ));
272282 MI32.mode .readingDone = 1 ;
@@ -710,6 +720,7 @@ void MI32Init(void) {
710720 NimBLEDevice::setSecurityAuth (true , true , true );
711721 #else
712722 NimBLEDevice::setSecurityAuth (false , true , true );
723+ NimBLEDevice::setSecurityIOCap (BLE_HS_IO_KEYBOARD_DISPLAY);
713724 #endif
714725
715726 AddLog (LOG_LEVEL_INFO,PSTR (" M32: Init BLE device: %s" ),TasmotaGlobal.hostname );
@@ -854,7 +865,7 @@ extern "C" {
854865 return ;
855866 }
856867 if (MI32.conCtx == nullptr ){
857- MI32.conCtx = new MI32connectionContextBerry_t;
868+ MI32.conCtx = new MI32connectionContextBerry_t{} ;
858869 }
859870 MI32.conCtx ->buffer = buffer;
860871 MI32.beServerCB = function;
@@ -876,19 +887,21 @@ extern "C" {
876887 if (MI32.conCtx != nullptr ){
877888 MI32.conCtx ->charUUID = NimBLEUUID (Chr);
878889 AddLog (LOG_LEVEL_DEBUG,PSTR (" M32: CHR: %s" ),MI32.conCtx ->charUUID .toString ().c_str ());
879- uint16_t _uuid = *( uint16_t *) MI32.conCtx ->charUUID .getValue (); // if not "notify op" -> present requested characteristic as return UUID
890+ uint16_t _uuid = *reinterpret_cast < const uint16_t *>( MI32.conCtx ->charUUID .getValue () + 12 ); // if not "notify op" -> present requested characteristic as return UUID
880891 MI32.conCtx ->returnCharUUID = _uuid;
881892 AddLog (LOG_LEVEL_DEBUG,PSTR (" M32: return 16-bit UUID: %04x" ),MI32.conCtx ->returnCharUUID );
882893 return true ;
883894 }
884895 return false ;
885896 }
886897
887- bool MI32setBerryCtxMAC (uint8_t *MAC, uint8_t type){
898+ bool MI32setBerryCtxMAC (uint8_t *MAC, uint8_t type, uint32_t pin ){
888899 if (MI32.conCtx != nullptr ){
889900 memcpy (MI32.conCtx ->MAC ,MAC,6 );
890901 if (type<4 ) MI32.conCtx ->addrType = type;
891- else MI32.conCtx ->addrType = 0 ;
902+ else MI32.conCtx ->addrType = 0 ;
903+ MI32.conCtx ->pin = pin;
904+
892905 return true ;
893906 }
894907 return false ;
@@ -1349,6 +1362,9 @@ void MI32ConnectionTask(void *pvParameters){
13491362#endif // CONFIG_IDF_TARGET_ESP32C3
13501363 MI32.conCtx ->error = MI32_CONN_NO_ERROR;
13511364 if (MI32ConnectActiveSensor ()){
1365+ if (BLERingBufferQueue == nullptr ){
1366+ BLERingBufferQueue = xRingbufferCreate (2048 , RINGBUF_TYPE_NOSPLIT);
1367+ }
13521368 MI32.mode .readingDone = 0 ;
13531369 uint32_t timer = 0 ;
13541370 while (MI32.mode .connected == 0 ){
@@ -1450,12 +1466,9 @@ void MI32ConnectionTask(void *pvParameters){
14501466 MI32.mode .readingDone = 1 ;
14511467 break ;
14521468 case 3 :
1453- if (BLERingBufferQueue == nullptr ){
1454- BLERingBufferQueue = xRingbufferCreate (2048 , RINGBUF_TYPE_NOSPLIT);
1455- if (!BLERingBufferQueue) {
1456- MI32.conCtx ->error = MI32_CONN_CAN_NOT_NOTIFY;
1457- break ;
1458- }
1469+ if (!BLERingBufferQueue) {
1470+ MI32.conCtx ->error = MI32_CONN_CAN_NOT_NOTIFY;
1471+ break ;
14591472 }
14601473 if (MI32.conCtx ->hasArg1 ){ // characteristic selected by handle
14611474 if (pChr->canNotify ()) {
@@ -1698,7 +1711,7 @@ void MI32ServerSetCharacteristic(NimBLEServer *pServer, std::vector<NimBLEServic
16981711 } item;
16991712 item.header .length = 0 ;
17001713 item.header .type = BLE_OP_SET_CHARACTERISTIC;
1701- item.header .returnCharUUID = *( uint16_t *) pCharacteristic->getUUID ().getValue ();
1714+ item.header .returnCharUUID = *reinterpret_cast < const uint16_t *>( pCharacteristic->getUUID ().getValue () + 12 );
17021715 item.header .handle = pCharacteristic->getHandle ();
17031716 xRingbufferSend (BLERingBufferQueue, (const void *)&item, sizeof (BLERingBufferItem_t), pdMS_TO_TICKS (1 ));
17041717}
0 commit comments