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/observer_list.h"
14 #include "chromeos/chromeos_export.h"
15 #include "chromeos/dbus/bluetooth_agent_service_provider.h"
16 #include "chromeos/dbus/bluetooth_device_client.h"
17 #include "chromeos/dbus/bluetooth_profile_service_provider.h"
18 #include "dbus/object_path.h"
19 #include "dbus/property.h"
23 // FakeBluetoothDeviceClient simulates the behavior of the Bluetooth Daemon
24 // device objects and is used both in test cases in place of a mock and on
26 class CHROMEOS_EXPORT FakeBluetoothDeviceClient
27 : public BluetoothDeviceClient
{
29 struct Properties
: public BluetoothDeviceClient::Properties
{
30 explicit Properties(const PropertyChangedCallback
& callback
);
31 ~Properties() override
;
33 // dbus::PropertySet override
34 void Get(dbus::PropertyBase
* property
,
35 dbus::PropertySet::GetCallback callback
) override
;
36 void GetAll() override
;
37 void Set(dbus::PropertyBase
* property
,
38 dbus::PropertySet::SetCallback callback
) override
;
41 FakeBluetoothDeviceClient();
42 ~FakeBluetoothDeviceClient() override
;
44 // BluetoothDeviceClient overrides
45 void Init(dbus::Bus
* bus
) override
;
46 void AddObserver(Observer
* observer
) override
;
47 void RemoveObserver(Observer
* observer
) override
;
48 std::vector
<dbus::ObjectPath
> GetDevicesForAdapter(
49 const dbus::ObjectPath
& adapter_path
) override
;
50 Properties
* GetProperties(const dbus::ObjectPath
& object_path
) override
;
51 void Connect(const dbus::ObjectPath
& object_path
,
52 const base::Closure
& callback
,
53 const ErrorCallback
& error_callback
) override
;
54 void Disconnect(const dbus::ObjectPath
& object_path
,
55 const base::Closure
& callback
,
56 const ErrorCallback
& error_callback
) override
;
57 void ConnectProfile(const dbus::ObjectPath
& object_path
,
58 const std::string
& uuid
,
59 const base::Closure
& callback
,
60 const ErrorCallback
& error_callback
) override
;
61 void DisconnectProfile(const dbus::ObjectPath
& object_path
,
62 const std::string
& uuid
,
63 const base::Closure
& callback
,
64 const ErrorCallback
& error_callback
) override
;
65 void Pair(const dbus::ObjectPath
& object_path
,
66 const base::Closure
& callback
,
67 const ErrorCallback
& error_callback
) override
;
68 void CancelPairing(const dbus::ObjectPath
& object_path
,
69 const base::Closure
& callback
,
70 const ErrorCallback
& error_callback
) override
;
71 void GetConnInfo(const dbus::ObjectPath
& object_path
,
72 const ConnInfoCallback
& callback
,
73 const ErrorCallback
& error_callback
) override
;
75 void SetSimulationIntervalMs(int interval_ms
);
77 // Simulates discovery of devices for the given adapter.
78 void BeginDiscoverySimulation(const dbus::ObjectPath
& adapter_path
);
79 void EndDiscoverySimulation(const dbus::ObjectPath
& adapter_path
);
81 // Simulates incoming pairing of devices for the given adapter.
82 void BeginIncomingPairingSimulation(const dbus::ObjectPath
& adapter_path
);
83 void EndIncomingPairingSimulation(const dbus::ObjectPath
& adapter_path
);
85 // Creates a device from the set we return for the given adapter.
86 void CreateDevice(const dbus::ObjectPath
& adapter_path
,
87 const dbus::ObjectPath
& device_path
);
89 // Removes a device from the set we return for the given adapter.
90 void RemoveDevice(const dbus::ObjectPath
& adapter_path
,
91 const dbus::ObjectPath
& device_path
);
93 // Simulates a pairing for the device with the given D-Bus object path,
94 // |object_path|. Set |incoming_request| to true if simulating an incoming
95 // pairing request, false for an outgoing one. On successful completion
96 // |callback| will be called, on failure, |error_callback| is called.
97 void SimulatePairing(const dbus::ObjectPath
& object_path
,
98 bool incoming_request
,
99 const base::Closure
& callback
,
100 const ErrorCallback
& error_callback
);
102 // Updates the connection properties of the fake device that will be returned
104 void UpdateConnectionInfo(uint16 connection_rssi
,
105 uint16 transmit_power
,
106 uint16 max_transmit_power
);
108 // Object paths, names, addresses and bluetooth classes of the devices
110 static const char kPairedDevicePath
[];
111 static const char kPairedDeviceName
[];
112 static const char kPairedDeviceAddress
[];
113 static const uint32 kPairedDeviceClass
;
115 static const char kLegacyAutopairPath
[];
116 static const char kLegacyAutopairName
[];
117 static const char kLegacyAutopairAddress
[];
118 static const uint32 kLegacyAutopairClass
;
120 static const char kDisplayPinCodePath
[];
121 static const char kDisplayPinCodeName
[];
122 static const char kDisplayPinCodeAddress
[];
123 static const uint32 kDisplayPinCodeClass
;
125 static const char kVanishingDevicePath
[];
126 static const char kVanishingDeviceName
[];
127 static const char kVanishingDeviceAddress
[];
128 static const uint32 kVanishingDeviceClass
;
130 static const char kConnectUnpairablePath
[];
131 static const char kConnectUnpairableName
[];
132 static const char kConnectUnpairableAddress
[];
133 static const uint32 kConnectUnpairableClass
;
135 static const char kDisplayPasskeyPath
[];
136 static const char kDisplayPasskeyName
[];
137 static const char kDisplayPasskeyAddress
[];
138 static const uint32 kDisplayPasskeyClass
;
140 static const char kRequestPinCodePath
[];
141 static const char kRequestPinCodeName
[];
142 static const char kRequestPinCodeAddress
[];
143 static const uint32 kRequestPinCodeClass
;
145 static const char kConfirmPasskeyPath
[];
146 static const char kConfirmPasskeyName
[];
147 static const char kConfirmPasskeyAddress
[];
148 static const uint32 kConfirmPasskeyClass
;
150 static const char kRequestPasskeyPath
[];
151 static const char kRequestPasskeyName
[];
152 static const char kRequestPasskeyAddress
[];
153 static const uint32 kRequestPasskeyClass
;
155 static const char kUnconnectableDevicePath
[];
156 static const char kUnconnectableDeviceName
[];
157 static const char kUnconnectableDeviceAddress
[];
158 static const uint32 kUnconnectableDeviceClass
;
160 static const char kUnpairableDevicePath
[];
161 static const char kUnpairableDeviceName
[];
162 static const char kUnpairableDeviceAddress
[];
163 static const uint32 kUnpairableDeviceClass
;
165 static const char kJustWorksPath
[];
166 static const char kJustWorksName
[];
167 static const char kJustWorksAddress
[];
168 static const uint32 kJustWorksClass
;
170 static const char kLowEnergyPath
[];
171 static const char kLowEnergyName
[];
172 static const char kLowEnergyAddress
[];
173 static const uint32 kLowEnergyClass
;
175 static const char kPairedUnconnectableDevicePath
[];
176 static const char kPairedUnconnectableDeviceName
[];
177 static const char kPairedUnconnectableDeviceAddress
[];
178 static const uint32 kPairedUnconnectableDeviceClass
;
181 // Property callback passed when we create Properties* structures.
182 void OnPropertyChanged(const dbus::ObjectPath
& object_path
,
183 const std::string
& property_name
);
185 void DiscoverySimulationTimer();
186 void IncomingPairingSimulationTimer();
188 void CompleteSimulatedPairing(
189 const dbus::ObjectPath
& object_path
,
190 const base::Closure
& callback
,
191 const ErrorCallback
& error_callback
);
192 void TimeoutSimulatedPairing(
193 const dbus::ObjectPath
& object_path
,
194 const ErrorCallback
& error_callback
);
195 void CancelSimulatedPairing(
196 const dbus::ObjectPath
& object_path
,
197 const ErrorCallback
& error_callback
);
198 void RejectSimulatedPairing(
199 const dbus::ObjectPath
& object_path
,
200 const ErrorCallback
& error_callback
);
201 void FailSimulatedPairing(
202 const dbus::ObjectPath
& object_path
,
203 const ErrorCallback
& error_callback
);
204 void AddInputDeviceIfNeeded(
205 const dbus::ObjectPath
& object_path
,
206 Properties
* properties
);
208 // Updates the inquiry RSSI property of fake device with object path
209 // |object_path| to |rssi|, if the fake device exists.
210 void UpdateDeviceRSSI(const dbus::ObjectPath
& object_path
, int16 rssi
);
212 void PinCodeCallback(
213 const dbus::ObjectPath
& object_path
,
214 const base::Closure
& callback
,
215 const ErrorCallback
& error_callback
,
216 BluetoothAgentServiceProvider::Delegate::Status status
,
217 const std::string
& pincode
);
218 void PasskeyCallback(
219 const dbus::ObjectPath
& object_path
,
220 const base::Closure
& callback
,
221 const ErrorCallback
& error_callback
,
222 BluetoothAgentServiceProvider::Delegate::Status status
,
224 void ConfirmationCallback(
225 const dbus::ObjectPath
& object_path
,
226 const base::Closure
& callback
,
227 const ErrorCallback
& error_callback
,
228 BluetoothAgentServiceProvider::Delegate::Status status
);
229 void SimulateKeypress(
231 const dbus::ObjectPath
& object_path
,
232 const base::Closure
& callback
,
233 const ErrorCallback
& error_callback
);
235 void ConnectionCallback(
236 const dbus::ObjectPath
& object_path
,
237 const base::Closure
& callback
,
238 const ErrorCallback
& error_callback
,
239 BluetoothProfileServiceProvider::Delegate::Status status
);
240 void DisconnectionCallback(
241 const dbus::ObjectPath
& object_path
,
242 const base::Closure
& callback
,
243 const ErrorCallback
& error_callback
,
244 BluetoothProfileServiceProvider::Delegate::Status status
);
246 // List of observers interested in event notifications from us.
247 base::ObserverList
<Observer
> observers_
;
249 // Static properties we return.
250 typedef std::map
<const dbus::ObjectPath
, Properties
*> PropertiesMap
;
251 PropertiesMap properties_map_
;
252 std::vector
<dbus::ObjectPath
> device_list_
;
254 int simulation_interval_ms_
;
255 uint32_t discovery_simulation_step_
;
256 uint32_t incoming_pairing_simulation_step_
;
257 bool pairing_cancelled_
;
259 int16 connection_rssi_
;
260 int16 transmit_power_
;
261 int16 max_transmit_power_
;
264 } // namespace chromeos
266 #endif // CHROMEOS_DBUS_FAKE_BLUETOOTH_DEVICE_CLIENT_H_