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.
5 #include "chrome/browser/extensions/api/serial/serial_connection.h"
10 namespace extensions {
13 int getBaudRate(int bitrate_) {
47 #if defined(OS_POSIX) && !defined(OS_MACOSX)
63 // MACOSX doesn't define constants bigger than 38400.
64 // So if it is MACOSX and the value doesn't fit any of the defined constants
65 // It will setup the bitrate with 'bitrate_' (just forwarding the value)
73 bool SerialConnection::PostOpen() {
74 struct termios options;
76 // Start with existing options and modify.
77 tcgetattr(file_, &options);
79 // Bitrate (sometimes erroneously referred to as baud rate).
81 int bitrate_opt_ = getBaudRate(bitrate_);
83 cfsetispeed(&options, bitrate_opt_);
84 cfsetospeed(&options, bitrate_opt_);
87 options.c_cflag &= ~CSIZE;
89 case serial::DATA_BIT_SEVENBIT:
90 options.c_cflag |= CS7;
92 case serial::DATA_BIT_EIGHTBIT:
94 options.c_cflag |= CS8;
98 case serial::STOP_BIT_TWOSTOPBIT:
99 options.c_cflag |= CSTOPB;
101 case serial::STOP_BIT_ONESTOPBIT:
103 options.c_cflag &= ~CSTOPB;
107 case serial::PARITY_BIT_EVENPARITY:
108 options.c_cflag |= PARENB;
109 options.c_cflag &= ~PARODD;
111 case serial::PARITY_BIT_ODDPARITY:
112 options.c_cflag |= (PARENB | PARODD);
114 case serial::PARITY_BIT_NOPARITY:
116 options.c_cflag &= ~(PARENB | PARODD);
120 // Set flags for 'raw' operation
121 // At least on Linux the flags are persistent and thus we cannot trust
122 // the default values.
123 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHONL | ISIG);
124 options.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR |
126 options.c_oflag &= ~OPOST;
128 // Enable receiver and set local mode
129 // See http://www.easysw.com/~mike/serial/serial.html to understand.
130 options.c_cflag |= (CLOCAL | CREAD);
132 // Write the options.
133 tcsetattr(file_, TCSANOW, &options);
138 bool SerialConnection::GetControlSignals(ControlSignals &control_signals) {
140 if (ioctl(file_, TIOCMGET, &status) == 0) {
141 control_signals.dcd = (status & TIOCM_CAR) != 0;
142 control_signals.cts = (status & TIOCM_CTS) != 0;
148 bool SerialConnection::SetControlSignals(
149 const ControlSignals &control_signals) {
152 if (ioctl(file_, TIOCMGET, &status) != 0)
155 if (control_signals.should_set_dtr) {
156 if (control_signals.dtr)
159 status &= ~TIOCM_DTR;
161 if (control_signals.should_set_rts) {
162 if (control_signals.rts)
165 status &= ~TIOCM_RTS;
168 return ioctl(file_, TIOCMSET, &status) == 0;
171 std::string SerialConnection::MaybeFixUpPortName(
172 const std::string &port_name) {
176 } // namespace extensions