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 : io_handler_(io_handler
) {
21 receiver_
= new DataSinkReceiver(
23 base::Bind(&SerialConnection::OnSendPipeReady
, base::Unretained(this)),
24 base::Bind(&SerialConnection::OnSendCancelled
, base::Unretained(this)),
25 base::Bind(base::DoNothing
));
26 sender_
= new DataSourceSender(
27 source
.Pass(), source_client
.Pass(),
28 base::Bind(&SerialConnection::OnReceivePipeReady
, base::Unretained(this)),
29 base::Bind(base::DoNothing
));
32 SerialConnection::~SerialConnection() {
33 receiver_
->ShutDown();
35 io_handler_
->CancelRead(serial::RECEIVE_ERROR_DISCONNECTED
);
36 io_handler_
->CancelWrite(serial::SEND_ERROR_DISCONNECTED
);
39 void SerialConnection::GetInfo(
40 const mojo::Callback
<void(serial::ConnectionInfoPtr
)>& callback
) {
41 callback
.Run(io_handler_
->GetPortInfo());
44 void SerialConnection::SetOptions(serial::ConnectionOptionsPtr options
,
45 const mojo::Callback
<void(bool)>& callback
) {
46 callback
.Run(io_handler_
->ConfigurePort(*options
));
47 io_handler_
->CancelRead(device::serial::RECEIVE_ERROR_NONE
);
50 void SerialConnection::SetControlSignals(
51 serial::HostControlSignalsPtr signals
,
52 const mojo::Callback
<void(bool)>& callback
) {
53 callback
.Run(io_handler_
->SetControlSignals(*signals
));
56 void SerialConnection::GetControlSignals(
57 const mojo::Callback
<void(serial::DeviceControlSignalsPtr
)>& callback
) {
58 callback
.Run(io_handler_
->GetControlSignals());
61 void SerialConnection::Flush(const mojo::Callback
<void(bool)>& callback
) {
62 callback
.Run(io_handler_
->Flush());
65 void SerialConnection::OnSendCancelled(int32_t error
) {
66 io_handler_
->CancelWrite(static_cast<serial::SendError
>(error
));
69 void SerialConnection::OnSendPipeReady(scoped_ptr
<ReadOnlyBuffer
> buffer
) {
70 io_handler_
->Write(buffer
.Pass());
73 void SerialConnection::OnReceivePipeReady(scoped_ptr
<WritableBuffer
> buffer
) {
74 io_handler_
->Read(buffer
.Pass());