From 412e2899494c23b695ad8d88676ae5033ee7317d Mon Sep 17 00:00:00 2001 From: Konsta Date: Sat, 16 Nov 2024 22:54:39 +0200 Subject: [PATCH] bluetooth: remove serial implementation * Remove serial bluetooth implementation that is used on cuttlefish as we're only using the HCI socket implementation on Raspberry Pi. --- bluetooth/Android.bp | 1 - bluetooth/BluetoothHci.cpp | 150 ++----------------------------------- bluetooth/BluetoothHci.h | 15 +--- 3 files changed, 7 insertions(+), 159 deletions(-) diff --git a/bluetooth/Android.bp b/bluetooth/Android.bp index 101d3de..5c45eed 100644 --- a/bluetooth/Android.bp +++ b/bluetooth/Android.bp @@ -22,7 +22,6 @@ cc_binary { "android.hardware.bluetooth-V1-ndk", "libbase", "libbinder_ndk", - "libcutils", "libhidlbase", "liblog", "libutils", diff --git a/bluetooth/BluetoothHci.cpp b/bluetooth/BluetoothHci.cpp index 67ae19c..6ad6147 100644 --- a/bluetooth/BluetoothHci.cpp +++ b/bluetooth/BluetoothHci.cpp @@ -19,30 +19,8 @@ #include "BluetoothHci.h" -#include -#include -#include -#include -#include -#include -#include -#include - #include "log/log.h" -namespace { -int SetTerminalRaw(int fd) { - termios terminal_settings; - int rval = tcgetattr(fd, &terminal_settings); - if (rval < 0) { - return rval; - } - cfmakeraw(&terminal_settings); - rval = tcsetattr(fd, TCSANOW, &terminal_settings); - return rval; -} -} // namespace - using namespace ::android::hardware::bluetooth::hci; using namespace ::android::hardware::bluetooth::async; using aidl::android::hardware::bluetooth::Status; @@ -51,19 +29,6 @@ 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) {} @@ -112,92 +77,10 @@ void OnDeath(void* cookie) { death_recipient->serviceDied(); } -BluetoothHci::BluetoothHci(const std::string& dev_path) { - char property_bytes[PROPERTY_VALUE_MAX]; - property_get("vendor.ser.bt-uart", property_bytes, dev_path.c_str()); - mDevPath = std::string(property_bytes); +BluetoothHci::BluetoothHci() { mDeathRecipient = std::make_shared(this); } -int BluetoothHci::getFdFromDevPath() { - int fd = open(mDevPath.c_str(), O_RDWR); - if (fd < 0) { - ALOGE("Could not connect to bt: %s (%s)", mDevPath.c_str(), - strerror(errno)); - return fd; - } - if (int ret = SetTerminalRaw(fd) < 0) { - ALOGI("Could not make %s a raw terminal %d(%s)", mDevPath.c_str(), ret, - strerror(errno)); - } - return fd; -} - -void BluetoothHci::reset() { - // Send a reset command and wait until the command complete comes back. - - std::vector reset = {0x03, 0x0c, 0x00}; - - auto resetPromise = std::make_shared>(); - auto resetFuture = resetPromise->get_future(); - - mH4 = std::make_shared( - mFd, - [](const std::vector& raw_command) { - ALOGI("Discarding %d bytes with command type", - static_cast(raw_command.size())); - }, - [](const std::vector& raw_acl) { - ALOGI("Discarding %d bytes with acl type", - static_cast(raw_acl.size())); - }, - [](const std::vector& raw_sco) { - ALOGI("Discarding %d bytes with sco type", - static_cast(raw_sco.size())); - }, - [resetPromise](const std::vector& raw_event) { - std::vector reset_complete = {0x0e, 0x04, 0x01, - 0x03, 0x0c, 0x00}; - bool valid = raw_event.size() == 6 && - raw_event[0] == reset_complete[0] && - raw_event[1] == reset_complete[1] && - // Don't compare the number of packets field. - raw_event[3] == reset_complete[3] && - raw_event[4] == reset_complete[4] && - raw_event[5] == reset_complete[5]; - if (valid) { - resetPromise->set_value(); - } else { - ALOGI("Discarding %d bytes with event type", - static_cast(raw_event.size())); - } - }, - [](const std::vector& raw_iso) { - ALOGI("Discarding %d bytes with iso type", - static_cast(raw_iso.size())); - }, - [this]() { - ALOGI("HCI socket device disconnected while waiting for reset"); - mFdWatcher.StopWatchingFileDescriptors(); - }); - mFdWatcher.WatchFdForNonBlockingReads(mFd, - [this](int) { mH4->OnDataReady(); }); - - ndk::ScopedAStatus result = send(PacketType::COMMAND, reset); - if (!result.isOk()) { - ALOGE("Error sending reset command"); - } - auto status = resetFuture.wait_for(std::chrono::seconds(1)); - mFdWatcher.StopWatchingFileDescriptors(); - if (status == std::future_status::ready) { - ALOGI("HCI Reset successful"); - } else { - ALOGE("HCI Reset Response not received in one second"); - } - - resetPromise.reset(); -} - ndk::ScopedAStatus BluetoothHci::initialize( const std::shared_ptr& cb) { ALOGI(__func__); @@ -230,31 +113,14 @@ ndk::ScopedAStatus BluetoothHci::initialize( if (mFd < 0) { management_.reset(); - ALOGI("Unable to open Linux interface, trying default path."); - mFd = getFdFromDevPath(); - if (mFd < 0) { - mState = HalState::READY; - cb->initializationComplete(Status::UNABLE_TO_OPEN_INTERFACE); - return ndk::ScopedAStatus::ok(); - } + ALOGI("Unable to open Linux interface."); + mState = HalState::READY; + cb->initializationComplete(Status::UNABLE_TO_OPEN_INTERFACE); + return ndk::ScopedAStatus::ok(); } mDeathRecipient->LinkToDeath(mCb); - // 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, [](const std::vector& /* raw_command */) { @@ -312,11 +178,7 @@ ndk::ScopedAStatus BluetoothHci::close() { mFdWatcher.StopWatchingFileDescriptors(); - if (management_) { - management_->closeHci(); - } else { - ::close(mFd); - } + management_->closeHci(); { std::lock_guard guard(mStateMutex); diff --git a/bluetooth/BluetoothHci.h b/bluetooth/BluetoothHci.h index 2050555..a27316c 100644 --- a/bluetooth/BluetoothHci.h +++ b/bluetooth/BluetoothHci.h @@ -20,9 +20,6 @@ #include #include -#include -#include - #include "async_fd_watcher.h" #include "h4_protocol.h" #include "net_bluetooth_mgmt.h" @@ -34,7 +31,7 @@ class BluetoothDeathRecipient; // This Bluetooth HAL implementation connects with a serial port at dev_path_. class BluetoothHci : public BnBluetoothHci { public: - BluetoothHci(const std::string& dev_path = "/dev/hvc5"); + BluetoothHci(); ndk::ScopedAStatus initialize( const std::shared_ptr& cb) override; @@ -50,10 +47,6 @@ class BluetoothHci : public BnBluetoothHci { ndk::ScopedAStatus close() override; - static void OnPacketReady(); - - static BluetoothHci* get(); - private: int mFd{-1}; std::shared_ptr mCb = nullptr; @@ -62,19 +55,13 @@ class BluetoothHci : public BnBluetoothHci { std::shared_ptr mDeathRecipient; - std::string mDevPath; - ::android::hardware::bluetooth::async::AsyncFdWatcher mFdWatcher; - int getFdFromDevPath(); [[nodiscard]] ndk::ScopedAStatus send( ::android::hardware::bluetooth::hci::PacketType type, const std::vector& packet); std::unique_ptr management_{}; - // Send a reset command and discard all packets until a reset is received. - void reset(); - // Don't close twice or open before close is complete std::mutex mStateMutex; enum class HalState {