Changeset 4009


Ignore:
Timestamp:
11/28/10 15:28:12 (10 years ago)
Author:
dingo35
Message:

ifd_smartreader.c: simplification of read/write routines

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/csctapi/ifd_smartreader.c

    r3913 r4009  
    4747                           enum smartreader_stopbits_type sbit, enum smartreader_parity_type parity);
    4848static void smart_flush(S_READER *reader);
    49 static int smart_read(S_READER *reader, unsigned char* buff, unsigned int size, int timeout_sec);
    50 static int smartreader_write_data(S_READER *reader, unsigned char *buf, unsigned int size);
    51 static int smart_write(S_READER *reader, unsigned char* buff, unsigned int size, int udelay);
    5249static int smartreader_set_latency_timer(S_READER *reader, unsigned short latency);
    5350static void EnableSmartReader(S_READER *reader, int clock, unsigned short Fi, unsigned char Di, unsigned char Ni, unsigned char T,unsigned char inv, int parity);
     
    156153
    157154    return OK;
     155}
     156
     157static int smart_read(S_READER *reader, unsigned char* buff, unsigned int size, int timeout_sec)
     158{
     159
     160    int ret = 0;
     161    unsigned int total_read = 0;
     162    struct timeval start, now, dif = {0,0};
     163    gettimeofday(&start,NULL);
     164   
     165    while(total_read < size && dif.tv_sec < timeout_sec) {
     166        pthread_mutex_lock(&reader->sr_config->g_read_mutex);
     167       
     168        if(reader->sr_config->g_read_buffer_size > 0) {
     169       
     170            ret = reader->sr_config->g_read_buffer_size > size-total_read ? size-total_read : reader->sr_config->g_read_buffer_size;
     171            memcpy(buff+total_read,reader->sr_config->g_read_buffer,ret);
     172            reader->sr_config->g_read_buffer_size -= ret;
     173
     174              if(reader->sr_config->g_read_buffer_size > 0)
     175                  memcpy(reader->sr_config->g_read_buffer, reader->sr_config->g_read_buffer + ret, reader->sr_config->g_read_buffer_size);
     176
     177            total_read+=ret;
     178        }
     179        pthread_mutex_unlock(&reader->sr_config->g_read_mutex);
     180       
     181        gettimeofday(&now,NULL);
     182        timersub(&now, &start, &dif);
     183                cs_sleepus(50);
     184        sched_yield();
     185    }
     186        cs_ddump(buff, total_read, "SR IO: Receive: ");
     187    return total_read;
    158188}
    159189
     
    253283}
    254284
     285static int smart_write(S_READER *reader, unsigned char* buff, unsigned int size)
     286{
     287
     288    unsigned int idx;
     289
     290    int write_size;
     291    unsigned int offset = 0;
     292    int total_written = 0;
     293    int written;
     294     
     295    if(size<reader->sr_config->writebuffer_chunksize)
     296        write_size=size;
     297    else
     298        write_size = reader->sr_config->writebuffer_chunksize;
     299
     300    while (offset < size)
     301    {
     302        if (offset+write_size > size)
     303            write_size = size-offset;
     304
     305        int ret = libusb_bulk_transfer(reader->sr_config->usb_dev_handle,
     306                                    reader->sr_config->in_ep,
     307                                    buff+offset,
     308                                    write_size,
     309                                    &written,
     310                                    reader->sr_config->usb_write_timeout);
     311        if (ret < 0) {
     312            cs_log("usb bulk write failed : ret = %d",ret);
     313            sched_yield();
     314            return(ret);
     315        }
     316        cs_ddump(buff+offset, written, "SR IO: Transmit: ");
     317        total_written += written;
     318        offset += write_size;
     319    }
     320    sched_yield();
     321    return total_written;
     322}
     323
    255324int SR_Transmit (struct s_reader *reader, BYTE * buffer, unsigned size)
    256 
    257325{
    258326    unsigned int ret;
    259     ret = smart_write(reader, buffer, size, 0);
     327    ret = smart_write(reader, buffer, size);
    260328    if (ret!=size)
    261329        return ERROR;
     
    377445        FiDi[2]=LOBYTE(Fi);
    378446        FiDi[3]=Di;
    379         ret = smart_write(reader,FiDi, sizeof (FiDi),0);
     447        ret = smart_write(reader,FiDi, sizeof (FiDi));
    380448    }
    381449    else {
     
    390458    Freq[1]=HIBYTE(freqk);
    391459    Freq[2]=LOBYTE(freqk);
    392     ret = smart_write(reader, Freq, sizeof (Freq),0);
     460    ret = smart_write(reader, Freq, sizeof (Freq));
    393461
    394462    // command 3, set paramter N
     
    396464    N[0]=0x03;
    397465    N[1]=Ni;
    398     ret = smart_write(reader, N, sizeof (N),0);
     466    ret = smart_write(reader, N, sizeof (N));
    399467
    400468    // command 4 , set parameter T
     
    413481    Prot[0]=0x04;
    414482    Prot[1]=T;
    415     ret = smart_write(reader, Prot, sizeof (Prot),0);
     483    ret = smart_write(reader, Prot, sizeof (Prot));
    416484
    417485    // command 5, set invert y/n
     
    419487    Invert[0]=0x05;
    420488    Invert[1]=inv;
    421     ret = smart_write(reader, Invert, sizeof (Invert),0);
     489    ret = smart_write(reader, Invert, sizeof (Invert));
    422490
    423491    ret = smartreader_set_line_property2(reader, BITS_8, STOP_BIT_2, parity, BREAK_ON);
     
    11231191}
    11241192
    1125 static int smart_read(S_READER *reader, unsigned char* buff, unsigned int size, int timeout_sec)
    1126 {
    1127 
    1128     int ret = 0;
    1129     unsigned int total_read = 0;
    1130     struct timeval start, now, dif = {0,0};
    1131     gettimeofday(&start,NULL);
    1132    
    1133     while(total_read < size && dif.tv_sec < timeout_sec) {
    1134         pthread_mutex_lock(&reader->sr_config->g_read_mutex);
    1135        
    1136         if(reader->sr_config->g_read_buffer_size > 0) {
    1137        
    1138             ret = reader->sr_config->g_read_buffer_size > size-total_read ? size-total_read : reader->sr_config->g_read_buffer_size;
    1139             memcpy(buff+total_read,reader->sr_config->g_read_buffer,ret);
    1140             reader->sr_config->g_read_buffer_size -= ret;
    1141 
    1142         if(reader->sr_config->g_read_buffer_size > 0){
    1143         memcpy(reader->sr_config->g_read_buffer, reader->sr_config->g_read_buffer + ret, reader->sr_config->g_read_buffer_size);
    1144         }
    1145 
    1146             total_read+=ret;
    1147         }
    1148         pthread_mutex_unlock(&reader->sr_config->g_read_mutex);
    1149        
    1150         gettimeofday(&now,NULL);
    1151         timersub(&now, &start, &dif);
    1152                 cs_sleepus(50);
    1153         sched_yield();
    1154     }
    1155         cs_ddump(buff, total_read, "SR IO: Receive: ");
    1156 
    1157    
    1158     return total_read;
    1159 }
    1160 
    1161 int smartreader_write_data(S_READER *reader, unsigned char *buf, unsigned int size)
    1162 {
    1163     int ret;
    1164     int write_size;
    1165     unsigned int offset = 0;
    1166     int total_written = 0;
    1167     int written;
    1168      
    1169     if(size<reader->sr_config->writebuffer_chunksize)
    1170         write_size=size;
    1171     else
    1172         write_size = reader->sr_config->writebuffer_chunksize;
    1173 
    1174     while (offset < size)
    1175     {
    1176 
    1177         if (offset+write_size > size)
    1178             write_size = size-offset;
    1179 
    1180         ret = libusb_bulk_transfer(reader->sr_config->usb_dev_handle,
    1181                                     reader->sr_config->in_ep,
    1182                                     buf+offset,
    1183                                     write_size,
    1184                                     &written,
    1185                                     reader->sr_config->usb_write_timeout);
    1186         if (ret < 0) {
    1187             cs_log("usb bulk write failed : ret = %d",ret);
    1188             return(ret);
    1189         }
    1190         cs_ddump(buf+offset, written, "SR IO: Transmit: ");
    1191         total_written += written;
    1192         offset += write_size;
    1193     }
    1194 
    1195     return total_written;
    1196 }
    1197 
    11981193static int smartreader_set_latency_timer(S_READER *reader, unsigned short latency)
    11991194{
     
    12191214
    12201215    return 0;
    1221 }
    1222 
    1223 static int smart_write(S_READER *reader, unsigned char* buff, unsigned int size, int udelay)
    1224 {
    1225 
    1226     int ret = 0;
    1227     unsigned int idx;
    1228 
    1229     if (udelay == 0) {
    1230         ret = smartreader_write_data(reader, buff, size);
    1231         if(ret<0) {
    1232             cs_debug_mask (D_IFD,"IO:SR: USB write error : %d",ret);
    1233         }
    1234     }
    1235     else {
    1236         for (idx = 0; idx < size; idx++) {
    1237             if ((ret = smartreader_write_data(reader, &buff[idx], 1)) < 0){
    1238                 break;
    1239             }
    1240               cs_sleepus(udelay);
    1241         }
    1242     }
    1243     sched_yield();
    1244     return ret;
    12451216}
    12461217
Note: See TracChangeset for help on using the changeset viewer.