]> sigrok.org Git - libsigrok.git/blame - hardware/common/serial.c
add serial_flush()
[libsigrok.git] / hardware / common / serial.c
CommitLineData
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
37HANDLE hdl;
38#endif
39
a1bb33af
UH
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-*",
986f7270 49 NULL,
a1bb33af
UH
50};
51
a1bb33af
UH
52GSList *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
74int 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
89int 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
98int serial_flush(int fd)
99{
100
101 tcflush(fd, TCIOFLUSH);
102
103}
104
d02a535e
BV
105void *serial_backup_params(int fd)
106{
926b866c
UH
107#ifdef _WIN32
108 /* TODO */
109#else
d02a535e
BV
110 struct termios *term;
111
112 term = malloc(sizeof(struct termios));
113 tcgetattr(fd, term);
114
115 return term;
926b866c 116#endif
d02a535e
BV
117}
118
d02a535e
BV
119void serial_restore_params(int fd, void *backup)
120{
926b866c
UH
121#ifdef _WIN32
122 /* TODO */
123#else
986f7270 124 tcsetattr(fd, TCSADRAIN, (struct termios *)backup);
926b866c 125#endif
d02a535e
BV
126}
127
d02a535e 128/* flowcontrol 1 = rts/cts 2 = xon/xoff */
986f7270
UH
129int serial_set_params(int fd, int speed, int bits, int parity, int stopbits,
130 int flowcontrol)
d02a535e 131{
926b866c
UH
132#ifdef _WIN32
133 DCB dcb;
134
135 if (!GetCommState(hdl, &dcb)) {
136 /* TODO: Error handling. */
137 }
138
139 /* TODO: Rename 'speed' to 'baudrate'. */
140 switch(speed) {
141 case 115200:
142 dcb.BaudRate = CBR_115200;
143 break;
144 case 57600:
145 dcb.BaudRate = CBR_57600;
146 break;
147 case 38400:
148 dcb.BaudRate = CBR_38400;
149 break;
150 case 19200:
151 dcb.BaudRate = CBR_19200;
152 break;
153 case 9600:
154 dcb.BaudRate = CBR_9600;
155 break;
156 default:
157 /* TODO: Error handling. */
158 break;
159 }
160 dcb.ByteSize = bits;
161 dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */
162 dcb.StopBits = ONESTOPBIT; /* TODO: Don't hardcode. */
163
164 if (!SetCommState(hdl, &dcb)) {
165 /* TODO: Error handling. */
166 }
167#else
d02a535e
BV
168 struct termios term;
169
986f7270
UH
170 /* Only supporting what we need really, currently the OLS driver. */
171 if (speed != 115200 || bits != 8 || parity != 0 || stopbits != 1
172 || flowcontrol != 2)
d02a535e
BV
173 return SIGROK_ERR;
174
986f7270 175 if (tcgetattr(fd, &term) < 0)
d02a535e 176 return SIGROK_ERR;
986f7270 177 if (cfsetispeed(&term, B115200) < 0)
d02a535e
BV
178 return SIGROK_ERR;
179 term.c_cflag &= ~CSIZE;
180 term.c_cflag |= CS8;
181 term.c_cflag &= ~CSTOPB;
182 term.c_cflag |= IXON | IXOFF;
183 term.c_iflag |= IGNPAR;
986f7270 184 if (tcsetattr(fd, TCSADRAIN, &term) < 0)
d02a535e 185 return SIGROK_ERR;
926b866c 186#endif
d02a535e
BV
187
188 return SIGROK_OK;
189}