]>
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 UH |
62 | for (i = 0; serial_port_glob[i]; i++) { |
63 | if (!glob(serial_port_glob[i], 0, NULL, &g)) { | |
64 | for (j = 0; j < g.gl_pathc; j++) | |
65 | ports = g_slist_append(ports, | |
66 | strdup(g.gl_pathv[j])); | |
a1bb33af UH |
67 | globfree(&g); |
68 | } | |
69 | } | |
70 | ||
71 | return ports; | |
926b866c | 72 | #endif |
a1bb33af UH |
73 | } |
74 | ||
d02a535e BV |
75 | int serial_open(const char *pathname, int flags) |
76 | { | |
926b866c UH |
77 | #ifdef _WIN32 |
78 | /* FIXME: Don't hardcode COM1. */ | |
79 | hdl = CreateFile("COM1", GENERIC_READ | GENERIC_WRITE, 0, 0, | |
80 | OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); | |
81 | if (hdl == INVALID_HANDLE_VALUE) { | |
82 | /* TODO: Error handling. */ | |
83 | } | |
84 | return 0; | |
85 | #else | |
d02a535e | 86 | return open(pathname, flags); |
926b866c | 87 | #endif |
d02a535e BV |
88 | } |
89 | ||
d02a535e BV |
90 | int serial_close(int fd) |
91 | { | |
926b866c UH |
92 | #ifdef _WIN32 |
93 | CloseHandle(hdl); | |
94 | #else | |
d02a535e | 95 | return close(fd); |
926b866c | 96 | #endif |
d02a535e BV |
97 | } |
98 | ||
d02a535e BV |
99 | void *serial_backup_params(int fd) |
100 | { | |
926b866c UH |
101 | #ifdef _WIN32 |
102 | /* TODO */ | |
103 | #else | |
d02a535e BV |
104 | struct termios *term; |
105 | ||
106 | term = malloc(sizeof(struct termios)); | |
107 | tcgetattr(fd, term); | |
108 | ||
109 | return term; | |
926b866c | 110 | #endif |
d02a535e BV |
111 | } |
112 | ||
d02a535e BV |
113 | void serial_restore_params(int fd, void *backup) |
114 | { | |
926b866c UH |
115 | #ifdef _WIN32 |
116 | /* TODO */ | |
117 | #else | |
986f7270 | 118 | tcsetattr(fd, TCSADRAIN, (struct termios *)backup); |
926b866c | 119 | #endif |
d02a535e BV |
120 | } |
121 | ||
d02a535e | 122 | /* flowcontrol 1 = rts/cts 2 = xon/xoff */ |
986f7270 UH |
123 | int serial_set_params(int fd, int speed, int bits, int parity, int stopbits, |
124 | int flowcontrol) | |
d02a535e | 125 | { |
926b866c UH |
126 | #ifdef _WIN32 |
127 | DCB dcb; | |
128 | ||
129 | if (!GetCommState(hdl, &dcb)) { | |
130 | /* TODO: Error handling. */ | |
131 | } | |
132 | ||
133 | /* TODO: Rename 'speed' to 'baudrate'. */ | |
134 | switch(speed) { | |
135 | case 115200: | |
136 | dcb.BaudRate = CBR_115200; | |
137 | break; | |
138 | case 57600: | |
139 | dcb.BaudRate = CBR_57600; | |
140 | break; | |
141 | case 38400: | |
142 | dcb.BaudRate = CBR_38400; | |
143 | break; | |
144 | case 19200: | |
145 | dcb.BaudRate = CBR_19200; | |
146 | break; | |
147 | case 9600: | |
148 | dcb.BaudRate = CBR_9600; | |
149 | break; | |
150 | default: | |
151 | /* TODO: Error handling. */ | |
152 | break; | |
153 | } | |
154 | dcb.ByteSize = bits; | |
155 | dcb.Parity = NOPARITY; /* TODO: Don't hardcode. */ | |
156 | dcb.StopBits = ONESTOPBIT; /* TODO: Don't hardcode. */ | |
157 | ||
158 | if (!SetCommState(hdl, &dcb)) { | |
159 | /* TODO: Error handling. */ | |
160 | } | |
161 | #else | |
d02a535e BV |
162 | struct termios term; |
163 | ||
986f7270 UH |
164 | /* Only supporting what we need really, currently the OLS driver. */ |
165 | if (speed != 115200 || bits != 8 || parity != 0 || stopbits != 1 | |
166 | || flowcontrol != 2) | |
d02a535e BV |
167 | return SIGROK_ERR; |
168 | ||
986f7270 | 169 | if (tcgetattr(fd, &term) < 0) |
d02a535e | 170 | return SIGROK_ERR; |
986f7270 | 171 | if (cfsetispeed(&term, B115200) < 0) |
d02a535e BV |
172 | return SIGROK_ERR; |
173 | term.c_cflag &= ~CSIZE; | |
174 | term.c_cflag |= CS8; | |
175 | term.c_cflag &= ~CSTOPB; | |
176 | term.c_cflag |= IXON | IXOFF; | |
177 | term.c_iflag |= IGNPAR; | |
986f7270 | 178 | if (tcsetattr(fd, TCSADRAIN, &term) < 0) |
d02a535e | 179 | return SIGROK_ERR; |
926b866c | 180 | #endif |
d02a535e BV |
181 | |
182 | return SIGROK_OK; | |
183 | } |