#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "services/device/serial/serial_io_handler_posix.h"
#include <sys/ioctl.h>
#include <termios.h>
#include <algorithm>
#include <utility>
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/posix/eintr_wrapper.h"
#include "base/task/single_thread_task_runner.h"
#include "build/build_config.h"
#include "components/device_event_log/device_event_log.h"
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
#include <asm-generic/ioctls.h>
#include <linux/serial.h>
extern "C" {
struct termios2 { … };
}
#endif
#if BUILDFLAG(IS_MAC)
#include <IOKit/serial/ioss.h>
#endif
namespace {
bool BitrateToSpeedConstant(int bitrate, speed_t* speed) { … }
#if !BUILDFLAG(IS_LINUX) && !BUILDFLAG(IS_CHROMEOS)
bool SpeedConstantToBitrate(speed_t speed, int* bitrate) {
#define SPEED_TO_BITRATE_CASE …
switch (speed) {
SPEED_TO_BITRATE_CASE(0)
SPEED_TO_BITRATE_CASE(50)
SPEED_TO_BITRATE_CASE(75)
SPEED_TO_BITRATE_CASE(110)
SPEED_TO_BITRATE_CASE(134)
SPEED_TO_BITRATE_CASE(150)
SPEED_TO_BITRATE_CASE(200)
SPEED_TO_BITRATE_CASE(300)
SPEED_TO_BITRATE_CASE(600)
SPEED_TO_BITRATE_CASE(1200)
SPEED_TO_BITRATE_CASE(1800)
SPEED_TO_BITRATE_CASE(2400)
SPEED_TO_BITRATE_CASE(4800)
SPEED_TO_BITRATE_CASE(9600)
SPEED_TO_BITRATE_CASE(19200)
SPEED_TO_BITRATE_CASE(38400)
default:
return false;
}
#undef SPEED_TO_BITRATE_CASE
}
#endif
}
namespace device {
scoped_refptr<SerialIoHandler> SerialIoHandler::Create(
const base::FilePath& port,
scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner) { … }
void SerialIoHandlerPosix::ReadImpl() { … }
void SerialIoHandlerPosix::WriteImpl() { … }
void SerialIoHandlerPosix::CancelReadImpl() { … }
void SerialIoHandlerPosix::CancelWriteImpl() { … }
bool SerialIoHandlerPosix::ConfigurePortImpl() { … }
bool SerialIoHandlerPosix::PostOpen() { … }
void SerialIoHandlerPosix::PreClose() { … }
SerialIoHandlerPosix::SerialIoHandlerPosix(
const base::FilePath& port,
scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner)
: … { … }
SerialIoHandlerPosix::~SerialIoHandlerPosix() = default;
void SerialIoHandlerPosix::AttemptRead() { … }
void SerialIoHandlerPosix::OnFileCanWriteWithoutBlocking() { … }
void SerialIoHandlerPosix::EnsureWatchingReads() { … }
void SerialIoHandlerPosix::EnsureWatchingWrites() { … }
void SerialIoHandlerPosix::StopWatchingFileRead() { … }
void SerialIoHandlerPosix::StopWatchingFileWrite() { … }
void SerialIoHandlerPosix::Flush(mojom::SerialPortFlushMode mode) const { … }
void SerialIoHandlerPosix::Drain() { … }
mojom::SerialPortControlSignalsPtr SerialIoHandlerPosix::GetControlSignals()
const { … }
bool SerialIoHandlerPosix::SetControlSignals(
const mojom::SerialHostControlSignals& signals) { … }
mojom::SerialConnectionInfoPtr SerialIoHandlerPosix::GetPortInfo() const { … }
size_t SerialIoHandlerPosix::CheckReceiveError(base::span<uint8_t> buffer,
size_t bytes_read,
bool& break_detected,
bool& parity_error_detected) { … }
}