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

Commit ccd619ce authored by Arman Uguray's avatar Arman Uguray
Browse files

service: Implement IBluetooth getAddress, getName, and setName

This CL implements the IBluetooth getAddress, getName, and setName
API methods.

Bug: 23227962
Change-Id: I5ba8395a08bdbcf6a882ea8f413448cf0a1f331a
parent 1dfb11c7
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ btserviceCommonSrc := \
	ipc/unix_ipc_host.cpp \
	logging_helpers.cpp \
	settings.cpp \
	util/atomic_string.cpp \
	uuid.cpp

btserviceBinderSrc := \
+52 −5
Original line number Diff line number Diff line
@@ -22,7 +22,15 @@

namespace bluetooth {

Adapter::Adapter() : state_(ADAPTER_STATE_OFF) {
// static
const char Adapter::kDefaultAddress[] = "00:00:00:00:00:00";
// static
const char Adapter::kDefaultName[] = "not-initialized";

Adapter::Adapter()
    : state_(ADAPTER_STATE_OFF),
      address_(kDefaultAddress),
      name_(kDefaultName) {
  hal::BluetoothInterface::Get()->AddObserver(this);
  hal::BluetoothInterface::Get()->GetHALInterface()->get_adapter_properties();
}
@@ -89,6 +97,10 @@ bool Adapter::Disable() {
  return true;
}

std::string Adapter::GetName() const {
  return name_.Get();
}

bool Adapter::SetName(const std::string& name) {
  bt_bdname_t hal_name;
  size_t max_name_len = sizeof(hal_name.name);
@@ -113,6 +125,10 @@ bool Adapter::SetName(const std::string& name) {
  return true;
}

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

void Adapter::AdapterStateChangedCallback(bt_state_t state) {
  LOG(INFO) << "Adapter state changed: " << BtStateText(state);

@@ -132,10 +148,41 @@ void Adapter::AdapterStateChangedCallback(bt_state_t state) {
  // TODO(armansito): Notify others of the state change.
}

void Adapter::AdapterPropertiesCallback(bt_status_t /* status */,
                                        int /* num_properties */,
                                        bt_property_t* /* properties */) {
  // TODO(armansito): Do something meaningful here.
void Adapter::AdapterPropertiesCallback(bt_status_t status,
                                        int num_properties,
                                        bt_property_t* properties) {
  LOG(INFO) << "Adapter properties changed";

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

  for (int i = 0; i < num_properties; i++) {
    bt_property_t* property = properties + i;
    switch (property->type) {
      case BT_PROPERTY_BDADDR: {
        std::string address = BtAddrString(reinterpret_cast<bt_bdaddr_t*>(
            property->val));
        LOG(INFO) << "Adapter address changed: " << address;
        address_.Set(address);
        break;
      }
      case BT_PROPERTY_BDNAME: {
        bt_bdname_t* hal_name = reinterpret_cast<bt_bdname_t*>(property->val);
        std::string name = reinterpret_cast<char*>(hal_name->name);
        LOG(INFO) << "Adapter name changed: " << name;
        name_.Set(name);
        break;
      }
      default:
        VLOG(1) << "Unhandled adapter property: "
                << BtPropertyText(property->type);
        break;
    }

    // TODO(armansito): notify others of the updated properties
  }
}

bool Adapter::SetAdapterProperty(bt_property_type_t type,
+20 −1
Original line number Diff line number Diff line
@@ -17,19 +17,25 @@
#pragma once

#include <atomic>
#include <memory>
#include <string>

#include <base/macros.h>

#include "service/adapter_state.h"
#include "service/hal/bluetooth_interface.h"
#include "service/util/atomic_string.h"

namespace bluetooth {

// Represents the local Bluetooth adapter.
class Adapter : hal::BluetoothInterface::Observer {
 public:
  // The default values returned before the Adapter is fully initialized and
  // powered. The complete values for these fields are obtained following a
  // successful call to "Enable".
  static const char kDefaultAddress[];
  static const char kDefaultName[];

  Adapter();
  ~Adapter() override;

@@ -50,10 +56,16 @@ class Adapter : hal::BluetoothInterface::Observer {
  // successfully sent to the Bluetooth controller.
  bool Disable();

  // Returns the name currently assigned to the local adapter.
  std::string GetName() const;

  // Sets the name assigned to the local Bluetooth adapter. This is the name
  // that the local controller will present to remote devices.
  bool SetName(const std::string& name);

  // Returns the local adapter addess in string form (XX:XX:XX:XX:XX:XX).
  std::string GetAddress() const;

 private:
  // hal::BluetoothInterface::Observer overrides.
  void AdapterStateChangedCallback(bt_state_t state) override;
@@ -67,6 +79,13 @@ class Adapter : hal::BluetoothInterface::Observer {
  // The current adapter state.
  std::atomic<AdapterState> state_;

  // The Bluetooth device address of the local adapter in string from
  // (i.e.. XX:XX:XX:XX:XX:XX)
  util::AtomicString address_;

  // The current local adapter name.
  util::AtomicString name_;

  DISALLOW_COPY_AND_ASSIGN(Adapter);
};

+34 −0
Original line number Diff line number Diff line
@@ -90,6 +90,40 @@ void FakeBluetoothInterface::NotifyAdapterStateChanged(bt_state_t state) {
  FOR_EACH_OBSERVER(Observer, observers_, AdapterStateChangedCallback(state));
}

void FakeBluetoothInterface::NotifyAdapterPropertiesChanged(
    int num_properties,
    bt_property_t* properties) {
  FOR_EACH_OBSERVER(
      Observer, observers_,
      AdapterPropertiesCallback(BT_STATUS_SUCCESS, num_properties, properties));
}

void FakeBluetoothInterface::NotifyAdapterNamePropertyChanged(
    const std::string& name) {
  bt_bdname_t hal_name;
  strncpy(reinterpret_cast<char*>(hal_name.name),
          name.c_str(),
          std::min(sizeof(hal_name)-1, name.length()));
  reinterpret_cast<char*>(hal_name.name)[name.length()] = '\0';

  bt_property_t property;
  property.len = sizeof(hal_name);
  property.val = &hal_name;
  property.type = BT_PROPERTY_BDNAME;

  NotifyAdapterPropertiesChanged(1, &property);
}

void FakeBluetoothInterface::NotifyAdapterAddressPropertyChanged(
    const bt_bdaddr_t* address) {
  bt_property_t property;
  property.len = sizeof(bt_bdaddr_t);
  property.val = (void*)address;
  property.type = BT_PROPERTY_BDADDR;

  NotifyAdapterPropertiesChanged(1, &property);
}

void FakeBluetoothInterface::AddObserver(Observer* observer) {
  observers_.AddObserver(observer);
}
+6 −0
Original line number Diff line number Diff line
@@ -45,6 +45,12 @@ class FakeBluetoothInterface : public BluetoothInterface {
  // Notifies the observers that the adapter state changed to |state|.
  void NotifyAdapterStateChanged(bt_state_t state);

  // Triggers an adapter property change event.
  void NotifyAdapterPropertiesChanged(int num_properties,
                                      bt_property_t* properties);
  void NotifyAdapterNamePropertyChanged(const std::string& name);
  void NotifyAdapterAddressPropertyChanged(const bt_bdaddr_t* address);

  // hal::BluetoothInterface overrides:
  void AddObserver(Observer* observer) override;
  void RemoveObserver(Observer* observer) override;
Loading