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. 4 5 #ifndef CHROMEOS_DBUS_FAKE_BLUETOOTH_DEVICE_CLIENT_H_ 6 #define CHROMEOS_DBUS_FAKE_BLUETOOTH_DEVICE_CLIENT_H_ 7 8 #include <map> 9 #include <vector> 10 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" 20 21 namespace chromeos { 22 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 25 // the Linux desktop. 26 class CHROMEOS_EXPORT FakeBluetoothDeviceClient 27 : public BluetoothDeviceClient { 28 public: 29 struct Properties : public BluetoothDeviceClient::Properties { 30 explicit Properties(const PropertyChangedCallback & callback); 31 virtual ~Properties(); 32 33 // dbus::PropertySet override 34 virtual void Get(dbus::PropertyBase* property, 35 dbus::PropertySet::GetCallback callback) OVERRIDE; 36 virtual void GetAll() OVERRIDE; 37 virtual void Set(dbus::PropertyBase* property, 38 dbus::PropertySet::SetCallback callback) OVERRIDE; 39 }; 40 41 FakeBluetoothDeviceClient(); 42 virtual ~FakeBluetoothDeviceClient(); 43 44 // BluetoothDeviceClient overrides 45 virtual void Init(dbus::Bus* bus) OVERRIDE; 46 virtual void AddObserver(Observer* observer) OVERRIDE; 47 virtual void RemoveObserver(Observer* observer) OVERRIDE; 48 virtual std::vector<dbus::ObjectPath> GetDevicesForAdapter( 49 const dbus::ObjectPath& adapter_path) OVERRIDE; 50 virtual Properties* GetProperties(const dbus::ObjectPath& object_path) 51 OVERRIDE; 52 virtual void Connect(const dbus::ObjectPath& object_path, 53 const base::Closure& callback, 54 const ErrorCallback& error_callback) OVERRIDE; 55 virtual void Disconnect(const dbus::ObjectPath& object_path, 56 const base::Closure& callback, 57 const ErrorCallback& error_callback) OVERRIDE; 58 virtual void ConnectProfile(const dbus::ObjectPath& object_path, 59 const std::string& uuid, 60 const base::Closure& callback, 61 const ErrorCallback& error_callback) OVERRIDE; 62 virtual void DisconnectProfile(const dbus::ObjectPath& object_path, 63 const std::string& uuid, 64 const base::Closure& callback, 65 const ErrorCallback& error_callback) OVERRIDE; 66 virtual void Pair(const dbus::ObjectPath& object_path, 67 const base::Closure& callback, 68 const ErrorCallback& error_callback) OVERRIDE; 69 virtual void CancelPairing(const dbus::ObjectPath& object_path, 70 const base::Closure& callback, 71 const ErrorCallback& error_callback) OVERRIDE; 72 virtual void StartConnectionMonitor( 73 const dbus::ObjectPath& object_path, 74 const base::Closure& callback, 75 const ErrorCallback& error_callback) OVERRIDE; 76 virtual void StopConnectionMonitor( 77 const dbus::ObjectPath& object_path, 78 const base::Closure& callback, 79 const ErrorCallback& error_callback) OVERRIDE; 80 81 void SetSimulationIntervalMs(int interval_ms); 82 83 // Simulates discovery of devices for the given adapter. 84 void BeginDiscoverySimulation(const dbus::ObjectPath& adapter_path); 85 void EndDiscoverySimulation(const dbus::ObjectPath& adapter_path); 86 87 // Simulates incoming pairing of devices for the given adapter. 88 void BeginIncomingPairingSimulation(const dbus::ObjectPath& adapter_path); 89 void EndIncomingPairingSimulation(const dbus::ObjectPath& adapter_path); 90 91 // Creates a device from the set we return for the given adapter. 92 void CreateDevice(const dbus::ObjectPath& adapter_path, 93 const dbus::ObjectPath& device_path); 94 95 // Removes a device from the set we return for the given adapter. 96 void RemoveDevice(const dbus::ObjectPath& adapter_path, 97 const dbus::ObjectPath& device_path); 98 99 // Simulates a pairing for the device with the given D-Bus object path, 100 // |object_path|. Set |incoming_request| to true if simulating an incoming 101 // pairing request, false for an outgoing one. On successful completion 102 // |callback| will be called, on failure, |error_callback| is called. 103 void SimulatePairing(const dbus::ObjectPath& object_path, 104 bool incoming_request, 105 const base::Closure& callback, 106 const ErrorCallback& error_callback); 107 108 // Object paths, names, addresses and bluetooth classes of the devices 109 // we can emulate. 110 static const char kPairedDevicePath[]; 111 static const char kPairedDeviceName[]; 112 static const char kPairedDeviceAddress[]; 113 static const uint32 kPairedDeviceClass; 114 115 static const char kLegacyAutopairPath[]; 116 static const char kLegacyAutopairName[]; 117 static const char kLegacyAutopairAddress[]; 118 static const uint32 kLegacyAutopairClass; 119 120 static const char kDisplayPinCodePath[]; 121 static const char kDisplayPinCodeName[]; 122 static const char kDisplayPinCodeAddress[]; 123 static const uint32 kDisplayPinCodeClass; 124 125 static const char kVanishingDevicePath[]; 126 static const char kVanishingDeviceName[]; 127 static const char kVanishingDeviceAddress[]; 128 static const uint32 kVanishingDeviceClass; 129 130 static const char kConnectUnpairablePath[]; 131 static const char kConnectUnpairableName[]; 132 static const char kConnectUnpairableAddress[]; 133 static const uint32 kConnectUnpairableClass; 134 135 static const char kDisplayPasskeyPath[]; 136 static const char kDisplayPasskeyName[]; 137 static const char kDisplayPasskeyAddress[]; 138 static const uint32 kDisplayPasskeyClass; 139 140 static const char kRequestPinCodePath[]; 141 static const char kRequestPinCodeName[]; 142 static const char kRequestPinCodeAddress[]; 143 static const uint32 kRequestPinCodeClass; 144 145 static const char kConfirmPasskeyPath[]; 146 static const char kConfirmPasskeyName[]; 147 static const char kConfirmPasskeyAddress[]; 148 static const uint32 kConfirmPasskeyClass; 149 150 static const char kRequestPasskeyPath[]; 151 static const char kRequestPasskeyName[]; 152 static const char kRequestPasskeyAddress[]; 153 static const uint32 kRequestPasskeyClass; 154 155 static const char kUnconnectableDevicePath[]; 156 static const char kUnconnectableDeviceName[]; 157 static const char kUnconnectableDeviceAddress[]; 158 static const uint32 kUnconnectableDeviceClass; 159 160 static const char kUnpairableDevicePath[]; 161 static const char kUnpairableDeviceName[]; 162 static const char kUnpairableDeviceAddress[]; 163 static const uint32 kUnpairableDeviceClass; 164 165 static const char kJustWorksPath[]; 166 static const char kJustWorksName[]; 167 static const char kJustWorksAddress[]; 168 static const uint32 kJustWorksClass; 169 170 static const char kLowEnergyPath[]; 171 static const char kLowEnergyName[]; 172 static const char kLowEnergyAddress[]; 173 static const uint32 kLowEnergyClass; 174 175 private: 176 // Property callback passed when we create Properties* structures. 177 void OnPropertyChanged(const dbus::ObjectPath& object_path, 178 const std::string& property_name); 179 180 void DiscoverySimulationTimer(); 181 void IncomingPairingSimulationTimer(); 182 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); 202 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); 206 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, 218 uint32 passkey); 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( 225 uint16 entered, 226 const dbus::ObjectPath& object_path, 227 const base::Closure& callback, 228 const ErrorCallback& error_callback); 229 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); 240 241 // List of observers interested in event notifications from us. 242 ObserverList<Observer> observers_; 243 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_; 248 249 int simulation_interval_ms_; 250 uint32_t discovery_simulation_step_; 251 uint32_t incoming_pairing_simulation_step_; 252 bool pairing_cancelled_; 253 bool connection_monitor_started_; 254 }; 255 256 } // namespace chromeos 257 258 #endif // CHROMEOS_DBUS_FAKE_BLUETOOTH_DEVICE_CLIENT_H_ 259