low_energy_client.h revision c2fc0f287f4dfaf206a51856b8d5dfa923af3c05
1//
2//  Copyright (C) 2015 Google, Inc.
3//
4//  Licensed under the Apache License, Version 2.0 (the "License");
5//  you may not use this file except in compliance with the License.
6//  You may obtain a copy of the License at:
7//
8//  http://www.apache.org/licenses/LICENSE-2.0
9//
10//  Unless required by applicable law or agreed to in writing, software
11//  distributed under the License is distributed on an "AS IS" BASIS,
12//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13//  See the License for the specific language governing permissions and
14//  limitations under the License.
15//
16
17#pragma once
18
19#include <functional>
20#include <map>
21#include <mutex>
22
23#include <base/macros.h>
24
25#include "hal/bluetooth_gatt_interface.h"
26#include "service/low_energy_constants.h"
27#include "service/uuid.h"
28
29namespace bluetooth {
30
31// A LowEnergyClient represents an application's handle to perform various
32// Bluetooth Low Energy GAP operations. Instances cannot be created directly and
33// should be obtained through the factory.
34class LowEnergyClient {
35 public:
36  // The destructor automatically unregisters this client instance from the
37  // stack.
38  ~LowEnergyClient();
39
40  // The app-specific unique ID used while registering this client.
41  const UUID& app_identifier() const { return app_identifier_; }
42
43  // The HAL bt_gatt_client "interface ID" assigned to us by the stack. This is
44  // what is used internally for BLE transactions.
45  int client_if() const { return client_if_; }
46
47 private:
48  friend class LowEnergyClientFactory;
49
50  // Constructor/destructor shouldn't be called directly as instances are meant
51  // to be obtained from the factory.
52  LowEnergyClient(const UUID& uuid, int client_if);
53
54  // See getters above for documentation.
55  UUID app_identifier_;
56  int client_if_;
57
58  DISALLOW_COPY_AND_ASSIGN(LowEnergyClient);
59};
60
61// LowEnergyClientFactory is used to register and obtain a per-application
62// LowEnergyClient instance. Users should call RegisterClient to obtain their
63// own unique LowEnergyClient instance that has been registered with the
64// Bluetooth stack.
65class LowEnergyClientFactory
66    : private hal::BluetoothGattInterface::ClientObserver {
67 public:
68  // Don't construct/destruct directly except in tests. Instead, obtain a handle
69  // from an Adapter instance..
70  LowEnergyClientFactory();
71  ~LowEnergyClientFactory();
72
73  // Registers a LowEnergyClient for the given unique identifier |uuid|. On
74  // success, this asynchronously invokes |callback| with a unique pointer to a
75  // LowEnergyClient instance whose ownership can be taken by the caller. In the
76  // case of an error, the pointer will contain a nullptr.
77  using ClientCallback =
78      std::function<
79          void(BLEStatus, const UUID&, std::unique_ptr<LowEnergyClient>)>;
80  bool RegisterClient(const UUID& uuid, const ClientCallback& callback);
81
82 private:
83  // BluetoothGattInterface::ClientObserver overrides:
84  void RegisterClientCallback(int status, int client_if,
85                              const bt_uuid_t& app_uuid) override;
86
87  // Map of pending calls to register.
88  std::mutex pending_calls_lock_;
89  std::map<UUID, ClientCallback> pending_calls_;
90
91  DISALLOW_COPY_AND_ASSIGN(LowEnergyClientFactory);
92};
93
94}  // namespace bluetooth
95