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_posix.h" | 5 #include "device/serial/serial_io_handler_posix.h" |
6 | 6 |
7 #include <sys/ioctl.h> | 7 #include <sys/ioctl.h> |
8 #include <termios.h> | 8 #include <termios.h> |
9 | 9 |
10 #include "base/posix/eintr_wrapper.h" | 10 #include "base/posix/eintr_wrapper.h" |
11 | 11 |
12 #if defined(OS_LINUX) | 12 #if defined(OS_LINUX) |
13 #include <linux/serial.h> | 13 #include <linux/serial.h> |
14 #endif | 14 #if defined(OS_CHROMEOS) |
| 15 #include "base/bind.h" |
| 16 #include "base/sys_info.h" |
| 17 #include "chromeos/dbus/dbus_thread_manager.h" |
| 18 #include "chromeos/dbus/permission_broker_client.h" |
| 19 #endif // defined(OS_CHROMEOS) |
| 20 #endif // defined(OS_LINUX) |
15 | 21 |
16 namespace { | 22 namespace { |
17 | 23 |
18 // Convert an integral bit rate to a nominal one. Returns |true| | 24 // Convert an integral bit rate to a nominal one. Returns |true| |
19 // if the conversion was successful and |false| otherwise. | 25 // if the conversion was successful and |false| otherwise. |
20 bool BitrateToSpeedConstant(int bitrate, speed_t* speed) { | 26 bool BitrateToSpeedConstant(int bitrate, speed_t* speed) { |
21 #define BITRATE_TO_SPEED_CASE(x) \ | 27 #define BITRATE_TO_SPEED_CASE(x) \ |
22 case x: \ | 28 case x: \ |
23 *speed = B##x; \ | 29 *speed = B##x; \ |
24 return true; | 30 return true; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 return false; | 122 return false; |
117 #endif | 123 #endif |
118 } | 124 } |
119 | 125 |
120 } // namespace | 126 } // namespace |
121 | 127 |
122 namespace device { | 128 namespace device { |
123 | 129 |
124 // static | 130 // static |
125 scoped_refptr<SerialIoHandler> SerialIoHandler::Create( | 131 scoped_refptr<SerialIoHandler> SerialIoHandler::Create( |
126 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop) { | 132 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop, |
127 return new SerialIoHandlerPosix(file_thread_message_loop); | 133 scoped_refptr<base::MessageLoopProxy> ui_thread_message_loop) { |
| 134 return new SerialIoHandlerPosix(file_thread_message_loop, |
| 135 ui_thread_message_loop); |
| 136 } |
| 137 |
| 138 void SerialIoHandlerPosix::RequestAccess( |
| 139 const std::string& port, |
| 140 scoped_refptr<base::MessageLoopProxy> file_message_loop, |
| 141 scoped_refptr<base::MessageLoopProxy> ui_message_loop) { |
| 142 #if defined(OS_LINUX) && defined(OS_CHROMEOS) |
| 143 if (base::SysInfo::IsRunningOnChromeOS()) { |
| 144 chromeos::PermissionBrokerClient* client = |
| 145 chromeos::DBusThreadManager::Get()->GetPermissionBrokerClient(); |
| 146 if (!client) { |
| 147 DVLOG(1) << "Could not get permission_broker client."; |
| 148 OnRequestAccessComplete(port, false /* failure */); |
| 149 return; |
| 150 } |
| 151 // PermissionBrokerClient should be called on the UI thread. |
| 152 ui_message_loop->PostTask( |
| 153 FROM_HERE, |
| 154 base::Bind( |
| 155 &chromeos::PermissionBrokerClient::RequestPathAccess, |
| 156 base::Unretained(client), |
| 157 port, |
| 158 -1, |
| 159 base::Bind(&SerialIoHandler::OnRequestAccessComplete, this, port))); |
| 160 } else { |
| 161 OnRequestAccessComplete(port, true /* success */); |
| 162 return; |
| 163 } |
| 164 #else |
| 165 OnRequestAccessComplete(port, true /* success */); |
| 166 #endif // defined(OS_LINUX) && defined(OS_CHROMEOS) |
128 } | 167 } |
129 | 168 |
130 void SerialIoHandlerPosix::ReadImpl() { | 169 void SerialIoHandlerPosix::ReadImpl() { |
131 DCHECK(CalledOnValidThread()); | 170 DCHECK(CalledOnValidThread()); |
132 DCHECK(pending_read_buffer()); | 171 DCHECK(pending_read_buffer()); |
133 DCHECK(file().IsValid()); | 172 DCHECK(file().IsValid()); |
134 | 173 |
135 EnsureWatchingReads(); | 174 EnsureWatchingReads(); |
136 } | 175 } |
137 | 176 |
(...skipping 13 matching lines...) Expand all Loading... |
151 } | 190 } |
152 | 191 |
153 void SerialIoHandlerPosix::CancelWriteImpl() { | 192 void SerialIoHandlerPosix::CancelWriteImpl() { |
154 DCHECK(CalledOnValidThread()); | 193 DCHECK(CalledOnValidThread()); |
155 is_watching_writes_ = false; | 194 is_watching_writes_ = false; |
156 file_write_watcher_.StopWatchingFileDescriptor(); | 195 file_write_watcher_.StopWatchingFileDescriptor(); |
157 QueueWriteCompleted(0, write_cancel_reason()); | 196 QueueWriteCompleted(0, write_cancel_reason()); |
158 } | 197 } |
159 | 198 |
160 SerialIoHandlerPosix::SerialIoHandlerPosix( | 199 SerialIoHandlerPosix::SerialIoHandlerPosix( |
161 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop) | 200 scoped_refptr<base::MessageLoopProxy> file_thread_message_loop, |
162 : SerialIoHandler(file_thread_message_loop), | 201 scoped_refptr<base::MessageLoopProxy> ui_thread_message_loop) |
| 202 : SerialIoHandler(file_thread_message_loop, ui_thread_message_loop), |
163 is_watching_reads_(false), | 203 is_watching_reads_(false), |
164 is_watching_writes_(false) { | 204 is_watching_writes_(false) { |
165 } | 205 } |
166 | 206 |
167 SerialIoHandlerPosix::~SerialIoHandlerPosix() { | 207 SerialIoHandlerPosix::~SerialIoHandlerPosix() { |
168 } | 208 } |
169 | 209 |
170 void SerialIoHandlerPosix::OnFileCanReadWithoutBlocking(int fd) { | 210 void SerialIoHandlerPosix::OnFileCanReadWithoutBlocking(int fd) { |
171 DCHECK(CalledOnValidThread()); | 211 DCHECK(CalledOnValidThread()); |
172 DCHECK_EQ(fd, file().GetPlatformFile()); | 212 DCHECK_EQ(fd, file().GetPlatformFile()); |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
403 (config.c_cflag & CSTOPB) ? serial::STOP_BITS_TWO : serial::STOP_BITS_ONE; | 443 (config.c_cflag & CSTOPB) ? serial::STOP_BITS_TWO : serial::STOP_BITS_ONE; |
404 info->cts_flow_control = (config.c_cflag & CRTSCTS) != 0; | 444 info->cts_flow_control = (config.c_cflag & CRTSCTS) != 0; |
405 return info.Pass(); | 445 return info.Pass(); |
406 } | 446 } |
407 | 447 |
408 std::string SerialIoHandler::MaybeFixUpPortName(const std::string& port_name) { | 448 std::string SerialIoHandler::MaybeFixUpPortName(const std::string& port_name) { |
409 return port_name; | 449 return port_name; |
410 } | 450 } |
411 | 451 |
412 } // namespace device | 452 } // namespace device |
OLD | NEW |