27
27
*/
28
28
#if defined(ESP32) && defined(USE_BLUFI)
29
29
# include " NimBLEDevice.h"
30
+ # include " NimBLEOta.h"
30
31
# include " esp_blufi_api.h"
31
32
# include " esp_timer.h"
32
33
@@ -35,6 +36,9 @@ extern "C" {
35
36
}
36
37
37
38
static esp_timer_handle_t connection_timer = nullptr ;
39
+ static NimBLEOta* pNimBLEOta;
40
+ static NimBLECharacteristic* pCommandCharacteristic;
41
+ static NimBLECharacteristic* pRecvFwCharacteristic;
38
42
39
43
struct pkt_info {
40
44
uint8_t * pkt;
@@ -183,6 +187,8 @@ void stop_connection_timer() {}
183
187
static void example_event_callback (esp_blufi_cb_event_t event, esp_blufi_cb_param_t * param) {
184
188
/* actually, should post to blufi_task handle the procedure,
185
189
* now, as a example, we do it more simply */
190
+ ble_gap_conn_desc desc;
191
+
186
192
switch (event) {
187
193
case ESP_BLUFI_EVENT_INIT_FINISH:
188
194
Log.trace (F (" BLUFI init finish" CR));
@@ -201,6 +207,9 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para
201
207
gatewayState = GatewayState::ONBOARDING;
202
208
omg_blufi_ble_connected = true ;
203
209
restart_connection_timer ();
210
+ ble_gap_conn_find (param->connect .conn_id , &desc);
211
+ pCommandCharacteristic->getCallbacks ()->onSubscribe (pCommandCharacteristic, *(NimBLEConnInfo*)&desc, 2 );
212
+ pRecvFwCharacteristic->getCallbacks ()->onSubscribe (pRecvFwCharacteristic, *(NimBLEConnInfo*)&desc, 2 );
204
213
esp_blufi_adv_stop ();
205
214
blufi_security_init ();
206
215
break ;
@@ -217,6 +226,9 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para
217
226
} else {
218
227
gatewayState = GatewayState::OFFLINE;
219
228
}
229
+ ble_gap_conn_find (param->connect .conn_id , &desc);
230
+ pCommandCharacteristic->getCallbacks ()->onSubscribe (pCommandCharacteristic, *(NimBLEConnInfo*)&desc, 0 );
231
+ pRecvFwCharacteristic->getCallbacks ()->onSubscribe (pRecvFwCharacteristic, *(NimBLEConnInfo*)&desc, 0 );
220
232
blufi_security_deinit ();
221
233
esp_blufi_adv_start ();
222
234
break ;
@@ -401,7 +413,14 @@ bool startBlufi() {
401
413
}
402
414
snprintf (advName, sizeof (advName), Gateway_Short_Name " _%02X%02X%02X%02X%02X%02X" , mac[0 ], mac[1 ], mac[2 ], mac[3 ], mac[4 ], mac[5 ]);
403
415
NimBLEDevice::init (advName);
416
+ NimBLEDevice::setMTU (517 );
417
+ NimBLEDevice::createServer (); // this initializes the GATT server so we need to reset it for blufi to init
418
+ ble_gatts_reset ();
404
419
esp_blufi_gatt_svr_init ();
420
+ pNimBLEOta = new NimBLEOta ();
421
+ NimBLEService* pNimBLEOtaSvc = pNimBLEOta->start ();
422
+ pCommandCharacteristic = pNimBLEOtaSvc->getCharacteristic (NimBLEUUID ((uint16_t )0x8022 ));
423
+ pRecvFwCharacteristic = pNimBLEOtaSvc->getCharacteristic (NimBLEUUID ((uint16_t )0x8020 ));
405
424
ble_gatts_start ();
406
425
Log.notice (F (" BLUFI started" CR));
407
426
return esp_blufi_profile_init () == ESP_OK;
0 commit comments