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