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 // TODO(youngki): Implement this file. | 5 // TODO(youngki): Implement this file. |
6 | 6 |
7 #include "device/bluetooth/bluetooth_adapter_win.h" | 7 #include "device/bluetooth/bluetooth_adapter_win.h" |
8 | 8 |
9 #include <BluetoothAPIs.h> | |
10 #if defined(_WIN32_WINNT_WIN8) && _MSC_VER < 1700 | |
11 // The Windows 8 SDK defines FACILITY_VISUALCPP in winerror.h. | |
12 #undef FACILITY_VISUALCPP | |
13 #endif | |
14 #include <delayimp.h> | |
15 #include <string> | 9 #include <string> |
16 #include "base/bind.h" | |
17 #include "base/logging.h" | 10 #include "base/logging.h" |
18 #include "base/message_loop.h" | 11 #include "base/sequenced_task_runner.h" |
19 #include "base/stringprintf.h" | 12 #include "base/single_thread_task_runner.h" |
20 #include "base/sys_string_conversions.h" | 13 #include "base/thread_task_runner_handle.h" |
21 #include "base/threading/thread_restrictions.h" | |
22 | |
23 #pragma comment(lib, "Bthprops.lib") | |
24 #pragma comment(lib, "delayimp.lib") | |
25 | |
26 namespace { | |
27 | |
28 const BLUETOOTH_FIND_RADIO_PARAMS bluetooth_adapter_param = | |
29 { sizeof(BLUETOOTH_FIND_RADIO_PARAMS) }; | |
30 | |
31 // A frame-based exception handler filter function for a handler for exceptions | |
32 // generated by the Visual C++ delay loader helper function. | |
33 int FilterVisualCPPExceptions(DWORD exception_code) { | |
34 return HRESULT_FACILITY(exception_code) == FACILITY_VISUALCPP ? | |
35 EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH; | |
36 } | |
37 | |
38 // Returns true if the machine has a bluetooth stack available. The first call | |
39 // to this function will involve file IO, so it should be done on an | |
40 // appropriate thread. This function is not threadsafe. | |
41 bool HasBluetoothStack() { | |
42 static enum { | |
43 HBS_UNKNOWN, | |
44 HBS_YES, | |
45 HBS_NO, | |
46 } has_bluetooth_stack = HBS_UNKNOWN; | |
47 | |
48 if (has_bluetooth_stack == HBS_UNKNOWN) { | |
49 base::ThreadRestrictions::AssertIOAllowed(); | |
50 HRESULT hr = E_FAIL; | |
51 __try { | |
52 hr = __HrLoadAllImportsForDll("bthprops.cpl"); | |
53 } __except(FilterVisualCPPExceptions(::GetExceptionCode())) { | |
54 hr = E_FAIL; | |
55 } | |
56 has_bluetooth_stack = SUCCEEDED(hr) ? HBS_YES : HBS_NO; | |
57 } | |
58 | |
59 return has_bluetooth_stack == HBS_YES; | |
60 } | |
61 | |
62 } // namespace | |
63 | 14 |
64 namespace device { | 15 namespace device { |
65 | 16 |
66 const int BluetoothAdapterWin::kPollIntervalMs = 500; | |
67 | |
68 BluetoothAdapterWin::BluetoothAdapterWin() | 17 BluetoothAdapterWin::BluetoothAdapterWin() |
69 : BluetoothAdapter(), | 18 : BluetoothAdapter(), |
70 powered_(false), | 19 powered_(false), |
71 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 20 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
72 } | 21 } |
73 | 22 |
74 BluetoothAdapterWin::~BluetoothAdapterWin() { | 23 BluetoothAdapterWin::~BluetoothAdapterWin() { |
| 24 if (task_manager_) { |
| 25 task_manager_->RemoveObserver(this); |
| 26 task_manager_->Shutdown(); |
| 27 } |
75 } | 28 } |
76 | 29 |
77 void BluetoothAdapterWin::AddObserver(BluetoothAdapter::Observer* observer) { | 30 void BluetoothAdapterWin::AddObserver(BluetoothAdapter::Observer* observer) { |
78 NOTIMPLEMENTED(); | 31 NOTIMPLEMENTED(); |
79 } | 32 } |
80 | 33 |
81 void BluetoothAdapterWin::RemoveObserver(BluetoothAdapter::Observer* observer) { | 34 void BluetoothAdapterWin::RemoveObserver(BluetoothAdapter::Observer* observer) { |
82 NOTIMPLEMENTED(); | 35 NOTIMPLEMENTED(); |
83 } | 36 } |
84 | 37 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 NOTIMPLEMENTED(); | 77 NOTIMPLEMENTED(); |
125 return NULL; | 78 return NULL; |
126 } | 79 } |
127 | 80 |
128 void BluetoothAdapterWin::ReadLocalOutOfBandPairingData( | 81 void BluetoothAdapterWin::ReadLocalOutOfBandPairingData( |
129 const BluetoothOutOfBandPairingDataCallback& callback, | 82 const BluetoothOutOfBandPairingDataCallback& callback, |
130 const ErrorCallback& error_callback) { | 83 const ErrorCallback& error_callback) { |
131 NOTIMPLEMENTED(); | 84 NOTIMPLEMENTED(); |
132 } | 85 } |
133 | 86 |
134 void BluetoothAdapterWin::UpdateAdapterState() { | 87 void BluetoothAdapterWin::AdapterStateChanged( |
135 // TODO(youngki): Move this check to the earliest point reasonable so that no | 88 const BluetoothTaskManagerWin::AdapterState& state) { |
136 // attempts are made to do any bluetooth processing if no BT stack is present. | 89 DCHECK(thread_checker_.CalledOnValidThread()); |
137 if (!HasBluetoothStack()) | 90 name_ = state.name; |
138 return; | 91 address_ = state.address; |
139 | 92 powered_ = state.powered; |
140 HBLUETOOTH_RADIO_FIND bluetooth_adapter_handle = NULL; | |
141 BLUETOOTH_RADIO_INFO bluetooth_adapter_info = | |
142 { sizeof(BLUETOOTH_RADIO_INFO), 0 }; | |
143 HBLUETOOTH_RADIO_FIND bluetooth_handle = BluetoothFindFirstRadio( | |
144 &bluetooth_adapter_param, &bluetooth_adapter_handle); | |
145 | |
146 if (bluetooth_adapter_handle) { | |
147 if (ERROR_SUCCESS == BluetoothGetRadioInfo(bluetooth_adapter_handle, | |
148 &bluetooth_adapter_info)) { | |
149 name_ = base::SysWideToUTF8(bluetooth_adapter_info.szName); | |
150 address_ = base::StringPrintf("%02X:%02X:%02X:%02X:%02X:%02X", | |
151 bluetooth_adapter_info.address.rgBytes[5], | |
152 bluetooth_adapter_info.address.rgBytes[4], | |
153 bluetooth_adapter_info.address.rgBytes[3], | |
154 bluetooth_adapter_info.address.rgBytes[2], | |
155 bluetooth_adapter_info.address.rgBytes[1], | |
156 bluetooth_adapter_info.address.rgBytes[0]); | |
157 powered_ = BluetoothIsConnectable(bluetooth_adapter_handle) || | |
158 BluetoothIsDiscoverable(bluetooth_adapter_handle); | |
159 } else { | |
160 name_.clear(); | |
161 address_.clear(); | |
162 powered_ = false; | |
163 } | |
164 } | |
165 | |
166 if (bluetooth_handle) | |
167 BluetoothFindRadioClose(bluetooth_handle); | |
168 } | 93 } |
169 | 94 |
170 void BluetoothAdapterWin::TrackDefaultAdapter() { | 95 void BluetoothAdapterWin::TrackDefaultAdapter() { |
171 PollAdapterState(); | 96 task_manager_ = |
172 } | 97 new BluetoothTaskManagerWin(base::ThreadTaskRunnerHandle::Get()); |
173 | 98 task_manager_->AddObserver(this); |
174 void BluetoothAdapterWin::PollAdapterState() { | 99 task_manager_->Initialize(); |
175 UpdateAdapterState(); | |
176 | |
177 MessageLoop::current()->PostDelayedTask( | |
178 FROM_HERE, | |
179 base::Bind(&BluetoothAdapterWin::PollAdapterState, | |
180 weak_ptr_factory_.GetWeakPtr()), | |
181 base::TimeDelta::FromMilliseconds(kPollIntervalMs)); | |
182 } | 100 } |
183 | 101 |
184 } // namespace device | 102 } // namespace device |
OLD | NEW |