Skip to content

Commit

Permalink
Merge branch 'bugfix/fix_blecrt_196' into 'master'
Browse files Browse the repository at this point in the history
feat(bt/bluedroid): Report HCI error code to app when command fails

Closes BLERP-897

See merge request espressif/esp-idf!32411
  • Loading branch information
Isl2017 committed Aug 5, 2024
2 parents ef3d063 + 1ba831b commit ba4e111
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 30 deletions.
8 changes: 8 additions & 0 deletions components/bt/host/bluedroid/api/include/api/esp_bt_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ typedef enum {
ESP_BT_STATUS_HCI_CONN_TOUT_DUE_TO_MIC_FAILURE,
ESP_BT_STATUS_HCI_CONN_FAILED_ESTABLISHMENT,
ESP_BT_STATUS_HCI_MAC_CONNECTION_FAILED,
ESP_BT_STATUS_HCI_CCA_REJECTED,
ESP_BT_STATUS_HCI_TYPE0_SUBMAP_NOT_DEFINED,
ESP_BT_STATUS_HCI_UNKNOWN_ADV_ID,
ESP_BT_STATUS_HCI_LIMIT_REACHED,
ESP_BT_STATUS_HCI_OPT_CANCEL_BY_HOST,
ESP_BT_STATUS_HCI_PKT_TOO_LONG,
ESP_BT_STATUS_HCI_TOO_LATE,
ESP_BT_STATUS_HCI_TOO_EARLY,
} esp_bt_status_t;


Expand Down
10 changes: 7 additions & 3 deletions components/bt/host/bluedroid/btc/core/btc_util.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
* SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand Down Expand Up @@ -352,7 +352,7 @@ esp_bt_status_t btc_hci_to_esp_status(uint8_t hci_status)
esp_bt_status_t btc_btm_status_to_esp_status (uint8_t btm_status)
{
esp_bt_status_t esp_status = ESP_BT_STATUS_FAIL;
switch(btm_status){
switch(btm_status) {
case BTM_SUCCESS:
esp_status = ESP_BT_STATUS_SUCCESS;
break;
Expand Down Expand Up @@ -381,7 +381,11 @@ esp_bt_status_t btc_btm_status_to_esp_status (uint8_t btm_status)
esp_status = ESP_BT_STATUS_FAIL;
break;
default:
esp_status = ESP_BT_STATUS_FAIL;
if (btm_status & BTM_HCI_ERROR) {
esp_status = ESP_BT_STATUS_BASE_FOR_HCI_ERR | (btm_status & 0x7F);
} else {
esp_status = ESP_BT_STATUS_FAIL;
}
break;
}

Expand Down
60 changes: 33 additions & 27 deletions components/bt/host/bluedroid/stack/btm/btm_ble_5_gap.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,10 @@ tBTM_STATUS BTM_BleSetPreferDefaultPhy(UINT8 tx_phy_mask, UINT8 rx_phy_mask)
if ((err = btsnd_hcic_ble_set_prefered_default_phy(all_phys, tx_phy_mask, rx_phy_mask)) != HCI_SUCCESS) {
BTM_TRACE_ERROR("%s, fail to send the hci command, the error code = %s(0x%x)",
__func__, btm_ble_hci_status_to_str(err), err);
status = BTM_ILLEGAL_VALUE;
status = BTM_HCI_ERROR | err;
}

cb_params.set_perf_def_phy.status = err;
cb_params.set_perf_def_phy.status = status;

BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_SET_PREFERED_DEFAULT_PHY_COMPLETE_EVT, &cb_params);

Expand Down Expand Up @@ -330,7 +330,7 @@ tBTM_STATUS BTM_BleSetExtendedAdvRandaddr(UINT8 instance, BD_ADDR rand_addr)
if((err = btsnd_hcic_ble_set_extend_rand_address(instance, rand_addr)) != HCI_SUCCESS) {
BTM_TRACE_ERROR("%s, fail to send the hci command, the error code = %s(0x%x)",
__func__, btm_ble_hci_status_to_str(err), err);
status = BTM_ILLEGAL_VALUE;
status = BTM_HCI_ERROR | err;
} else {
// set random address success, update address info
if(extend_adv_cb.inst[instance].configured && extend_adv_cb.inst[instance].connetable) {
Expand Down Expand Up @@ -403,7 +403,7 @@ tBTM_STATUS BTM_BleSetExtendedAdvParams(UINT8 instance, tBTM_BLE_GAP_EXT_ADV_PAR
params->primary_phy, params->max_skip,
params->secondary_phy, params->sid, params->scan_req_notif)) != HCI_SUCCESS) {
BTM_TRACE_ERROR("LE EA SetParams: cmd err=0x%x", err);
status = BTM_ILLEGAL_VALUE;
status = BTM_HCI_ERROR | err;
goto end;
}

Expand All @@ -414,7 +414,7 @@ tBTM_STATUS BTM_BleSetExtendedAdvParams(UINT8 instance, tBTM_BLE_GAP_EXT_ADV_PAR
// update RPA address
if((err = btsnd_hcic_ble_set_extend_rand_address(instance, rand_addr)) != HCI_SUCCESS) {
BTM_TRACE_ERROR("LE EA SetParams: cmd err=0x%x", err);
status = BTM_ILLEGAL_VALUE;
status = BTM_HCI_ERROR | err;
} else {
// set addr success, update address info
BTM_UpdateAddrInfor(BLE_ADDR_RANDOM, rand_addr);
Expand Down Expand Up @@ -452,23 +452,25 @@ tBTM_STATUS BTM_BleConfigExtendedAdvDataRaw(BOOLEAN is_scan_rsp, UINT8 instance,
} else if (rem_len <= BTM_BLE_EXT_ADV_DATA_LEN_MAX) {
operation = BTM_BLE_ADV_DATA_OP_LAST_FRAG;
} else {
operation = BTM_BLE_ADV_DATA_OP_INTERMEDIATE_FRAG;
}
operation = BTM_BLE_ADV_DATA_OP_INTERMEDIATE_FRAG;
}
}
if (!is_scan_rsp) {
if ((err = btsnd_hcic_ble_set_ext_adv_data(instance, operation, 0, send_data_len, &data[data_offset])) != HCI_SUCCESS) {
BTM_TRACE_ERROR("LE EA SetAdvData: cmd err=0x%x", err);
status = BTM_ILLEGAL_VALUE;
status = BTM_HCI_ERROR | err;
break;
}
} else {
if ((err = btsnd_hcic_ble_set_ext_adv_scan_rsp_data(instance, operation, 0, send_data_len, &data[data_offset])) != HCI_SUCCESS) {
BTM_TRACE_ERROR("LE EA SetScanRspData: cmd err=0x%x", err);
status = BTM_ILLEGAL_VALUE;
status = BTM_HCI_ERROR | err;
break;
}
}

rem_len -= send_data_len;
data_offset += send_data_len;
data_offset += send_data_len;
} while (rem_len);

end:
Expand Down Expand Up @@ -522,7 +524,7 @@ tBTM_STATUS BTM_BleStartExtAdv(BOOLEAN enable, UINT8 num, tBTM_BLE_EXT_ADV *ext_
if ((err = btsnd_hcic_ble_ext_adv_enable(enable, num, instance,
duration, max_events)) != HCI_SUCCESS) {
BTM_TRACE_ERROR("LE EA En=%d: cmd err=0x%x", enable, err);
status = BTM_ILLEGAL_VALUE;
status = BTM_HCI_ERROR | err;
}

osi_free(instance);
Expand All @@ -533,7 +535,7 @@ tBTM_STATUS BTM_BleStartExtAdv(BOOLEAN enable, UINT8 num, tBTM_BLE_EXT_ADV *ext_

if ((err = btsnd_hcic_ble_ext_adv_enable(enable, num, NULL, NULL, NULL)) != HCI_SUCCESS) {
BTM_TRACE_ERROR("LE EA En=%d: cmd err=0x%x", enable, err);
status = BTM_ILLEGAL_VALUE;
status = BTM_HCI_ERROR | err;
}
goto end;
}
Expand Down Expand Up @@ -631,7 +633,7 @@ tBTM_STATUS BTM_BleExtAdvSetRemove(UINT8 instance)

if ((err = btsnd_hcic_ble_remove_adv_set(instance)) != HCI_SUCCESS) {
BTM_TRACE_ERROR("LE EAS Rm: cmd err=0x%x", err);
status = BTM_ILLEGAL_VALUE;
status = BTM_HCI_ERROR | err;
} else {
extend_adv_cb.inst[instance].configured = false;
extend_adv_cb.inst[instance].legacy_pdu = false;
Expand Down Expand Up @@ -659,7 +661,7 @@ tBTM_STATUS BTM_BleExtAdvSetClear(void)

if ((err = btsnd_hcic_ble_clear_adv_set()) != HCI_SUCCESS) {
BTM_TRACE_ERROR("LE EAS Clr: cmd err=0x%x", err);
status = BTM_ILLEGAL_VALUE;
status = BTM_HCI_ERROR | err;
} else {
for (uint8_t i = 0; i < MAX_BLE_ADV_INSTANCE; i++) {
extend_adv_cb.inst[i].configured = false;
Expand Down Expand Up @@ -703,7 +705,7 @@ tBTM_STATUS BTM_BlePeriodicAdvSetParams(UINT8 instance, tBTM_BLE_Periodic_Adv_Pa
if ((err= btsnd_hcic_ble_set_periodic_adv_params(instance, params->interval_min,
params->interval_max, params->properties)) != HCI_SUCCESS) {
BTM_TRACE_ERROR("LE PA SetParams: cmd err=0x%x", err);
status = BTM_ILLEGAL_VALUE;
status = BTM_HCI_ERROR | err;
}

end:
Expand Down Expand Up @@ -756,10 +758,12 @@ tBTM_STATUS BTM_BlePeriodicAdvCfgDataRaw(UINT8 instance, UINT16 len, UINT8 *data

if ((err = btsnd_hcic_ble_set_periodic_adv_data(instance, operation, send_data_len, &data[data_offset])) != HCI_SUCCESS) {
BTM_TRACE_ERROR("LE PA SetData: cmd err=0x%x", err);
status = BTM_ILLEGAL_VALUE;
status = BTM_HCI_ERROR | err;
break;
}

rem_len -= send_data_len;
data_offset += send_data_len;
data_offset += send_data_len;
} while(rem_len);

end:
Expand All @@ -785,7 +789,7 @@ tBTM_STATUS BTM_BlePeriodicAdvEnable(UINT8 instance, UINT8 enable)

if ((err = btsnd_hcic_ble_periodic_adv_enable(enable, instance)) != HCI_SUCCESS) {
BTM_TRACE_ERROR("LE PA En=%d: cmd err=0x%x", enable, err);
status = BTM_ILLEGAL_VALUE;
status = BTM_HCI_ERROR | err;
}

end:
Expand Down Expand Up @@ -860,6 +864,7 @@ tBTM_STATUS BTM_BlePeriodicAdvCreateSync(tBTM_BLE_Periodic_Sync_Params *params)

return status;
}

void btm_set_phy_callback(UINT8 status)
{
tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
Expand All @@ -868,6 +873,7 @@ void btm_set_phy_callback(UINT8 status)
BTM_ExtBleCallbackTrigger(BTM_BLE_5_GAP_SET_PREFERED_PHY_COMPLETE_EVT, &cb_params);

}

void btm_create_sync_callback(UINT8 status)
{
tBTM_BLE_5_GAP_CB_PARAMS cb_params = {0};
Expand Down Expand Up @@ -903,7 +909,7 @@ tBTM_STATUS BTM_BlePeriodicAdvSyncCancel(void)

if ((err = btsnd_hcic_ble_periodic_adv_create_sync_cancel()) != HCI_SUCCESS) {
BTM_TRACE_ERROR("LE PA SyncCancel, cmd err=0x%x", err);
status = BTM_ILLEGAL_VALUE;
status = BTM_HCI_ERROR | err;
}

cb_params.status = status;
Expand All @@ -921,7 +927,7 @@ tBTM_STATUS BTM_BlePeriodicAdvSyncTerm(UINT16 sync_handle)

if (( err = btsnd_hcic_ble_periodic_adv_term_sync(sync_handle)) != HCI_SUCCESS) {
BTM_TRACE_ERROR("LE PA SyncTerm: cmd err=0x%x", err);
status = BTM_ILLEGAL_VALUE;
status = BTM_HCI_ERROR | err;
}

cb_params.status = status;
Expand All @@ -945,7 +951,7 @@ tBTM_STATUS BTM_BlePeriodicAdvAddDevToList(tBLE_ADDR_TYPE addr_type, BD_ADDR add

if ((err = btsnd_hcic_ble_add_dev_to_periodic_adv_list(addr_type, addr, sid)) != HCI_SUCCESS) {
BTM_TRACE_ERROR("LE PA AddDevToList: cmd err=0x%x", err);
status = BTM_ILLEGAL_VALUE;
status = BTM_HCI_ERROR | err;
}

end:
Expand All @@ -969,7 +975,7 @@ tBTM_STATUS BTM_BlePeriodicAdvRemoveDevFromList(tBLE_ADDR_TYPE addr_type, BD_ADD

if ((err = btsnd_hcic_ble_rm_dev_from_periodic_adv_list(addr_type, addr, sid)) != HCI_SUCCESS) {
BTM_TRACE_ERROR("LE PA RmDevFromList: cmd err=0x%x", err);
status = BTM_ILLEGAL_VALUE;
status = BTM_HCI_ERROR | err;
}

end:
Expand All @@ -987,7 +993,7 @@ tBTM_STATUS BTM_BlePeriodicAdvClearDev(void)

if ((err = btsnd_hcic_ble_clear_periodic_adv_list()) != HCI_SUCCESS) {
BTM_TRACE_ERROR("LE PA ClrDev: cmd err=0x%x", err);
status = BTM_ILLEGAL_VALUE;
status = BTM_HCI_ERROR | err;
}

cb_params.status = status;
Expand Down Expand Up @@ -1039,7 +1045,7 @@ tBTM_STATUS BTM_BleSetExtendedScanParams(tBTM_BLE_EXT_SCAN_PARAMS *params)
if ((err = btsnd_hcic_ble_set_ext_scan_params(params->own_addr_type, params->filter_policy, phy_mask, phy_count,
hci_params)) != HCI_SUCCESS) {
BTM_TRACE_ERROR("LE ES SetParams: cmd err=0x%x", err);
status = BTM_ILLEGAL_VALUE;
status = BTM_HCI_ERROR | err;
}

end:
Expand All @@ -1065,7 +1071,7 @@ tBTM_STATUS BTM_BleExtendedScan(BOOLEAN enable, UINT16 duration, UINT16 period)

if ((err = btsnd_hcic_ble_ext_scan_enable(enable, extend_adv_cb.scan_duplicate, duration, period)) != HCI_SUCCESS) {
BTM_TRACE_ERROR("LE ES En=%d: cmd err=0x%x", enable, err);
status = BTM_ILLEGAL_VALUE;
status = BTM_HCI_ERROR | err;
}

end:
Expand Down Expand Up @@ -1401,7 +1407,7 @@ void BTM_BlePeriodicAdvRecvEnable(UINT16 sync_handle, UINT8 enable)

if ((err = btsnd_hcic_ble_set_periodic_adv_recv_enable(sync_handle, enable)) != HCI_SUCCESS) {
BTM_TRACE_ERROR("%s cmd err=0x%x", __func__, err);
status = BTM_ILLEGAL_VALUE;
status = BTM_HCI_ERROR | err;
}

cb_params.status = status;
Expand Down Expand Up @@ -1460,7 +1466,7 @@ void BTM_BleSetPeriodicAdvSyncTransParams(BD_ADDR bd_addr, UINT8 mode, UINT16 sk
tHCI_STATUS err = HCI_SUCCESS;
if ((err = btsnd_hcic_ble_set_default_periodic_adv_sync_trans_params(mode, skip, sync_timeout, cte_type)) != HCI_SUCCESS) {
BTM_TRACE_ERROR("%s cmd err=0x%x", __func__, err);
status = BTM_ILLEGAL_VALUE;
status = BTM_HCI_ERROR | err;
}

cb_params.set_past_params.status = status;
Expand Down
1 change: 1 addition & 0 deletions components/bt/host/bluedroid/stack/include/stack/btm_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ enum {
BTM_SET_STATIC_RAND_ADDR_FAIL, /* 25 Command failed */
BTM_INVALID_STATIC_RAND_ADDR, /* 26 invalid static rand addr */
BTM_SEC_DEV_REC_REMOVED, /* 27 Device record relate to the bd_addr is removed */
BTM_HCI_ERROR = 128, /* 128 HCI error code from controller (0x80) */
};

typedef uint8_t tBTM_STATUS;
Expand Down

0 comments on commit ba4e111

Please sign in to comment.