Changeset 4015


Ignore:
Timestamp:
11/29/10 10:36:44 (10 years ago)
Author:
dingo35
Message:

ifd_smartreader.c : when running multiple smartreaders in native mode, only run 1 libusb event handler

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/csctapi/ifd_smartreader.c

    r4009 r4015  
    2525#define NUM_TXFERS 2
    2626
    27 typedef struct s_reader S_READER;
     27static int number_of_smartreaders_running=0; //counts how many smartreaders are running, if 0 no libusbeventhandler needs to be called
     28static pthread_t libusb_thread;
    2829
    2930static bool smartreader_check_endpoint(libusb_device *usb_dev, uint8_t out_endpoint);
    3031static struct libusb_device *find_smartreader(const char*busname,const char *devname, uint8_t out_endpoint);
    31 static void smartreader_init(S_READER *reader, uint8_t out_endpoint);
    32 static unsigned int smartreader_determine_max_packet_size(S_READER *reader);
    33 static int smartreader_usb_close_internal (S_READER *reader);
    34 static int smartreader_usb_reset(S_READER *reader);
    35 static int smartreader_usb_open_dev(S_READER *reader);
    36 static int smartreader_usb_purge_rx_buffer(S_READER *reader);
    37 static int smartreader_usb_purge_tx_buffer(S_READER *reader);
    38 static int smartreader_usb_purge_buffers(S_READER *reader);
    39 static int smartreader_convert_baudrate(int baudrate, S_READER *reader, unsigned short *value, unsigned short *index);
    40 static int smartreader_set_baudrate(S_READER *reader, int baudrate);
    41 static int smartreader_setdtr_rts(S_READER *reader, int dtr, int rts);
    42 static int smartreader_setflowctrl(S_READER *reader, int flowctrl);
    43 static int smartreader_set_line_property2(S_READER *reader, enum smartreader_bits_type bits,
     32static void smartreader_init(struct s_reader *reader, uint8_t out_endpoint);
     33static unsigned int smartreader_determine_max_packet_size(struct s_reader *reader);
     34static int smartreader_usb_close_internal (struct s_reader *reader);
     35static int smartreader_usb_reset(struct s_reader *reader);
     36static int smartreader_usb_open_dev(struct s_reader *reader);
     37static int smartreader_usb_purge_rx_buffer(struct s_reader *reader);
     38static int smartreader_usb_purge_tx_buffer(struct s_reader *reader);
     39static int smartreader_usb_purge_buffers(struct s_reader *reader);
     40static int smartreader_convert_baudrate(int baudrate, struct s_reader *reader, unsigned short *value, unsigned short *index);
     41static int smartreader_set_baudrate(struct s_reader *reader, int baudrate);
     42static int smartreader_setdtr_rts(struct s_reader *reader, int dtr, int rts);
     43static int smartreader_setflowctrl(struct s_reader *reader, int flowctrl);
     44static int smartreader_set_line_property2(struct s_reader *reader, enum smartreader_bits_type bits,
    4445                            enum smartreader_stopbits_type sbit, enum smartreader_parity_type parity,
    4546                            enum smartreader_break_type break_type);
    46 static int smartreader_set_line_property(S_READER *reader, enum smartreader_bits_type bits,
     47static int smartreader_set_line_property(struct s_reader *reader, enum smartreader_bits_type bits,
    4748                           enum smartreader_stopbits_type sbit, enum smartreader_parity_type parity);
    48 static void smart_flush(S_READER *reader);
    49 static int smartreader_set_latency_timer(S_READER *reader, unsigned short latency);
    50 static void EnableSmartReader(S_READER *reader, int clock, unsigned short Fi, unsigned char Di, unsigned char Ni, unsigned char T,unsigned char inv, int parity);
     49static void smart_flush(struct s_reader *reader);
     50static int smartreader_set_latency_timer(struct s_reader *reader, unsigned short latency);
     51static void EnableSmartReader(struct s_reader *reader, int clock, unsigned short Fi, unsigned char Di, unsigned char Ni, unsigned char T,unsigned char inv, int parity);
    5152static void *ReaderThread(void *p);
    5253
     
    7475    cs_debug_mask (D_IFD,"IO:SR: Looking for device %s on bus %s",devname,busname);
    7576
    76     ret = libusb_init(NULL);
    77     if (ret < 0) {
     77    ret = libusb_init(NULL); //FIXME should we really init for every smartreader or only for first one?
     78    if (ret < 0) {
    7879        cs_log("Libusb init error : %d",ret);
    7980        return ret;
     
    127128    pthread_mutex_init(&reader->sr_config->g_read_mutex,NULL);
    128129    pthread_mutex_init(&reader->sr_config->g_usb_mutex,NULL);
    129     ret = pthread_create(&reader->sr_config->rt, NULL, ReaderThread, (void *)(reader));
    130     if (ret) {
     130    if (number_of_smartreaders_running++ <= 1) { //first smartreader starting needs to start readerthread
     131      ret = pthread_create(&libusb_thread, NULL, ReaderThread, (void *)(reader));
     132      if (ret) {
    131133        cs_log("ERROR; return code from pthread_create() is %d", ret);
    132134        return ERROR;
    133     }
    134 
    135     return OK;
     135      }
     136    }
     137    return OK;
    136138}
    137139
     
    155157}
    156158
    157 static int smart_read(S_READER *reader, unsigned char* buff, unsigned int size, int timeout_sec)
     159static int smart_read(struct s_reader *reader, unsigned char* buff, unsigned int size, int timeout_sec)
    158160{
    159161
     
    283285}
    284286
    285 static int smart_write(S_READER *reader, unsigned char* buff, unsigned int size)
     287static int smart_write(struct s_reader *reader, unsigned char* buff, unsigned int size)
    286288{
    287289
     
    390392{
    391393
    392     cs_debug_mask(D_IFD,"IO:SR: Closing smarteader\n");
    393 
    394     reader->sr_config->running=FALSE;
    395     pthread_join(reader->sr_config->rt,NULL);
     394    cs_debug_mask(D_IFD,"IO:SR: Closing smarteader\n");
     395
    396396    libusb_close(reader->sr_config->usb_dev_handle);
    397     libusb_exit(NULL);
     397    if (number_of_smartreaders_running-- <= 0) {//last reader is closed, thread must be closed also
     398      pthread_join(libusb_thread,NULL);
     399      libusb_exit(NULL);
     400    }
    398401    free(reader->sr_config);
    399402    return OK;
     
    422425}
    423426
    424 static void EnableSmartReader(S_READER *reader, int clock, unsigned short Fi, unsigned char Di, unsigned char Ni, unsigned char T, unsigned char inv,int parity) {
     427static void EnableSmartReader(struct s_reader *reader, int clock, unsigned short Fi, unsigned char Di, unsigned char Ni, unsigned char T, unsigned char inv,int parity) {
    425428
    426429    int ret = 0;
     
    615618}
    616619
    617 void smartreader_init(S_READER *reader,uint8_t out_endpoint)
     620void smartreader_init(struct s_reader *reader,uint8_t out_endpoint)
    618621{
    619622    reader->sr_config->usb_dev = NULL;
     
    636639
    637640
    638 static unsigned int smartreader_determine_max_packet_size(S_READER *reader)
     641static unsigned int smartreader_determine_max_packet_size(struct s_reader *reader)
    639642{
    640643    unsigned int packet_size;
     
    684687
    685688
    686 static int smartreader_usb_close_internal (S_READER *reader)
     689static int smartreader_usb_close_internal (struct s_reader *reader)
    687690{
    688691    int ret = 0;
     
    698701
    699702
    700 int smartreader_usb_reset(S_READER *reader)
     703int smartreader_usb_reset(struct s_reader *reader)
    701704{
    702705    if (libusb_control_transfer(reader->sr_config->usb_dev_handle,
     
    717720
    718721
    719 int smartreader_usb_open_dev(S_READER *reader)
     722int smartreader_usb_open_dev(struct s_reader *reader)
    720723{
    721724    int detach_errno = 0;
     
    851854
    852855
    853 int smartreader_usb_purge_rx_buffer(S_READER *reader)
     856int smartreader_usb_purge_rx_buffer(struct s_reader *reader)
    854857{
    855858    if (libusb_control_transfer(reader->sr_config->usb_dev_handle,
     
    869872}
    870873
    871 int smartreader_usb_purge_tx_buffer(S_READER *reader)
     874int smartreader_usb_purge_tx_buffer(struct s_reader *reader)
    872875{
    873876    if (libusb_control_transfer(reader->sr_config->usb_dev_handle,
     
    886889}
    887890
    888 int smartreader_usb_purge_buffers(S_READER *reader)
     891int smartreader_usb_purge_buffers(struct s_reader *reader)
    889892{
    890893    int result;
     
    901904}
    902905
    903 static int smartreader_convert_baudrate(int baudrate, S_READER *reader, unsigned short *value, unsigned short *index)
     906static int smartreader_convert_baudrate(int baudrate, struct s_reader *reader, unsigned short *value, unsigned short *index)
    904907{
    905908    static const char am_adjust_up[8] = {0, 0, 0, 1, 0, 3, 2, 1};
     
    10211024}
    10221025
    1023 int smartreader_set_baudrate(S_READER *reader, int baudrate)
     1026int smartreader_set_baudrate(struct s_reader *reader, int baudrate)
    10241027{
    10251028    unsigned short value, index;
     
    10621065}
    10631066
    1064 int smartreader_setdtr_rts(S_READER *reader, int dtr, int rts)
     1067int smartreader_setdtr_rts(struct s_reader *reader, int dtr, int rts)
    10651068{
    10661069    unsigned short usb_val;
     
    10911094}
    10921095
    1093 int smartreader_setflowctrl(S_READER *reader, int flowctrl)
     1096int smartreader_setflowctrl(struct s_reader *reader, int flowctrl)
    10941097{
    10951098    if (libusb_control_transfer(reader->sr_config->usb_dev_handle,
     
    11081111}
    11091112
    1110 int smartreader_set_line_property2(S_READER *reader, enum smartreader_bits_type bits,
     1113int smartreader_set_line_property2(struct s_reader *reader, enum smartreader_bits_type bits,
    11111114                            enum smartreader_stopbits_type sbit, enum smartreader_parity_type parity,
    11121115                            enum smartreader_break_type break_type)
     
    11721175
    11731176
    1174 int smartreader_set_line_property(S_READER *reader, enum smartreader_bits_type bits,
     1177int smartreader_set_line_property(struct s_reader *reader, enum smartreader_bits_type bits,
    11751178                           enum smartreader_stopbits_type sbit, enum smartreader_parity_type parity)
    11761179{
     
    11801183
    11811184
    1182 void smart_flush(S_READER *reader)
     1185void smart_flush(struct s_reader *reader)
    11831186{
    11841187
     
    11911194}
    11921195
    1193 static int smartreader_set_latency_timer(S_READER *reader, unsigned short latency)
     1196static int smartreader_set_latency_timer(struct s_reader *reader, unsigned short latency)
    11941197{
    11951198    unsigned short usb_val;
     
    12771280   
    12781281    reader = (struct s_reader *)p;
    1279     reader->sr_config->running=TRUE;
    12801282
    12811283    for(idx=0; idx<NUM_TXFERS; idx++) {
     
    12941296    }
    12951297
    1296     while(reader->sr_config->running) {
     1298    while(number_of_smartreaders_running > 0) {
    12971299        ret = libusb_handle_events(NULL);
    12981300        if(ret!=0)
  • trunk/globals.h

    r3998 r4015  
    415415    int parity;
    416416    int irdeto;
    417     int running;
    418417    libusb_device *usb_dev;
    419418    libusb_device_handle *usb_dev_handle;
     
    436435    pthread_mutex_t g_read_mutex;
    437436    pthread_mutex_t g_usb_mutex;
    438     pthread_t rt;
    439437    unsigned char modem_status;
    440438} SR_CONFIG;
Note: See TracChangeset for help on using the changeset viewer.