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
3 // found in the LICENSE file.
5 #ifndef DEVICE_SERIAL_SERIAL_IO_HANDLER_POSIX_H_
6 #define DEVICE_SERIAL_SERIAL_IO_HANDLER_POSIX_H_
8 #include "base/message_loop/message_loop.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "device/serial/serial_io_handler.h"
15 // Linux reports breaks and parity errors by inserting the sequence '\377\0x'
16 // into the byte stream where 'x' is '\0' for a break and the corrupted byte for
18 enum class ErrorDetectState
{ NO_ERROR
, MARK_377_SEEN
, MARK_0_SEEN
};
20 class SerialIoHandlerPosix
: public SerialIoHandler
,
21 public base::MessageLoopForIO::Watcher
{
23 // SerialIoHandler impl.
24 void ReadImpl() override
;
25 void WriteImpl() override
;
26 void CancelReadImpl() override
;
27 void CancelWriteImpl() override
;
28 bool ConfigurePortImpl() override
;
29 bool Flush() const override
;
30 serial::DeviceControlSignalsPtr
GetControlSignals() const override
;
31 bool SetControlSignals(
32 const serial::HostControlSignals
& control_signals
) override
;
33 serial::ConnectionInfoPtr
GetPortInfo() const override
;
34 bool SetBreak() override
;
35 bool ClearBreak() override
;
36 int CheckReceiveError(char* buffer
,
40 bool& parity_error_detected
);
43 friend class SerialIoHandler
;
44 friend class SerialIoHandlerPosixTest
;
47 scoped_refptr
<base::SingleThreadTaskRunner
> file_thread_task_runner
,
48 scoped_refptr
<base::SingleThreadTaskRunner
> ui_thread_task_runner
);
49 ~SerialIoHandlerPosix() override
;
51 // base::MessageLoopForIO::Watcher implementation.
52 void OnFileCanWriteWithoutBlocking(int fd
) override
;
53 void OnFileCanReadWithoutBlocking(int fd
) override
;
55 void EnsureWatchingReads();
56 void EnsureWatchingWrites();
58 base::MessageLoopForIO::FileDescriptorWatcher file_read_watcher_
;
59 base::MessageLoopForIO::FileDescriptorWatcher file_write_watcher_
;
61 // Flags indicating if the message loop is watching the device for IO events.
62 bool is_watching_reads_
;
63 bool is_watching_writes_
;
65 ErrorDetectState error_detect_state_
;
66 bool parity_check_enabled_
;
67 char chars_stashed_
[2];
68 int num_chars_stashed_
;
70 DISALLOW_COPY_AND_ASSIGN(SerialIoHandlerPosix
);
75 #endif // DEVICE_SERIAL_SERIAL_IO_HANDLER_POSIX_H_