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
;
176 // Property callback passed when we create Properties* structures.
177 void OnPropertyChanged(const dbus::ObjectPath
& object_path
,
178 const std::string
& property_name
);
180 void DiscoverySimulationTimer();
181 void IncomingPairingSimulationTimer();
183 void CompleteSimulatedPairing(
184 const dbus::ObjectPath
& object_path
,
185 const base::Closure
& callback
,
186 const ErrorCallback
& error_callback
);
187 void TimeoutSimulatedPairing(
188 const dbus::ObjectPath
& object_path
,
189 const ErrorCallback
& error_callback
);
190 void CancelSimulatedPairing(
191 const dbus::ObjectPath
& object_path
,
192 const ErrorCallback
& error_callback
);
193 void RejectSimulatedPairing(
194 const dbus::ObjectPath
& object_path
,
195 const ErrorCallback
& error_callback
);
196 void FailSimulatedPairing(
197 const dbus::ObjectPath
& object_path
,
198 const ErrorCallback
& error_callback
);
199 void AddInputDeviceIfNeeded(
200 const dbus::ObjectPath
& object_path
,
201 Properties
* properties
);
203 // Updates the inquiry RSSI property of fake device with object path
204 // |object_path| to |rssi|, if the fake device exists.
205 void UpdateDeviceRSSI(const dbus::ObjectPath
& object_path
, int16 rssi
);
207 void PinCodeCallback(
208 const dbus::ObjectPath
& object_path
,
209 const base::Closure
& callback
,
210 const ErrorCallback
& error_callback
,
211 BluetoothAgentServiceProvider::Delegate::Status status
,
212 const std::string
& pincode
);
213 void PasskeyCallback(
214 const dbus::ObjectPath
& object_path
,
215 const base::Closure
& callback
,
216 const ErrorCallback
& error_callback
,
217 BluetoothAgentServiceProvider::Delegate::Status status
,
219 void ConfirmationCallback(
220 const dbus::ObjectPath
& object_path
,
221 const base::Closure
& callback
,
222 const ErrorCallback
& error_callback
,
223 BluetoothAgentServiceProvider::Delegate::Status status
);
224 void SimulateKeypress(
226 const dbus::ObjectPath
& object_path
,
227 const base::Closure
& callback
,
228 const ErrorCallback
& error_callback
);
230 void ConnectionCallback(
231 const dbus::ObjectPath
& object_path
,
232 const base::Closure
& callback
,
233 const ErrorCallback
& error_callback
,
234 BluetoothProfileServiceProvider::Delegate::Status status
);
235 void DisconnectionCallback(
236 const dbus::ObjectPath
& object_path
,
237 const base::Closure
& callback
,
238 const ErrorCallback
& error_callback
,
239 BluetoothProfileServiceProvider::Delegate::Status status
);
241 // List of observers interested in event notifications from us.
242 ObserverList
<Observer
> observers_
;
244 // Static properties we return.
245 typedef std::map
<const dbus::ObjectPath
, Properties
*> PropertiesMap
;
246 PropertiesMap properties_map_
;
247 std::vector
<dbus::ObjectPath
> device_list_
;
249 int simulation_interval_ms_
;
250 uint32_t discovery_simulation_step_
;
251 uint32_t incoming_pairing_simulation_step_
;
252 bool pairing_cancelled_
;
254 int16 connection_rssi_
;
255 int16 transmit_power_
;
256 int16 max_transmit_power_
;
259 } // namespace chromeos
261 #endif // CHROMEOS_DBUS_FAKE_BLUETOOTH_DEVICE_CLIENT_H_