]>
Commit | Line | Data |
---|---|---|
fd667521 | 1 | /*\r |
2 | * Generic uart / rs232/ serial port library\r | |
3 | *\r | |
4 | * Copyright (c) 2013, Roel Verdult\r | |
5 | * Copyright (c) 2018 Google\r | |
6 | * All rights reserved.\r | |
7 | *\r | |
8 | * Redistribution and use in source and binary forms, with or without\r | |
9 | * modification, are permitted provided that the following conditions are met:\r | |
10 | * 1. Redistributions of source code must retain the above copyright\r | |
11 | * notice, this list of conditions and the following disclaimer.\r | |
12 | * 2. Redistributions in binary form must reproduce the above copyright\r | |
13 | * notice, this list of conditions and the following disclaimer in the\r | |
14 | * documentation and/or other materials provided with the distribution.\r | |
15 | * 3. Neither the name of the copyright holders nor the\r | |
16 | * names of its contributors may be used to endorse or promote products\r | |
17 | * derived from this software without specific prior written permission.\r | |
18 | *\r | |
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY\r | |
20 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED\r | |
21 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r | |
22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY\r | |
23 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r | |
24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\r | |
25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND\r | |
26 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\r | |
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS\r | |
28 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r | |
29 | *\r | |
30 | * @file uart_posix.c\r | |
31 | *\r | |
32 | * This version of the library has functionality removed which was not used by\r | |
33 | * proxmark3 project.\r | |
34 | */\r | |
35 | \r | |
36 | // Test if we are dealing with posix operating systems\r | |
37 | #ifndef _WIN32\r | |
38 | #define _DEFAULT_SOURCE\r | |
39 | \r | |
40 | #include "uart.h"\r | |
41 | \r | |
42 | #include <stdlib.h>\r | |
43 | #include <stdio.h>\r | |
44 | #include <string.h>\r | |
45 | #include <stdbool.h>\r | |
46 | #include <termios.h>\r | |
47 | #include <sys/ioctl.h>\r | |
48 | #include <unistd.h>\r | |
49 | #include <fcntl.h>\r | |
50 | #include <sys/types.h>\r | |
51 | #include <sys/stat.h>\r | |
52 | #include <sys/socket.h>\r | |
53 | #include <netinet/tcp.h>\r | |
54 | #include <netdb.h>\r | |
d2ca5dbf | 55 | #include <sys/time.h>\r |
56 | #include <errno.h>\r | |
fd667521 | 57 | \r |
58 | // Fix missing definition on OS X.\r | |
59 | // Taken from https://github.com/unbit/uwsgi/commit/b608eb1772641d525bfde268fe9d6d8d0d5efde7\r | |
60 | #ifndef SOL_TCP\r | |
61 | #define SOL_TCP IPPROTO_TCP\r | |
62 | #endif\r | |
63 | \r | |
64 | typedef struct termios term_info;\r | |
65 | typedef struct {\r | |
66 | int fd; // Serial port file descriptor\r | |
67 | term_info tiOld; // Terminal info before using the port\r | |
68 | term_info tiNew; // Terminal info during the transaction\r | |
69 | } serial_port_unix;\r | |
70 | \r | |
71 | // Set time-out on 30 miliseconds\r | |
72 | static struct timeval timeout = {\r | |
73 | .tv_sec = 0, // 0 second\r | |
74 | .tv_usec = 30000 // 30000 micro seconds\r | |
75 | };\r | |
76 | \r | |
77 | \r | |
78 | void uart_close(const serial_port sp) {\r | |
79 | serial_port_unix* spu = (serial_port_unix*)sp;\r | |
d2ca5dbf | 80 | tcflush(spu->fd, TCIOFLUSH);\r |
81 | tcsetattr(spu->fd, TCSANOW, &(spu->tiOld));\r | |
fd667521 | 82 | struct flock fl;\r |
83 | fl.l_type = F_UNLCK;\r | |
84 | fl.l_whence = SEEK_SET;\r | |
85 | fl.l_start = 0;\r | |
86 | fl.l_len = 0;\r | |
87 | fl.l_pid = getpid();\r | |
88 | fcntl(spu->fd, F_SETLK, &fl);\r | |
89 | close(spu->fd);\r | |
90 | free(sp);\r | |
91 | }\r | |
92 | \r | |
93 | \r | |
94 | serial_port uart_open(const char* pcPortName) {\r | |
95 | \r | |
96 | serial_port_unix* sp = malloc(sizeof(serial_port_unix));\r | |
97 | if (sp == 0) return INVALID_SERIAL_PORT;\r | |
98 | \r | |
99 | if (memcmp(pcPortName, "tcp:", 4) == 0) {\r | |
100 | struct addrinfo *addr = NULL, *rp;\r | |
101 | char *addrstr = strdup(pcPortName + 4);\r | |
102 | if (addrstr == NULL) {\r | |
103 | printf("Error: strdup\n");\r | |
104 | return INVALID_SERIAL_PORT;\r | |
105 | }\r | |
106 | char *colon = strrchr(addrstr, ':');\r | |
107 | char *portstr;\r | |
108 | \r | |
109 | // Set time-out to 300 milliseconds only for TCP port\r | |
110 | timeout.tv_usec = 300000;\r | |
111 | \r | |
112 | if (colon) {\r | |
113 | portstr = colon + 1;\r | |
114 | *colon = '\0';\r | |
115 | } else {\r | |
116 | portstr = "7901";\r | |
117 | }\r | |
118 | \r | |
119 | struct addrinfo info;\r | |
120 | \r | |
121 | memset(&info, 0, sizeof(info));\r | |
122 | \r | |
123 | info.ai_socktype = SOCK_STREAM;\r | |
124 | \r | |
125 | int s = getaddrinfo(addrstr, portstr, &info, &addr);\r | |
126 | if (s != 0) {\r | |
127 | printf("Error: getaddrinfo: %s\n", gai_strerror(s));\r | |
128 | return INVALID_SERIAL_PORT;\r | |
129 | }\r | |
130 | \r | |
131 | int sfd;\r | |
132 | for (rp = addr; rp != NULL; rp = rp->ai_next) {\r | |
133 | sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);\r | |
134 | if (sfd == -1)\r | |
135 | continue;\r | |
136 | if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)\r | |
137 | break;\r | |
138 | close(sfd);\r | |
139 | }\r | |
140 | \r | |
141 | if (rp == NULL) { /* No address succeeded */\r | |
142 | printf("Error: Could not connect\n");\r | |
143 | return INVALID_SERIAL_PORT;\r | |
144 | }\r | |
145 | \r | |
146 | freeaddrinfo(addr);\r | |
147 | free(addrstr);\r | |
148 | \r | |
149 | sp->fd = sfd;\r | |
150 | \r | |
151 | int one = 1;\r | |
152 | setsockopt(sp->fd, SOL_TCP, TCP_NODELAY, &one, sizeof(one));\r | |
153 | return sp;\r | |
154 | }\r | |
155 | \r | |
156 | sp->fd = open(pcPortName, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);\r | |
157 | if (sp->fd == -1) {\r | |
158 | uart_close(sp);\r | |
159 | return INVALID_SERIAL_PORT;\r | |
160 | }\r | |
161 | \r | |
162 | // Finally figured out a way to claim a serial port interface under unix\r | |
163 | // We just try to set a (advisory) lock on the file descriptor\r | |
164 | struct flock fl;\r | |
165 | fl.l_type = F_WRLCK;\r | |
166 | fl.l_whence = SEEK_SET;\r | |
167 | fl.l_start = 0;\r | |
168 | fl.l_len = 0;\r | |
169 | fl.l_pid = getpid();\r | |
170 | \r | |
171 | // Does the system allows us to place a lock on this file descriptor\r | |
172 | if (fcntl(sp->fd, F_SETLK, &fl) == -1) {\r | |
173 | // A conflicting lock is held by another process\r | |
174 | free(sp);\r | |
175 | return CLAIMED_SERIAL_PORT;\r | |
176 | }\r | |
177 | \r | |
178 | // Try to retrieve the old (current) terminal info struct\r | |
d2ca5dbf | 179 | if (tcgetattr(sp->fd,&sp->tiOld) == -1) {\r |
fd667521 | 180 | uart_close(sp);\r |
181 | return INVALID_SERIAL_PORT;\r | |
182 | }\r | |
183 | \r | |
184 | // Duplicate the (old) terminal info struct\r | |
185 | sp->tiNew = sp->tiOld;\r | |
186 | \r | |
187 | // Configure the serial port\r | |
d2ca5dbf | 188 | sp->tiNew.c_cflag &= ~(CSIZE | PARENB);\r |
189 | sp->tiNew.c_cflag |= (CS8 | CLOCAL | CREAD);\r | |
190 | sp->tiNew.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);\r | |
191 | sp->tiNew.c_iflag |= IGNPAR;\r | |
192 | sp->tiNew.c_oflag &= ~OPOST;\r | |
193 | sp->tiNew.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);\r | |
fd667521 | 194 | \r |
fd667521 | 195 | \r |
196 | // Try to set the new terminal info struct\r | |
d2ca5dbf | 197 | if (tcsetattr(sp->fd, TCSANOW, &sp->tiNew) == -1) {\r |
fd667521 | 198 | uart_close(sp);\r |
199 | return INVALID_SERIAL_PORT;\r | |
200 | }\r | |
201 | \r | |
202 | // Flush all lingering data that may exist\r | |
203 | tcflush(sp->fd, TCIOFLUSH);\r | |
204 | \r | |
205 | return sp;\r | |
206 | }\r | |
207 | \r | |
208 | \r | |
d2ca5dbf | 209 | bool uart_receive(const serial_port sp, uint8_t* pbtRx, size_t szMaxRxLen, size_t* pszRxLen) {\r |
fd667521 | 210 | \r |
fd667521 | 211 | *pszRxLen = 0;\r |
212 | \r | |
d2ca5dbf | 213 | if (szMaxRxLen == 0) return true;\r |
fd667521 | 214 | \r |
d2ca5dbf | 215 | struct timeval t_current;\r |
216 | gettimeofday(&t_current, NULL);\r | |
217 | struct timeval t_end;\r | |
218 | timeradd(&t_current, &timeout, &t_end);\r | |
fd667521 | 219 | \r |
d2ca5dbf | 220 | while (true) {\r |
221 | int res = read(((serial_port_unix*)sp)->fd, pbtRx, szMaxRxLen - *pszRxLen);\r | |
222 | if (res < 0 && errno != EAGAIN && errno != EWOULDBLOCK) return false;\r | |
223 | if (res > 0) {\r | |
224 | *pszRxLen += res;\r | |
225 | pbtRx += res;\r | |
fd667521 | 226 | }\r |
d2ca5dbf | 227 | if (*pszRxLen == szMaxRxLen) return true; // we could read all requested bytes in time\r |
228 | gettimeofday(&t_current, NULL);\r | |
229 | if (timercmp(&t_current, &t_end, >)) return true; // timeout\r | |
230 | // set next select timeout\r | |
231 | struct timeval t_remains;\r | |
232 | timersub(&t_end, &t_current, &t_remains);\r | |
233 | // Set the file descriptor set\r | |
234 | fd_set rfds;\r | |
235 | FD_ZERO(&rfds);\r | |
236 | FD_SET(((serial_port_unix*)sp)->fd, &rfds);\r | |
237 | // wait for more bytes available\r | |
238 | res = select(((serial_port_unix*)sp)->fd+1, &rfds, NULL, NULL, &t_remains);\r | |
fd667521 | 239 | if (res < 0) return false;\r |
d2ca5dbf | 240 | if (res == 0) return true; // timeout\r |
241 | }\r | |
242 | return true; // should never come here\r | |
fd667521 | 243 | }\r |
244 | \r | |
245 | \r | |
246 | bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t szTxLen) {\r | |
fd667521 | 247 | \r |
d2ca5dbf | 248 | if (szTxLen == 0) return true;\r |
fd667521 | 249 | \r |
d2ca5dbf | 250 | size_t bytes_written = 0;\r |
fd667521 | 251 | \r |
d2ca5dbf | 252 | struct timeval t_current;\r |
253 | gettimeofday(&t_current, NULL);\r | |
254 | struct timeval t_end;\r | |
255 | timeradd(&t_current, &timeout, &t_end);\r | |
fd667521 | 256 | \r |
d2ca5dbf | 257 | while (true) {\r |
258 | int res = write(((serial_port_unix*)sp)->fd, pbtTx, szTxLen - bytes_written);\r | |
259 | if (res < 0 && res != EAGAIN && res != EWOULDBLOCK) return false;\r | |
260 | if (res > 0) {\r | |
261 | pbtTx += res;\r | |
262 | bytes_written += res;\r | |
263 | }\r | |
264 | if (bytes_written == szTxLen) return true; // we could write all bytes\r | |
265 | gettimeofday(&t_current, NULL);\r | |
266 | if (timercmp(&t_current, &t_end, >)) return false; // timeout\r | |
267 | // set next select timeout\r | |
268 | struct timeval t_remains;\r | |
269 | timersub(&t_end, &t_current, &t_remains);\r | |
270 | // Set the file descriptor set\r | |
271 | fd_set wfds;\r | |
272 | FD_ZERO(&wfds);\r | |
273 | FD_SET(((serial_port_unix*)sp)->fd, &wfds);\r | |
274 | // wait until more bytes can be written\r | |
275 | res = select(((serial_port_unix*)sp)->fd+1, NULL, &wfds, NULL, &t_remains);\r | |
276 | if (res < 0) return false; // error\r | |
277 | if (res == 0) return false; // timeout\r | |
fd667521 | 278 | }\r |
279 | return true;\r | |
280 | }\r | |
281 | \r | |
282 | \r | |
283 | bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) {\r | |
284 | const serial_port_unix* spu = (serial_port_unix*)sp;\r | |
285 | speed_t stPortSpeed;\r | |
286 | switch (uiPortSpeed) {\r | |
287 | case 0: stPortSpeed = B0; break;\r | |
288 | case 50: stPortSpeed = B50; break;\r | |
289 | case 75: stPortSpeed = B75; break;\r | |
290 | case 110: stPortSpeed = B110; break;\r | |
291 | case 134: stPortSpeed = B134; break;\r | |
292 | case 150: stPortSpeed = B150; break;\r | |
293 | case 300: stPortSpeed = B300; break;\r | |
294 | case 600: stPortSpeed = B600; break;\r | |
295 | case 1200: stPortSpeed = B1200; break;\r | |
296 | case 1800: stPortSpeed = B1800; break;\r | |
297 | case 2400: stPortSpeed = B2400; break;\r | |
298 | case 4800: stPortSpeed = B4800; break;\r | |
299 | case 9600: stPortSpeed = B9600; break;\r | |
300 | case 19200: stPortSpeed = B19200; break;\r | |
301 | case 38400: stPortSpeed = B38400; break;\r | |
302 | # ifdef B57600\r | |
303 | case 57600: stPortSpeed = B57600; break;\r | |
304 | # endif\r | |
305 | # ifdef B115200\r | |
306 | case 115200: stPortSpeed = B115200; break;\r | |
307 | # endif\r | |
308 | # ifdef B230400\r | |
309 | case 230400: stPortSpeed = B230400; break;\r | |
310 | # endif\r | |
311 | # ifdef B460800\r | |
312 | case 460800: stPortSpeed = B460800; break;\r | |
313 | # endif\r | |
314 | # ifdef B921600\r | |
315 | case 921600: stPortSpeed = B921600; break;\r | |
316 | # endif\r | |
317 | default: return false;\r | |
318 | };\r | |
319 | struct termios ti;\r | |
320 | if (tcgetattr(spu->fd, &ti) == -1) return false;\r | |
321 | // Set port speed (Input and Output)\r | |
322 | cfsetispeed(&ti,stPortSpeed);\r | |
323 | cfsetospeed(&ti,stPortSpeed);\r | |
324 | return (tcsetattr(spu->fd, TCSANOW, &ti) != -1);\r | |
325 | }\r | |
326 | \r | |
327 | uint32_t uart_get_speed(const serial_port sp) {\r | |
328 | struct termios ti;\r | |
329 | uint32_t uiPortSpeed;\r | |
330 | const serial_port_unix* spu = (serial_port_unix*)sp;\r | |
331 | if (tcgetattr(spu->fd, &ti) == -1) return 0;\r | |
332 | // Set port speed (Input)\r | |
333 | speed_t stPortSpeed = cfgetispeed(&ti);\r | |
334 | switch (stPortSpeed) {\r | |
335 | case B0: uiPortSpeed = 0; break;\r | |
336 | case B50: uiPortSpeed = 50; break;\r | |
337 | case B75: uiPortSpeed = 75; break;\r | |
338 | case B110: uiPortSpeed = 110; break;\r | |
339 | case B134: uiPortSpeed = 134; break;\r | |
340 | case B150: uiPortSpeed = 150; break;\r | |
341 | case B300: uiPortSpeed = 300; break;\r | |
342 | case B600: uiPortSpeed = 600; break;\r | |
343 | case B1200: uiPortSpeed = 1200; break;\r | |
344 | case B1800: uiPortSpeed = 1800; break;\r | |
345 | case B2400: uiPortSpeed = 2400; break;\r | |
346 | case B4800: uiPortSpeed = 4800; break;\r | |
347 | case B9600: uiPortSpeed = 9600; break;\r | |
348 | case B19200: uiPortSpeed = 19200; break;\r | |
349 | case B38400: uiPortSpeed = 38400; break;\r | |
350 | # ifdef B57600\r | |
351 | case B57600: uiPortSpeed = 57600; break;\r | |
352 | # endif\r | |
353 | # ifdef B115200\r | |
354 | case B115200: uiPortSpeed = 115200; break;\r | |
355 | # endif\r | |
356 | # ifdef B230400\r | |
357 | case B230400: uiPortSpeed = 230400; break;\r | |
358 | # endif\r | |
359 | # ifdef B460800\r | |
360 | case B460800: uiPortSpeed = 460800; break;\r | |
361 | # endif\r | |
362 | # ifdef B921600\r | |
363 | case B921600: uiPortSpeed = 921600; break;\r | |
364 | # endif\r | |
365 | default: return 0;\r | |
366 | };\r | |
367 | return uiPortSpeed;\r | |
368 | }\r | |
369 | \r | |
370 | #endif\r |