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 OpenCompleteCallback
& callback
) {
33 bool TestSerialIoHandler::ConfigurePort(
34 const serial::ConnectionOptions
& options
) {
36 info_
.bitrate
= options
.bitrate
;
37 if (options
.data_bits
!= serial::DATA_BITS_NONE
)
38 info_
.data_bits
= options
.data_bits
;
39 if (options
.parity_bit
!= serial::PARITY_BIT_NONE
)
40 info_
.parity_bit
= options
.parity_bit
;
41 if (options
.stop_bits
!= serial::STOP_BITS_NONE
)
42 info_
.stop_bits
= options
.stop_bits
;
43 if (options
.has_cts_flow_control
)
44 info_
.cts_flow_control
= options
.cts_flow_control
;
48 void TestSerialIoHandler::ReadImpl() {
49 if (!pending_read_buffer())
55 std::min(buffer_
.size(), static_cast<size_t>(pending_read_buffer_len()));
56 memcpy(pending_read_buffer(), buffer_
.c_str(), num_bytes
);
57 buffer_
= buffer_
.substr(num_bytes
);
58 ReadCompleted(static_cast<uint32_t>(num_bytes
), serial::RECEIVE_ERROR_NONE
);
61 void TestSerialIoHandler::CancelReadImpl() {
62 ReadCompleted(0, read_cancel_reason());
65 void TestSerialIoHandler::WriteImpl() {
66 if (!send_callback_
.is_null()) {
67 base::Closure callback
= send_callback_
;
68 send_callback_
.Reset();
72 buffer_
+= std::string(pending_write_buffer(), pending_write_buffer_len());
73 WriteCompleted(pending_write_buffer_len(), serial::SEND_ERROR_NONE
);
74 if (pending_read_buffer())
78 void TestSerialIoHandler::CancelWriteImpl() {
79 WriteCompleted(0, write_cancel_reason());
82 serial::DeviceControlSignalsPtr
TestSerialIoHandler::GetControlSignals() const {
83 serial::DeviceControlSignalsPtr
signals(serial::DeviceControlSignals::New());
84 *signals
= device_control_signals_
;
85 return signals
.Pass();
88 serial::ConnectionInfoPtr
TestSerialIoHandler::GetPortInfo() const {
89 serial::ConnectionInfoPtr
info(serial::ConnectionInfo::New());
94 bool TestSerialIoHandler::Flush() const {
99 bool TestSerialIoHandler::SetControlSignals(
100 const serial::HostControlSignals
& signals
) {
108 TestSerialIoHandler::~TestSerialIoHandler() {
111 } // namespace device