Home | History | Annotate | Download | only in bluetooth
      1 // Copyright (c) 2012 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 DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_
      6 #define DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/files/file_path.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/scoped_vector.h"
     14 #include "base/observer_list.h"
     15 #include "base/win/scoped_handle.h"
     16 #include "device/bluetooth/bluetooth_adapter.h"
     17 
     18 namespace base {
     19 
     20 class SequencedTaskRunner;
     21 class SequencedWorkerPool;
     22 
     23 }  // namespace base
     24 
     25 namespace device {
     26 
     27 // Manages the blocking Bluetooth tasks using |SequencedWorkerPool|. It runs
     28 // bluetooth tasks using |SequencedWorkerPool| and informs its observers of
     29 // bluetooth adapter state changes and any other bluetooth device inquiry
     30 // result.
     31 //
     32 // It delegates the blocking Windows API calls to |bluetooth_task_runner_|'s
     33 // message loop, and receives responses via methods like OnAdapterStateChanged
     34 // posted to UI thread.
     35 class BluetoothTaskManagerWin
     36     : public base::RefCountedThreadSafe<BluetoothTaskManagerWin> {
     37  public:
     38   struct AdapterState {
     39     AdapterState();
     40     ~AdapterState();
     41     std::string name;
     42     std::string address;
     43     bool powered;
     44   };
     45 
     46   struct ServiceRecordState {
     47     ServiceRecordState();
     48     ~ServiceRecordState();
     49     // Properties common to Bluetooth Classic and LE devices.
     50     std::string name;
     51     // Properties specific to Bluetooth Classic devices.
     52     std::vector<uint8> sdp_bytes;
     53     // Properties specific to Bluetooth LE devices.
     54     BluetoothUUID gatt_uuid;
     55   };
     56 
     57   struct DeviceState {
     58     DeviceState();
     59     ~DeviceState();
     60 
     61     bool is_bluetooth_classic() const { return path.empty(); }
     62 
     63     // Properties common to Bluetooth Classic and LE devices.
     64     std::string address;  // This uniquely identifies the device.
     65     std::string name;     // Friendly name
     66     bool visible;
     67     bool connected;
     68     bool authenticated;
     69     ScopedVector<ServiceRecordState> service_record_states;
     70     // Properties specific to Bluetooth Classic devices.
     71     uint32 bluetooth_class;
     72     // Properties specific to Bluetooth LE devices.
     73     base::FilePath path;
     74   };
     75 
     76   class Observer {
     77    public:
     78      virtual ~Observer() {}
     79 
     80      virtual void AdapterStateChanged(const AdapterState& state) {}
     81      virtual void DiscoveryStarted(bool success) {}
     82      virtual void DiscoveryStopped() {}
     83      // Called when the adapter has just been polled for the list of *all* known
     84      // devices. This includes devices previously paired, devices paired using
     85      // the underlying Operating System UI, and devices discovered recently due
     86      // to an active discovery session. Note that for a given device (address),
     87      // the associated state can change over time. For example, during a
     88      // discovery session, the "friendly" name may initially be "unknown" before
     89      // the actual name is retrieved in subsequent poll events.
     90      virtual void DevicesPolled(const ScopedVector<DeviceState>& devices) {}
     91   };
     92 
     93   explicit BluetoothTaskManagerWin(
     94       scoped_refptr<base::SequencedTaskRunner> ui_task_runner);
     95 
     96   void AddObserver(Observer* observer);
     97   void RemoveObserver(Observer* observer);
     98 
     99   void Initialize();
    100   void InitializeWithBluetoothTaskRunner(
    101       scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner);
    102   void Shutdown();
    103 
    104   void PostSetPoweredBluetoothTask(
    105       bool powered,
    106       const base::Closure& callback,
    107       const BluetoothAdapter::ErrorCallback& error_callback);
    108   void PostStartDiscoveryTask();
    109   void PostStopDiscoveryTask();
    110 
    111  private:
    112   friend class base::RefCountedThreadSafe<BluetoothTaskManagerWin>;
    113   friend class BluetoothTaskManagerWinTest;
    114 
    115   static const int kPollIntervalMs;
    116 
    117   virtual ~BluetoothTaskManagerWin();
    118 
    119   // Logs Win32 errors occuring during polling on the worker thread. The method
    120   // may discards messages to avoid logging being too verbose.
    121   void LogPollingError(const char* message, int win32_error);
    122 
    123   // Notify all Observers of updated AdapterState. Should only be called on the
    124   // UI thread.
    125   void OnAdapterStateChanged(const AdapterState* state);
    126   void OnDiscoveryStarted(bool success);
    127   void OnDiscoveryStopped();
    128   void OnDevicesPolled(const ScopedVector<DeviceState>* devices);
    129 
    130   // Called on BluetoothTaskRunner.
    131   void StartPolling();
    132   void PollAdapter();
    133   void PostAdapterStateToUi();
    134   void SetPowered(bool powered,
    135                   const base::Closure& callback,
    136                   const BluetoothAdapter::ErrorCallback& error_callback);
    137 
    138   // Starts discovery. Once the discovery starts, it issues a discovery inquiry
    139   // with a short timeout, then issues more inquiries with greater timeout
    140   // values. The discovery finishes when StopDiscovery() is called or timeout
    141   // has reached its maximum value.
    142   void StartDiscovery();
    143   void StopDiscovery();
    144 
    145   // Issues a device inquiry that runs for |timeout_multiplier| * 1.28 seconds.
    146   // This posts itself again with |timeout_multiplier| + 1 until
    147   // |timeout_multiplier| reaches the maximum value or stop discovery call is
    148   // received.
    149   void DiscoverDevices(int timeout_multiplier);
    150 
    151   // Fetch already known device information. Similar to |StartDiscovery|, except
    152   // this function does not issue a discovery inquiry. Instead it gets the
    153   // device info cached in the adapter.
    154   void GetKnownDevices();
    155 
    156   // Looks for Bluetooth Classic and Low Energy devices, as well as the services
    157   // exposed by those devices.
    158   bool SearchDevices(int timeout_multiplier,
    159                      bool search_cached_devices_only,
    160                      ScopedVector<DeviceState>* device_list);
    161 
    162   // Sends a device search API call to the adapter to look for Bluetooth Classic
    163   // devices.
    164   bool SearchClassicDevices(int timeout_multiplier,
    165                             bool search_cached_devices_only,
    166                             ScopedVector<DeviceState>* device_list);
    167 
    168   // Enumerate Bluetooth Low Energy devices.
    169   bool SearchLowEnergyDevices(ScopedVector<DeviceState>* device_list);
    170 
    171   // Discover services for the devices in |device_list|.
    172   bool DiscoverServices(ScopedVector<DeviceState>* device_list,
    173                         bool search_cached_services_only);
    174 
    175   // Discover Bluetooth Classic services for the given |device_address|.
    176   bool DiscoverClassicDeviceServices(
    177       const std::string& device_address,
    178       const GUID& protocol_uuid,
    179       bool search_cached_services_only,
    180       ScopedVector<ServiceRecordState>* service_record_states);
    181 
    182   // Discover Bluetooth Classic services for the given |device_address|.
    183   // Returns a Win32 error code.
    184   int DiscoverClassicDeviceServicesWorker(
    185       const std::string& device_address,
    186       const GUID& protocol_uuid,
    187       bool search_cached_services_only,
    188       ScopedVector<ServiceRecordState>* service_record_states);
    189 
    190   // Discover Bluetooth Low Energy services for the given |device_path|.
    191   bool DiscoverLowEnergyDeviceServices(
    192       const base::FilePath& device_path,
    193       ScopedVector<ServiceRecordState>* service_record_states);
    194 
    195   // UI task runner reference.
    196   scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;
    197 
    198   scoped_refptr<base::SequencedWorkerPool> worker_pool_;
    199   scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner_;
    200 
    201   // List of observers interested in event notifications.
    202   ObserverList<Observer> observers_;
    203 
    204   // Adapter handle owned by bluetooth task runner.
    205   base::win::ScopedHandle adapter_handle_;
    206 
    207   // indicates whether the adapter is in discovery mode or not.
    208   bool discovering_;
    209 
    210   // Use for discarding too many log messages.
    211   base::TimeTicks current_logging_batch_ticks_;
    212   int current_logging_batch_count_;
    213 
    214   DISALLOW_COPY_AND_ASSIGN(BluetoothTaskManagerWin);
    215 };
    216 
    217 }  // namespace device
    218 
    219 #endif  // DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_
    220