1 // Copyright 2017 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 #include "services/device/serial/serial_port_impl.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/task/post_task.h"
12 #include "mojo/public/cpp/bindings/strong_binding.h"
13 #include "services/device/serial/buffer.h"
14 #include "services/device/serial/serial_io_handler.h"
19 void SerialPortImpl::Create(
20 const base::FilePath& path,
21 mojom::SerialPortRequest request,
22 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner) {
23 mojo::MakeStrongBinding(
24 std::make_unique<SerialPortImpl>(path, ui_task_runner),
28 SerialPortImpl::SerialPortImpl(
29 const base::FilePath& path,
30 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner)
31 : io_handler_(device::SerialIoHandler::Create(path, ui_task_runner)) {}
33 SerialPortImpl::~SerialPortImpl() = default;
35 void SerialPortImpl::Open(mojom::SerialConnectionOptionsPtr options,
36 OpenCallback callback) {
37 io_handler_->Open(*options, std::move(callback));
40 void SerialPortImpl::Read(uint32_t bytes, ReadCallback callback) {
41 auto buffer = base::MakeRefCounted<net::IOBuffer>(static_cast<size_t>(bytes));
42 io_handler_->Read(std::make_unique<ReceiveBuffer>(
45 [](ReadCallback callback, scoped_refptr<net::IOBuffer> buffer,
46 int bytes_read, mojom::SerialReceiveError error) {
47 std::move(callback).Run(
48 std::vector<uint8_t>(buffer->data(),
49 buffer->data() + bytes_read),
52 std::move(callback), buffer)));
55 void SerialPortImpl::Write(const std::vector<uint8_t>& data,
56 WriteCallback callback) {
57 io_handler_->Write(std::make_unique<SendBuffer>(
59 [](WriteCallback callback, int bytes_sent,
60 mojom::SerialSendError error) {
61 std::move(callback).Run(bytes_sent, error);
63 std::move(callback))));
66 void SerialPortImpl::CancelRead(mojom::SerialReceiveError reason) {
67 io_handler_->CancelRead(reason);
70 void SerialPortImpl::CancelWrite(mojom::SerialSendError reason) {
71 io_handler_->CancelWrite(reason);
74 void SerialPortImpl::Flush(FlushCallback callback) {
75 std::move(callback).Run(io_handler_->Flush());
78 void SerialPortImpl::GetControlSignals(GetControlSignalsCallback callback) {
79 std::move(callback).Run(io_handler_->GetControlSignals());
82 void SerialPortImpl::SetControlSignals(
83 mojom::SerialHostControlSignalsPtr signals,
84 SetControlSignalsCallback callback) {
85 std::move(callback).Run(io_handler_->SetControlSignals(*signals));
88 void SerialPortImpl::ConfigurePort(mojom::SerialConnectionOptionsPtr options,
89 ConfigurePortCallback callback) {
90 std::move(callback).Run(io_handler_->ConfigurePort(*options));
93 void SerialPortImpl::GetPortInfo(GetPortInfoCallback callback) {
94 std::move(callback).Run(io_handler_->GetPortInfo());
97 void SerialPortImpl::SetBreak(SetBreakCallback callback) {
98 std::move(callback).Run(io_handler_->SetBreak());
101 void SerialPortImpl::ClearBreak(ClearBreakCallback callback) {
102 std::move(callback).Run(io_handler_->ClearBreak());
105 } // namespace device