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/test_serial_io_handler.h"
6
7 #include <algorithm>
8
9 #include "base/bind.h"
10 #include "device/serial/serial.mojom.h"
11
12 namespace device {
13
TestSerialIoHandler()14 TestSerialIoHandler::TestSerialIoHandler()
15 : SerialIoHandler(NULL),
16 opened_(false),
17 dtr_(false),
18 rts_(false),
19 flushes_(0) {
20 }
21
Create()22 scoped_refptr<SerialIoHandler> TestSerialIoHandler::Create() {
23 return scoped_refptr<SerialIoHandler>(new TestSerialIoHandler);
24 }
25
Open(const std::string & port,const OpenCompleteCallback & callback)26 void TestSerialIoHandler::Open(const std::string& port,
27 const OpenCompleteCallback& callback) {
28 DCHECK(!opened_);
29 opened_ = true;
30 callback.Run(true);
31 }
32
ConfigurePort(const serial::ConnectionOptions & options)33 bool TestSerialIoHandler::ConfigurePort(
34 const serial::ConnectionOptions& options) {
35 if (options.bitrate)
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;
45 return true;
46 }
47
ReadImpl()48 void TestSerialIoHandler::ReadImpl() {
49 if (!pending_read_buffer())
50 return;
51 if (buffer_.empty())
52 return;
53
54 size_t num_bytes =
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);
59 }
60
CancelReadImpl()61 void TestSerialIoHandler::CancelReadImpl() {
62 ReadCompleted(0, read_cancel_reason());
63 }
64
WriteImpl()65 void TestSerialIoHandler::WriteImpl() {
66 if (!send_callback_.is_null()) {
67 base::Closure callback = send_callback_;
68 send_callback_.Reset();
69 callback.Run();
70 return;
71 }
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())
75 ReadImpl();
76 }
77
CancelWriteImpl()78 void TestSerialIoHandler::CancelWriteImpl() {
79 WriteCompleted(0, write_cancel_reason());
80 }
81
GetControlSignals() const82 serial::DeviceControlSignalsPtr TestSerialIoHandler::GetControlSignals() const {
83 serial::DeviceControlSignalsPtr signals(serial::DeviceControlSignals::New());
84 *signals = device_control_signals_;
85 return signals.Pass();
86 }
87
GetPortInfo() const88 serial::ConnectionInfoPtr TestSerialIoHandler::GetPortInfo() const {
89 serial::ConnectionInfoPtr info(serial::ConnectionInfo::New());
90 *info = info_;
91 return info.Pass();
92 }
93
Flush() const94 bool TestSerialIoHandler::Flush() const {
95 flushes_++;
96 return true;
97 }
98
SetControlSignals(const serial::HostControlSignals & signals)99 bool TestSerialIoHandler::SetControlSignals(
100 const serial::HostControlSignals& signals) {
101 if (signals.has_dtr)
102 dtr_ = signals.dtr;
103 if (signals.has_rts)
104 rts_ = signals.rts;
105 return true;
106 }
107
~TestSerialIoHandler()108 TestSerialIoHandler::~TestSerialIoHandler() {
109 }
110
111 } // namespace device
112