]>
cvs.zerfleddert.de Git - proxmark3-svn/blob - uart/uart_posix.c
2 * Generic uart / rs232/ serial port library
4 * Copyright (c) 2013, Roel Verdult
5 * Copyright (c) 2018 Google
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.
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.
32 * This version of the library has functionality removed which was not used by
36 // Test if we are dealing with posix operating systems
38 #define _DEFAULT_SOURCE
43 #include <sys/ioctl.h>
46 #include <sys/types.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>
58 // Fix missing definition on OS X.
59 // Taken from https://github.com/unbit/uwsgi/commit/b608eb1772641d525bfde268fe9d6d8d0d5efde7
61 #define SOL_TCP IPPROTO_TCP
64 typedef struct termios term_info
;
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
71 // Set time-out on 30 miliseconds
72 const struct timeval timeout
= {
73 .tv_sec
= 0, // 0 second
74 .tv_usec
= 300000 // 300000 micro seconds
77 serial_port
uart_open(const char* pcPortName
)
79 serial_port_unix
* sp
= malloc(sizeof(serial_port_unix
));
80 if (sp
== 0) return INVALID_SERIAL_PORT
;
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
;
89 char *colon
= strrchr(addrstr
, ':');
97 int s
= getaddrinfo(addrstr
, portstr
, NULL
, &addr
);
99 printf("Error: getaddrinfo: %s\n", gai_strerror(s
));
100 return INVALID_SERIAL_PORT
;
104 for (rp
= addr
; rp
!= NULL
; rp
= rp
->ai_next
) {
105 sfd
= socket(rp
->ai_family
, rp
->ai_socktype
,
110 if (connect(sfd
, rp
->ai_addr
, rp
->ai_addrlen
) != -1)
116 if (rp
== NULL
) { /* No address succeeded */
117 printf("Error: Could not connect\n");
118 return INVALID_SERIAL_PORT
;
127 setsockopt(sp
->fd
, SOL_TCP
, TCP_NODELAY
, &one
, sizeof(one
));
131 sp
->fd
= open(pcPortName
, O_RDWR
| O_NOCTTY
| O_NDELAY
| O_NONBLOCK
);
134 return INVALID_SERIAL_PORT
;
137 // Finally figured out a way to claim a serial port interface under unix
138 // We just try to set a (advisory) lock on the file descriptor
141 fl
.l_whence
= SEEK_SET
;
146 // Does the system allows us to place a lock on this file descriptor
147 if (fcntl(sp
->fd
, F_SETLK
, &fl
) == -1) {
148 // A conflicting lock is held by another process
150 return CLAIMED_SERIAL_PORT
;
153 // Try to retrieve the old (current) terminal info struct
154 if(tcgetattr(sp
->fd
,&sp
->tiOld
) == -1) {
156 return INVALID_SERIAL_PORT
;
159 // Duplicate the (old) terminal info struct
160 sp
->tiNew
= sp
->tiOld
;
162 // Configure the serial port
163 sp
->tiNew
.c_cflag
= CS8
| CLOCAL
| CREAD
;
164 sp
->tiNew
.c_iflag
= IGNPAR
;
165 sp
->tiNew
.c_oflag
= 0;
166 sp
->tiNew
.c_lflag
= 0;
168 // Block until n bytes are received
169 sp
->tiNew
.c_cc
[VMIN
] = 0;
170 // Block until a timer expires (n * 100 mSec.)
171 sp
->tiNew
.c_cc
[VTIME
] = 0;
173 // Try to set the new terminal info struct
174 if(tcsetattr(sp
->fd
,TCSANOW
,&sp
->tiNew
) == -1) {
176 return INVALID_SERIAL_PORT
;
179 // Flush all lingering data that may exist
180 tcflush(sp
->fd
, TCIOFLUSH
);
185 void uart_close(const serial_port sp
) {
186 serial_port_unix
* spu
= (serial_port_unix
*)sp
;
187 tcflush(spu
->fd
,TCIOFLUSH
);
188 tcsetattr(spu
->fd
,TCSANOW
,&(spu
->tiOld
));
191 fl
.l_whence
= SEEK_SET
;
195 fcntl(spu
->fd
, F_SETLK
, &fl
);
200 bool uart_receive(const serial_port sp
, uint8_t* pbtRx
, size_t pszMaxRxLen
, size_t* pszRxLen
) {
206 // Reset the output count
210 // Reset file descriptor
212 FD_SET(((serial_port_unix
*)sp
)->fd
,&rfds
);
214 res
= select(((serial_port_unix
*)sp
)->fd
+1, &rfds
, NULL
, NULL
, &tv
);
223 if (*pszRxLen
== 0) {
224 // Error, we received no data
227 // We received some data, but nothing more is available
232 // Retrieve the count of the incoming bytes
233 res
= ioctl(((serial_port_unix
*)sp
)->fd
, FIONREAD
, &byteCount
);
234 if (res
< 0) return false;
236 // Cap the number of bytes, so we don't overrun the buffer
237 if (pszMaxRxLen
- (*pszRxLen
) < byteCount
) {
238 byteCount
= pszMaxRxLen
- (*pszRxLen
);
241 // There is something available, read the data
242 res
= read(((serial_port_unix
*)sp
)->fd
, pbtRx
+(*pszRxLen
), byteCount
);
244 // Stop if the OS has some troubles reading the data
245 if (res
<= 0) return false;
249 if (*pszRxLen
== pszMaxRxLen
) {
250 // We have all the data we wanted.
259 bool uart_send(const serial_port sp
, const uint8_t* pbtTx
, const size_t szTxLen
) {
265 while (szPos
< szTxLen
) {
266 // Reset file descriptor
268 FD_SET(((serial_port_unix
*)sp
)->fd
,&rfds
);
270 res
= select(((serial_port_unix
*)sp
)->fd
+1, NULL
, &rfds
, NULL
, &tv
);
282 // Send away the bytes
283 res
= write(((serial_port_unix
*)sp
)->fd
,pbtTx
+szPos
,szTxLen
-szPos
);
285 // Stop if the OS has some troubles sending the data
286 if (res
<= 0) return false;
293 bool uart_set_speed(serial_port sp
, const uint32_t uiPortSpeed
) {
294 const serial_port_unix
* spu
= (serial_port_unix
*)sp
;
296 switch (uiPortSpeed
) {
297 case 0: stPortSpeed
= B0
; break;
298 case 50: stPortSpeed
= B50
; break;
299 case 75: stPortSpeed
= B75
; break;
300 case 110: stPortSpeed
= B110
; break;
301 case 134: stPortSpeed
= B134
; break;
302 case 150: stPortSpeed
= B150
; break;
303 case 300: stPortSpeed
= B300
; break;
304 case 600: stPortSpeed
= B600
; break;
305 case 1200: stPortSpeed
= B1200
; break;
306 case 1800: stPortSpeed
= B1800
; break;
307 case 2400: stPortSpeed
= B2400
; break;
308 case 4800: stPortSpeed
= B4800
; break;
309 case 9600: stPortSpeed
= B9600
; break;
310 case 19200: stPortSpeed
= B19200
; break;
311 case 38400: stPortSpeed
= B38400
; break;
313 case 57600: stPortSpeed
= B57600
; break;
316 case 115200: stPortSpeed
= B115200
; break;
319 case 230400: stPortSpeed
= B230400
; break;
322 case 460800: stPortSpeed
= B460800
; break;
325 case 921600: stPortSpeed
= B921600
; break;
327 default: return false;
330 if (tcgetattr(spu
->fd
,&ti
) == -1) return false;
331 // Set port speed (Input and Output)
332 cfsetispeed(&ti
,stPortSpeed
);
333 cfsetospeed(&ti
,stPortSpeed
);
334 return (tcsetattr(spu
->fd
,TCSANOW
,&ti
) != -1);
337 uint32_t uart_get_speed(const serial_port sp
) {
339 uint32_t uiPortSpeed
;
340 const serial_port_unix
* spu
= (serial_port_unix
*)sp
;
341 if (tcgetattr(spu
->fd
,&ti
) == -1) return 0;
342 // Set port speed (Input)
343 speed_t stPortSpeed
= cfgetispeed(&ti
);
344 switch (stPortSpeed
) {
345 case B0
: uiPortSpeed
= 0; break;
346 case B50
: uiPortSpeed
= 50; break;
347 case B75
: uiPortSpeed
= 75; break;
348 case B110
: uiPortSpeed
= 110; break;
349 case B134
: uiPortSpeed
= 134; break;
350 case B150
: uiPortSpeed
= 150; break;
351 case B300
: uiPortSpeed
= 300; break;
352 case B600
: uiPortSpeed
= 600; break;
353 case B1200
: uiPortSpeed
= 1200; break;
354 case B1800
: uiPortSpeed
= 1800; break;
355 case B2400
: uiPortSpeed
= 2400; break;
356 case B4800
: uiPortSpeed
= 4800; break;
357 case B9600
: uiPortSpeed
= 9600; break;
358 case B19200
: uiPortSpeed
= 19200; break;
359 case B38400
: uiPortSpeed
= 38400; break;
361 case B57600
: uiPortSpeed
= 57600; break;
364 case B115200
: uiPortSpeed
= 115200; break;
367 case B230400
: uiPortSpeed
= 230400; break;
370 case B460800
: uiPortSpeed
= 460800; break;
373 case B921600
: uiPortSpeed
= 921600; break;