Skip to content

Commit

Permalink
Add config option for BLE OTA updates
Browse files Browse the repository at this point in the history
  • Loading branch information
h2zero committed Dec 23, 2024
1 parent 626b29e commit 5b3b831
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
3 changes: 2 additions & 1 deletion environments.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1900,6 +1900,7 @@ lib_deps =
${com-esp32.lib_deps}
${libraries.decoder}
h2zero/[email protected]
h2zero/[email protected]
build_flags =
${com-esp32.build_flags}
'-DZgatewayBT="BT"'
Expand All @@ -1908,6 +1909,6 @@ build_flags =
'-DGateway_Name="OMG_ESP32_BLE"'
'-DUSE_BLUFI=1'
'-DenableMultiGTWSync=false'
'-DARDUINO_LOOP_STACK_SIZE=17500'
'-DCONFIG_NIMBLE_CPP_FREERTOS_TASK_BLOCK_BIT=31'
'-DCONFIG_NIMBLE_CPP_LOG_LEVEL=5'
custom_description = Regular BLE gateway based on esp-idf with adaptive scanning activated, automatically adapts the scan parameters depending on your devices
19 changes: 19 additions & 0 deletions main/Zblufi.ino
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
#if defined(ESP32) && defined(USE_BLUFI)
# include "NimBLEDevice.h"
# include "NimBLEOta.h"
# include "esp_blufi_api.h"
# include "esp_timer.h"

Expand All @@ -35,6 +36,9 @@ extern "C" {
}

static esp_timer_handle_t connection_timer = nullptr;
static NimBLEOta* pNimBLEOta;
static NimBLECharacteristic* pCommandCharacteristic;
static NimBLECharacteristic* pRecvFwCharacteristic;

struct pkt_info {
uint8_t* pkt;
Expand Down Expand Up @@ -183,6 +187,8 @@ void stop_connection_timer() {}
static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_t* param) {
/* actually, should post to blufi_task handle the procedure,
* now, as a example, we do it more simply */
ble_gap_conn_desc desc;

switch (event) {
case ESP_BLUFI_EVENT_INIT_FINISH:
Log.trace(F("BLUFI init finish" CR));
Expand All @@ -201,6 +207,9 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para
gatewayState = GatewayState::ONBOARDING;
omg_blufi_ble_connected = true;
restart_connection_timer();
ble_gap_conn_find(param->connect.conn_id, &desc);
pCommandCharacteristic->getCallbacks()->onSubscribe(pCommandCharacteristic, *(NimBLEConnInfo*)&desc, 2);
pRecvFwCharacteristic->getCallbacks()->onSubscribe(pRecvFwCharacteristic, *(NimBLEConnInfo*)&desc, 2);
esp_blufi_adv_stop();
blufi_security_init();
break;
Expand All @@ -217,6 +226,9 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para
} else {
gatewayState = GatewayState::OFFLINE;
}
ble_gap_conn_find(param->connect.conn_id, &desc);
pCommandCharacteristic->getCallbacks()->onSubscribe(pCommandCharacteristic, *(NimBLEConnInfo*)&desc, 0);
pRecvFwCharacteristic->getCallbacks()->onSubscribe(pRecvFwCharacteristic, *(NimBLEConnInfo*)&desc, 0);
blufi_security_deinit();
esp_blufi_adv_start();
break;
Expand Down Expand Up @@ -401,7 +413,14 @@ bool startBlufi() {
}
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]);
NimBLEDevice::init(advName);
NimBLEDevice::setMTU(517);
NimBLEDevice::createServer(); // this initializes the GATT server so we need to reset it for blufi to init
ble_gatts_reset();
esp_blufi_gatt_svr_init();
pNimBLEOta = new NimBLEOta();
NimBLEService* pNimBLEOtaSvc = pNimBLEOta->start();
pCommandCharacteristic = pNimBLEOtaSvc->getCharacteristic(NimBLEUUID((uint16_t)0x8022));
pRecvFwCharacteristic = pNimBLEOtaSvc->getCharacteristic(NimBLEUUID((uint16_t)0x8020));
ble_gatts_start();
Log.notice(F("BLUFI started" CR));
return esp_blufi_profile_init() == ESP_OK;
Expand Down
2 changes: 1 addition & 1 deletion sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ CONFIG_AUTOSTART_ARDUINO=y
CONFIG_ARDUINO_RUN_CORE1=y
# CONFIG_ARDUINO_RUN_NO_AFFINITY is not set
CONFIG_ARDUINO_RUNNING_CORE=1
CONFIG_ARDUINO_LOOP_STACK_SIZE=8192
CONFIG_ARDUINO_LOOP_STACK_SIZE=17500
# CONFIG_ARDUINO_EVENT_RUN_CORE0 is not set
CONFIG_ARDUINO_EVENT_RUN_CORE1=y
# CONFIG_ARDUINO_EVENT_RUN_NO_AFFINITY is not set
Expand Down

0 comments on commit 5b3b831

Please sign in to comment.