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 SimulatedPairingOptions();
44 ~SimulatedPairingOptions();
46 bool incoming
= false;
47 std::string pairing_method
;
48 std::string pairing_auth_token
;
49 std::string pairing_action
;
52 // Stores properties of a device that is about to be created.
53 struct IncomingDeviceProperties
{
54 IncomingDeviceProperties();
55 ~IncomingDeviceProperties();
57 std::string device_address
;
58 std::string device_alias
;
60 std::string device_name
;
61 std::string device_path
;
62 bool is_trusted
= true;
63 bool incoming
= false;
64 std::string pairing_action
;
65 std::string pairing_auth_token
;
66 std::string pairing_method
;
69 FakeBluetoothDeviceClient();
70 ~FakeBluetoothDeviceClient() override
;
72 // BluetoothDeviceClient overrides
73 void Init(dbus::Bus
* bus
) override
;
74 void AddObserver(Observer
* observer
) override
;
75 void RemoveObserver(Observer
* observer
) override
;
76 std::vector
<dbus::ObjectPath
> GetDevicesForAdapter(
77 const dbus::ObjectPath
& adapter_path
) override
;
78 Properties
* GetProperties(const dbus::ObjectPath
& object_path
) override
;
79 void Connect(const dbus::ObjectPath
& object_path
,
80 const base::Closure
& callback
,
81 const ErrorCallback
& error_callback
) override
;
82 void Disconnect(const dbus::ObjectPath
& object_path
,
83 const base::Closure
& callback
,
84 const ErrorCallback
& error_callback
) override
;
85 void ConnectProfile(const dbus::ObjectPath
& object_path
,
86 const std::string
& uuid
,
87 const base::Closure
& callback
,
88 const ErrorCallback
& error_callback
) override
;
89 void DisconnectProfile(const dbus::ObjectPath
& object_path
,
90 const std::string
& uuid
,
91 const base::Closure
& callback
,
92 const ErrorCallback
& error_callback
) override
;
93 void Pair(const dbus::ObjectPath
& object_path
,
94 const base::Closure
& callback
,
95 const ErrorCallback
& error_callback
) override
;
96 void CancelPairing(const dbus::ObjectPath
& object_path
,
97 const base::Closure
& callback
,
98 const ErrorCallback
& error_callback
) override
;
99 void GetConnInfo(const dbus::ObjectPath
& object_path
,
100 const ConnInfoCallback
& callback
,
101 const ErrorCallback
& error_callback
) override
;
103 void SetSimulationIntervalMs(int interval_ms
);
105 // Simulates discovery of devices for the given adapter.
106 void BeginDiscoverySimulation(const dbus::ObjectPath
& adapter_path
);
107 void EndDiscoverySimulation(const dbus::ObjectPath
& adapter_path
);
109 // Simulates incoming pairing of devices for the given adapter.
110 void BeginIncomingPairingSimulation(const dbus::ObjectPath
& adapter_path
);
111 void EndIncomingPairingSimulation(const dbus::ObjectPath
& adapter_path
);
113 // Creates a device from the set we return for the given adapter.
114 void CreateDevice(const dbus::ObjectPath
& adapter_path
,
115 const dbus::ObjectPath
& device_path
);
117 // Creates a device with the given properties.
118 void CreateDeviceWithProperties(const dbus::ObjectPath
& adapter_path
,
119 const IncomingDeviceProperties
& props
);
121 // Creates and returns a list of scoped_ptr<base::DictionaryValue>
122 // objects, which contain all the data from the constants for devices with
123 // predefined behavior.
124 scoped_ptr
<base::ListValue
> GetBluetoothDevicesAsDictionaries() const;
126 SimulatedPairingOptions
* GetPairingOptions(
127 const dbus::ObjectPath
& object_path
);
129 // Removes a device from the set we return for the given adapter.
130 void RemoveDevice(const dbus::ObjectPath
& adapter_path
,
131 const dbus::ObjectPath
& device_path
);
133 // Simulates a pairing for the device with the given D-Bus object path,
134 // |object_path|. Set |incoming_request| to true if simulating an incoming
135 // pairing request, false for an outgoing one. On successful completion
136 // |callback| will be called, on failure, |error_callback| is called.
137 void SimulatePairing(const dbus::ObjectPath
& object_path
,
138 bool incoming_request
,
139 const base::Closure
& callback
,
140 const ErrorCallback
& error_callback
);
142 // Updates the connection properties of the fake device that will be returned
144 void UpdateConnectionInfo(uint16 connection_rssi
,
145 uint16 transmit_power
,
146 uint16 max_transmit_power
);
148 static const char kTestPinCode
[];
149 static const int kTestPassKey
;
151 static const char kPairingMethodNone
[];
152 static const char kPairingMethodPinCode
[];
153 static const char kPairingMethodPassKey
[];
155 static const char kPairingActionConfirmation
[];
156 static const char kPairingActionDisplay
[];
157 static const char kPairingActionFail
[];
158 static const char kPairingActionRequest
[];
160 // Object paths, names, addresses and bluetooth classes of the devices
162 static const char kPairedDevicePath
[];
163 static const char kPairedDeviceName
[];
164 static const char kPairedDeviceAddress
[];
165 static const uint32 kPairedDeviceClass
;
167 static const char kLegacyAutopairPath
[];
168 static const char kLegacyAutopairName
[];
169 static const char kLegacyAutopairAddress
[];
170 static const uint32 kLegacyAutopairClass
;
172 static const char kDisplayPinCodePath
[];
173 static const char kDisplayPinCodeName
[];
174 static const char kDisplayPinCodeAddress
[];
175 static const uint32 kDisplayPinCodeClass
;
177 static const char kVanishingDevicePath
[];
178 static const char kVanishingDeviceName
[];
179 static const char kVanishingDeviceAddress
[];
180 static const uint32 kVanishingDeviceClass
;
182 static const char kConnectUnpairablePath
[];
183 static const char kConnectUnpairableName
[];
184 static const char kConnectUnpairableAddress
[];
185 static const uint32 kConnectUnpairableClass
;
187 static const char kDisplayPasskeyPath
[];
188 static const char kDisplayPasskeyName
[];
189 static const char kDisplayPasskeyAddress
[];
190 static const uint32 kDisplayPasskeyClass
;
192 static const char kRequestPinCodePath
[];
193 static const char kRequestPinCodeName
[];
194 static const char kRequestPinCodeAddress
[];
195 static const uint32 kRequestPinCodeClass
;
197 static const char kConfirmPasskeyPath
[];
198 static const char kConfirmPasskeyName
[];
199 static const char kConfirmPasskeyAddress
[];
200 static const uint32 kConfirmPasskeyClass
;
202 static const char kRequestPasskeyPath
[];
203 static const char kRequestPasskeyName
[];
204 static const char kRequestPasskeyAddress
[];
205 static const uint32 kRequestPasskeyClass
;
207 static const char kUnconnectableDevicePath
[];
208 static const char kUnconnectableDeviceName
[];
209 static const char kUnconnectableDeviceAddress
[];
210 static const uint32 kUnconnectableDeviceClass
;
212 static const char kUnpairableDevicePath
[];
213 static const char kUnpairableDeviceName
[];
214 static const char kUnpairableDeviceAddress
[];
215 static const uint32 kUnpairableDeviceClass
;
217 static const char kJustWorksPath
[];
218 static const char kJustWorksName
[];
219 static const char kJustWorksAddress
[];
220 static const uint32 kJustWorksClass
;
222 static const char kLowEnergyPath
[];
223 static const char kLowEnergyName
[];
224 static const char kLowEnergyAddress
[];
225 static const uint32 kLowEnergyClass
;
227 static const char kPairedUnconnectableDevicePath
[];
228 static const char kPairedUnconnectableDeviceName
[];
229 static const char kPairedUnconnectableDeviceAddress
[];
230 static const uint32 kPairedUnconnectableDeviceClass
;
233 // Property callback passed when we create Properties* structures.
234 void OnPropertyChanged(const dbus::ObjectPath
& object_path
,
235 const std::string
& property_name
);
237 void DiscoverySimulationTimer();
238 void IncomingPairingSimulationTimer();
240 void CompleteSimulatedPairing(
241 const dbus::ObjectPath
& object_path
,
242 const base::Closure
& callback
,
243 const ErrorCallback
& error_callback
);
244 void TimeoutSimulatedPairing(
245 const dbus::ObjectPath
& object_path
,
246 const ErrorCallback
& error_callback
);
247 void CancelSimulatedPairing(
248 const dbus::ObjectPath
& object_path
,
249 const ErrorCallback
& error_callback
);
250 void RejectSimulatedPairing(
251 const dbus::ObjectPath
& object_path
,
252 const ErrorCallback
& error_callback
);
253 void FailSimulatedPairing(
254 const dbus::ObjectPath
& object_path
,
255 const ErrorCallback
& error_callback
);
256 void AddInputDeviceIfNeeded(
257 const dbus::ObjectPath
& object_path
,
258 Properties
* properties
);
260 // Updates the inquiry RSSI property of fake device with object path
261 // |object_path| to |rssi|, if the fake device exists.
262 void UpdateDeviceRSSI(const dbus::ObjectPath
& object_path
, int16 rssi
);
264 void PinCodeCallback(
265 const dbus::ObjectPath
& object_path
,
266 const base::Closure
& callback
,
267 const ErrorCallback
& error_callback
,
268 BluetoothAgentServiceProvider::Delegate::Status status
,
269 const std::string
& pincode
);
270 void PasskeyCallback(
271 const dbus::ObjectPath
& object_path
,
272 const base::Closure
& callback
,
273 const ErrorCallback
& error_callback
,
274 BluetoothAgentServiceProvider::Delegate::Status status
,
276 void ConfirmationCallback(
277 const dbus::ObjectPath
& object_path
,
278 const base::Closure
& callback
,
279 const ErrorCallback
& error_callback
,
280 BluetoothAgentServiceProvider::Delegate::Status status
);
281 void SimulateKeypress(
283 const dbus::ObjectPath
& object_path
,
284 const base::Closure
& callback
,
285 const ErrorCallback
& error_callback
);
287 void ConnectionCallback(
288 const dbus::ObjectPath
& object_path
,
289 const base::Closure
& callback
,
290 const ErrorCallback
& error_callback
,
291 BluetoothProfileServiceProvider::Delegate::Status status
);
292 void DisconnectionCallback(
293 const dbus::ObjectPath
& object_path
,
294 const base::Closure
& callback
,
295 const ErrorCallback
& error_callback
,
296 BluetoothProfileServiceProvider::Delegate::Status status
);
298 // List of observers interested in event notifications from us.
299 base::ObserverList
<Observer
> observers_
;
301 using PropertiesMap
=
302 base::ScopedPtrMap
<const dbus::ObjectPath
, scoped_ptr
<Properties
>>;
303 PropertiesMap properties_map_
;
304 std::vector
<dbus::ObjectPath
> device_list_
;
306 // Properties which are used to decied which method of pairing should
307 // be done on request.
308 using PairingOptionsMap
=
309 base::ScopedPtrMap
<const dbus::ObjectPath
,
310 scoped_ptr
<SimulatedPairingOptions
>>;
311 PairingOptionsMap pairing_options_map_
;
313 int simulation_interval_ms_
;
314 uint32_t discovery_simulation_step_
;
315 uint32_t incoming_pairing_simulation_step_
;
316 bool pairing_cancelled_
;
318 int16 connection_rssi_
;
319 int16 transmit_power_
;
320 int16 max_transmit_power_
;
323 } // namespace chromeos
325 #endif // CHROMEOS_DBUS_FAKE_BLUETOOTH_DEVICE_CLIENT_H_