source: trunk/csctapi/io_serial.c@ 1224

Last change on this file since 1224 was 1224, checked in by dingo35, 12 years ago

Move phoenix/smartmouse transmit and receive to ifd_phoenix.c

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