chromium/services/device/serial/serial_io_handler_posix.cc

// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#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>

// The definition of struct termios2 is copied from asm-generic/termbits.h
// because including that header directly conflicts with termios.h.
extern "C" {
struct termios2 {};
}

#endif  // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)

#if BUILDFLAG(IS_MAC)
#include <IOKit/serial/ioss.h>
#endif

namespace {

// Convert an integral bit rate to a nominal one. Returns |true|
// if the conversion was successful and |false| otherwise.
bool BitrateToSpeedConstant(int bitrate, speed_t* speed) {}

#if !BUILDFLAG(IS_LINUX) && !BUILDFLAG(IS_CHROMEOS)
// Convert a known nominal speed into an integral bitrate. Returns |true|
// if the conversion was successful and |false| otherwise.
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

namespace device {

// static
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 {}

// break sequence:
// '\377'       -->        ErrorDetectState::MARK_377_SEEN
// '\0'         -->          ErrorDetectState::MARK_0_SEEN
// '\0'         -->                         break detected
//
// parity error sequence:
// '\377'       -->        ErrorDetectState::MARK_377_SEEN
// '\0'         -->          ErrorDetectState::MARK_0_SEEN
// character with parity error  -->  parity error detected
//
// break/parity error sequences are removed from the byte stream
// '\377' '\377' sequence is replaced with '\377'
size_t SerialIoHandlerPosix::CheckReceiveError(base::span<uint8_t> buffer,
                                               size_t bytes_read,
                                               bool& break_detected,
                                               bool& parity_error_detected) {}

}  // namespace device