Home | History | Annotate | Download | only in dbus
      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 "chromeos/dbus/dbus_client_implementation_type.h"
     19 #include "dbus/object_path.h"
     20 #include "dbus/property.h"
     21 
     22 namespace chromeos {
     23 
     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
     26 // the Linux desktop.
     27 class CHROMEOS_EXPORT FakeBluetoothDeviceClient
     28     : public BluetoothDeviceClient {
     29  public:
     30   struct Properties : public BluetoothDeviceClient::Properties {
     31     explicit Properties(const PropertyChangedCallback & callback);
     32     virtual ~Properties();
     33 
     34     // dbus::PropertySet override
     35     virtual void Get(dbus::PropertyBase* property,
     36                      dbus::PropertySet::GetCallback callback) OVERRIDE;
     37     virtual void GetAll() OVERRIDE;
     38     virtual void Set(dbus::PropertyBase* property,
     39                      dbus::PropertySet::SetCallback callback) OVERRIDE;
     40   };
     41 
     42   FakeBluetoothDeviceClient();
     43   virtual ~FakeBluetoothDeviceClient();
     44 
     45   // BluetoothDeviceClient 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 
     73   void SetSimulationIntervalMs(int interval_ms);
     74 
     75   // Simulate discovery of devices for the given adapter.
     76   void BeginDiscoverySimulation(const dbus::ObjectPath& adapter_path);
     77   void EndDiscoverySimulation(const dbus::ObjectPath& adapter_path);
     78 
     79   // Remove a device from the set we return for the given adapter.
     80   void RemoveDevice(const dbus::ObjectPath& adapter_path,
     81                     const dbus::ObjectPath& device_path);
     82 
     83   // Object paths, names, addresses and bluetooth classes of the devices
     84   // we can emulate.
     85   static const char kPairedDevicePath[];
     86   static const char kPairedDeviceName[];
     87   static const char kPairedDeviceAddress[];
     88   static const uint32 kPairedDeviceClass;
     89 
     90   static const char kAppleMousePath[];
     91   static const char kAppleMouseName[];
     92   static const char kAppleMouseAddress[];
     93   static const uint32 kAppleMouseClass;
     94 
     95   static const char kAppleKeyboardPath[];
     96   static const char kAppleKeyboardName[];
     97   static const char kAppleKeyboardAddress[];
     98   static const uint32 kAppleKeyboardClass;
     99 
    100   static const char kVanishingDevicePath[];
    101   static const char kVanishingDeviceName[];
    102   static const char kVanishingDeviceAddress[];
    103   static const uint32 kVanishingDeviceClass;
    104 
    105   static const char kMicrosoftMousePath[];
    106   static const char kMicrosoftMouseName[];
    107   static const char kMicrosoftMouseAddress[];
    108   static const uint32 kMicrosoftMouseClass;
    109 
    110   static const char kMotorolaKeyboardPath[];
    111   static const char kMotorolaKeyboardName[];
    112   static const char kMotorolaKeyboardAddress[];
    113   static const uint32 kMotorolaKeyboardClass;
    114 
    115   static const char kSonyHeadphonesPath[];
    116   static const char kSonyHeadphonesName[];
    117   static const char kSonyHeadphonesAddress[];
    118   static const uint32 kSonyHeadphonesClass;
    119 
    120   static const char kPhonePath[];
    121   static const char kPhoneName[];
    122   static const char kPhoneAddress[];
    123   static const uint32 kPhoneClass;
    124 
    125   static const char kWeirdDevicePath[];
    126   static const char kWeirdDeviceName[];
    127   static const char kWeirdDeviceAddress[];
    128   static const uint32 kWeirdDeviceClass;
    129 
    130   static const char kUnconnectableDevicePath[];
    131   static const char kUnconnectableDeviceName[];
    132   static const char kUnconnectableDeviceAddress[];
    133   static const uint32 kUnconnectableDeviceClass;
    134 
    135   static const char kUnpairableDevicePath[];
    136   static const char kUnpairableDeviceName[];
    137   static const char kUnpairableDeviceAddress[];
    138   static const uint32 kUnpairableDeviceClass;
    139 
    140  private:
    141   // Property callback passed when we create Properties* structures.
    142   void OnPropertyChanged(const dbus::ObjectPath& object_path,
    143                          const std::string& property_name);
    144 
    145   void DiscoverySimulationTimer();
    146 
    147   void CompleteSimulatedPairing(
    148       const dbus::ObjectPath& object_path,
    149       const base::Closure& callback,
    150       const ErrorCallback& error_callback);
    151   void TimeoutSimulatedPairing(
    152       const dbus::ObjectPath& object_path,
    153       const ErrorCallback& error_callback);
    154   void CancelSimulatedPairing(
    155       const dbus::ObjectPath& object_path,
    156       const ErrorCallback& error_callback);
    157   void RejectSimulatedPairing(
    158       const dbus::ObjectPath& object_path,
    159       const ErrorCallback& error_callback);
    160   void FailSimulatedPairing(
    161       const dbus::ObjectPath& object_path,
    162       const ErrorCallback& error_callback);
    163   void AddInputDeviceIfNeeded(
    164       const dbus::ObjectPath& object_path,
    165       Properties* properties);
    166 
    167   void PinCodeCallback(
    168       const dbus::ObjectPath& object_path,
    169       const base::Closure& callback,
    170       const ErrorCallback& error_callback,
    171       BluetoothAgentServiceProvider::Delegate::Status status,
    172       const std::string& pincode);
    173   void PasskeyCallback(
    174       const dbus::ObjectPath& object_path,
    175       const base::Closure& callback,
    176       const ErrorCallback& error_callback,
    177       BluetoothAgentServiceProvider::Delegate::Status status,
    178       uint32 passkey);
    179   void ConfirmationCallback(
    180       const dbus::ObjectPath& object_path,
    181       const base::Closure& callback,
    182       const ErrorCallback& error_callback,
    183       BluetoothAgentServiceProvider::Delegate::Status status);
    184   void SimulateKeypress(
    185       uint16 entered,
    186       const dbus::ObjectPath& object_path,
    187       const base::Closure& callback,
    188       const ErrorCallback& error_callback);
    189 
    190   void ConnectionCallback(
    191       const dbus::ObjectPath& object_path,
    192       const base::Closure& callback,
    193       const ErrorCallback& error_callback,
    194       BluetoothProfileServiceProvider::Delegate::Status status);
    195   void DisconnectionCallback(
    196       const dbus::ObjectPath& object_path,
    197       const base::Closure& callback,
    198       const ErrorCallback& error_callback,
    199       BluetoothProfileServiceProvider::Delegate::Status status);
    200 
    201   // List of observers interested in event notifications from us.
    202   ObserverList<Observer> observers_;
    203 
    204   // Static properties we return.
    205   typedef std::map<const dbus::ObjectPath, Properties *> PropertiesMap;
    206   PropertiesMap properties_map_;
    207   std::vector<dbus::ObjectPath> device_list_;
    208 
    209   int simulation_interval_ms_;
    210   uint32_t discovery_simulation_step_;
    211   bool pairing_cancelled_;
    212 };
    213 
    214 }  // namespace chromeos
    215 
    216 #endif  // CHROMEOS_DBUS_FAKE_BLUETOOTH_DEVICE_CLIENT_H_
    217