From 502d47fa4406f101b12919aebdfdace3b964d416 Mon Sep 17 00:00:00 2001 From: zwl Date: Mon, 8 Aug 2022 21:57:22 +0800 Subject: [PATCH] add set/get txpwr api and fix issues such as disconnect and abnormal tx behavior for esp32c2 --- components/bt/controller/esp32c2/bt.c | 124 +++++++++++++++++- .../bt/controller/lib_esp32c2/esp32c2-bt-lib | 2 +- .../bt/include/esp32c2/include/esp_bt.h | 59 ++++++--- .../bt/porting/nimble/include/nimble/ble.h | 3 +- components/esp_rom/esp32c2/ld/esp32c2.rom.ld | 17 --- 5 files changed, 165 insertions(+), 40 deletions(-) diff --git a/components/bt/controller/esp32c2/bt.c b/components/bt/controller/esp32c2/bt.c index dbad0cc881..65cfc8b2ac 100644 --- a/components/bt/controller/esp32c2/bt.c +++ b/components/bt/controller/esp32c2/bt.c @@ -61,6 +61,12 @@ #define EXT_FUNC_VERSION 0x20220125 #define EXT_FUNC_MAGIC_VALUE 0xA5A5A5A5 + +#ifdef CONFIG_BT_BLUEDROID_ENABLED +/* ACL_DATA_MBUF_LEADINGSPCAE: The leadingspace in user info header for ACL data */ +#define ACL_DATA_MBUF_LEADINGSPCAE 4 +#endif + /* Types definition ************************************************************************ */ @@ -125,6 +131,8 @@ extern int ble_sm_alg_gen_dhkey(const uint8_t *peer_pub_key_x, const uint8_t *peer_pub_key_y, const uint8_t *our_priv_key, uint8_t *out_dhkey); extern int ble_sm_alg_gen_key_pair(uint8_t *pub, uint8_t *priv); +extern int ble_txpwr_set(esp_ble_enhanced_power_type_t power_type, uint16_t handle, int power_level); +extern int ble_txpwr_get(esp_ble_enhanced_power_type_t power_type, uint16_t handle); /* Local Function Declaration ********************************************************************* @@ -175,6 +183,7 @@ static void btdm_slp_tmr_callback(void *arg); static DRAM_ATTR esp_timer_handle_t s_btdm_slp_tmr = NULL; #endif + static const struct osi_coex_funcs_t s_osi_coex_funcs_ro = { ._magic = OSI_COEX_MAGIC_VALUE, ._version = OSI_COEX_VERSION, @@ -238,6 +247,7 @@ static void coex_schm_status_bit_clear_wrapper(uint32_t type, uint32_t status) #endif } #ifdef CONFIG_BT_BLUEDROID_ENABLED + bool esp_vhci_host_check_send_available(void) { if (ble_controller_status != ESP_BT_CONTROLLER_STATUS_ENABLED) { @@ -295,7 +305,7 @@ void esp_vhci_host_send_packet(uint8_t *data, uint16_t len) } if (*(data) == DATA_TYPE_ACL) { - struct os_mbuf *om = os_msys_get_pkthdr(0, 0); + struct os_mbuf *om = os_msys_get_pkthdr(0, ACL_DATA_MBUF_LEADINGSPCAE); assert(om); memcpy(om->om_data, &data[1], len - 1); om->om_len = len - 1; @@ -770,16 +780,120 @@ esp_bt_controller_status_t esp_bt_controller_get_status(void) /* extra functions */ esp_err_t esp_ble_tx_power_set(esp_ble_power_type_t power_type, esp_power_level_t power_level) { - ESP_LOGW(NIMBLE_PORT_LOG_TAG, "%s not implemented, return OK", __func__); - return ESP_OK; + esp_err_t stat = ESP_FAIL; + + switch (power_type) { + case ESP_BLE_PWR_TYPE_DEFAULT: + case ESP_BLE_PWR_TYPE_ADV: + case ESP_BLE_PWR_TYPE_SCAN: + if (ble_txpwr_set(ESP_BLE_ENHANCED_PWR_TYPE_DEFAULT, 0, power_level) == 0) { + stat = ESP_OK; + } + break; + case ESP_BLE_PWR_TYPE_CONN_HDL0: + case ESP_BLE_PWR_TYPE_CONN_HDL1: + case ESP_BLE_PWR_TYPE_CONN_HDL2: + case ESP_BLE_PWR_TYPE_CONN_HDL3: + case ESP_BLE_PWR_TYPE_CONN_HDL4: + case ESP_BLE_PWR_TYPE_CONN_HDL5: + case ESP_BLE_PWR_TYPE_CONN_HDL6: + case ESP_BLE_PWR_TYPE_CONN_HDL7: + case ESP_BLE_PWR_TYPE_CONN_HDL8: + if (ble_txpwr_set(ESP_BLE_ENHANCED_PWR_TYPE_CONN, power_type, power_level) == 0) { + stat = ESP_OK; + } + break; + default: + stat = ESP_ERR_NOT_SUPPORTED; + break; + } + + return stat; +} + +esp_err_t esp_ble_tx_power_set_enhanced(esp_ble_enhanced_power_type_t power_type, uint16_t handle, esp_power_level_t power_level) +{ + esp_err_t stat = ESP_FAIL; + switch (power_type) { + case ESP_BLE_ENHANCED_PWR_TYPE_DEFAULT: + case ESP_BLE_ENHANCED_PWR_TYPE_SCAN: + case ESP_BLE_ENHANCED_PWR_TYPE_INIT: + if (ble_txpwr_set(ESP_BLE_ENHANCED_PWR_TYPE_DEFAULT, 0, power_level) == 0) { + stat = ESP_OK; + } + break; + case ESP_BLE_ENHANCED_PWR_TYPE_ADV: + case ESP_BLE_ENHANCED_PWR_TYPE_CONN: + if (ble_txpwr_set(power_type, handle, power_level) == 0) { + stat = ESP_OK; + } + break; + default: + stat = ESP_ERR_NOT_SUPPORTED; + break; + } + + return stat; } esp_power_level_t esp_ble_tx_power_get(esp_ble_power_type_t power_type) { - ESP_LOGW(NIMBLE_PORT_LOG_TAG, "%s not implemented, return OK", __func__); - return ESP_PWR_LVL_N0; + int tx_level = 0; + + switch (power_type) { + case ESP_BLE_PWR_TYPE_ADV: + case ESP_BLE_PWR_TYPE_SCAN: + case ESP_BLE_PWR_TYPE_DEFAULT: + tx_level = ble_txpwr_get(ESP_BLE_ENHANCED_PWR_TYPE_DEFAULT, 0); + break; + case ESP_BLE_PWR_TYPE_CONN_HDL0: + case ESP_BLE_PWR_TYPE_CONN_HDL1: + case ESP_BLE_PWR_TYPE_CONN_HDL2: + case ESP_BLE_PWR_TYPE_CONN_HDL3: + case ESP_BLE_PWR_TYPE_CONN_HDL4: + case ESP_BLE_PWR_TYPE_CONN_HDL5: + case ESP_BLE_PWR_TYPE_CONN_HDL6: + case ESP_BLE_PWR_TYPE_CONN_HDL7: + case ESP_BLE_PWR_TYPE_CONN_HDL8: + tx_level = ble_txpwr_get(ESP_BLE_ENHANCED_PWR_TYPE_CONN, power_type); + break; + default: + return ESP_PWR_LVL_INVALID; + } + + if (tx_level < 0) { + return ESP_PWR_LVL_INVALID; + } + + return (esp_power_level_t)tx_level; } +esp_power_level_t esp_ble_tx_power_get_enhanced(esp_ble_enhanced_power_type_t power_type, uint16_t handle) +{ + int tx_level = 0; + + switch (power_type) { + case ESP_BLE_ENHANCED_PWR_TYPE_DEFAULT: + case ESP_BLE_ENHANCED_PWR_TYPE_SCAN: + case ESP_BLE_ENHANCED_PWR_TYPE_INIT: + tx_level = ble_txpwr_get(ESP_BLE_ENHANCED_PWR_TYPE_DEFAULT, 0); + break; + case ESP_BLE_ENHANCED_PWR_TYPE_ADV: + case ESP_BLE_ENHANCED_PWR_TYPE_CONN: + tx_level = ble_txpwr_get(power_type, handle); + break; + default: + return ESP_PWR_LVL_INVALID; + } + + if (tx_level < 0) { + return ESP_PWR_LVL_INVALID; + } + + return (esp_power_level_t)tx_level; +} + + #if (!CONFIG_BT_NIMBLE_ENABLED) && (CONFIG_BT_CONTROLLER_ENABLED == true) #define BLE_SM_KEY_ERR 0x17 diff --git a/components/bt/controller/lib_esp32c2/esp32c2-bt-lib b/components/bt/controller/lib_esp32c2/esp32c2-bt-lib index 34e6a82bab..09e1db0eec 160000 --- a/components/bt/controller/lib_esp32c2/esp32c2-bt-lib +++ b/components/bt/controller/lib_esp32c2/esp32c2-bt-lib @@ -1 +1 @@ -Subproject commit 34e6a82bab1a0d09eb8d96277d8c7b4d9006071e +Subproject commit 09e1db0eec6bc41d1742c9ab3ebba3884f9277f1 diff --git a/components/bt/include/esp32c2/include/esp_bt.h b/components/bt/include/esp32c2/include/esp_bt.h index 3cac31b703..0a844dba0d 100644 --- a/components/bt/include/esp32c2/include/esp_bt.h +++ b/components/bt/include/esp32c2/include/esp_bt.h @@ -76,25 +76,34 @@ typedef enum { * @brief Bluetooth TX power level(index), it's just a index corresponding to power(dbm). */ typedef enum { - ESP_PWR_LVL_N27 = 0, /*!< Corresponding to -27dbm */ - ESP_PWR_LVL_N24 = 1, /*!< Corresponding to -24dbm */ - ESP_PWR_LVL_N21 = 2, /*!< Corresponding to -21dbm */ - ESP_PWR_LVL_N18 = 3, /*!< Corresponding to -18dbm */ - ESP_PWR_LVL_N15 = 4, /*!< Corresponding to -15dbm */ - ESP_PWR_LVL_N12 = 5, /*!< Corresponding to -12dbm */ - ESP_PWR_LVL_N9 = 6, /*!< Corresponding to -9dbm */ - ESP_PWR_LVL_N6 = 7, /*!< Corresponding to -6dbm */ - ESP_PWR_LVL_N3 = 8, /*!< Corresponding to -3dbm */ - ESP_PWR_LVL_N0 = 9, /*!< Corresponding to 0dbm */ - ESP_PWR_LVL_P3 = 10, /*!< Corresponding to +3dbm */ - ESP_PWR_LVL_P6 = 11, /*!< Corresponding to +6dbm */ - ESP_PWR_LVL_P9 = 12, /*!< Corresponding to +9dbm */ - ESP_PWR_LVL_P12 = 13, /*!< Corresponding to +12dbm */ - ESP_PWR_LVL_P15 = 14, /*!< Corresponding to +15dbm */ - ESP_PWR_LVL_P18 = 15, /*!< Corresponding to +18dbm */ + ESP_PWR_LVL_N24 = 0, /*!< Corresponding to -24dbm */ + ESP_PWR_LVL_N21 = 1, /*!< Corresponding to -21dbm */ + ESP_PWR_LVL_N18 = 2, /*!< Corresponding to -18dbm */ + ESP_PWR_LVL_N15 = 3, /*!< Corresponding to -15dbm */ + ESP_PWR_LVL_N12 = 4, /*!< Corresponding to -12dbm */ + ESP_PWR_LVL_N9 = 5, /*!< Corresponding to -9dbm */ + ESP_PWR_LVL_N6 = 6, /*!< Corresponding to -6dbm */ + ESP_PWR_LVL_N3 = 7, /*!< Corresponding to -3dbm */ + ESP_PWR_LVL_N0 = 8, /*!< Corresponding to 0dbm */ + ESP_PWR_LVL_P3 = 9, /*!< Corresponding to +3dbm */ + ESP_PWR_LVL_P6 = 10, /*!< Corresponding to +6dbm */ + ESP_PWR_LVL_P9 = 11, /*!< Corresponding to +9dbm */ + ESP_PWR_LVL_P12 = 12, /*!< Corresponding to +12dbm */ + ESP_PWR_LVL_P15 = 13, /*!< Corresponding to +15dbm */ + ESP_PWR_LVL_P18 = 14, /*!< Corresponding to +18dbm */ + ESP_PWR_LVL_P21 = 15, /*!< Corresponding to +21dbm */ ESP_PWR_LVL_INVALID = 0xFF, /*!< Indicates an invalid value */ } esp_power_level_t; +typedef enum { + ESP_BLE_ENHANCED_PWR_TYPE_DEFAULT = 0, + ESP_BLE_ENHANCED_PWR_TYPE_ADV, + ESP_BLE_ENHANCED_PWR_TYPE_SCAN, + ESP_BLE_ENHANCED_PWR_TYPE_INIT, + ESP_BLE_ENHANCED_PWR_TYPE_CONN, + ESP_BLE_ENHANCED_PWR_TYPE_MAX, +} esp_ble_enhanced_power_type_t; + typedef struct { uint8_t type; uint8_t val[6]; @@ -117,6 +126,24 @@ esp_err_t esp_ble_tx_power_set(esp_ble_power_type_t power_type, esp_power_level_ */ esp_power_level_t esp_ble_tx_power_get(esp_ble_power_type_t power_type); +/** + * @brief ENHANCED API for Setting BLE TX power + * Connection Tx power should only be set after connection created. + * @param power_type : The enhanced type of which tx power, could set Advertising/Connection/Default and etc + * @param handle : The handle of Advertising or Connection and the value 0 for other enhanced power types. + * @param power_level: Power level(index) corresponding to absolute value(dbm) + * @return ESP_OK - success, other - failed + */ +esp_err_t esp_ble_tx_power_set_enhanced(esp_ble_enhanced_power_type_t power_type, uint16_t handle, esp_power_level_t power_level); + +/** + * @brief ENHANCED API of Getting BLE TX power + * Connection Tx power should only be get after connection created. + * @param power_type : The enhanced type of which tx power, could set Advertising/Connection/Default and etc + * @param handle : The handle of Advertising or Connection and the value 0 for other enhanced power types. + * @return >= 0 - Power level, < 0 - Invalid + */ +esp_power_level_t esp_ble_tx_power_get_enhanced(esp_ble_enhanced_power_type_t power_type, uint16_t handle); #define CONFIG_VERSION 0x20220729 #define CONFIG_MAGIC 0x5A5AA5A5 diff --git a/components/bt/porting/nimble/include/nimble/ble.h b/components/bt/porting/nimble/include/nimble/ble.h index 5f47299e3e..f037d3565b 100644 --- a/components/bt/porting/nimble/include/nimble/ble.h +++ b/components/bt/porting/nimble/include/nimble/ble.h @@ -116,9 +116,10 @@ struct ble_mbuf_hdr_rxinfo struct ble_mbuf_hdr_txinfo { uint8_t flags; - uint8_t offset; + uint8_t reserve0; uint8_t pyld_len; uint8_t hdr_byte; + uint16_t offset; }; struct ble_mbuf_hdr diff --git a/components/esp_rom/esp32c2/ld/esp32c2.rom.ld b/components/esp_rom/esp32c2/ld/esp32c2.rom.ld index 82275d2481..e782a058b2 100644 --- a/components/esp_rom/esp32c2/ld/esp32c2.rom.ld +++ b/components/esp_rom/esp32c2/ld/esp32c2.rom.ld @@ -720,7 +720,6 @@ r_ble_ll_conn_cth_flow_have_credit = 0x40000d6c; r_ble_ll_conn_cth_flow_is_enabled = 0x40000d70; r_ble_ll_conn_cth_flow_process_cmd = 0x40000d74; r_ble_ll_conn_cth_flow_set_buffers = 0x40000d78; -r_ble_ll_conn_enqueue_pkt = 0x40000d7c; r_ble_ll_conn_ext_master_init = 0x40000d84; r_ble_ll_conn_find_active_conn = 0x40000d88; r_ble_ll_conn_get_active_conn = 0x40000d8c; @@ -752,7 +751,6 @@ r_ble_ll_conn_master_init = 0x40000df8; r_ble_ll_conn_module_reset = 0x40000e04; r_ble_ll_conn_next_event = 0x40000e08; r_ble_ll_conn_num_comp_pkts_event_send = 0x40000e0c; -r_ble_ll_conn_prepare_tx_pdu = 0x40000e10; r_ble_ll_conn_process_conn_params = 0x40000e14; r_ble_ll_conn_req_peer_sca = 0x40000e18; r_ble_ll_conn_rx_data_pdu = 0x40000e1c; @@ -904,7 +902,6 @@ r_ble_ll_hw_err_timer_cb = 0x400010a0; r_ble_ll_hw_error = 0x400010a4; r_ble_ll_init_alloc_conn_comp_ev = 0x400010ac; r_ble_ll_init_get_conn_comp_ev = 0x400010b0; -r_ble_ll_init_rx_pkt_in = 0x400010b4; r_ble_ll_is_addr_empty = 0x400010b8; r_ble_ll_is_controller_busy = 0x400010bc; r_ble_ll_is_on_resolv_list = 0x400010c0; @@ -914,7 +911,6 @@ r_ble_ll_is_valid_adv_mode = 0x400010cc; r_ble_ll_is_valid_own_addr_type = 0x400010d0; r_ble_ll_is_valid_public_addr = 0x400010d4; r_ble_ll_is_valid_random_addr = 0x400010d8; -r_ble_ll_mbuf_init = 0x400010dc; r_ble_ll_misc_options_set = 0x400010e0; r_ble_ll_modify_sca = 0x400010e4; r_ble_ll_modify_sca_action = 0x400010e8; @@ -934,7 +930,6 @@ r_ble_ll_read_local_p256_pub_key = 0x4000111c; r_ble_ll_read_rf_path_compensation = 0x40001120; r_ble_ll_read_supp_features = 0x40001124; r_ble_ll_read_supp_states = 0x40001128; -r_ble_ll_read_tx_power = 0x4000112c; r_ble_ll_reset = 0x40001130; r_ble_ll_resolv_clear_all_pl_bit = 0x40001134; r_ble_ll_resolv_clear_all_wl_bit = 0x40001138; @@ -1007,7 +1002,6 @@ r_ble_ll_scan_record_new_adv = 0x40001250; r_ble_ll_scan_refresh_nrpa = 0x40001254; r_ble_ll_scan_reset = 0x40001258; r_ble_ll_scan_rx_pkt_in = 0x4000125c; -r_ble_ll_scan_rx_pkt_in_on_legacy = 0x40001264; r_ble_ll_scan_rx_pkt_in_restore_addr_data = 0x40001268; r_ble_ll_scan_rxed = 0x4000126c; r_ble_ll_scan_send_adv_report = 0x40001270; @@ -1072,7 +1066,6 @@ r_ble_ll_trace_u32 = 0x40001384; r_ble_ll_trace_u32x2 = 0x40001388; r_ble_ll_trace_u32x3 = 0x4000138c; r_ble_ll_tx_flat_mbuf_pducb = 0x40001390; -r_ble_ll_tx_mbuf_pducb = 0x40001394; r_ble_ll_tx_pkt_in = 0x40001398; r_ble_ll_update_max_tx_octets_phy_mode = 0x4000139c; r_ble_ll_usecs_to_ticks_round_up = 0x400013a0; @@ -1154,7 +1147,6 @@ r_ble_lll_conn_coex_dpc_update_on_event_end = 0x400014e0; r_ble_lll_conn_coex_dpc_update_on_event_scheduled = 0x400014e4; r_ble_lll_conn_coex_dpc_update_on_event_started = 0x400014e8; r_ble_lll_conn_cth_flow_alloc_credit = 0x400014ec; -r_ble_lll_conn_cth_flow_free_credit = 0x400014f0; r_ble_lll_conn_current_sm_over = 0x400014f4; r_ble_lll_conn_env_deinit = 0x400014fc; r_ble_lll_conn_env_init = 0x40001500; @@ -1167,16 +1159,13 @@ r_ble_lll_conn_free_rx_mbuf = 0x40001518; r_ble_lll_conn_get_addr_info_from_rx_buf = 0x4000151c; r_ble_lll_conn_get_ce_end_time = 0x40001520; r_ble_lll_conn_get_next_sched_time = 0x40001524; -r_ble_lll_conn_get_rx_mbuf = 0x40001528; r_ble_lll_conn_halt = 0x4000152c; r_ble_lll_conn_master_common_init = 0x40001530; r_ble_lll_conn_master_new = 0x40001534; r_ble_lll_conn_module_deinit = 0x40001538; r_ble_lll_conn_module_reset = 0x40001540; -r_ble_lll_conn_no_mem_evt_pre_cb = 0x40001544; r_ble_lll_conn_pre_process = 0x40001548; r_ble_lll_conn_process_acked_pdu = 0x4000154c; -r_ble_lll_conn_process_in_isr = 0x40001550; r_ble_lll_conn_recv_ack = 0x40001554; r_ble_lll_conn_recv_valid_packet = 0x40001558; r_ble_lll_conn_reset_pending_sched = 0x4000155c; @@ -1184,7 +1173,6 @@ r_ble_lll_conn_rx_pkt_isr = 0x40001560; r_ble_lll_conn_sched_next_anchor = 0x40001564; r_ble_lll_conn_sched_next_event = 0x40001568; r_ble_lll_conn_set_slave_flow_control = 0x4000156c; -r_ble_lll_conn_slave_new = 0x40001570; r_ble_lll_conn_sm_new = 0x40001574; r_ble_lll_conn_sm_npl_deinit = 0x40001578; r_ble_lll_conn_sm_npl_init = 0x4000157c; @@ -1210,7 +1198,6 @@ r_ble_lll_dtm_rx_sched_cb = 0x400015cc; r_ble_lll_dtm_rx_start = 0x400015d0; r_ble_lll_dtm_rx_test = 0x400015d4; r_ble_lll_dtm_set_next = 0x400015d8; -r_ble_lll_dtm_tx_create_ctx = 0x400015dc; r_ble_lll_dtm_tx_done = 0x400015e0; r_ble_lll_dtm_tx_sched_cb = 0x400015e4; r_ble_lll_dtm_tx_test = 0x400015e8; @@ -1278,7 +1265,6 @@ r_ble_lll_scan_period_timer_cb = 0x400016e0; r_ble_lll_scan_process_adv_in_isr = 0x400016e4; r_ble_lll_scan_req_backoff = 0x400016ec; r_ble_lll_scan_restart = 0x400016f0; -r_ble_lll_scan_rx_pkt_isr = 0x400016fc; r_ble_lll_scan_sched_next_aux = 0x40001700; r_ble_lll_scan_sched_remove = 0x40001704; r_ble_lll_scan_start = 0x40001708; @@ -1350,7 +1336,6 @@ r_ble_phy_get_packet_status = 0x40001818; r_ble_phy_get_pyld_time_offset = 0x4000181c; r_ble_phy_get_rx_phy_mode = 0x40001820; r_ble_phy_get_seq_end_st = 0x40001824; -r_ble_phy_isr = 0x4000182c; r_ble_phy_max_data_pdu_pyld = 0x40001830; r_ble_phy_mode_config = 0x40001834; r_ble_phy_mode_convert = 0x40001838; @@ -1379,9 +1364,7 @@ r_ble_phy_set_conn_ind_pdu = 0x40001890; r_ble_phy_set_conn_mode = 0x40001894; r_ble_phy_set_dev_address = 0x40001898; r_ble_phy_set_rx_pwr_compensation = 0x4000189c; -r_ble_phy_set_rxhdr = 0x400018a0; r_ble_phy_set_scan_mode = 0x400018a4; -r_ble_phy_set_sequence_mode = 0x400018a8; r_ble_phy_set_single_packet_rx_sequence = 0x400018ac; r_ble_phy_set_single_packet_tx_sequence = 0x400018b0; r_ble_phy_set_tx_rx_transition = 0x400018b4;