From e09eee954157456f706fcdf94a2c9006ed5bb271 Mon Sep 17 00:00:00 2001 From: hmz007 Date: Fri, 30 May 2025 14:00:59 +0800 Subject: [PATCH] Bluetooth: btlinux: adapt to dynamic HCI interface index Signed-off-by: hmz007 --- .../vendor_libs/linux/interface/bluetooth_hci.cc | 15 +++++++++++++-- .../vendor_libs/linux/interface/h4_protocol.cc | 10 ++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/modules/Bluetooth/system/vendor_libs/linux/interface/bluetooth_hci.cc b/packages/modules/Bluetooth/system/vendor_libs/linux/interface/bluetooth_hci.cc index 7f4814ff613..8cef2930b38 100644 --- a/packages/modules/Bluetooth/system/vendor_libs/linux/interface/bluetooth_hci.cc +++ b/packages/modules/Bluetooth/system/vendor_libs/linux/interface/bluetooth_hci.cc @@ -84,10 +84,14 @@ int BluetoothHci::openBtHci() { } bt_soc_fd_ = fd; - if (waitHciDev(hci_interface)) { + int ret = waitHciDev(hci_interface); + if (ret < 0) { ALOGE( "HCI interface (%d) not found", hci_interface); ::close(fd); return -1; + } else if (hci_interface != ret) { + hci_interface = ret; + ALOGD( "HCI interface (%d) found", hci_interface); } struct sockaddr_hci addr; memset(&addr, 0, sizeof(addr)); @@ -173,7 +177,10 @@ int BluetoothHci::waitHciDev(int hci_interface) { break; } - if (ev.opcode == MGMT_EV_INDEX_ADDED && ev.index == hci_interface) { + if (ev.opcode == MGMT_EV_INDEX_ADDED) { + if (hci_interface != ev.index) { + ret = ev.index; + } goto end; } else if (ev.opcode == MGMT_EV_COMMAND_COMP) { struct mgmt_event_read_index* cc; @@ -186,6 +193,10 @@ int BluetoothHci::waitHciDev(int hci_interface) { for (i = 0; i < cc->num_intf; i++) { if (cc->index[i] == hci_interface) goto end; } + if (cc->num_intf > 0) { + ret = cc->index[0]; + goto end; + } } } } diff --git a/packages/modules/Bluetooth/system/vendor_libs/linux/interface/h4_protocol.cc b/packages/modules/Bluetooth/system/vendor_libs/linux/interface/h4_protocol.cc index 2ff11fe27f0..073ce91f0aa 100644 --- a/packages/modules/Bluetooth/system/vendor_libs/linux/interface/h4_protocol.cc +++ b/packages/modules/Bluetooth/system/vendor_libs/linux/interface/h4_protocol.cc @@ -40,6 +40,11 @@ H4Protocol::H4Protocol(int fd, PacketReadCallback event_cb, disconnect_cb_(std::move(disconnect_cb)) {} size_t H4Protocol::Send(uint8_t type, const uint8_t* data, size_t length) { + if (disconnected_) { + ALOGE("%s error writing to disconnected device", __func__); + return 0; + } + /* For HCI communication over USB dongle, multiple write results in * response timeout as driver expect type + data at once to process * the command, so using "writev"(for atomicity) here. @@ -126,6 +131,11 @@ void H4Protocol::OnDataReady(int fd) { } if (bytes_read < 0) { ALOGW("error reading from UART (%s)", strerror(errno)); + if (errno == EPIPE) { + disconnected_ = true; + disconnect_cb_(); + // LOG_ALWAYS_FATAL("HCI device disconnected"); + } return; } send_data_to_packetizer(buffer, bytes_read);