| 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 #ifndef DEVICE_SERIAL_SERIAL_IO_HANDLER_POSIX_H_ | 5 #ifndef DEVICE_SERIAL_SERIAL_IO_HANDLER_POSIX_H_ |
| 6 #define DEVICE_SERIAL_SERIAL_IO_HANDLER_POSIX_H_ | 6 #define DEVICE_SERIAL_SERIAL_IO_HANDLER_POSIX_H_ |
| 7 | 7 |
| 8 #include <memory> |
| 9 |
| 10 #include "base/files/file_descriptor_watcher_posix.h" |
| 8 #include "base/macros.h" | 11 #include "base/macros.h" |
| 9 #include "base/message_loop/message_loop.h" | |
| 10 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 11 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 12 #include "device/serial/serial_io_handler.h" | 14 #include "device/serial/serial_io_handler.h" |
| 13 | 15 |
| 14 namespace device { | 16 namespace device { |
| 15 | 17 |
| 16 // Linux reports breaks and parity errors by inserting the sequence '\377\0x' | 18 // Linux reports breaks and parity errors by inserting the sequence '\377\0x' |
| 17 // into the byte stream where 'x' is '\0' for a break and the corrupted byte for | 19 // into the byte stream where 'x' is '\0' for a break and the corrupted byte for |
| 18 // a parity error. | 20 // a parity error. |
| 19 enum class ErrorDetectState { NO_ERROR, MARK_377_SEEN, MARK_0_SEEN }; | 21 enum class ErrorDetectState { NO_ERROR, MARK_377_SEEN, MARK_0_SEEN }; |
| 20 | 22 |
| 21 class SerialIoHandlerPosix : public SerialIoHandler, | 23 class SerialIoHandlerPosix : public SerialIoHandler { |
| 22 public base::MessageLoopForIO::Watcher { | |
| 23 protected: | 24 protected: |
| 24 // SerialIoHandler impl. | 25 // SerialIoHandler impl. |
| 25 void ReadImpl() override; | 26 void ReadImpl() override; |
| 26 void WriteImpl() override; | 27 void WriteImpl() override; |
| 27 void CancelReadImpl() override; | 28 void CancelReadImpl() override; |
| 28 void CancelWriteImpl() override; | 29 void CancelWriteImpl() override; |
| 29 bool ConfigurePortImpl() override; | 30 bool ConfigurePortImpl() override; |
| 30 bool Flush() const override; | 31 bool Flush() const override; |
| 31 serial::DeviceControlSignalsPtr GetControlSignals() const override; | 32 serial::DeviceControlSignalsPtr GetControlSignals() const override; |
| 32 bool SetControlSignals( | 33 bool SetControlSignals( |
| 33 const serial::HostControlSignals& control_signals) override; | 34 const serial::HostControlSignals& control_signals) override; |
| 34 serial::ConnectionInfoPtr GetPortInfo() const override; | 35 serial::ConnectionInfoPtr GetPortInfo() const override; |
| 35 bool SetBreak() override; | 36 bool SetBreak() override; |
| 36 bool ClearBreak() override; | 37 bool ClearBreak() override; |
| 37 int CheckReceiveError(char* buffer, | 38 int CheckReceiveError(char* buffer, |
| 38 int buffer_len, | 39 int buffer_len, |
| 39 int bytes_read, | 40 int bytes_read, |
| 40 bool& break_detected, | 41 bool& break_detected, |
| 41 bool& parity_error_detected); | 42 bool& parity_error_detected); |
| 42 | 43 |
| 43 private: | 44 private: |
| 44 friend class SerialIoHandler; | 45 friend class SerialIoHandler; |
| 45 friend class SerialIoHandlerPosixTest; | 46 friend class SerialIoHandlerPosixTest; |
| 46 | 47 |
| 47 SerialIoHandlerPosix( | 48 SerialIoHandlerPosix( |
| 48 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, | 49 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, |
| 49 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner); | 50 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner); |
| 50 ~SerialIoHandlerPosix() override; | 51 ~SerialIoHandlerPosix() override; |
| 51 | 52 |
| 52 bool AttemptRead(bool within_read); | 53 void AttemptRead(bool within_read); |
| 53 void RunReadCompleted(bool within_read, | 54 void RunReadCompleted(bool within_read, |
| 54 int bytes_read, | 55 int bytes_read, |
| 55 serial::ReceiveError error); | 56 serial::ReceiveError error); |
| 56 | 57 |
| 57 // base::MessageLoopForIO::Watcher implementation. | 58 // Called when file() is writable without blocking. |
| 58 void OnFileCanWriteWithoutBlocking(int fd) override; | 59 void OnFileCanWriteWithoutBlocking(); |
| 59 void OnFileCanReadWithoutBlocking(int fd) override; | |
| 60 | 60 |
| 61 void EnsureWatchingReads(); | 61 void EnsureWatchingReads(); |
| 62 void EnsureWatchingWrites(); | 62 void EnsureWatchingWrites(); |
| 63 | 63 |
| 64 base::MessageLoopForIO::FileDescriptorWatcher file_read_watcher_; | 64 std::unique_ptr<base::FileDescriptorWatcher::Controller> file_read_watcher_; |
| 65 base::MessageLoopForIO::FileDescriptorWatcher file_write_watcher_; | 65 std::unique_ptr<base::FileDescriptorWatcher::Controller> file_write_watcher_; |
| 66 | |
| 67 // Flags indicating if the message loop is watching the device for IO events. | |
| 68 bool is_watching_reads_; | |
| 69 bool is_watching_writes_; | |
| 70 | 66 |
| 71 ErrorDetectState error_detect_state_; | 67 ErrorDetectState error_detect_state_; |
| 72 bool parity_check_enabled_; | 68 bool parity_check_enabled_; |
| 73 char chars_stashed_[2]; | 69 char chars_stashed_[2]; |
| 74 int num_chars_stashed_; | 70 int num_chars_stashed_; |
| 75 | 71 |
| 76 DISALLOW_COPY_AND_ASSIGN(SerialIoHandlerPosix); | 72 DISALLOW_COPY_AND_ASSIGN(SerialIoHandlerPosix); |
| 77 }; | 73 }; |
| 78 | 74 |
| 79 } // namespace device | 75 } // namespace device |
| 80 | 76 |
| 81 #endif // DEVICE_SERIAL_SERIAL_IO_HANDLER_POSIX_H_ | 77 #endif // DEVICE_SERIAL_SERIAL_IO_HANDLER_POSIX_H_ |
| OLD | NEW |