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