Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 63c25994 authored by Antoine Soulier's avatar Antoine Soulier Committed by Gerrit Code Review
Browse files

Merge changes from topic "new_proxy_header" into main

* changes:
  LE Audio Software offload: New HAL Proxy integration
  Revert "HCI HAL: HCI Proxy Audio Software offload integration"
  Revert "LE Audio Software offload: Handle death of client"
parents 5637e2b5 35fef7c0
Loading
Loading
Loading
Loading
+8 −0
Original line number Original line Diff line number Diff line
@@ -16,6 +16,13 @@ aidl_interface {
    srcs: ["android/hardware/bluetooth/*.aidl"],
    srcs: ["android/hardware/bluetooth/*.aidl"],
    stability: "vintf",
    stability: "vintf",
    backend: {
    backend: {
        cpp: {
            // FIXME should this be disabled?
            // prefer NDK backend which can be used anywhere
            // If you disable this, you also need to delete the C++
            // translate code.
            enabled: true,
        },
        rust: {
        rust: {
            enabled: true,
            enabled: true,
        },
        },
@@ -37,4 +44,5 @@ aidl_interface {
        },
        },
    ],
    ],
    frozen: true,
    frozen: true,

}
}
+31 −34
Original line number Original line Diff line number Diff line
@@ -2,61 +2,58 @@ package {
    default_applicable_licenses: ["Android-Apache-2.0"],
    default_applicable_licenses: ["Android-Apache-2.0"],
}
}


cc_library_static {
cc_defaults {
    name: "libbluetoothhcihalimpl",
    name: "android.hardware.bluetooth-service-build-defaults",
    vendor_available: true,
    host_supported: true,
    srcs: [
        "BluetoothHci.cpp",
        "net_bluetooth_mgmt.cpp",
    ],
    cflags: [
    cflags: [
        "-Wall",
        "-Wall",
        "-Wextra",
        "-Wextra",
    ],
    ],
    header_libs: [
        "libbluetooth_offload_hal_headers",
    ],
    static_libs: [
        "android.hardware.bluetooth.async",
        "android.hardware.bluetooth.hci",
    ],
    shared_libs: [
    shared_libs: [
        "android.hardware.bluetooth-V1-ndk",
        "libbase",
        "libbase",
        "libbinder_ndk",
        "libcutils",
        "libcutils",
        "libhidlbase",
        "liblog",
        "liblog",
        "libutils",
        "libutils",
    ],
    ],
    static_libs: [
        "android.hardware.bluetooth.async",
        "android.hardware.bluetooth.hci",
        "libbluetooth_offload_hal",
    ],
}
}


rust_binary {
cc_library_static {
    name: "libbluetoothhcihalimpl",
    vendor_available: true,
    defaults: ["android.hardware.bluetooth-service-build-defaults"],
    srcs: [
        "BluetoothHci.cpp",
        "net_bluetooth_mgmt.cpp",
    ],
}

cc_binary {
    name: "android.hardware.bluetooth-service.default",
    name: "android.hardware.bluetooth-service.default",
    crate_name: "bluetooth_hci_hal_server",
    relative_install_path: "hw",
    relative_install_path: "hw",
    init_rc: ["bluetooth-service-default.rc"],
    init_rc: ["bluetooth-service-default.rc"],
    vintf_fragments: [":manifest_android.hardware.bluetooth-service.default.xml"],
    vintf_fragments: [":manifest_android.hardware.bluetooth-service.default.xml"],
    vendor: true,
    vendor: true,
    prefer_rlib: true,
    defaults: ["android.hardware.bluetooth-service-build-defaults"],
    srcs: ["main.rs"],
    srcs: [
    rustlibs: [
        "service.cpp",
        "android.hardware.bluetooth-V1-rust",
        "libbluetooth_offload_hal",
        "libbluetooth_offload_leaudio_hci",
        "libbinder_rs",
        "liblogger",
        "liblog_rust",
    ],
    static_libs: [
        "android.hardware.bluetooth.async",
        "android.hardware.bluetooth.hci",
        "libbluetoothhcihalimpl",
    ],
    ],
    shared_libs: [
    shared_libs: [
        "android.hardware.bluetooth-V1-ndk",
        "libbase",
        "libbase",
        "libc++",
        "libbinder_ndk",
        "libcutils",
        "libhidlbase",
        "liblog",
        "libutils",
        "libutils",
        "liblog",
    ],
    static_libs: [
        "libbluetoothhcihalimpl",
    ],
    ],
}
}


+202 −277
Original line number Original line Diff line number Diff line
/*
/*
 * Copyright 2024 The Android Open Source Project
 * Copyright 2022 The Android Open Source Project
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * you may not use this file except in compliance with the License.
@@ -16,20 +16,18 @@


#define LOG_TAG "android.hardware.bluetooth.service.default"
#define LOG_TAG "android.hardware.bluetooth.service.default"


#include "BluetoothHci.h"

#include <cutils/properties.h>
#include <cutils/properties.h>
#include <fcntl.h>
#include <fcntl.h>
#include <hal/ffi.h>
#include <netdb.h>
#include <netinet/in.h>
#include <poll.h>
#include <string.h>
#include <string.h>
#include <sys/uio.h>
#include <termios.h>
#include <termios.h>


#include <future>
#include <memory>
#include <vector>

#include "async_fd_watcher.h"
#include "h4_protocol.h"
#include "log/log.h"
#include "log/log.h"
#include "net_bluetooth_mgmt.h"


namespace {
namespace {
int SetTerminalRaw(int fd) {
int SetTerminalRaw(int fd) {
@@ -46,6 +44,7 @@ int SetTerminalRaw(int fd) {


using namespace ::android::hardware::bluetooth::hci;
using namespace ::android::hardware::bluetooth::hci;
using namespace ::android::hardware::bluetooth::async;
using namespace ::android::hardware::bluetooth::async;
using aidl::android::hardware::bluetooth::hal::Status;


namespace aidl::android::hardware::bluetooth::impl {
namespace aidl::android::hardware::bluetooth::impl {


@@ -62,47 +61,13 @@ bool starts_with(const std::string& str, const std::string& prefix) {
  return str.compare(0, prefix.length(), prefix) == 0;
  return str.compare(0, prefix.length(), prefix) == 0;
}
}


class Hal {
BluetoothHci::BluetoothHci(const std::string& dev_path) {
 public:
  Hal(const std::string& dev_path = "/dev/hvc5") {
  char property_bytes[PROPERTY_VALUE_MAX];
  char property_bytes[PROPERTY_VALUE_MAX];
  property_get("vendor.ser.bt-uart", property_bytes, dev_path.c_str());
  property_get("vendor.ser.bt-uart", property_bytes, dev_path.c_str());
  mDevPath = std::string(property_bytes);
  mDevPath = std::string(property_bytes);
}
}


  static void Initialize(void* instance,
int BluetoothHci::getFdFromDevPath() {
                         const struct hal_callbacks* callbacks) {
    static_cast<Hal*>(instance)->Initialize(callbacks);
  }

  static void Close(void* instance) { static_cast<Hal*>(instance)->Close(); }

  static void SendCommand(void* instance, const uint8_t* data, size_t len) {
    static_cast<Hal*>(instance)->SendCommand(
        std::vector<uint8_t>(data, data + len));
  }

  static void SendAcl(void* instance, const uint8_t* data, size_t len) {
    static_cast<Hal*>(instance)->SendAcl(
        std::vector<uint8_t>(data, data + len));
  }

  static void SendSco(void* instance, const uint8_t* data, size_t len) {
    static_cast<Hal*>(instance)->SendSco(
        std::vector<uint8_t>(data, data + len));
  }

  static void SendIso(void* instance, const uint8_t* data, size_t len) {
    static_cast<Hal*>(instance)->SendIso(
        std::vector<uint8_t>(data, data + len));
  }

  static void ClientDied(void* instance) {
    static_cast<Hal*>(instance)->ClientDied();
  }

 private:
  int getFdFromDevPath() {
  int fd = open(mDevPath.c_str(), O_RDWR);
  int fd = open(mDevPath.c_str(), O_RDWR);
  if (fd < 0) {
  if (fd < 0) {
    ALOGE("Could not connect to bt: %s (%s)", mDevPath.c_str(),
    ALOGE("Could not connect to bt: %s (%s)", mDevPath.c_str(),
@@ -116,7 +81,7 @@ class Hal {
  return fd;
  return fd;
}
}


  void reset() {
void BluetoothHci::reset() {
  // Send a reset command and wait until the command complete comes back.
  // Send a reset command and wait until the command complete comes back.


  std::vector<uint8_t> reset = {0x03, 0x0c, 0x00};
  std::vector<uint8_t> reset = {0x03, 0x0c, 0x00};
@@ -166,9 +131,7 @@ class Hal {
  mFdWatcher.WatchFdForNonBlockingReads(mFd,
  mFdWatcher.WatchFdForNonBlockingReads(mFd,
                                        [this](int) { mH4->OnDataReady(); });
                                        [this](int) { mH4->OnDataReady(); });


    if (!send(PacketType::COMMAND, reset)) {
  send(PacketType::COMMAND, reset);
      ALOGE("Error sending reset command");
    }
  auto status = resetFuture.wait_for(std::chrono::seconds(1));
  auto status = resetFuture.wait_for(std::chrono::seconds(1));
  mFdWatcher.StopWatchingFileDescriptors();
  mFdWatcher.StopWatchingFileDescriptors();
  if (status == std::future_status::ready) {
  if (status == std::future_status::ready) {
@@ -180,9 +143,15 @@ class Hal {
  resetPromise.reset();
  resetPromise.reset();
}
}


  void Initialize(const struct hal_callbacks* callbacks) {
void BluetoothHci::initialize(
    const std::shared_ptr<hal::IBluetoothHciCallbacks>& cb) {
  ALOGI(__func__);
  ALOGI(__func__);


  if (cb == nullptr) {
    ALOGE("cb == nullptr! -> Unable to call initializationComplete(ERR)");
    abort();
  }

  HalState old_state = HalState::READY;
  HalState old_state = HalState::READY;
  {
  {
    std::lock_guard<std::mutex> guard(mStateMutex);
    std::lock_guard<std::mutex> guard(mStateMutex);
@@ -195,13 +164,11 @@ class Hal {


  if (old_state != HalState::READY) {
  if (old_state != HalState::READY) {
    ALOGE("initialize: Unexpected State %d", static_cast<int>(old_state));
    ALOGE("initialize: Unexpected State %d", static_cast<int>(old_state));
      Close();
    close();
      callbacks->initialization_complete(callbacks->handle,
    cb->initializationComplete(Status::ALREADY_INITIALIZED);
                                         STATUS_ALREADY_INITIALIZED);
      return;
  }
  }


    mCallbacks = std::make_unique<struct hal_callbacks>(*callbacks);
  mCb = cb;
  management_.reset(new NetBluetoothMgmt);
  management_.reset(new NetBluetoothMgmt);
  mFd = management_->openHci();
  mFd = management_->openHci();
  if (mFd < 0) {
  if (mFd < 0) {
@@ -211,9 +178,7 @@ class Hal {
    mFd = getFdFromDevPath();
    mFd = getFdFromDevPath();
    if (mFd < 0) {
    if (mFd < 0) {
      mState = HalState::READY;
      mState = HalState::READY;
        mCallbacks->initialization_complete(mCallbacks->handle,
      cb->initializationComplete(Status::UNABLE_TO_OPEN_INTERFACE);
                                            STATUS_UNABLE_TO_OPEN_INTERFACE);
        return;
    }
    }
  }
  }


@@ -225,8 +190,8 @@ class Hal {
  const std::string kBoardProperty = "ro.product.board";
  const std::string kBoardProperty = "ro.product.board";
  const std::string kCuttlefishBoard = "cutf";
  const std::string kCuttlefishBoard = "cutf";
  auto board_name = GetSystemProperty(kBoardProperty);
  auto board_name = GetSystemProperty(kBoardProperty);
    if (board_name.has_value() &&
  if (board_name.has_value() && (
        (starts_with(board_name.value(), "cutf") ||
        starts_with(board_name.value(), "cutf") ||
        starts_with(board_name.value(), "goldfish"))) {
        starts_with(board_name.value(), "goldfish"))) {
    reset();
    reset();
  }
  }
@@ -237,20 +202,16 @@ class Hal {
        LOG_ALWAYS_FATAL("Unexpected command!");
        LOG_ALWAYS_FATAL("Unexpected command!");
      },
      },
      [this](const std::vector<uint8_t>& raw_acl) {
      [this](const std::vector<uint8_t>& raw_acl) {
          mCallbacks->acl_received(mCallbacks->handle, raw_acl.data(),
        mCb->aclDataReceived(raw_acl);
                                   raw_acl.size());
      },
      },
      [this](const std::vector<uint8_t>& raw_sco) {
      [this](const std::vector<uint8_t>& raw_sco) {
          mCallbacks->sco_received(mCallbacks->handle, raw_sco.data(),
        mCb->scoDataReceived(raw_sco);
                                   raw_sco.size());
      },
      },
      [this](const std::vector<uint8_t>& raw_event) {
      [this](const std::vector<uint8_t>& raw_event) {
          mCallbacks->event_received(mCallbacks->handle, raw_event.data(),
        mCb->hciEventReceived(raw_event);
                                     raw_event.size());
      },
      },
      [this](const std::vector<uint8_t>& raw_iso) {
      [this](const std::vector<uint8_t>& raw_iso) {
          mCallbacks->iso_received(mCallbacks->handle, raw_iso.data(),
        mCb->isoDataReceived(raw_iso);
                                   raw_iso.size());
      },
      },
      [this]() {
      [this]() {
        ALOGI("HCI socket device disconnected");
        ALOGI("HCI socket device disconnected");
@@ -263,12 +224,11 @@ class Hal {
    std::lock_guard<std::mutex> guard(mStateMutex);
    std::lock_guard<std::mutex> guard(mStateMutex);
    mState = HalState::ONE_CLIENT;
    mState = HalState::ONE_CLIENT;
  }
  }

  ALOGI("initialization complete");
  ALOGI("initialization complete");
    mCallbacks->initialization_complete(mCallbacks->handle, STATUS_SUCCESS);
  mCb->initializationComplete(Status::SUCCESS);
}
}


  void Close() {
void BluetoothHci::close() {
  ALOGI(__func__);
  ALOGI(__func__);
  {
  {
    std::lock_guard<std::mutex> guard(mStateMutex);
    std::lock_guard<std::mutex> guard(mStateMutex);
@@ -276,9 +236,7 @@ class Hal {
      LOG_ALWAYS_FATAL_IF(mState == HalState::INITIALIZING,
      LOG_ALWAYS_FATAL_IF(mState == HalState::INITIALIZING,
                          "mState is INITIALIZING");
                          "mState is INITIALIZING");
      ALOGI("Already closed");
      ALOGI("Already closed");
        return;
    }
    }
      mCallbacks.reset();
    mState = HalState::CLOSING;
    mState = HalState::CLOSING;
  }
  }


@@ -297,73 +255,40 @@ class Hal {
  }
  }
}
}


  void SendCommand(const std::vector<uint8_t>& data) {
void BluetoothHci::clientDied() {
    send(PacketType::COMMAND, data);
  ALOGI(__func__);
  close();
}
}
  void SendAcl(const std::vector<uint8_t>& data) {

    send(PacketType::ACL_DATA, data);
void BluetoothHci::sendHciCommand(const std::vector<uint8_t>& packet) {
  return send(PacketType::COMMAND, packet);
}
}
  void SendSco(const std::vector<uint8_t>& data) {

    send(PacketType::SCO_DATA, data);
void BluetoothHci::sendAclData(const std::vector<uint8_t>& packet) {
  return send(PacketType::ACL_DATA, packet);
}
}
  void SendIso(const std::vector<uint8_t>& data) {

    send(PacketType::ISO_DATA, data);
void BluetoothHci::sendScoData(const std::vector<uint8_t>& packet) {
  return send(PacketType::SCO_DATA, packet);
}

void BluetoothHci::sendIsoData(const std::vector<uint8_t>& packet) {
  return send(PacketType::ISO_DATA, packet);
}
}


  bool send(PacketType type, const std::vector<uint8_t>& v) {
void BluetoothHci::send(PacketType type, const std::vector<uint8_t>& v) {
  if (v.empty()) {
  if (v.empty()) {
    ALOGE("Packet is empty, no data was found to be sent");
    ALOGE("Packet is empty, no data was found to be sent");
      return false;
    abort();
  }
  }


  std::lock_guard<std::mutex> guard(mStateMutex);
  std::lock_guard<std::mutex> guard(mStateMutex);
  if (mH4 == nullptr) {
  if (mH4 == nullptr) {
    ALOGE("Illegal State");
    ALOGE("Illegal State");
      return false;
    abort();
  }
  }


  mH4->Send(type, v);
  mH4->Send(type, v);
    return true;
}
}


  void ClientDied() {
    ALOGE("Bluetooth client has died");
    Close();
  }

  std::unique_ptr<struct hal_callbacks> mCallbacks;
  std::string mDevPath;
  int mFd{-1};
  ::android::hardware::bluetooth::async::AsyncFdWatcher mFdWatcher;
  std::shared_ptr<::android::hardware::bluetooth::hci::H4Protocol> mH4;
  std::unique_ptr<NetBluetoothMgmt> management_{};

  // Don't close twice or open before close is complete
  std::mutex mStateMutex;
  enum class HalState {
    READY,
    INITIALIZING,
    ONE_CLIENT,
    CLOSING,
  } mState{HalState::READY};
};

}  // namespace aidl::android::hardware::bluetooth::impl
}  // namespace aidl::android::hardware::bluetooth::impl

extern "C" {

using namespace aidl::android::hardware::bluetooth::impl;

struct hal_interface hal_new() {
  return (struct hal_interface){
      .handle = new Hal(),
      .initialize = &Hal::Initialize,
      .close = &Hal::Close,
      .send_command = &Hal::SendCommand,
      .send_acl = &Hal::SendAcl,
      .send_sco = &Hal::SendSco,
      .send_iso = &Hal::SendIso,
      .client_died = &Hal::ClientDied,
  };
}
}
+82 −0
Original line number Original line Diff line number Diff line
/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <hal/ffi.h>

#include <future>
#include <string>

#include "async_fd_watcher.h"
#include "h4_protocol.h"
#include "net_bluetooth_mgmt.h"

namespace aidl::android::hardware::bluetooth::impl {

// This Bluetooth HAL implementation connects with a serial port at dev_path_.
class BluetoothHci : public hal::IBluetoothHci {
 public:
  BluetoothHci(const std::string& dev_path = "/dev/hvc5");

  void initialize(
      const std::shared_ptr<hal::IBluetoothHciCallbacks>& cb) override;

  void sendHciCommand(const std::vector<uint8_t>& packet) override;

  void sendAclData(const std::vector<uint8_t>& packet) override;

  void sendScoData(const std::vector<uint8_t>& packet) override;

  void sendIsoData(const std::vector<uint8_t>& packet) override;

  void close() override;

  void clientDied() override;

  static void OnPacketReady();

  static BluetoothHci* get();

 private:
  int mFd{-1};
  std::shared_ptr<hal::IBluetoothHciCallbacks> mCb = nullptr;

  std::shared_ptr<::android::hardware::bluetooth::hci::H4Protocol> mH4;

  std::string mDevPath;

  ::android::hardware::bluetooth::async::AsyncFdWatcher mFdWatcher;

  int getFdFromDevPath();
  void send(::android::hardware::bluetooth::hci::PacketType type,
            const std::vector<uint8_t>& packet);
  std::unique_ptr<NetBluetoothMgmt> 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 {
    READY,
    INITIALIZING,
    ONE_CLIENT,
    CLOSING,
  } mState{HalState::READY};
};

}  // namespace aidl::android::hardware::bluetooth::impl

bluetooth/aidl/default/main.rs

deleted100644 → 0
+0 −58
Original line number Original line Diff line number Diff line
// Copyright 2024, The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use android_hardware_bluetooth::aidl::android::hardware::bluetooth::IBluetoothHci::{
    self,
    IBluetoothHci as _
};
use android_hardware_bluetooth::binder;
use bluetooth_offload_hal::{ HciHalProxy, CInterface };
use bluetooth_offload_leaudio_hci::LeAudioModuleBuilder;
use log;
use std::panic;

fn new_hal() -> CInterface {
    extern "C" { fn hal_new() -> CInterface; }
    unsafe { hal_new() }
}

fn main() {
    logger::init(
        logger::Config::default()
            .with_max_level(log::LevelFilter::Debug)
            .with_tag_on_device("android.hardware.bluetooth"),
    );

    panic::set_hook(Box::new(|panic_info| {
        log::error!("{}", panic_info);
    }));

    log::info!("Bluetooth HAL starting up");

    binder::ProcessState::set_thread_pool_max_thread_count(0);
    binder::ProcessState::start_thread_pool();

    binder::add_service(
        &format!("{}/default", IBluetoothHci::BpBluetoothHci::get_descriptor()),
        IBluetoothHci::BnBluetoothHci::new_binder(
            HciHalProxy::new(
                vec![ Box::new(LeAudioModuleBuilder {}) ],
                new_hal()
            ),
            binder::BinderFeatures::default(),
        ).as_binder()
    ).expect("Failed to register service");

    binder::ProcessState::join_thread_pool();
}
Loading