1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROMEOS_DBUS_FAKE_BLUETOOTH_DEVICE_CLIENT_H_
6 #define CHROMEOS_DBUS_FAKE_BLUETOOTH_DEVICE_CLIENT_H_
11 #include "base/bind.h"
12 #include "base/callback.h"
13 #include "base/containers/scoped_ptr_map.h"
14 #include "base/observer_list.h"
15 #include "chromeos/chromeos_export.h"
16 #include "chromeos/dbus/bluetooth_agent_service_provider.h"
17 #include "chromeos/dbus/bluetooth_device_client.h"
18 #include "chromeos/dbus/bluetooth_profile_service_provider.h"
19 #include "dbus/object_path.h"
20 #include "dbus/property.h"
24 // FakeBluetoothDeviceClient simulates the behavior of the Bluetooth Daemon
25 // device objects and is used both in test cases in place of a mock and on
27 class CHROMEOS_EXPORT FakeBluetoothDeviceClient
28 : public BluetoothDeviceClient
{
30 struct Properties
: public BluetoothDeviceClient::Properties
{
31 explicit Properties(const PropertyChangedCallback
& callback
);
32 ~Properties() override
;
34 // dbus::PropertySet override
35 void Get(dbus::PropertyBase
* property
,
36 dbus::PropertySet::GetCallback callback
) override
;
37 void GetAll() override
;
38 void Set(dbus::PropertyBase
* property
,
39 dbus::PropertySet::SetCallback callback
) override
;
42 struct SimulatedPairingOptions
{
43 std::string pairing_method
;
44 std::string pairing_auth_token
;
47 // Stores properties of a device that is about to be created.
48 struct IncomingDeviceProperties
{
49 IncomingDeviceProperties();
50 ~IncomingDeviceProperties();
52 std::string device_name
;
53 std::string device_alias
;
54 std::string device_address
;
55 std::string device_path
;
56 std::string pairing_method
;
57 std::string pairing_auth_token
;
59 bool is_trusted
= true;
62 FakeBluetoothDeviceClient();
63 ~FakeBluetoothDeviceClient() override
;
65 // BluetoothDeviceClient overrides
66 void Init(dbus::Bus
* bus
) override
;
67 void AddObserver(Observer
* observer
) override
;
68 void RemoveObserver(Observer
* observer
) override
;
69 std::vector
<dbus::ObjectPath
> GetDevicesForAdapter(
70 const dbus::ObjectPath
& adapter_path
) override
;
71 Properties
* GetProperties(const dbus::ObjectPath
& object_path
) override
;
72 void Connect(const dbus::ObjectPath
& object_path
,
73 const base::Closure
& callback
,
74 const ErrorCallback
& error_callback
) override
;
75 void Disconnect(const dbus::ObjectPath
& object_path
,
76 const base::Closure
& callback
,
77 const ErrorCallback
& error_callback
) override
;
78 void ConnectProfile(const dbus::ObjectPath
& object_path
,
79 const std::string
& uuid
,
80 const base::Closure
& callback
,
81 const ErrorCallback
& error_callback
) override
;
82 void DisconnectProfile(const dbus::ObjectPath
& object_path
,
83 const std::string
& uuid
,
84 const base::Closure
& callback
,
85 const ErrorCallback
& error_callback
) override
;
86 void Pair(const dbus::ObjectPath
& object_path
,
87 const base::Closure
& callback
,
88 const ErrorCallback
& error_callback
) override
;
89 void CancelPairing(const dbus::ObjectPath
& object_path
,
90 const base::Closure
& callback
,
91 const ErrorCallback
& error_callback
) override
;
92 void GetConnInfo(const dbus::ObjectPath
& object_path
,
93 const ConnInfoCallback
& callback
,
94 const ErrorCallback
& error_callback
) override
;
96 void SetSimulationIntervalMs(int interval_ms
);
98 // Simulates discovery of devices for the given adapter.
99 void BeginDiscoverySimulation(const dbus::ObjectPath
& adapter_path
);
100 void EndDiscoverySimulation(const dbus::ObjectPath
& adapter_path
);
102 // Simulates incoming pairing of devices for the given adapter.
103 void BeginIncomingPairingSimulation(const dbus::ObjectPath
& adapter_path
);
104 void EndIncomingPairingSimulation(const dbus::ObjectPath
& adapter_path
);
106 // Creates a device from the set we return for the given adapter.
107 void CreateDevice(const dbus::ObjectPath
& adapter_path
,
108 const dbus::ObjectPath
& device_path
);
110 // Creates a device with the given properties.
111 void CreateDeviceWithProperties(const dbus::ObjectPath
& adapter_path
,
112 const IncomingDeviceProperties
& props
);
114 SimulatedPairingOptions
* GetPairingOptions(
115 const dbus::ObjectPath
& object_path
);
117 // Removes a device from the set we return for the given adapter.
118 void RemoveDevice(const dbus::ObjectPath
& adapter_path
,
119 const dbus::ObjectPath
& device_path
);
121 // Simulates a pairing for the device with the given D-Bus object path,
122 // |object_path|. Set |incoming_request| to true if simulating an incoming
123 // pairing request, false for an outgoing one. On successful completion
124 // |callback| will be called, on failure, |error_callback| is called.
125 void SimulatePairing(const dbus::ObjectPath
& object_path
,
126 bool incoming_request
,
127 const base::Closure
& callback
,
128 const ErrorCallback
& error_callback
);
130 // Updates the connection properties of the fake device that will be returned
132 void UpdateConnectionInfo(uint16 connection_rssi
,
133 uint16 transmit_power
,
134 uint16 max_transmit_power
);
136 // Object paths, names, addresses and bluetooth classes of the devices
138 static const char kPairedDevicePath
[];
139 static const char kPairedDeviceName
[];
140 static const char kPairedDeviceAddress
[];
141 static const uint32 kPairedDeviceClass
;
143 static const char kLegacyAutopairPath
[];
144 static const char kLegacyAutopairName
[];
145 static const char kLegacyAutopairAddress
[];
146 static const uint32 kLegacyAutopairClass
;
148 static const char kDisplayPinCodePath
[];
149 static const char kDisplayPinCodeName
[];
150 static const char kDisplayPinCodeAddress
[];
151 static const uint32 kDisplayPinCodeClass
;
153 static const char kVanishingDevicePath
[];
154 static const char kVanishingDeviceName
[];
155 static const char kVanishingDeviceAddress
[];
156 static const uint32 kVanishingDeviceClass
;
158 static const char kConnectUnpairablePath
[];
159 static const char kConnectUnpairableName
[];
160 static const char kConnectUnpairableAddress
[];
161 static const uint32 kConnectUnpairableClass
;
163 static const char kDisplayPasskeyPath
[];
164 static const char kDisplayPasskeyName
[];
165 static const char kDisplayPasskeyAddress
[];
166 static const uint32 kDisplayPasskeyClass
;
168 static const char kRequestPinCodePath
[];
169 static const char kRequestPinCodeName
[];
170 static const char kRequestPinCodeAddress
[];
171 static const uint32 kRequestPinCodeClass
;
173 static const char kConfirmPasskeyPath
[];
174 static const char kConfirmPasskeyName
[];
175 static const char kConfirmPasskeyAddress
[];
176 static const uint32 kConfirmPasskeyClass
;
178 static const char kRequestPasskeyPath
[];
179 static const char kRequestPasskeyName
[];
180 static const char kRequestPasskeyAddress
[];
181 static const uint32 kRequestPasskeyClass
;
183 static const char kUnconnectableDevicePath
[];
184 static const char kUnconnectableDeviceName
[];
185 static const char kUnconnectableDeviceAddress
[];
186 static const uint32 kUnconnectableDeviceClass
;
188 static const char kUnpairableDevicePath
[];
189 static const char kUnpairableDeviceName
[];
190 static const char kUnpairableDeviceAddress
[];
191 static const uint32 kUnpairableDeviceClass
;
193 static const char kJustWorksPath
[];
194 static const char kJustWorksName
[];
195 static const char kJustWorksAddress
[];
196 static const uint32 kJustWorksClass
;
198 static const char kLowEnergyPath
[];
199 static const char kLowEnergyName
[];
200 static const char kLowEnergyAddress
[];
201 static const uint32 kLowEnergyClass
;
203 static const char kPairedUnconnectableDevicePath
[];
204 static const char kPairedUnconnectableDeviceName
[];
205 static const char kPairedUnconnectableDeviceAddress
[];
206 static const uint32 kPairedUnconnectableDeviceClass
;
209 // Property callback passed when we create Properties* structures.
210 void OnPropertyChanged(const dbus::ObjectPath
& object_path
,
211 const std::string
& property_name
);
213 void DiscoverySimulationTimer();
214 void IncomingPairingSimulationTimer();
216 void CompleteSimulatedPairing(
217 const dbus::ObjectPath
& object_path
,
218 const base::Closure
& callback
,
219 const ErrorCallback
& error_callback
);
220 void TimeoutSimulatedPairing(
221 const dbus::ObjectPath
& object_path
,
222 const ErrorCallback
& error_callback
);
223 void CancelSimulatedPairing(
224 const dbus::ObjectPath
& object_path
,
225 const ErrorCallback
& error_callback
);
226 void RejectSimulatedPairing(
227 const dbus::ObjectPath
& object_path
,
228 const ErrorCallback
& error_callback
);
229 void FailSimulatedPairing(
230 const dbus::ObjectPath
& object_path
,
231 const ErrorCallback
& error_callback
);
232 void AddInputDeviceIfNeeded(
233 const dbus::ObjectPath
& object_path
,
234 Properties
* properties
);
236 // Updates the inquiry RSSI property of fake device with object path
237 // |object_path| to |rssi|, if the fake device exists.
238 void UpdateDeviceRSSI(const dbus::ObjectPath
& object_path
, int16 rssi
);
240 void PinCodeCallback(
241 const dbus::ObjectPath
& object_path
,
242 const base::Closure
& callback
,
243 const ErrorCallback
& error_callback
,
244 BluetoothAgentServiceProvider::Delegate::Status status
,
245 const std::string
& pincode
);
246 void PasskeyCallback(
247 const dbus::ObjectPath
& object_path
,
248 const base::Closure
& callback
,
249 const ErrorCallback
& error_callback
,
250 BluetoothAgentServiceProvider::Delegate::Status status
,
252 void ConfirmationCallback(
253 const dbus::ObjectPath
& object_path
,
254 const base::Closure
& callback
,
255 const ErrorCallback
& error_callback
,
256 BluetoothAgentServiceProvider::Delegate::Status status
);
257 void SimulateKeypress(
259 const dbus::ObjectPath
& object_path
,
260 const base::Closure
& callback
,
261 const ErrorCallback
& error_callback
);
263 void ConnectionCallback(
264 const dbus::ObjectPath
& object_path
,
265 const base::Closure
& callback
,
266 const ErrorCallback
& error_callback
,
267 BluetoothProfileServiceProvider::Delegate::Status status
);
268 void DisconnectionCallback(
269 const dbus::ObjectPath
& object_path
,
270 const base::Closure
& callback
,
271 const ErrorCallback
& error_callback
,
272 BluetoothProfileServiceProvider::Delegate::Status status
);
274 // List of observers interested in event notifications from us.
275 base::ObserverList
<Observer
> observers_
;
277 using PropertiesMap
=
278 base::ScopedPtrMap
<const dbus::ObjectPath
, scoped_ptr
<Properties
>>;
279 PropertiesMap properties_map_
;
280 std::vector
<dbus::ObjectPath
> device_list_
;
282 // Properties which are used to decied which method of pairing should
283 // be done on request.
284 using PairingOptionsMap
=
285 base::ScopedPtrMap
<const dbus::ObjectPath
,
286 scoped_ptr
<SimulatedPairingOptions
>>;
287 PairingOptionsMap pairing_options_map_
;
289 int simulation_interval_ms_
;
290 uint32_t discovery_simulation_step_
;
291 uint32_t incoming_pairing_simulation_step_
;
292 bool pairing_cancelled_
;
294 int16 connection_rssi_
;
295 int16 transmit_power_
;
296 int16 max_transmit_power_
;
299 } // namespace chromeos
301 #endif // CHROMEOS_DBUS_FAKE_BLUETOOTH_DEVICE_CLIENT_H_