diff --git a/main/blufi.cpp b/main/blufi.cpp index c62239d2..4041f129 100644 --- a/main/blufi.cpp +++ b/main/blufi.cpp @@ -36,6 +36,7 @@ # include "esp_blufi_api.h" # include "esp_mac.h" # include "esp_timer.h" +# include "services/gap/ble_svc_gap.h" extern "C" { # include "esp_blufi.h" @@ -48,6 +49,10 @@ static NimBLEOta* pNimBLEOta; static NimBLECharacteristic* pCommandCharacteristic; static NimBLECharacteristic* pRecvFwCharacteristic; +#ifndef BLUFI_MFG_ID +# define BLUFI_MFG_ID 0xFFFF // Default Manufacturer ID if not defined +#endif + struct pkt_info { uint8_t* pkt; int pkt_len; @@ -196,16 +201,45 @@ void restart_connection_timer() {} void stop_connection_timer() {} # endif +void set_blufi_mfg_data () { + ble_hs_adv_fields fields; + ble_uuid16_t blufi_uuid = BLE_UUID16_INIT(BLUFI_APP_UUID); + memset(&fields, 0, sizeof(fields)); + fields.flags = BLE_HS_ADV_F_DISC_GEN | BLE_HS_ADV_F_BREDR_UNSUP; + const char* name = ble_svc_gap_device_name(); + fields.name = (uint8_t*)name; + fields.name_len = strlen(name); + fields.name_is_complete = true; + fields.uuids16 = &blufi_uuid; + fields.num_uuids16 = 1; + fields.uuids16_is_complete = true; + uint8_t omg_blufi_mfg_data[] = {BLUFI_MFG_ID, BLUFI_MFG_ID >> 8, 'O', gatewayState}; + fields.mfg_data = omg_blufi_mfg_data; + fields.mfg_data_len = sizeof(omg_blufi_mfg_data); + auto rc = ble_gap_adv_set_fields(&fields); + if (rc != 0) { + Log.error(F("Failed to set BLE advertising fields: %d" CR), rc); + } else { + Log.trace(F("BLE advertising fields set successfully" CR)); + } +} + +void start_blufi_advertising() { + esp_blufi_adv_start(); + set_blufi_mfg_data(); +} + 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: + case ESP_BLUFI_EVENT_INIT_FINISH: { Log.trace(F("BLUFI init finish" CR)); - esp_blufi_adv_start(); + start_blufi_advertising(); break; + } case ESP_BLUFI_EVENT_DEINIT_FINISH: Log.trace(F("BLUFI deinit finish" CR)); NimBLEDevice::deinit(true); @@ -243,7 +277,7 @@ static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_para pCommandCharacteristic->getCallbacks()->onSubscribe(pCommandCharacteristic, *(NimBLEConnInfo*)&desc, 0); pRecvFwCharacteristic->getCallbacks()->onSubscribe(pRecvFwCharacteristic, *(NimBLEConnInfo*)&desc, 0); blufi_security_deinit(); - esp_blufi_adv_start(); + start_blufi_advertising(); break; case ESP_BLUFI_EVENT_REQ_CONNECT_TO_AP: Log.trace(F("BLUFI request wifi connect to AP" CR)); diff --git a/main/main.cpp b/main/main.cpp index 957d6537..bd2b379f 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -36,6 +36,7 @@ #include "TheengsUtils.h" GatewayState gatewayState = GatewayState::WAITING_ONBOARDING; +static GatewayState previousGatewayState = gatewayState; // Macros and structure to enable the duplicates removing on the following gateways #if defined(ZgatewayRF) || defined(ZgatewayIR) || defined(ZgatewaySRFB) || defined(ZgatewayWeatherStation) || defined(ZgatewayRTL_433) @@ -222,6 +223,7 @@ extern bool isBlufiConnected(); extern bool isStaConnecting(); extern bool startBlufi(); extern bool stopBlufi(); +extern void set_blufi_mfg_data(); #endif /*------------------------------------------------------------------------*/ @@ -1252,7 +1254,6 @@ void updateAndHandleLEDsTask(void* pvParameters) { #endif void updateAndHandleLEDsTask() { - static GatewayState previousGatewayState; if (previousGatewayState != gatewayState) { #ifdef LED_POWER ledManager.setMode(STRIP_POWER, LED_POWER, LEDManager::STATIC, LED_POWER_COLOR, -1); @@ -2555,6 +2556,17 @@ void loop() { gatewayState = GatewayState::NTWK_CONNECTED; } } + +#if defined(ESP32) && defined(USE_BLUFI) + if (SYSConfig.blufi) { + if (gatewayState != previousGatewayState) { + Log.notice(F("Gateway state changed to: %d" CR), gatewayState); + previousGatewayState = gatewayState; + set_blufi_mfg_data(); + } + } +#endif + if (!ProcessLock) { if (now > (timer_sys_measures + (TimeBetweenReadingSYS * 1000)) || !timer_sys_measures) { timer_sys_measures = millis();