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 #include "device/serial/serial_connection.h"
8 #include "device/serial/buffer.h"
9 #include "device/serial/data_sink_receiver.h"
10 #include "device/serial/data_source_sender.h"
11 #include "device/serial/serial_io_handler.h"
15 SerialConnection::SerialConnection(
16 scoped_refptr
<SerialIoHandler
> io_handler
,
17 mojo::InterfaceRequest
<serial::DataSink
> sink
,
18 mojo::InterfaceRequest
<serial::DataSource
> source
,
19 mojo::InterfacePtr
<serial::DataSourceClient
> source_client
,
20 mojo::InterfaceRequest
<serial::Connection
> request
)
21 : io_handler_(io_handler
), binding_(this, request
.Pass()) {
22 receiver_
= new DataSinkReceiver(
24 base::Bind(&SerialConnection::OnSendPipeReady
, base::Unretained(this)),
25 base::Bind(&SerialConnection::OnSendCancelled
, base::Unretained(this)),
26 base::Bind(base::DoNothing
));
27 sender_
= new DataSourceSender(
28 source
.Pass(), source_client
.Pass(),
29 base::Bind(&SerialConnection::OnReceivePipeReady
, base::Unretained(this)),
30 base::Bind(base::DoNothing
));
33 SerialConnection::~SerialConnection() {
34 receiver_
->ShutDown();
36 io_handler_
->CancelRead(serial::RECEIVE_ERROR_DISCONNECTED
);
37 io_handler_
->CancelWrite(serial::SEND_ERROR_DISCONNECTED
);
40 void SerialConnection::GetInfo(
41 const mojo::Callback
<void(serial::ConnectionInfoPtr
)>& callback
) {
42 callback
.Run(io_handler_
->GetPortInfo());
45 void SerialConnection::SetOptions(serial::ConnectionOptionsPtr options
,
46 const mojo::Callback
<void(bool)>& callback
) {
47 callback
.Run(io_handler_
->ConfigurePort(*options
));
48 io_handler_
->CancelRead(device::serial::RECEIVE_ERROR_NONE
);
51 void SerialConnection::SetControlSignals(
52 serial::HostControlSignalsPtr signals
,
53 const mojo::Callback
<void(bool)>& callback
) {
54 callback
.Run(io_handler_
->SetControlSignals(*signals
));
57 void SerialConnection::GetControlSignals(
58 const mojo::Callback
<void(serial::DeviceControlSignalsPtr
)>& callback
) {
59 callback
.Run(io_handler_
->GetControlSignals());
62 void SerialConnection::Flush(const mojo::Callback
<void(bool)>& callback
) {
63 callback
.Run(io_handler_
->Flush());
66 void SerialConnection::OnSendCancelled(int32_t error
) {
67 io_handler_
->CancelWrite(static_cast<serial::SendError
>(error
));
70 void SerialConnection::OnSendPipeReady(scoped_ptr
<ReadOnlyBuffer
> buffer
) {
71 io_handler_
->Write(buffer
.Pass());
74 void SerialConnection::OnReceivePipeReady(scoped_ptr
<WritableBuffer
> buffer
) {
75 io_handler_
->Read(buffer
.Pass());