summaryrefslogtreecommitdiffstats
path: root/device/serial/serial_connection.cc
blob: 9b88c9d0e5da9a3550068c5bdc53066140a0e2ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "device/serial/serial_connection.h"

#include <utility>

#include "base/bind.h"
#include "device/serial/buffer.h"
#include "device/serial/data_sink_receiver.h"
#include "device/serial/data_source_sender.h"
#include "device/serial/serial_io_handler.h"

namespace device {

SerialConnection::SerialConnection(
    scoped_refptr<SerialIoHandler> io_handler,
    mojo::InterfaceRequest<serial::DataSink> sink,
    mojo::InterfaceRequest<serial::DataSource> source,
    mojo::InterfacePtr<serial::DataSourceClient> source_client,
    mojo::InterfaceRequest<serial::Connection> request)
    : io_handler_(io_handler), binding_(this, std::move(request)) {
  receiver_ = new DataSinkReceiver(
      std::move(sink),
      base::Bind(&SerialConnection::OnSendPipeReady, base::Unretained(this)),
      base::Bind(&SerialConnection::OnSendCancelled, base::Unretained(this)),
      base::Bind(base::DoNothing));
  sender_ = new DataSourceSender(
      std::move(source), std::move(source_client),
      base::Bind(&SerialConnection::OnReceivePipeReady, base::Unretained(this)),
      base::Bind(base::DoNothing));
}

SerialConnection::~SerialConnection() {
  receiver_->ShutDown();
  sender_->ShutDown();
  io_handler_->CancelRead(serial::ReceiveError::DISCONNECTED);
  io_handler_->CancelWrite(serial::SendError::DISCONNECTED);
}

void SerialConnection::GetInfo(
    const mojo::Callback<void(serial::ConnectionInfoPtr)>& callback) {
  callback.Run(io_handler_->GetPortInfo());
}

void SerialConnection::SetOptions(serial::ConnectionOptionsPtr options,
                                  const mojo::Callback<void(bool)>& callback) {
  callback.Run(io_handler_->ConfigurePort(*options));
  io_handler_->CancelRead(device::serial::ReceiveError::NONE);
}

void SerialConnection::SetControlSignals(
    serial::HostControlSignalsPtr signals,
    const mojo::Callback<void(bool)>& callback) {
  callback.Run(io_handler_->SetControlSignals(*signals));
}

void SerialConnection::GetControlSignals(
    const mojo::Callback<void(serial::DeviceControlSignalsPtr)>& callback) {
  callback.Run(io_handler_->GetControlSignals());
}

void SerialConnection::Flush(const mojo::Callback<void(bool)>& callback) {
  callback.Run(io_handler_->Flush());
}

void SerialConnection::OnSendCancelled(int32_t error) {
  io_handler_->CancelWrite(static_cast<serial::SendError>(error));
}

void SerialConnection::OnSendPipeReady(scoped_ptr<ReadOnlyBuffer> buffer) {
  io_handler_->Write(std::move(buffer));
}

void SerialConnection::OnReceivePipeReady(scoped_ptr<WritableBuffer> buffer) {
  io_handler_->Read(std::move(buffer));
}

}  // namespace device