aboutsummaryrefslogtreecommitdiff
path: root/service/hal/bluetooth_gatt_interface.h
blob: eabe46458119d8b80937f9c620ee06f4f7214957 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//
//  Copyright 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 <base/macros.h>
#include <hardware/bluetooth.h>
#include <hardware/bt_gatt.h>

#include <mutex>
#include <unordered_set>
#include <vector>

#include "types/raw_address.h"

namespace bluetooth {
namespace hal {

// This class represents the standard BT-GATT interface. This class combines
// GATT profile server and client role operations with general GAP profile
// operations of various roles (central, scanner, peripheral, advertiser),
// wrapping around the underlying bt_gatt_interface_t structure. A single
// instance of this class exists per application and it allows multiple classes
// to interface with the global HAL interface by multiplexing callbacks among
// registered clients.
//
// This is declared as an abstract interface so that a fake implementation can
// be injected for testing the upper layer.
class BluetoothGattInterface {
 public:
  // The HAL interface doesn't allow registering "user data" that carries
  // context beyond the callback parameters, forcing implementations to deal
  // with global variables. The *Observer interface is to redirect these events
  // to interested parties in an object-oriented manner.

  // The standard LE scanner callback interface.
  class ScannerObserver {
   public:
    virtual ~ScannerObserver() = default;

    // All of the events below correspond to callbacks defined in
    // "btgatt_scanner_callbacks_t" in the HAL API definitions.

    virtual void ScanResultCallback(
        BluetoothGattInterface* gatt_iface, const RawAddress& bda, int rssi,
        std::vector<uint8_t> adv_data);  // NOLINT(pass-by-value)
  };

  // The standard BT-GATT client callback interface.
  class ClientObserver {
   public:
    virtual ~ClientObserver() = default;

    // All of the events below correspond to callbacks defined in
    // "bt_gatt_client_callbacks_t" in the HAL API definitions.

    virtual void RegisterClientCallback(BluetoothGattInterface* gatt_iface,
                                        int status, int client_if,
                                        const bluetooth::Uuid& app_uuid);

    virtual void ConnectCallback(BluetoothGattInterface* gatt_iface,
                                 int conn_id, int status, int client_if,
                                 const RawAddress& bda);

    virtual void DisconnectCallback(BluetoothGattInterface* gatt_iface,
                                    int conn_id, int status, int client_if,
                                    const RawAddress& bda);

    virtual void SearchCompleteCallback(BluetoothGattInterface* gatt_iface,
                                        int conn_id, int status);

    virtual void RegisterForNotificationCallback(
        BluetoothGattInterface* gatt_iface, int conn_id, int registered,
        int status, uint16_t handle);

    virtual void NotifyCallback(BluetoothGattInterface* gatt_iface, int conn_id,
                                const btgatt_notify_params_t& p_data);

    virtual void WriteCharacteristicCallback(BluetoothGattInterface* gatt_iface,
                                             int conn_id, int status,
                                             uint16_t handle);

    virtual void WriteDescriptorCallback(BluetoothGattInterface* gatt_iface,
                                         int conn_id, int status,
                                         uint16_t handle);

    virtual void MtuChangedCallback(BluetoothGattInterface* gatt_iface,
                                    int conn_id, int status, int mtu);

    virtual void GetGattDbCallback(BluetoothGattInterface* gatt_iface,
                                   int conn_id,
                                   const btgatt_db_element_t* gatt_db,
                                   int size);

    virtual void ServicesRemovedCallback(BluetoothGattInterface* gatt_iface,
                                         int conn_id, uint16_t start_handle,
                                         uint16_t end_handle);

    virtual void ServicesAddedCallback(BluetoothGattInterface* gatt_iface,
                                       int conn_id,
                                       const btgatt_db_element_t& added,
                                       int added_count);
  };

  // The standard BT-GATT server callback interface.
  class ServerObserver {
   public:
    virtual ~ServerObserver() = default;

    virtual void RegisterServerCallback(BluetoothGattInterface* gatt_iface,
                                        int status, int server_if,
                                        const bluetooth::Uuid& app_uuid);

    virtual void ConnectionCallback(BluetoothGattInterface* gatt_iface,
                                    int conn_id, int server_if, int connected,
                                    const RawAddress& bda);

    virtual void ServiceAddedCallback(
        BluetoothGattInterface* gatt_iface, int status, int server_if,
        std::vector<btgatt_db_element_t> service);  // NOLINT(pass-by-value)

    virtual void ServiceStoppedCallback(BluetoothGattInterface* gatt_iface,
                                        int status, int server_if,
                                        int srvc_handle);

    virtual void ServiceDeletedCallback(BluetoothGattInterface* gatt_iface,
                                        int status, int server_if,
                                        int srvc_handle);

    virtual void RequestReadCharacteristicCallback(
        BluetoothGattInterface* gatt_iface, int conn_id, int trans_id,
        const RawAddress& bda, int attr_handle, int offset, bool is_long);

    virtual void RequestReadDescriptorCallback(
        BluetoothGattInterface* gatt_iface, int conn_id, int trans_id,
        const RawAddress& bda, int attr_handle, int offset, bool is_long);

    virtual void RequestWriteCharacteristicCallback(
        BluetoothGattInterface* gatt_iface, int conn_id, int trans_id,
        const RawAddress& bda, int attr_handle, int offset, bool need_rsp,
        bool is_prep,
        std::vector<uint8_t> value);  // NOLINT(pass-by-value)

    virtual void RequestWriteDescriptorCallback(
        BluetoothGattInterface* gatt_iface, int conn_id, int trans_id,
        const RawAddress& bda, int attr_handle, int offset, bool need_rsp,
        bool is_prep,
        std::vector<uint8_t> value);  // NOLINT(pass-by-alue)

    virtual void RequestExecWriteCallback(BluetoothGattInterface* gatt_iface,
                                          int conn_id, int trans_id,
                                          const RawAddress& bda,
                                          int exec_write);

    virtual void ResponseConfirmationCallback(
        BluetoothGattInterface* gatt_iface, int status, int handle);

    virtual void IndicationSentCallback(BluetoothGattInterface* gatt_iface,
                                        int conn_id, int status);

    virtual void MtuChangedCallback(BluetoothGattInterface* gatt_iface,
                                    int conn_id, int mtu);
  };

  // Initialize and clean up the BluetoothInterface singleton. Returns false if
  // the underlying HAL interface failed to initialize, and true on success.
  static bool Initialize();

  // Shuts down and cleans up the interface. CleanUp must be called on the same
  // thread that called Initialize.
  static void CleanUp();

  // Returns true if the interface was initialized and a global singleton has
  // been created.
  static bool IsInitialized();

  // Initialize for testing. Use this to inject a test version of
  // BluetoothGattInterface. To be used from unit tests only.
  static void InitializeForTesting(BluetoothGattInterface* test_instance);

  // Returns the BluetoothGattInterface singleton. If the interface has
  // not been initialized, returns nullptr. This method is thread-safe, in that
  // it will block if the internal lock is being held by another thread. Don't
  // call this re-entrantly from an observer event as this may cause a deadlock.
  static BluetoothGattInterface* Get();

  // Add or remove an observer that is interested in LE scanner interface
  // notifications from us. Thread-safety is guaranteed by ObserverList.
  virtual void AddScannerObserver(ScannerObserver* observer) = 0;
  virtual void RemoveScannerObserver(ScannerObserver* observer) = 0;

  // Add or remove an observer that is interested in GATT client interface
  // notifications from us. Thread-safety is guaranteed by ObserverList.
  virtual void AddClientObserver(ClientObserver* observer) = 0;
  virtual void RemoveClientObserver(ClientObserver* observer) = 0;

  // Add or remove an observer that is interested in GATT server interface
  // notifications from us. Thread-safety is guaranteed by ObserverList.
  virtual void AddServerObserver(ServerObserver* observer) = 0;
  virtual void RemoveServerObserver(ServerObserver* observer) = 0;

  // The HAL module pointer that represents the standard BT LE advertiser
  // interface. This is implemented in and provided by the shared Bluetooth
  // library, so this isn't owned by us.
  //
  // Upper layers can make ble_advertiser_interface_t API calls through this
  // structure.
  virtual BleAdvertiserInterface* GetAdvertiserHALInterface() const = 0;

  // The HAL module pointer that represents the standard BT LE scanner
  // interface. This is implemented in and provided by the shared Bluetooth
  // library, so this isn't owned by us.
  //
  // Upper layers can make ble_scanner_interface_t API calls through this
  // structure.
  virtual BleScannerInterface* GetScannerHALInterface() const = 0;

  // The HAL module pointer that represents the standard BT-GATT client
  // interface. This is implemented in and provided by the shared Bluetooth
  // library, so this isn't owned by us.
  //
  // Upper layers can make btgatt_client_interface_t API calls through this
  // structure.
  virtual const btgatt_client_interface_t* GetClientHALInterface() const = 0;

  // The HAL module pointer that represents the standard BT-GATT server
  // interface. This is implemented in and provided by the shared Bluetooth
  // library, so this isn't owned by us.
  //
  // Upper layers can make btgatt_server_interface_t API calls through this
  // structure.
  virtual const btgatt_server_interface_t* GetServerHALInterface() const = 0;

  // Initiates a regular BLE device scan. This is called internally from each
  // LowEnergyClient. This function synchronizes the scan requests and maintains
  // an internal reference count for each scan client that is interested.
  bt_status_t StartScan(int client_id);
  bt_status_t StopScan(int client_id);

 protected:
  BluetoothGattInterface() = default;
  virtual ~BluetoothGattInterface() = default;

 private:
  // Used to keep a reference count for the different BLE scan clients.
  std::mutex scan_clients_lock_;
  std::unordered_set<int> scan_client_set_;

  DISALLOW_COPY_AND_ASSIGN(BluetoothGattInterface);
};

}  // namespace hal
}  // namespace bluetooth