OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_ | 5 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_ |
6 #define DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_ | 6 #define DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 28 matching lines...) Expand all Loading... |
39 AdapterState(); | 39 AdapterState(); |
40 ~AdapterState(); | 40 ~AdapterState(); |
41 std::string name; | 41 std::string name; |
42 std::string address; | 42 std::string address; |
43 bool powered; | 43 bool powered; |
44 }; | 44 }; |
45 | 45 |
46 struct ServiceRecordState { | 46 struct ServiceRecordState { |
47 ServiceRecordState(); | 47 ServiceRecordState(); |
48 ~ServiceRecordState(); | 48 ~ServiceRecordState(); |
49 // Properties common to Bluetooth Radio and LE devices. | 49 // Properties common to Bluetooth Classic and LE devices. |
50 std::string name; | 50 std::string name; |
51 // Properties specific to Bluetooth Radio devices. | 51 // Properties specific to Bluetooth Classic devices. |
52 std::vector<uint8> sdp_bytes; | 52 std::vector<uint8> sdp_bytes; |
53 // Properties specific to Bluetooth LE devices. | 53 // Properties specific to Bluetooth LE devices. |
54 BluetoothUUID gatt_uuid; | 54 BluetoothUUID gatt_uuid; |
55 }; | 55 }; |
56 | 56 |
57 struct DeviceState { | 57 struct DeviceState { |
58 DeviceState(); | 58 DeviceState(); |
59 ~DeviceState(); | 59 ~DeviceState(); |
60 // Properties common to Bluetooth Radio and LE devices. | 60 |
61 std::string name; | 61 bool is_bluetooth_classic() const { return path.empty(); } |
62 std::string address; | 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 |
63 bool visible; | 66 bool visible; |
64 bool connected; | 67 bool connected; |
65 bool authenticated; | 68 bool authenticated; |
66 ScopedVector<ServiceRecordState> service_record_states; | 69 ScopedVector<ServiceRecordState> service_record_states; |
67 // Properties specific to Bluetooth Radio devices. | 70 // Properties specific to Bluetooth Classic devices. |
68 uint32 bluetooth_class; | 71 uint32 bluetooth_class; |
69 // Properties specific to Bluetooth LE devices. | 72 // Properties specific to Bluetooth LE devices. |
70 base::FilePath path; | 73 base::FilePath path; |
71 }; | 74 }; |
72 | 75 |
73 class Observer { | 76 class Observer { |
74 public: | 77 public: |
75 virtual ~Observer() {} | 78 virtual ~Observer() {} |
76 | 79 |
77 virtual void AdapterStateChanged(const AdapterState& state) {} | 80 virtual void AdapterStateChanged(const AdapterState& state) {} |
78 virtual void DiscoveryStarted(bool success) {} | 81 virtual void DiscoveryStarted(bool success) {} |
79 virtual void DiscoveryStopped() {} | 82 virtual void DiscoveryStopped() {} |
80 virtual void DevicesUpdated(const ScopedVector<DeviceState>& devices) {} | 83 // Called when the adapter has just been polled for the list of *all* known |
81 virtual void DevicesDiscovered(const ScopedVector<DeviceState>& devices) {} | 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) {} |
82 }; | 91 }; |
83 | 92 |
84 explicit BluetoothTaskManagerWin( | 93 explicit BluetoothTaskManagerWin( |
85 scoped_refptr<base::SequencedTaskRunner> ui_task_runner); | 94 scoped_refptr<base::SequencedTaskRunner> ui_task_runner); |
86 | 95 |
87 void AddObserver(Observer* observer); | 96 void AddObserver(Observer* observer); |
88 void RemoveObserver(Observer* observer); | 97 void RemoveObserver(Observer* observer); |
89 | 98 |
90 void Initialize(); | 99 void Initialize(); |
91 void InitializeWithBluetoothTaskRunner( | 100 void InitializeWithBluetoothTaskRunner( |
92 scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner); | 101 scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner); |
93 void Shutdown(); | 102 void Shutdown(); |
94 | 103 |
95 void PostSetPoweredBluetoothTask( | 104 void PostSetPoweredBluetoothTask( |
96 bool powered, | 105 bool powered, |
97 const base::Closure& callback, | 106 const base::Closure& callback, |
98 const BluetoothAdapter::ErrorCallback& error_callback); | 107 const BluetoothAdapter::ErrorCallback& error_callback); |
99 void PostStartDiscoveryTask(); | 108 void PostStartDiscoveryTask(); |
100 void PostStopDiscoveryTask(); | 109 void PostStopDiscoveryTask(); |
101 | 110 |
102 private: | 111 private: |
103 friend class base::RefCountedThreadSafe<BluetoothTaskManagerWin>; | 112 friend class base::RefCountedThreadSafe<BluetoothTaskManagerWin>; |
104 friend class BluetoothTaskManagerWinTest; | 113 friend class BluetoothTaskManagerWinTest; |
105 | 114 |
106 static const int kPollIntervalMs; | 115 static const int kPollIntervalMs; |
107 | 116 |
108 virtual ~BluetoothTaskManagerWin(); | 117 virtual ~BluetoothTaskManagerWin(); |
109 | 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 |
110 // Notify all Observers of updated AdapterState. Should only be called on the | 123 // Notify all Observers of updated AdapterState. Should only be called on the |
111 // UI thread. | 124 // UI thread. |
112 void OnAdapterStateChanged(const AdapterState* state); | 125 void OnAdapterStateChanged(const AdapterState* state); |
113 void OnDiscoveryStarted(bool success); | 126 void OnDiscoveryStarted(bool success); |
114 void OnDiscoveryStopped(); | 127 void OnDiscoveryStopped(); |
115 void OnDevicesUpdated(const ScopedVector<DeviceState>* devices); | 128 void OnDevicesPolled(const ScopedVector<DeviceState>* devices); |
116 void OnDevicesDiscovered(const ScopedVector<DeviceState>* devices); | |
117 | 129 |
118 // Called on BluetoothTaskRunner. | 130 // Called on BluetoothTaskRunner. |
119 void StartPolling(); | 131 void StartPolling(); |
120 void PollAdapter(); | 132 void PollAdapter(); |
121 void PostAdapterStateToUi(); | 133 void PostAdapterStateToUi(); |
122 void SetPowered(bool powered, | 134 void SetPowered(bool powered, |
123 const base::Closure& callback, | 135 const base::Closure& callback, |
124 const BluetoothAdapter::ErrorCallback& error_callback); | 136 const BluetoothAdapter::ErrorCallback& error_callback); |
125 | 137 |
126 // Starts discovery. Once the discovery starts, it issues a discovery inquiry | 138 // Starts discovery. Once the discovery starts, it issues a discovery inquiry |
127 // with a short timeout, then issues more inquiries with greater timeout | 139 // with a short timeout, then issues more inquiries with greater timeout |
128 // values. The discovery finishes when StopDiscovery() is called or timeout | 140 // values. The discovery finishes when StopDiscovery() is called or timeout |
129 // has reached its maximum value. | 141 // has reached its maximum value. |
130 void StartDiscovery(); | 142 void StartDiscovery(); |
131 void StopDiscovery(); | 143 void StopDiscovery(); |
132 | 144 |
133 // Issues a device inquiry that runs for |timeout| * 1.28 seconds. | 145 // Issues a device inquiry that runs for |timeout_multiplier| * 1.28 seconds. |
134 // This posts itself again with |timeout| + 1 until |timeout| reaches the | 146 // This posts itself again with |timeout_multiplier| + 1 until |
135 // maximum value or stop discovery call is received. | 147 // |timeout_multiplier| reaches the maximum value or stop discovery call is |
136 void DiscoverDevices(int timeout); | 148 // received. |
| 149 void DiscoverDevices(int timeout_multiplier); |
137 | 150 |
138 // Fetch already known device information. Similar to |StartDiscovery|, except | 151 // Fetch already known device information. Similar to |StartDiscovery|, except |
139 // this function does not issue a discovery inquiry. Instead it gets the | 152 // this function does not issue a discovery inquiry. Instead it gets the |
140 // device info cached in the adapter. | 153 // device info cached in the adapter. |
141 void GetKnownDevices(); | 154 void GetKnownDevices(); |
142 | 155 |
143 // Sends a device search API call to the adapter. | 156 // Looks for Bluetooth Classic and Low Energy devices, as well as the services |
144 void SearchDevices(int timeout, | 157 // exposed by those devices. |
| 158 bool SearchDevices(int timeout_multiplier, |
145 bool search_cached_devices_only, | 159 bool search_cached_devices_only, |
146 ScopedVector<DeviceState>* device_list); | 160 ScopedVector<DeviceState>* device_list); |
147 | 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 |
148 // Discover services for the devices in |device_list|. | 171 // Discover services for the devices in |device_list|. |
149 void DiscoverServices(ScopedVector<DeviceState>* 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 Low Energy services for the given |device_path|. |
| 183 bool DiscoverLowEnergyDeviceServices( |
| 184 const base::FilePath& device_path, |
| 185 ScopedVector<ServiceRecordState>* service_record_states); |
150 | 186 |
151 // UI task runner reference. | 187 // UI task runner reference. |
152 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_; | 188 scoped_refptr<base::SequencedTaskRunner> ui_task_runner_; |
153 | 189 |
154 scoped_refptr<base::SequencedWorkerPool> worker_pool_; | 190 scoped_refptr<base::SequencedWorkerPool> worker_pool_; |
155 scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner_; | 191 scoped_refptr<base::SequencedTaskRunner> bluetooth_task_runner_; |
156 | 192 |
157 // List of observers interested in event notifications. | 193 // List of observers interested in event notifications. |
158 ObserverList<Observer> observers_; | 194 ObserverList<Observer> observers_; |
159 | 195 |
160 // Adapter handle owned by bluetooth task runner. | 196 // Adapter handle owned by bluetooth task runner. |
161 base::win::ScopedHandle adapter_handle_; | 197 base::win::ScopedHandle adapter_handle_; |
162 | 198 |
163 // indicates whether the adapter is in discovery mode or not. | 199 // indicates whether the adapter is in discovery mode or not. |
164 bool discovering_; | 200 bool discovering_; |
165 | 201 |
| 202 // Use for discarding too many log messages. |
| 203 base::TimeTicks current_logging_batch_ticks_; |
| 204 int current_logging_batch_count_; |
| 205 |
166 DISALLOW_COPY_AND_ASSIGN(BluetoothTaskManagerWin); | 206 DISALLOW_COPY_AND_ASSIGN(BluetoothTaskManagerWin); |
167 }; | 207 }; |
168 | 208 |
169 } // namespace device | 209 } // namespace device |
170 | 210 |
171 #endif // DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_ | 211 #endif // DEVICE_BLUETOOTH_BLUETOOTH_TASK_MANAGER_WIN_H_ |
OLD | NEW |