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

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: updated 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_ = ErrorDetectState::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);
Reilly Grant (use Gerrit) 2015/07/30 00:09:12 Sorry, I should have caught this earlier. ReadComp
juncai 2015/07/30 16:04:36 Done.
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' --> ErrorDetectState::MARK_377_SEEN
489 // '\0' --> ErrorDetectState::MARK_0_SEEN
490 // '\0' --> break detected
491 //
492 // parity error sequence:
493 // '\377' --> ErrorDetectState::MARK_377_SEEN
494 // '\0' --> ErrorDetectState::MARK_0_SEEN
495 // character with parity error --> parity error detected
496 //
497 // break/parity error sequences are removed from the byte stream
498 // '\377' '\377' sequence is replaced with '\377'
499 int SerialIoHandlerPosix::CheckReceiveError(char* buffer,
500 int buffer_len,
501 int bytes_read,
502 bool& break_detected,
503 bool& parity_error_detected) {
504 int new_bytes_read = num_chars_ignored_;
505
506 for (int i = 0; i < bytes_read; ++i) {
507 char ch = buffer[i];
508 if (new_bytes_read == 0) {
509 chars_ignored_[0] = ch;
510 } else if (new_bytes_read == 1) {
511 chars_ignored_[1] = ch;
512 } else {
513 buffer[new_bytes_read - 2] = ch;
514 }
515 ++new_bytes_read;
516 switch (error_detect_state_) {
517 case ErrorDetectState::NO_ERROR:
518 if (ch == '\377') {
519 error_detect_state_ = ErrorDetectState::MARK_377_SEEN;
520 }
521 break;
522 case ErrorDetectState::MARK_377_SEEN:
523 if (ch == '\0') {
524 error_detect_state_ = ErrorDetectState::MARK_0_SEEN;
525 } else {
526 if (ch == '\377') {
527 // receive two bytes '\377' '\377', since ISTRIP is not set and
528 // PARMRK is set, a valid byte '\377' is passed to the program as
529 // two bytes, '\377' '\377'. Replace these two bytes with one byte
530 // of '\377', and set error_detect_state_ back to
531 // ErrorDetectState::NO_ERROR.
532 --new_bytes_read;
533 }
534 error_detect_state_ = ErrorDetectState::NO_ERROR;
535 }
536 break;
537 case ErrorDetectState::MARK_0_SEEN:
538 if (ch == '\0') {
539 break_detected = true;
540 new_bytes_read -= 3;
541 error_detect_state_ = ErrorDetectState::NO_ERROR;
542 } else {
543 if (parity_check_enabled_) {
544 parity_error_detected = true;
545 new_bytes_read -= 3;
546 error_detect_state_ = ErrorDetectState::NO_ERROR;
547 } else if (ch == '\377') {
548 error_detect_state_ = ErrorDetectState::MARK_377_SEEN;
549 } else {
550 error_detect_state_ = ErrorDetectState::NO_ERROR;
551 }
552 }
553 break;
554 }
555 }
556 // Now number of new_bytes_read bytes are read (including the previously
557 // ignored characters that were stored at chars_ignored_[]) and are stored at:
558 // chars_ignored_[0], chars_ignored_[1], buffer[...].
559
560 // Ignore some characters that are potentially part of the break/parity error
561 // sequence or buffer is not large enough to store all the bytes.
562 // tmp[] stores the characters that need to be ignored for this read.
563 char tmp[2];
564 num_chars_ignored_ = 0;
565 if (error_detect_state_ == ErrorDetectState::MARK_0_SEEN ||
566 new_bytes_read - buffer_len == 2) {
567 // need to ignore the last two characters
568 if (new_bytes_read == 2) {
569 memcpy(tmp, chars_ignored_, new_bytes_read);
570 } else {
571 if (new_bytes_read == 3) {
572 tmp[0] = chars_ignored_[1];
573 } else {
574 tmp[0] = buffer[new_bytes_read - 4];
575 }
576 tmp[1] = buffer[new_bytes_read - 3];
577 }
578 num_chars_ignored_ = 2;
579 } else if (error_detect_state_ == ErrorDetectState::MARK_377_SEEN ||
580 new_bytes_read - buffer_len == 1) {
581 // need to ignore the last character
582 if (new_bytes_read <= 2) {
583 tmp[0] = chars_ignored_[new_bytes_read - 1];
584 } else {
585 tmp[0] = buffer[new_bytes_read - 3];
586 }
587 num_chars_ignored_ = 1;
588 }
589
590 new_bytes_read -= num_chars_ignored_;
591 if (new_bytes_read > 2) {
592 // right shift two bytes to store bytes from chars_ignored_[]
593 memmove(buffer + 2, buffer, new_bytes_read - 2);
594 }
595 memcpy(buffer, chars_ignored_, std::min(new_bytes_read, 2));
596 memcpy(chars_ignored_, tmp, num_chars_ignored_);
597 return new_bytes_read;
598 }
599
457 std::string SerialIoHandler::MaybeFixUpPortName(const std::string& port_name) { 600 std::string SerialIoHandler::MaybeFixUpPortName(const std::string& port_name) {
458 return port_name; 601 return port_name;
459 } 602 }
460 603
461 } // namespace device 604 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698