1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/browser/extensions/api/serial/serial_connection.h" 6 7 #include <sys/ioctl.h> 8 #include <termios.h> 9 10 namespace extensions { 11 12 bool SerialConnection::PostOpen() { 13 struct termios options; 14 15 // Start with existing options and modify. 16 tcgetattr(file_, &options); 17 18 // Bitrate (sometimes erroneously referred to as baud rate). 19 if (bitrate_ >= 0) { 20 int bitrate_opt_; 21 switch (bitrate_) { 22 case 0: 23 bitrate_opt_ = B0; 24 break; 25 case 50: 26 bitrate_opt_ = B50; 27 break; 28 case 75: 29 bitrate_opt_ = B75; 30 break; 31 case 110: 32 bitrate_opt_ = B110; 33 break; 34 case 134: 35 bitrate_opt_ = B134; 36 break; 37 case 150: 38 bitrate_opt_ = B150; 39 break; 40 case 200: 41 bitrate_opt_ = B200; 42 break; 43 case 300: 44 bitrate_opt_ = B300; 45 break; 46 case 600: 47 bitrate_opt_ = B600; 48 break; 49 case 1200: 50 bitrate_opt_ = B1200; 51 break; 52 case 1800: 53 bitrate_opt_ = B1800; 54 break; 55 case 2400: 56 bitrate_opt_ = B2400; 57 break; 58 case 4800: 59 bitrate_opt_ = B4800; 60 break; 61 case 9600: 62 bitrate_opt_ = B9600; 63 break; 64 case 19200: 65 bitrate_opt_ = B19200; 66 break; 67 case 38400: 68 bitrate_opt_ = B38400; 69 break; 70 #if defined(OS_POSIX) && !defined(OS_MACOSX) 71 case 57600: 72 bitrate_opt_ = B57600; 73 break; 74 case 115200: 75 bitrate_opt_ = B115200; 76 break; 77 case 230400: 78 bitrate_opt_ = B230400; 79 break; 80 case 460800: 81 bitrate_opt_ = B460800; 82 break; 83 case 576000: 84 bitrate_opt_ = B576000; 85 break; 86 case 921600: 87 bitrate_opt_ = B921600; 88 break; 89 default: 90 bitrate_opt_ = B9600; 91 #else 92 // MACOSX doesn't define constants bigger than 38400. 93 // So if it is MACOSX and the value doesn't fit any of the defined constants 94 // It will setup the bitrate with 'bitrate_' (just forwarding the value) 95 default: 96 bitrate_opt_ = bitrate_; 97 #endif 98 } 99 100 cfsetispeed(&options, bitrate_opt_); 101 cfsetospeed(&options, bitrate_opt_); 102 } 103 104 // 8N1 105 options.c_cflag &= ~PARENB; 106 options.c_cflag &= ~CSTOPB; 107 options.c_cflag &= ~CSIZE; 108 options.c_cflag |= CS8; 109 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); 110 111 // Enable receiver and set local mode 112 // See http://www.easysw.com/~mike/serial/serial.html to understand. 113 options.c_cflag |= (CLOCAL | CREAD); 114 115 // Write the options. 116 tcsetattr(file_, TCSANOW, &options); 117 118 return true; 119 } 120 121 bool SerialConnection::GetControlSignals(ControlSignals &control_signals) { 122 int status; 123 if (ioctl(file_, TIOCMGET, &status) == 0) { 124 control_signals.dcd = (status & TIOCM_CAR) != 0; 125 control_signals.cts = (status & TIOCM_CTS) != 0; 126 return true; 127 } 128 return false; 129 } 130 131 bool SerialConnection::SetControlSignals( 132 const ControlSignals &control_signals) { 133 int status; 134 135 if (ioctl(file_, TIOCMGET, &status) != 0) 136 return false; 137 138 if (control_signals.should_set_dtr) { 139 if (control_signals.dtr) 140 status |= TIOCM_DTR; 141 else 142 status &= ~TIOCM_DTR; 143 } 144 if (control_signals.should_set_rts) { 145 if (control_signals.rts) 146 status |= TIOCM_RTS; 147 else 148 status &= ~TIOCM_RTS; 149 } 150 151 return ioctl(file_, TIOCMSET, &status) == 0; 152 } 153 154 std::string SerialConnection::MaybeFixUpPortName( 155 const std::string &port_name) { 156 return port_name; 157 } 158 159 } // namespace extensions 160