Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(177)

Side by Side Diff: device/serial/serial_io_handler_posix.cc

Issue 1249933004: Add code to detect new added ReceiveError on Linux (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: added more test code. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 179
180 bool SerialIoHandlerPosix::ConfigurePortImpl() { 180 bool SerialIoHandlerPosix::ConfigurePortImpl() {
181 struct termios config; 181 struct termios config;
182 if (tcgetattr(file().GetPlatformFile(), &config) != 0) { 182 if (tcgetattr(file().GetPlatformFile(), &config) != 0) {
183 VPLOG(1) << "Failed to get port attributes"; 183 VPLOG(1) << "Failed to get port attributes";
184 return false; 184 return false;
185 } 185 }
186 186
187 // Set flags for 'raw' operation 187 // Set flags for 'raw' operation
188 config.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHONL | ISIG); 188 config.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHONL | ISIG);
189 config.c_iflag &= 189 config.c_iflag &= ~(IGNBRK | BRKINT | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
190 ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON); 190 config.c_iflag |= PARMRK;
191 config.c_oflag &= ~OPOST; 191 config.c_oflag &= ~OPOST;
192 192
193 // CLOCAL causes the system to disregard the DCD signal state. 193 // CLOCAL causes the system to disregard the DCD signal state.
194 // CREAD enables reading from the port. 194 // CREAD enables reading from the port.
195 config.c_cflag |= (CLOCAL | CREAD); 195 config.c_cflag |= (CLOCAL | CREAD);
196 196
197 DCHECK(options().bitrate); 197 DCHECK(options().bitrate);
198 speed_t bitrate_opt = B0; 198 speed_t bitrate_opt = B0;
199 if (BitrateToSpeedConstant(options().bitrate, &bitrate_opt)) { 199 if (BitrateToSpeedConstant(options().bitrate, &bitrate_opt)) {
200 cfsetispeed(&config, bitrate_opt); 200 cfsetispeed(&config, bitrate_opt);
(...skipping 26 matching lines...) Expand all
227 break; 227 break;
228 case serial::PARITY_BIT_ODD: 228 case serial::PARITY_BIT_ODD:
229 config.c_cflag |= (PARODD | PARENB); 229 config.c_cflag |= (PARODD | PARENB);
230 break; 230 break;
231 case serial::PARITY_BIT_NO: 231 case serial::PARITY_BIT_NO:
232 default: 232 default:
233 config.c_cflag &= ~(PARODD | PARENB); 233 config.c_cflag &= ~(PARODD | PARENB);
234 break; 234 break;
235 } 235 }
236 236
237 error_detect_state_ = NO_ERROR;
238 num_chars_ignored_ = 0;
239
240 if (config.c_cflag & PARENB) {
241 config.c_iflag &= ~IGNPAR;
242 config.c_iflag |= INPCK;
243 parity_check_enabled_ = true;
244 } else {
245 config.c_iflag |= IGNPAR;
246 config.c_iflag &= ~INPCK;
247 parity_check_enabled_ = false;
248 }
249
237 DCHECK(options().stop_bits != serial::STOP_BITS_NONE); 250 DCHECK(options().stop_bits != serial::STOP_BITS_NONE);
238 switch (options().stop_bits) { 251 switch (options().stop_bits) {
239 case serial::STOP_BITS_TWO: 252 case serial::STOP_BITS_TWO:
240 config.c_cflag |= CSTOPB; 253 config.c_cflag |= CSTOPB;
241 break; 254 break;
242 case serial::STOP_BITS_ONE: 255 case serial::STOP_BITS_ONE:
243 default: 256 default:
244 config.c_cflag &= ~CSTOPB; 257 config.c_cflag &= ~CSTOPB;
245 break; 258 break;
246 } 259 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 pending_read_buffer_len())); 293 pending_read_buffer_len()));
281 if (bytes_read < 0) { 294 if (bytes_read < 0) {
282 if (errno == ENXIO) { 295 if (errno == ENXIO) {
283 ReadCompleted(0, serial::RECEIVE_ERROR_DEVICE_LOST); 296 ReadCompleted(0, serial::RECEIVE_ERROR_DEVICE_LOST);
284 } else { 297 } else {
285 ReadCompleted(0, serial::RECEIVE_ERROR_SYSTEM_ERROR); 298 ReadCompleted(0, serial::RECEIVE_ERROR_SYSTEM_ERROR);
286 } 299 }
287 } else if (bytes_read == 0) { 300 } else if (bytes_read == 0) {
288 ReadCompleted(0, serial::RECEIVE_ERROR_DEVICE_LOST); 301 ReadCompleted(0, serial::RECEIVE_ERROR_DEVICE_LOST);
289 } else { 302 } else {
290 ReadCompleted(bytes_read, serial::RECEIVE_ERROR_NONE); 303 bool break_detected = false;
304 bool parity_error_detected = false;
305
306 int new_bytes_read =
307 CheckReceiveError(pending_read_buffer(), pending_read_buffer_len(),
308 bytes_read, break_detected, parity_error_detected);
309
310 if (new_bytes_read != 0) {
311 ReadCompleted(new_bytes_read, serial::RECEIVE_ERROR_NONE);
312 }
313
314 if (break_detected) {
315 ReadCompleted(0, serial::RECEIVE_ERROR_BREAK);
316 }
317
318 if (parity_error_detected) {
319 ReadCompleted(0, serial::RECEIVE_ERROR_PARITY_ERROR);
320 }
291 } 321 }
292 } else { 322 } else {
293 // Stop watching the fd if we get notifications with no pending 323 // Stop watching the fd if we get notifications with no pending
294 // reads or writes to avoid starving the message loop. 324 // reads or writes to avoid starving the message loop.
295 is_watching_reads_ = false; 325 is_watching_reads_ = false;
296 file_read_watcher_.StopWatchingFileDescriptor(); 326 file_read_watcher_.StopWatchingFileDescriptor();
297 } 327 }
298 } 328 }
299 329
300 void SerialIoHandlerPosix::OnFileCanWriteWithoutBlocking(int fd) { 330 void SerialIoHandlerPosix::OnFileCanWriteWithoutBlocking(int fd) {
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
447 } 477 }
448 478
449 bool SerialIoHandlerPosix::ClearBreak() { 479 bool SerialIoHandlerPosix::ClearBreak() {
450 if (ioctl(file().GetPlatformFile(), TIOCCBRK, 0) != 0) { 480 if (ioctl(file().GetPlatformFile(), TIOCCBRK, 0) != 0) {
451 VPLOG(1) << "Failed to clear break"; 481 VPLOG(1) << "Failed to clear break";
452 return false; 482 return false;
453 } 483 }
454 return true; 484 return true;
455 } 485 }
456 486
487 // break sequence:
488 // '\377' '\0' '\0'
489 // MARK_377_SEEN MARK_0_SEEN break detected
490 //
491 // parity error sequence:
492 // '\377' '\0' character with parity error
493 // MARK_377_SEEN MARK_0_SEEN parity error detected
494 //
495 // break/parity error sequences are removed from the byte stream
496 // '\377' '\377' sequence is replaced with '\377'
497 int SerialIoHandlerPosix::CheckReceiveError(char* buffer,
498 int buffer_len,
499 int bytes_read,
500 bool& break_detected,
501 bool& parity_error_detected) {
502 int new_bytes_read = num_chars_ignored_;
503
504 for (int i = 0; i < bytes_read; ++i) {
505 char ch = buffer[i];
506 if (new_bytes_read == 0) {
507 chars_ignored_[0] = ch;
508 } else if (new_bytes_read == 1) {
509 chars_ignored_[1] = ch;
510 } else {
511 buffer[new_bytes_read - 2] = ch;
512 }
513 ++new_bytes_read;
514 switch (error_detect_state_) {
515 case NO_ERROR:
516 if (ch == '\377') {
517 error_detect_state_ = MARK_377_SEEN;
518 }
519 break;
520 case MARK_377_SEEN:
521 if (ch == '\0') {
522 error_detect_state_ = MARK_0_SEEN;
523 } else {
524 if (ch == '\377') {
525 // receive two bytes '\377' '\377', since ISTRIP is not set and
526 // PARMRK is set, a valid byte '\377' is passed to the program as
527 // two bytes, '\377' '\377'. Replace these two bytes with one byte
528 // of '\377', and set error_detect_state_ back to NO_ERROR.
529 --new_bytes_read;
530 }
531 error_detect_state_ = NO_ERROR;
532 }
533 break;
534 case MARK_0_SEEN:
535 if (ch == '\0') {
536 break_detected = true;
537 new_bytes_read -= 3;
538 error_detect_state_ = NO_ERROR;
539 } else {
540 if (parity_check_enabled_) {
541 parity_error_detected = true;
542 new_bytes_read -= 3;
543 error_detect_state_ = NO_ERROR;
544 } else if (ch == '\377') {
545 error_detect_state_ = MARK_377_SEEN;
546 } else {
547 error_detect_state_ = NO_ERROR;
548 }
549 }
550 break;
551 }
552 }
553 // Now number of new_bytes_read bytes are read (including the previously
554 // ignored characters that were stored at chars_ignored_[]) and are stored at:
555 // chars_ignored_[0], chars_ignored_[1], buffer[...].
556
557 // Ignore some characters that are potentially part of the break/parity error
558 // sequence or buffer is not large enough to store all the bytes.
559 // tmp[] stores the characters that need to be ignored for this read.
560 char tmp[2];
561 num_chars_ignored_ = 0;
562 if (error_detect_state_ == MARK_0_SEEN || new_bytes_read - buffer_len == 2) {
563 // need to ignore the last two characters
564 if (new_bytes_read == 2) {
565 memcpy(tmp, chars_ignored_, new_bytes_read);
566 } else {
567 if (new_bytes_read == 3) {
568 tmp[0] = chars_ignored_[1];
569 } else {
570 tmp[0] = buffer[new_bytes_read - 4];
571 }
572 tmp[1] = buffer[new_bytes_read - 3];
573 }
574 num_chars_ignored_ = 2;
575 } else if (error_detect_state_ == MARK_377_SEEN ||
576 new_bytes_read - buffer_len == 1) {
577 // need to ignore the last character
578 if (new_bytes_read <= 2) {
579 tmp[0] = chars_ignored_[new_bytes_read - 1];
580 } else {
581 tmp[0] = buffer[new_bytes_read - 3];
582 }
583 num_chars_ignored_ = 1;
584 }
585
586 new_bytes_read -= num_chars_ignored_;
587 if (new_bytes_read > 2) {
588 // right shift two bytes to store bytes from chars_ignored_[]
589 memmove(buffer + 2, buffer, new_bytes_read - 2);
590 }
591 memcpy(buffer, chars_ignored_, std::min(new_bytes_read, 2));
592 memcpy(chars_ignored_, tmp, num_chars_ignored_);
593 return new_bytes_read;
594 }
595
457 std::string SerialIoHandler::MaybeFixUpPortName(const std::string& port_name) { 596 std::string SerialIoHandler::MaybeFixUpPortName(const std::string& port_name) {
458 return port_name; 597 return port_name;
459 } 598 }
460 599
461 } // namespace device 600 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698