| 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_posix.h" | 5 #include "device/serial/serial_io_handler_posix.h" |
| 6 | 6 |
| 7 #include <sys/ioctl.h> | 7 #include <sys/ioctl.h> |
| 8 #include <termios.h> | 8 #include <termios.h> |
| 9 | 9 |
| 10 #include "base/posix/eintr_wrapper.h" | 10 #include "base/posix/eintr_wrapper.h" |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner) { | 116 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner) { |
| 117 return new SerialIoHandlerPosix(file_thread_task_runner, | 117 return new SerialIoHandlerPosix(file_thread_task_runner, |
| 118 ui_thread_task_runner); | 118 ui_thread_task_runner); |
| 119 } | 119 } |
| 120 | 120 |
| 121 void SerialIoHandlerPosix::ReadImpl() { | 121 void SerialIoHandlerPosix::ReadImpl() { |
| 122 DCHECK(CalledOnValidThread()); | 122 DCHECK(CalledOnValidThread()); |
| 123 DCHECK(pending_read_buffer()); | 123 DCHECK(pending_read_buffer()); |
| 124 DCHECK(file().IsValid()); | 124 DCHECK(file().IsValid()); |
| 125 | 125 |
| 126 EnsureWatchingReads(); | |
| 127 | |
| 128 // Try to read immediately. This is needed because on some platforms | 126 // Try to read immediately. This is needed because on some platforms |
| 129 // (e.g., OSX) there may not be a notification from the message loop | 127 // (e.g., OSX) there may not be a notification from the message loop |
| 130 // when the fd is ready to read immediately after it is opened. There | 128 // when the fd is ready to read immediately after it is opened. There |
| 131 // is no danger of blocking because the fd is opened with async flag. | 129 // is no danger of blocking because the fd is opened with async flag. |
| 132 AttemptRead(true); | 130 AttemptRead(true); |
| 133 } | 131 } |
| 134 | 132 |
| 135 void SerialIoHandlerPosix::WriteImpl() { | 133 void SerialIoHandlerPosix::WriteImpl() { |
| 136 DCHECK(CalledOnValidThread()); | 134 DCHECK(CalledOnValidThread()); |
| 137 DCHECK(pending_write_buffer()); | 135 DCHECK(pending_write_buffer()); |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 296 SerialIoHandlerPosix::~SerialIoHandlerPosix() { | 294 SerialIoHandlerPosix::~SerialIoHandlerPosix() { |
| 297 } | 295 } |
| 298 | 296 |
| 299 void SerialIoHandlerPosix::OnFileCanReadWithoutBlocking(int fd) { | 297 void SerialIoHandlerPosix::OnFileCanReadWithoutBlocking(int fd) { |
| 300 DCHECK(CalledOnValidThread()); | 298 DCHECK(CalledOnValidThread()); |
| 301 DCHECK_EQ(fd, file().GetPlatformFile()); | 299 DCHECK_EQ(fd, file().GetPlatformFile()); |
| 302 | 300 |
| 303 AttemptRead(false); | 301 AttemptRead(false); |
| 304 } | 302 } |
| 305 | 303 |
| 306 void SerialIoHandlerPosix::AttemptRead(bool within_read) { | 304 bool SerialIoHandlerPosix::AttemptRead(bool within_read) { |
| 307 if (pending_read_buffer()) { | 305 if (pending_read_buffer()) { |
| 308 int bytes_read = HANDLE_EINTR(read(file().GetPlatformFile(), | 306 int bytes_read = HANDLE_EINTR(read(file().GetPlatformFile(), |
| 309 pending_read_buffer(), | 307 pending_read_buffer(), |
| 310 pending_read_buffer_len())); | 308 pending_read_buffer_len())); |
| 311 if (bytes_read < 0) { | 309 if (bytes_read < 0) { |
| 312 if (errno == EAGAIN) { | 310 if (errno == EAGAIN) { |
| 313 // The fd does not have data to read yet so continue waiting. | 311 // The fd does not have data to read yet so continue waiting. |
| 314 return; | 312 EnsureWatchingReads(); |
| 315 } else if (errno == ENXIO) { | 313 } else if (errno == ENXIO) { |
| 316 RunReadCompleted(within_read, 0, serial::ReceiveError::DEVICE_LOST); | 314 RunReadCompleted(within_read, 0, serial::ReceiveError::DEVICE_LOST); |
| 317 } else { | 315 } else { |
| 318 RunReadCompleted(within_read, 0, serial::ReceiveError::SYSTEM_ERROR); | 316 RunReadCompleted(within_read, 0, serial::ReceiveError::SYSTEM_ERROR); |
| 319 } | 317 } |
| 320 } else if (bytes_read == 0) { | 318 } else if (bytes_read == 0) { |
| 321 RunReadCompleted(within_read, 0, serial::ReceiveError::DEVICE_LOST); | 319 RunReadCompleted(within_read, 0, serial::ReceiveError::DEVICE_LOST); |
| 322 } else { | 320 } else { |
| 323 bool break_detected = false; | 321 bool break_detected = false; |
| 324 bool parity_error_detected = false; | 322 bool parity_error_detected = false; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 336 RunReadCompleted(within_read, new_bytes_read, | 334 RunReadCompleted(within_read, new_bytes_read, |
| 337 serial::ReceiveError::NONE); | 335 serial::ReceiveError::NONE); |
| 338 } | 336 } |
| 339 } | 337 } |
| 340 } else { | 338 } else { |
| 341 // Stop watching the fd if we get notifications with no pending | 339 // Stop watching the fd if we get notifications with no pending |
| 342 // reads or writes to avoid starving the message loop. | 340 // reads or writes to avoid starving the message loop. |
| 343 is_watching_reads_ = false; | 341 is_watching_reads_ = false; |
| 344 file_read_watcher_.StopWatchingFileDescriptor(); | 342 file_read_watcher_.StopWatchingFileDescriptor(); |
| 345 } | 343 } |
| 344 |
| 345 return true; |
| 346 } | 346 } |
| 347 | 347 |
| 348 void SerialIoHandlerPosix::RunReadCompleted(bool within_read, | 348 void SerialIoHandlerPosix::RunReadCompleted(bool within_read, |
| 349 int bytes_read, | 349 int bytes_read, |
| 350 serial::ReceiveError error) { | 350 serial::ReceiveError error) { |
| 351 if (within_read) | 351 if (within_read) |
| 352 QueueReadCompleted(bytes_read, error); | 352 QueueReadCompleted(bytes_read, error); |
| 353 else | 353 else |
| 354 ReadCompleted(bytes_read, error); | 354 ReadCompleted(bytes_read, error); |
| 355 } | 355 } |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 637 memcpy(buffer, chars_stashed_, std::min(new_bytes_read, 2)); | 637 memcpy(buffer, chars_stashed_, std::min(new_bytes_read, 2)); |
| 638 memcpy(chars_stashed_, tmp, num_chars_stashed_); | 638 memcpy(chars_stashed_, tmp, num_chars_stashed_); |
| 639 return new_bytes_read; | 639 return new_bytes_read; |
| 640 } | 640 } |
| 641 | 641 |
| 642 std::string SerialIoHandler::MaybeFixUpPortName(const std::string& port_name) { | 642 std::string SerialIoHandler::MaybeFixUpPortName(const std::string& port_name) { |
| 643 return port_name; | 643 return port_name; |
| 644 } | 644 } |
| 645 | 645 |
| 646 } // namespace device | 646 } // namespace device |
| OLD | NEW |