]>
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 | { | |
2119ab03 UH |
54 | GSList *ports; |
55 | ||
926b866c UH |
56 | #ifdef _WIN32 |
57 | /* TODO */ | |
2119ab03 UH |
58 | ports = NULL; |
59 | ports = g_slist_append(ports, strdup("COM1")); | |
926b866c | 60 | #else |
a1bb33af | 61 | glob_t g; |
afc8e4de | 62 | unsigned int i, j; |
a1bb33af UH |
63 | |
64 | ports = NULL; | |
986f7270 | 65 | for (i = 0; serial_port_glob[i]; i++) { |
71dda106 PS |
66 | if (glob(serial_port_glob[i], 0, NULL, &g)) |
67 | continue; | |
68 | for (j = 0; j < g.gl_pathc; j++) | |
69 | ports = g_slist_append(ports, strdup(g.gl_pathv[j])); | |
70 | globfree(&g); | |
a1bb33af | 71 | } |
2119ab03 | 72 | #endif |
a1bb33af UH |
73 | |
74 | return ports; | |
75 | } | |
76 | ||
d02a535e BV |
77 | int serial_open(const char *pathname, int flags) |
78 | { | |
926b866c UH |
79 | #ifdef _WIN32 |
80 | /* FIXME: Don't hardcode COM1. */ | |
81 | hdl = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, | |
82 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); | |
83 | if (hdl == INVALID_HANDLE_VALUE) { | |
84 | /* TODO: Error handling. */ | |
85 | } | |
86 | return 0; | |
87 | #else | |
d02a535e | 88 | return open(pathname, flags); |
926b866c | 89 | #endif |
d02a535e BV |
90 | } |
91 | ||
2119ab03 UH |
92 | /* |
93 | * Close the serial port. | |
94 | * Returns 0 upon success, -1 upon failure. | |
95 | */ | |
d02a535e BV |
96 | int serial_close(int fd) |
97 | { | |
926b866c | 98 | #ifdef _WIN32 |
2119ab03 UH |
99 | /* Returns non-zero upon success, 0 upon failure. */ |
100 | return (CloseHandle(hdl) == 0) ? -1 : 0; | |
926b866c | 101 | #else |
2119ab03 | 102 | /* Returns 0 upon success, -1 upon failure. */ |
d02a535e | 103 | return close(fd); |
926b866c | 104 | #endif |
d02a535e BV |
105 | } |
106 | ||
1fdb75e1 UH |
107 | /* |
108 | * Flush serial port buffers (if any). | |
109 | * Returns 0 upon success, -1 upon failure. | |
110 | */ | |
06d64eb8 BV |
111 | int serial_flush(int fd) |
112 | { | |
1fdb75e1 UH |
113 | #ifdef _WIN32 |
114 | /* Returns non-zero upon success, 0 upon failure. */ | |
2119ab03 | 115 | return (PurgeComm(hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0) ? -1 : 0; |
1fdb75e1 UH |
116 | #else |
117 | /* Returns 0 upon success, -1 upon failure. */ | |
f0551a65 | 118 | return tcflush(fd, TCIOFLUSH); |
1fdb75e1 | 119 | #endif |
06d64eb8 BV |
120 | } |
121 | ||
2119ab03 UH |
122 | /* |
123 | * Write a number of bytes to the specified serial port. | |
124 | * Returns the number of bytes written, or -1 upon failure. | |
125 | */ | |
126 | int serial_write(int fd, const void *buf, size_t count) | |
127 | { | |
128 | #ifdef _WIN32 | |
129 | DWORD tmp = 0; | |
130 | ||
131 | /* FIXME */ | |
132 | /* Returns non-zero upon success, 0 upon failure. */ | |
133 | WriteFile(hdl, buf, count, &tmp, NULL); | |
134 | #else | |
135 | /* Returns the number of bytes written, or -1 upon failure. */ | |
136 | return write(fd, buf, count); | |
137 | #endif | |
138 | } | |
139 | ||
140 | /* | |
141 | * Read a number of bytes from the specified serial port. | |
142 | * Returns the number of bytes read, or -1 upon failure. | |
143 | */ | |
144 | int serial_read(int fd, void *buf, size_t count) | |
145 | { | |
146 | #ifdef _WIN32 | |
147 | DWORD tmp = 0; | |
148 | ||
149 | /* FIXME */ | |
150 | /* Returns non-zero upon success, 0 upon failure. */ | |
151 | return ReadFile(hdl, buf, count, &tmp, NULL); | |
152 | #else | |
153 | /* Returns the number of bytes read, or -1 upon failure. */ | |
154 | return read(fd, buf, count); | |
155 | #endif | |
156 | } | |
157 | ||
d02a535e BV |
158 | void *serial_backup_params(int fd) |
159 | { | |
926b866c UH |
160 | #ifdef _WIN32 |
161 | /* TODO */ | |
162 | #else | |
d02a535e BV |
163 | struct termios *term; |
164 | ||
165 | term = malloc(sizeof(struct termios)); | |
166 | tcgetattr(fd, term); | |
167 | ||
168 | return term; | |
926b866c | 169 | #endif |
d02a535e BV |
170 | } |
171 | ||
d02a535e BV |
172 | void serial_restore_params(int fd, void *backup) |
173 | { | |
926b866c UH |
174 | #ifdef _WIN32 |
175 | /* TODO */ | |
176 | #else | |
986f7270 | 177 | tcsetattr(fd, TCSADRAIN, (struct termios *)backup); |
926b866c | 178 | #endif |
d02a535e BV |
179 | } |
180 | ||
1ff7712c | 181 | /* |
2119ab03 UH |
182 | * Set serial parameters. |
183 | * | |
184 | * flowcontrol: 1 = rts/cts, 2 = xon/xoff | |
185 | * parity: 0 = none, 1 = even, 2 = odd | |
1ff7712c | 186 | */ |
986f7270 UH |
187 | int serial_set_params(int fd, int speed, int bits, int parity, int stopbits, |
188 | int flowcontrol) | |
d02a535e | 189 | { |
926b866c UH |
190 | #ifdef _WIN32 |
191 | DCB dcb; | |
192 | ||
193 | if (!GetCommState(hdl, &dcb)) { | |
194 | /* TODO: Error handling. */ | |
2119ab03 | 195 | return SIGROK_ERR; |
926b866c UH |
196 | } |
197 | ||
198 | /* TODO: Rename 'speed' to 'baudrate'. */ | |
199 | switch(speed) { | |
1fdb75e1 | 200 | /* TODO: Support for higher baud rates. */ |
926b866c UH |
201 | case 115200: |
202 | dcb.BaudRate = CBR_115200; | |
203 | break; | |
204 | case 57600: | |
205 | dcb.BaudRate = CBR_57600; | |
206 | break; | |
207 | case 38400: | |
208 | dcb.BaudRate = CBR_38400; | |
209 | break; | |
210 | case 19200: | |
211 | dcb.BaudRate = CBR_19200; | |
212 | break; | |
213 | case 9600: | |
214 | dcb.BaudRate = CBR_9600; | |
215 | break; | |
216 | default: | |
217 | /* TODO: Error handling. */ | |
218 | break; | |
219 | } | |
220 | dcb.ByteSize = bits; | |
221 | dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */ | |
222 | dcb.StopBits = ONESTOPBIT; /* TODO: Don't hardcode. */ | |
223 | ||
224 | if (!SetCommState(hdl, &dcb)) { | |
225 | /* TODO: Error handling. */ | |
2119ab03 | 226 | return SIGROK_ERR; |
926b866c UH |
227 | } |
228 | #else | |
d02a535e | 229 | struct termios term; |
1ff7712c | 230 | speed_t baud; |
d02a535e | 231 | |
1ff7712c DR |
232 | switch (speed) { |
233 | case 9600: | |
234 | baud = B9600; | |
235 | break; | |
236 | case 38400: | |
237 | baud = B38400; | |
238 | break; | |
239 | case 57600: | |
240 | baud = B57600; | |
241 | break; | |
242 | case 115200: | |
243 | baud = B115200; | |
244 | break; | |
245 | case 460800: | |
246 | baud = B460800; | |
247 | break; | |
248 | default: | |
d02a535e | 249 | return SIGROK_ERR; |
1ff7712c | 250 | } |
d02a535e | 251 | |
986f7270 | 252 | if (tcgetattr(fd, &term) < 0) |
d02a535e | 253 | return SIGROK_ERR; |
1ff7712c | 254 | if (cfsetispeed(&term, baud) < 0) |
d02a535e | 255 | return SIGROK_ERR; |
1ff7712c | 256 | |
d02a535e | 257 | term.c_cflag &= ~CSIZE; |
1ff7712c DR |
258 | switch (bits) { |
259 | case 8: | |
260 | term.c_cflag |= CS8; | |
261 | break; | |
262 | case 7: | |
263 | term.c_cflag |= CS7; | |
264 | break; | |
265 | default: | |
266 | return SIGROK_ERR; | |
267 | } | |
268 | ||
d02a535e | 269 | term.c_cflag &= ~CSTOPB; |
1ff7712c DR |
270 | switch (stopbits) { |
271 | case 1: | |
272 | break; | |
273 | case 2: | |
274 | term.c_cflag |= CSTOPB; | |
275 | default: | |
276 | return SIGROK_ERR; | |
277 | } | |
278 | ||
279 | term.c_cflag &= ~(IXON | IXOFF | CRTSCTS); | |
280 | switch (flowcontrol) { | |
281 | case 2: | |
282 | term.c_cflag |= IXON | IXOFF; | |
283 | break; | |
284 | case 1: | |
285 | term.c_cflag |= CRTSCTS; | |
286 | default: | |
287 | return SIGROK_ERR; | |
288 | } | |
289 | ||
ac4a2ea4 DR |
290 | term.c_iflag &= ~IGNPAR; |
291 | term.c_cflag &= ~(PARODD | PARENB); | |
1ff7712c DR |
292 | switch (parity) { |
293 | case 0: | |
294 | term.c_iflag |= IGNPAR; | |
295 | break; | |
296 | case 1: | |
ac4a2ea4 | 297 | term.c_cflag |= PARENB; |
1ff7712c DR |
298 | break; |
299 | case 2: | |
ac4a2ea4 | 300 | term.c_cflag |= PARENB | PARODD; |
1ff7712c DR |
301 | break; |
302 | default: | |
303 | return SIGROK_ERR; | |
304 | } | |
305 | ||
986f7270 | 306 | if (tcsetattr(fd, TCSADRAIN, &term) < 0) |
d02a535e | 307 | return SIGROK_ERR; |
926b866c | 308 | #endif |
d02a535e BV |
309 | |
310 | return SIGROK_OK; | |
311 | } |