Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1536)

Side by Side Diff: device/serial/serial_io_handler_win.cc

Issue 1439443002: Reland: Add code to deal with serial device disconnection detection on Windows (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address grt@'s comments Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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;
grt (UTC plus 2) 2015/11/17 16:06:56 omit device::
juncai 2015/11/17 19:14:07 Done.
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 std::string friendly_name;
215 if (!device_info_query.GetDeviceStringProperty(SPDRP_FRIENDLYNAME,
216 &friendly_name)) {
217 DVPLOG(1) << "Failed to get device service property";
218 return;
219 }
220
221 std::string com_port;
222 if (!GetCOMPort(friendly_name, &com_port)) {
223 DVPLOG(1) << "Failed to get port name from \"" << friendly_name << "\".";
224 return;
225 }
226
227 if (port() == com_port)
228 CancelRead(serial::RECEIVE_ERROR_DISCONNECTED);
229 }
230
142 bool SerialIoHandlerWin::PostOpen() { 231 bool SerialIoHandlerWin::PostOpen() {
143 DCHECK(!comm_context_); 232 DCHECK(!comm_context_);
144 DCHECK(!read_context_); 233 DCHECK(!read_context_);
145 DCHECK(!write_context_); 234 DCHECK(!write_context_);
146 235
147 base::MessageLoopForIO::current()->RegisterIOHandler(file().GetPlatformFile(), 236 base::MessageLoopForIO::current()->RegisterIOHandler(file().GetPlatformFile(),
148 this); 237 this);
149 238
150 comm_context_.reset(new base::MessageLoopForIO::IOContext()); 239 comm_context_.reset(new base::MessageLoopForIO::IOContext());
151 comm_context_->handler = this; 240 comm_context_->handler = this;
152 memset(&comm_context_->overlapped, 0, sizeof(comm_context_->overlapped)); 241 memset(&comm_context_->overlapped, 0, sizeof(comm_context_->overlapped));
153 242
154 read_context_.reset(new base::MessageLoopForIO::IOContext()); 243 read_context_.reset(new base::MessageLoopForIO::IOContext());
155 read_context_->handler = this; 244 read_context_->handler = this;
156 memset(&read_context_->overlapped, 0, sizeof(read_context_->overlapped)); 245 memset(&read_context_->overlapped, 0, sizeof(read_context_->overlapped));
157 246
158 write_context_.reset(new base::MessageLoopForIO::IOContext()); 247 write_context_.reset(new base::MessageLoopForIO::IOContext());
159 write_context_->handler = this; 248 write_context_->handler = this;
160 memset(&write_context_->overlapped, 0, sizeof(write_context_->overlapped)); 249 memset(&write_context_->overlapped, 0, sizeof(write_context_->overlapped));
161 250
251 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner =
252 base::ThreadTaskRunnerHandle::Get();
253 helper_ =
254 new UiThreadHelper(weak_factory_.GetWeakPtr(), io_thread_task_runner);
255 ui_thread_task_runner()->PostTask(
256 FROM_HERE, base::Bind(&UiThreadHelper::Start, helper_));
257
162 // A ReadIntervalTimeout of MAXDWORD will cause async reads to complete 258 // A ReadIntervalTimeout of MAXDWORD will cause async reads to complete
163 // immediately with any data that's available, even if there is none. 259 // 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 260 // This is OK because we never issue a read request until WaitCommEvent
165 // signals that data is available. 261 // signals that data is available.
166 COMMTIMEOUTS timeouts = {0}; 262 COMMTIMEOUTS timeouts = {0};
167 timeouts.ReadIntervalTimeout = MAXDWORD; 263 timeouts.ReadIntervalTimeout = MAXDWORD;
168 if (!::SetCommTimeouts(file().GetPlatformFile(), &timeouts)) { 264 if (!::SetCommTimeouts(file().GetPlatformFile(), &timeouts)) {
169 VPLOG(1) << "Failed to set serial timeouts"; 265 VPLOG(1) << "Failed to set serial timeouts";
170 return false; 266 return false;
171 } 267 }
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 return false; 360 return false;
265 } 361 }
266 return true; 362 return true;
267 } 363 }
268 364
269 SerialIoHandlerWin::SerialIoHandlerWin( 365 SerialIoHandlerWin::SerialIoHandlerWin(
270 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, 366 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner,
271 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner) 367 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner)
272 : SerialIoHandler(file_thread_task_runner, ui_thread_task_runner), 368 : SerialIoHandler(file_thread_task_runner, ui_thread_task_runner),
273 event_mask_(0), 369 event_mask_(0),
274 is_comm_pending_(false) { 370 is_comm_pending_(false),
275 } 371 weak_factory_(this) {}
276 372
277 SerialIoHandlerWin::~SerialIoHandlerWin() { 373 SerialIoHandlerWin::~SerialIoHandlerWin() {
374 ui_thread_task_runner()->DeleteSoon(FROM_HERE, helper_);
278 } 375 }
279 376
280 void SerialIoHandlerWin::OnIOCompleted( 377 void SerialIoHandlerWin::OnIOCompleted(
281 base::MessageLoopForIO::IOContext* context, 378 base::MessageLoopForIO::IOContext* context,
282 DWORD bytes_transferred, 379 DWORD bytes_transferred,
283 DWORD error) { 380 DWORD error) {
284 DCHECK(CalledOnValidThread()); 381 DCHECK(CalledOnValidThread());
285 if (context == comm_context_) { 382 if (context == comm_context_) {
286 DWORD errors; 383 DWORD errors;
287 COMSTAT status; 384 COMSTAT status;
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
434 std::string SerialIoHandler::MaybeFixUpPortName(const std::string& port_name) { 531 std::string SerialIoHandler::MaybeFixUpPortName(const std::string& port_name) {
435 // For COM numbers less than 9, CreateFile is called with a string such as 532 // 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. 533 // "COM1". For numbers greater than 9, a prefix of "\\\\.\\" must be added.
437 if (port_name.length() > std::string("COM9").length()) 534 if (port_name.length() > std::string("COM9").length())
438 return std::string("\\\\.\\").append(port_name); 535 return std::string("\\\\.\\").append(port_name);
439 536
440 return port_name; 537 return port_name;
441 } 538 }
442 539
443 } // namespace device 540 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698