| 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" |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 192 #elif defined(OS_MACOSX) | 192 #elif defined(OS_MACOSX) |
| 193 // cfsetispeed and cfsetospeed sometimes work for custom baud rates on OS | 193 // cfsetispeed and cfsetospeed sometimes work for custom baud rates on OS |
| 194 // X but the IOSSIOSPEED ioctl is more reliable but has to be done after | 194 // X but the IOSSIOSPEED ioctl is more reliable but has to be done after |
| 195 // the rest of the port parameters are set or else it will be overwritten. | 195 // the rest of the port parameters are set or else it will be overwritten. |
| 196 need_iossiospeed = true; | 196 need_iossiospeed = true; |
| 197 #else | 197 #else |
| 198 return false; | 198 return false; |
| 199 #endif | 199 #endif |
| 200 } | 200 } |
| 201 | 201 |
| 202 DCHECK(options().data_bits != serial::DATA_BITS_NONE); | 202 DCHECK(options().data_bits != serial::DataBits::NONE); |
| 203 config.c_cflag &= ~CSIZE; | 203 config.c_cflag &= ~CSIZE; |
| 204 switch (options().data_bits) { | 204 switch (options().data_bits) { |
| 205 case serial::DATA_BITS_SEVEN: | 205 case serial::DataBits::SEVEN: |
| 206 config.c_cflag |= CS7; | 206 config.c_cflag |= CS7; |
| 207 break; | 207 break; |
| 208 case serial::DATA_BITS_EIGHT: | 208 case serial::DataBits::EIGHT: |
| 209 default: | 209 default: |
| 210 config.c_cflag |= CS8; | 210 config.c_cflag |= CS8; |
| 211 break; | 211 break; |
| 212 } | 212 } |
| 213 | 213 |
| 214 DCHECK(options().parity_bit != serial::PARITY_BIT_NONE); | 214 DCHECK(options().parity_bit != serial::ParityBit::NONE); |
| 215 switch (options().parity_bit) { | 215 switch (options().parity_bit) { |
| 216 case serial::PARITY_BIT_EVEN: | 216 case serial::ParityBit::EVEN: |
| 217 config.c_cflag |= PARENB; | 217 config.c_cflag |= PARENB; |
| 218 config.c_cflag &= ~PARODD; | 218 config.c_cflag &= ~PARODD; |
| 219 break; | 219 break; |
| 220 case serial::PARITY_BIT_ODD: | 220 case serial::ParityBit::ODD: |
| 221 config.c_cflag |= (PARODD | PARENB); | 221 config.c_cflag |= (PARODD | PARENB); |
| 222 break; | 222 break; |
| 223 case serial::PARITY_BIT_NO: | 223 case serial::ParityBit::NO: |
| 224 default: | 224 default: |
| 225 config.c_cflag &= ~(PARODD | PARENB); | 225 config.c_cflag &= ~(PARODD | PARENB); |
| 226 break; | 226 break; |
| 227 } | 227 } |
| 228 | 228 |
| 229 error_detect_state_ = ErrorDetectState::NO_ERROR; | 229 error_detect_state_ = ErrorDetectState::NO_ERROR; |
| 230 num_chars_stashed_ = 0; | 230 num_chars_stashed_ = 0; |
| 231 | 231 |
| 232 if (config.c_cflag & PARENB) { | 232 if (config.c_cflag & PARENB) { |
| 233 config.c_iflag &= ~IGNPAR; | 233 config.c_iflag &= ~IGNPAR; |
| 234 config.c_iflag |= INPCK; | 234 config.c_iflag |= INPCK; |
| 235 parity_check_enabled_ = true; | 235 parity_check_enabled_ = true; |
| 236 } else { | 236 } else { |
| 237 config.c_iflag |= IGNPAR; | 237 config.c_iflag |= IGNPAR; |
| 238 config.c_iflag &= ~INPCK; | 238 config.c_iflag &= ~INPCK; |
| 239 parity_check_enabled_ = false; | 239 parity_check_enabled_ = false; |
| 240 } | 240 } |
| 241 | 241 |
| 242 DCHECK(options().stop_bits != serial::STOP_BITS_NONE); | 242 DCHECK(options().stop_bits != serial::StopBits::NONE); |
| 243 switch (options().stop_bits) { | 243 switch (options().stop_bits) { |
| 244 case serial::STOP_BITS_TWO: | 244 case serial::StopBits::TWO: |
| 245 config.c_cflag |= CSTOPB; | 245 config.c_cflag |= CSTOPB; |
| 246 break; | 246 break; |
| 247 case serial::STOP_BITS_ONE: | 247 case serial::StopBits::ONE: |
| 248 default: | 248 default: |
| 249 config.c_cflag &= ~CSTOPB; | 249 config.c_cflag &= ~CSTOPB; |
| 250 break; | 250 break; |
| 251 } | 251 } |
| 252 | 252 |
| 253 DCHECK(options().has_cts_flow_control); | 253 DCHECK(options().has_cts_flow_control); |
| 254 if (options().cts_flow_control) { | 254 if (options().cts_flow_control) { |
| 255 config.c_cflag |= CRTSCTS; | 255 config.c_cflag |= CRTSCTS; |
| 256 } else { | 256 } else { |
| 257 config.c_cflag &= ~CRTSCTS; | 257 config.c_cflag &= ~CRTSCTS; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 void SerialIoHandlerPosix::OnFileCanReadWithoutBlocking(int fd) { | 293 void SerialIoHandlerPosix::OnFileCanReadWithoutBlocking(int fd) { |
| 294 DCHECK(CalledOnValidThread()); | 294 DCHECK(CalledOnValidThread()); |
| 295 DCHECK_EQ(fd, file().GetPlatformFile()); | 295 DCHECK_EQ(fd, file().GetPlatformFile()); |
| 296 | 296 |
| 297 if (pending_read_buffer()) { | 297 if (pending_read_buffer()) { |
| 298 int bytes_read = HANDLE_EINTR(read(file().GetPlatformFile(), | 298 int bytes_read = HANDLE_EINTR(read(file().GetPlatformFile(), |
| 299 pending_read_buffer(), | 299 pending_read_buffer(), |
| 300 pending_read_buffer_len())); | 300 pending_read_buffer_len())); |
| 301 if (bytes_read < 0) { | 301 if (bytes_read < 0) { |
| 302 if (errno == ENXIO) { | 302 if (errno == ENXIO) { |
| 303 ReadCompleted(0, serial::RECEIVE_ERROR_DEVICE_LOST); | 303 ReadCompleted(0, serial::ReceiveError::DEVICE_LOST); |
| 304 } else { | 304 } else { |
| 305 ReadCompleted(0, serial::RECEIVE_ERROR_SYSTEM_ERROR); | 305 ReadCompleted(0, serial::ReceiveError::SYSTEM_ERROR); |
| 306 } | 306 } |
| 307 } else if (bytes_read == 0) { | 307 } else if (bytes_read == 0) { |
| 308 ReadCompleted(0, serial::RECEIVE_ERROR_DEVICE_LOST); | 308 ReadCompleted(0, serial::ReceiveError::DEVICE_LOST); |
| 309 } else { | 309 } else { |
| 310 bool break_detected = false; | 310 bool break_detected = false; |
| 311 bool parity_error_detected = false; | 311 bool parity_error_detected = false; |
| 312 int new_bytes_read = | 312 int new_bytes_read = |
| 313 CheckReceiveError(pending_read_buffer(), pending_read_buffer_len(), | 313 CheckReceiveError(pending_read_buffer(), pending_read_buffer_len(), |
| 314 bytes_read, break_detected, parity_error_detected); | 314 bytes_read, break_detected, parity_error_detected); |
| 315 | 315 |
| 316 if (break_detected) { | 316 if (break_detected) { |
| 317 ReadCompleted(new_bytes_read, serial::RECEIVE_ERROR_BREAK); | 317 ReadCompleted(new_bytes_read, serial::ReceiveError::BREAK); |
| 318 } else if (parity_error_detected) { | 318 } else if (parity_error_detected) { |
| 319 ReadCompleted(new_bytes_read, serial::RECEIVE_ERROR_PARITY_ERROR); | 319 ReadCompleted(new_bytes_read, serial::ReceiveError::PARITY_ERROR); |
| 320 } else { | 320 } else { |
| 321 ReadCompleted(new_bytes_read, serial::RECEIVE_ERROR_NONE); | 321 ReadCompleted(new_bytes_read, serial::ReceiveError::NONE); |
| 322 } | 322 } |
| 323 } | 323 } |
| 324 } else { | 324 } else { |
| 325 // Stop watching the fd if we get notifications with no pending | 325 // Stop watching the fd if we get notifications with no pending |
| 326 // reads or writes to avoid starving the message loop. | 326 // reads or writes to avoid starving the message loop. |
| 327 is_watching_reads_ = false; | 327 is_watching_reads_ = false; |
| 328 file_read_watcher_.StopWatchingFileDescriptor(); | 328 file_read_watcher_.StopWatchingFileDescriptor(); |
| 329 } | 329 } |
| 330 } | 330 } |
| 331 | 331 |
| 332 void SerialIoHandlerPosix::OnFileCanWriteWithoutBlocking(int fd) { | 332 void SerialIoHandlerPosix::OnFileCanWriteWithoutBlocking(int fd) { |
| 333 DCHECK(CalledOnValidThread()); | 333 DCHECK(CalledOnValidThread()); |
| 334 DCHECK_EQ(fd, file().GetPlatformFile()); | 334 DCHECK_EQ(fd, file().GetPlatformFile()); |
| 335 | 335 |
| 336 if (pending_write_buffer()) { | 336 if (pending_write_buffer()) { |
| 337 int bytes_written = HANDLE_EINTR(write(file().GetPlatformFile(), | 337 int bytes_written = HANDLE_EINTR(write(file().GetPlatformFile(), |
| 338 pending_write_buffer(), | 338 pending_write_buffer(), |
| 339 pending_write_buffer_len())); | 339 pending_write_buffer_len())); |
| 340 if (bytes_written < 0) { | 340 if (bytes_written < 0) { |
| 341 WriteCompleted(0, serial::SEND_ERROR_SYSTEM_ERROR); | 341 WriteCompleted(0, serial::SendError::SYSTEM_ERROR); |
| 342 } else { | 342 } else { |
| 343 WriteCompleted(bytes_written, serial::SEND_ERROR_NONE); | 343 WriteCompleted(bytes_written, serial::SendError::NONE); |
| 344 } | 344 } |
| 345 } else { | 345 } else { |
| 346 // Stop watching the fd if we get notifications with no pending | 346 // Stop watching the fd if we get notifications with no pending |
| 347 // writes to avoid starving the message loop. | 347 // writes to avoid starving the message loop. |
| 348 is_watching_writes_ = false; | 348 is_watching_writes_ = false; |
| 349 file_write_watcher_.StopWatchingFileDescriptor(); | 349 file_write_watcher_.StopWatchingFileDescriptor(); |
| 350 } | 350 } |
| 351 } | 351 } |
| 352 | 352 |
| 353 void SerialIoHandlerPosix::EnsureWatchingReads() { | 353 void SerialIoHandlerPosix::EnsureWatchingReads() { |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 456 int bitrate = 0; | 456 int bitrate = 0; |
| 457 if (SpeedConstantToBitrate(ispeed, &bitrate)) { | 457 if (SpeedConstantToBitrate(ispeed, &bitrate)) { |
| 458 info->bitrate = bitrate; | 458 info->bitrate = bitrate; |
| 459 } else if (ispeed > 0) { | 459 } else if (ispeed > 0) { |
| 460 info->bitrate = static_cast<int>(ispeed); | 460 info->bitrate = static_cast<int>(ispeed); |
| 461 } | 461 } |
| 462 } | 462 } |
| 463 #endif | 463 #endif |
| 464 | 464 |
| 465 if ((config.c_cflag & CSIZE) == CS7) { | 465 if ((config.c_cflag & CSIZE) == CS7) { |
| 466 info->data_bits = serial::DATA_BITS_SEVEN; | 466 info->data_bits = serial::DataBits::SEVEN; |
| 467 } else if ((config.c_cflag & CSIZE) == CS8) { | 467 } else if ((config.c_cflag & CSIZE) == CS8) { |
| 468 info->data_bits = serial::DATA_BITS_EIGHT; | 468 info->data_bits = serial::DataBits::EIGHT; |
| 469 } else { | 469 } else { |
| 470 info->data_bits = serial::DATA_BITS_NONE; | 470 info->data_bits = serial::DataBits::NONE; |
| 471 } | 471 } |
| 472 if (config.c_cflag & PARENB) { | 472 if (config.c_cflag & PARENB) { |
| 473 info->parity_bit = (config.c_cflag & PARODD) ? serial::PARITY_BIT_ODD | 473 info->parity_bit = (config.c_cflag & PARODD) ? serial::ParityBit::ODD |
| 474 : serial::PARITY_BIT_EVEN; | 474 : serial::ParityBit::EVEN; |
| 475 } else { | 475 } else { |
| 476 info->parity_bit = serial::PARITY_BIT_NO; | 476 info->parity_bit = serial::ParityBit::NO; |
| 477 } | 477 } |
| 478 info->stop_bits = | 478 info->stop_bits = |
| 479 (config.c_cflag & CSTOPB) ? serial::STOP_BITS_TWO : serial::STOP_BITS_ONE; | 479 (config.c_cflag & CSTOPB) ? serial::StopBits::TWO : serial::StopBits::ONE; |
| 480 info->cts_flow_control = (config.c_cflag & CRTSCTS) != 0; | 480 info->cts_flow_control = (config.c_cflag & CRTSCTS) != 0; |
| 481 return info; | 481 return info; |
| 482 } | 482 } |
| 483 | 483 |
| 484 bool SerialIoHandlerPosix::SetBreak() { | 484 bool SerialIoHandlerPosix::SetBreak() { |
| 485 if (ioctl(file().GetPlatformFile(), TIOCSBRK, 0) != 0) { | 485 if (ioctl(file().GetPlatformFile(), TIOCSBRK, 0) != 0) { |
| 486 VPLOG(1) << "Failed to set break"; | 486 VPLOG(1) << "Failed to set break"; |
| 487 return false; | 487 return false; |
| 488 } | 488 } |
| 489 | 489 |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 612 memcpy(buffer, chars_stashed_, std::min(new_bytes_read, 2)); | 612 memcpy(buffer, chars_stashed_, std::min(new_bytes_read, 2)); |
| 613 memcpy(chars_stashed_, tmp, num_chars_stashed_); | 613 memcpy(chars_stashed_, tmp, num_chars_stashed_); |
| 614 return new_bytes_read; | 614 return new_bytes_read; |
| 615 } | 615 } |
| 616 | 616 |
| 617 std::string SerialIoHandler::MaybeFixUpPortName(const std::string& port_name) { | 617 std::string SerialIoHandler::MaybeFixUpPortName(const std::string& port_name) { |
| 618 return port_name; | 618 return port_name; |
| 619 } | 619 } |
| 620 | 620 |
| 621 } // namespace device | 621 } // namespace device |
| OLD | NEW |