From fb5564627013190cad0c120b5627821e85d3a838 Mon Sep 17 00:00:00 2001 From: Rahul Tank Date: Wed, 24 Jul 2024 09:22:49 +0530 Subject: [PATCH] fix(nimble): Add notify characteristic flag support --- .../protocomm/include/transports/protocomm_ble.h | 6 +++++- .../protocomm/src/transports/protocomm_ble.c | 11 ++++++++++- .../protocomm/src/transports/protocomm_nimble.c | 16 ++++++++++++++++ components/wifi_provisioning/Kconfig | 8 ++++++++ components/wifi_provisioning/src/scheme_ble.c | 5 +++++ 5 files changed, 44 insertions(+), 2 deletions(-) diff --git a/components/protocomm/include/transports/protocomm_ble.h b/components/protocomm/include/transports/protocomm_ble.h index 04af25f9fa..22cd84785e 100644 --- a/components/protocomm/include/transports/protocomm_ble.h +++ b/components/protocomm/include/transports/protocomm_ble.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2018-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -148,6 +148,10 @@ typedef struct protocomm_ble_config { */ unsigned keep_ble_on:1; + /** + * BLE characteristic notify flag + */ + unsigned ble_notify:1; } protocomm_ble_config_t; /** diff --git a/components/protocomm/src/transports/protocomm_ble.c b/components/protocomm/src/transports/protocomm_ble.c index a780319dde..740775acec 100644 --- a/components/protocomm/src/transports/protocomm_ble.c +++ b/components/protocomm/src/transports/protocomm_ble.c @@ -37,6 +37,8 @@ static const uint16_t primary_service_uuid = ESP_GATT_UUID_PRI_SERVICE; static const uint16_t character_declaration_uuid = ESP_GATT_UUID_CHAR_DECLARE; static const uint16_t character_user_description = ESP_GATT_UUID_CHAR_DESCRIPTION; static const uint8_t character_prop_read_write = ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_WRITE; +static const uint8_t character_prop_read_write_notify = ESP_GATT_CHAR_PROP_BIT_READ | ESP_GATT_CHAR_PROP_BIT_WRITE | \ + ESP_GATT_CHAR_PROP_BIT_NOTIFY; typedef struct { uint8_t type; @@ -64,6 +66,7 @@ typedef struct _protocomm_ble { uint16_t gatt_mtu; uint8_t *service_uuid; unsigned ble_link_encryption:1; + unsigned ble_notify:1; } _protocomm_ble_internal_t; static _protocomm_ble_internal_t *protoble_internal; @@ -450,7 +453,12 @@ static ssize_t populate_gatt_db(esp_gatts_attr_db_t **gatt_db_generated) (*gatt_db_generated)[i].att_desc.uuid_p = (uint8_t *) &character_declaration_uuid; (*gatt_db_generated)[i].att_desc.max_length = sizeof(uint8_t); (*gatt_db_generated)[i].att_desc.length = sizeof(uint8_t); - (*gatt_db_generated)[i].att_desc.value = (uint8_t *) &character_prop_read_write; + + if (protoble_internal->ble_notify) { + (*gatt_db_generated)[i].att_desc.value = (uint8_t *) &character_prop_read_write_notify; + } else { + (*gatt_db_generated)[i].att_desc.value = (uint8_t *) &character_prop_read_write; + } } else if (i % 3 == 2) { /* Characteristic Value */ (*gatt_db_generated)[i].att_desc.perm = ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE ; @@ -562,6 +570,7 @@ esp_err_t protocomm_ble_start(protocomm_t *pc, const protocomm_ble_config_t *con protoble_internal->pc_ble = pc; protoble_internal->gatt_mtu = ESP_GATT_DEF_BLE_MTU_SIZE; protoble_internal->ble_link_encryption = config->ble_link_encryption; + protoble_internal->ble_notify = config->ble_notify; // Config adv data adv_config.service_uuid_len = ESP_UUID_LEN_128; diff --git a/components/protocomm/src/transports/protocomm_nimble.c b/components/protocomm/src/transports/protocomm_nimble.c index c95b518622..51f5c63327 100644 --- a/components/protocomm/src/transports/protocomm_nimble.c +++ b/components/protocomm/src/transports/protocomm_nimble.c @@ -69,6 +69,7 @@ typedef struct _protocomm_ble { ssize_t g_nu_lookup_count; uint16_t gatt_mtu; unsigned ble_link_encryption:1; + unsigned ble_notify:1; } _protocomm_ble_internal_t; static _protocomm_ble_internal_t *protoble_internal; @@ -135,6 +136,8 @@ typedef struct { uint8_t *ble_addr; /** Flag to keep BLE on */ unsigned keep_ble_on:1; + /** BLE Characteristic notify flag */ + unsigned ble_notify:1; } simple_ble_cfg_t; static simple_ble_cfg_t *ble_cfg_p; @@ -267,6 +270,14 @@ simple_ble_gap_event(struct ble_gap_event *event, void *arg) event->mtu.value); transport_simple_ble_set_mtu(event, arg); return 0; + case BLE_GAP_EVENT_NOTIFY_TX: + ESP_LOGI(TAG, "notify_tx event; conn_handle=%d attr_handle=%d " + "status=%d is_indication=%d", + event->notify_tx.conn_handle, + event->notify_tx.attr_handle, + event->notify_tx.status, + event->notify_tx.indication); + return 0; } return 0; } @@ -717,6 +728,10 @@ ble_gatt_add_characteristics(struct ble_gatt_chr_def *characteristics, int idx) BLE_GATT_CHR_F_WRITE_ENC; } + if (protoble_internal->ble_notify) { + (characteristics + idx)->flags |= BLE_GATT_CHR_F_NOTIFY; + } + (characteristics + idx)->access_cb = gatt_svr_chr_access; /* Out of 128 bit UUID, 16 bits from g_nu_lookup table. Currently @@ -974,6 +989,7 @@ esp_err_t protocomm_ble_start(protocomm_t *pc, const protocomm_ble_config_t *con protoble_internal->pc_ble = pc; protoble_internal->gatt_mtu = BLE_ATT_MTU_DFLT; protoble_internal->ble_link_encryption = config->ble_link_encryption; + protoble_internal->ble_notify = config->ble_notify; simple_ble_cfg_t *ble_config = (simple_ble_cfg_t *) calloc(1, sizeof(simple_ble_cfg_t)); if (ble_config == NULL) { diff --git a/components/wifi_provisioning/Kconfig b/components/wifi_provisioning/Kconfig index 9403d0ee23..d5a7d7aa36 100644 --- a/components/wifi_provisioning/Kconfig +++ b/components/wifi_provisioning/Kconfig @@ -33,9 +33,17 @@ menu "Wi-Fi Provisioning Manager" config WIFI_PROV_BLE_FORCE_ENCRYPTION bool prompt "Force Link Encryption during characteristic Read / Write" + depends on BT_ENABLED help Used to enforce link encryption when attempting to read / write characteristic + config WIFI_PROV_BLE_NOTIFY + bool + prompt "Add support for Notification for provisioning BLE descriptors" + depends on BT_ENABLED + help + Used to enable support Notification in BLE descriptors of prov* characteristics + config WIFI_PROV_KEEP_BLE_ON_AFTER_PROV bool "Keep BT on after provisioning is done" depends on BT_ENABLED diff --git a/components/wifi_provisioning/src/scheme_ble.c b/components/wifi_provisioning/src/scheme_ble.c index 22782762a6..0ab935c018 100644 --- a/components/wifi_provisioning/src/scheme_ble.c +++ b/components/wifi_provisioning/src/scheme_ble.c @@ -65,6 +65,11 @@ static esp_err_t prov_start(protocomm_t *pc, void *config) #if defined(CONFIG_WIFI_PROV_BLE_FORCE_ENCRYPTION) ble_config->ble_link_encryption = 1; #endif + +#if defined(CONFIG_WIFI_PROV_BLE_NOTIFY) + ble_config->ble_notify = 1; +#endif + /* Start protocomm as BLE service */ if (protocomm_ble_start(pc, ble_config) != ESP_OK) { ESP_LOGE(TAG, "Failed to start protocomm BLE service");