Skip to content

Commit

Permalink
feat(ble): Support blufi command
Browse files Browse the repository at this point in the history
  • Loading branch information
xiewenxiang authored and ustccw committed Nov 29, 2024
1 parent 5a79d6b commit 016888b
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 1 deletion.
147 changes: 147 additions & 0 deletions module_config/module_esp32c5_default/patch/blufi-adv.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
From f59ad54c5e2de90423fb8c2db344e829d05f6db4 Mon Sep 17 00:00:00 2001
From: xiewenxiang <[email protected]>
Date: Thu, 14 Nov 2024 16:34:27 +0800
Subject: [PATCH] feat(blufi): Support ext adv

---
.../profile/esp/blufi/nimble_host/esp_blufi.c | 98 ++++++++++++++++++-
1 file changed, 97 insertions(+), 1 deletion(-)

diff --git a/components/bt/common/btc/profile/esp/blufi/nimble_host/esp_blufi.c b/components/bt/common/btc/profile/esp/blufi/nimble_host/esp_blufi.c
index bd24eb8ba9..0515c25adc 100644
--- a/components/bt/common/btc/profile/esp/blufi/nimble_host/esp_blufi.c
+++ b/components/bt/common/btc/profile/esp/blufi/nimble_host/esp_blufi.c
@@ -32,11 +32,22 @@

#if (BLUFI_INCLUDED == TRUE)

+#if !CONFIG_BT_NIMBLE_EXT_ADV
static uint8_t own_addr_type;
+#endif

struct gatt_value gatt_values[SERVER_MAX_VALUES];
const static char *TAG = "BLUFI_EXAMPLE";

+#if CONFIG_BT_NIMBLE_EXT_ADV
+static uint8_t ext_adv_pattern_1[] = {
+ 0x02, 0x01, 0x06,
+ 0x03, 0x03, 0xFF, 0xFF,
+ 0x0d, 0X09, 'B', 'L', 'U', 'F','I', '_' ,'D','E','V','I','C','E',
+ 0x02, 0x0A, 0x09
+};
+#endif
+
enum {
GATT_VALUE_TYPE_CHR,
GATT_VALUE_TYPE_DSC,
@@ -357,6 +368,79 @@ esp_blufi_gap_event(struct ble_gap_event *event, void *arg)

void esp_blufi_adv_start(void)
{
+#if CONFIG_BT_NIMBLE_EXT_ADV //Extended Adv
+ struct ble_gap_ext_adv_params params;
+ struct os_mbuf *data;
+ uint8_t instance = 0;
+ int rc;
+ const char *name;
+ uint8_t adv_data[31] = {0};
+
+ /* use defaults for non-set params */
+ memset (&params, 0, sizeof(params));
+
+ /* enable connectable advertising */
+ params.connectable = 1;
+ params.scannable = 1;
+ params.legacy_pdu = 1;
+
+ /* advertise using public addr */
+ params.own_addr_type = BLE_OWN_ADDR_PUBLIC;
+
+ params.primary_phy = BLE_HCI_LE_PHY_1M;
+ params.secondary_phy = BLE_HCI_LE_PHY_1M;
+ params.sid = 1;
+
+ params.itvl_min = BLE_GAP_ADV_FAST_INTERVAL1_MIN;
+ params.itvl_max = BLE_GAP_ADV_FAST_INTERVAL1_MIN;
+
+ /* configure instance 0 */
+ rc = ble_gap_ext_adv_configure(instance, &params, NULL,
+ esp_blufi_gap_event, NULL);
+ if (rc != 0) {
+ ESP_LOGE(TAG, "Configuration failed with reason : %d \n" , rc);
+ return;
+ }
+
+ name = ble_svc_gap_device_name();
+ adv_data[0] = 0x02;
+ adv_data[1] = 0x01;
+ adv_data[2] = 0x06;
+ adv_data[3] = 1 + strlen(name);
+ adv_data[4] = 0x09;
+ memcpy(adv_data + 5, name, strlen(name));
+
+ /* get mbuf for scan rsp data */
+ data = os_msys_get_pkthdr(strlen(name) + 5, 0);
+
+ if (data == NULL) {
+ ESP_LOGE(TAG, "Failed to get mbuf \n");
+ return;
+ }
+
+ /* fill mbuf with scan rsp data */
+ rc = os_mbuf_append(data, adv_data, strlen(name) + 5);
+
+ if (rc != 0) {
+ ESP_LOGE(TAG, "Failed to fill scan rsp data with reason: %d \n", rc);
+ return;
+ }
+
+ rc = ble_gap_ext_adv_set_data(instance, data);
+
+ if (rc != 0) {
+ ESP_LOGE(TAG, "Failed to set adv data with reason: %d \n", rc);
+ return;
+ }
+
+ /* start advertising */
+ rc = ble_gap_ext_adv_start(instance, 0, 0);
+
+ if (rc != 0) {
+ ESP_LOGE(TAG, "Failed to start ext adv with reason: %d \n", rc);
+ return;
+ }
+#else // Legacy ADV
int rc;

rc = ble_hs_util_ensure_addr(0);
@@ -428,6 +512,7 @@ void esp_blufi_adv_start(void)
ESP_LOGE(TAG, "error enabling advertisement; rc=%d", rc);
return;
}
+#endif
}

uint8_t esp_blufi_init(void)
@@ -473,7 +558,18 @@ void esp_blufi_disconnect(void)
ble_gap_terminate(blufi_env.conn_id, BLE_ERR_REM_USER_CONN_TERM);
}

-void esp_blufi_adv_stop(void) {}
+void esp_blufi_adv_stop(void)
+{
+#if CONFIG_BT_NIMBLE_EXT_ADV
+ int i;
+
+ for (i = 0; i < BLE_ADV_INSTANCES; i++) {
+ ble_gap_ext_adv_stop(i);
+ }
+#else
+ ble_gap_adv_stop();
+#endif
+}

void esp_blufi_send_encap(void *arg)
{
--
2.25.1

4 changes: 4 additions & 0 deletions module_config/module_esp32c5_default/patch/patch_list.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
[support_ext_partition.patch]
path = esp-idf
note = "[IDF-9560] Support to use partition table outside of the project directory"

[blufi-adv.patch]
path = esp-idf
note = "Add the support for BluFi advertising if uses nimble"
2 changes: 1 addition & 1 deletion module_config/module_esp32c5_default/sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ CONFIG_AT_OTA_SSL_TOKEN_KEY="dd93253c287f725de50d4071a05dd28b72056ca7"
CONFIG_AT_SOCKET_MAX_CONN_NUM=5
CONFIG_ESP_AT_FW_VERSION="v5.0.0.0-dev"
CONFIG_AT_BLE_HID_COMMAND_SUPPORT=n
CONFIG_AT_BLUFI_COMMAND_SUPPORT=n
CONFIG_AT_BLUFI_COMMAND_SUPPORT=y

# Wear Levelling
CONFIG_WL_SECTOR_SIZE_512=y
Expand Down

0 comments on commit 016888b

Please sign in to comment.