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

Commit 8f7688e7 authored by Arman Uguray's avatar Arman Uguray
Browse files

service: Introduce bluetooth::LowEnergyClient

Added the LowEnergyClient class which manages a per-application BLE
API client instance. This currently only supports getting registered
with the stack and obtaining a client_if handle but will expose APIs
in the future for various BLE operations.

Bug: 23793954
Change-Id: I423f6d12321877826525e5e52136682fa9d5a441
parent 04b0094d
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ btserviceCommonSrc := \
	ipc/ipc_manager.cpp \
	ipc/unix_ipc_host.cpp \
	logging_helpers.cpp \
	low_energy_client.cpp \
	settings.cpp \
	util/atomic_string.cpp \
	uuid.cpp
@@ -76,6 +77,7 @@ LOCAL_SRC_FILES := \
	test/adapter_unittest.cpp \
	test/fake_hal_util.cpp \
	test/ipc_unix_unittest.cpp \
	test/low_energy_client_unittest.cpp \
	test/settings_unittest.cpp \
	test/stub_ipc_handler_binder.cpp \
	test/uuid_unittest.cpp
+5 −0
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@ Adapter::Adapter()
      name_(kDefaultName) {
  memset(&local_le_features_, 0, sizeof(local_le_features_));
  hal::BluetoothInterface::Get()->AddObserver(this);
  ble_client_factory_.reset(new LowEnergyClientFactory());
  hal::BluetoothInterface::Get()->GetHALInterface()->get_adapter_properties();
}

@@ -159,6 +160,10 @@ bool Adapter::IsMultiAdvertisementSupported() const {
  return local_le_features_.max_adv_instance >= kMinAdvInstancesForMultiAdv;
}

LowEnergyClientFactory* Adapter::GetLowEnergyClientFactory() const {
  return ble_client_factory_.get();
}

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

+9 −1
Original line number Diff line number Diff line
@@ -25,12 +25,13 @@

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

namespace bluetooth {

// Represents the local Bluetooth adapter.
class Adapter : hal::BluetoothInterface::Observer {
class Adapter : public 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
@@ -88,6 +89,10 @@ class Adapter : hal::BluetoothInterface::Observer {
  // multi-advertisement feature.
  bool IsMultiAdvertisementSupported() const;

  // Returns a pointer to the LowEnergyClientFactory. This can be used to
  // perform BLE GAP operations.
  LowEnergyClientFactory* GetLowEnergyClientFactory() const;

 private:
  // hal::BluetoothInterface::Observer overrides.
  void AdapterStateChangedCallback(bt_state_t state) override;
@@ -121,6 +126,9 @@ class Adapter : hal::BluetoothInterface::Observer {
  std::mutex observers_lock_;
  base::ObserverList<Observer> observers_;

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

  DISALLOW_COPY_AND_ASSIGN(Adapter);
};

+97 −0
Original line number Diff line number Diff line
//
//  Copyright (C) 2015 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/low_energy_client.h"

#include <base/logging.h>

using std::lock_guard;
using std::mutex;

namespace bluetooth {

LowEnergyClient::LowEnergyClient(const UUID& uuid, int client_if)
    : app_identifier_(uuid),
      client_if_(client_if) {
}

LowEnergyClient::~LowEnergyClient() {
  // Automatically unregister the client.
  VLOG(1) << "LowEnergyClient unregistering client: " << client_if_;
  hal::BluetoothGattInterface::Get()->
      GetClientHALInterface()->unregister_client(client_if_);
}

LowEnergyClientFactory::LowEnergyClientFactory() {
  hal::BluetoothGattInterface::Get()->AddClientObserver(this);
}

LowEnergyClientFactory::~LowEnergyClientFactory() {
  hal::BluetoothGattInterface::Get()->RemoveClientObserver(this);
}

bool LowEnergyClientFactory::RegisterClient(const UUID& uuid,
                                            const ClientCallback& callback) {
  VLOG(1) << __func__ << " - UUID: " << uuid.ToString();
  lock_guard<mutex> lock(pending_calls_lock_);

  if (pending_calls_.find(uuid) != pending_calls_.end()) {
    LOG(ERROR) << "Low-Energy client with given UUID already registered - "
               << "UUID: " << uuid.ToString();
    return false;
  }

  const btgatt_client_interface_t* hal_iface =
      hal::BluetoothGattInterface::Get()->GetClientHALInterface();
  bt_uuid_t app_uuid = uuid.GetBlueDroid();

  if (hal_iface->register_client(&app_uuid) != BT_STATUS_SUCCESS)
    return false;

  pending_calls_[uuid] = callback;

  return true;
}

void LowEnergyClientFactory::RegisterClientCallback(
    int status, int client_if,
    const bt_uuid_t& app_uuid) {
  UUID uuid(app_uuid);

  VLOG(1) << __func__ << " - UUID: " << uuid.ToString();
  lock_guard<mutex> lock(pending_calls_lock_);

  auto iter = pending_calls_.find(uuid);
  if (iter == pending_calls_.end()) {
    VLOG(1) << "Ignoring callback for unknown app_id: " << uuid.ToString();
    return;
  }

  // No need to construct a client if the call wasn't successful.
  std::unique_ptr<LowEnergyClient> client;
  BLEStatus result = BLE_STATUS_FAILURE;
  if (status == BT_STATUS_SUCCESS) {
    client.reset(new LowEnergyClient(uuid, client_if));
    result = BLE_STATUS_SUCCESS;
  }

  // Notify the result via the success callback.
  iter->second(result, uuid, std::move(client));

  pending_calls_.erase(iter);
}

}  // namespace bluetooth
+94 −0
Original line number Diff line number Diff line
//
//  Copyright (C) 2015 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 <functional>
#include <map>
#include <mutex>

#include <base/macros.h>

#include "hal/bluetooth_gatt_interface.h"
#include "service/low_energy_constants.h"
#include "service/uuid.h"

namespace bluetooth {

// A LowEnergyClient represents an application's handle to perform various
// Bluetooth Low Energy GAP operations. Instances cannot be created directly and
// should be obtained through the factory.
class LowEnergyClient {
 public:
  // The destructor automatically unregisters this client instance from the
  // stack.
  ~LowEnergyClient();

  // The app-specific unique ID used while registering this client.
  const UUID& app_identifier() const { return app_identifier_; }

  // The HAL bt_gatt_client "interface ID" assigned to us by the stack. This is
  // what is used internally for BLE transactions.
  int client_if() const { return client_if_; }

 private:
  friend class LowEnergyClientFactory;

  // Constructor/destructor shouldn't be called directly as instances are meant
  // to be obtained from the factory.
  LowEnergyClient(const UUID& uuid, int client_if);

  // See getters above for documentation.
  UUID app_identifier_;
  int client_if_;

  DISALLOW_COPY_AND_ASSIGN(LowEnergyClient);
};

// LowEnergyClientFactory is used to register and obtain a per-application
// LowEnergyClient instance. Users should call RegisterClient to obtain their
// own unique LowEnergyClient instance that has been registered with the
// Bluetooth stack.
class LowEnergyClientFactory
    : private hal::BluetoothGattInterface::ClientObserver {
 public:
  // Don't construct/destruct directly except in tests. Instead, obtain a handle
  // from an Adapter instance..
  LowEnergyClientFactory();
  ~LowEnergyClientFactory();

  // Registers a LowEnergyClient for the given unique identifier |uuid|. On
  // success, this asynchronously invokes |callback| with a unique pointer to a
  // LowEnergyClient instance whose ownership can be taken by the caller. In the
  // case of an error, the pointer will contain a nullptr.
  using ClientCallback =
      std::function<
          void(BLEStatus, const UUID&, std::unique_ptr<LowEnergyClient>)>;
  bool RegisterClient(const UUID& uuid, const ClientCallback& callback);

 private:
  // BluetoothGattInterface::ClientObserver overrides:
  void RegisterClientCallback(int status, int client_if,
                              const bt_uuid_t& app_uuid) override;

  // Map of pending calls to register.
  std::mutex pending_calls_lock_;
  std::map<UUID, ClientCallback> pending_calls_;

  DISALLOW_COPY_AND_ASSIGN(LowEnergyClientFactory);
};

}  // namespace bluetooth
Loading