File: fake_peripheral.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (193 lines) | stat: -rw-r--r-- 7,603 bytes parent folder | download | duplicates (5)
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
// Copyright 2017 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_EMULATION_FAKE_PERIPHERAL_H_
#define DEVICE_BLUETOOTH_EMULATION_FAKE_PERIPHERAL_H_

#include <optional>
#include <string>

#include "base/compiler_specific.h"
#include "base/memory/weak_ptr.h"
#include "build/build_config.h"
#include "device/bluetooth/bluetooth_device.h"
#include "device/bluetooth/emulation/fake_central.h"
#include "device/bluetooth/emulation/fake_remote_gatt_service.h"

namespace device {
class BluetoothUUID;
}

namespace bluetooth {

// Implements device::BluetoothDevice. Meant to be used by FakeCentral
// to keep track of the peripheral's state and attributes.
//
// Not intended for direct use by clients.  See README.md.
class FakePeripheral : public device::BluetoothDevice {
 public:
  FakePeripheral(FakeCentral* fake_central, const std::string& address);

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

  ~FakePeripheral() override;

  // Changes the name of the device.
  void SetName(std::optional<std::string> name);

  // Set it to indicate if the system has connected to the Peripheral outside of
  // the Bluetooth interface e.g. the user connected to the device through
  // system settings.
  void SetSystemConnected(bool gatt_connected);

  // Updates the peripheral's UUIDs that are returned by
  // BluetoothDevice::GetUUIDs().
  void SetServiceUUIDs(UUIDSet service_uuids);

  // Updates the peripheral's Manufacturer Data that are returned by
  // BluetoothDevice::GetManufacturerData().
  void SetManufacturerData(ManufacturerDataMap manufacturer_data);

  // If |code| is kHCISuccess calls a pending success callback for
  // CreateGattConnection. Otherwise calls a pending error callback
  // with the ConnectErrorCode corresponding to |code|.
  void SetNextGATTConnectionResponse(uint16_t code);

  // If |code| is kHCISuccess, calls
  // BluetoothAdapter::Observer::GattServicesDiscovered otherwise
  // sets IsGattDiscoveryComplete to false. Both of these happen
  // after IsGattDiscoveryComplete is called.
  void SetNextGATTDiscoveryResponse(uint16_t code);

  // Simulate a response to a pending GATT connection request with |code|.
  void SimulateGATTConnectionResponse(uint16_t code);

  // Simulate a response to a pending GATT service discovery request with
  // |code|.
  void SimulateGATTDiscoveryResponse(uint16_t code);

  // Returns true if there are no pending responses for this peripheral or any
  // of its GATT services.
  bool AllResponsesConsumed();

  // Simulates a GATT disconnection from the peripheral.
  void SimulateGATTDisconnection();

  // Adds a fake primary service with |service_uuid| to this peripheral.
  // Returns the service's Id.
  std::string AddFakeService(const device::BluetoothUUID& service_uuid);

  // Remove a fake service with |identifier| from this peripheral.
  bool RemoveFakeService(const std::string& identifier);

  FakeCentral& fake_central() const { return fake_central_.get(); }

  // BluetoothDevice overrides:
  uint32_t GetBluetoothClass() const override;
#if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_ANDROID)
  device::BluetoothTransport GetType() const override;
#endif  // BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
  std::string GetIdentifier() const override;
  std::string GetAddress() const override;
  AddressType GetAddressType() const override;
  VendorIDSource GetVendorIDSource() const override;
  uint16_t GetVendorID() const override;
  uint16_t GetProductID() const override;
  uint16_t GetDeviceID() const override;
  uint16_t GetAppearance() const override;
  std::optional<std::string> GetName() const override;
  std::u16string GetNameForDisplay() const override;
  bool IsPaired() const override;
#if BUILDFLAG(IS_CHROMEOS)
  bool IsBonded() const override;
#endif  // BUILDFLAG(IS_CHROMEOS)
  bool IsConnected() const override;
  bool IsGattConnected() const override;
  bool IsConnectable() const override;
  bool IsConnecting() const override;
  bool ExpectingPinCode() const override;
  bool ExpectingPasskey() const override;
  bool ExpectingConfirmation() const override;
  void GetConnectionInfo(ConnectionInfoCallback callback) override;
  void SetConnectionLatency(ConnectionLatency connection_latency,
                            base::OnceClosure callback,
                            ErrorCallback error_callback) override;
  void Connect(PairingDelegate* pairing_delegate,
               ConnectCallback callback) override;
#if BUILDFLAG(IS_CHROMEOS)
  void ConnectClassic(PairingDelegate* pairing_delegate,
                      ConnectCallback callback) override;
#endif  // BUILDFLAG(IS_CHROMEOS)
  void SetPinCode(const std::string& pincode) override;
  void SetPasskey(uint32_t passkey) override;
  void ConfirmPairing() override;
  void RejectPairing() override;
  void CancelPairing() override;
  void Disconnect(base::OnceClosure callback,
                  ErrorCallback error_callback) override;
  void Forget(base::OnceClosure callback,
              ErrorCallback error_callback) override;
  void ConnectToService(const device::BluetoothUUID& uuid,
                        ConnectToServiceCallback callback,
                        ConnectToServiceErrorCallback error_callback) override;
  void ConnectToServiceInsecurely(
      const device::BluetoothUUID& uuid,
      ConnectToServiceCallback callback,
      ConnectToServiceErrorCallback error_callback) override;
  void CreateGattConnection(
      GattConnectionCallback callback,
      std::optional<device::BluetoothUUID> service_uuid) override;
  bool IsGattServicesDiscoveryComplete() const override;
#if BUILDFLAG(IS_APPLE)
  bool IsLowEnergyDevice() override;
#endif  // BUILDFLAG(IS_APPLE)
#if BUILDFLAG(IS_CHROMEOS)
  void ExecuteWrite(base::OnceClosure callback,
                    ExecuteWriteErrorCallback error_callback) override;
  void AbortWrite(base::OnceClosure callback,
                  AbortWriteErrorCallback error_callback) override;
#endif  // BUILDFLAG(IS_CHROMEOS)

 protected:
  void CreateGattConnectionImpl(std::optional<device::BluetoothUUID>) override;
  void DisconnectGatt() override;

 private:
  void DispatchConnectionEvent();
  void DispatchDiscoveryEvent();

  const std::string address_;
  std::optional<std::string> name_;
  // True when the system has connected to the device outside of the Bluetooth
  // interface e.g. the user connected to the device through system settings.
  bool system_connected_;
  // True when this Bluetooth interface is connected to the device.
  bool gatt_connected_;

  // Keeps track of the last Id used to create a fake service. Incremented
  // every time a new fake service is added.
  size_t last_service_id_;

  // Used to simulate a GATT Discovery procedure.
  // Mutable because IsGattServicesDiscoveryComplete needs to set this but
  // is const.
  mutable bool pending_gatt_discovery_;

  // Used to decide which callback should be called when
  // CreateGattConnection is called.
  std::optional<uint16_t> next_connection_response_;

  // Used to decide if the GattServicesDiscovered method is called.
  std::optional<uint16_t> next_discovery_response_;

  const raw_ref<FakeCentral> fake_central_;

  // Mutable because IsGattServicesDiscoveryComplete needs to post a task but
  // is const.
  mutable base::WeakPtrFactory<FakePeripheral> weak_ptr_factory_{this};
};

}  // namespace bluetooth

#endif  // DEVICE_BLUETOOTH_EMULATION_FAKE_PERIPHERAL_H_