OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/serial/serial_io_handler.h" | 5 #include "device/serial/serial_io_handler.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
12 #include "base/location.h" | 12 #include "base/location.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "build/build_config.h" | 14 #include "build/build_config.h" |
15 | 15 |
16 #if defined(OS_CHROMEOS) | 16 #if defined(OS_CHROMEOS) |
17 #include "chromeos/dbus/dbus_thread_manager.h" | 17 #include "chromeos/dbus/dbus_thread_manager.h" |
18 #include "chromeos/dbus/permission_broker_client.h" | 18 #include "chromeos/dbus/permission_broker_client.h" |
19 #endif // defined(OS_CHROMEOS) | 19 #endif // defined(OS_CHROMEOS) |
20 | 20 |
21 namespace device { | 21 namespace device { |
22 | 22 |
23 SerialIoHandler::SerialIoHandler( | 23 SerialIoHandler::SerialIoHandler( |
24 scoped_refptr<base::SingleThreadTaskRunner> file_thread_task_runner, | |
25 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner) | 24 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner) |
26 : file_thread_task_runner_(file_thread_task_runner), | 25 : ui_thread_task_runner_(ui_thread_task_runner) { |
27 ui_thread_task_runner_(ui_thread_task_runner) { | |
28 options_.bitrate = 9600; | 26 options_.bitrate = 9600; |
29 options_.data_bits = serial::DataBits::EIGHT; | 27 options_.data_bits = serial::DataBits::EIGHT; |
30 options_.parity_bit = serial::ParityBit::NO_PARITY; | 28 options_.parity_bit = serial::ParityBit::NO_PARITY; |
31 options_.stop_bits = serial::StopBits::ONE; | 29 options_.stop_bits = serial::StopBits::ONE; |
32 options_.cts_flow_control = false; | 30 options_.cts_flow_control = false; |
33 options_.has_cts_flow_control = true; | 31 options_.has_cts_flow_control = true; |
34 } | 32 } |
35 | 33 |
36 SerialIoHandler::~SerialIoHandler() { | 34 SerialIoHandler::~SerialIoHandler() { |
37 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 35 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
38 Close(); | |
39 } | 36 } |
40 | 37 |
41 void SerialIoHandler::Open(const std::string& port, | 38 void SerialIoHandler::Open(const std::string& port, |
42 const serial::ConnectionOptions& options, | 39 const serial::ConnectionOptions& options, |
43 const OpenCompleteCallback& callback) { | 40 const OpenCompleteCallback& callback) { |
44 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 41 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
45 DCHECK(open_complete_.is_null()); | 42 DCHECK(open_complete_.is_null()); |
46 open_complete_ = callback; | 43 open_complete_ = callback; |
47 DCHECK(file_thread_task_runner_.get()); | |
48 DCHECK(ui_thread_task_runner_.get()); | 44 DCHECK(ui_thread_task_runner_.get()); |
49 MergeConnectionOptions(options); | 45 MergeConnectionOptions(options); |
50 port_ = port; | 46 port_ = port; |
51 | 47 |
52 #if defined(OS_CHROMEOS) | 48 #if defined(OS_CHROMEOS) |
53 chromeos::PermissionBrokerClient* client = | 49 chromeos::PermissionBrokerClient* client = |
54 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient(); | 50 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient(); |
55 DCHECK(client) << "Could not get permission_broker client."; | 51 DCHECK(client) << "Could not get permission_broker client."; |
56 // PermissionBrokerClient should be called on the UI thread. | 52 // PermissionBrokerClient should be called on the UI thread. |
57 scoped_refptr<base::SingleThreadTaskRunner> task_runner = | 53 scoped_refptr<base::SingleThreadTaskRunner> task_runner = |
58 base::ThreadTaskRunnerHandle::Get(); | 54 base::ThreadTaskRunnerHandle::Get(); |
59 ui_thread_task_runner_->PostTask( | 55 ui_thread_task_runner_->PostTask( |
60 FROM_HERE, | 56 FROM_HERE, |
61 base::Bind( | 57 base::Bind( |
62 &chromeos::PermissionBrokerClient::OpenPath, base::Unretained(client), | 58 &chromeos::PermissionBrokerClient::OpenPath, base::Unretained(client), |
63 port, base::Bind(&SerialIoHandler::OnPathOpened, this, task_runner), | 59 port, base::Bind(&SerialIoHandler::OnPathOpened, this, task_runner), |
64 base::Bind(&SerialIoHandler::OnPathOpenError, this, task_runner))); | 60 base::Bind(&SerialIoHandler::OnPathOpenError, this, task_runner))); |
65 #else | |
66 file_thread_task_runner_->PostTask( | |
67 FROM_HERE, base::Bind(&SerialIoHandler::StartOpen, this, port, | |
68 base::ThreadTaskRunnerHandle::Get())); | |
Reilly Grant (use Gerrit)
2017/06/01 13:48:21
file_thread_task_runner_ is clearly used here so t
sujith
2017/06/01 14:57:31
Done.
| |
69 #endif // defined(OS_CHROMEOS) | 61 #endif // defined(OS_CHROMEOS) |
70 } | 62 } |
71 | 63 |
72 #if defined(OS_CHROMEOS) | 64 #if defined(OS_CHROMEOS) |
73 | 65 |
74 void SerialIoHandler::OnPathOpened( | 66 void SerialIoHandler::OnPathOpened( |
75 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner, | 67 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner, |
76 base::ScopedFD fd) { | 68 base::ScopedFD fd) { |
77 base::File file(fd.release()); | 69 base::File file(fd.release()); |
78 io_thread_task_runner->PostTask( | 70 io_thread_task_runner->PostTask( |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
119 if (options.has_cts_flow_control) { | 111 if (options.has_cts_flow_control) { |
120 DCHECK(options_.has_cts_flow_control); | 112 DCHECK(options_.has_cts_flow_control); |
121 options_.cts_flow_control = options.cts_flow_control; | 113 options_.cts_flow_control = options.cts_flow_control; |
122 } | 114 } |
123 } | 115 } |
124 | 116 |
125 void SerialIoHandler::StartOpen( | 117 void SerialIoHandler::StartOpen( |
126 const std::string& port, | 118 const std::string& port, |
127 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) { | 119 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) { |
128 DCHECK(!open_complete_.is_null()); | 120 DCHECK(!open_complete_.is_null()); |
129 DCHECK(file_thread_task_runner_->RunsTasksInCurrentSequence()); | |
130 DCHECK(!file_.IsValid()); | 121 DCHECK(!file_.IsValid()); |
131 // It's the responsibility of the API wrapper around SerialIoHandler to | 122 // It's the responsibility of the API wrapper around SerialIoHandler to |
132 // validate the supplied path against the set of valid port names, and | 123 // validate the supplied path against the set of valid port names, and |
133 // it is a reasonable assumption that serial port names are ASCII. | 124 // it is a reasonable assumption that serial port names are ASCII. |
134 DCHECK(base::IsStringASCII(port)); | 125 DCHECK(base::IsStringASCII(port)); |
135 base::FilePath path(base::FilePath::FromUTF8Unsafe(MaybeFixUpPortName(port))); | 126 base::FilePath path(base::FilePath::FromUTF8Unsafe(MaybeFixUpPortName(port))); |
136 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | | 127 int flags = base::File::FLAG_OPEN | base::File::FLAG_READ | |
137 base::File::FLAG_EXCLUSIVE_READ | base::File::FLAG_WRITE | | 128 base::File::FLAG_EXCLUSIVE_READ | base::File::FLAG_WRITE | |
138 base::File::FLAG_EXCLUSIVE_WRITE | base::File::FLAG_ASYNC | | 129 base::File::FLAG_EXCLUSIVE_WRITE | base::File::FLAG_ASYNC | |
139 base::File::FLAG_TERMINAL_DEVICE; | 130 base::File::FLAG_TERMINAL_DEVICE; |
(...skipping 11 matching lines...) Expand all Loading... | |
151 if (!file.IsValid()) { | 142 if (!file.IsValid()) { |
152 LOG(ERROR) << "Failed to open serial port: " | 143 LOG(ERROR) << "Failed to open serial port: " |
153 << base::File::ErrorToString(file.error_details()); | 144 << base::File::ErrorToString(file.error_details()); |
154 callback.Run(false); | 145 callback.Run(false); |
155 return; | 146 return; |
156 } | 147 } |
157 | 148 |
158 file_ = std::move(file); | 149 file_ = std::move(file); |
159 | 150 |
160 bool success = PostOpen() && ConfigurePortImpl(); | 151 bool success = PostOpen() && ConfigurePortImpl(); |
161 if (!success) | |
162 Close(); | |
163 | 152 |
164 callback.Run(success); | 153 callback.Run(success); |
165 } | 154 } |
166 | 155 |
167 bool SerialIoHandler::PostOpen() { | 156 bool SerialIoHandler::PostOpen() { |
168 return true; | 157 return true; |
169 } | 158 } |
170 | 159 |
171 void SerialIoHandler::Close() { | |
172 if (file_.IsValid()) { | |
173 DCHECK(file_thread_task_runner_.get()); | |
174 file_thread_task_runner_->PostTask( | |
Reilly Grant (use Gerrit)
2017/06/01 13:48:21
Same here. We can't just remove the Close method.
sujith
2017/06/01 14:57:31
Done.
| |
175 FROM_HERE, | |
176 base::Bind(&SerialIoHandler::DoClose, Passed(std::move(file_)))); | |
177 } | |
178 } | |
179 | |
180 // static | 160 // static |
181 void SerialIoHandler::DoClose(base::File port) { | 161 void SerialIoHandler::DoClose(base::File port) { |
182 // port closed by destructor. | 162 // port closed by destructor. |
183 } | 163 } |
184 | 164 |
185 void SerialIoHandler::Read(std::unique_ptr<WritableBuffer> buffer) { | 165 void SerialIoHandler::Read(std::unique_ptr<WritableBuffer> buffer) { |
186 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); | 166 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
187 DCHECK(!IsReadPending()); | 167 DCHECK(!IsReadPending()); |
188 pending_read_buffer_ = std::move(buffer); | 168 pending_read_buffer_ = std::move(buffer); |
189 read_canceled_ = false; | 169 read_canceled_ = false; |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
270 } | 250 } |
271 | 251 |
272 void SerialIoHandler::QueueWriteCompleted(int bytes_written, | 252 void SerialIoHandler::QueueWriteCompleted(int bytes_written, |
273 serial::SendError error) { | 253 serial::SendError error) { |
274 base::ThreadTaskRunnerHandle::Get()->PostTask( | 254 base::ThreadTaskRunnerHandle::Get()->PostTask( |
275 FROM_HERE, | 255 FROM_HERE, |
276 base::Bind(&SerialIoHandler::WriteCompleted, this, bytes_written, error)); | 256 base::Bind(&SerialIoHandler::WriteCompleted, this, bytes_written, error)); |
277 } | 257 } |
278 | 258 |
279 } // namespace device | 259 } // namespace device |
OLD | NEW |