From 3cadeddde88dcd7bb0c94f1373a6ec40a9603416 Mon Sep 17 00:00:00 2001 From: Chirag Atal Date: Mon, 23 May 2022 19:38:55 +0530 Subject: [PATCH] app_ble: Add support to disable BLE examples: Disabled BLE once commissioning is complete. --- examples/common/app_ble/CMakeLists.txt | 3 ++ examples/common/app_ble/Kconfig | 8 ++++ examples/common/app_ble/app_ble.cpp | 45 ++++++++++++++++++++ examples/common/app_ble/app_ble.h | 16 +++++++ examples/light/main/CMakeLists.txt | 2 +- examples/light/main/app_main.cpp | 2 + examples/light_switch/main/CMakeLists.txt | 2 +- examples/light_switch/main/app_main.cpp | 2 + examples/rainmaker_light/main/CMakeLists.txt | 2 +- examples/rainmaker_light/main/app_main.cpp | 2 + examples/zap_light/main/CMakeLists.txt | 2 +- examples/zap_light/main/app_main.cpp | 2 + examples/zigbee_bridge/main/CMakeLists.txt | 3 +- examples/zigbee_bridge/main/app_main.cpp | 6 ++- 14 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 examples/common/app_ble/CMakeLists.txt create mode 100644 examples/common/app_ble/Kconfig create mode 100644 examples/common/app_ble/app_ble.cpp create mode 100644 examples/common/app_ble/app_ble.h diff --git a/examples/common/app_ble/CMakeLists.txt b/examples/common/app_ble/CMakeLists.txt new file mode 100644 index 000000000..0b70017cf --- /dev/null +++ b/examples/common/app_ble/CMakeLists.txt @@ -0,0 +1,3 @@ +idf_component_register(SRCS app_ble.cpp + INCLUDE_DIRS . + REQUIRES bt) diff --git a/examples/common/app_ble/Kconfig b/examples/common/app_ble/Kconfig new file mode 100644 index 000000000..16b84e610 --- /dev/null +++ b/examples/common/app_ble/Kconfig @@ -0,0 +1,8 @@ +menu "App BLE" + + config DEINIT_BLE_ON_COMMISSIONING_COMPLETE + bool "Disable and DeInit BLE on commissioning complete" + default y + help + Disable and deinit BLE and reclaim all its memory, once the commissioning is successful and Commissioning complete event is received in the application. +endmenu diff --git a/examples/common/app_ble/app_ble.cpp b/examples/common/app_ble/app_ble.cpp new file mode 100644 index 000000000..0e71b93cd --- /dev/null +++ b/examples/common/app_ble/app_ble.cpp @@ -0,0 +1,45 @@ +/* + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. +*/ + +/* It is recommended to copy this code in your example so that you can modify as per your application's needs, + * especially for the indicator calbacks, button_factory_reset_pressed_cb() and button_factory_reset_released_cb(). + */ + +#include + +#include +#include +#include +#include + +static const char *TAG = "app_ble"; + +esp_err_t app_ble_disable() +{ +#if CONFIG_BT_NIMBLE_ENABLED && CONFIG_DEINIT_BLE_ON_COMMISSIONING_COMPLETE + if (!ble_hs_is_enabled()) { + ESP_LOGI(TAG, "BLE already deinited"); + return ESP_OK; + } + + int ret = nimble_port_stop(); + if (ret != 0) { + ESP_LOGE(TAG, "nimble_port_stop() failed"); + return ESP_FAIL; + } + nimble_port_deinit(); + esp_err_t err = esp_nimble_hci_and_controller_deinit(); + err |= esp_bt_mem_release(ESP_BT_MODE_BLE); + if (err != ESP_OK) { + ESP_LOGE(TAG, "BLE deinit failed"); + return ESP_FAIL; + } + ESP_LOGI(TAG, "BLE deinit successful and memory reclaimed"); +#endif + return ESP_OK; +} diff --git a/examples/common/app_ble/app_ble.h b/examples/common/app_ble/app_ble.h new file mode 100644 index 000000000..8b6fd7b9e --- /dev/null +++ b/examples/common/app_ble/app_ble.h @@ -0,0 +1,16 @@ +/* + This example code is in the Public Domain (or CC0 licensed, at your option.) + + Unless required by applicable law or agreed to in writing, this + software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + CONDITIONS OF ANY KIND, either express or implied. +*/ + +#pragma once + +/** Disable BLE + * + * @return ESP_OK on success. + * @return error in case of failure. + */ +esp_err_t app_ble_disable(); diff --git a/examples/light/main/CMakeLists.txt b/examples/light/main/CMakeLists.txt index b9a2c8e4b..3d78c6b5f 100644 --- a/examples/light/main/CMakeLists.txt +++ b/examples/light/main/CMakeLists.txt @@ -1,4 +1,4 @@ -set(PRIV_REQUIRES_LIST device esp_matter esp_matter_console route_hook app_qrcode app_reset esp_matter_ota) +set(PRIV_REQUIRES_LIST device esp_matter esp_matter_console route_hook app_ble app_qrcode app_reset esp_matter_ota) idf_component_register(SRC_DIRS "." PRIV_INCLUDE_DIRS "." diff --git a/examples/light/main/app_main.cpp b/examples/light/main/app_main.cpp index 6ad35725d..65725b8c4 100644 --- a/examples/light/main/app_main.cpp +++ b/examples/light/main/app_main.cpp @@ -15,6 +15,7 @@ #include #include +#include #include #include @@ -37,6 +38,7 @@ static void app_event_cb(const ChipDeviceEvent *event, intptr_t arg) case chip::DeviceLayer::DeviceEventType::PublicEventTypes::kCommissioningComplete: ESP_LOGI(TAG, "Commissioning complete"); + app_ble_disable(); break; default: diff --git a/examples/light_switch/main/CMakeLists.txt b/examples/light_switch/main/CMakeLists.txt index b9a2c8e4b..3d78c6b5f 100644 --- a/examples/light_switch/main/CMakeLists.txt +++ b/examples/light_switch/main/CMakeLists.txt @@ -1,4 +1,4 @@ -set(PRIV_REQUIRES_LIST device esp_matter esp_matter_console route_hook app_qrcode app_reset esp_matter_ota) +set(PRIV_REQUIRES_LIST device esp_matter esp_matter_console route_hook app_ble app_qrcode app_reset esp_matter_ota) idf_component_register(SRC_DIRS "." PRIV_INCLUDE_DIRS "." diff --git a/examples/light_switch/main/app_main.cpp b/examples/light_switch/main/app_main.cpp index e31c11f42..405480d8c 100644 --- a/examples/light_switch/main/app_main.cpp +++ b/examples/light_switch/main/app_main.cpp @@ -15,6 +15,7 @@ #include #include +#include #include #include @@ -37,6 +38,7 @@ static void app_event_cb(const ChipDeviceEvent *event, intptr_t arg) case chip::DeviceLayer::DeviceEventType::PublicEventTypes::kCommissioningComplete: ESP_LOGI(TAG, "Commissioning complete"); + app_ble_disable(); break; default: diff --git a/examples/rainmaker_light/main/CMakeLists.txt b/examples/rainmaker_light/main/CMakeLists.txt index d144c63df..759c5ec2e 100644 --- a/examples/rainmaker_light/main/CMakeLists.txt +++ b/examples/rainmaker_light/main/CMakeLists.txt @@ -1,4 +1,4 @@ -set(PRIV_REQUIRES_LIST device esp_matter esp_matter_console esp_matter_rainmaker route_hook app_qrcode app_reset +set(PRIV_REQUIRES_LIST device esp_matter esp_matter_console esp_matter_rainmaker route_hook app_ble app_qrcode app_reset esp_rainmaker) idf_component_register(SRC_DIRS "." diff --git a/examples/rainmaker_light/main/app_main.cpp b/examples/rainmaker_light/main/app_main.cpp index 76c964c60..eed3c853c 100644 --- a/examples/rainmaker_light/main/app_main.cpp +++ b/examples/rainmaker_light/main/app_main.cpp @@ -14,6 +14,7 @@ #include #include +#include #include #include #include @@ -37,6 +38,7 @@ static void app_event_cb(const ChipDeviceEvent *event, intptr_t arg) case chip::DeviceLayer::DeviceEventType::PublicEventTypes::kCommissioningComplete: ESP_LOGI(TAG, "Commissioning complete"); + app_ble_disable(); break; default: diff --git a/examples/zap_light/main/CMakeLists.txt b/examples/zap_light/main/CMakeLists.txt index 3ac9120af..90fca8cbf 100644 --- a/examples/zap_light/main/CMakeLists.txt +++ b/examples/zap_light/main/CMakeLists.txt @@ -1,4 +1,4 @@ -set(PRIV_REQUIRES_LIST device esp_matter esp_matter_console route_hook app_qrcode app_reset) +set(PRIV_REQUIRES_LIST device esp_matter esp_matter_console route_hook app_ble app_qrcode app_reset) idf_component_register(SRC_DIRS "." PRIV_INCLUDE_DIRS "." diff --git a/examples/zap_light/main/app_main.cpp b/examples/zap_light/main/app_main.cpp index 961d6b07c..802ed9ea4 100644 --- a/examples/zap_light/main/app_main.cpp +++ b/examples/zap_light/main/app_main.cpp @@ -14,6 +14,7 @@ #include #include +#include #include #include @@ -35,6 +36,7 @@ static void app_event_cb(const ChipDeviceEvent *event, intptr_t arg) case chip::DeviceLayer::DeviceEventType::PublicEventTypes::kCommissioningComplete: ESP_LOGI(TAG, "Commissioning complete"); + app_ble_disable(); break; default: diff --git a/examples/zigbee_bridge/main/CMakeLists.txt b/examples/zigbee_bridge/main/CMakeLists.txt index be4ed6371..403c7c4e2 100644 --- a/examples/zigbee_bridge/main/CMakeLists.txt +++ b/examples/zigbee_bridge/main/CMakeLists.txt @@ -1,4 +1,5 @@ -set(PRIV_REQUIRES_LIST device esp_matter esp_matter_console route_hook app_qrcode esp_matter_ota app_bridge esp-zboss-lib) +set(PRIV_REQUIRES_LIST device esp_matter esp_matter_console route_hook app_ble app_qrcode esp_matter_ota app_bridge + esp-zboss-lib) idf_component_register(SRC_DIRS "." PRIV_INCLUDE_DIRS "." diff --git a/examples/zigbee_bridge/main/app_main.cpp b/examples/zigbee_bridge/main/app_main.cpp index 31d5812b6..a0bce089c 100644 --- a/examples/zigbee_bridge/main/app_main.cpp +++ b/examples/zigbee_bridge/main/app_main.cpp @@ -15,11 +15,12 @@ #include #include -#include +#include #include #include +#include +#include -#include "zigbee_bridge.h" static const char *TAG = "app_main"; using namespace esp_matter; @@ -37,6 +38,7 @@ static void app_event_cb(const ChipDeviceEvent *event, intptr_t arg) case chip::DeviceLayer::DeviceEventType::PublicEventTypes::kCommissioningComplete: ESP_LOGI(TAG, "Commissioning complete"); + app_ble_disable(); break; default: