cleaning up uart_posix.c
[proxmark3-svn] / uart / uart_posix.c
1 /*
2 * Generic uart / rs232/ serial port library
3 *
4 * Copyright (c) 2013, Roel Verdult
5 * Copyright (c) 2018 Google
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 *
30 * @file uart_posix.c
31 *
32 * This version of the library has functionality removed which was not used by
33 * proxmark3 project.
34 */
35
36 // Test if we are dealing with posix operating systems
37 #ifndef _WIN32
38 #define _DEFAULT_SOURCE
39
40 #include "uart.h"
41
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <stdbool.h>
46 #include <termios.h>
47 #include <sys/ioctl.h>
48 #include <unistd.h>
49 #include <fcntl.h>
50 #include <sys/types.h>
51 #include <sys/stat.h>
52 #include <sys/socket.h>
53 #include <netinet/tcp.h>
54 #include <netdb.h>
55
56 // Fix missing definition on OS X.
57 // Taken from https://github.com/unbit/uwsgi/commit/b608eb1772641d525bfde268fe9d6d8d0d5efde7
58 #ifndef SOL_TCP
59 #define SOL_TCP IPPROTO_TCP
60 #endif
61
62 typedef struct termios term_info;
63 typedef struct {
64 int fd; // Serial port file descriptor
65 term_info tiOld; // Terminal info before using the port
66 term_info tiNew; // Terminal info during the transaction
67 } serial_port_unix;
68
69 // Set time-out on 30 miliseconds
70 static struct timeval timeout = {
71 .tv_sec = 0, // 0 second
72 .tv_usec = 30000 // 30000 micro seconds
73 };
74
75
76 void uart_close(const serial_port sp) {
77 serial_port_unix* spu = (serial_port_unix*)sp;
78 tcflush(spu->fd,TCIOFLUSH);
79 tcsetattr(spu->fd,TCSANOW,&(spu->tiOld));
80 struct flock fl;
81 fl.l_type = F_UNLCK;
82 fl.l_whence = SEEK_SET;
83 fl.l_start = 0;
84 fl.l_len = 0;
85 fl.l_pid = getpid();
86 fcntl(spu->fd, F_SETLK, &fl);
87 close(spu->fd);
88 free(sp);
89 }
90
91
92 serial_port uart_open(const char* pcPortName) {
93
94 serial_port_unix* sp = malloc(sizeof(serial_port_unix));
95 if (sp == 0) return INVALID_SERIAL_PORT;
96
97 if (memcmp(pcPortName, "tcp:", 4) == 0) {
98 struct addrinfo *addr = NULL, *rp;
99 char *addrstr = strdup(pcPortName + 4);
100 if (addrstr == NULL) {
101 printf("Error: strdup\n");
102 return INVALID_SERIAL_PORT;
103 }
104 char *colon = strrchr(addrstr, ':');
105 char *portstr;
106
107 // Set time-out to 300 milliseconds only for TCP port
108 timeout.tv_usec = 300000;
109
110 if (colon) {
111 portstr = colon + 1;
112 *colon = '\0';
113 } else {
114 portstr = "7901";
115 }
116
117 struct addrinfo info;
118
119 memset(&info, 0, sizeof(info));
120
121 info.ai_socktype = SOCK_STREAM;
122
123 int s = getaddrinfo(addrstr, portstr, &info, &addr);
124 if (s != 0) {
125 printf("Error: getaddrinfo: %s\n", gai_strerror(s));
126 return INVALID_SERIAL_PORT;
127 }
128
129 int sfd;
130 for (rp = addr; rp != NULL; rp = rp->ai_next) {
131 sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
132 if (sfd == -1)
133 continue;
134 if (connect(sfd, rp->ai_addr, rp->ai_addrlen) != -1)
135 break;
136 close(sfd);
137 }
138
139 if (rp == NULL) { /* No address succeeded */
140 printf("Error: Could not connect\n");
141 return INVALID_SERIAL_PORT;
142 }
143
144 freeaddrinfo(addr);
145 free(addrstr);
146
147 sp->fd = sfd;
148
149 int one = 1;
150 setsockopt(sp->fd, SOL_TCP, TCP_NODELAY, &one, sizeof(one));
151 return sp;
152 }
153
154 sp->fd = open(pcPortName, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
155 if (sp->fd == -1) {
156 uart_close(sp);
157 return INVALID_SERIAL_PORT;
158 }
159
160 // Finally figured out a way to claim a serial port interface under unix
161 // We just try to set a (advisory) lock on the file descriptor
162 struct flock fl;
163 fl.l_type = F_WRLCK;
164 fl.l_whence = SEEK_SET;
165 fl.l_start = 0;
166 fl.l_len = 0;
167 fl.l_pid = getpid();
168
169 // Does the system allows us to place a lock on this file descriptor
170 if (fcntl(sp->fd, F_SETLK, &fl) == -1) {
171 // A conflicting lock is held by another process
172 free(sp);
173 return CLAIMED_SERIAL_PORT;
174 }
175
176 // Try to retrieve the old (current) terminal info struct
177 if(tcgetattr(sp->fd,&sp->tiOld) == -1) {
178 uart_close(sp);
179 return INVALID_SERIAL_PORT;
180 }
181
182 // Duplicate the (old) terminal info struct
183 sp->tiNew = sp->tiOld;
184
185 // Configure the serial port
186 sp->tiNew.c_cflag = CS8 | CLOCAL | CREAD;
187 sp->tiNew.c_iflag = IGNPAR;
188 sp->tiNew.c_oflag = 0;
189 sp->tiNew.c_lflag = 0;
190
191 // Block until n bytes are received
192 sp->tiNew.c_cc[VMIN] = 0;
193 // Block until a timer expires (n * 100 mSec.)
194 sp->tiNew.c_cc[VTIME] = 0;
195
196 // Try to set the new terminal info struct
197 if(tcsetattr(sp->fd,TCSANOW,&sp->tiNew) == -1) {
198 uart_close(sp);
199 return INVALID_SERIAL_PORT;
200 }
201
202 // Flush all lingering data that may exist
203 tcflush(sp->fd, TCIOFLUSH);
204
205 return sp;
206 }
207
208
209 bool uart_receive(const serial_port sp, uint8_t* pbtRx, size_t pszMaxRxLen, size_t* pszRxLen) {
210 int byteCount;
211 fd_set rfds;
212 struct timeval tv;
213
214 // Reset the output count
215 *pszRxLen = 0;
216
217 do {
218 // Reset file descriptor
219 FD_ZERO(&rfds);
220 FD_SET(((serial_port_unix*)sp)->fd,&rfds);
221 tv = timeout;
222 int res = select(((serial_port_unix*)sp)->fd+1, &rfds, NULL, NULL, &tv);
223
224 // Read error
225 if (res < 0) {
226 return false;
227 }
228
229 // Read time-out
230 if (res == 0) {
231 if (*pszRxLen == 0) {
232 // Error, we received no data
233 return false;
234 } else {
235 // We received some data, but nothing more is available
236 return true;
237 }
238 }
239
240 // Retrieve the count of the incoming bytes
241 res = ioctl(((serial_port_unix*)sp)->fd, FIONREAD, &byteCount);
242 if (res < 0) return false;
243
244 // Cap the number of bytes, so we don't overrun the buffer
245 if (pszMaxRxLen - (*pszRxLen) < byteCount) {
246 byteCount = pszMaxRxLen - (*pszRxLen);
247 }
248
249 // There is something available, read the data
250 res = read(((serial_port_unix*)sp)->fd, pbtRx+(*pszRxLen), byteCount);
251
252 // Stop if the OS has some troubles reading the data
253 if (res <= 0) return false;
254
255 *pszRxLen += res;
256
257 if (*pszRxLen == pszMaxRxLen) {
258 // We have all the data we wanted.
259 return true;
260 }
261
262 } while (byteCount);
263
264 return true;
265 }
266
267
268 bool uart_send(const serial_port sp, const uint8_t* pbtTx, const size_t szTxLen) {
269 size_t szPos = 0;
270 fd_set rfds;
271 struct timeval tv;
272
273 while (szPos < szTxLen) {
274 // Reset file descriptor
275 FD_ZERO(&rfds);
276 FD_SET(((serial_port_unix*)sp)->fd,&rfds);
277 tv = timeout;
278 int res = select(((serial_port_unix*)sp)->fd+1, NULL, &rfds, NULL, &tv);
279
280 // Write error
281 if (res < 0) {
282 return false;
283 }
284
285 // Write time-out
286 if (res == 0) {
287 return false;
288 }
289
290 // Send away the bytes
291 res = write(((serial_port_unix*)sp)->fd, pbtTx+szPos, szTxLen-szPos);
292
293 // Stop if the OS has some troubles sending the data
294 if (res <= 0) return false;
295
296 szPos += res;
297 }
298 return true;
299 }
300
301
302 bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) {
303 const serial_port_unix* spu = (serial_port_unix*)sp;
304 speed_t stPortSpeed;
305 switch (uiPortSpeed) {
306 case 0: stPortSpeed = B0; break;
307 case 50: stPortSpeed = B50; break;
308 case 75: stPortSpeed = B75; break;
309 case 110: stPortSpeed = B110; break;
310 case 134: stPortSpeed = B134; break;
311 case 150: stPortSpeed = B150; break;
312 case 300: stPortSpeed = B300; break;
313 case 600: stPortSpeed = B600; break;
314 case 1200: stPortSpeed = B1200; break;
315 case 1800: stPortSpeed = B1800; break;
316 case 2400: stPortSpeed = B2400; break;
317 case 4800: stPortSpeed = B4800; break;
318 case 9600: stPortSpeed = B9600; break;
319 case 19200: stPortSpeed = B19200; break;
320 case 38400: stPortSpeed = B38400; break;
321 # ifdef B57600
322 case 57600: stPortSpeed = B57600; break;
323 # endif
324 # ifdef B115200
325 case 115200: stPortSpeed = B115200; break;
326 # endif
327 # ifdef B230400
328 case 230400: stPortSpeed = B230400; break;
329 # endif
330 # ifdef B460800
331 case 460800: stPortSpeed = B460800; break;
332 # endif
333 # ifdef B921600
334 case 921600: stPortSpeed = B921600; break;
335 # endif
336 default: return false;
337 };
338 struct termios ti;
339 if (tcgetattr(spu->fd, &ti) == -1) return false;
340 // Set port speed (Input and Output)
341 cfsetispeed(&ti,stPortSpeed);
342 cfsetospeed(&ti,stPortSpeed);
343 return (tcsetattr(spu->fd, TCSANOW, &ti) != -1);
344 }
345
346 uint32_t uart_get_speed(const serial_port sp) {
347 struct termios ti;
348 uint32_t uiPortSpeed;
349 const serial_port_unix* spu = (serial_port_unix*)sp;
350 if (tcgetattr(spu->fd, &ti) == -1) return 0;
351 // Set port speed (Input)
352 speed_t stPortSpeed = cfgetispeed(&ti);
353 switch (stPortSpeed) {
354 case B0: uiPortSpeed = 0; break;
355 case B50: uiPortSpeed = 50; break;
356 case B75: uiPortSpeed = 75; break;
357 case B110: uiPortSpeed = 110; break;
358 case B134: uiPortSpeed = 134; break;
359 case B150: uiPortSpeed = 150; break;
360 case B300: uiPortSpeed = 300; break;
361 case B600: uiPortSpeed = 600; break;
362 case B1200: uiPortSpeed = 1200; break;
363 case B1800: uiPortSpeed = 1800; break;
364 case B2400: uiPortSpeed = 2400; break;
365 case B4800: uiPortSpeed = 4800; break;
366 case B9600: uiPortSpeed = 9600; break;
367 case B19200: uiPortSpeed = 19200; break;
368 case B38400: uiPortSpeed = 38400; break;
369 # ifdef B57600
370 case B57600: uiPortSpeed = 57600; break;
371 # endif
372 # ifdef B115200
373 case B115200: uiPortSpeed = 115200; break;
374 # endif
375 # ifdef B230400
376 case B230400: uiPortSpeed = 230400; break;
377 # endif
378 # ifdef B460800
379 case B460800: uiPortSpeed = 460800; break;
380 # endif
381 # ifdef B921600
382 case B921600: uiPortSpeed = 921600; break;
383 # endif
384 default: return 0;
385 };
386 return uiPortSpeed;
387 }
388
389 #endif
Impressum, Datenschutz