Extensions: Store disable reasons in Sync
[chromium-blink-merge.git] / device / serial / test_serial_io_handler.cc
blob9713c070f98bd1f44fc6a737eb1c42d941f48d88
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 serial::ConnectionOptions& options,
28 const OpenCompleteCallback& callback) {
29 DCHECK(!opened_);
30 opened_ = true;
31 ConfigurePort(options);
32 callback.Run(true);
35 void TestSerialIoHandler::ReadImpl() {
36 if (!pending_read_buffer())
37 return;
38 if (buffer_.empty())
39 return;
41 size_t num_bytes =
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();
56 callback.Run();
57 return;
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())
62 ReadImpl();
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;
75 return true;
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());
86 *info = info_;
87 return info.Pass();
90 bool TestSerialIoHandler::Flush() const {
91 flushes_++;
92 return true;
95 bool TestSerialIoHandler::SetControlSignals(
96 const serial::HostControlSignals& signals) {
97 if (signals.has_dtr)
98 dtr_ = signals.dtr;
99 if (signals.has_rts)
100 rts_ = signals.rts;
101 return true;
104 TestSerialIoHandler::~TestSerialIoHandler() {
107 } // namespace device