Skip to content

Commit 5f7cb57

Browse files
authored
MI32: add pin, some fixes (#24191)
1 parent 7d0f351 commit 5f7cb57

File tree

4 files changed

+40
-22
lines changed

4 files changed

+40
-22
lines changed

lib/libesp32/berry_tasmota/src/be_MI32_lib.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ BE_FUNC_CTYPE_DECLARE(be_BLE_reg_adv_cb, "", "c[c]");
6464
extern void be_BLE_reg_server_cb(void* function, uint8_t *buffer);
6565
BE_FUNC_CTYPE_DECLARE(be_BLE_reg_server_cb, "", "c[c]");
6666

67-
extern void be_BLE_set_MAC(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type);
68-
BE_FUNC_CTYPE_DECLARE(be_BLE_set_MAC, "", "@(bytes)~[i]");
67+
extern void be_BLE_set_MAC(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type, uint32_t pin);
68+
BE_FUNC_CTYPE_DECLARE(be_BLE_set_MAC, "", "@(bytes)~[ii]");
6969

7070
extern void be_BLE_set_characteristic(struct bvm *vm, const char *Chr);
7171
BE_FUNC_CTYPE_DECLARE(be_BLE_set_characteristic, "", "@s");

tasmota/include/xsns_62_esp32_mi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ struct MI32connectionContextBerry_t{
178178
uint8_t addrType;
179179
int error;
180180
int32_t arg1;
181+
uint32_t pin;
181182
bool hasArg1;
182183
bool oneOp;
183184
bool response;

tasmota/tasmota_xdrv_driver/xdrv_52_3_berry_MI32.ino

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ extern "C" {
110110
extern bool MI32runBerryConnection(uint8_t operation, bbool response, int32_t *arg1);
111111
extern bool MI32setBerryCtxSvc(const char *Svc, bbool discoverAttributes);
112112
extern bool MI32setBerryCtxChr(const char *Chr);
113-
extern bool MI32setBerryCtxMAC(uint8_t *MAC, uint8_t type);
113+
extern bool MI32setBerryCtxMAC(uint8_t *MAC, uint8_t type, uint32_t pin);
114114
extern bool MI32addMACtoWatchList(uint8_t *MAC, uint8_t type);
115115
extern void MI32setBerryStoreRec(uint8_t *buffer, size_t size);
116116

@@ -159,16 +159,20 @@ extern "C" {
159159
return true;
160160
}
161161

162-
void be_BLE_set_MAC(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type);
163-
void be_BLE_set_MAC(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type){
162+
void be_BLE_set_MAC(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type, uint32_t pin);
163+
void be_BLE_set_MAC(struct bvm *vm, uint8_t *buf, size_t size, uint8_t type, uint32_t pin){
164164
if(!be_BLE_MAC_size(vm, size)){
165165
return;
166166
}
167167
uint8_t _type = 0;
168168
if(type){
169169
_type = type;
170170
}
171-
if (MI32setBerryCtxMAC(buf,_type)) return;
171+
uint32_t _pin = 0;
172+
if (pin){
173+
_pin = pin;
174+
}
175+
if (MI32setBerryCtxMAC(buf,_type, _pin)) return;
172176

173177
be_raisef(vm, "ble_error", "BLE: could not set MAC");
174178
}

tasmota/tasmota_xsns_sensor/xsns_62_esp32_mi.ino

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@
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+
7178
void MI32notifyCB(NimBLERemoteCharacteristic* pRemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify);
7279
void MI32AddKey(mi_bindKey_t keyMAC);
7380
void 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

105115
class 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

Comments
 (0)