]> sigrok.org Git - libsigrok.git/blame_incremental - hardware/common/serial.c
add ontinuous mode for demo driver
[libsigrok.git] / hardware / common / serial.c
... / ...
CommitLineData
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
20#include <string.h>
21#include <sys/types.h>
22#include <sys/stat.h>
23#include <fcntl.h>
24#include <unistd.h>
25#ifdef _WIN32
26#include <conio.h>
27#else
28#include <glob.h>
29#include <termios.h>
30#endif
31#include <stdlib.h>
32#include <glib.h>
33#include <sigrok.h>
34
35// FIXME: Must be moved, or rather passed as function argument.
36#ifdef _WIN32
37HANDLE hdl;
38#endif
39
40char *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-*",
49 NULL,
50};
51
52GSList *list_serial_ports(void)
53{
54#ifdef _WIN32
55 /* TODO */
56#else
57 glob_t g;
58 GSList *ports;
59 unsigned int i, j;
60
61 ports = NULL;
62 for (i = 0; serial_port_glob[i]; i++) {
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);
68 }
69
70 return ports;
71#endif
72}
73
74int serial_open(const char *pathname, int flags)
75{
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
85 return open(pathname, flags);
86#endif
87}
88
89int serial_close(int fd)
90{
91#ifdef _WIN32
92 CloseHandle(hdl);
93#else
94 return close(fd);
95#endif
96}
97
98/*
99 * Flush serial port buffers (if any).
100 * Returns 0 upon success, -1 upon failure.
101 */
102int serial_flush(int fd)
103{
104#ifdef _WIN32
105 /* Returns non-zero upon success, 0 upon failure. */
106 if (PurgeComm(hdl, PURGE_RXCLEAR | PURGE_TXCLEAR) == 0)
107 return -1;
108 else
109 return 0;
110#else
111 /* Returns 0 upon success, -1 upon failure. */
112 return tcflush(fd, TCIOFLUSH);
113#endif
114}
115
116void *serial_backup_params(int fd)
117{
118#ifdef _WIN32
119 /* TODO */
120#else
121 struct termios *term;
122
123 term = malloc(sizeof(struct termios));
124 tcgetattr(fd, term);
125
126 return term;
127#endif
128}
129
130void serial_restore_params(int fd, void *backup)
131{
132#ifdef _WIN32
133 /* TODO */
134#else
135 tcsetattr(fd, TCSADRAIN, (struct termios *)backup);
136#endif
137}
138
139/*
140 * flowcontrol 1 = rts/cts 2 = xon/xoff
141 * parity 0 = none, 1 = even, 2 = odd
142 */
143int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
144 int flowcontrol)
145{
146#ifdef _WIN32
147 DCB dcb;
148
149 if (!GetCommState(hdl, &dcb)) {
150 /* TODO: Error handling. */
151 }
152
153 /* TODO: Rename 'speed' to 'baudrate'. */
154 switch(speed) {
155 /* TODO: Support for higher baud rates. */
156 case 115200:
157 dcb.BaudRate = CBR_115200;
158 break;
159 case 57600:
160 dcb.BaudRate = CBR_57600;
161 break;
162 case 38400:
163 dcb.BaudRate = CBR_38400;
164 break;
165 case 19200:
166 dcb.BaudRate = CBR_19200;
167 break;
168 case 9600:
169 dcb.BaudRate = CBR_9600;
170 break;
171 default:
172 /* TODO: Error handling. */
173 break;
174 }
175 dcb.ByteSize = bits;
176 dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */
177 dcb.StopBits = ONESTOPBIT; /* TODO: Don't hardcode. */
178
179 if (!SetCommState(hdl, &dcb)) {
180 /* TODO: Error handling. */
181 }
182#else
183 struct termios term;
184 speed_t baud;
185
186 switch (speed) {
187 case 9600:
188 baud = B9600;
189 break;
190 case 38400:
191 baud = B38400;
192 break;
193 case 57600:
194 baud = B57600;
195 break;
196 case 115200:
197 baud = B115200;
198 break;
199 case 460800:
200 baud = B460800;
201 break;
202 default:
203 return SIGROK_ERR;
204 }
205
206 if (tcgetattr(fd, &term) < 0)
207 return SIGROK_ERR;
208 if (cfsetispeed(&term, baud) < 0)
209 return SIGROK_ERR;
210
211 term.c_cflag &= ~CSIZE;
212 switch (bits) {
213 case 8:
214 term.c_cflag |= CS8;
215 break;
216 case 7:
217 term.c_cflag |= CS7;
218 break;
219 default:
220 return SIGROK_ERR;
221 }
222
223 term.c_cflag &= ~CSTOPB;
224 switch (stopbits) {
225 case 1:
226 break;
227 case 2:
228 term.c_cflag |= CSTOPB;
229 default:
230 return SIGROK_ERR;
231 }
232
233 term.c_cflag &= ~(IXON | IXOFF | CRTSCTS);
234 switch (flowcontrol) {
235 case 2:
236 term.c_cflag |= IXON | IXOFF;
237 break;
238 case 1:
239 term.c_cflag |= CRTSCTS;
240 default:
241 return SIGROK_ERR;
242 }
243
244 term.c_iflag &= ~IGNPAR;
245 term.c_cflag &= ~(PARODD | PARENB);
246 switch (parity) {
247 case 0:
248 term.c_iflag |= IGNPAR;
249 break;
250 case 1:
251 term.c_cflag |= PARENB;
252 break;
253 case 2:
254 term.c_cflag |= PARENB | PARODD;
255 break;
256 default:
257 return SIGROK_ERR;
258 }
259
260 if (tcsetattr(fd, TCSADRAIN, &term) < 0)
261 return SIGROK_ERR;
262#endif
263
264 return SIGROK_OK;
265}