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/test_serial_io_handler.h"
10 #include "device/serial/serial.mojom.h"
14 TestSerialIoHandler::TestSerialIoHandler()
15 : SerialIoHandler(NULL
, NULL
),
22 scoped_refptr
<SerialIoHandler
> TestSerialIoHandler::Create() {
23 return scoped_refptr
<SerialIoHandler
>(new TestSerialIoHandler
);
26 void TestSerialIoHandler::Open(const std::string
& port
,
27 const serial::ConnectionOptions
& options
,
28 const OpenCompleteCallback
& callback
) {
31 ConfigurePort(options
);
35 void TestSerialIoHandler::ReadImpl() {
36 if (!pending_read_buffer())
42 std::min(buffer_
.size(), static_cast<size_t>(pending_read_buffer_len()));
43 memcpy(pending_read_buffer(), buffer_
.c_str(), num_bytes
);
44 buffer_
= buffer_
.substr(num_bytes
);
45 ReadCompleted(static_cast<uint32_t>(num_bytes
), serial::RECEIVE_ERROR_NONE
);
48 void TestSerialIoHandler::CancelReadImpl() {
49 ReadCompleted(0, read_cancel_reason());
52 void TestSerialIoHandler::WriteImpl() {
53 if (!send_callback_
.is_null()) {
54 base::Closure callback
= send_callback_
;
55 send_callback_
.Reset();
59 buffer_
+= std::string(pending_write_buffer(), pending_write_buffer_len());
60 WriteCompleted(pending_write_buffer_len(), serial::SEND_ERROR_NONE
);
61 if (pending_read_buffer())
65 void TestSerialIoHandler::CancelWriteImpl() {
66 WriteCompleted(0, write_cancel_reason());
69 bool TestSerialIoHandler::ConfigurePortImpl() {
70 info_
.bitrate
= options().bitrate
;
71 info_
.data_bits
= options().data_bits
;
72 info_
.parity_bit
= options().parity_bit
;
73 info_
.stop_bits
= options().stop_bits
;
74 info_
.cts_flow_control
= options().cts_flow_control
;
78 serial::DeviceControlSignalsPtr
TestSerialIoHandler::GetControlSignals() const {
79 serial::DeviceControlSignalsPtr
signals(serial::DeviceControlSignals::New());
80 *signals
= device_control_signals_
;
81 return signals
.Pass();
84 serial::ConnectionInfoPtr
TestSerialIoHandler::GetPortInfo() const {
85 serial::ConnectionInfoPtr
info(serial::ConnectionInfo::New());
90 bool TestSerialIoHandler::Flush() const {
95 bool TestSerialIoHandler::SetControlSignals(
96 const serial::HostControlSignals
& signals
) {
104 bool TestSerialIoHandler::SetBreak() {
108 bool TestSerialIoHandler::ClearBreak() {
112 TestSerialIoHandler::~TestSerialIoHandler() {
115 } // namespace device