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