1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "device/serial/test_serial_io_handler.h"
#include <algorithm>
#include "base/bind.h"
#include "device/serial/serial.mojom.h"
namespace device {
TestSerialIoHandler::TestSerialIoHandler()
: SerialIoHandler(NULL, NULL),
opened_(false),
dtr_(false),
rts_(false),
flushes_(0) {
}
scoped_refptr<SerialIoHandler> TestSerialIoHandler::Create() {
return scoped_refptr<SerialIoHandler>(new TestSerialIoHandler);
}
void TestSerialIoHandler::Open(const std::string& port,
const serial::ConnectionOptions& options,
const OpenCompleteCallback& callback) {
DCHECK(!opened_);
opened_ = true;
ConfigurePort(options);
callback.Run(true);
}
void TestSerialIoHandler::ReadImpl() {
if (!pending_read_buffer())
return;
if (buffer_.empty())
return;
size_t num_bytes =
std::min(buffer_.size(), static_cast<size_t>(pending_read_buffer_len()));
memcpy(pending_read_buffer(), buffer_.c_str(), num_bytes);
buffer_ = buffer_.substr(num_bytes);
ReadCompleted(static_cast<uint32_t>(num_bytes), serial::RECEIVE_ERROR_NONE);
}
void TestSerialIoHandler::CancelReadImpl() {
ReadCompleted(0, read_cancel_reason());
}
void TestSerialIoHandler::WriteImpl() {
if (!send_callback_.is_null()) {
base::Closure callback = send_callback_;
send_callback_.Reset();
callback.Run();
return;
}
buffer_ += std::string(pending_write_buffer(), pending_write_buffer_len());
WriteCompleted(pending_write_buffer_len(), serial::SEND_ERROR_NONE);
if (pending_read_buffer())
ReadImpl();
}
void TestSerialIoHandler::CancelWriteImpl() {
WriteCompleted(0, write_cancel_reason());
}
bool TestSerialIoHandler::ConfigurePortImpl() {
info_.bitrate = options().bitrate;
info_.data_bits = options().data_bits;
info_.parity_bit = options().parity_bit;
info_.stop_bits = options().stop_bits;
info_.cts_flow_control = options().cts_flow_control;
return true;
}
serial::DeviceControlSignalsPtr TestSerialIoHandler::GetControlSignals() const {
serial::DeviceControlSignalsPtr signals(serial::DeviceControlSignals::New());
*signals = device_control_signals_;
return signals.Pass();
}
serial::ConnectionInfoPtr TestSerialIoHandler::GetPortInfo() const {
serial::ConnectionInfoPtr info(serial::ConnectionInfo::New());
*info = info_;
return info.Pass();
}
bool TestSerialIoHandler::Flush() const {
flushes_++;
return true;
}
bool TestSerialIoHandler::SetControlSignals(
const serial::HostControlSignals& signals) {
if (signals.has_dtr)
dtr_ = signals.dtr;
if (signals.has_rts)
rts_ = signals.rts;
return true;
}
TestSerialIoHandler::~TestSerialIoHandler() {
}
} // namespace device
|