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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
// 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/serial_io_handler.h"
#include "base/bind.h"
#include "base/files/file_path.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/string_util.h"
namespace device {
SerialIoHandler::SerialIoHandler(
scoped_refptr<base::MessageLoopProxy> file_thread_message_loop,
scoped_refptr<base::MessageLoopProxy> ui_thread_message_loop)
: file_thread_message_loop_(file_thread_message_loop),
ui_thread_message_loop_(ui_thread_message_loop) {
}
SerialIoHandler::~SerialIoHandler() {
DCHECK(CalledOnValidThread());
Close();
}
void SerialIoHandler::Open(const std::string& port,
const OpenCompleteCallback& callback) {
DCHECK(CalledOnValidThread());
DCHECK(open_complete_.is_null());
open_complete_ = callback;
DCHECK(file_thread_message_loop_.get());
DCHECK(ui_thread_message_loop_.get());
RequestAccess(port, file_thread_message_loop_, ui_thread_message_loop_);
}
void SerialIoHandler::RequestAccess(
const std::string& port,
scoped_refptr<base::MessageLoopProxy> file_message_loop,
scoped_refptr<base::MessageLoopProxy> ui_message_loop) {
OnRequestAccessComplete(port, true /* success */);
}
void SerialIoHandler::OnRequestAccessComplete(const std::string& port,
bool success) {
DCHECK(CalledOnValidThread());
if (success) {
DCHECK(file_thread_message_loop_.get());
file_thread_message_loop_->PostTask(
FROM_HERE,
base::Bind(&SerialIoHandler::StartOpen,
this,
port,
base::MessageLoopProxy::current()));
return;
} else {
DCHECK(!open_complete_.is_null());
OpenCompleteCallback callback = open_complete_;
open_complete_.Reset();
callback.Run(false);
return;
}
}
void SerialIoHandler::StartOpen(
const std::string& port,
scoped_refptr<base::MessageLoopProxy> io_message_loop) {
DCHECK(!open_complete_.is_null());
DCHECK(file_thread_message_loop_->RunsTasksOnCurrentThread());
DCHECK(!file_.IsValid());
// It's the responsibility of the API wrapper around SerialIoHandler to
// validate the supplied path against the set of valid port names, and
// it is a reasonable assumption that serial port names are ASCII.
DCHECK(base::IsStringASCII(port));
base::FilePath path(base::FilePath::FromUTF8Unsafe(MaybeFixUpPortName(port)));
int flags = base::File::FLAG_OPEN | base::File::FLAG_READ |
base::File::FLAG_EXCLUSIVE_READ | base::File::FLAG_WRITE |
base::File::FLAG_EXCLUSIVE_WRITE | base::File::FLAG_ASYNC |
base::File::FLAG_TERMINAL_DEVICE;
base::File file(path, flags);
io_message_loop->PostTask(
FROM_HERE,
base::Bind(&SerialIoHandler::FinishOpen, this, Passed(file.Pass())));
}
void SerialIoHandler::FinishOpen(base::File file) {
DCHECK(CalledOnValidThread());
DCHECK(!open_complete_.is_null());
OpenCompleteCallback callback = open_complete_;
open_complete_.Reset();
if (!file.IsValid()) {
callback.Run(false);
return;
}
file_ = file.Pass();
bool success = PostOpen();
if (!success)
Close();
callback.Run(success);
}
bool SerialIoHandler::PostOpen() {
return true;
}
void SerialIoHandler::Close() {
if (file_.IsValid()) {
DCHECK(file_thread_message_loop_.get());
file_thread_message_loop_->PostTask(
FROM_HERE, base::Bind(&SerialIoHandler::DoClose, Passed(file_.Pass())));
}
}
// static
void SerialIoHandler::DoClose(base::File port) {
// port closed by destructor.
}
void SerialIoHandler::Read(scoped_ptr<WritableBuffer> buffer) {
DCHECK(CalledOnValidThread());
DCHECK(!IsReadPending());
pending_read_buffer_ = buffer.Pass();
read_canceled_ = false;
AddRef();
ReadImpl();
}
void SerialIoHandler::Write(scoped_ptr<ReadOnlyBuffer> buffer) {
DCHECK(CalledOnValidThread());
DCHECK(!IsWritePending());
pending_write_buffer_ = buffer.Pass();
write_canceled_ = false;
AddRef();
WriteImpl();
}
void SerialIoHandler::ReadCompleted(int bytes_read,
serial::ReceiveError error) {
DCHECK(CalledOnValidThread());
DCHECK(IsReadPending());
scoped_ptr<WritableBuffer> pending_read_buffer = pending_read_buffer_.Pass();
if (error == serial::RECEIVE_ERROR_NONE) {
pending_read_buffer->Done(bytes_read);
} else {
pending_read_buffer->DoneWithError(bytes_read, error);
}
Release();
}
void SerialIoHandler::WriteCompleted(int bytes_written,
serial::SendError error) {
DCHECK(CalledOnValidThread());
DCHECK(IsWritePending());
scoped_ptr<ReadOnlyBuffer> pending_write_buffer =
pending_write_buffer_.Pass();
if (error == serial::SEND_ERROR_NONE) {
pending_write_buffer->Done(bytes_written);
} else {
pending_write_buffer->DoneWithError(bytes_written, error);
}
Release();
}
bool SerialIoHandler::IsReadPending() const {
DCHECK(CalledOnValidThread());
return pending_read_buffer_ != NULL;
}
bool SerialIoHandler::IsWritePending() const {
DCHECK(CalledOnValidThread());
return pending_write_buffer_ != NULL;
}
void SerialIoHandler::CancelRead(serial::ReceiveError reason) {
DCHECK(CalledOnValidThread());
if (IsReadPending() && !read_canceled_) {
read_canceled_ = true;
read_cancel_reason_ = reason;
CancelReadImpl();
}
}
void SerialIoHandler::CancelWrite(serial::SendError reason) {
DCHECK(CalledOnValidThread());
if (IsWritePending() && !write_canceled_) {
write_canceled_ = true;
write_cancel_reason_ = reason;
CancelWriteImpl();
}
}
void SerialIoHandler::QueueReadCompleted(int bytes_read,
serial::ReceiveError error) {
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&SerialIoHandler::ReadCompleted, this, bytes_read, error));
}
void SerialIoHandler::QueueWriteCompleted(int bytes_written,
serial::SendError error) {
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&SerialIoHandler::WriteCompleted, this, bytes_written, error));
}
} // namespace device
|