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_
10 #include "base/files/file_descriptor_watcher_posix.h"
11 #include "base/macros.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/threading/thread_task_runner_handle.h"
14 #include "device/serial/serial_io_handler.h"
18 // Linux reports breaks and parity errors by inserting the sequence '\377\0x'
19 // into the byte stream where 'x' is '\0' for a break and the corrupted byte for
21 enum class ErrorDetectState { NO_ERROR, MARK_377_SEEN, MARK_0_SEEN };
23 class SerialIoHandlerPosix : public SerialIoHandler {
25 // SerialIoHandler impl.
26 void ReadImpl() override;
27 void WriteImpl() override;
28 void CancelReadImpl() override;
29 void CancelWriteImpl() override;
30 bool ConfigurePortImpl() override;
31 bool PostOpen() override;
32 bool Flush() const override;
33 mojom::SerialDeviceControlSignalsPtr GetControlSignals() const override;
34 bool SetControlSignals(
35 const mojom::SerialHostControlSignals& control_signals) override;
36 mojom::SerialConnectionInfoPtr GetPortInfo() const override;
37 bool SetBreak() override;
38 bool ClearBreak() override;
39 int CheckReceiveError(char* buffer,
43 bool& parity_error_detected);
46 friend class SerialIoHandler;
47 friend class SerialIoHandlerPosixTest;
50 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner);
51 ~SerialIoHandlerPosix() override;
53 void AttemptRead(bool within_read);
54 void RunReadCompleted(bool within_read,
56 mojom::SerialReceiveError error);
58 // Called when file() is writable without blocking.
59 void OnFileCanWriteWithoutBlocking();
61 void EnsureWatchingReads();
62 void EnsureWatchingWrites();
64 std::unique_ptr<base::FileDescriptorWatcher::Controller> file_read_watcher_;
65 std::unique_ptr<base::FileDescriptorWatcher::Controller> file_write_watcher_;
67 ErrorDetectState error_detect_state_;
68 bool parity_check_enabled_;
69 char chars_stashed_[2];
70 int num_chars_stashed_;
72 DISALLOW_COPY_AND_ASSIGN(SerialIoHandlerPosix);
77 #endif // DEVICE_SERIAL_SERIAL_IO_HANDLER_POSIX_H_