diff --git a/components/protocomm/include/transports/protocomm_ble.h b/components/protocomm/include/transports/protocomm_ble.h index 361028790b..a6758a4cf3 100644 --- a/components/protocomm/include/transports/protocomm_ble.h +++ b/components/protocomm/include/transports/protocomm_ble.h @@ -142,6 +142,11 @@ typedef struct protocomm_ble_config { * BLE address */ uint8_t *ble_addr; + + /** + * 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 6fedd85abb..742f453d9c 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 8aa2e4ec6d..5d83f56359 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; @@ -133,6 +134,8 @@ typedef struct { unsigned ble_link_encryption:1; /** BLE address */ uint8_t *ble_addr; + /** BLE Characteristic notify flag */ + unsigned ble_notify:1; } simple_ble_cfg_t; static simple_ble_cfg_t *ble_cfg_p; @@ -265,6 +268,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; } @@ -715,6 +726,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 @@ -972,6 +987,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 e070a1324c..faaac06a03 100644 --- a/components/wifi_provisioning/src/scheme_ble.c +++ b/components/wifi_provisioning/src/scheme_ble.c @@ -51,6 +51,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");