OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 #include "device/serial/serial_io_handler_win.h" | |
6 | |
5 #include <windows.h> | 7 #include <windows.h> |
8 #include <setupapi.h> | |
6 | 9 |
7 #include "device/serial/serial_io_handler_win.h" | 10 #include "base/bind.h" |
11 #include "base/scoped_observer.h" | |
12 #include "base/threading/thread_checker.h" | |
13 #include "device/core/device_info_query_win.h" | |
14 #include "device/core/device_monitor_win.h" | |
15 #include "third_party/re2/re2/re2.h" | |
8 | 16 |
9 namespace device { | 17 namespace device { |
10 | 18 |
11 namespace { | 19 namespace { |
12 | 20 |
13 int BitrateToSpeedConstant(int bitrate) { | 21 int BitrateToSpeedConstant(int bitrate) { |
14 #define BITRATE_TO_SPEED_CASE(x) \ | 22 #define BITRATE_TO_SPEED_CASE(x) \ |
15 case x: \ | 23 case x: \ |
16 return CBR_##x; | 24 return CBR_##x; |
17 switch (bitrate) { | 25 switch (bitrate) { |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
123 serial::StopBits StopBitsConstantToEnum(int stop_bits) { | 131 serial::StopBits StopBitsConstantToEnum(int stop_bits) { |
124 switch (stop_bits) { | 132 switch (stop_bits) { |
125 case TWOSTOPBITS: | 133 case TWOSTOPBITS: |
126 return serial::STOP_BITS_TWO; | 134 return serial::STOP_BITS_TWO; |
127 case ONESTOPBIT: | 135 case ONESTOPBIT: |
128 default: | 136 default: |
129 return serial::STOP_BITS_ONE; | 137 return serial::STOP_BITS_ONE; |
130 } | 138 } |
131 } | 139 } |
132 | 140 |
141 // Searches for the COM port in the device's friendly name, assigns its value to | |
142 // com_port, and returns whether the operation was successful. | |
143 bool GetCOMPort(const std::string friendly_name, std::string* com_port) { | |
144 return RE2::PartialMatch(friendly_name, ".* \\((COM[0-9]+)\\)", com_port); | |
145 } | |
146 | |
133 } // namespace | 147 } // namespace |
134 | 148 |
135 // static | 149 // static |
136 scoped_refptr<SerialIoHandler> SerialIoHandler::Create( | 150 scoped_refptr<SerialIoHandler> SerialIoHandler::Create( |
137 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, | 151 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, |
138 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner) { | 152 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner) { |
139 return new SerialIoHandlerWin(file_thread_task_runner, ui_thread_task_runner); | 153 return new SerialIoHandlerWin(file_thread_task_runner, ui_thread_task_runner); |
140 } | 154 } |
141 | 155 |
156 class SerialIoHandlerWin::UiThreadHelper : public DeviceMonitorWin::Observer { | |
157 public: | |
158 UiThreadHelper( | |
159 base::WeakPtr<SerialIoHandlerWin> io_handler, | |
160 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner) | |
161 : device_observer_(this), | |
162 io_handler_(io_handler), | |
163 io_thread_task_runner_(io_thread_task_runner) {} | |
164 | |
165 ~UiThreadHelper() { DCHECK(thread_checker_.CalledOnValidThread()); } | |
166 | |
167 static void Start(UiThreadHelper* self) { | |
168 self->thread_checker_.DetachFromThread(); | |
169 DeviceMonitorWin* device_monitor = DeviceMonitorWin::GetForAllInterfaces(); | |
170 if (device_monitor) | |
171 self->device_observer_.Add(device_monitor); | |
172 } | |
173 | |
174 private: | |
175 // DeviceMonitorWin::Observer | |
176 void OnDeviceRemoved(const GUID& class_guid, | |
177 const std::string& device_path) override { | |
178 DCHECK(thread_checker_.CalledOnValidThread()); | |
179 io_thread_task_runner_->PostTask( | |
180 FROM_HERE, base::Bind(&SerialIoHandlerWin::OnDeviceRemoved, io_handler_, | |
181 device_path)); | |
182 } | |
183 | |
184 base::ThreadChecker thread_checker_; | |
185 ScopedObserver<DeviceMonitorWin, DeviceMonitorWin::Observer> device_observer_; | |
186 | |
187 // This weak pointer is only valid when checked on this task runner. | |
188 base::WeakPtr<SerialIoHandlerWin> io_handler_; | |
189 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner_; | |
190 | |
191 DISALLOW_COPY_AND_ASSIGN(UiThreadHelper); | |
192 }; | |
193 | |
194 void SerialIoHandlerWin::OnDeviceRemoved(const std::string& device_path) { | |
195 DCHECK(CalledOnValidThread()); | |
196 | |
197 device::DeviceInfoQueryWin device_info_query; | |
198 if (!device_info_query.device_info_list_valid()) { | |
199 DVPLOG(1) << "Failed to create a device information set"; | |
200 return; | |
201 } | |
202 | |
203 // This will add the device so we can query driver info. | |
204 if (!device_info_query.AddDevice(device_path.c_str())) { | |
205 DVPLOG(1) << "Failed to get device interface data for " << device_path; | |
206 return; | |
207 } | |
208 | |
209 if (!device_info_query.GetDeviceInfo()) { | |
210 DVPLOG(1) << "Failed to get device info for " << device_path; | |
211 return; | |
212 } | |
213 | |
214 BYTE friendly_name[512]; | |
215 if (!SetupDiGetDeviceRegistryPropertyA( | |
grt (UTC plus 2)
2015/11/16 16:14:16
how about wrapping this with something like:
st
juncai
2015/11/16 21:10:34
Done.
| |
216 device_info_query.device_info_list(), | |
217 device_info_query.device_info_data(), SPDRP_FRIENDLYNAME, NULL, | |
218 friendly_name, sizeof(friendly_name), NULL)) { | |
219 DVPLOG(1) << "Failed to get device service property"; | |
220 return; | |
221 } | |
222 | |
223 std::string com_port; | |
224 if (!GetCOMPort(reinterpret_cast<const char*>(friendly_name), &com_port)) { | |
225 DVPLOG(1) << "Failed to get port name from \"" << friendly_name << "\"."; | |
226 return; | |
227 } | |
228 | |
229 if (port() == com_port) | |
230 CancelRead(serial::RECEIVE_ERROR_DISCONNECTED); | |
231 } | |
232 | |
142 bool SerialIoHandlerWin::PostOpen() { | 233 bool SerialIoHandlerWin::PostOpen() { |
143 DCHECK(!comm_context_); | 234 DCHECK(!comm_context_); |
144 DCHECK(!read_context_); | 235 DCHECK(!read_context_); |
145 DCHECK(!write_context_); | 236 DCHECK(!write_context_); |
146 | 237 |
147 base::MessageLoopForIO::current()->RegisterIOHandler(file().GetPlatformFile(), | 238 base::MessageLoopForIO::current()->RegisterIOHandler(file().GetPlatformFile(), |
148 this); | 239 this); |
149 | 240 |
150 comm_context_.reset(new base::MessageLoopForIO::IOContext()); | 241 comm_context_.reset(new base::MessageLoopForIO::IOContext()); |
151 comm_context_->handler = this; | 242 comm_context_->handler = this; |
152 memset(&comm_context_->overlapped, 0, sizeof(comm_context_->overlapped)); | 243 memset(&comm_context_->overlapped, 0, sizeof(comm_context_->overlapped)); |
153 | 244 |
154 read_context_.reset(new base::MessageLoopForIO::IOContext()); | 245 read_context_.reset(new base::MessageLoopForIO::IOContext()); |
155 read_context_->handler = this; | 246 read_context_->handler = this; |
156 memset(&read_context_->overlapped, 0, sizeof(read_context_->overlapped)); | 247 memset(&read_context_->overlapped, 0, sizeof(read_context_->overlapped)); |
157 | 248 |
158 write_context_.reset(new base::MessageLoopForIO::IOContext()); | 249 write_context_.reset(new base::MessageLoopForIO::IOContext()); |
159 write_context_->handler = this; | 250 write_context_->handler = this; |
160 memset(&write_context_->overlapped, 0, sizeof(write_context_->overlapped)); | 251 memset(&write_context_->overlapped, 0, sizeof(write_context_->overlapped)); |
161 | 252 |
253 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner = | |
254 base::ThreadTaskRunnerHandle::Get(); | |
255 helper_ = | |
256 new UiThreadHelper(weak_factory_.GetWeakPtr(), io_thread_task_runner); | |
257 ui_thread_task_runner()->PostTask( | |
258 FROM_HERE, base::Bind(&UiThreadHelper::Start, helper_)); | |
259 | |
162 // A ReadIntervalTimeout of MAXDWORD will cause async reads to complete | 260 // A ReadIntervalTimeout of MAXDWORD will cause async reads to complete |
163 // immediately with any data that's available, even if there is none. | 261 // immediately with any data that's available, even if there is none. |
164 // This is OK because we never issue a read request until WaitCommEvent | 262 // This is OK because we never issue a read request until WaitCommEvent |
165 // signals that data is available. | 263 // signals that data is available. |
166 COMMTIMEOUTS timeouts = {0}; | 264 COMMTIMEOUTS timeouts = {0}; |
167 timeouts.ReadIntervalTimeout = MAXDWORD; | 265 timeouts.ReadIntervalTimeout = MAXDWORD; |
168 if (!::SetCommTimeouts(file().GetPlatformFile(), &timeouts)) { | 266 if (!::SetCommTimeouts(file().GetPlatformFile(), &timeouts)) { |
169 VPLOG(1) << "Failed to set serial timeouts"; | 267 VPLOG(1) << "Failed to set serial timeouts"; |
170 return false; | 268 return false; |
171 } | 269 } |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
264 return false; | 362 return false; |
265 } | 363 } |
266 return true; | 364 return true; |
267 } | 365 } |
268 | 366 |
269 SerialIoHandlerWin::SerialIoHandlerWin( | 367 SerialIoHandlerWin::SerialIoHandlerWin( |
270 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, | 368 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, |
271 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner) | 369 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner) |
272 : SerialIoHandler(file_thread_task_runner, ui_thread_task_runner), | 370 : SerialIoHandler(file_thread_task_runner, ui_thread_task_runner), |
273 event_mask_(0), | 371 event_mask_(0), |
274 is_comm_pending_(false) { | 372 is_comm_pending_(false), |
275 } | 373 weak_factory_(this) {} |
276 | 374 |
277 SerialIoHandlerWin::~SerialIoHandlerWin() { | 375 SerialIoHandlerWin::~SerialIoHandlerWin() { |
376 ui_thread_task_runner()->DeleteSoon(FROM_HERE, helper_); | |
278 } | 377 } |
279 | 378 |
280 void SerialIoHandlerWin::OnIOCompleted( | 379 void SerialIoHandlerWin::OnIOCompleted( |
281 base::MessageLoopForIO::IOContext* context, | 380 base::MessageLoopForIO::IOContext* context, |
282 DWORD bytes_transferred, | 381 DWORD bytes_transferred, |
283 DWORD error) { | 382 DWORD error) { |
284 DCHECK(CalledOnValidThread()); | 383 DCHECK(CalledOnValidThread()); |
285 if (context == comm_context_) { | 384 if (context == comm_context_) { |
286 DWORD errors; | 385 DWORD errors; |
287 COMSTAT status; | 386 COMSTAT status; |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
434 std::string SerialIoHandler::MaybeFixUpPortName(const std::string& port_name) { | 533 std::string SerialIoHandler::MaybeFixUpPortName(const std::string& port_name) { |
435 // For COM numbers less than 9, CreateFile is called with a string such as | 534 // For COM numbers less than 9, CreateFile is called with a string such as |
436 // "COM1". For numbers greater than 9, a prefix of "\\\\.\\" must be added. | 535 // "COM1". For numbers greater than 9, a prefix of "\\\\.\\" must be added. |
437 if (port_name.length() > std::string("COM9").length()) | 536 if (port_name.length() > std::string("COM9").length()) |
438 return std::string("\\\\.\\").append(port_name); | 537 return std::string("\\\\.\\").append(port_name); |
439 | 538 |
440 return port_name; | 539 return port_name; |
441 } | 540 } |
442 | 541 |
443 } // namespace device | 542 } // namespace device |
OLD | NEW |