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

Commit 22ba5f16 authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Implement A2DP sink and AVRCP controller binder server"

parents dfebf34e 69986fe3
Loading
Loading
Loading
Loading
+9 −1
Original line number Original line Diff line number Diff line
@@ -14,11 +14,15 @@ cc_defaults {
// Source variables
// Source variables
// ========================================================
// ========================================================
btserviceDaemonSrc = [
btserviceDaemonSrc = [
    "a2dp_sink.cc",
    "adapter.cc",
    "adapter.cc",
    "avrcp_control.cc",
    "daemon.cc",
    "daemon.cc",
    "gatt_client.cc",
    "gatt_client.cc",
    "gatt_server.cc",
    "gatt_server.cc",
    "gatt_server_old.cc",
    "gatt_server_old.cc",
    "hal/bluetooth_av_interface.cc",
    "hal/bluetooth_avrcp_interface.cc",
    "hal/bluetooth_gatt_interface.cc",
    "hal/bluetooth_gatt_interface.cc",
    "hal/bluetooth_interface.cc",
    "hal/bluetooth_interface.cc",
    "ipc/ipc_handler.cc",
    "ipc/ipc_handler.cc",
@@ -36,6 +40,8 @@ btserviceLinuxSrc = [
]
]


btserviceBinderDaemonSrc = [
btserviceBinderDaemonSrc = [
    "ipc/binder/bluetooth_a2dp_sink_binder_server.cc",
    "ipc/binder/bluetooth_avrcp_control_binder_server.cc",
    "ipc/binder/bluetooth_binder_server.cc",
    "ipc/binder/bluetooth_binder_server.cc",
    "ipc/binder/bluetooth_gatt_client_binder_server.cc",
    "ipc/binder/bluetooth_gatt_client_binder_server.cc",
    "ipc/binder/bluetooth_gatt_server_binder_server.cc",
    "ipc/binder/bluetooth_gatt_server_binder_server.cc",
@@ -49,8 +55,10 @@ btserviceBinderDaemonSrc = [
// Main unit test sources. These get built for host and target.
// Main unit test sources. These get built for host and target.
// ========================================================
// ========================================================
btserviceBaseTestSrc = [
btserviceBaseTestSrc = [
    "hal/fake_bluetooth_av_interface.cc",
    "hal/fake_bluetooth_gatt_interface.cc",
    "hal/fake_bluetooth_gatt_interface.cc",
    "hal/fake_bluetooth_interface.cc",
    "hal/fake_bluetooth_interface.cc",
    "test/a2dp_sink_unittest.cc",
    "test/adapter_unittest.cc",
    "test/adapter_unittest.cc",
    "test/advertise_data_unittest.cc",
    "test/advertise_data_unittest.cc",
    "test/fake_hal_util.cc",
    "test/fake_hal_util.cc",
@@ -109,6 +117,7 @@ cc_test {
        "libgmock",
        "libgmock",
        "liblog",
        "liblog",
        "libbluetooth-types",
        "libbluetooth-types",
        "libutils",
    ],
    ],
    host_supported: true,
    host_supported: true,
    target: {
    target: {
@@ -124,7 +133,6 @@ cc_test {
            ],
            ],
            shared_libs: [
            shared_libs: [
                "libbinder",
                "libbinder",
                "libutils",
            ],
            ],
        },
        },
        host: {
        host: {
+154 −0
Original line number Original line Diff line number Diff line
//
//  Copyright 2017 Google, Inc.
//
//  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.
//

#include "service/a2dp_sink.h"

#include <base/logging.h>
#include <base/memory/ptr_util.h>

#include "service/logging_helpers.h"

using bluetooth::hal::BluetoothAvInterface;

namespace bluetooth {

// static
const int A2dpSink::kSingletonInstanceId = 0;

A2dpSink::A2dpSink(const Uuid& uuid) : app_identifier_(uuid) {
  hal::BluetoothAvInterface::Get()->AddA2dpSinkObserver(this);
}

A2dpSink::~A2dpSink() {
  hal::BluetoothAvInterface::Get()->RemoveA2dpSinkObserver(this);
}

const Uuid& A2dpSink::GetAppIdentifier() const { return app_identifier_; }

int A2dpSink::GetInstanceId() const { return kSingletonInstanceId; }

void A2dpSink::SetDelegate(Delegate* delegate) {
  std::lock_guard<std::mutex> lock(delegate_mutex_);
  delegate_ = delegate;
}

bool A2dpSink::Enable() {
  std::lock_guard<std::mutex> lock(mutex_);
  return hal::BluetoothAvInterface::Get()->A2dpSinkEnable();
}

void A2dpSink::Disable() {
  std::lock_guard<std::mutex> lock(mutex_);
  hal::BluetoothAvInterface::Get()->A2dpSinkDisable();
}

bool A2dpSink::Connect(const std::string& device_address) {
  std::lock_guard<std::mutex> lock(mutex_);
  RawAddress addr;
  if (!RawAddress::FromString(device_address, addr)) {
    LOG(ERROR) << "Invalid device address given: " << device_address;
    return false;
  }

  bt_status_t status =
      hal::BluetoothAvInterface::Get()->GetA2dpSinkHALInterface()->connect(
          addr);
  if (status != BT_STATUS_SUCCESS) {
    LOG(ERROR) << "Failed to connect";
    return false;
  }

  return true;
}

bool A2dpSink::Disconnect(const std::string& device_address) {
  std::lock_guard<std::mutex> lock(mutex_);
  RawAddress addr;
  if (!RawAddress::FromString(device_address, addr)) {
    LOG(ERROR) << "Invalid device address given: " << device_address;
    return false;
  }

  bt_status_t status =
      hal::BluetoothAvInterface::Get()->GetA2dpSinkHALInterface()->disconnect(
          addr);
  if (status != BT_STATUS_SUCCESS) {
    LOG(ERROR) << "Failed to disconnect";
    return false;
  }

  return true;
}

void A2dpSink::SetAudioFocusState(int focus_state) {
  std::lock_guard<std::mutex> lock(mutex_);
  hal::BluetoothAvInterface::Get()
      ->GetA2dpSinkHALInterface()
      ->set_audio_focus_state(focus_state);
}

void A2dpSink::SetAudioTrackGain(float gain) {
  std::lock_guard<std::mutex> lock(mutex_);
  hal::BluetoothAvInterface::Get()
      ->GetA2dpSinkHALInterface()
      ->set_audio_track_gain(gain);
}

void A2dpSink::ConnectionStateCallback(BluetoothAvInterface* iface,
                                       const RawAddress& bd_addr,
                                       btav_connection_state_t state) {
  std::string device_address = BtAddrString(&bd_addr);
  std::lock_guard<std::mutex> lock(delegate_mutex_);

  if (delegate_)
    delegate_->OnConnectionState(device_address, static_cast<int>(state));
}

void A2dpSink::AudioStateCallback(BluetoothAvInterface* iface,
                                  const RawAddress& bd_addr,
                                  btav_audio_state_t state) {
  std::string device_address = BtAddrString(&bd_addr);
  std::lock_guard<std::mutex> lock(delegate_mutex_);

  if (delegate_)
    delegate_->OnAudioState(device_address, static_cast<int>(state));
}

void A2dpSink::AudioConfigCallback(BluetoothAvInterface* iface,
                                   const RawAddress& bd_addr,
                                   uint32_t sample_rate,
                                   uint8_t channel_count) {
  std::string device_address = BtAddrString(&bd_addr);
  std::lock_guard<std::mutex> lock(delegate_mutex_);
  if (delegate_)
    delegate_->OnAudioConfig(device_address, sample_rate, channel_count);
}

// A2dpSinkFactory implementation
// ========================================================
A2dpSinkFactory::A2dpSinkFactory() = default;
A2dpSinkFactory::~A2dpSinkFactory() = default;

bool A2dpSinkFactory::RegisterInstance(const Uuid& uuid,
                                       const RegisterCallback& callback) {
  VLOG(1) << __func__ << " - Uuid: " << uuid.ToString();

  auto a2dp_sink = base::WrapUnique(new A2dpSink(uuid));
  callback(BLE_STATUS_SUCCESS, uuid, std::move(a2dp_sink));
  return true;
}

}  // namespace bluetooth
+103 −0
Original line number Original line Diff line number Diff line
//
//  Copyright (C) 2017 Google, Inc.
//
//  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 <atomic>
#include <mutex>
#include <string>
#include <vector>

#include <base/macros.h>

#include "service/bluetooth_instance.h"
#include "service/hal/bluetooth_av_interface.h"

namespace bluetooth {

class A2dpSink : public BluetoothInstance,
                 private hal::BluetoothAvInterface::A2dpSinkObserver {
 public:
  // We only allow one instance of this object at a time.
  static const int kSingletonInstanceId;

  class Delegate {
   public:
    virtual void OnConnectionState(const std::string& device_address,
                                   int state) = 0;
    virtual void OnAudioState(const std::string& device_address, int state) = 0;
    virtual void OnAudioConfig(const std::string& device_address,
                               uint32_t sample_rate, uint8_t channel_count) = 0;

   protected:
    virtual ~Delegate() = default;
  };

  ~A2dpSink() override;

  void SetDelegate(Delegate* delegate);

  // BluetoothInstance implementation:
  const Uuid& GetAppIdentifier() const override;
  int GetInstanceId() const override;

  bool Enable();
  void Disable();
  bool Connect(const std::string& device_address);
  bool Disconnect(const std::string& device_address);
  void SetAudioFocusState(int focus_state);
  void SetAudioTrackGain(float gain);

 private:
  friend class A2dpSinkFactory;

  explicit A2dpSink(const Uuid& uuid);

  // hal::bluetooth::hal::BluetoothAvInterface::Observer implementation:
  void ConnectionStateCallback(bluetooth::hal::BluetoothAvInterface* iface,
                               const RawAddress& bd_addr,
                               btav_connection_state_t state) override;
  void AudioStateCallback(bluetooth::hal::BluetoothAvInterface* iface,
                          const RawAddress& bd_addr,
                          btav_audio_state_t state) override;
  void AudioConfigCallback(bluetooth::hal::BluetoothAvInterface* iface,
                           const RawAddress& bd_addr, uint32_t sample_rate,
                           uint8_t channel_count) override;

  // See getters above for documentation.
  const Uuid app_identifier_;

  std::mutex mutex_;
  std::mutex delegate_mutex_;
  Delegate* delegate_ = nullptr;

  DISALLOW_COPY_AND_ASSIGN(A2dpSink);
};

class A2dpSinkFactory : public BluetoothInstanceFactory {
 public:
  A2dpSinkFactory();
  ~A2dpSinkFactory() override;

  // BluetoothInstanceFactory override:
  bool RegisterInstance(const Uuid& uuid,
                        const RegisterCallback& callback) override;

 private:
  DISALLOW_COPY_AND_ASSIGN(A2dpSinkFactory);
};

}  // namespace bluetooth
+377 −2
Original line number Original line Diff line number Diff line
@@ -24,6 +24,8 @@
#include <base/logging.h>
#include <base/logging.h>
#include <base/observer_list.h>
#include <base/observer_list.h>


#include "service/a2dp_sink.h"
#include "service/avrcp_control.h"
#include "service/common/bluetooth/util/atomic_string.h"
#include "service/common/bluetooth/util/atomic_string.h"
#include "service/gatt_client.h"
#include "service/gatt_client.h"
#include "service/gatt_server.h"
#include "service/gatt_server.h"
@@ -33,11 +35,97 @@
#include "service/low_energy_client.h"
#include "service/low_energy_client.h"
#include "service/low_energy_scanner.h"
#include "service/low_energy_scanner.h"


using android::String16;
using std::lock_guard;
using std::lock_guard;
using std::mutex;
using std::mutex;


namespace bluetooth {
namespace bluetooth {


namespace {

RemoteDeviceProps ParseRemoteDeviceProps(int num_properties,
                                         bt_property_t* properties) {
  android::String16 name;
  android::String16 address;
  std::vector<Uuid> service_uuids;
  int32_t device_class = 0;
  int32_t device_type = 0;
  int32_t rssi = 0;

  for (int i = 0; i < num_properties; ++i) {
    bt_property_t* property = properties + i;
    switch (property->type) {
      case BT_PROPERTY_BDNAME: {
        if (property->len < 0) {
          NOTREACHED() << "Invalid length for BT_PROPERTY_BDNAME";
          break;
        }
        bt_bdname_t* hal_name = reinterpret_cast<bt_bdname_t*>(property->val);
        name = String16(reinterpret_cast<char*>(hal_name->name));
        break;
      }
      case BT_PROPERTY_BDADDR: {
        if (property->len != sizeof(RawAddress)) {
          NOTREACHED() << "Invalid length for BT_PROPERTY_BDADDR";
          break;
        }
        std::string remote_bdaddr_str =
            BtAddrString(reinterpret_cast<RawAddress*>(property->val));
        address = String16(remote_bdaddr_str.c_str(), remote_bdaddr_str.size());
        break;
      }
      case BT_PROPERTY_UUIDS: {
        if (property->len < 0) {
          NOTREACHED() << "Negative length on BT_PROPERTY_UUIDS:";
          break;
        }
        if (property->len % sizeof(Uuid::UUID128Bit) != 0) {
          NOTREACHED() << "Trailing bytes on BT_PROPERTY_UUIDS:";
        }
        auto uuids = static_cast<const Uuid::UUID128Bit*>(property->val);

        for (size_t i = 0; i < property->len / sizeof(Uuid::UUID128Bit); ++i) {
          service_uuids.push_back(Uuid::From128BitLE(uuids[i]));
        }
        break;
      }
      case BT_PROPERTY_CLASS_OF_DEVICE: {
        if (property->len != sizeof(int32_t)) {
          NOTREACHED() << "Invalid length for BT_PROPERTY_CLASS_OF_DEVICE";
          break;
        }
        device_class = *reinterpret_cast<const int32_t*>(property->val);
        break;
      }
      case BT_PROPERTY_TYPE_OF_DEVICE: {
        if (property->len != sizeof(int32_t)) {
          NOTREACHED() << "Invalid length for BT_PROPERTY_TYPE_OF_DEVICE";
          break;
        }
        device_type = *reinterpret_cast<const int32_t*>(property->val);
        break;
      }
      case BT_PROPERTY_REMOTE_RSSI: {
        if (property->len != sizeof(int8_t)) {
          NOTREACHED() << "Invalid length for BT_PROPERTY_REMOTE_RSSI";
          break;
        }
        rssi = *reinterpret_cast<const int8_t*>(property->val);
        break;
      }
      default:
        VLOG(1) << "Unhandled adapter property: "
                << BtPropertyText(property->type);
        break;
    }
  }

  return RemoteDeviceProps(name, address, service_uuids, device_class,
                           device_type, rssi);
}

}  // namespace

// static
// static
const char Adapter::kDefaultAddress[] = "00:00:00:00:00:00";
const char Adapter::kDefaultAddress[] = "00:00:00:00:00:00";
// static
// static
@@ -69,6 +157,41 @@ void Adapter::Observer::OnDeviceConnectionStateChanged(
  // Default implementation does nothing
  // Default implementation does nothing
}
}


void Adapter::Observer::OnScanEnableChanged(Adapter* adapter,
                                            bool scan_enabled) {
  // Default implementation does nothing
}

void Adapter::Observer::OnSspRequest(Adapter* adapter,
                                     const std::string& device_address,
                                     const std::string& device_name, int cod,
                                     int pairing_variant, int pass_key) {
  // Default implementation does nothing
}

void Adapter::Observer::OnBondStateChanged(Adapter* adapter, int status,
                                           const std::string& device_address,
                                           int state) {
  // Default implementation does nothing
}

void Adapter::Observer::OnGetBondedDevices(
    Adapter* adapter, int status,
    const std::vector<std::string>& bonded_devices) {
  // Default implementation does nothing
}

void Adapter::Observer::OnGetRemoteDeviceProperties(
    Adapter* adapter, int status, const std::string& device_address,
    const RemoteDeviceProps& properties) {
  // Default implementation does nothing
}

void Adapter::Observer::OnDeviceFound(Adapter* adapter,
                                      const RemoteDeviceProps& properties) {
  // Default implementation does nothing
}

// The real Adapter implementation used in production.
// The real Adapter implementation used in production.
class AdapterImpl : public Adapter, public hal::BluetoothInterface::Observer {
class AdapterImpl : public Adapter, public hal::BluetoothInterface::Observer {
 public:
 public:
@@ -78,6 +201,8 @@ class AdapterImpl : public Adapter, public hal::BluetoothInterface::Observer {
        name_(kDefaultName) {
        name_(kDefaultName) {
    memset(&local_le_features_, 0, sizeof(local_le_features_));
    memset(&local_le_features_, 0, sizeof(local_le_features_));
    hal::BluetoothInterface::Get()->AddObserver(this);
    hal::BluetoothInterface::Get()->AddObserver(this);
    a2dp_sink_factory_.reset(new A2dpSinkFactory);
    avrcp_control_factory_.reset(new AvrcpControlFactory);
    ble_client_factory_.reset(new LowEnergyClientFactory(*this));
    ble_client_factory_.reset(new LowEnergyClientFactory(*this));
    ble_advertiser_factory_.reset(new LowEnergyAdvertiserFactory());
    ble_advertiser_factory_.reset(new LowEnergyAdvertiserFactory());
    ble_scanner_factory_.reset(new LowEnergyScannerFactory(*this));
    ble_scanner_factory_.reset(new LowEnergyScannerFactory(*this));
@@ -183,6 +308,84 @@ class AdapterImpl : public Adapter, public hal::BluetoothInterface::Observer {


  std::string GetAddress() const override { return address_.Get(); }
  std::string GetAddress() const override { return address_.Get(); }


  bool SetScanMode(int scan_mode) override {
    switch (scan_mode) {
      case BT_SCAN_MODE_NONE:
      case BT_SCAN_MODE_CONNECTABLE:
      case BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE:
        break;
      default:
        LOG(ERROR) << "Unknown scan mode: " << scan_mode;
        return false;
    }

    auto bd_scanmode = static_cast<bt_scan_mode_t>(scan_mode);

    if (!SetAdapterProperty(BT_PROPERTY_ADAPTER_SCAN_MODE, &bd_scanmode,
                            sizeof(bd_scanmode))) {
      LOG(ERROR) << "Failed to set scan mode to : " << scan_mode;
      return false;
    }

    return true;
  }

  bool SetScanEnable(bool scan_enable) override {
    if (scan_enable) {
      int status =
          hal::BluetoothInterface::Get()->GetHALInterface()->start_discovery();
      if (status != BT_STATUS_SUCCESS) {
        LOG(ERROR) << "Failed to enable scanning";
        return false;
      }
    } else {
      int status =
          hal::BluetoothInterface::Get()->GetHALInterface()->cancel_discovery();
      if (status != BT_STATUS_SUCCESS) {
        LOG(ERROR) << "Failed to disable scanning";
        return false;
      }
    }
    return true;
  }

  bool SspReply(const std::string& device_address, int variant, bool accept,
                int32_t pass_key) override {
    RawAddress addr;
    if (!RawAddress::FromString(device_address, addr)) {
      LOG(ERROR) << "Invalid device address given: " << device_address;
      return false;
    }

    int status = hal::BluetoothInterface::Get()->GetHALInterface()->ssp_reply(
        &addr, static_cast<bt_ssp_variant_t>(variant), accept, pass_key);
    if (status != BT_STATUS_SUCCESS) {
      LOG(ERROR) << "Failed to send SSP response - status: "
                 << BtStatusText((const bt_status_t)status);
      return false;
    }

    return true;
  }

  bool CreateBond(const std::string& device_address, int transport) override {
    RawAddress addr;
    if (!RawAddress::FromString(device_address, addr)) {
      LOG(ERROR) << "Invalid device address given: " << device_address;
      return false;
    }

    int status = hal::BluetoothInterface::Get()->GetHALInterface()->create_bond(
        &addr, transport);
    if (status != BT_STATUS_SUCCESS) {
      LOG(ERROR) << "Failed to create bond - status: "
                 << BtStatusText((const bt_status_t)status);
      return false;
    }

    return true;
  }

  bool IsMultiAdvertisementSupported() override {
  bool IsMultiAdvertisementSupported() override {
    lock_guard<mutex> lock(local_le_features_lock_);
    lock_guard<mutex> lock(local_le_features_lock_);
    return local_le_features_.max_adv_instance >= kMinAdvInstancesForMultiAdv;
    return local_le_features_.max_adv_instance >= kMinAdvInstancesForMultiAdv;
@@ -209,6 +412,64 @@ class AdapterImpl : public Adapter, public hal::BluetoothInterface::Observer {
           kMinOffloadedScanStorageBytes;
           kMinOffloadedScanStorageBytes;
  }
  }


  bool GetBondedDevices() override {
    int status =
        hal::BluetoothInterface::Get()->GetHALInterface()->get_adapter_property(
            BT_PROPERTY_ADAPTER_BONDED_DEVICES);
    if (status != BT_STATUS_SUCCESS) {
      LOG(ERROR) << "Failed to get bonded devices. Status: "
                 << BtStatusText(static_cast<bt_status_t>(status));
      return false;
    }

    return true;
  }

  bool RemoveBond(const std::string& device_address) override {
    RawAddress addr;
    if (!RawAddress::FromString(device_address, addr)) {
      LOG(ERROR) << "Invalid device address given: " << device_address;
      return false;
    }

    int status =
        hal::BluetoothInterface::Get()->GetHALInterface()->remove_bond(&addr);
    if (status != BT_STATUS_SUCCESS) {
      LOG(ERROR) << "Failed to send remove bond - status: "
                 << BtStatusText(static_cast<bt_status_t>(status));
      return false;
    }

    return true;
  }

  bool GetRemoteDeviceProperties(const std::string& device_address) override {
    RawAddress addr;
    if (!RawAddress::FromString(device_address, addr)) {
      LOG(ERROR) << "Invalid device address given: " << device_address;
      return false;
    }

    int status = hal::BluetoothInterface::Get()
                     ->GetHALInterface()
                     ->get_remote_device_properties(&addr);
    if (status != BT_STATUS_SUCCESS) {
      LOG(ERROR) << "Failed to send GetRemoteDeviceProperties - status: "
                 << BtStatusText((const bt_status_t)status);
      return false;
    }

    return true;
  }

  A2dpSinkFactory* GetA2dpSinkFactory() const override {
    return a2dp_sink_factory_.get();
  }

  AvrcpControlFactory* GetAvrcpControlFactory() const override {
    return avrcp_control_factory_.get();
  }

  LowEnergyClientFactory* GetLowEnergyClientFactory() const override {
  LowEnergyClientFactory* GetLowEnergyClientFactory() const override {
    return ble_client_factory_.get();
    return ble_client_factory_.get();
  }
  }
@@ -257,6 +518,16 @@ class AdapterImpl : public Adapter, public hal::BluetoothInterface::Observer {


    if (status != BT_STATUS_SUCCESS) {
    if (status != BT_STATUS_SUCCESS) {
      LOG(ERROR) << "status: " << BtStatusText(status);
      LOG(ERROR) << "status: " << BtStatusText(status);

      for (int i = 0; i < num_properties; ++i) {
        bt_property_t* property = properties + i;
        if (property->type == BT_PROPERTY_ADAPTER_BONDED_DEVICES) {
          lock_guard<mutex> lock(observers_lock_);
          for (auto& observer : observers_) {
            observer.OnGetBondedDevices(this, status, {});
          }
        }
      }
      return;
      return;
    }
    }


@@ -290,6 +561,29 @@ class AdapterImpl : public Adapter, public hal::BluetoothInterface::Observer {
          LOG(INFO) << "Supported LE features updated";
          LOG(INFO) << "Supported LE features updated";
          break;
          break;
        }
        }
        case BT_PROPERTY_ADAPTER_BONDED_DEVICES: {
          if (property->len < 0) {
            NOTREACHED() << "Negative property length";
            break;
          }
          auto addrs = reinterpret_cast<const RawAddress*>(property->val);
          if (property->len % sizeof(addrs[0]) != 0) {
            LOG(ERROR) << "Invalid property length: " << property->len;
            // TODO(bcf): Seems to be a bug where we hit this somewhat
            // frequently.
            break;
          }
          std::vector<std::string> str_addrs;

          for (size_t i = 0; i < property->len / sizeof(addrs[0]); ++i)
            str_addrs.push_back(BtAddrString(addrs + i));

          lock_guard<mutex> lock(observers_lock_);
          for (auto& observer : observers_) {
            observer.OnGetBondedDevices(this, status, str_addrs);
          }
          break;
        }
        default:
        default:
          VLOG(1) << "Unhandled adapter property: "
          VLOG(1) << "Unhandled adapter property: "
                  << BtPropertyText(property->type);
                  << BtPropertyText(property->type);
@@ -300,9 +594,84 @@ class AdapterImpl : public Adapter, public hal::BluetoothInterface::Observer {
    }
    }
  }
  }


  void SSPRequestCallback(RawAddress*, bt_bdname_t*, uint32_t, bt_ssp_variant_t,
  void RemoteDevicePropertiesCallback(bt_status_t status,
                                      RawAddress* remote_bdaddr,
                                      int num_properties,
                                      bt_property_t* properties) override {
    std::string device_address = BtAddrString(remote_bdaddr);
    if (status != BT_STATUS_SUCCESS) {
      lock_guard<mutex> lock(observers_lock_);
      for (auto& observer : observers_) {
        observer.OnGetRemoteDeviceProperties(this, status, device_address,
                                             RemoteDeviceProps());
      }
      return;
    }

    RemoteDeviceProps props =
        ParseRemoteDeviceProps(num_properties, properties);

    std::string remote_bdaddr_str = BtAddrString(remote_bdaddr);
    android::String16 address =
        String16(remote_bdaddr_str.c_str(), remote_bdaddr_str.size());
    props.set_address(address);

    lock_guard<mutex> lock(observers_lock_);
    for (auto& observer : observers_) {
      observer.OnGetRemoteDeviceProperties(this, status, device_address, props);
    }
  }

  void DeviceFoundCallback(int num_properties,
                           bt_property_t* properties) override {
    RemoteDeviceProps props =
        ParseRemoteDeviceProps(num_properties, properties);

    lock_guard<mutex> lock(observers_lock_);
    for (auto& observer : observers_) {
      observer.OnDeviceFound(this, props);
    }
  }

  void DiscoveryStateChangedCallback(bt_discovery_state_t state) override {
    bool enabled = false;
    switch (state) {
      case BT_DISCOVERY_STOPPED:
        enabled = false;
        break;
      case BT_DISCOVERY_STARTED:
        enabled = true;
        break;
      default:
        NOTREACHED();
    }

    for (auto& observer : observers_) {
      observer.OnScanEnableChanged(this, enabled);
    }
  }

  void SSPRequestCallback(RawAddress* remote_bdaddr, bt_bdname_t* bd_name,
                          uint32_t cod, bt_ssp_variant_t pairing_variant,
                          uint32_t pass_key) override {
                          uint32_t pass_key) override {
    LOG(INFO) << "Passkey is: " << pass_key;
    std::string device_address = BtAddrString(remote_bdaddr);
    std::string name = reinterpret_cast<char*>(bd_name->name);

    lock_guard<mutex> lock(observers_lock_);
    for (auto& observer : observers_) {
      observer.OnSspRequest(this, device_address, name, cod, pairing_variant,
                            pass_key);
    }
  }

  void BondStateChangedCallback(bt_status_t status, RawAddress* remote_bdaddr,
                                bt_bond_state_t state) override {
    std::string device_address = BtAddrString(remote_bdaddr);

    lock_guard<mutex> lock(observers_lock_);
    for (auto& observer : observers_) {
      observer.OnBondStateChanged(this, status, device_address, state);
    }
  }
  }


  void AclStateChangedCallback(bt_status_t status,
  void AclStateChangedCallback(bt_status_t status,
@@ -392,6 +761,12 @@ class AdapterImpl : public Adapter, public hal::BluetoothInterface::Observer {
  std::mutex connected_devices_lock_;
  std::mutex connected_devices_lock_;
  std::unordered_set<std::string> connected_devices_;
  std::unordered_set<std::string> connected_devices_;


  // Factory used to create per-app A2dpSink instances.
  std::unique_ptr<A2dpSinkFactory> a2dp_sink_factory_;

  // Factory used to create per-app AvrcpControl instances.
  std::unique_ptr<AvrcpControlFactory> avrcp_control_factory_;

  // Factory used to create per-app LowEnergyClient instances.
  // Factory used to create per-app LowEnergyClient instances.
  std::unique_ptr<LowEnergyClientFactory> ble_client_factory_;
  std::unique_ptr<LowEnergyClientFactory> ble_client_factory_;


+67 −1

File changed.

Preview size limit exceeded, changes collapsed.

Loading