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" | |
19 #include "base/stringprintf.h" | |
20 #include "base/sys_string_conversions.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 | 11 |
64 namespace device { | 12 namespace device { |
65 | 13 |
66 const int BluetoothAdapterWin::kPollIntervalMs = 500; | |
67 | |
68 BluetoothAdapterWin::BluetoothAdapterWin() | 14 BluetoothAdapterWin::BluetoothAdapterWin() |
69 : BluetoothAdapter(), | 15 : BluetoothAdapter(), |
70 powered_(false), | 16 powered_(false), |
| 17 task_manager_(new BluetoothTaskManagerWin()), |
71 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 18 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 19 task_manager_->AddObserver(this); |
72 } | 20 } |
73 | 21 |
74 BluetoothAdapterWin::~BluetoothAdapterWin() { | 22 BluetoothAdapterWin::~BluetoothAdapterWin() { |
| 23 task_manager_->RemoveObserver(this); |
| 24 task_manager_->Shutdown(); |
75 } | 25 } |
76 | 26 |
77 void BluetoothAdapterWin::AddObserver(BluetoothAdapter::Observer* observer) { | 27 void BluetoothAdapterWin::AddObserver(BluetoothAdapter::Observer* observer) { |
78 NOTIMPLEMENTED(); | 28 NOTIMPLEMENTED(); |
79 } | 29 } |
80 | 30 |
81 void BluetoothAdapterWin::RemoveObserver(BluetoothAdapter::Observer* observer) { | 31 void BluetoothAdapterWin::RemoveObserver(BluetoothAdapter::Observer* observer) { |
82 NOTIMPLEMENTED(); | 32 NOTIMPLEMENTED(); |
83 } | 33 } |
84 | 34 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 NOTIMPLEMENTED(); | 74 NOTIMPLEMENTED(); |
125 return NULL; | 75 return NULL; |
126 } | 76 } |
127 | 77 |
128 void BluetoothAdapterWin::ReadLocalOutOfBandPairingData( | 78 void BluetoothAdapterWin::ReadLocalOutOfBandPairingData( |
129 const BluetoothOutOfBandPairingDataCallback& callback, | 79 const BluetoothOutOfBandPairingDataCallback& callback, |
130 const ErrorCallback& error_callback) { | 80 const ErrorCallback& error_callback) { |
131 NOTIMPLEMENTED(); | 81 NOTIMPLEMENTED(); |
132 } | 82 } |
133 | 83 |
134 void BluetoothAdapterWin::UpdateAdapterState() { | 84 void BluetoothAdapterWin::AdapterStateChanged( |
135 // TODO(youngki): Move this check to the earliest point reasonable so that no | 85 const BluetoothTaskManagerWin::AdapterState& state) { |
136 // attempts are made to do any bluetooth processing if no BT stack is present. | 86 DCHECK(thread_checker_.CalledOnValidThread()); |
137 if (!HasBluetoothStack()) | 87 name_ = state.name; |
138 return; | 88 address_ = state.address; |
139 | 89 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 } | 90 } |
169 | 91 |
170 void BluetoothAdapterWin::TrackDefaultAdapter() { | 92 void BluetoothAdapterWin::TrackDefaultAdapter() { |
171 PollAdapterState(); | 93 task_manager_->Initialize(); |
172 } | |
173 | |
174 void BluetoothAdapterWin::PollAdapterState() { | |
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 } | 94 } |
183 | 95 |
184 } // namespace device | 96 } // namespace device |
OLD | NEW |