Improved the state machine in GuestView.
[chromium-blink-merge.git] / device / serial / test_serial_io_handler.cc
blob8ec564851cb50205f4446efa7ad1b9f763dbb228
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"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "device/serial/serial.mojom.h"
12 namespace device {
14 TestSerialIoHandler::TestSerialIoHandler()
15 : SerialIoHandler(NULL, NULL),
16 opened_(false),
17 dtr_(false),
18 rts_(false),
19 flushes_(0) {
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) {
28 DCHECK(!opened_);
29 opened_ = true;
30 callback.Run(true);
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;
48 void TestSerialIoHandler::ReadImpl() {
49 if (!pending_read_buffer())
50 return;
51 if (buffer_.empty())
52 return;
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);
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();
69 callback.Run();
70 return;
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();
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());
90 *info = info_;
91 return info.Pass();
94 bool TestSerialIoHandler::Flush() const {
95 flushes_++;
96 return true;
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;
108 TestSerialIoHandler::~TestSerialIoHandler() {
111 } // namespace device