From 1267ff2c46b324ba44894cf5564018f4356d8347 Mon Sep 17 00:00:00 2001 From: Henri Chataing Date: Wed, 9 Aug 2023 22:38:24 +0000 Subject: [PATCH] bluetooth/aidl: Enable HCI reset in default hal only when running on emulator devices Bug: 290329516 Test: m Change-Id: I624f26824372fb708598e6be322adf06e40cb5a2 --- bluetooth/aidl/default/BluetoothHci.cpp | 28 +++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/bluetooth/aidl/default/BluetoothHci.cpp b/bluetooth/aidl/default/BluetoothHci.cpp index 782122f75f..013ab7f874 100644 --- a/bluetooth/aidl/default/BluetoothHci.cpp +++ b/bluetooth/aidl/default/BluetoothHci.cpp @@ -55,6 +55,19 @@ namespace aidl::android::hardware::bluetooth::impl { void OnDeath(void* cookie); +std::optional GetSystemProperty(const std::string& property) { + std::array value_array{0}; + auto value_len = property_get(property.c_str(), value_array.data(), nullptr); + if (value_len <= 0) { + return std::nullopt; + } + return std::string(value_array.data(), value_len); +} + +bool starts_with(const std::string& str, const std::string& prefix) { + return str.compare(0, prefix.length(), prefix) == 0; +} + class BluetoothDeathRecipient { public: BluetoothDeathRecipient(BluetoothHci* hci) : mHci(hci) {} @@ -232,8 +245,19 @@ ndk::ScopedAStatus BluetoothHci::initialize( mDeathRecipient->LinkToDeath(mCb); - // TODO: This should not be necessary when the device implements rfkill. - reset(); + // TODO: HCI Reset on emulators since the bluetooth controller + // cannot be powered on/off during the HAL setup; and the stack + // might received spurious packets/events during boottime. + // Proper solution would be to use bt-virtio or vsock to better + // control the link to rootcanal and the controller lifetime. + const std::string kBoardProperty = "ro.product.board"; + const std::string kCuttlefishBoard = "cutf"; + auto board_name = GetSystemProperty(kBoardProperty); + if (board_name.has_value() && ( + starts_with(board_name.value(), "cutf") || + starts_with(board_name.value(), "goldfish"))) { + reset(); + } mH4 = std::make_shared( mFd,