feat(examples): Add BLE support in the esp_local_ctrl example

This commit is contained in:
harshal.patil 2023-12-15 11:06:06 +05:30
parent fd012bf2c4
commit fd637d5be7
No known key found for this signature in database
GPG Key ID: 5B5EC97C35B9A2E5
7 changed files with 74 additions and 7 deletions

View File

@ -11,7 +11,7 @@ set(srcs "src/esp_local_ctrl.c"
"proto-c/esp_local_ctrl.pb-c.c")
if(CONFIG_BT_ENABLED)
if(CONFIG_BT_BLUEDROID_ENABLED)
if(CONFIG_BT_BLUEDROID_ENABLED OR CONFIG_BT_NIMBLE_ENABLED)
list(APPEND srcs
"src/esp_local_ctrl_transport_ble.c")
endif()

View File

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2019-2022 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
@ -195,7 +195,7 @@ const esp_local_ctrl_transport_t *esp_local_ctrl_get_transport_httpd(void);
* @brief Configuration for transport mode BLE
*
* This is a forward declaration for `protocomm_ble_config_t`.
* To use this, application must set CONFIG_BT_BLUEDROID_ENABLED
* To use this, application must set CONFIG_BT_ENABLED
* and include `protocomm_ble.h`.
*/
typedef struct protocomm_ble_config esp_local_ctrl_transport_config_ble_t;

View File

@ -1,5 +1,22 @@
menu "Example Configuration"
choice EXAMPLE_LOCAL_CTRL_TRANSPORT
bool "Local Control Transport"
default EXAMPLE_LOCAL_CTRL_TRANSPORT_BLE if !SOC_WIFI_SUPPORTED
default EXAMPLE_LOCAL_CTRL_TRANSPORT_SOFTAP
help
Local Control component offers both, SoftAP and BLE transports. Choose any one.
config EXAMPLE_LOCAL_CTRL_TRANSPORT_SOFTAP
bool "Soft AP"
select LWIP_IPV4
depends on SOC_WIFI_SUPPORTED
config EXAMPLE_LOCAL_CTRL_TRANSPORT_BLE
bool "BLE"
select BT_ENABLED
endchoice
choice EXAMPLE_PROTOCOMM_SECURITY_VERSION
bool "Protocomm security version"
default EXAMPLE_PROTOCOMM_SECURITY_VERSION_2
@ -41,4 +58,13 @@ menu "Example Configuration"
security version 2.
endchoice
config EXAMPLE_LOCAL_CTRL_USING_BLUEDROID
bool
depends on (BT_BLUEDROID_ENABLED && !IDF_TARGET_ESP32)
select BT_BLE_42_FEATURES_SUPPORTED
default y
help
This enables BLE 4.2 features for Bluedroid which are required for
the API definitions that are present in the esp_gap_ble_api header.
endmenu

View File

@ -38,12 +38,17 @@ void app_main(void)
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
#ifdef CONFIG_EXAMPLE_LOCAL_CTRL_TRANSPORT_SOFTAP
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
if (example_connect() == ESP_OK) {
start_esp_local_ctrl_service();
} else {
if (example_connect() != ESP_OK) {
ESP_LOGI(TAG, "Connection failed, not starting esp_local_ctrl service");
vTaskDelay(portMAX_DELAY);
}
#endif /* CONFIG_EXAMPLE_LOCAL_CTRL_TRANSPORT_SOFTAP */
start_esp_local_ctrl_service();
ESP_LOGI(TAG, "esp_local_ctrl service started");
}

View File

@ -20,6 +20,7 @@
#include <esp_log.h>
#include <esp_timer.h>
#include <esp_local_ctrl.h>
#include <protocomm_ble.h>
static const char *TAG = "control";
@ -224,6 +225,7 @@ static void free_str(void *arg)
/* Function used by app_main to start the esp_local_ctrl service */
void start_esp_local_ctrl_service(void)
{
#ifdef CONFIG_EXAMPLE_LOCAL_CTRL_TRANSPORT_SOFTAP
#ifdef CONFIG_ESP_HTTPS_SERVER_ENABLE
/* Set the configuration */
httpd_ssl_config_t https_conf = HTTPD_SSL_CONFIG_DEFAULT();
@ -242,6 +244,26 @@ void start_esp_local_ctrl_service(void)
#else
httpd_config_t http_conf = HTTPD_DEFAULT_CONFIG();
#endif
#else /* CONFIG_EXAMPLE_LOCAL_CTRL_TRANSPORT_BLE */
protocomm_ble_config_t *ble_conf = & (protocomm_ble_config_t) {
.device_name = SERVICE_NAME,
/* Set a random 128 bit UUID which will be included in the BLE advertisement
* and will correspond to the primary GATT service that provides provisioning
* endpoints as GATT characteristics. Each GATT characteristic will be
* formed using the primary service UUID as base, with different auto assigned
* 12th and 13th bytes (assume counting starts from 0th byte). The client side
* applications must identify the endpoints by reading the User Characteristic
* Description descriptor (0x2901) for each characteristic, which contains the
* endpoint name of the characteristic */
.service_uuid = {
/* LSB <---------------------------------------
* ---------------------------------------> MSB */
0x21, 0xd5, 0x3b, 0x8d, 0xbd, 0x75, 0x68, 0x8a,
0xb4, 0x42, 0xeb, 0x31, 0x4a, 0x1e, 0x98, 0x3d,
}
};
#endif /* CONFIG_EXAMPLE_LOCAL_CTRL_TRANSPORT_SOFTAP */
#ifdef CONFIG_EXAMPLE_PROTOCOMM_SECURITY_VERSION_1
/* What is the security level that we want (0, 1, 2):
* - PROTOCOMM_SECURITY_0 is simply plain text communication.
@ -284,6 +306,7 @@ void start_esp_local_ctrl_service(void)
#endif
esp_local_ctrl_config_t config = {
#ifdef CONFIG_EXAMPLE_LOCAL_CTRL_TRANSPORT_SOFTAP
.transport = ESP_LOCAL_CTRL_TRANSPORT_HTTPD,
.transport_config = {
#ifdef CONFIG_ESP_HTTPS_SERVER_ENABLE
@ -292,6 +315,12 @@ void start_esp_local_ctrl_service(void)
.httpd = &http_conf,
#endif
},
#else /* CONFIG_EXAMPLE_LOCAL_CTRL_TRANSPORT_BLE */
.transport = ESP_LOCAL_CTRL_TRANSPORT_BLE,
.transport_config = {
.ble = ble_conf,
},
#endif /* CONFIG_EXAMPLE_LOCAL_CTRL_TRANSPORT_SOFTAP */
.proto_sec = {
.version = security,
.custom_handle = NULL,
@ -308,8 +337,10 @@ void start_esp_local_ctrl_service(void)
.max_properties = 10
};
#ifdef CONFIG_EXAMPLE_LOCAL_CTRL_TRANSPORT_SOFTAP
mdns_init();
mdns_hostname_set(SERVICE_NAME);
#endif
/* Start esp_local_ctrl service */
ESP_ERROR_CHECK(esp_local_ctrl_start(&config));

View File

@ -136,7 +136,7 @@ async def get_transport(sel_transport, service_name, check_hostname):
tp = esp_prov.transport.Transport_HTTP(service_name, ssl_ctx)
elif (sel_transport == 'ble'):
tp = esp_prov.transport.Transport_BLE(
devname=service_name, service_uuid='0000ffff-0000-1000-8000-00805f9b34fb',
service_uuid='3d981e4a-31eb-42b4-8a68-75bd8d3bd521',
nu_lookup={'esp_local_ctrl/version': '0001',
'esp_local_ctrl/session': '0002',
'esp_local_ctrl/control': '0003'}

View File

@ -0,0 +1,5 @@
# Override some defaults so BT stack is enabled and
CONFIG_BT_ENABLED=y
CONFIG_BT_NIMBLE_ENABLED=y
CONFIG_EXAMPLE_LOCAL_CTRL_TRANSPORT_BLE=y