From e6171b7338d25f4e2fc96a910856042c59f579cc Mon Sep 17 00:00:00 2001 From: Laukik Hase Date: Fri, 21 Oct 2022 10:25:19 +0530 Subject: [PATCH] wifi_prov: Exposed event for transport pairing Closes https://github.com/espressif/esp-idf/issues/10007 --- .../include/transports/protocomm_ble.h | 14 +++++++ .../protocomm/src/transports/protocomm_ble.c | 11 +++++ .../src/transports/protocomm_nimble.c | 18 +++++++- .../wifi_prov_mgr/main/app_main.c | 41 ++++++++++++++++--- 4 files changed, 77 insertions(+), 7 deletions(-) diff --git a/components/protocomm/include/transports/protocomm_ble.h b/components/protocomm/include/transports/protocomm_ble.h index 0efb31c213..700dc2e3e3 100644 --- a/components/protocomm/include/transports/protocomm_ble.h +++ b/components/protocomm/include/transports/protocomm_ble.h @@ -7,11 +7,25 @@ #pragma once #include +#include "esp_event.h" #ifdef __cplusplus extern "C" { #endif +ESP_EVENT_DECLARE_BASE(PROTOCOMM_TRANSPORT_BLE_EVENT); + +/** + * @brief Events generated by BLE transport + * + * These events are generated when the BLE transport is paired + * and disconnected. + */ +typedef enum { + PROTOCOMM_TRANSPORT_BLE_CONNECTED, + PROTOCOMM_TRANSPORT_BLE_DISCONNECTED, +} protocomm_transport_ble_event_t; + /** * BLE device name cannot be larger than this value * 31 bytes (max scan response size) - 1 byte (length) - 1 byte (type) = 29 bytes diff --git a/components/protocomm/src/transports/protocomm_ble.c b/components/protocomm/src/transports/protocomm_ble.c index e419181a0e..2c903ffa83 100644 --- a/components/protocomm/src/transports/protocomm_ble.c +++ b/components/protocomm/src/transports/protocomm_ble.c @@ -15,6 +15,8 @@ #include "protocomm_priv.h" #include "simple_ble.h" +ESP_EVENT_DEFINE_BASE(PROTOCOMM_TRANSPORT_BLE_EVENT); + /* NOTE: For the security2 scheme, the payload size is quite larger * than that for security1. The increased value has been selected * keeping in mind the largest packet size for security2 and the @@ -25,6 +27,7 @@ #else #define CHAR_VAL_LEN_MAX (256 + 1) #endif // CONFIG_ESP_PROTOCOMM_SUPPORT_SECURITY_VERSION_2 + #define PREPARE_BUF_MAX_SIZE CHAR_VAL_LEN_MAX static const char *TAG = "protocomm_ble"; @@ -342,6 +345,10 @@ static void transport_simple_ble_disconnect(esp_gatts_cb_event_t event, esp_gatt param->disconnect.conn_id); if (ret != ESP_OK) { ESP_LOGE(TAG, "error closing the session after disconnect"); + } else { + if (esp_event_post(PROTOCOMM_TRANSPORT_BLE_EVENT, PROTOCOMM_TRANSPORT_BLE_DISCONNECTED, NULL, 0, portMAX_DELAY) != ESP_OK) { + ESP_LOGE(TAG, "Failed to post transport disconnection event"); + } } } protoble_internal->gatt_mtu = ESP_GATT_DEF_BLE_MTU_SIZE; @@ -357,6 +364,10 @@ static void transport_simple_ble_connect(esp_gatts_cb_event_t event, esp_gatt_if param->connect.conn_id); if (ret != ESP_OK) { ESP_LOGE(TAG, "error creating the session"); + } else { + if (esp_event_post(PROTOCOMM_TRANSPORT_BLE_EVENT, PROTOCOMM_TRANSPORT_BLE_CONNECTED, NULL, 0, portMAX_DELAY) != ESP_OK) { + ESP_LOGE(TAG, "Failed to post transport pairing event"); + } } } } diff --git a/components/protocomm/src/transports/protocomm_nimble.c b/components/protocomm/src/transports/protocomm_nimble.c index b24740d778..aa325730e2 100644 --- a/components/protocomm/src/transports/protocomm_nimble.c +++ b/components/protocomm/src/transports/protocomm_nimble.c @@ -22,6 +22,8 @@ static const char *TAG = "protocomm_nimble"; +ESP_EVENT_DEFINE_BASE(PROTOCOMM_TRANSPORT_BLE_EVENT); + int ble_uuid_flat(const ble_uuid_t *, void *); static uint8_t ble_uuid_base[BLE_UUID128_VAL_LENGTH]; static int num_chr_dsc; @@ -226,7 +228,7 @@ simple_ble_gap_event(struct ble_gap_event *event, void *arg) ESP_LOGE(TAG, "No open connection with the specified handle"); return rc; } - s_cached_conn_handle = event->connect.conn_handle; + s_cached_conn_handle = event->connect.conn_handle; } else { /* Connection failed; resume advertising. */ simple_ble_advertise(); @@ -236,7 +238,11 @@ simple_ble_gap_event(struct ble_gap_event *event, void *arg) case BLE_GAP_EVENT_DISCONNECT: ESP_LOGD(TAG, "disconnect; reason=%d ", event->disconnect.reason); transport_simple_ble_disconnect(event, arg); - s_cached_conn_handle = 0; /* Clear conn_handle value */ + /* Clear conn_handle value */ + s_cached_conn_handle = 0; + if (esp_event_post(PROTOCOMM_TRANSPORT_BLE_EVENT, PROTOCOMM_TRANSPORT_BLE_DISCONNECTED, NULL, 0, portMAX_DELAY) != ESP_OK) { + ESP_LOGE(TAG, "Failed to post pairing event"); + } /* Connection terminated; resume advertising. */ simple_ble_advertise(); return 0; @@ -552,6 +558,10 @@ static void transport_simple_ble_disconnect(struct ble_gap_event *event, void *a protoble_internal->pc_ble->sec->close_transport_session(protoble_internal->pc_ble->sec_inst, event->disconnect.conn.conn_handle); if (ret != ESP_OK) { ESP_LOGE(TAG, "error closing the session after disconnect"); + } else { + if (esp_event_post(PROTOCOMM_TRANSPORT_BLE_EVENT, PROTOCOMM_TRANSPORT_BLE_DISCONNECTED, NULL, 0, portMAX_DELAY) != ESP_OK) { + ESP_LOGE(TAG, "Failed to post transport disconnection event"); + } } } protoble_internal->gatt_mtu = BLE_ATT_MTU_DFLT; @@ -567,6 +577,10 @@ static void transport_simple_ble_connect(struct ble_gap_event *event, void *arg) protoble_internal->pc_ble->sec->new_transport_session(protoble_internal->pc_ble->sec_inst, event->connect.conn_handle); if (ret != ESP_OK) { ESP_LOGE(TAG, "error creating the session"); + } else { + if (esp_event_post(PROTOCOMM_TRANSPORT_BLE_EVENT, PROTOCOMM_TRANSPORT_BLE_CONNECTED, NULL, 0, portMAX_DELAY) != ESP_OK) { + ESP_LOGE(TAG, "Failed to post transport pairing event"); + } } } } diff --git a/examples/provisioning/wifi_prov_mgr/main/app_main.c b/examples/provisioning/wifi_prov_mgr/main/app_main.c index 8e1055efd8..985acc1216 100644 --- a/examples/provisioning/wifi_prov_mgr/main/app_main.c +++ b/examples/provisioning/wifi_prov_mgr/main/app_main.c @@ -156,16 +156,44 @@ static void event_handler(void* arg, esp_event_base_t event_base, default: break; } - } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) { - esp_wifi_connect(); + } else if (event_base == WIFI_EVENT) { + switch (event_id) { + case WIFI_EVENT_STA_START: + esp_wifi_connect(); + break; + case WIFI_EVENT_STA_DISCONNECTED: + ESP_LOGI(TAG, "Disconnected. Connecting to the AP again..."); + esp_wifi_connect(); + break; +#ifdef CONFIG_EXAMPLE_PROV_TRANSPORT_SOFTAP + case WIFI_EVENT_AP_STACONNECTED: + ESP_LOGI(TAG, "SoftAP transport: Connected!"); + break; + case WIFI_EVENT_AP_STADISCONNECTED: + ESP_LOGI(TAG, "SoftAP transport: Disconnected!"); + break; +#endif + default: + break; + } } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) { ip_event_got_ip_t* event = (ip_event_got_ip_t*) event_data; ESP_LOGI(TAG, "Connected with IP Address:" IPSTR, IP2STR(&event->ip_info.ip)); /* Signal main application to continue execution */ xEventGroupSetBits(wifi_event_group, WIFI_CONNECTED_EVENT); - } else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) { - ESP_LOGI(TAG, "Disconnected. Connecting to the AP again..."); - esp_wifi_connect(); +#ifdef CONFIG_EXAMPLE_PROV_TRANSPORT_BLE + } else if (event_base == PROTOCOMM_TRANSPORT_BLE_EVENT) { + switch (event_id) { + case PROTOCOMM_TRANSPORT_BLE_CONNECTED: + ESP_LOGI(TAG, "BLE transport: Connected!"); + break; + case PROTOCOMM_TRANSPORT_BLE_DISCONNECTED: + ESP_LOGI(TAG, "BLE transport: Disconnected!"); + break; + default: + break; + } +#endif } } @@ -258,6 +286,9 @@ void app_main(void) /* Register our event handler for Wi-Fi, IP and Provisioning related events */ ESP_ERROR_CHECK(esp_event_handler_register(WIFI_PROV_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); +#ifdef CONFIG_EXAMPLE_PROV_TRANSPORT_BLE + ESP_ERROR_CHECK(esp_event_handler_register(PROTOCOMM_TRANSPORT_BLE_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); +#endif ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL)); ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL));