source: branches/smartreader/csctapi/io_serial.c@ 1239

Last change on this file since 1239 was 1239, checked in by rorothetroll, 11 years ago

resync with trunk

File size: 21.6 KB
Line 
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 "../globals.h"
27#include <stdio.h>
28#include <string.h>
29#include <stdlib.h>
30#include <errno.h>
31#ifdef OS_HPUX
32#include <sys/modem.h>
33#endif
34#include <termios.h>
35#include <unistd.h>
36#include <sys/stat.h>
37#include <fcntl.h>
38#ifdef HAVE_POLL
39#include <sys/poll.h>
40#else
41#include <sys/signal.h>
42#include <sys/types.h>
43#endif
44#include <sys/time.h>
45#include <sys/ioctl.h>
46#include <time.h>
47#include "io_serial.h"
48#include "mc_global.h"
49
50#ifdef OS_LINUX
51#include <linux/serial.h>
52#endif
53
54#define IO_SERIAL_FILENAME_LENGTH 32
55
56/*
57 * Internal functions declaration
58 */
59
60static int IO_Serial_Bitrate(int bitrate);
61
62static bool IO_Serial_WaitToRead (unsigned delay_ms, unsigned timeout_ms);
63
64static bool IO_Serial_WaitToWrite (unsigned delay_ms, unsigned timeout_ms);
65
66static bool IO_Serial_InitPnP (IO_Serial * io);
67
68static void IO_Serial_Clear (IO_Serial * io);
69
70static int _in_echo_read = 0;
71int io_serial_need_dummy_char = 0;
72
73extern int fdmc;
74
75#if defined(TUXBOX) && defined(PPC)
76void IO_Serial_Ioctl_Lock(int flag)
77{
78 extern int *oscam_sem;
79 if ((reader[ridx].typ != R_DB2COM1) && (reader[ridx].typ != R_DB2COM2)) return;
80 if (!flag)
81 *oscam_sem=0;
82 else while (*oscam_sem!=reader[ridx].typ)
83 {
84 while (*oscam_sem)
85 usleep((reader[ridx].typ)*2000); //FIXME is this right ?!?!
86 *oscam_sem=reader[ridx].typ;
87 usleep(1000);
88 }
89}
90
91static bool IO_Serial_DTR_RTS_dbox2(int mcport, int dtr, int set)
92{
93 int rc;
94 unsigned short msr;
95 unsigned int mbit;
96 unsigned short rts_bits[2]={ 0x10, 0x800};
97 unsigned short dtr_bits[2]={0x100, 0};
98
99#ifdef DEBUG_IO
100printf("IO: multicam.o %s %s\n", dtr ? "dtr" : "rts", set ? "set" : "clear"); fflush(stdout);
101#endif
102 if ((rc=ioctl(fdmc, GET_PCDAT, &msr))>=0)
103 {
104 if (dtr) // DTR
105 {
106 if (dtr_bits[mcport])
107 {
108 if (set)
109 msr&=(unsigned short)(~dtr_bits[mcport]);
110 else
111 msr|=dtr_bits[mcport];
112 rc=ioctl(fdmc, SET_PCDAT, &msr);
113 }
114 else
115 rc=0; // Dummy, can't handle using multicam.o
116 }
117 else // RTS
118 {
119 if (set)
120 msr&=(unsigned short)(~rts_bits[mcport]);
121 else
122 msr|=rts_bits[mcport];
123 rc=ioctl(fdmc, SET_PCDAT, &msr);
124 }
125 }
126 return((rc<0) ? FALSE : TRUE);
127}
128#endif
129
130bool IO_Serial_DTR_RTS(int dtr, int set)
131{
132 unsigned int msr;
133 unsigned int mbit;
134
135#if defined(TUXBOX) && defined(PPC)
136 if ((reader[ridx].typ == R_DB2COM1) || (reader[ridx].typ == R_DB2COM2))
137 return(IO_Serial_DTR_RTS_dbox2(reader[ridx].typ == R_DB2COM2, dtr, set));
138#endif
139
140 mbit=(dtr) ? TIOCM_DTR : TIOCM_RTS;
141#if defined(TIOCMBIS) && defined(TIOBMBIC)
142 if (ioctl (reader[ridx].handle, set ? TIOCMBIS : TIOCMBIC, &mbit) < 0)
143 return FALSE;
144#else
145 if (ioctl(reader[ridx].handle, TIOCMGET, &msr) < 0)
146 return FALSE;
147 if (set)
148 msr|=mbit;
149 else
150 msr&=~mbit;
151 return((ioctl(reader[ridx].handle, TIOCMSET, &msr)<0) ? FALSE : TRUE);
152#endif
153}
154
155/*
156 * Public functions definition
157 */
158
159IO_Serial * IO_Serial_New (int mhz, int cardmhz)
160{
161 IO_Serial *io;
162
163 io = (IO_Serial *) malloc (sizeof (IO_Serial));
164
165 if (io != NULL)
166 IO_Serial_Clear (io);
167
168 return io;
169}
170
171bool IO_Serial_Init (IO_Serial * io, int reader_type)
172{
173 if (reader[ridx].typ != R_INTERNAL)
174 IO_Serial_InitPnP (io);
175
176 if(reader[ridx].typ!=R_INTERNAL)
177 IO_Serial_Flush();
178
179 return TRUE;
180}
181
182bool IO_Serial_GetPropertiesOld (IO_Serial * io)
183{
184 struct termios currtio;
185 speed_t i_speed, o_speed;
186 unsigned int mctl;
187
188#ifdef SCI_DEV
189 if(reader[ridx].typ == R_INTERNAL)
190 return FALSE;
191#endif
192
193 if (io->input_bitrate != 0 && io->output_bitrate != 0) //properties are already filled
194 return TRUE;
195
196 if (tcgetattr (reader[ridx].handle, &currtio) != 0)
197 return FALSE;
198
199 o_speed = cfgetospeed (&currtio);
200
201 switch (o_speed)
202 {
203#ifdef B0
204 case B0:
205 io->output_bitrate = 0;
206 break;
207#endif
208#ifdef B50
209 case B50:
210 io->output_bitrate = 50;
211 break;
212#endif
213#ifdef B75
214 case B75:
215 io->output_bitrate = 75;
216 break;
217#endif
218#ifdef B110
219 case B110:
220 io->output_bitrate = 110;
221 break;
222#endif
223#ifdef B134
224 case B134:
225 io->output_bitrate = 134;
226 break;
227#endif
228#ifdef B150
229 case B150:
230 io->output_bitrate = 150;
231 break;
232#endif
233#ifdef B200
234 case B200:
235 io->output_bitrate = 200;
236 break;
237#endif
238#ifdef B300
239 case B300:
240 io->output_bitrate = 300;
241 break;
242#endif
243#ifdef B600
244 case B600:
245 io->output_bitrate = 600;
246 break;
247#endif
248#ifdef B1200
249 case B1200:
250 io->output_bitrate = 1200;
251 break;
252#endif
253#ifdef B1800
254 case B1800:
255 io->output_bitrate = 1800;
256 break;
257#endif
258#ifdef B2400
259 case B2400:
260 io->output_bitrate = 2400;
261 break;
262#endif
263#ifdef B4800
264 case B4800:
265 io->output_bitrate = 4800;
266 break;
267#endif
268#ifdef B9600
269 case B9600:
270 io->output_bitrate = 9600;
271 break;
272#endif
273#ifdef B19200
274 case B19200:
275 io->output_bitrate = 19200;
276 break;
277#endif
278#ifdef B38400
279 case B38400:
280 io->output_bitrate = 38400;
281 break;
282#endif
283#ifdef B57600
284 case B57600:
285 io->output_bitrate = 57600;
286 break;
287#endif
288#ifdef B115200
289 case B115200:
290 io->output_bitrate = 115200;
291 break;
292#endif
293#ifdef B230400
294 case B230400:
295 io->output_bitrate = 230400;
296 break;
297#endif
298 default:
299 io->output_bitrate = 1200;
300 break;
301 }
302
303 i_speed = cfgetispeed (&currtio);
304
305 switch (i_speed)
306 {
307#ifdef B0
308 case B0:
309 io->input_bitrate = 0;
310 break;
311#endif
312#ifdef B50
313 case B50:
314 io->input_bitrate = 50;
315 break;
316#endif
317#ifdef B75
318 case B75:
319 io->input_bitrate = 75;
320 break;
321#endif
322#ifdef B110
323 case B110:
324 io->input_bitrate = 110;
325 break;
326#endif
327#ifdef B134
328 case B134:
329 io->input_bitrate = 134;
330 break;
331#endif
332#ifdef B150
333 case B150:
334 io->input_bitrate = 150;
335 break;
336#endif
337#ifdef B200
338 case B200:
339 io->input_bitrate = 200;
340 break;
341#endif
342#ifdef B300
343 case B300:
344 io->input_bitrate = 300;
345 break;
346#endif
347#ifdef B600
348 case B600:
349 io->input_bitrate = 600;
350 break;
351#endif
352#ifdef B1200
353 case B1200:
354 io->input_bitrate = 1200;
355 break;
356#endif
357#ifdef B1800
358 case B1800:
359 io->input_bitrate = 1800;
360 break;
361#endif
362#ifdef B2400
363 case B2400:
364 io->input_bitrate = 2400;
365 break;
366#endif
367#ifdef B4800
368 case B4800:
369 io->input_bitrate = 4800;
370 break;
371#endif
372#ifdef B9600
373 case B9600:
374 io->input_bitrate = 9600;
375 break;
376#endif
377#ifdef B19200
378 case B19200:
379 io->input_bitrate = 19200;
380 break;
381#endif
382#ifdef B38400
383 case B38400:
384 io->input_bitrate = 38400;
385 break;
386#endif
387#ifdef B57600
388 case B57600:
389 io->input_bitrate = 57600;
390 break;
391#endif
392#ifdef B115200
393 case B115200:
394 io->input_bitrate = 115200;
395 break;
396#endif
397#ifdef B230400
398 case B230400:
399 io->input_bitrate = 230400;
400 break;
401#endif
402 default:
403 io->input_bitrate = 1200;
404 break;
405 }
406
407 switch (currtio.c_cflag & CSIZE)
408 {
409 case CS5:
410 io->bits = 5;
411 break;
412 case CS6:
413 io->bits = 6;
414 break;
415 case CS7:
416 io->bits = 7;
417 break;
418 case CS8:
419 io->bits = 8;
420 break;
421 }
422
423 if (((currtio.c_cflag) & PARENB) == PARENB)
424 {
425 if (((currtio.c_cflag) & PARODD) == PARODD)
426 io->parity = PARITY_ODD;
427 else
428 io->parity = PARITY_EVEN;
429 }
430 else
431 {
432 io->parity = PARITY_NONE;
433 }
434
435 if (((currtio.c_cflag) & CSTOPB) == CSTOPB)
436 io->stopbits = 2;
437 else
438 io->stopbits = 1;
439
440 if (ioctl (reader[ridx].handle, TIOCMGET, &mctl) < 0)
441 return FALSE;
442
443 io->dtr = ((mctl & TIOCM_DTR) ? IO_SERIAL_HIGH : IO_SERIAL_LOW);
444 io->rts = ((mctl & TIOCM_RTS) ? IO_SERIAL_HIGH : IO_SERIAL_LOW);
445
446#ifdef DEBUG_IO
447 printf("IO: Getting properties: %ld bps; %d bits/byte; %s parity; %d stopbits; dtr=%d; rts=%d\n", io->input_bitrate, io->bits, io->parity == PARITY_EVEN ? "Even" : io->parity == PARITY_ODD ? "Odd" : "None", io->stopbits, io->dtr, io->rts);
448#endif
449
450 return TRUE;
451}
452
453bool IO_Serial_SetPropertiesOld (IO_Serial * io)
454{
455 struct termios newtio;
456
457#ifdef SCI_DEV
458 if(reader[ridx].typ == R_INTERNAL)
459 return FALSE;
460#endif
461
462 // printf("IO: Setting properties: reader_type%d, %ld bps; %d bits/byte; %s parity; %d stopbits; dtr=%d; rts=%d\n", reader[ridx].typ, io->input_bitrate, io->bits, io->parity == PARITY_EVEN ? "Even" : io->parity == PARITY_ODD ? "Odd" : "None", io->stopbits, io->dtr, io->rts);
463 memset (&newtio, 0, sizeof (newtio));
464
465
466 /* Set the bitrate */
467#ifdef OS_LINUX
468 if (reader[ridx].mhz == reader[ridx].cardmhz)
469#endif
470 { //no overclocking
471 cfsetospeed(&newtio, IO_Serial_Bitrate(io->output_bitrate));
472 cfsetispeed(&newtio, IO_Serial_Bitrate(io->input_bitrate));
473 cs_debug("standard baudrate: cardmhz=%d mhz=%d -> effective baudrate %lu", reader[ridx].cardmhz, reader[ridx].mhz, io->output_bitrate);
474 }
475#ifdef OS_LINUX
476 else { //over or underclocking
477 /* these structures are only available on linux as fas as we know so limit this code to OS_LINUX */
478 struct serial_struct nuts;
479 ioctl(reader[ridx].handle, TIOCGSERIAL, &nuts);
480 int custom_baud = io->output_bitrate * reader[ridx].mhz / reader[ridx].cardmhz;
481 nuts.custom_divisor = (nuts.baud_base + (custom_baud/2))/ custom_baud;
482 cs_debug("custom baudrate: cardmhz=%d mhz=%d custom_baud=%d baud_base=%d divisor=%d -> effective baudrate %d",
483 reader[ridx].cardmhz, reader[ridx].mhz, custom_baud, nuts.baud_base, nuts.custom_divisor, nuts.baud_base/nuts.custom_divisor);
484 nuts.flags &= ~ASYNC_SPD_MASK;
485 nuts.flags |= ASYNC_SPD_CUST;
486 ioctl(reader[ridx].handle, TIOCSSERIAL, &nuts);
487 cfsetospeed(&newtio, IO_Serial_Bitrate(38400));
488 cfsetispeed(&newtio, IO_Serial_Bitrate(38400));
489 }
490#endif
491
492 /* Set the character size */
493 switch (io->bits)
494 {
495 case 5:
496 newtio.c_cflag |= CS5;
497 break;
498
499 case 6:
500 newtio.c_cflag |= CS6;
501 break;
502
503 case 7:
504 newtio.c_cflag |= CS7;
505 break;
506
507 case 8:
508 newtio.c_cflag |= CS8;
509 break;
510 }
511
512 /* Set the parity */
513 switch (io->parity)
514 {
515 case PARITY_ODD:
516 newtio.c_cflag |= PARENB;
517 newtio.c_cflag |= PARODD;
518 break;
519
520 case PARITY_EVEN:
521 newtio.c_cflag |= PARENB;
522 newtio.c_cflag &= ~PARODD;
523 break;
524
525 case PARITY_NONE:
526 newtio.c_cflag &= ~PARENB;
527 break;
528 }
529
530 /* Set the number of stop bits */
531 switch (io->stopbits)
532 {
533 case 1:
534 newtio.c_cflag &= (~CSTOPB);
535 break;
536 case 2:
537 newtio.c_cflag |= CSTOPB;
538 break;
539 }
540
541 /* Selects raw (non-canonical) input and output */
542 newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
543 newtio.c_oflag &= ~OPOST;
544#if 1
545 newtio.c_iflag |= IGNPAR;
546 /* Ignore parity errors!!! Windows driver does so why shouldn't I? */
547#endif
548 /* Enable receiber, hang on close, ignore control line */
549 newtio.c_cflag |= CREAD | HUPCL | CLOCAL;
550
551 /* Read 1 byte minimun, no timeout specified */
552 newtio.c_cc[VMIN] = 1;
553 newtio.c_cc[VTIME] = 0;
554
555// tcdrain(reader[ridx].handle);
556 if (tcsetattr (reader[ridx].handle, TCSANOW, &newtio) < 0)
557 return FALSE;
558// tcflush(reader[ridx].handle, TCIOFLUSH);
559// if (tcsetattr (reader[ridx].handle, TCSAFLUSH, &newtio) < 0)
560// return FALSE;
561
562 IO_Serial_Ioctl_Lock(1);
563 IO_Serial_DTR_RTS(0, io->rts == IO_SERIAL_HIGH);
564 IO_Serial_DTR_RTS(1, io->dtr == IO_SERIAL_HIGH);
565 IO_Serial_Ioctl_Lock(0);
566
567#ifdef DEBUG_IO
568 printf("IO: Setting properties: reader_type%d, %ld bps; %d bits/byte; %s parity; %d stopbits; dtr=%d; rts=%d\n", reader[ridx].typ, io->input_bitrate, io->bits, io->parity == PARITY_EVEN ? "Even" : io->parity == PARITY_ODD ? "Odd" : "None", io->stopbits, io->dtr, io->rts);
569#endif
570 return TRUE;
571}
572
573bool IO_Serial_SetProperties (struct termios newtio)
574{
575 if(reader[ridx].typ == R_INTERNAL)
576 return FALSE;
577
578 if (tcsetattr (reader[ridx].handle, TCSANOW, &newtio) < 0)
579 return FALSE;
580// tcflush(reader[ridx].handle, TCIOFLUSH);
581// if (tcsetattr (reader[ridx].handle, TCSAFLUSH, &newtio) < 0)
582// return FALSE;
583
584 unsigned int mctl;
585 if (ioctl (reader[ridx].handle, TIOCMGET, &mctl) < 0)
586 return FALSE;
587
588 int dtr = ((mctl & TIOCM_DTR) ? IO_SERIAL_HIGH : IO_SERIAL_LOW);
589 int rts = ((mctl & TIOCM_RTS) ? IO_SERIAL_HIGH : IO_SERIAL_LOW);
590
591 IO_Serial_Ioctl_Lock(1);
592 IO_Serial_DTR_RTS(0, rts == IO_SERIAL_HIGH);
593 IO_Serial_DTR_RTS(1, dtr == IO_SERIAL_HIGH);
594 IO_Serial_Ioctl_Lock(0);
595
596#ifdef DEBUG_IO
597 printf("IO: Setting properties\n");
598#endif
599
600 return TRUE;
601}
602
603int IO_Serial_SetParity (BYTE parity)
604{
605 if(reader[ridx].typ == R_INTERNAL)
606 return TRUE;
607
608 if ((parity != PARITY_EVEN) && (parity != PARITY_ODD) && (parity != PARITY_NONE))
609 return FALSE;
610
611 struct termios tio;
612 int current_parity;
613 // Get current parity
614 if (tcgetattr (reader[ridx].handle, &tio) != 0)
615 return FALSE;
616
617 if (((tio.c_cflag) & PARENB) == PARENB)
618 {
619 if (((tio.c_cflag) & PARODD) == PARODD)
620 current_parity = PARITY_ODD;
621 else
622 current_parity = PARITY_EVEN;
623 }
624 else
625 {
626 current_parity = PARITY_NONE;
627 }
628
629#ifdef DEBUG_IFD
630 printf ("IFD: Setting parity from %s to %s\n",
631 current_parity == PARITY_ODD ? "Odd" :
632 current_parity == PARITY_NONE ? "None" :
633 current_parity == PARITY_EVEN ? "Even" : "Invalid",
634 parity == PARITY_ODD ? "Odd" :
635 parity == PARITY_NONE ? "None" :
636 parity == PARITY_EVEN ? "Even" : "Invalid");
637#endif
638
639 if (current_parity != parity)
640 {
641
642 // Set the parity
643 switch (parity)
644 {
645 case PARITY_ODD:
646 tio.c_cflag |= PARENB;
647 tio.c_cflag |= PARODD;
648 break;
649
650 case PARITY_EVEN:
651 tio.c_cflag |= PARENB;
652 tio.c_cflag &= ~PARODD;
653 break;
654
655 case PARITY_NONE:
656 tio.c_cflag &= ~PARENB;
657 break;
658 }
659 if (!IO_Serial_SetProperties (tio))
660 return FALSE;
661 }
662
663 return TRUE;
664}
665
666void IO_Serial_Flush ()
667{
668 BYTE b;
669 while(IO_Serial_Read(1000, 1, &b));
670}
671
672
673void IO_Serial_GetPnPId (IO_Serial * io, BYTE * pnp_id, unsigned *length)
674{
675 (*length) = io->PnP_id_size;
676 memcpy (pnp_id, io->PnP_id, io->PnP_id_size);
677}
678
679bool IO_Serial_Read (unsigned timeout, unsigned size, BYTE * data)
680{
681 BYTE c;
682 uint count = 0;
683#ifdef SH4
684 bool readed;
685 struct timeval tv, tv_spent;
686#endif
687
688 if((reader[ridx].typ != R_INTERNAL) && (wr>0))
689 {
690 BYTE buf[256];
691 int n = wr;
692 wr = 0;
693
694 if(!IO_Serial_Read (timeout, n, buf))
695 {
696 return FALSE;
697 }
698 }
699
700#ifdef DEBUG_IO
701 printf ("IO: Receiving: ");
702 fflush (stdout);
703#endif
704 for (count = 0; count < size * (_in_echo_read ? (1+io_serial_need_dummy_char) : 1); count++)
705 {
706#ifdef SH4
707 gettimeofday(&tv,0);
708 memcpy(&tv_spent,&tv,sizeof(struct timeval));
709 readed=FALSE;
710 while( (((tv_spent.tv_sec-tv.tv_sec)*1000) + ((tv_spent.tv_usec-tv.tv_usec)/1000L))<timeout )
711 {
712 if (read (reader[ridx].handle, &c, 1) == 1)
713 {
714 readed=TRUE;
715 break;
716 }
717 gettimeofday(&tv_spent,0);
718 }
719 if(!readed) return FALSE;
720
721 data[_in_echo_read ? count/(1+io_serial_need_dummy_char) : count] = c;
722#ifdef DEBUG_IO
723 printf ("%X ", c);
724 fflush (stdout);
725#endif
726#else
727 if (IO_Serial_WaitToRead (0, timeout))
728 {
729 if (read (reader[ridx].handle, &c, 1) != 1)
730 {
731#ifdef DEBUG_IO
732 printf ("ERROR\n");
733 fflush (stdout);
734#endif
735 return FALSE;
736 }
737 data[_in_echo_read ? count/(1+io_serial_need_dummy_char) : count] = c;
738
739#ifdef DEBUG_IO
740 printf ("%X ", c);
741 fflush (stdout);
742#endif
743 }
744 else
745 {
746#ifdef DEBUG_IO
747 printf ("TIMEOUT\n");
748 fflush (stdout);
749#endif
750 tcflush (reader[ridx].handle, TCIFLUSH);
751 return FALSE;
752 }
753#endif
754 }
755
756 _in_echo_read = 0;
757
758#ifdef DEBUG_IO
759 printf ("\n");
760 fflush (stdout);
761#endif
762
763 return TRUE;
764}
765
766
767
768
769bool IO_Serial_Write (unsigned delay, unsigned size, BYTE * data)
770{
771 unsigned count, to_send, i_w;
772 BYTE data_w[512];
773#ifdef DEBUG_IO
774 unsigned i;
775
776 printf ("IO: Sending: ");
777 fflush (stdout);
778#endif
779 /* Discard input data from previous commands */
780// tcflush (reader[ridx].handle, TCIFLUSH);
781
782 for (count = 0; count < size; count += to_send)
783 {
784// if(reader[ridx].typ == R_INTERNAL)
785// to_send = 1;
786// else
787 to_send = (delay? 1: size);
788
789 if (IO_Serial_WaitToWrite (delay, 1000))
790 {
791 for (i_w=0; i_w < to_send; i_w++) {
792 data_w [(1+io_serial_need_dummy_char)*i_w] = data [count + i_w];
793 if (io_serial_need_dummy_char) {
794 data_w [2*i_w+1] = 0x00;
795 }
796 }
797 unsigned int u = write (reader[ridx].handle, data_w, (1+io_serial_need_dummy_char)*to_send);
798 _in_echo_read = 1;
799 if (u != (1+io_serial_need_dummy_char)*to_send)
800 {
801#ifdef DEBUG_IO
802 printf ("ERROR\n");
803 fflush (stdout);
804#endif
805 if(reader[ridx].typ != R_INTERNAL)
806 wr += u;
807 return FALSE;
808 }
809
810 if(reader[ridx].typ != R_INTERNAL)
811 wr += to_send;
812
813#ifdef DEBUG_IO
814 for (i=0; i<(1+io_serial_need_dummy_char)*to_send; i++)
815 printf ("%X ", data_w[count + i]);
816 fflush (stdout);
817#endif
818 }
819 else
820 {
821#ifdef DEBUG_IO
822 printf ("TIMEOUT\n");
823 fflush (stdout);
824#endif
825// tcflush (reader[ridx].handle, TCIFLUSH);
826 return FALSE;
827 }
828 }
829
830#ifdef DEBUG_IO
831 printf ("\n");
832 fflush (stdout);
833#endif
834
835 return TRUE;
836}
837
838bool IO_Serial_Close (IO_Serial * io)
839{
840
841#ifdef DEBUG_IO
842 printf ("IO: Clossing serial port %s\n", reader[ridx].device);
843#endif
844
845#if defined(TUXBOX) && defined(PPC)
846 close(fdmc);
847#endif
848 if (close (reader[ridx].handle) != 0)
849 return FALSE;
850
851 IO_Serial_Clear (io);
852
853 return TRUE;
854}
855
856/*
857 * Internal functions definition
858 */
859
860static int IO_Serial_Bitrate(int bitrate)
861{
862#ifdef B230400
863 if ((bitrate)>=230400) return B230400;
864#endif
865#ifdef B115200
866 if ((bitrate)>=115200) return B115200;
867#endif
868#ifdef B57600
869 if ((bitrate)>=57600) return B57600;
870#endif
871#ifdef B38400
872 if ((bitrate)>=38400) return B38400;
873#endif
874#ifdef B19200
875 if ((bitrate)>=19200) return B19200;
876#endif
877#ifdef B9600
878 if ((bitrate)>=9600) return B9600;
879#endif
880#ifdef B4800
881 if ((bitrate)>=4800) return B4800;
882#endif
883#ifdef B2400
884 if ((bitrate)>=2400) return B2400;
885#endif
886#ifdef B1800
887 if ((bitrate)>=1800) return B1800;
888#endif
889#ifdef B1200
890 if ((bitrate)>=1200) return B1200;
891#endif
892#ifdef B600
893 if ((bitrate)>=600) return B600;
894#endif
895#ifdef B300
896 if ((bitrate)>=300) return B300;
897#endif
898#ifdef B200
899 if ((bitrate)>=200) return B200;
900#endif
901#ifdef B150
902 if ((bitrate)>=150) return B150;
903#endif
904#ifdef B134
905 if ((bitrate)>=134) return B134;
906#endif
907#ifdef B110
908 if ((bitrate)>=110) return B110;
909#endif
910#ifdef B75
911 if ((bitrate)>=75) return B75;
912#endif
913#ifdef B50
914 if ((bitrate)>=50) return B50;
915#endif
916#ifdef B0
917 if ((bitrate)>=0) return B0;
918#endif
919 return 0; /* Should never get here */
920}
921
922static bool IO_Serial_WaitToRead (unsigned delay_ms, unsigned timeout_ms)
923{
924 fd_set rfds;
925 fd_set erfds;
926 struct timeval tv;
927 int select_ret;
928 int in_fd;
929
930 if (delay_ms > 0)
931 {
932#ifdef HAVE_NANOSLEEP
933 struct timespec req_ts;
934
935 req_ts.tv_sec = delay_ms / 1000;
936 req_ts.tv_nsec = (delay_ms % 1000) * 1000000L;
937 nanosleep (&req_ts, NULL);
938#else
939 usleep (delay_ms * 1000L);
940#endif
941 }
942
943 in_fd=reader[ridx].handle;
944
945 FD_ZERO(&rfds);
946 FD_SET(in_fd, &rfds);
947
948 FD_ZERO(&erfds);
949 FD_SET(in_fd, &erfds);
950
951 tv.tv_sec = timeout_ms/1000;
952 tv.tv_usec = (timeout_ms % 1000) * 1000L;
953 select_ret = select(in_fd+1, &rfds, NULL, &erfds, &tv);
954 if(select_ret==-1)
955 {
956 printf("select_ret=%i\n" , select_ret);
957 printf("errno =%d\n", errno);
958 fflush(stdout);
959 return (FALSE);
960 }
961
962 if (FD_ISSET(in_fd, &erfds))
963 {
964 printf("fd is in error fds\n");
965 printf("errno =%d\n", errno);
966 fflush(stdout);
967 return (FALSE);
968 }
969
970 return(FD_ISSET(in_fd,&rfds));
971}
972
973static bool IO_Serial_WaitToWrite (unsigned delay_ms, unsigned timeout_ms)
974{
975 fd_set wfds;
976 fd_set ewfds;
977 struct timeval tv;
978 int select_ret;
979 int out_fd;
980
981#ifdef SCI_DEV
982 if(reader[ridx].typ == R_INTERNAL)
983 return TRUE;
984#endif
985
986 if (delay_ms > 0)
987 {
988#ifdef HAVE_NANOSLEEP
989 struct timespec req_ts;
990
991 req_ts.tv_sec = delay_ms / 1000;
992 req_ts.tv_nsec = (delay_ms % 1000) * 1000000L;
993 nanosleep (&req_ts, NULL);
994#else
995 usleep (delay_ms * 1000L);
996#endif
997 }
998
999 out_fd=reader[ridx].handle;
1000
1001 FD_ZERO(&wfds);
1002 FD_SET(out_fd, &wfds);
1003
1004 FD_ZERO(&ewfds);
1005 FD_SET(out_fd, &ewfds);
1006
1007 tv.tv_sec = timeout_ms/1000L;
1008 tv.tv_usec = (timeout_ms % 1000) * 1000L;
1009
1010 select_ret = select(out_fd+1, NULL, &wfds, &ewfds, &tv);
1011
1012 if(select_ret==-1)
1013 {
1014 printf("select_ret=%d\n" , select_ret);
1015 printf("errno =%d\n", errno);
1016 fflush(stdout);
1017 return (FALSE);
1018 }
1019
1020 if (FD_ISSET(out_fd, &ewfds))
1021 {
1022 printf("fd is in ewfds\n");
1023 printf("errno =%d\n", errno);
1024 fflush(stdout);
1025 return (FALSE);
1026 }
1027
1028 return(FD_ISSET(out_fd,&wfds));
1029
1030}
1031
1032static void IO_Serial_Clear (IO_Serial * io)
1033{
1034 reader[ridx].handle = -1;
1035 reader[ridx].typ = 0;
1036 memset (io->PnP_id, 0, IO_SERIAL_PNPID_SIZE);
1037 io->PnP_id_size = 0;
1038 wr = 0;
1039 //modifyable properties:
1040 io->input_bitrate = 0;
1041 io->output_bitrate = 0;
1042 io->bits = 0;
1043 io->stopbits = 0;
1044 io->parity = 0;
1045 io->dtr = 0;
1046 io->rts = 0;
1047}
1048
1049static bool IO_Serial_InitPnP (IO_Serial * io)
1050{
1051 int i = 0;
1052 io->input_bitrate = 1200;
1053 io->output_bitrate = 1200;
1054 io->parity = PARITY_NONE;
1055 io->bits = 7;
1056 io->stopbits = 1;
1057 io->dtr = IO_SERIAL_HIGH;
1058 io->rts = IO_SERIAL_LOW;
1059
1060 if (!IO_Serial_SetPropertiesOld (io))
1061 return FALSE;
1062
1063 while ((i < IO_SERIAL_PNPID_SIZE) && IO_Serial_Read (200, 1, &(io->PnP_id[i])))
1064 i++;
1065
1066 io->PnP_id_size = i;
1067 return TRUE;
1068}
1069
Note: See TracBrowser for help on using the repository browser.