]> git.zerfleddert.de Git - proxmark3-svn/blame - client/uart.c
Merge remote-tracking branch 'upstream/master'
[proxmark3-svn] / client / uart.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 *
29 * @file uart.c
30 * @brief
31 *
5bcc76c4 32 */
33
34#include "uart.h"
5bcc76c4 35
36// Test if we are dealing with unix operating systems
37#ifndef _WIN32
38
39#include <termios.h>
40typedef struct termios term_info;
41typedef struct {
42 int fd; // Serial port file descriptor
43 term_info tiOld; // Terminal info before using the port
44 term_info tiNew; // Terminal info during the transaction
45} serial_port_unix;
46
47// Set time-out on 30 miliseconds
13dbdd6b 48const struct timeval timeout = {
5bcc76c4 49 .tv_sec = 0, // 0 second
50 .tv_usec = 30000 // 30000 micro seconds
51};
52
5bcc76c4 53serial_port uart_open(const char* pcPortName)
54{
55 serial_port_unix* sp = malloc(sizeof(serial_port_unix));
5bcc76c4 56 if (sp == 0) return INVALID_SERIAL_PORT;
13dbdd6b 57
5bcc76c4 58 sp->fd = open(pcPortName, O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK);
62ee4fed 59 if(sp->fd == -1) {
5bcc76c4 60 uart_close(sp);
61 return INVALID_SERIAL_PORT;
62 }
62ee4fed 63
64 // Finally figured out a way to claim a serial port interface under unix
65 // We just try to set a (advisory) lock on the file descriptor
66 struct flock fl;
67 fl.l_type = F_WRLCK;
68 fl.l_whence = SEEK_SET;
69 fl.l_start = 0;
70 fl.l_len = 0;
71 fl.l_pid = getpid();
13dbdd6b 72
62ee4fed 73 // Does the system allows us to place a lock on this file descriptor
74 if (fcntl(sp->fd, F_SETLK, &fl) == -1) {
75 // A conflicting lock is held by another process
ca4714cd 76 free(sp);
62ee4fed 77 return CLAIMED_SERIAL_PORT;
5bcc76c4 78 }
62ee4fed 79
80 // Try to retrieve the old (current) terminal info struct
81 if(tcgetattr(sp->fd,&sp->tiOld) == -1) {
5bcc76c4 82 uart_close(sp);
62ee4fed 83 return INVALID_SERIAL_PORT;
5bcc76c4 84 }
13dbdd6b 85
62ee4fed 86 // Duplicate the (old) terminal info struct
5bcc76c4 87 sp->tiNew = sp->tiOld;
13dbdd6b 88
62ee4fed 89 // Configure the serial port
5bcc76c4 90 sp->tiNew.c_cflag = CS8 | CLOCAL | CREAD;
62ee4fed 91 sp->tiNew.c_iflag = IGNPAR;
5bcc76c4 92 sp->tiNew.c_oflag = 0;
93 sp->tiNew.c_lflag = 0;
62ee4fed 94
95 // Block until n bytes are received
96 sp->tiNew.c_cc[VMIN] = 0;
97 // Block until a timer expires (n * 100 mSec.)
98 sp->tiNew.c_cc[VTIME] = 0;
0a24369c 99
62ee4fed 100 // Try to set the new terminal info struct
101 if(tcsetattr(sp->fd,TCSANOW,&sp->tiNew) == -1) {
5bcc76c4 102 uart_close(sp);
103 return INVALID_SERIAL_PORT;
104 }
13dbdd6b 105
62ee4fed 106 // Flush all lingering data that may exist
0a24369c 107 tcflush(sp->fd, TCIOFLUSH);
62ee4fed 108
5bcc76c4 109 return sp;
110}
111
62ee4fed 112void uart_close(const serial_port sp) {
113 serial_port_unix* spu = (serial_port_unix*)sp;
114 tcflush(spu->fd,TCIOFLUSH);
115 tcsetattr(spu->fd,TCSANOW,&(spu->tiOld));
116 struct flock fl;
117 fl.l_type = F_UNLCK;
118 fl.l_whence = SEEK_SET;
119 fl.l_start = 0;
120 fl.l_len = 0;
121 fl.l_pid = getpid();
122 fcntl(spu->fd, F_SETLK, &fl);
123 close(spu->fd);
124 free(sp);
125}
126
13dbdd6b 127bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) {
5bcc76c4 128 const serial_port_unix* spu = (serial_port_unix*)sp;
62ee4fed 129 speed_t stPortSpeed;
130 switch (uiPortSpeed) {
131 case 0: stPortSpeed = B0; break;
132 case 50: stPortSpeed = B50; break;
133 case 75: stPortSpeed = B75; break;
134 case 110: stPortSpeed = B110; break;
135 case 134: stPortSpeed = B134; break;
136 case 150: stPortSpeed = B150; break;
137 case 300: stPortSpeed = B300; break;
138 case 600: stPortSpeed = B600; break;
139 case 1200: stPortSpeed = B1200; break;
140 case 1800: stPortSpeed = B1800; break;
141 case 2400: stPortSpeed = B2400; break;
142 case 4800: stPortSpeed = B4800; break;
143 case 9600: stPortSpeed = B9600; break;
144 case 19200: stPortSpeed = B19200; break;
145 case 38400: stPortSpeed = B38400; break;
146# ifdef B57600
147 case 57600: stPortSpeed = B57600; break;
148# endif
149# ifdef B115200
150 case 115200: stPortSpeed = B115200; break;
151# endif
152# ifdef B230400
153 case 230400: stPortSpeed = B230400; break;
154# endif
155# ifdef B460800
156 case 460800: stPortSpeed = B460800; break;
157# endif
158# ifdef B921600
159 case 921600: stPortSpeed = B921600; break;
160# endif
161 default: return false;
162 };
163 struct termios ti;
164 if (tcgetattr(spu->fd,&ti) == -1) return false;
165 // Set port speed (Input and Output)
166 cfsetispeed(&ti,stPortSpeed);
167 cfsetospeed(&ti,stPortSpeed);
168 return (tcsetattr(spu->fd,TCSANOW,&ti) != -1);
5bcc76c4 169}
170
13dbdd6b 171uint32_t uart_get_speed(const serial_port sp) {
62ee4fed 172 struct termios ti;
173 uint32_t uiPortSpeed;
5bcc76c4 174 const serial_port_unix* spu = (serial_port_unix*)sp;
62ee4fed 175 if (tcgetattr(spu->fd,&ti) == -1) return 0;
176 // Set port speed (Input)
177 speed_t stPortSpeed = cfgetispeed(&ti);
178 switch (stPortSpeed) {
179 case B0: uiPortSpeed = 0; break;
180 case B50: uiPortSpeed = 50; break;
181 case B75: uiPortSpeed = 75; break;
182 case B110: uiPortSpeed = 110; break;
183 case B134: uiPortSpeed = 134; break;
184 case B150: uiPortSpeed = 150; break;
185 case B300: uiPortSpeed = 300; break;
186 case B600: uiPortSpeed = 600; break;
187 case B1200: uiPortSpeed = 1200; break;
188 case B1800: uiPortSpeed = 1800; break;
189 case B2400: uiPortSpeed = 2400; break;
190 case B4800: uiPortSpeed = 4800; break;
191 case B9600: uiPortSpeed = 9600; break;
192 case B19200: uiPortSpeed = 19200; break;
193 case B38400: uiPortSpeed = 38400; break;
194# ifdef B57600
195 case B57600: uiPortSpeed = 57600; break;
196# endif
197# ifdef B115200
198 case B115200: uiPortSpeed = 115200; break;
199# endif
200# ifdef B230400
201 case B230400: uiPortSpeed = 230400; break;
202# endif
203# ifdef B460800
204 case B460800: uiPortSpeed = 460800; break;
205# endif
206# ifdef B921600
207 case B921600: uiPortSpeed = 921600; break;
208# endif
209 default: return 0;
210 };
5bcc76c4 211 return uiPortSpeed;
212}
213
62ee4fed 214bool uart_set_parity(serial_port sp, serial_port_parity spp) {
215 struct termios ti;
216 const serial_port_unix* spu = (serial_port_unix*)sp;
217 if (tcgetattr(spu->fd,&ti) == -1) return false;
218 switch(spp) {
219 case SP_INVALID: return false;
220 case SP_NONE: ti.c_cflag &= ~(PARENB | PARODD); break;
221 case SP_EVEN: ti.c_cflag |= PARENB; ti.c_cflag &= ~(PARODD); break;
222 case SP_ODD: ti.c_cflag |= PARENB | PARODD; break;
223 }
224 return (tcsetattr(spu->fd,TCSANOW,&ti) != -1);
225}
226
227serial_port_parity uart_get_parity(const serial_port sp) {
228 struct termios ti;
229 const serial_port_unix* spu = (serial_port_unix*)sp;
230 if (tcgetattr(spu->fd,&ti) == -1) return SP_INVALID;
231
232 if (ti.c_cflag & PARENB) {
233 if (ti.c_cflag & PARODD) {
234 return SP_ODD;
235 } else {
236 return SP_EVEN;
237 }
238 } else {
239 return SP_NONE;
240 }
5bcc76c4 241}
242
13dbdd6b 243bool uart_cts(const serial_port sp) {
5bcc76c4 244 char status;
245 if (ioctl(((serial_port_unix*)sp)->fd,TIOCMGET,&status) < 0) return false;
246 return (status & TIOCM_CTS);
247}
248
13dbdd6b 249bool uart_receive(const serial_port sp, byte_t* pbtRx, size_t* pszRxLen) {
62ee4fed 250
5bcc76c4 251 int res;
252 int byteCount;
253 fd_set rfds;
254 struct timeval tv;
13dbdd6b 255
256 // Reset the output count
5bcc76c4 257 *pszRxLen = 0;
13dbdd6b 258
5bcc76c4 259 do {
260 // Reset file descriptor
261 FD_ZERO(&rfds);
262 FD_SET(((serial_port_unix*)sp)->fd,&rfds);
263 tv = timeout;
264 res = select(((serial_port_unix*)sp)->fd+1, &rfds, NULL, NULL, &tv);
13dbdd6b 265
5bcc76c4 266 // Read error
267 if (res < 0) {
5bcc76c4 268 return false;
269 }
babfcaa0 270
5bcc76c4 271 // Read time-out
272 if (res == 0) {
273 if (*pszRxLen == 0) {
274 // Error, we received no data
5bcc76c4 275 return false;
276 } else {
277 // We received some data, but nothing more is available
278 return true;
279 }
280 }
babfcaa0 281
5bcc76c4 282 // Retrieve the count of the incoming bytes
283 res = ioctl(((serial_port_unix*)sp)->fd, FIONREAD, &byteCount);
284 if (res < 0) return false;
babfcaa0 285
5bcc76c4 286 // There is something available, read the data
287 res = read(((serial_port_unix*)sp)->fd,pbtRx+(*pszRxLen),byteCount);
babfcaa0 288
5bcc76c4 289 // Stop if the OS has some troubles reading the data
290 if (res <= 0) return false;
babfcaa0 291
5bcc76c4 292 *pszRxLen += res;
babfcaa0 293
294 if(res==byteCount)
295 return true;
13dbdd6b 296
5bcc76c4 297 } while (byteCount);
babfcaa0 298
5bcc76c4 299 return true;
300}
301
13dbdd6b 302bool uart_send(const serial_port sp, const byte_t* pbtTx, const size_t szTxLen) {
5bcc76c4 303 int32_t res;
304 size_t szPos = 0;
305 fd_set rfds;
306 struct timeval tv;
13dbdd6b 307
62ee4fed 308 while (szPos < szTxLen) {
5bcc76c4 309 // Reset file descriptor
310 FD_ZERO(&rfds);
311 FD_SET(((serial_port_unix*)sp)->fd,&rfds);
312 tv = timeout;
313 res = select(((serial_port_unix*)sp)->fd+1, NULL, &rfds, NULL, &tv);
13dbdd6b 314
5bcc76c4 315 // Write error
316 if (res < 0) {
5bcc76c4 317 return false;
318 }
13dbdd6b 319
5bcc76c4 320 // Write time-out
321 if (res == 0) {
5bcc76c4 322 return false;
323 }
13dbdd6b 324
5bcc76c4 325 // Send away the bytes
326 res = write(((serial_port_unix*)sp)->fd,pbtTx+szPos,szTxLen-szPos);
327
328 // Stop if the OS has some troubles sending the data
329 if (res <= 0) return false;
13dbdd6b 330
5bcc76c4 331 szPos += res;
332 }
333 return true;
334}
335
336#else
337// The windows serial port implementation
338
13dbdd6b 339typedef struct {
5bcc76c4 340 HANDLE hPort; // Serial port handle
341 DCB dcb; // Device control settings
342 COMMTIMEOUTS ct; // Serial port time-out configuration
343} serial_port_windows;
344
a0bbdb76 345void upcase(char *p) {
62ee4fed 346 while(*p != '\0') {
347 if(*p >= 97 && *p <= 122) {
348 *p -= 32;
a0bbdb76 349 }
62ee4fed 350 ++p;
351 }
a0bbdb76 352}
353
13dbdd6b 354serial_port uart_open(const char* pcPortName) {
5bcc76c4 355 char acPortName[255];
356 serial_port_windows* sp = malloc(sizeof(serial_port_windows));
13dbdd6b 357
5bcc76c4 358 // Copy the input "com?" to "\\.\COM?" format
359 sprintf(acPortName,"\\\\.\\%s",pcPortName);
a0bbdb76 360 upcase(acPortName);
13dbdd6b 361
5bcc76c4 362 // Try to open the serial port
363 sp->hPort = CreateFileA(acPortName,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
62ee4fed 364 if (sp->hPort == INVALID_HANDLE_VALUE) {
5bcc76c4 365 uart_close(sp);
366 return INVALID_SERIAL_PORT;
367 }
13dbdd6b 368
5bcc76c4 369 // Prepare the device control
370 memset(&sp->dcb, 0, sizeof(DCB));
371 sp->dcb.DCBlength = sizeof(DCB);
62ee4fed 372 if(!BuildCommDCBA("baud=9600 data=8 parity=N stop=1",&sp->dcb)) {
5bcc76c4 373 uart_close(sp);
374 return INVALID_SERIAL_PORT;
375 }
13dbdd6b 376
5bcc76c4 377 // Update the active serial port
62ee4fed 378 if(!SetCommState(sp->hPort,&sp->dcb)) {
5bcc76c4 379 uart_close(sp);
380 return INVALID_SERIAL_PORT;
381 }
13dbdd6b 382
5bcc76c4 383 sp->ct.ReadIntervalTimeout = 0;
384 sp->ct.ReadTotalTimeoutMultiplier = 0;
385 sp->ct.ReadTotalTimeoutConstant = 30;
386 sp->ct.WriteTotalTimeoutMultiplier = 0;
387 sp->ct.WriteTotalTimeoutConstant = 30;
13dbdd6b 388
62ee4fed 389 if(!SetCommTimeouts(sp->hPort,&sp->ct)) {
5bcc76c4 390 uart_close(sp);
391 return INVALID_SERIAL_PORT;
392 }
13dbdd6b 393
5bcc76c4 394 PurgeComm(sp->hPort, PURGE_RXABORT | PURGE_RXCLEAR);
13dbdd6b 395
5bcc76c4 396 return sp;
397}
398
13dbdd6b 399void uart_close(const serial_port sp) {
5bcc76c4 400 CloseHandle(((serial_port_windows*)sp)->hPort);
401 free(sp);
402}
403
13dbdd6b 404bool uart_set_speed(serial_port sp, const uint32_t uiPortSpeed) {
5bcc76c4 405 serial_port_windows* spw;
5bcc76c4 406 spw = (serial_port_windows*)sp;
407 spw->dcb.BaudRate = uiPortSpeed;
13dbdd6b 408 return SetCommState(spw->hPort, &spw->dcb);
5bcc76c4 409}
410
13dbdd6b 411uint32_t uart_get_speed(const serial_port sp) {
5bcc76c4 412 const serial_port_windows* spw = (serial_port_windows*)sp;
13dbdd6b 413 if (!GetCommState(spw->hPort, (serial_port)&spw->dcb)) {
5bcc76c4 414 return spw->dcb.BaudRate;
13dbdd6b 415 }
5bcc76c4 416 return 0;
417}
418
13dbdd6b 419bool uart_receive(const serial_port sp, byte_t* pbtRx, size_t* pszRxLen) {
5bcc76c4 420 ReadFile(((serial_port_windows*)sp)->hPort,pbtRx,*pszRxLen,(LPDWORD)pszRxLen,NULL);
421 return (*pszRxLen != 0);
422}
423
13dbdd6b 424bool uart_send(const serial_port sp, const byte_t* pbtTx, const size_t szTxLen) {
5bcc76c4 425 DWORD dwTxLen = 0;
426 return WriteFile(((serial_port_windows*)sp)->hPort,pbtTx,szTxLen,&dwTxLen,NULL);
427 return (dwTxLen != 0);
428}
429
430#endif
Impressum, Datenschutz