Ignore:
Timestamp:
05/12/09 05:25:27 (12 years ago)
Author:
rorothetroll
Message:

csctapi/io_serial.c : fix the serial port read/write wait routine to use select instead of poll

This make them compatible with Mac OS X and now oscamd works on OS X. The same code off course
still works on linux and other unix platforms. I let that code ran for 24h before commiting this code.
If you have any issue let me know and I'llr evert to poll for non compatible machines.

all the others : fix all the warning due to sign difference (mostly uchar versus char). This make the code compile

with -Werror on the more strict version of gcc (which is the case on OS X). I also noticed that in a lot of places
the code use a buffer defined as an uchar * ... and use strings functions (strnXXXX) where some memcpy/memcmp .. function
would have been preferable as we're suposedly manipulation a binary buffer. Anyway I fixed all of them and it now compile
without any warning or error on linux and OS X (and also probably on all the other unices but we will have to try before
adding -Werror on the other platform).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/csctapi/io_serial.c

    r69 r70  
    1 /*
     1   /*
    22    io_serial.c
    33    Serial port input/output functions
     
    2727#include <string.h>
    2828#include <stdlib.h>
     29#include <errno.h>
    2930#ifdef OS_HPUX
    3031#include <sys/modem.h>
     
    658659{
    659660    BYTE b;
    660 #ifdef OS_MACOSX
    661     while(IO_Serial_Read_MacOSX(io, 1000, 1, &b));
    662 #else
    663661    while(IO_Serial_Read(io, 1000, 1, &b));
    664 #endif
    665662}
    666663
     
    676673    return io->com;
    677674}
    678 
    679 //
    680 // read "size" Byte from the port.
    681 // return true if all data were read, false on error or timeout
    682 //
    683 
    684 bool IO_Serial_Read_MacOSX(IO_Serial * io, unsigned timeout, unsigned size, BYTE * data)
    685 
    686 {
    687    
    688    int nByte;
    689    int totalRead;
    690    int m_timeout;
    691    int length;
    692    BYTE c;
    693    
    694    totalRead=0;
    695    m_timeout=0;
    696    nByte=0;
    697    length= size * (_in_echo_read ? (1+io_serial_need_dummy_char) : 1);
    698 
    699     if((io->com!=RTYP_SCI) && (io->wr>0))
    700     {
    701         BYTE buf[256];
    702         int n = io->wr;
    703         io->wr = 0;
    704    
    705         if(!IO_Serial_Read_MacOSX (io, timeout, n, buf))
    706         {
    707             return FALSE;
    708         }
    709     }
    710 
    711 #ifdef DEBUG_IO
    712    printf ("IO: IO_Serial_Read_MacOSX\n");
    713     printf ("IO: Receiving %d byte(s): ",size);
    714     fflush (stdout);
    715 #endif
    716    while(TRUE)
    717    {
    718       nByte=read(io->fd,&c,1);
    719       if(nByte<=0)
    720       {
    721          usleep (1000L); // 1ms
    722          m_timeout++;
    723          if(m_timeout==timeout)
    724          {
    725 #ifdef DEBUG_IO
    726             printf ("TIMEOUT\n");
    727             fflush (stdout);
    728 #endif
    729             tcflush (io->fd, TCIFLUSH);
    730             return FALSE;
    731          }
    732       }
    733       else
    734       {
    735          m_timeout=0;
    736 #ifdef DEBUG_IO
    737             printf ("%02X ", c);
    738             fflush (stdout);
    739 #endif
    740          data[_in_echo_read ? totalRead/(1+io_serial_need_dummy_char) : totalRead] = c;
    741          totalRead+=nByte;
    742          if(length == totalRead)
    743             break;
    744       }
    745    }
    746    
    747    _in_echo_read = 0;
    748 
    749 #ifdef DEBUG_IO
    750    printf ("\n");
    751    fflush (stdout);
    752    printf("exiting IO_Serial_Read_MacOSX [true]\n" );
    753 #endif
    754    return TRUE;
    755 }
    756 
    757 //
    758 // Send "size" Byte data present in data
    759 // return true if data was correctly send
    760 //
    761 bool IO_Serial_Write_MacOSX (IO_Serial * io, unsigned delay, unsigned size, BYTE * data)
    762 {
    763    int nByte;
    764    int m_delay;
    765    BYTE data_w[512];
    766    int i_w;
    767    unsigned count, to_send;
    768 
    769 #ifdef DEBUG_IO
    770     unsigned i;
    771     printf ("IO: IO_Serial_Write_MacOSX\n");
    772     printf ("IO: Sending %d byte(s) : ",size);
    773     fflush (stdout);
    774 #endif
    775    
    776    m_delay=0;
    777    nByte=0;
    778    to_send = (delay? 1: size);
    779    
    780    // send the data
    781    for (count = 0; count < size; count += to_send)
    782    {
    783       for (i_w=0; i_w < to_send; i_w++)
    784       {
    785          data_w [(1+io_serial_need_dummy_char)*i_w] = data [count + i_w];
    786          if (io_serial_need_dummy_char)
    787          {
    788             data_w [2*i_w+1] = 0x00;
    789          }
    790       }
    791      
    792       nByte=write(io->fd,data_w,(1+io_serial_need_dummy_char)*to_send);
    793       _in_echo_read = 1;
    794      
    795       if(nByte<=0 || nByte!=(1+io_serial_need_dummy_char)*to_send)
    796       {
    797          tcflush (io->fd, TCIFLUSH);
    798          if(io->com!=RTYP_SCI)
    799             io->wr += nByte;
    800 
    801 #ifdef DEBUG_IO
    802          printf ("ERROR\n");
    803          printf("exiting IO_Serial_Write_MacOSX [false]\n" );
    804          fflush (stdout);
    805 #endif
    806          return FALSE;
    807       }
    808       if(delay)
    809          usleep(delay*1000L);
    810 
    811       if(io->com!=RTYP_SCI)
    812          io->wr += to_send;
    813 
    814 #ifdef DEBUG_IO
    815       for (i=0; i<(1+io_serial_need_dummy_char)*to_send; i++)
    816          printf ("%02 X ", data_w[count + i]);
    817       fflush (stdout);
    818 #endif
    819      
    820    }
    821 
    822 #ifdef DEBUG_IO
    823     printf ("\n");
    824     fflush (stdout);
    825 #endif
    826     return TRUE;
    827 }
    828 
    829675
    830676
     
    1062908static bool IO_Serial_WaitToRead (int hnd, unsigned delay_ms, unsigned timeout_ms)
    1063909{
    1064     int rval;
    1065     struct pollfd ufds;
    1066    
    1067     if (delay_ms > 0)
    1068     {
     910   fd_set rfds;
     911   fd_set erfds;
     912   struct timeval tv;
     913   int select_ret;
     914   int in_fd;
     915   
     916   if (delay_ms > 0)
     917   {
    1069918#ifdef HAVE_NANOSLEEP
    1070         struct timespec req_ts;
     919      struct timespec req_ts;
     920     
     921      req_ts.tv_sec = delay_ms / 1000;
     922      req_ts.tv_nsec = (delay_ms % 1000) * 1000000L;
     923      nanosleep (&req_ts, NULL);
     924#else
     925      usleep (delay_ms * 1000L);
     926#endif
     927   }
     928   
     929   in_fd=hnd;
     930   
     931   FD_ZERO(&rfds);
     932   FD_SET(in_fd, &rfds);
     933   
     934   FD_ZERO(&erfds);
     935   FD_SET(in_fd, &erfds);
     936   
     937   tv.tv_sec = timeout_ms/1000;
     938   tv.tv_usec = (timeout_ms % 1000) * 1000L;
     939   select_ret = select(in_fd+1, &rfds, NULL,  &erfds, &tv);
     940   if(select_ret==-1)
     941   {
     942      printf("select_ret=%i\n" , select_ret);
     943      printf("errno =%d\n", errno);
     944      fflush(stdout);
     945      return (FALSE);
     946   }
     947
     948   if (FD_ISSET(in_fd, &erfds))
     949   {
     950      printf("fd is in error fds\n");
     951      printf("errno =%d\n", errno);
     952      fflush(stdout);
     953      return (FALSE);
     954   }
     955
     956   return(FD_ISSET(in_fd,&rfds));
     957}
     958
     959static bool IO_Serial_WaitToWrite (IO_Serial *io, unsigned delay_ms, unsigned timeout_ms)
     960{
     961   fd_set wfds;
     962   fd_set ewfds;
     963   struct timeval tv;
     964   int select_ret;
     965   int out_fd;
     966   
     967#ifdef SCI_DEV
     968   if(io->com==RTYP_SCI)
     969      return TRUE;
     970#endif
    1071971       
    1072         req_ts.tv_sec = delay_ms / 1000;
    1073         req_ts.tv_nsec = (delay_ms % 1000) * 1000000L;
    1074         nanosleep (&req_ts, NULL);
     972   if (delay_ms > 0)
     973    {
     974#ifdef HAVE_NANOSLEEP
     975      struct timespec req_ts;
     976     
     977      req_ts.tv_sec = delay_ms / 1000;
     978      req_ts.tv_nsec = (delay_ms % 1000) * 1000000L;
     979      nanosleep (&req_ts, NULL);
    1075980#else
    1076         usleep (delay_ms * 1000L);
    1077 #endif
    1078     }
    1079    
    1080     ufds.fd = hnd;
    1081     ufds.events = POLLIN;
    1082     ufds.revents = 0x0000;
    1083    
    1084     rval = poll (&ufds, 1, timeout_ms);
    1085     if (rval != 1)
    1086         return (FALSE);
    1087    
    1088     return (((ufds.revents) & POLLIN) == POLLIN);
    1089 }
    1090 
    1091 static bool IO_Serial_WaitToWrite (IO_Serial *io, unsigned delay_ms, unsigned timeout_ms)
    1092 {
    1093     int rval;
    1094     struct pollfd ufds;
    1095 
    1096 #ifdef SCI_DEV
    1097     if(io->com==RTYP_SCI)
    1098         return TRUE;
    1099 #endif
    1100        
    1101     if (delay_ms > 0)
    1102     {
    1103 #ifdef HAVE_NANOSLEEP
    1104         struct timespec req_ts;
    1105        
    1106         req_ts.tv_sec = delay_ms / 1000;
    1107         req_ts.tv_nsec = (delay_ms % 1000) * 1000000L;
    1108         nanosleep (&req_ts, NULL);
    1109 #else
    1110         usleep (delay_ms * 1000L);
    1111 #endif
    1112     }
    1113    
    1114     ufds.fd = io->fd;
    1115     ufds.events = POLLOUT;
    1116     ufds.revents = 0x0000;
    1117    
    1118     rval = poll (&ufds, 1, timeout_ms);
    1119     if (rval != 1)
    1120         return (FALSE);
    1121    
    1122     return (((ufds.revents) & POLLOUT) == POLLOUT);
     981      usleep (delay_ms * 1000L);
     982#endif
     983   }
     984
     985   out_fd=io->fd;
     986   
     987   FD_ZERO(&wfds);
     988   FD_SET(out_fd, &wfds);
     989   
     990   FD_ZERO(&ewfds);
     991   FD_SET(out_fd, &ewfds);
     992   
     993   tv.tv_sec = timeout_ms/1000L;
     994   tv.tv_usec = (timeout_ms % 1000) * 1000L;
     995
     996   select_ret = select(out_fd+1, NULL, &wfds, &ewfds, &tv);
     997
     998   if(select_ret==-1)
     999   {
     1000      printf("select_ret=%d\n" , select_ret);
     1001      printf("errno =%d\n", errno);
     1002      fflush(stdout);
     1003      return (FALSE);
     1004   }
     1005
     1006   if (FD_ISSET(out_fd, &ewfds))
     1007   {
     1008      printf("fd is in ewfds\n");
     1009      printf("errno =%d\n", errno);
     1010      fflush(stdout);
     1011      return (FALSE);
     1012   }
     1013
     1014   return(FD_ISSET(out_fd,&wfds));
     1015   
    11231016}
    11241017
     
    12041097        return FALSE;
    12051098
    1206 #ifdef OS_MACOSX
    1207     while ((i < IO_SERIAL_PNPID_SIZE) && IO_Serial_Read_MacOSX (io, 200, 1, &(io->PnP_id[i])))
    1208       i++;
    1209 #else   
    12101099    while ((i < IO_SERIAL_PNPID_SIZE) && IO_Serial_Read (io, 200, 1, &(io->PnP_id[i])))
    12111100      i++;
    1212 #endif
    12131101
    12141102    io->PnP_id_size = i;
Note: See TracChangeset for help on using the changeset viewer.