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

Side by Side Diff: chrome/browser/extensions/api/serial/serial_connection_posix.cc

Issue 22804008: Adds Serial API to set data bits, parity, stop bits. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Created 7 years, 3 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "chrome/browser/extensions/api/serial/serial_connection.h" 5 #include "chrome/browser/extensions/api/serial/serial_connection.h"
6 6
7 #include <sys/ioctl.h> 7 #include <sys/ioctl.h>
8 #include <termios.h> 8 #include <termios.h>
9 9
10 namespace extensions { 10 namespace extensions {
11 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
12 bool SerialConnection::PostOpen() { 73 bool SerialConnection::PostOpen() {
13 struct termios options; 74 struct termios options;
14 75
15 // Start with existing options and modify. 76 // Start with existing options and modify.
16 tcgetattr(file_, &options); 77 tcgetattr(file_, &options);
17 78
18 // Bitrate (sometimes erroneously referred to as baud rate). 79 // Bitrate (sometimes erroneously referred to as baud rate).
19 if (bitrate_ >= 0) { 80 if (bitrate_ >= 0) {
20 int bitrate_opt_; 81 int bitrate_opt_ = getBaudRate(bitrate_);
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 82
100 cfsetispeed(&options, bitrate_opt_); 83 cfsetispeed(&options, bitrate_opt_);
101 cfsetospeed(&options, bitrate_opt_); 84 cfsetospeed(&options, bitrate_opt_);
102 } 85 }
103 86
104 // 8N1
105 options.c_cflag &= ~PARENB;
106 options.c_cflag &= ~CSTOPB;
107 options.c_cflag &= ~CSIZE; 87 options.c_cflag &= ~CSIZE;
108 options.c_cflag |= CS8; 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 }
109 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); 119 options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
110 120
111 // Enable receiver and set local mode 121 // Enable receiver and set local mode
112 // See http://www.easysw.com/~mike/serial/serial.html to understand. 122 // See http://www.easysw.com/~mike/serial/serial.html to understand.
113 options.c_cflag |= (CLOCAL | CREAD); 123 options.c_cflag |= (CLOCAL | CREAD);
114 124
115 // Write the options. 125 // Write the options.
116 tcsetattr(file_, TCSANOW, &options); 126 tcsetattr(file_, TCSANOW, &options);
117 127
118 return true; 128 return true;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 160
151 return ioctl(file_, TIOCMSET, &status) == 0; 161 return ioctl(file_, TIOCMSET, &status) == 0;
152 } 162 }
153 163
154 std::string SerialConnection::MaybeFixUpPortName( 164 std::string SerialConnection::MaybeFixUpPortName(
155 const std::string &port_name) { 165 const std::string &port_name) {
156 return port_name; 166 return port_name;
157 } 167 }
158 168
159 } // namespace extensions 169 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698