| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "device/serial/serial_connection.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "device/serial/buffer.h" | |
| 11 #include "device/serial/data_sink_receiver.h" | |
| 12 #include "device/serial/data_source_sender.h" | |
| 13 #include "device/serial/serial_io_handler.h" | |
| 14 | |
| 15 namespace device { | |
| 16 | |
| 17 SerialConnection::SerialConnection( | |
| 18 scoped_refptr<SerialIoHandler> io_handler, | |
| 19 mojo::InterfaceRequest<serial::DataSink> sink, | |
| 20 mojo::InterfaceRequest<serial::DataSource> source, | |
| 21 mojo::InterfacePtr<serial::DataSourceClient> source_client) | |
| 22 : io_handler_(io_handler) { | |
| 23 receiver_ = new DataSinkReceiver( | |
| 24 std::move(sink), | |
| 25 base::Bind(&SerialConnection::OnSendPipeReady, base::Unretained(this)), | |
| 26 base::Bind(&SerialConnection::OnSendCancelled, base::Unretained(this)), | |
| 27 base::Bind(base::DoNothing)); | |
| 28 sender_ = new DataSourceSender( | |
| 29 std::move(source), std::move(source_client), | |
| 30 base::Bind(&SerialConnection::OnReceivePipeReady, base::Unretained(this)), | |
| 31 base::Bind(base::DoNothing)); | |
| 32 } | |
| 33 | |
| 34 SerialConnection::~SerialConnection() { | |
| 35 receiver_->ShutDown(); | |
| 36 sender_->ShutDown(); | |
| 37 io_handler_->CancelRead(serial::ReceiveError::DISCONNECTED); | |
| 38 io_handler_->CancelWrite(serial::SendError::DISCONNECTED); | |
| 39 } | |
| 40 | |
| 41 void SerialConnection::GetInfo(const GetInfoCallback& callback) { | |
| 42 callback.Run(io_handler_->GetPortInfo()); | |
| 43 } | |
| 44 | |
| 45 void SerialConnection::SetOptions(serial::ConnectionOptionsPtr options, | |
| 46 const SetOptionsCallback& callback) { | |
| 47 callback.Run(io_handler_->ConfigurePort(*options)); | |
| 48 io_handler_->CancelRead(device::serial::ReceiveError::NONE); | |
| 49 } | |
| 50 | |
| 51 void SerialConnection::SetControlSignals( | |
| 52 serial::HostControlSignalsPtr signals, | |
| 53 const SetControlSignalsCallback& callback) { | |
| 54 callback.Run(io_handler_->SetControlSignals(*signals)); | |
| 55 } | |
| 56 | |
| 57 void SerialConnection::GetControlSignals( | |
| 58 const GetControlSignalsCallback& callback) { | |
| 59 callback.Run(io_handler_->GetControlSignals()); | |
| 60 } | |
| 61 | |
| 62 void SerialConnection::Flush(const FlushCallback& callback) { | |
| 63 callback.Run(io_handler_->Flush()); | |
| 64 } | |
| 65 | |
| 66 void SerialConnection::OnSendCancelled(int32_t error) { | |
| 67 io_handler_->CancelWrite(static_cast<serial::SendError>(error)); | |
| 68 } | |
| 69 | |
| 70 void SerialConnection::OnSendPipeReady(std::unique_ptr<ReadOnlyBuffer> buffer) { | |
| 71 io_handler_->Write(std::move(buffer)); | |
| 72 } | |
| 73 | |
| 74 void SerialConnection::OnReceivePipeReady( | |
| 75 std::unique_ptr<WritableBuffer> buffer) { | |
| 76 io_handler_->Read(std::move(buffer)); | |
| 77 } | |
| 78 | |
| 79 } // namespace device | |
| OLD | NEW |