File: bluetooth_remote_gatt_characteristic.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (325 lines) | stat: -rw-r--r-- 14,721 bytes parent folder | download | duplicates (6)
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_H_
#define DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_H_

#include <stdint.h>

#include <memory>
#include <optional>
#include <set>
#include <string>
#include <vector>

#include "base/containers/flat_map.h"
#include "base/containers/queue.h"
#include "base/functional/callback.h"
#include "base/functional/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "device/bluetooth/bluetooth_export.h"
#include "device/bluetooth/bluetooth_gatt_characteristic.h"
#include "device/bluetooth/bluetooth_gatt_notify_session.h"
#include "device/bluetooth/bluetooth_remote_gatt_service.h"
#include "device/bluetooth/public/cpp/bluetooth_uuid.h"

namespace device {

class BluetoothRemoteGattDescriptor;

// BluetoothRemoteGattCharacteristic represents a remote GATT characteristic.
// This class is used to represent GATT characteristics that belong to a service
// hosted by a remote device. In this case the characteristic will be
// constructed by the subsystem.
//
// Note: We use virtual inheritance on the GATT characteristic since it will be
// inherited by platform specific versions of the GATT characteristic classes
// also. The platform specific remote GATT characteristic classes will inherit
// both this class and their GATT characteristic class, hence causing an
// inheritance diamond.
class DEVICE_BLUETOOTH_EXPORT BluetoothRemoteGattCharacteristic
    : public virtual BluetoothGattCharacteristic {
 public:
  // Parameter for WriteRemoteCharacteristic
  enum class WriteType {
    kWithResponse,
    kWithoutResponse,
  };

  // The ValueCallback is used to return the value of a remote characteristic
  // upon a read request. Upon successful completion |error_code| will not
  // have a value and |value| may be used. When unsuccessful |error_code| will
  // have a value and |value| must be ignored.
  using ValueCallback = base::OnceCallback<void(
      std::optional<BluetoothGattService::GattErrorCode> error_code,
      const std::vector<uint8_t>& value)>;

  // The NotifySessionCallback is used to return sessions after they have
  // been successfully started.
  using NotifySessionCallback =
      base::OnceCallback<void(std::unique_ptr<BluetoothGattNotifySession>)>;

  BluetoothRemoteGattCharacteristic(const BluetoothRemoteGattCharacteristic&) =
      delete;
  BluetoothRemoteGattCharacteristic& operator=(
      const BluetoothRemoteGattCharacteristic&) = delete;

  ~BluetoothRemoteGattCharacteristic() override;

  // Returns the value of the characteristic. For remote characteristics, this
  // is the most recently cached value. For local characteristics, this is the
  // most recently updated value or the value retrieved from the delegate.
  virtual const std::vector<uint8_t>& GetValue() const = 0;

  // Returns a pointer to the GATT service this characteristic belongs to.
  virtual BluetoothRemoteGattService* GetService() const = 0;

  // Returns the list of GATT characteristic descriptors that provide more
  // information about this characteristic.
  virtual std::vector<BluetoothRemoteGattDescriptor*> GetDescriptors() const;

  // Returns the GATT characteristic descriptor with identifier |identifier| if
  // it belongs to this GATT characteristic.
  virtual BluetoothRemoteGattDescriptor* GetDescriptor(
      const std::string& identifier) const;

  // Returns the GATT characteristic descriptors that match |uuid|. There may be
  // multiple, as illustrated by Core Bluetooth Specification [V4.2 Vol 3 Part G
  // 3.3.3.5 Characteristic Presentation Format].
  virtual std::vector<BluetoothRemoteGattDescriptor*> GetDescriptorsByUUID(
      const BluetoothUUID& uuid) const;

  // Get a weak pointer to the characteristic.
  base::WeakPtr<BluetoothRemoteGattCharacteristic> GetWeakPtr();

  // Returns whether or not this characteristic is currently sending value
  // updates in the form of a notification or indication.
  //
  // If your code wants to receive notifications, you MUST call
  // StartNotifySession and hold on to the resulting session object for as long
  // as you want to keep receiving notifications. Even if this method returns
  // true, and you are able to see the notifications coming in, you have no
  // guarantee that the notifications will keep flowing for as long as you
  // need, unless you open your own session.
  virtual bool IsNotifying() const;

  // Starts a notify session for the remote characteristic, if it supports
  // notifications/indications. On success, the characteristic starts sending
  // value notifications and |callback| is called with a session object whose
  // ownership belongs to the caller. |error_callback| is called on errors.
  //
  // This method handles all logic regarding multiple sessions so that
  // specific platform implementations of the remote characteristic class
  // do not have to. Rather than overriding this method, it is recommended
  // to override the SubscribeToNotifications method below.
  //
  // The code in SubscribeToNotifications writes to the Client Characteristic
  // Configuration descriptor to enable notifications/indications. Core
  // Bluetooth Specification [V4.2 Vol 3 Part G Section 3.3.1.1. Characteristic
  // Properties] requires this descriptor to be present when
  // notifications/indications are supported. If the descriptor is not present
  // |error_callback| will be run.
  //
  // Writing a non-zero value to the remote characteristic's Client
  // Characteristic Configuration descriptor, causes the remote characteristic
  // to start sending us notifications whenever the characteristic's value
  // changes. When a new notification is received,
  // BluetoothAdapterObserver::GattCharacteristicValueChanged is called with
  // the characteristic's new value.
  //
  // To stop the flow of notifications, simply call the Stop method on the
  // BluetoothGattNotifySession object that you received in |callback|.
  virtual void StartNotifySession(NotifySessionCallback callback,
                                  ErrorCallback error_callback);
#if BUILDFLAG(IS_CHROMEOS)
  // TODO(crbug.com/40579213): This method should also be implemented on
  // Android and Windows.
  // macOS does not support specifying a notification type. According to macOS
  // documentation if the characteristic supports both notify and indicate, only
  // notifications will be enabled.
  // https://developer.apple.com/documentation/corebluetooth/cbperipheral/1518949-setnotifyvalue?language=objc#discussion
  virtual void StartNotifySession(NotificationType notification_type,
                                  NotifySessionCallback callback,
                                  ErrorCallback error_callback);
#endif  // BUILDFLAG(IS_CHROMEOS)

  // Sends a read request to a remote characteristic to read its value.
  // |callback| is called to return the read value or error.
  virtual void ReadRemoteCharacteristic(ValueCallback callback) = 0;

  // Sends a write request to a remote characteristic with the value |value|
  // using the specified |write_type|. |callback| is called to signal success
  // and |error_callback| for failures. This method only applies to remote
  // characteristics and will fail for those that are locally hosted.
  virtual void WriteRemoteCharacteristic(base::span<const uint8_t> value,
                                         WriteType write_type,
                                         base::OnceClosure callback,
                                         ErrorCallback error_callback) = 0;

  // DEPRECATED: Use WriteRemoteCharacteristic instead. This method remains
  // for backward compatibility.
  // Sends a write request to a remote characteristic with the value |value|.
  // |callback| is called to signal success and |error_callback| for failures.
  // This method only applies to remote characteristics and will fail for those
  // that are locally hosted.
  virtual void DeprecatedWriteRemoteCharacteristic(
      base::span<const uint8_t> value,
      base::OnceClosure callback,
      ErrorCallback error_callback) = 0;

#if BUILDFLAG(IS_CHROMEOS)
  // Sends a prepare write request to a remote characteristic with the value
  // |value|. |callback| is called to signal success and |error_callback| for
  // failures. This method only applies to remote characteristics and will fail
  // for those that are locally hosted.
  // Callers should use BluetoothDevice::ExecuteWrite() to commit or
  // BluetoothDevice::AbortWrite() to abort the change.
  virtual void PrepareWriteRemoteCharacteristic(
      base::span<const uint8_t> value,
      base::OnceClosure callback,
      ErrorCallback error_callback) = 0;
#endif  // BUILDFLAG(IS_CHROMEOS)

 protected:
  using DescriptorMap =
      base::flat_map<std::string,
                     std::unique_ptr<BluetoothRemoteGattDescriptor>>;

  BluetoothRemoteGattCharacteristic();

  // Writes to the Client Characteristic Configuration descriptor to enable
  // notifications/indications. This method is meant to be called from
  // StartNotifySession and should contain only the code necessary to start
  // listening to characteristic notifications on a particular platform.
#if BUILDFLAG(IS_CHROMEOS)
  // |notification_type| specifies the type of notifications that will be
  // enabled: notifications or indications.
  // TODO(crbug.com/40579213): This method should also be implemented on
  // Android and Windows.
  virtual void SubscribeToNotifications(
      BluetoothRemoteGattDescriptor* ccc_descriptor,
      NotificationType notification_type,
      base::OnceClosure callback,
      ErrorCallback error_callback) = 0;
#else
  virtual void SubscribeToNotifications(
      BluetoothRemoteGattDescriptor* ccc_descriptor,
      base::OnceClosure callback,
      ErrorCallback error_callback) = 0;
#endif  // BUILDFLAG(IS_CHROMEOS)

  // Writes to the Client Characteristic Configuration descriptor to disable
  // notifications/indications. This method is meant to be called from
  // StopNotifySession and should contain only the code necessary to stop
  // listening to characteristic notifications on a particular platform.
  virtual void UnsubscribeFromNotifications(
      BluetoothRemoteGattDescriptor* ccc_descriptor,
      base::OnceClosure callback,
      ErrorCallback error_callback) = 0;

  // Utility function to add a |descriptor| to the map of |descriptors_|.
  bool AddDescriptor(std::unique_ptr<BluetoothRemoteGattDescriptor> descriptor);

  // Descriptors owned by the chracteristic. The descriptors' identifiers serve
  // as keys.
  DescriptorMap descriptors_;

 private:
  friend class BluetoothGattNotifySession;

  enum class CommandType { kNone, kStart, kStop };

  struct CommandStatus {
    explicit CommandStatus(CommandType type = CommandType::kNone,
                           std::optional<BluetoothGattService::GattErrorCode>
                               error_code = std::nullopt);
    CommandStatus(CommandStatus&& other);

    CommandType type;
    std::optional<BluetoothGattService::GattErrorCode> error_code;
  };

  // Stops an active notify session for the remote characteristic. On success,
  // the characteristic removes this session from the list of active sessions.
  // If there are no more active sessions, notifications/indications are
  // turned off.
  //
  // This method is, and should only be, called from
  // BluetoothGattNotifySession::Stop().
  //
  // The code in UnsubscribeFromNotifications writes to the Client
  // Characteristic Configuration descriptor to disable
  // notifications/indications. Core Bluetooth Specification [V4.2 Vol 3 Part G
  // Section 3.3.1.1. Characteristic Properties] requires this descriptor to be
  // present when notifications/indications are supported.
  virtual void StopNotifySession(BluetoothGattNotifySession::Id session,
                                 base::OnceClosure callback);

  class NotifySessionCommand {
   public:
    using ExecuteCallback = base::OnceCallback<void(CommandStatus)>;

    NotifySessionCommand(ExecuteCallback execute_callback,
                         base::OnceClosure cancel_callback);
    NotifySessionCommand(NotifySessionCommand&& other);
    ~NotifySessionCommand();

    bool IsExecuted() const;
    void Execute();
    void Execute(CommandStatus previous_command);
    void Cancel();

   private:
    ExecuteCallback execute_callback_;
    base::OnceClosure cancel_callback_;
  };

  void StartNotifySessionInternal(
      const std::optional<NotificationType>& notification_type,
      NotifySessionCallback callback,
      ErrorCallback error_callback);
  void ExecuteStartNotifySession(
      const std::optional<NotificationType>& notification_type,
      NotifySessionCallback callback,
      ErrorCallback error_callback,
      CommandStatus previous_command);
  void CancelStartNotifySession(base::OnceClosure callback);
  void OnStartNotifySessionSuccess(NotifySessionCallback callback);
  void OnStartNotifySessionError(ErrorCallback error_callback,
                                 BluetoothGattService::GattErrorCode error);

  void ExecuteStopNotifySession(BluetoothGattNotifySession::Id session,
                                base::OnceClosure callback,
                                CommandStatus previous_command);
  void CancelStopNotifySession(base::OnceClosure callback);
  void OnStopNotifySessionSuccess(BluetoothGattNotifySession::Id session,
                                  base::OnceClosure callback);
  void OnStopNotifySessionError(BluetoothGattNotifySession::Id session,
                                base::OnceClosure callback,
                                BluetoothGattService::GattErrorCode error);
  bool IsNotificationTypeSupported(
      const std::optional<NotificationType>& notification_type);

  // Pending StartNotifySession / StopNotifySession calls.
  // The front will either be awaiting execution, or in the process of being
  // executed. Items are popped upon completion (success or error) of the
  // currently executing command.
  base::queue<NotifySessionCommand> pending_notify_commands_;

  // Is there a NotifySessionCommand currently executing?
  bool notify_command_running_ = false;

  // Set of active notify sessions.
  std::set<BluetoothGattNotifySession::Id> notify_sessions_;

  SEQUENCE_CHECKER(sequence_checker_);

  base::WeakPtrFactory<BluetoothRemoteGattCharacteristic> weak_ptr_factory_{
      this};
};

}  // namespace device

#endif  // DEVICE_BLUETOOTH_BLUETOOTH_REMOTE_GATT_CHARACTERISTIC_H_