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.
5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_
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 #include "device/bluetooth/bluetooth_export.h"
21 class SequencedTaskRunner
;
22 class SequencedWorkerPool
;
28 // Manages the blocking Bluetooth tasks using |SequencedWorkerPool|. It runs
29 // bluetooth tasks using |SequencedWorkerPool| and informs its observers of
30 // bluetooth adapter state changes and any other bluetooth device inquiry
33 // It delegates the blocking Windows API calls to |bluetooth_task_runner_|'s
34 // message loop, and receives responses via methods like OnAdapterStateChanged
35 // posted to UI thread.
36 class DEVICE_BLUETOOTH_EXPORT BluetoothTaskManagerWin
37 : public base::RefCountedThreadSafe
<BluetoothTaskManagerWin
> {
39 struct DEVICE_BLUETOOTH_EXPORT AdapterState
{
47 struct DEVICE_BLUETOOTH_EXPORT ServiceRecordState
{
49 ~ServiceRecordState();
50 // Properties common to Bluetooth Classic and LE devices.
52 // Properties specific to Bluetooth Classic devices.
53 std::vector
<uint8
> sdp_bytes
;
54 // Properties specific to Bluetooth LE devices.
55 BluetoothUUID gatt_uuid
;
58 struct DEVICE_BLUETOOTH_EXPORT DeviceState
{
62 bool is_bluetooth_classic() const { return path
.empty(); }
64 // Properties common to Bluetooth Classic and LE devices.
65 std::string address
; // This uniquely identifies the device.
66 std::string name
; // Friendly name
70 ScopedVector
<ServiceRecordState
> service_record_states
;
71 // Properties specific to Bluetooth Classic devices.
72 uint32 bluetooth_class
;
73 // Properties specific to Bluetooth LE devices.
77 class DEVICE_BLUETOOTH_EXPORT Observer
{
79 virtual ~Observer() {}
81 virtual void AdapterStateChanged(const AdapterState
& state
) {}
82 virtual void DiscoveryStarted(bool success
) {}
83 virtual void DiscoveryStopped() {}
84 // Called when the adapter has just been polled for the list of *all* known
85 // devices. This includes devices previously paired, devices paired using
86 // the underlying Operating System UI, and devices discovered recently due
87 // to an active discovery session. Note that for a given device (address),
88 // the associated state can change over time. For example, during a
89 // discovery session, the "friendly" name may initially be "unknown" before
90 // the actual name is retrieved in subsequent poll events.
91 virtual void DevicesPolled(const ScopedVector
<DeviceState
>& devices
) {}
94 explicit BluetoothTaskManagerWin(
95 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner
);
97 void AddObserver(Observer
* observer
);
98 void RemoveObserver(Observer
* observer
);
101 void InitializeWithBluetoothTaskRunner(
102 scoped_refptr
<base::SequencedTaskRunner
> bluetooth_task_runner
);
105 void PostSetPoweredBluetoothTask(
107 const base::Closure
& callback
,
108 const BluetoothAdapter::ErrorCallback
& error_callback
);
109 void PostStartDiscoveryTask();
110 void PostStopDiscoveryTask();
113 friend class base::RefCountedThreadSafe
<BluetoothTaskManagerWin
>;
114 friend class BluetoothTaskManagerWinTest
;
116 static const int kPollIntervalMs
;
118 virtual ~BluetoothTaskManagerWin();
120 // Logs Win32 errors occurring during polling on the worker thread. The method
121 // may discard messages to avoid logging being too verbose.
122 void LogPollingError(const char* message
, int win32_error
);
124 // Notify all Observers of updated AdapterState. Should only be called on the
126 void OnAdapterStateChanged(const AdapterState
* state
);
127 void OnDiscoveryStarted(bool success
);
128 void OnDiscoveryStopped();
129 void OnDevicesPolled(const ScopedVector
<DeviceState
>* devices
);
131 // Called on BluetoothTaskRunner.
134 void PostAdapterStateToUi();
135 void SetPowered(bool powered
,
136 const base::Closure
& callback
,
137 const BluetoothAdapter::ErrorCallback
& error_callback
);
139 // Starts discovery. Once the discovery starts, it issues a discovery inquiry
140 // with a short timeout, then issues more inquiries with greater timeout
141 // values. The discovery finishes when StopDiscovery() is called or timeout
142 // has reached its maximum value.
143 void StartDiscovery();
144 void StopDiscovery();
146 // Issues a device inquiry that runs for |timeout_multiplier| * 1.28 seconds.
147 // This posts itself again with |timeout_multiplier| + 1 until
148 // |timeout_multiplier| reaches the maximum value or stop discovery call is
150 void DiscoverDevices(int timeout_multiplier
);
152 // Fetch already known device information. Similar to |StartDiscovery|, except
153 // this function does not issue a discovery inquiry. Instead it gets the
154 // device info cached in the adapter.
155 void GetKnownDevices();
157 // Looks for Bluetooth Classic and Low Energy devices, as well as the services
158 // exposed by those devices.
159 bool SearchDevices(int timeout_multiplier
,
160 bool search_cached_devices_only
,
161 ScopedVector
<DeviceState
>* device_list
);
163 // Sends a device search API call to the adapter to look for Bluetooth Classic
165 bool SearchClassicDevices(int timeout_multiplier
,
166 bool search_cached_devices_only
,
167 ScopedVector
<DeviceState
>* device_list
);
169 // Enumerate Bluetooth Low Energy devices.
170 bool SearchLowEnergyDevices(ScopedVector
<DeviceState
>* device_list
);
172 // Discover services for the devices in |device_list|.
173 bool DiscoverServices(ScopedVector
<DeviceState
>* device_list
,
174 bool search_cached_services_only
);
176 // Discover Bluetooth Classic services for the given |device_address|.
177 bool DiscoverClassicDeviceServices(
178 const std::string
& device_address
,
179 const GUID
& protocol_uuid
,
180 bool search_cached_services_only
,
181 ScopedVector
<ServiceRecordState
>* service_record_states
);
183 // Discover Bluetooth Classic services for the given |device_address|.
184 // Returns a Win32 error code.
185 int DiscoverClassicDeviceServicesWorker(
186 const std::string
& device_address
,
187 const GUID
& protocol_uuid
,
188 bool search_cached_services_only
,
189 ScopedVector
<ServiceRecordState
>* service_record_states
);
191 // Discover Bluetooth Low Energy services for the given |device_path|.
192 bool DiscoverLowEnergyDeviceServices(
193 const base::FilePath
& device_path
,
194 ScopedVector
<ServiceRecordState
>* service_record_states
);
196 // UI task runner reference.
197 scoped_refptr
<base::SequencedTaskRunner
> ui_task_runner_
;
199 scoped_refptr
<base::SequencedWorkerPool
> worker_pool_
;
200 scoped_refptr
<base::SequencedTaskRunner
> bluetooth_task_runner_
;
202 // List of observers interested in event notifications.
203 base::ObserverList
<Observer
> observers_
;
205 // Adapter handle owned by bluetooth task runner.
206 base::win::ScopedHandle adapter_handle_
;
208 // indicates whether the adapter is in discovery mode or not.
211 // Use for discarding too many log messages.
212 base::TimeTicks current_logging_batch_ticks_
;
213 int current_logging_batch_count_
;
215 DISALLOW_COPY_AND_ASSIGN(BluetoothTaskManagerWin
);
218 } // namespace device
220 #endif // DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_