]>
Commit | Line | Data |
---|---|---|
a1bb33af UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2010 Bert Vermeulen <bert@biot.com> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
54b38f64 | 20 | #include <string.h> |
d02a535e BV |
21 | #include <sys/types.h> |
22 | #include <sys/stat.h> | |
23 | #include <fcntl.h> | |
24 | #include <unistd.h> | |
926b866c UH |
25 | #ifdef _WIN32 |
26 | #include <conio.h> | |
27 | #else | |
28 | #include <glob.h> | |
d02a535e | 29 | #include <termios.h> |
926b866c | 30 | #endif |
d02a535e | 31 | #include <stdlib.h> |
a1bb33af | 32 | #include <glib.h> |
986f7270 | 33 | #include <sigrok.h> |
a1bb33af | 34 | |
926b866c UH |
35 | // FIXME: Must be moved, or rather passed as function argument. |
36 | #ifdef _WIN32 | |
37 | HANDLE hdl; | |
38 | #endif | |
39 | ||
a1bb33af UH |
40 | char *serial_port_glob[] = { |
41 | /* Linux */ | |
42 | "/dev/ttyS*", | |
43 | "/dev/ttyUSB*", | |
44 | "/dev/ttyACM*", | |
45 | /* MacOS X */ | |
46 | "/dev/ttys*", | |
47 | "/dev/tty.USB-*", | |
48 | "/dev/tty.Modem-*", | |
986f7270 | 49 | NULL, |
a1bb33af UH |
50 | }; |
51 | ||
a1bb33af UH |
52 | GSList *list_serial_ports(void) |
53 | { | |
926b866c UH |
54 | #ifdef _WIN32 |
55 | /* TODO */ | |
56 | #else | |
a1bb33af UH |
57 | glob_t g; |
58 | GSList *ports; | |
afc8e4de | 59 | unsigned int i, j; |
a1bb33af UH |
60 | |
61 | ports = NULL; | |
986f7270 | 62 | for (i = 0; serial_port_glob[i]; i++) { |
71dda106 PS |
63 | if (glob(serial_port_glob[i], 0, NULL, &g)) |
64 | continue; | |
65 | for (j = 0; j < g.gl_pathc; j++) | |
66 | ports = g_slist_append(ports, strdup(g.gl_pathv[j])); | |
67 | globfree(&g); | |
a1bb33af UH |
68 | } |
69 | ||
70 | return ports; | |
926b866c | 71 | #endif |
a1bb33af UH |
72 | } |
73 | ||
d02a535e BV |
74 | int serial_open(const char *pathname, int flags) |
75 | { | |
926b866c UH |
76 | #ifdef _WIN32 |
77 | /* FIXME: Don't hardcode COM1. */ | |
78 | hdl = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, | |
79 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); | |
80 | if (hdl == INVALID_HANDLE_VALUE) { | |
81 | /* TODO: Error handling. */ | |
82 | } | |
83 | return 0; | |
84 | #else | |
d02a535e | 85 | return open(pathname, flags); |
926b866c | 86 | #endif |
d02a535e BV |
87 | } |
88 | ||
d02a535e BV |
89 | int serial_close(int fd) |
90 | { | |
926b866c UH |
91 | #ifdef _WIN32 |
92 | CloseHandle(hdl); | |
93 | #else | |
d02a535e | 94 | return close(fd); |
926b866c | 95 | #endif |
d02a535e BV |
96 | } |
97 | ||
06d64eb8 BV |
98 | int serial_flush(int fd) |
99 | { | |
100 | ||
f0551a65 | 101 | return tcflush(fd, TCIOFLUSH); |
06d64eb8 BV |
102 | } |
103 | ||
d02a535e BV |
104 | void *serial_backup_params(int fd) |
105 | { | |
926b866c UH |
106 | #ifdef _WIN32 |
107 | /* TODO */ | |
108 | #else | |
d02a535e BV |
109 | struct termios *term; |
110 | ||
111 | term = malloc(sizeof(struct termios)); | |
112 | tcgetattr(fd, term); | |
113 | ||
114 | return term; | |
926b866c | 115 | #endif |
d02a535e BV |
116 | } |
117 | ||
d02a535e BV |
118 | void serial_restore_params(int fd, void *backup) |
119 | { | |
926b866c UH |
120 | #ifdef _WIN32 |
121 | /* TODO */ | |
122 | #else | |
986f7270 | 123 | tcsetattr(fd, TCSADRAIN, (struct termios *)backup); |
926b866c | 124 | #endif |
d02a535e BV |
125 | } |
126 | ||
1ff7712c DR |
127 | /* |
128 | * flowcontrol 1 = rts/cts 2 = xon/xoff | |
129 | * parity 0 = none, 1 = even, 2 = odd | |
130 | */ | |
986f7270 UH |
131 | int serial_set_params(int fd, int speed, int bits, int parity, int stopbits, |
132 | int flowcontrol) | |
d02a535e | 133 | { |
926b866c UH |
134 | #ifdef _WIN32 |
135 | DCB dcb; | |
136 | ||
137 | if (!GetCommState(hdl, &dcb)) { | |
138 | /* TODO: Error handling. */ | |
139 | } | |
140 | ||
141 | /* TODO: Rename 'speed' to 'baudrate'. */ | |
142 | switch(speed) { | |
143 | case 115200: | |
144 | dcb.BaudRate = CBR_115200; | |
145 | break; | |
146 | case 57600: | |
147 | dcb.BaudRate = CBR_57600; | |
148 | break; | |
149 | case 38400: | |
150 | dcb.BaudRate = CBR_38400; | |
151 | break; | |
152 | case 19200: | |
153 | dcb.BaudRate = CBR_19200; | |
154 | break; | |
155 | case 9600: | |
156 | dcb.BaudRate = CBR_9600; | |
157 | break; | |
158 | default: | |
159 | /* TODO: Error handling. */ | |
160 | break; | |
161 | } | |
162 | dcb.ByteSize = bits; | |
163 | dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */ | |
164 | dcb.StopBits = ONESTOPBIT; /* TODO: Don't hardcode. */ | |
165 | ||
166 | if (!SetCommState(hdl, &dcb)) { | |
167 | /* TODO: Error handling. */ | |
168 | } | |
169 | #else | |
d02a535e | 170 | struct termios term; |
1ff7712c | 171 | speed_t baud; |
d02a535e | 172 | |
1ff7712c DR |
173 | switch (speed) { |
174 | case 9600: | |
175 | baud = B9600; | |
176 | break; | |
177 | case 38400: | |
178 | baud = B38400; | |
179 | break; | |
180 | case 57600: | |
181 | baud = B57600; | |
182 | break; | |
183 | case 115200: | |
184 | baud = B115200; | |
185 | break; | |
186 | case 460800: | |
187 | baud = B460800; | |
188 | break; | |
189 | default: | |
d02a535e | 190 | return SIGROK_ERR; |
1ff7712c | 191 | } |
d02a535e | 192 | |
986f7270 | 193 | if (tcgetattr(fd, &term) < 0) |
d02a535e | 194 | return SIGROK_ERR; |
1ff7712c | 195 | if (cfsetispeed(&term, baud) < 0) |
d02a535e | 196 | return SIGROK_ERR; |
1ff7712c | 197 | |
d02a535e | 198 | term.c_cflag &= ~CSIZE; |
1ff7712c DR |
199 | switch (bits) { |
200 | case 8: | |
201 | term.c_cflag |= CS8; | |
202 | break; | |
203 | case 7: | |
204 | term.c_cflag |= CS7; | |
205 | break; | |
206 | default: | |
207 | return SIGROK_ERR; | |
208 | } | |
209 | ||
d02a535e | 210 | term.c_cflag &= ~CSTOPB; |
1ff7712c DR |
211 | switch (stopbits) { |
212 | case 1: | |
213 | break; | |
214 | case 2: | |
215 | term.c_cflag |= CSTOPB; | |
216 | default: | |
217 | return SIGROK_ERR; | |
218 | } | |
219 | ||
220 | term.c_cflag &= ~(IXON | IXOFF | CRTSCTS); | |
221 | switch (flowcontrol) { | |
222 | case 2: | |
223 | term.c_cflag |= IXON | IXOFF; | |
224 | break; | |
225 | case 1: | |
226 | term.c_cflag |= CRTSCTS; | |
227 | default: | |
228 | return SIGROK_ERR; | |
229 | } | |
230 | ||
ac4a2ea4 DR |
231 | term.c_iflag &= ~IGNPAR; |
232 | term.c_cflag &= ~(PARODD | PARENB); | |
1ff7712c DR |
233 | switch (parity) { |
234 | case 0: | |
235 | term.c_iflag |= IGNPAR; | |
236 | break; | |
237 | case 1: | |
ac4a2ea4 | 238 | term.c_cflag |= PARENB; |
1ff7712c DR |
239 | break; |
240 | case 2: | |
ac4a2ea4 | 241 | term.c_cflag |= PARENB | PARODD; |
1ff7712c DR |
242 | break; |
243 | default: | |
244 | return SIGROK_ERR; | |
245 | } | |
246 | ||
986f7270 | 247 | if (tcsetattr(fd, TCSADRAIN, &term) < 0) |
d02a535e | 248 | return SIGROK_ERR; |
926b866c | 249 | #endif |
d02a535e BV |
250 | |
251 | return SIGROK_OK; | |
252 | } |