1 | /*
|
---|
2 | io_serial.c
|
---|
3 | Serial port input/output functions
|
---|
4 |
|
---|
5 | This file is part of the Unix driver for Towitoko smartcard readers
|
---|
6 | Copyright (C) 2000 2001 Carlos Prados <cprados@yahoo.com>
|
---|
7 |
|
---|
8 | This version is modified by doz21 to work in a special manner ;)
|
---|
9 |
|
---|
10 | This library is free software; you can redistribute it and/or
|
---|
11 | modify it under the terms of the GNU Lesser General Public
|
---|
12 | License as published by the Free Software Foundation; either
|
---|
13 | version 2 of the License, or (at your option) any later version.
|
---|
14 |
|
---|
15 | This library is distributed in the hope that it will be useful,
|
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
18 | Lesser General Public License for more details.
|
---|
19 |
|
---|
20 | You should have received a copy of the GNU Lesser General Public
|
---|
21 | License along with this library; if not, write to the Free Software
|
---|
22 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
23 | */
|
---|
24 |
|
---|
25 | #include "defines.h"
|
---|
26 | #include <stdio.h>
|
---|
27 | #include <string.h>
|
---|
28 | #include <stdlib.h>
|
---|
29 | #ifdef OS_HPUX
|
---|
30 | #include <sys/modem.h>
|
---|
31 | #endif
|
---|
32 | #include <termios.h>
|
---|
33 | #include <unistd.h>
|
---|
34 | #include <sys/stat.h>
|
---|
35 | #include <fcntl.h>
|
---|
36 | #ifdef HAVE_POLL
|
---|
37 | #include <sys/poll.h>
|
---|
38 | #else
|
---|
39 | #include <sys/signal.h>
|
---|
40 | #include <sys/types.h>
|
---|
41 | #include <sys/time.h>
|
---|
42 | #endif
|
---|
43 | #include <sys/ioctl.h>
|
---|
44 | #include <time.h>
|
---|
45 | #include "io_serial.h"
|
---|
46 | #include "mc_global.h"
|
---|
47 |
|
---|
48 | #define IO_SERIAL_FILENAME_LENGTH 32
|
---|
49 |
|
---|
50 | /*
|
---|
51 | * Internal functions declaration
|
---|
52 | */
|
---|
53 |
|
---|
54 | static int IO_Serial_Bitrate(int bitrate);
|
---|
55 |
|
---|
56 | static bool IO_Serial_WaitToRead (int hnd, unsigned delay_ms, unsigned timeout_ms);
|
---|
57 |
|
---|
58 | static bool IO_Serial_WaitToWrite (IO_Serial *io, unsigned delay_ms, unsigned timeout_ms);
|
---|
59 |
|
---|
60 | static void IO_Serial_DeviceName (unsigned com, bool usbserial, char * filename, unsigned length);
|
---|
61 |
|
---|
62 | static bool IO_Serial_InitPnP (IO_Serial * io);
|
---|
63 |
|
---|
64 | static void IO_Serial_Clear (IO_Serial * io);
|
---|
65 |
|
---|
66 | static bool IO_Serial_GetPropertiesCache(IO_Serial * io, IO_Serial_Properties * props);
|
---|
67 |
|
---|
68 | static void IO_Serial_SetPropertiesCache(IO_Serial * io, IO_Serial_Properties * props);
|
---|
69 |
|
---|
70 | static void IO_Serial_ClearPropertiesCache (IO_Serial * io);
|
---|
71 |
|
---|
72 | int fdmc=(-1);
|
---|
73 |
|
---|
74 | #if defined(TUXBOX) && defined(PPC)
|
---|
75 | void IO_Serial_Ioctl_Lock(IO_Serial * io, int flag)
|
---|
76 | {
|
---|
77 | extern int *mpcs_sem;
|
---|
78 | if ((io->com!=RTYP_DB2COM1) && (io->com!=RTYP_DB2COM2)) return;
|
---|
79 | if (!flag)
|
---|
80 | *mpcs_sem=0;
|
---|
81 | else while (*mpcs_sem!=io->com)
|
---|
82 | {
|
---|
83 | while (*mpcs_sem)
|
---|
84 | usleep((io->com)*2000);
|
---|
85 | *mpcs_sem=io->com;
|
---|
86 | usleep(1000);
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | static bool IO_Serial_DTR_RTS_dbox2(int mcport, int dtr, int set)
|
---|
91 | {
|
---|
92 | int rc;
|
---|
93 | unsigned short msr;
|
---|
94 | unsigned int mbit;
|
---|
95 | unsigned short rts_bits[2]={ 0x10, 0x800};
|
---|
96 | unsigned short dtr_bits[2]={0x100, 0};
|
---|
97 |
|
---|
98 | #ifdef DEBUG_IO
|
---|
99 | printf("IO: multicam.o %s %s\n", dtr ? "dtr" : "rts", set ? "set" : "clear"); fflush(stdout);
|
---|
100 | #endif
|
---|
101 | if ((rc=ioctl(fdmc, GET_PCDAT, &msr))>=0)
|
---|
102 | {
|
---|
103 | if (dtr) // DTR
|
---|
104 | {
|
---|
105 | if (dtr_bits[mcport])
|
---|
106 | {
|
---|
107 | if (set)
|
---|
108 | msr&=(unsigned short)(~dtr_bits[mcport]);
|
---|
109 | else
|
---|
110 | msr|=dtr_bits[mcport];
|
---|
111 | rc=ioctl(fdmc, SET_PCDAT, &msr);
|
---|
112 | }
|
---|
113 | else
|
---|
114 | rc=0; // Dummy, can't handle using multicam.o
|
---|
115 | }
|
---|
116 | else // RTS
|
---|
117 | {
|
---|
118 | if (set)
|
---|
119 | msr&=(unsigned short)(~rts_bits[mcport]);
|
---|
120 | else
|
---|
121 | msr|=rts_bits[mcport];
|
---|
122 | rc=ioctl(fdmc, SET_PCDAT, &msr);
|
---|
123 | }
|
---|
124 | }
|
---|
125 | return((rc<0) ? FALSE : TRUE);
|
---|
126 | }
|
---|
127 | #endif
|
---|
128 |
|
---|
129 | bool IO_Serial_DTR_RTS(IO_Serial * io, int dtr, int set)
|
---|
130 | {
|
---|
131 | unsigned int msr;
|
---|
132 | unsigned int mbit;
|
---|
133 |
|
---|
134 | #if defined(TUXBOX) && defined(PPC)
|
---|
135 | if ((io->com==RTYP_DB2COM1) || (io->com==RTYP_DB2COM2))
|
---|
136 | return(IO_Serial_DTR_RTS_dbox2(io->com==RTYP_DB2COM2, dtr, set));
|
---|
137 | #endif
|
---|
138 |
|
---|
139 | mbit=(dtr) ? TIOCM_DTR : TIOCM_RTS;
|
---|
140 | #if defined(TIOCMBIS) && defined(TIOBMBIC)
|
---|
141 | if (ioctl (io->fd, set ? TIOCMBIS : TIOCMBIC, &mbit) < 0)
|
---|
142 | return FALSE;
|
---|
143 | #else
|
---|
144 | if (ioctl(io->fd, TIOCMGET, &msr) < 0)
|
---|
145 | return FALSE;
|
---|
146 | if (set)
|
---|
147 | msr|=mbit;
|
---|
148 | else
|
---|
149 | msr&=~mbit;
|
---|
150 | return((ioctl(io->fd, TIOCMSET, &msr)<0) ? FALSE : TRUE);
|
---|
151 | #endif
|
---|
152 | }
|
---|
153 |
|
---|
154 | /*
|
---|
155 | * Public functions definition
|
---|
156 | */
|
---|
157 |
|
---|
158 | IO_Serial * IO_Serial_New (void)
|
---|
159 | {
|
---|
160 | IO_Serial *io;
|
---|
161 |
|
---|
162 | io = (IO_Serial *) malloc (sizeof (IO_Serial));
|
---|
163 |
|
---|
164 | if (io != NULL)
|
---|
165 | IO_Serial_Clear (io);
|
---|
166 |
|
---|
167 | return io;
|
---|
168 | }
|
---|
169 |
|
---|
170 | bool IO_Serial_Init (IO_Serial * io, unsigned com, bool usbserial, bool pnp)
|
---|
171 | {
|
---|
172 | char filename[IO_SERIAL_FILENAME_LENGTH];
|
---|
173 |
|
---|
174 | IO_Serial_DeviceName (com, usbserial, filename, IO_SERIAL_FILENAME_LENGTH);
|
---|
175 |
|
---|
176 | #ifdef DEBUG_IO
|
---|
177 | printf ("IO: Opening serial port %s\n", filename);
|
---|
178 | #endif
|
---|
179 |
|
---|
180 | if (com < 1)
|
---|
181 | return FALSE;
|
---|
182 |
|
---|
183 | io->com = com;
|
---|
184 |
|
---|
185 | #ifdef SCI_DEV
|
---|
186 | if (com==RTYP_SCI)
|
---|
187 | io->fd = open (filename, O_RDWR);
|
---|
188 | else
|
---|
189 | #endif
|
---|
190 | io->fd = open (filename, O_RDWR | O_NOCTTY | O_SYNC);
|
---|
191 |
|
---|
192 | if (io->fd < 0)
|
---|
193 | return FALSE;
|
---|
194 |
|
---|
195 | #if defined(TUXBOX) && defined(PPC)
|
---|
196 | if ((com==RTYP_DB2COM1) || (com==RTYP_DB2COM2))
|
---|
197 | if ((fdmc = open(DEV_MULTICAM, O_RDWR)) < 0)
|
---|
198 | {
|
---|
199 | close(io->fd);
|
---|
200 | return FALSE;
|
---|
201 | }
|
---|
202 | #endif
|
---|
203 |
|
---|
204 | if (com!=RTYP_SCI)
|
---|
205 | IO_Serial_InitPnP (io);
|
---|
206 |
|
---|
207 | io->usbserial=usbserial;
|
---|
208 |
|
---|
209 | if(io->com!=RTYP_SCI)
|
---|
210 | IO_Serial_Flush(io);
|
---|
211 |
|
---|
212 | return TRUE;
|
---|
213 | }
|
---|
214 |
|
---|
215 | bool IO_Serial_GetProperties (IO_Serial * io, IO_Serial_Properties * props)
|
---|
216 | {
|
---|
217 | struct termios currtio;
|
---|
218 | speed_t i_speed, o_speed;
|
---|
219 | unsigned int mctl;
|
---|
220 |
|
---|
221 | #ifdef SCI_DEV
|
---|
222 | if(io->com==RTYP_SCI)
|
---|
223 | return FALSE;
|
---|
224 | #endif
|
---|
225 |
|
---|
226 | if (IO_Serial_GetPropertiesCache(io, props))
|
---|
227 | return TRUE;
|
---|
228 |
|
---|
229 | if (tcgetattr (io->fd, &currtio) != 0)
|
---|
230 | return FALSE;
|
---|
231 |
|
---|
232 | o_speed = cfgetospeed (&currtio);
|
---|
233 |
|
---|
234 | switch (o_speed)
|
---|
235 | {
|
---|
236 | #ifdef B0
|
---|
237 | case B0:
|
---|
238 | props->output_bitrate = 0;
|
---|
239 | break;
|
---|
240 | #endif
|
---|
241 | #ifdef B50
|
---|
242 | case B50:
|
---|
243 | props->output_bitrate = 50;
|
---|
244 | break;
|
---|
245 | #endif
|
---|
246 | #ifdef B75
|
---|
247 | case B75:
|
---|
248 | props->output_bitrate = 75;
|
---|
249 | break;
|
---|
250 | #endif
|
---|
251 | #ifdef B110
|
---|
252 | case B110:
|
---|
253 | props->output_bitrate = 110;
|
---|
254 | break;
|
---|
255 | #endif
|
---|
256 | #ifdef B134
|
---|
257 | case B134:
|
---|
258 | props->output_bitrate = 134;
|
---|
259 | break;
|
---|
260 | #endif
|
---|
261 | #ifdef B150
|
---|
262 | case B150:
|
---|
263 | props->output_bitrate = 150;
|
---|
264 | break;
|
---|
265 | #endif
|
---|
266 | #ifdef B200
|
---|
267 | case B200:
|
---|
268 | props->output_bitrate = 200;
|
---|
269 | break;
|
---|
270 | #endif
|
---|
271 | #ifdef B300
|
---|
272 | case B300:
|
---|
273 | props->output_bitrate = 300;
|
---|
274 | break;
|
---|
275 | #endif
|
---|
276 | #ifdef B600
|
---|
277 | case B600:
|
---|
278 | props->output_bitrate = 600;
|
---|
279 | break;
|
---|
280 | #endif
|
---|
281 | #ifdef B1200
|
---|
282 | case B1200:
|
---|
283 | props->output_bitrate = 1200;
|
---|
284 | break;
|
---|
285 | #endif
|
---|
286 | #ifdef B1800
|
---|
287 | case B1800:
|
---|
288 | props->output_bitrate = 1800;
|
---|
289 | break;
|
---|
290 | #endif
|
---|
291 | #ifdef B2400
|
---|
292 | case B2400:
|
---|
293 | props->output_bitrate = 2400;
|
---|
294 | break;
|
---|
295 | #endif
|
---|
296 | #ifdef B4800
|
---|
297 | case B4800:
|
---|
298 | props->output_bitrate = 4800;
|
---|
299 | break;
|
---|
300 | #endif
|
---|
301 | #ifdef B9600
|
---|
302 | case B9600:
|
---|
303 | props->output_bitrate = 9600;
|
---|
304 | break;
|
---|
305 | #endif
|
---|
306 | #ifdef B19200
|
---|
307 | case B19200:
|
---|
308 | props->output_bitrate = 19200;
|
---|
309 | break;
|
---|
310 | #endif
|
---|
311 | #ifdef B38400
|
---|
312 | case B38400:
|
---|
313 | props->output_bitrate = 38400;
|
---|
314 | break;
|
---|
315 | #endif
|
---|
316 | #ifdef B57600
|
---|
317 | case B57600:
|
---|
318 | props->output_bitrate = 57600;
|
---|
319 | break;
|
---|
320 | #endif
|
---|
321 | #ifdef B115200
|
---|
322 | case B115200:
|
---|
323 | props->output_bitrate = 115200;
|
---|
324 | break;
|
---|
325 | #endif
|
---|
326 | #ifdef B230400
|
---|
327 | case B230400:
|
---|
328 | props->output_bitrate = 230400;
|
---|
329 | break;
|
---|
330 | #endif
|
---|
331 | default:
|
---|
332 | props->output_bitrate = 1200;
|
---|
333 | break;
|
---|
334 | }
|
---|
335 |
|
---|
336 | i_speed = cfgetispeed (&currtio);
|
---|
337 |
|
---|
338 | switch (i_speed)
|
---|
339 | {
|
---|
340 | #ifdef B0
|
---|
341 | case B0:
|
---|
342 | props->input_bitrate = 0;
|
---|
343 | break;
|
---|
344 | #endif
|
---|
345 | #ifdef B50
|
---|
346 | case B50:
|
---|
347 | props->input_bitrate = 50;
|
---|
348 | break;
|
---|
349 | #endif
|
---|
350 | #ifdef B75
|
---|
351 | case B75:
|
---|
352 | props->input_bitrate = 75;
|
---|
353 | break;
|
---|
354 | #endif
|
---|
355 | #ifdef B110
|
---|
356 | case B110:
|
---|
357 | props->input_bitrate = 110;
|
---|
358 | break;
|
---|
359 | #endif
|
---|
360 | #ifdef B134
|
---|
361 | case B134:
|
---|
362 | props->input_bitrate = 134;
|
---|
363 | break;
|
---|
364 | #endif
|
---|
365 | #ifdef B150
|
---|
366 | case B150:
|
---|
367 | props->input_bitrate = 150;
|
---|
368 | break;
|
---|
369 | #endif
|
---|
370 | #ifdef B200
|
---|
371 | case B200:
|
---|
372 | props->input_bitrate = 200;
|
---|
373 | break;
|
---|
374 | #endif
|
---|
375 | #ifdef B300
|
---|
376 | case B300:
|
---|
377 | props->input_bitrate = 300;
|
---|
378 | break;
|
---|
379 | #endif
|
---|
380 | #ifdef B600
|
---|
381 | case B600:
|
---|
382 | props->input_bitrate = 600;
|
---|
383 | break;
|
---|
384 | #endif
|
---|
385 | #ifdef B1200
|
---|
386 | case B1200:
|
---|
387 | props->input_bitrate = 1200;
|
---|
388 | break;
|
---|
389 | #endif
|
---|
390 | #ifdef B1800
|
---|
391 | case B1800:
|
---|
392 | props->input_bitrate = 1800;
|
---|
393 | break;
|
---|
394 | #endif
|
---|
395 | #ifdef B2400
|
---|
396 | case B2400:
|
---|
397 | props->input_bitrate = 2400;
|
---|
398 | break;
|
---|
399 | #endif
|
---|
400 | #ifdef B4800
|
---|
401 | case B4800:
|
---|
402 | props->input_bitrate = 4800;
|
---|
403 | break;
|
---|
404 | #endif
|
---|
405 | #ifdef B9600
|
---|
406 | case B9600:
|
---|
407 | props->input_bitrate = 9600;
|
---|
408 | break;
|
---|
409 | #endif
|
---|
410 | #ifdef B19200
|
---|
411 | case B19200:
|
---|
412 | props->input_bitrate = 19200;
|
---|
413 | break;
|
---|
414 | #endif
|
---|
415 | #ifdef B38400
|
---|
416 | case B38400:
|
---|
417 | props->input_bitrate = 38400;
|
---|
418 | break;
|
---|
419 | #endif
|
---|
420 | #ifdef B57600
|
---|
421 | case B57600:
|
---|
422 | props->input_bitrate = 57600;
|
---|
423 | break;
|
---|
424 | #endif
|
---|
425 | #ifdef B115200
|
---|
426 | case B115200:
|
---|
427 | props->input_bitrate = 115200;
|
---|
428 | break;
|
---|
429 | #endif
|
---|
430 | #ifdef B230400
|
---|
431 | case B230400:
|
---|
432 | props->input_bitrate = 230400;
|
---|
433 | break;
|
---|
434 | #endif
|
---|
435 | default:
|
---|
436 | props->input_bitrate = 1200;
|
---|
437 | break;
|
---|
438 | }
|
---|
439 |
|
---|
440 | switch (currtio.c_cflag & CSIZE)
|
---|
441 | {
|
---|
442 | case CS5:
|
---|
443 | props->bits = 5;
|
---|
444 | break;
|
---|
445 | case CS6:
|
---|
446 | props->bits = 6;
|
---|
447 | break;
|
---|
448 | case CS7:
|
---|
449 | props->bits = 7;
|
---|
450 | break;
|
---|
451 | case CS8:
|
---|
452 | props->bits = 8;
|
---|
453 | break;
|
---|
454 | }
|
---|
455 |
|
---|
456 | if (((currtio.c_cflag) & PARENB) == PARENB)
|
---|
457 | {
|
---|
458 | if (((currtio.c_cflag) & PARODD) == PARODD)
|
---|
459 | props->parity = IO_SERIAL_PARITY_ODD;
|
---|
460 | else
|
---|
461 | props->parity = IO_SERIAL_PARITY_EVEN;
|
---|
462 | }
|
---|
463 | else
|
---|
464 | {
|
---|
465 | props->parity = IO_SERIAL_PARITY_NONE;
|
---|
466 | }
|
---|
467 |
|
---|
468 | if (((currtio.c_cflag) & CSTOPB) == CSTOPB)
|
---|
469 | props->stopbits = 2;
|
---|
470 | else
|
---|
471 | props->stopbits = 1;
|
---|
472 |
|
---|
473 | if (ioctl (io->fd, TIOCMGET, &mctl) < 0)
|
---|
474 | return FALSE;
|
---|
475 |
|
---|
476 | props->dtr = ((mctl & TIOCM_DTR) ? IO_SERIAL_HIGH : IO_SERIAL_LOW);
|
---|
477 | props->rts = ((mctl & TIOCM_RTS) ? IO_SERIAL_HIGH : IO_SERIAL_LOW);
|
---|
478 |
|
---|
479 | IO_Serial_SetPropertiesCache (io, props);
|
---|
480 |
|
---|
481 | #ifdef DEBUG_IO
|
---|
482 | printf("IO: Getting properties: %ld bps; %d bits/byte; %s parity; %d stopbits; dtr=%d; rts=%d\n", props->input_bitrate, props->bits, props->parity == IO_SERIAL_PARITY_EVEN ? "Even" : props->parity == IO_SERIAL_PARITY_ODD ? "Odd" : "None", props->stopbits, props->dtr, props->rts);
|
---|
483 | #endif
|
---|
484 |
|
---|
485 | return TRUE;
|
---|
486 | }
|
---|
487 |
|
---|
488 | bool IO_Serial_SetProperties (IO_Serial * io, IO_Serial_Properties * props)
|
---|
489 | {
|
---|
490 | struct termios newtio;
|
---|
491 | unsigned int modembits;
|
---|
492 |
|
---|
493 | #ifdef SCI_DEV
|
---|
494 | if(io->com==RTYP_SCI)
|
---|
495 | return FALSE;
|
---|
496 | #endif
|
---|
497 |
|
---|
498 | // printf("IO: Setting properties: com%d, %ld bps; %d bits/byte; %s parity; %d stopbits; dtr=%d; rts=%d\n", io->com, props->input_bitrate, props->bits, props->parity == IO_SERIAL_PARITY_EVEN ? "Even" : props->parity == IO_SERIAL_PARITY_ODD ? "Odd" : "None", props->stopbits, props->dtr, props->rts);
|
---|
499 | memset (&newtio, 0, sizeof (newtio));
|
---|
500 | /* Set the bitrate */
|
---|
501 | cfsetospeed(&newtio, IO_Serial_Bitrate(props->output_bitrate));
|
---|
502 | cfsetispeed(&newtio, IO_Serial_Bitrate(props->input_bitrate));
|
---|
503 |
|
---|
504 | /* Set the character size */
|
---|
505 | switch (props->bits)
|
---|
506 | {
|
---|
507 |
|
---|
508 | case 5:
|
---|
509 | newtio.c_cflag |= CS5;
|
---|
510 | break;
|
---|
511 |
|
---|
512 | case 6:
|
---|
513 | newtio.c_cflag |= CS6;
|
---|
514 | break;
|
---|
515 |
|
---|
516 | case 7:
|
---|
517 | newtio.c_cflag |= CS7;
|
---|
518 | break;
|
---|
519 |
|
---|
520 | case 8:
|
---|
521 | newtio.c_cflag |= CS8;
|
---|
522 | break;
|
---|
523 | }
|
---|
524 |
|
---|
525 | /* Set the parity */
|
---|
526 | switch (props->parity)
|
---|
527 | {
|
---|
528 | case IO_SERIAL_PARITY_ODD:
|
---|
529 | newtio.c_cflag |= PARENB;
|
---|
530 | newtio.c_cflag |= PARODD;
|
---|
531 | break;
|
---|
532 |
|
---|
533 | case IO_SERIAL_PARITY_EVEN:
|
---|
534 | newtio.c_cflag |= PARENB;
|
---|
535 | newtio.c_cflag &= ~PARODD;
|
---|
536 | break;
|
---|
537 |
|
---|
538 | case IO_SERIAL_PARITY_NONE:
|
---|
539 | newtio.c_cflag &= ~PARENB;
|
---|
540 | break;
|
---|
541 | }
|
---|
542 |
|
---|
543 | /* Set the number of stop bits */
|
---|
544 | switch (props->stopbits)
|
---|
545 | {
|
---|
546 | case 1:
|
---|
547 | newtio.c_cflag &= (~CSTOPB);
|
---|
548 | break;
|
---|
549 | case 2:
|
---|
550 | newtio.c_cflag |= CSTOPB;
|
---|
551 | break;
|
---|
552 | }
|
---|
553 |
|
---|
554 | /* Selects raw (non-canonical) input and output */
|
---|
555 | newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
|
---|
556 | newtio.c_oflag &= ~OPOST;
|
---|
557 | #if 1
|
---|
558 | newtio.c_iflag |= IGNPAR;
|
---|
559 | /* Ignore parity errors!!! Windows driver does so why shouldn't I? */
|
---|
560 | #endif
|
---|
561 | /* Enable receiber, hang on close, ignore control line */
|
---|
562 | newtio.c_cflag |= CREAD | HUPCL | CLOCAL;
|
---|
563 |
|
---|
564 | /* Read 1 byte minimun, no timeout specified */
|
---|
565 | newtio.c_cc[VMIN] = 1;
|
---|
566 | newtio.c_cc[VTIME] = 0;
|
---|
567 |
|
---|
568 | // tcdrain(io->fd);
|
---|
569 | if (tcsetattr (io->fd, TCSANOW, &newtio) < 0)
|
---|
570 | return FALSE;
|
---|
571 | // tcflush(io->fd, TCIOFLUSH);
|
---|
572 | // if (tcsetattr (io->fd, TCSAFLUSH, &newtio) < 0)
|
---|
573 | // return FALSE;
|
---|
574 |
|
---|
575 | IO_Serial_Ioctl_Lock(io, 1);
|
---|
576 | IO_Serial_DTR_RTS(io, 0, props->rts == IO_SERIAL_HIGH);
|
---|
577 | IO_Serial_DTR_RTS(io, 1, props->dtr == IO_SERIAL_HIGH);
|
---|
578 | IO_Serial_Ioctl_Lock(io, 0);
|
---|
579 |
|
---|
580 | IO_Serial_SetPropertiesCache (io, props);
|
---|
581 |
|
---|
582 | #ifdef DEBUG_IO
|
---|
583 | printf("IO: Setting properties: com%d, %ld bps; %d bits/byte; %s parity; %d stopbits; dtr=%d; rts=%d\n", io->com, props->input_bitrate, props->bits, props->parity == IO_SERIAL_PARITY_EVEN ? "Even" : props->parity == IO_SERIAL_PARITY_ODD ? "Odd" : "None", props->stopbits, props->dtr, props->rts);
|
---|
584 | #endif
|
---|
585 | return TRUE;
|
---|
586 | }
|
---|
587 |
|
---|
588 | void IO_Serial_Flush (IO_Serial * io)
|
---|
589 | {
|
---|
590 | BYTE b;
|
---|
591 | while(IO_Serial_Read(io, 1000, 1, &b));
|
---|
592 |
|
---|
593 | }
|
---|
594 |
|
---|
595 |
|
---|
596 | void IO_Serial_GetPnPId (IO_Serial * io, BYTE * pnp_id, unsigned *length)
|
---|
597 | {
|
---|
598 | (*length) = io->PnP_id_size;
|
---|
599 | memcpy (pnp_id, io->PnP_id, io->PnP_id_size);
|
---|
600 | }
|
---|
601 |
|
---|
602 | unsigned IO_Serial_GetCom (IO_Serial * io)
|
---|
603 | {
|
---|
604 | return io->com;
|
---|
605 | }
|
---|
606 |
|
---|
607 |
|
---|
608 | bool IO_Serial_Read (IO_Serial * io, unsigned timeout, unsigned size, BYTE * data)
|
---|
609 | {
|
---|
610 | BYTE c;
|
---|
611 | int count = 0;
|
---|
612 |
|
---|
613 |
|
---|
614 | if((io->com!=RTYP_SCI) && (io->wr>0))
|
---|
615 | {
|
---|
616 | BYTE buf[256];
|
---|
617 | int n = io->wr;
|
---|
618 | io->wr = 0;
|
---|
619 |
|
---|
620 | if(!IO_Serial_Read (io, timeout, n, buf))
|
---|
621 | {
|
---|
622 | return FALSE;
|
---|
623 | }
|
---|
624 | }
|
---|
625 |
|
---|
626 | #ifdef DEBUG_IO
|
---|
627 | printf ("IO: Receiving: ");
|
---|
628 | fflush (stdout);
|
---|
629 | #endif
|
---|
630 | for (count = 0; count < size; count++)
|
---|
631 | {
|
---|
632 | if (IO_Serial_WaitToRead (io->fd, 0, timeout))
|
---|
633 | {
|
---|
634 | if (read (io->fd, &c, 1) != 1)
|
---|
635 | {
|
---|
636 | #ifdef DEBUG_IO
|
---|
637 | printf ("ERROR\n");
|
---|
638 | fflush (stdout);
|
---|
639 | #endif
|
---|
640 | return FALSE;
|
---|
641 | }
|
---|
642 | data[count] = c;
|
---|
643 |
|
---|
644 | #ifdef DEBUG_IO
|
---|
645 | printf ("%X ", c);
|
---|
646 | fflush (stdout);
|
---|
647 | #endif
|
---|
648 | }
|
---|
649 | else
|
---|
650 | {
|
---|
651 | #ifdef DEBUG_IO
|
---|
652 | printf ("TIMEOUT\n");
|
---|
653 | fflush (stdout);
|
---|
654 | #endif
|
---|
655 | tcflush (io->fd, TCIFLUSH);
|
---|
656 | return FALSE;
|
---|
657 | }
|
---|
658 | }
|
---|
659 |
|
---|
660 | #ifdef DEBUG_IO
|
---|
661 | printf ("\n");
|
---|
662 | fflush (stdout);
|
---|
663 | #endif
|
---|
664 |
|
---|
665 | return TRUE;
|
---|
666 | }
|
---|
667 |
|
---|
668 | bool IO_Serial_Write (IO_Serial * io, unsigned delay, unsigned size, BYTE * data)
|
---|
669 | {
|
---|
670 | unsigned count, to_send;
|
---|
671 | #ifdef DEBUG_IO
|
---|
672 | unsigned i;
|
---|
673 |
|
---|
674 | printf ("IO: Sending: ");
|
---|
675 | fflush (stdout);
|
---|
676 | #endif
|
---|
677 | /* Discard input data from previous commands */
|
---|
678 | // tcflush (io->fd, TCIFLUSH);
|
---|
679 |
|
---|
680 | for (count = 0; count < size; count += to_send)
|
---|
681 | {
|
---|
682 | // if(io->com==RTYP_SCI)
|
---|
683 | // to_send = 1;
|
---|
684 | // else
|
---|
685 | to_send = (delay? 1: size);
|
---|
686 |
|
---|
687 | if (IO_Serial_WaitToWrite (io, delay, 1000))
|
---|
688 | {
|
---|
689 | unsigned int u = write (io->fd, data + count, to_send);
|
---|
690 | if (u != to_send)
|
---|
691 | {
|
---|
692 | #ifdef DEBUG_IO
|
---|
693 | printf ("ERROR\n");
|
---|
694 | fflush (stdout);
|
---|
695 | #endif
|
---|
696 | if(io->com!=RTYP_SCI)
|
---|
697 | io->wr += u;
|
---|
698 | return FALSE;
|
---|
699 | }
|
---|
700 |
|
---|
701 | if(io->com!=RTYP_SCI)
|
---|
702 | io->wr += to_send;
|
---|
703 |
|
---|
704 | #ifdef DEBUG_IO
|
---|
705 | for (i=0; i<to_send; i++)
|
---|
706 | printf ("%X ", data[count + i]);
|
---|
707 | fflush (stdout);
|
---|
708 | #endif
|
---|
709 | }
|
---|
710 | else
|
---|
711 | {
|
---|
712 | #ifdef DEBUG_IO
|
---|
713 | printf ("TIMEOUT\n");
|
---|
714 | fflush (stdout);
|
---|
715 | #endif
|
---|
716 | // tcflush (io->fd, TCIFLUSH);
|
---|
717 | return FALSE;
|
---|
718 | }
|
---|
719 | }
|
---|
720 |
|
---|
721 | #ifdef DEBUG_IO
|
---|
722 | printf ("\n");
|
---|
723 | fflush (stdout);
|
---|
724 | #endif
|
---|
725 |
|
---|
726 | return TRUE;
|
---|
727 | }
|
---|
728 |
|
---|
729 | bool IO_Serial_Close (IO_Serial * io)
|
---|
730 | {
|
---|
731 | char filename[IO_SERIAL_FILENAME_LENGTH];
|
---|
732 |
|
---|
733 | IO_Serial_DeviceName (io->com, io->usbserial, filename, IO_SERIAL_FILENAME_LENGTH);
|
---|
734 |
|
---|
735 | #ifdef DEBUG_IO
|
---|
736 | printf ("IO: Clossing serial port %s\n", filename);
|
---|
737 | #endif
|
---|
738 |
|
---|
739 | #if defined(TUXBOX) && defined(PPC)
|
---|
740 | close(fdmc);
|
---|
741 | #endif
|
---|
742 | if (close (io->fd) != 0)
|
---|
743 | return FALSE;
|
---|
744 |
|
---|
745 | IO_Serial_ClearPropertiesCache (io);
|
---|
746 | IO_Serial_Clear (io);
|
---|
747 |
|
---|
748 | return TRUE;
|
---|
749 | }
|
---|
750 |
|
---|
751 | void IO_Serial_Delete (IO_Serial * io)
|
---|
752 | {
|
---|
753 | if (io->props != NULL)
|
---|
754 | free (io->props);
|
---|
755 |
|
---|
756 | free (io);
|
---|
757 | }
|
---|
758 |
|
---|
759 | /*
|
---|
760 | * Internal functions definition
|
---|
761 | */
|
---|
762 |
|
---|
763 | static int IO_Serial_Bitrate(int bitrate)
|
---|
764 | {
|
---|
765 | #ifdef B230400
|
---|
766 | if ((bitrate)>=230400) return B230400;
|
---|
767 | #endif
|
---|
768 | #ifdef B115200
|
---|
769 | if ((bitrate)>=115200) return B115200;
|
---|
770 | #endif
|
---|
771 | #ifdef B57600
|
---|
772 | if ((bitrate)>=57600) return B57600;
|
---|
773 | #endif
|
---|
774 | #ifdef B38400
|
---|
775 | if ((bitrate)>=38400) return B38400;
|
---|
776 | #endif
|
---|
777 | #ifdef B19200
|
---|
778 | if ((bitrate)>=19200) return B19200;
|
---|
779 | #endif
|
---|
780 | #ifdef B9600
|
---|
781 | if ((bitrate)>=9600) return B9600;
|
---|
782 | #endif
|
---|
783 | #ifdef B4800
|
---|
784 | if ((bitrate)>=4800) return B4800;
|
---|
785 | #endif
|
---|
786 | #ifdef B2400
|
---|
787 | if ((bitrate)>=2400) return B2400;
|
---|
788 | #endif
|
---|
789 | #ifdef B1800
|
---|
790 | if ((bitrate)>=1800) return B1800;
|
---|
791 | #endif
|
---|
792 | #ifdef B1200
|
---|
793 | if ((bitrate)>=1200) return B1200;
|
---|
794 | #endif
|
---|
795 | #ifdef B600
|
---|
796 | if ((bitrate)>=600) return B600;
|
---|
797 | #endif
|
---|
798 | #ifdef B300
|
---|
799 | if ((bitrate)>=300) return B300;
|
---|
800 | #endif
|
---|
801 | #ifdef B200
|
---|
802 | if ((bitrate)>=200) return B200;
|
---|
803 | #endif
|
---|
804 | #ifdef B150
|
---|
805 | if ((bitrate)>=150) return B150;
|
---|
806 | #endif
|
---|
807 | #ifdef B134
|
---|
808 | if ((bitrate)>=134) return B134;
|
---|
809 | #endif
|
---|
810 | #ifdef B110
|
---|
811 | if ((bitrate)>=110) return B110;
|
---|
812 | #endif
|
---|
813 | #ifdef B75
|
---|
814 | if ((bitrate)>=75) return B75;
|
---|
815 | #endif
|
---|
816 | #ifdef B50
|
---|
817 | if ((bitrate)>=50) return B50;
|
---|
818 | #endif
|
---|
819 | #ifdef B0
|
---|
820 | if ((bitrate)>=0) return B0;
|
---|
821 | #endif
|
---|
822 | return 0; /* Should never get here */
|
---|
823 | }
|
---|
824 |
|
---|
825 | static bool IO_Serial_WaitToRead (int hnd, unsigned delay_ms, unsigned timeout_ms)
|
---|
826 | {
|
---|
827 | int rval;
|
---|
828 | struct pollfd ufds;
|
---|
829 |
|
---|
830 | if (delay_ms > 0)
|
---|
831 | {
|
---|
832 | #ifdef HAVE_NANOSLEEP
|
---|
833 | struct timespec req_ts;
|
---|
834 |
|
---|
835 | req_ts.tv_sec = delay_ms / 1000;
|
---|
836 | req_ts.tv_nsec = (delay_ms % 1000) * 1000000L;
|
---|
837 | nanosleep (&req_ts, NULL);
|
---|
838 | #else
|
---|
839 | usleep (delay_ms * 1000L);
|
---|
840 | #endif
|
---|
841 | }
|
---|
842 |
|
---|
843 | ufds.fd = hnd;
|
---|
844 | ufds.events = POLLIN;
|
---|
845 | ufds.revents = 0x0000;
|
---|
846 |
|
---|
847 | rval = poll (&ufds, 1, timeout_ms);
|
---|
848 | if (rval != 1)
|
---|
849 | return (FALSE);
|
---|
850 |
|
---|
851 | return (((ufds.revents) & POLLIN) == POLLIN);
|
---|
852 | }
|
---|
853 |
|
---|
854 | static bool IO_Serial_WaitToWrite (IO_Serial *io, unsigned delay_ms, unsigned timeout_ms)
|
---|
855 | {
|
---|
856 | int rval;
|
---|
857 | struct pollfd ufds;
|
---|
858 |
|
---|
859 | #ifdef SCI_DEV
|
---|
860 | if(io->com==RTYP_SCI)
|
---|
861 | return TRUE;
|
---|
862 | #endif
|
---|
863 |
|
---|
864 | if (delay_ms > 0)
|
---|
865 | {
|
---|
866 | #ifdef HAVE_NANOSLEEP
|
---|
867 | struct timespec req_ts;
|
---|
868 |
|
---|
869 | req_ts.tv_sec = delay_ms / 1000;
|
---|
870 | req_ts.tv_nsec = (delay_ms % 1000) * 1000000L;
|
---|
871 | nanosleep (&req_ts, NULL);
|
---|
872 | #else
|
---|
873 | usleep (delay_ms * 1000L);
|
---|
874 | #endif
|
---|
875 | }
|
---|
876 |
|
---|
877 | ufds.fd = io->fd;
|
---|
878 | ufds.events = POLLOUT;
|
---|
879 | ufds.revents = 0x0000;
|
---|
880 |
|
---|
881 | rval = poll (&ufds, 1, timeout_ms);
|
---|
882 | if (rval != 1)
|
---|
883 | return (FALSE);
|
---|
884 |
|
---|
885 | return (((ufds.revents) & POLLOUT) == POLLOUT);
|
---|
886 | }
|
---|
887 |
|
---|
888 | static void IO_Serial_Clear (IO_Serial * io)
|
---|
889 | {
|
---|
890 | io->fd = -1;
|
---|
891 | io->props = NULL;
|
---|
892 | io->com = 0;
|
---|
893 | memset (io->PnP_id, 0, IO_SERIAL_PNPID_SIZE);
|
---|
894 | io->PnP_id_size = 0;
|
---|
895 | io->usbserial = FALSE;
|
---|
896 | io->wr = 0;
|
---|
897 | }
|
---|
898 |
|
---|
899 | static void IO_Serial_SetPropertiesCache(IO_Serial * io, IO_Serial_Properties * props)
|
---|
900 | {
|
---|
901 | #ifdef SCI_DEV
|
---|
902 | if(io->com==RTYP_SCI)
|
---|
903 | return;
|
---|
904 | #endif
|
---|
905 |
|
---|
906 | if (io->props == NULL)
|
---|
907 | io->props = (IO_Serial_Properties *) malloc (sizeof (IO_Serial_Properties));
|
---|
908 | #ifdef DEBUG_IO
|
---|
909 | printf ("IO: Catching properties\n");
|
---|
910 | #endif
|
---|
911 |
|
---|
912 | memcpy (io->props, props, sizeof (IO_Serial_Properties));
|
---|
913 | }
|
---|
914 |
|
---|
915 | static bool IO_Serial_GetPropertiesCache(IO_Serial * io, IO_Serial_Properties * props)
|
---|
916 | {
|
---|
917 | if (io->props != NULL)
|
---|
918 | {
|
---|
919 | memcpy (props, io->props, sizeof (IO_Serial_Properties));
|
---|
920 | #if 0
|
---|
921 | #ifdef DEBUG_IO
|
---|
922 | printf("IO: Getting properties (catched): %ld bps; %d bits/byte; %s parity; %d stopbits; dtr=%d; rts=%d\n", props->input_bitrate, props->bits, props->parity == IO_SERIAL_PARITY_EVEN ? "Even" : props->parity == IO_SERIAL_PARITY_ODD ? "Odd" : "None", props->stopbits, props->dtr, props->rts);
|
---|
923 | #endif
|
---|
924 | #endif
|
---|
925 | return TRUE;
|
---|
926 | }
|
---|
927 |
|
---|
928 | return FALSE;
|
---|
929 | }
|
---|
930 |
|
---|
931 | static void IO_Serial_ClearPropertiesCache (IO_Serial * io)
|
---|
932 | {
|
---|
933 | #ifdef DEBUG_IO
|
---|
934 | printf ("IO: Clearing properties cache\n");
|
---|
935 | #endif
|
---|
936 | if (io->props != NULL)
|
---|
937 | {
|
---|
938 | free (io->props);
|
---|
939 | io->props = NULL;
|
---|
940 | }
|
---|
941 | }
|
---|
942 |
|
---|
943 | static void IO_Serial_DeviceName (unsigned com, bool usbserial, char * filename, unsigned length)
|
---|
944 | {
|
---|
945 | extern char mpcs_device[];
|
---|
946 | snprintf (filename, length, "%s", mpcs_device);
|
---|
947 | // if(com==1)
|
---|
948 | // snprintf (filename, length, "/dev/tts/%d", com - 1);
|
---|
949 | // else
|
---|
950 | // snprintf (filename, length, "/dev/sci%d", com - 2);
|
---|
951 | }
|
---|
952 |
|
---|
953 | static bool IO_Serial_InitPnP (IO_Serial * io)
|
---|
954 | {
|
---|
955 | IO_Serial_Properties props;
|
---|
956 | int i = 0;
|
---|
957 | props.input_bitrate = 1200;
|
---|
958 | props.output_bitrate = 1200;
|
---|
959 | props.parity = IO_SERIAL_PARITY_NONE;
|
---|
960 | props.bits = 7;
|
---|
961 | props.stopbits = 1;
|
---|
962 | props.dtr = IO_SERIAL_HIGH;
|
---|
963 | // props.rts = IO_SERIAL_HIGH;
|
---|
964 | props.rts = IO_SERIAL_LOW;
|
---|
965 |
|
---|
966 | if (!IO_Serial_SetProperties (io, &props))
|
---|
967 | return FALSE;
|
---|
968 |
|
---|
969 | while ((i < IO_SERIAL_PNPID_SIZE) && IO_Serial_Read (io, 200, 1, &(io->PnP_id[i])))
|
---|
970 | i++;
|
---|
971 |
|
---|
972 | io->PnP_id_size = i;
|
---|
973 | return TRUE;
|
---|
974 | }
|
---|