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