Skip to content

Commit

Permalink
Merge branch 'bugfix/add_notify_flag_for_wifi_prov' into 'master'
Browse files Browse the repository at this point in the history
fix(wifi_prov): Add notify characteristic flag support

See merge request espressif/esp-idf!32311
  • Loading branch information
rahult-github committed Sep 6, 2024
2 parents b341791 + fb55646 commit 0f11052
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
6 changes: 5 additions & 1 deletion components/protocomm/include/transports/protocomm_ble.h
Original file line number Diff line number Diff line change
@@ -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
*/
Expand Down Expand Up @@ -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;

/**
Expand Down
11 changes: 10 additions & 1 deletion components/protocomm/src/transports/protocomm_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 ;
Expand Down Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions components/protocomm/src/transports/protocomm_nimble.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions components/wifi_provisioning/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions components/wifi_provisioning/src/scheme_ble.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit 0f11052

Please sign in to comment.