- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / serial / serial_connection_posix.cc
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 namespace {
13   int getBaudRate(int bitrate_) {
14     switch (bitrate_) {
15       case 0:
16         return B0;
17       case 50:
18         return B50;
19       case 75:
20         return B75;
21       case 110:
22         return B110;
23       case 134:
24         return B134;
25       case 150:
26         return B150;
27       case 200:
28         return B200;
29       case 300:
30         return B300;
31       case 600:
32         return B600;
33       case 1200:
34         return B1200;
35       case 1800:
36         return B1800;
37       case 2400:
38         return B2400;
39       case 4800:
40         return B4800;
41       case 9600:
42         return B9600;
43       case 19200:
44         return B19200;
45       case 38400:
46         return B38400;
47 #if defined(OS_POSIX) && !defined(OS_MACOSX)
48       case 57600:
49         return B57600;
50       case 115200:
51         return B115200;
52       case 230400:
53         return B230400;
54       case 460800:
55         return B460800;
56       case 576000:
57         return B576000;
58       case 921600:
59         return B921600;
60       default:
61         return B9600;
62 #else
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)
66       default:
67         return bitrate_;
68 #endif
69     }
70   }
71 }  // namespace
72
73 bool SerialConnection::PostOpen() {
74   struct termios options;
75
76   // Start with existing options and modify.
77   tcgetattr(file_, &options);
78
79   // Bitrate (sometimes erroneously referred to as baud rate).
80   if (bitrate_ >= 0) {
81     int bitrate_opt_ = getBaudRate(bitrate_);
82
83     cfsetispeed(&options, bitrate_opt_);
84     cfsetospeed(&options, bitrate_opt_);
85   }
86
87   options.c_cflag &= ~CSIZE;
88   switch (databit_) {
89     case serial::DATA_BIT_SEVENBIT:
90       options.c_cflag |= CS7;
91       break;
92     case serial::DATA_BIT_EIGHTBIT:
93     default:
94       options.c_cflag |= CS8;
95       break;
96   }
97   switch (stopbit_) {
98     case serial::STOP_BIT_TWOSTOPBIT:
99       options.c_cflag |= CSTOPB;
100       break;
101     case serial::STOP_BIT_ONESTOPBIT:
102     default:
103       options.c_cflag &= ~CSTOPB;
104       break;
105   }
106   switch (parity_) {
107     case serial::PARITY_BIT_EVENPARITY:
108       options.c_cflag |= PARENB;
109       options.c_cflag &= ~PARODD;
110       break;
111     case serial::PARITY_BIT_ODDPARITY:
112       options.c_cflag |= (PARENB | PARODD);
113       break;
114     case serial::PARITY_BIT_NOPARITY:
115     default:
116       options.c_cflag &= ~(PARENB | PARODD);
117       break;
118   }
119
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 |
125                        ICRNL | IXON);
126   options.c_oflag &= ~OPOST;
127
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);
131
132   // Write the options.
133   tcsetattr(file_, TCSANOW, &options);
134
135   return true;
136 }
137
138 bool SerialConnection::GetControlSignals(ControlSignals &control_signals) {
139   int status;
140   if (ioctl(file_, TIOCMGET, &status) == 0) {
141     control_signals.dcd = (status & TIOCM_CAR) != 0;
142     control_signals.cts = (status & TIOCM_CTS) != 0;
143     return true;
144   }
145   return false;
146 }
147
148 bool SerialConnection::SetControlSignals(
149     const ControlSignals &control_signals) {
150   int status;
151
152   if (ioctl(file_, TIOCMGET, &status) != 0)
153     return false;
154
155   if (control_signals.should_set_dtr) {
156     if (control_signals.dtr)
157       status |= TIOCM_DTR;
158     else
159       status &= ~TIOCM_DTR;
160   }
161   if (control_signals.should_set_rts) {
162     if (control_signals.rts)
163       status |= TIOCM_RTS;
164     else
165       status &= ~TIOCM_RTS;
166   }
167
168   return ioctl(file_, TIOCMSET, &status) == 0;
169 }
170
171 std::string SerialConnection::MaybeFixUpPortName(
172     const std::string &port_name) {
173   return port_name;
174 }
175
176 }  // namespace extensions