From fe5d4e0f395ffbc79353adc6a4a1c00e9fab0d23 Mon Sep 17 00:00:00 2001 From: Rahul Tank Date: Wed, 14 Sep 2022 14:34:32 +0530 Subject: [PATCH] Nimble: Add LE Power CTRL changes --- components/bt/host/nimble/Kconfig.in | 11 +++ components/bt/host/nimble/nimble | 2 +- .../host/nimble/port/include/esp_nimble_cfg.h | 4 ++ .../nimble/include/nimble/hci_common.h | 69 +++++++++++++++++-- .../nimble/blecent/sdkconfig.defaults.esp32c2 | 2 +- examples/bluetooth/nimble/bleprph/main/main.c | 46 +++++++++++++ 6 files changed, 128 insertions(+), 6 deletions(-) diff --git a/components/bt/host/nimble/Kconfig.in b/components/bt/host/nimble/Kconfig.in index 6e149a1b6f..ceeb05b560 100644 --- a/components/bt/host/nimble/Kconfig.in +++ b/components/bt/host/nimble/Kconfig.in @@ -617,6 +617,17 @@ choice BT_NIMBLE_COEX_PHY_CODED_TX_RX_TLIM Disable the limitation on max tx/rx time for Coded-PHY connection endchoice +menuconfig BT_NIMBLE_52_FEATURE_SUPPORT + bool "Enable BLE 5.2 Feature" + help + Enable this option to select 5.2 features + +config BT_NIMBLE_BLE_POWER_CONTROL + bool "Enable support for BLE Power Control" + depends on BT_NIMBLE_52_FEATURE_SUPPORT && SOC_ESP_NIMBLE_CONTROLLER + help + Set this option to enable the Power Control feature + config BT_NIMBLE_COEX_PHY_CODED_TX_RX_TLIM_EFF int default 0 if !(ESP32_WIFI_SW_COEXIST_ENABLE && BT_NIMBLE_ENABLED) diff --git a/components/bt/host/nimble/nimble b/components/bt/host/nimble/nimble index a13c79b4fe..d7aa88a1f9 160000 --- a/components/bt/host/nimble/nimble +++ b/components/bt/host/nimble/nimble @@ -1 +1 @@ -Subproject commit a13c79b4febea7a6a0ec1e245f4d017c4cdf5c84 +Subproject commit d7aa88a1f97ec584585cd81cb4745d5c909057c3 diff --git a/components/bt/host/nimble/port/include/esp_nimble_cfg.h b/components/bt/host/nimble/port/include/esp_nimble_cfg.h index 842a234740..8ff2c26c26 100644 --- a/components/bt/host/nimble/port/include/esp_nimble_cfg.h +++ b/components/bt/host/nimble/port/include/esp_nimble_cfg.h @@ -1595,4 +1595,8 @@ #endif #endif +#ifndef MYNEWT_VAL_BLE_POWER_CONTROL +#define MYNEWT_VAL_BLE_POWER_CONTROL CONFIG_BT_NIMBLE_BLE_POWER_CONTROL +#endif + #endif diff --git a/components/bt/porting/nimble/include/nimble/hci_common.h b/components/bt/porting/nimble/include/nimble/hci_common.h index a77b2018eb..a98acf14d0 100644 --- a/components/bt/porting/nimble/include/nimble/hci_common.h +++ b/components/bt/porting/nimble/include/nimble/hci_common.h @@ -1068,7 +1068,49 @@ struct ble_hci_le_set_host_feat_cp { uint8_t val; } __attribute__((packed)); -#define BLE_HCI_OCF_LE_SET_DEFAULT_SUBRATE (0x007D) +#define BLE_HCI_OCF_LE_ENH_READ_TRANSMIT_POWER_LEVEL (0x0076) +struct ble_hci_le_enh_read_transmit_power_level_cp { + uint16_t conn_handle; + uint8_t phy; +} __attribute__((packed)); +struct ble_hci_le_enh_read_transmit_power_level_rp { + uint8_t status; + uint16_t conn_handle; + uint8_t phy; + uint8_t curr_tx_power_level; + uint8_t max_tx_power_level; +} __attribute__((packed)); + +#define BLE_HCI_OCF_LE_READ_REMOTE_TRANSMIT_POWER_LEVEL (0x0077) +struct ble_hci_le_read_remote_transmit_power_level_cp { + uint16_t conn_handle; + uint8_t phy; +} __attribute__((packed)); + +#define BLE_HCI_OCF_LE_SET_PATH_LOSS_REPORT_PARAM (0x0078) +struct ble_hci_le_set_path_loss_report_param_cp { + uint16_t conn_handle; + uint8_t high_threshold; + uint8_t high_hysteresis; + uint8_t low_threshold; + uint8_t low_hysteresis; + uint16_t min_time_spent; +} __attribute__((packed)); + +#define BLE_HCI_OCF_LE_SET_PATH_LOSS_REPORT_ENABLE (0x0079) +struct ble_hci_le_set_path_loss_report_enable_cp { + uint16_t conn_handle; + uint8_t enable; +} __attribute__((packed)); + +#define BLE_HCI_OCF_LE_SET_TRANS_PWR_REPORT_ENABLE (0x007A) +struct ble_hci_le_set_transmit_power_report_enable_cp { + uint16_t conn_handle; + uint8_t local_enable; + uint8_t remote_enable; +} __attribute__((packed)); + +#define BLE_HCI_OCF_LE_SET_DEFAULT_SUBRATE (0x007D) struct ble_hci_le_set_default_subrate_cp { uint16_t subrate_min; uint16_t subrate_max; @@ -1077,7 +1119,7 @@ struct ble_hci_le_set_default_subrate_cp { uint16_t supervision_tmo; } __attribute__((packed)); -#define BLE_HCI_OCF_LE_SUBRATE_REQ (0x007E) +#define BLE_HCI_OCF_LE_SUBRATE_REQ (0x007E) struct ble_hci_le_subrate_req_cp { uint16_t conn_handle; uint16_t subrate_min; @@ -1798,6 +1840,26 @@ struct ble_hci_ev_le_subev_peer_sca_complete { uint8_t sca; } __attribute__((packed)); +#define BLE_HCI_LE_SUBEV_PATH_LOSS_THRESHOLD (0x20) +struct ble_hci_ev_le_subev_path_loss_threshold { + uint8_t subev_code; + uint16_t conn_handle; + uint8_t current_path_loss; + uint8_t zone_entered; +} __attribute__((packed)); + +#define BLE_HCI_LE_SUBEV_TRANSMIT_POWER_REPORT (0x21) +struct ble_hci_ev_le_subev_transmit_power_report { + uint8_t subev_code; + uint8_t status; + uint16_t conn_handle; + uint8_t reason; + uint8_t phy; + uint8_t transmit_power_level; + uint8_t transmit_power_level_flag; + uint8_t delta; +} __attribute__((packed)); + #define BLE_HCI_LE_SUBEV_BIGINFO_ADV_REPORT (0x22) struct ble_hci_ev_le_subev_biginfo_adv_report { uint8_t subev_code; @@ -1816,7 +1878,7 @@ struct ble_hci_ev_le_subev_biginfo_adv_report { uint8_t encryption; } __attribute__((packed)); -#define BLE_HCI_LE_SUBEV_SUBRATE_CHANGE (0x23) +#define BLE_HCI_LE_SUBEV_SUBRATE_CHANGE (0x23) struct ble_hci_ev_le_subev_subrate_change { uint8_t subev_code; uint8_t status; @@ -1827,7 +1889,6 @@ struct ble_hci_ev_le_subev_subrate_change { uint16_t supervision_tmo; } __attribute__((packed)); - /* Data buffer overflow event */ #define BLE_HCI_EVENT_ACL_BUF_OVERFLOW (0x01) diff --git a/examples/bluetooth/nimble/blecent/sdkconfig.defaults.esp32c2 b/examples/bluetooth/nimble/blecent/sdkconfig.defaults.esp32c2 index 587d67f490..a6d6545a23 100644 --- a/examples/bluetooth/nimble/blecent/sdkconfig.defaults.esp32c2 +++ b/examples/bluetooth/nimble/blecent/sdkconfig.defaults.esp32c2 @@ -2,7 +2,7 @@ # Espressif IoT Development Framework (ESP-IDF) Project Minimal Configuration # CONFIG_IDF_TARGET="esp32c2" -# CONFIG_EXAMPLE_EXTENDED_ADV is not set +CONFIG_BT_NIMBLE_EXT_ADV=y CONFIG_BT_ENABLED=y CONFIG_BT_NIMBLE_ENABLED=y # CONFIG_BT_NIMBLE_50_FEATURE_SUPPORT is not set diff --git a/examples/bluetooth/nimble/bleprph/main/main.c b/examples/bluetooth/nimble/bleprph/main/main.c index 5580e0e3e6..8aec386e38 100644 --- a/examples/bluetooth/nimble/bleprph/main/main.c +++ b/examples/bluetooth/nimble/bleprph/main/main.c @@ -47,6 +47,10 @@ static uint8_t own_addr_type; void ble_store_config_init(void); +#if MYNEWT_VAL(BLE_POWER_CONTROL) +static struct ble_gap_event_listener power_control_event_listener; +#endif + /** * Logs information about a connection to the console. */ @@ -197,6 +201,19 @@ bleprph_advertise(void) } #endif +#if MYNEWT_VAL(BLE_POWER_CONTROL) +static void bleprph_power_control(uint16_t conn_handle) +{ + int rc; + + rc = ble_gap_read_remote_transmit_power_level(conn_handle, 0x01 ); // Attempting on LE 1M phy + assert (rc == 0); + + rc = ble_gap_set_transmit_power_reporting_enable(conn_handle, 0x1, 0x1); + assert (rc == 0); +} +#endif + /** * The nimble host executes this callback when a GAP event occurs. The * application associates a GAP event callback with each connection that forms. @@ -239,6 +256,13 @@ bleprph_gap_event(struct ble_gap_event *event, void *arg) bleprph_advertise(); #endif } + +#if MYNEWT_VAL(BLE_POWER_CONTROL) + bleprph_power_control(event->connect.conn_handle); + + ble_gap_event_listener_register(&power_control_event_listener, + bleprph_gap_event, NULL); +#endif return 0; case BLE_GAP_EVENT_DISCONNECT: @@ -361,6 +385,28 @@ bleprph_gap_event(struct ble_gap_event *event, void *arg) ESP_LOGI(tag, "ble_sm_inject_io result: %d\n", rc); } return 0; + +#if MYNEWT_VAL(BLE_POWER_CONTROL) + case BLE_GAP_EVENT_TRANSMIT_POWER: + MODLOG_DFLT(INFO, "Transmit power event : status=%d conn_handle=%d reason=%d " + "phy=%d power_level=%x power_level_flag=%d delta=%d", + event->transmit_power.status, + event->transmit_power.conn_handle, + event->transmit_power.reason, + event->transmit_power.phy, + event->transmit_power.transmit_power_level, + event->transmit_power.transmit_power_level_flag, + event->transmit_power.delta); + return 0; + + case BLE_GAP_EVENT_PATHLOSS_THRESHOLD: + MODLOG_DFLT(INFO, "Pathloss threshold event : conn_handle=%d current path loss=%d " + "zone_entered =%d", + event->pathloss_threshold.conn_handle, + event->pathloss_threshold.current_path_loss, + event->pathloss_threshold.zone_entered); + return 0; +#endif } return 0;