Skip to content

Commit 5b3b831

Browse files
committed
Add config option for BLE OTA updates
1 parent 626b29e commit 5b3b831

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

environments.ini

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1900,6 +1900,7 @@ lib_deps =
19001900
${com-esp32.lib_deps}
19011901
${libraries.decoder}
19021902
1903+
19031904
build_flags =
19041905
${com-esp32.build_flags}
19051906
'-DZgatewayBT="BT"'
@@ -1908,6 +1909,6 @@ build_flags =
19081909
'-DGateway_Name="OMG_ESP32_BLE"'
19091910
'-DUSE_BLUFI=1'
19101911
'-DenableMultiGTWSync=false'
1911-
'-DARDUINO_LOOP_STACK_SIZE=17500'
19121912
'-DCONFIG_NIMBLE_CPP_FREERTOS_TASK_BLOCK_BIT=31'
1913+
'-DCONFIG_NIMBLE_CPP_LOG_LEVEL=5'
19131914
custom_description = Regular BLE gateway based on esp-idf with adaptive scanning activated, automatically adapts the scan parameters depending on your devices

main/Zblufi.ino

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*/
2828
#if defined(ESP32) && defined(USE_BLUFI)
2929
# include "NimBLEDevice.h"
30+
# include "NimBLEOta.h"
3031
# include "esp_blufi_api.h"
3132
# include "esp_timer.h"
3233

@@ -35,6 +36,9 @@ extern "C" {
3536
}
3637

3738
static esp_timer_handle_t connection_timer = nullptr;
39+
static NimBLEOta* pNimBLEOta;
40+
static NimBLECharacteristic* pCommandCharacteristic;
41+
static NimBLECharacteristic* pRecvFwCharacteristic;
3842

3943
struct pkt_info {
4044
uint8_t* pkt;
@@ -183,6 +187,8 @@ void stop_connection_timer() {}
183187
static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_t* param) {
184188
/* actually, should post to blufi_task handle the procedure,
185189
* now, as a example, we do it more simply */
190+
ble_gap_conn_desc desc;
191+
186192
switch (event) {
187193
case ESP_BLUFI_EVENT_INIT_FINISH:
188194
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
201207
gatewayState = GatewayState::ONBOARDING;
202208
omg_blufi_ble_connected = true;
203209
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);
204213
esp_blufi_adv_stop();
205214
blufi_security_init();
206215
break;
@@ -217,6 +226,9 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para
217226
} else {
218227
gatewayState = GatewayState::OFFLINE;
219228
}
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);
220232
blufi_security_deinit();
221233
esp_blufi_adv_start();
222234
break;
@@ -401,7 +413,14 @@ bool startBlufi() {
401413
}
402414
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]);
403415
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();
404419
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));
405424
ble_gatts_start();
406425
Log.notice(F("BLUFI started" CR));
407426
return esp_blufi_profile_init() == ESP_OK;

sdkconfig.defaults

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ CONFIG_AUTOSTART_ARDUINO=y
135135
CONFIG_ARDUINO_RUN_CORE1=y
136136
# CONFIG_ARDUINO_RUN_NO_AFFINITY is not set
137137
CONFIG_ARDUINO_RUNNING_CORE=1
138-
CONFIG_ARDUINO_LOOP_STACK_SIZE=8192
138+
CONFIG_ARDUINO_LOOP_STACK_SIZE=17500
139139
# CONFIG_ARDUINO_EVENT_RUN_CORE0 is not set
140140
CONFIG_ARDUINO_EVENT_RUN_CORE1=y
141141
# CONFIG_ARDUINO_EVENT_RUN_NO_AFFINITY is not set

0 commit comments

Comments
 (0)