/[pcsclite]/trunk/Drivers/ccid/src/ccid_serial.c
ViewVC logotype

Diff of /trunk/Drivers/ccid/src/ccid_serial.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 414 by rousseau, Wed Sep 10 09:17:43 2003 UTC revision 608 by rousseau, Thu Jan 15 08:30:31 2004 UTC
# Line 40  Line 40 
40  #include "utils.h"  #include "utils.h"
41    
42  /* communication timeout in seconds */  /* communication timeout in seconds */
43  #define SERIAL_TIMEOUT 10  #define SERIAL_TIMEOUT 2
44    
45  #define SYNC 0x03  #define SYNC 0x03
46  #define CTRL_ACK 0x06  #define CTRL_ACK 0x06
# Line 67  Line 67 
67   * 1 : LRC (0x16)   * 1 : LRC (0x16)
68   *   *
69   * Card insertion/withdrawal   * Card insertion/withdrawal
  * 1 : SYNC  
  * 1 : CTRL  
70   * 1 : RDR_to_PC_NotifySlotChange (0x50)   * 1 : RDR_to_PC_NotifySlotChange (0x50)
71   * 1 : bmSlotIccState   * 1 : bmSlotIccState
72   *     0x02 if card absent   *     0x02 if card absent
73   *     0x03 is card present   *     0x03 is card present
  * 1 : LRC  
74   *   *
75   * Time request   * Time request
76   * T=1 : normal CCID command   * T=1 : normal CCID command
# Line 81  Line 78 
78   *   *
79   */   */
80    
81    /*
82     * You may get read timeout after a card movement.
83     * This is because you will get the echo of the CCID command
84     * but not the result of the command.
85     *
86     * This is not an applicative issue since the card is either removed (and
87     * powered off) or just inserted (and not yet powered on).
88     */
89    
90    /* 271 = max size for short APDU
91     * 2 bytes for header
92     * 1 byte checksum
93     * doubled for echo
94     */
95    #define GEMPCTWIN_MAXBUF (271 +2 +1) * 2
96    
97  typedef struct  typedef struct
98  {  {
99          /*          /*
# Line 94  typedef struct Line 107  typedef struct
107          int channel;          int channel;
108    
109          /*          /*
110             * serial communication buffer
111             */
112            unsigned char buffer[GEMPCTWIN_MAXBUF];
113    
114            /*
115             * next available byte
116             */
117            int buffer_offset;
118    
119            /*
120             * number of available bytes
121             */
122            int buffer_offset_last;
123    
124            /*
125           * CCID infos common to USB and serial           * CCID infos common to USB and serial
126           */           */
127          _ccid_descriptor ccid;          _ccid_descriptor ccid;
# Line 107  static _serialDevice serialDevice[PCSCLI Line 135  static _serialDevice serialDevice[PCSCLI
135          [ 0 ... (PCSCLITE_MAX_READERS-1) ] = { -1, -1 }          [ 0 ... (PCSCLITE_MAX_READERS-1) ] = { -1, -1 }
136  };  };
137    
 /* 271 = max size for short APDU  
  * 2 bytes for header  
  * 1 byte checksum  
  * doubled for echo */  
 #define GEMPCTWIN_MAXBUF (271 +2 +1) *2  
   
138  /*****************************************************************************  /*****************************************************************************
139   *   *
140   *                              WriteSerial: Send bytes to the card reader   *                              WriteSerial: Send bytes to the card reader
# Line 121  static _serialDevice serialDevice[PCSCLI Line 143  static _serialDevice serialDevice[PCSCLI
143  status_t WriteSerial(int lun, int length, unsigned char *buffer)  status_t WriteSerial(int lun, int length, unsigned char *buffer)
144  {  {
145          int i;          int i;
         int reader = LunToReaderIndex(lun);  
146          unsigned char lrc;          unsigned char lrc;
147          unsigned char low_level_buffer[GEMPCTWIN_MAXBUF];          unsigned char low_level_buffer[GEMPCTWIN_MAXBUF];
148    
# Line 133  status_t WriteSerial(int lun, int length Line 154  status_t WriteSerial(int lun, int length
154    
155          if (length > GEMPCTWIN_MAXBUF-3)          if (length > GEMPCTWIN_MAXBUF-3)
156          {          {
157                  DEBUG_CRITICAL3("WriteSerial: command too long: %d for max %d",                  DEBUG_CRITICAL3("command too long: %d for max %d",
158                          length, GEMPCTWIN_MAXBUF-3);                          length, GEMPCTWIN_MAXBUF-3);
159                  return STATUS_UNSUCCESSFUL;                  return STATUS_UNSUCCESSFUL;
160          }          }
# Line 155  status_t WriteSerial(int lun, int length Line 176  status_t WriteSerial(int lun, int length
176          DEBUG_XXD(debug_header, low_level_buffer, length+3);          DEBUG_XXD(debug_header, low_level_buffer, length+3);
177  #endif  #endif
178    
179          if (write(serialDevice[reader].fd, low_level_buffer, length+3) != length+3)          if (write(serialDevice[LunToReaderIndex(lun)].fd, low_level_buffer,
180                    length+3) != length+3)
181          {          {
182                  DEBUG_CRITICAL2("WriteSerial: write error: %s", strerror(errno));                  DEBUG_CRITICAL2("write error: %s", strerror(errno));
183                  return STATUS_UNSUCCESSFUL;                  return STATUS_UNSUCCESSFUL;
184          }          }
185    
# Line 172  status_t WriteSerial(int lun, int length Line 194  status_t WriteSerial(int lun, int length
194   *****************************************************************************/   *****************************************************************************/
195  status_t ReadSerial(int lun, int *length, unsigned char *buffer)  status_t ReadSerial(int lun, int *length, unsigned char *buffer)
196  {  {
197          int fd = serialDevice[LunToReaderIndex(lun)].fd;          unsigned char c;
198          static unsigned char low_level_buffer[GEMPCTWIN_MAXBUF];          int rv;
199          int buffer_size, len, rv, already_read, to_read;          int echo;
200          unsigned char *reader_to_pc;          int to_read;
201          unsigned char lrc;          int i;
         int skipped, i;  
 #ifdef DEBUG_LEVEL_COMM  
         char debug_header[] = "<- 121234 ";  
202    
203          sprintf(debug_header, "<- %06X ", (int)lun);          /* we get the echo first */
204  #endif          echo = TRUE;
205    
206          buffer_size = *length;  start:
207            DEBUG_COMM("start");
208            if ((rv = get_bytes(lun, &c, 1)) != STATUS_SUCCESS)
209                    return rv;
210    
211          /* error by default (zero length) */          if (c == RDR_to_PC_NotifySlotChange)
212          *length = 0;                  goto slot_change;
213    
214          rv = ReadChunk(fd, low_level_buffer, sizeof(low_level_buffer), 3, lun);          if (c == SYNC)
215          if (rv < 0)                  goto sync;
                 return STATUS_UNSUCCESSFUL;  
216    
217          /* Nb of bytes already read */          if (c >= 0x80)
218          already_read = rv;          {
219                    DEBUG_COMM2("time request: 0x%02X", c);
220                    goto start;
221            }
222    
223  #ifdef DEBUG_LEVEL_COMM          DEBUG_CRITICAL2("Got 0x%02X", c);
224          DEBUG_XXD(debug_header, low_level_buffer, rv);          return STATUS_COMM_ERROR;
 #endif  
225    
226          /* skip the echo of the previous command */  slot_change:
227          skipped = skip_echo(low_level_buffer, sizeof(low_level_buffer));          DEBUG_COMM("slot change");
228          if (skipped == 0)          if ((rv = get_bytes(lun, &c, 1)) != STATUS_SUCCESS)
229                  return STATUS_UNSUCCESSFUL;                  return rv;
230    
231          /* Nb of bytes until the end of the buffer */          if (c == CARD_ABSENT)
232          len = sizeof(low_level_buffer) - skipped;                  DEBUG_COMM("Card removed");
233          already_read -= skipped;          else
234          reader_to_pc = low_level_buffer + skipped;                  if (c == CARD_PRESENT)
235                            DEBUG_COMM("Card inserted");
236                    else
237                            DEBUG_COMM2("Unknown card movement: %d", buffer[3]);
238            goto start;
239    
240          to_read = 2 +1; /* minimal frame size */  sync:
241            DEBUG_COMM("sync");
242            if ((rv = get_bytes(lun, &c, 1)) != STATUS_SUCCESS)
243                    return rv;
244    
245          while (already_read < to_read)          if (c == CTRL_ACK)
246          {                  goto ack;
                 rv = ReadChunk(fd, reader_to_pc + already_read, len, 1, lun);  
                 if (rv < 0)  
                         return STATUS_UNSUCCESSFUL;  
247    
248                  already_read += rv;          if (c == CTRL_NAK)
249                  len -= rv;                  goto nak;
         }  
250    
251          if (reader_to_pc[0] != SYNC)          DEBUG_CRITICAL2("Got 0x%02X instead of ACK/NAK", c);
252          {          return STATUS_COMM_ERROR;
                 DEBUG_CRITICAL2("No SYNC byte found: 0x%02X", buffer[0]);  
                 return STATUS_UNSUCCESSFUL;  
         }  
253    
254          if (reader_to_pc[1] != CTRL_ACK)  nak:
255          {          DEBUG_COMM("nak");
256                  DEBUG_CRITICAL2("No ACK byte found: 0x%02X", buffer[0]);          if ((rv = get_bytes(lun, &c, 1)) != STATUS_SUCCESS)
257                  return 0;                  return rv;
         }  
258    
259          /* read up to CCID frame length */          if (c != (SYNC ^ CTRL_NAK))
         to_read = 5; /* bMessageType + dwLength */  
         while (already_read < to_read)  
260          {          {
261                  rv = ReadChunk(fd, reader_to_pc + already_read, len, 1, lun);                  DEBUG_CRITICAL2("Wrong LRC: 0x%02X", c);
262                  if (rv < 0)                  return STATUS_COMM_ERROR;
                         return STATUS_UNSUCCESSFUL;  
   
                 already_read += rv;  
                 len -= rv;  
263          }          }
264            else
265                    goto start;
266    
267          /* normal CCID command */  ack:
268          to_read = 10+3+dw2i(reader_to_pc, 3);          DEBUG_COMM("ack");
269            /* normal CCID frame */
270            if ((rv = get_bytes(lun, buffer, 5)) != STATUS_SUCCESS)
271                    return rv;
272    
273            /* total frame size */
274            to_read = 10+dw2i(buffer, 1);
275    
276            DEBUG_COMM2("frame size: %d", to_read);
277            if ((rv = get_bytes(lun, buffer+5, to_read-5)) != STATUS_SUCCESS)
278                    return rv;
279    
280          while (already_read < to_read)  #ifdef DEBUG_LEVEL_COMM
281          {                  DEBUG_XXD("frame: ", buffer, to_read);
282                  rv = ReadChunk(fd, reader_to_pc + already_read, len, 1, lun);  #endif
                 if (rv < 0)  
                         return STATUS_UNSUCCESSFUL;  
283    
284                  already_read += rv;          /* lrc */
285                  len -= rv;          DEBUG_COMM("lrc");
286          }          if ((rv = get_bytes(lun, &c, 1)) != STATUS_SUCCESS)
287                    return rv;
288    
289          lrc = 0;          DEBUG_COMM2("lrc: 0x%02X", c);
290          for (i=0; i<to_read; i++)          for (i=0; i<to_read; i++)
291                  lrc ^= reader_to_pc[i];                  c ^= buffer[i];
   
         if (lrc != 0)  
                 DEBUG_CRITICAL2("Wrong lrc: 0x%02X", lrc);  
292    
293  #ifdef DEBUG_LEVEL_COMM          if (c != (SYNC ^ CTRL_ACK))
294          DEBUG_XXD(debug_header, reader_to_pc, to_read);          {
295  #endif                  DEBUG_CRITICAL2("Wrong LRC: 0x%02X", c);
296                    //return STATUS_COMM_ERROR;
297          /* copy up to buffer_size bytes */          }
         *length = to_read-3;  
298    
299          memcpy(buffer, reader_to_pc+2, *length);          if (echo)
300            {
301                    echo = FALSE;
302                    goto start;
303            }
304    
305          return STATUS_SUCCESS;          return STATUS_SUCCESS;
306  } /* ReadSerial */  } /* ReadSerial */
# Line 281  status_t ReadSerial(int lun, int *length Line 308  status_t ReadSerial(int lun, int *length
308    
309  /*****************************************************************************  /*****************************************************************************
310   *   *
311   *                              skip_echo: skip the echo of the previous command   *                              get_bytes: get n bytes
312   *   *
313   *****************************************************************************/   *****************************************************************************/
314  int skip_echo(unsigned char *buffer, int buffer_length)  int get_bytes(int lun, unsigned char *buffer, int length)
315  {  {
316          unsigned char lrc;          int offset = serialDevice[LunToReaderIndex(lun)].buffer_offset;
317          int i;          int offset_last = serialDevice[LunToReaderIndex(lun)].buffer_offset_last;
318    
319          if (buffer[0] != SYNC)          DEBUG_COMM3("available: %d, needed: %d", offset_last-offset,
320          {                  length);
321                  DEBUG_CRITICAL2("No SYNC byte found: 0x%02X", buffer[0]);          /* enough data are available */
322                  return 0;          if (offset + length <= offset_last)
323            {
324                    DEBUG_COMM("data available");
325                    memcpy(buffer, serialDevice[LunToReaderIndex(lun)].buffer + offset, length);
326                    serialDevice[LunToReaderIndex(lun)].buffer_offset += length;
327          }          }
328            else
         if (buffer[1] != CTRL_ACK)  
329          {          {
330                  DEBUG_CRITICAL2("No ACK byte found: 0x%02X", buffer[0]);                  int present, rv;
                 return 0;  
         }  
331    
332          if (buffer[2] == RDR_to_PC_NotifySlotChange)                  /* copy available data */
333          {                  present = offset_last - offset;
334                  switch (buffer[3])  
335                    if (present > 0)
336                  {                  {
337                          case CARD_PRESENT:                          DEBUG_COMM2("some data available: %d", present);
338                                  DEBUG_INFO("Card inserted");                          memcpy(buffer, serialDevice[LunToReaderIndex(lun)].buffer + offset,
339                                  break;                                  present);
   
                         case CARD_ABSENT:  
                                 DEBUG_INFO("Card removed");  
                                 break;  
   
                         default:  
                                 DEBUG_INFO2("Unknown card movement: %d", buffer[3]);  
                                 break;  
340                  }                  }
   
                 lrc = 0;  
                 for (i=0; i<5; i++)  
                         lrc ^= buffer[i];  
341    
342                  if (lrc != 0)                  /* get fresh data */
343                          DEBUG_CRITICAL2("Wrong lrc: 0x%02", lrc);                  DEBUG_COMM2("get more data: %d", length - present);
344                    rv = ReadChunk(lun, serialDevice[LunToReaderIndex(lun)].buffer, sizeof(serialDevice[LunToReaderIndex(lun)].buffer), length - present);
345                    if (rv < 0)
346                            return STATUS_COMM_ERROR;
347    
348                  /* Card insertion/withdrawal */                  /* fill the buffer */
349                  return 5;                  memcpy(buffer + present, serialDevice[LunToReaderIndex(lun)].buffer,
350                            length - present);
351                    serialDevice[LunToReaderIndex(lun)].buffer_offset = length - present;
352                    serialDevice[LunToReaderIndex(lun)].buffer_offset_last = rv;
353                    DEBUG_COMM3("offset: %d, last_offset: %d",
354                            serialDevice[LunToReaderIndex(lun)].buffer_offset,
355                            serialDevice[LunToReaderIndex(lun)].buffer_offset_last);
356          }          }
357    
358          /* normal CCID command, 2 for SYNC CTRL, 1 for length offset in CCID cmd */          return STATUS_SUCCESS;
359          return 2 + 10 + dw2i(buffer, 2 +1) +1;  } /* get_bytes */
 } /* skip_echo */  
360    
361    
362  /*****************************************************************************  /*****************************************************************************
# Line 339  int skip_echo(unsigned char *buffer, int Line 364  int skip_echo(unsigned char *buffer, int
364   *                              ReadChunk: read a minimum number of bytes   *                              ReadChunk: read a minimum number of bytes
365   *   *
366   *****************************************************************************/   *****************************************************************************/
367  int ReadChunk(int fd, unsigned char *buffer, int buffer_length, int min_length, int lun)  int ReadChunk(int lun, unsigned char *buffer, int buffer_length, int min_length)
368  {  {
369            int fd = serialDevice[LunToReaderIndex(lun)].fd;
370          fd_set fdset;          fd_set fdset;
371          struct timeval t;          struct timeval t;
372          int i, rv;          int i, rv = 0;
373            int already_read;
374  #ifdef DEBUG_LEVEL_COMM  #ifdef DEBUG_LEVEL_COMM
375          char debug_header[] = "<- 121234 ";          char debug_header[] = "<- 121234 ";
376    
377          sprintf(debug_header, "<- %06X ", (int)lun);          sprintf(debug_header, "<- %06X ", (int)lun);
378  #endif  #endif
379    
380  time_request:          already_read = 0;
381          /* use select() to, eventually, timeout */          while (already_read < min_length)
         FD_ZERO(&fdset);  
         FD_SET(fd, &fdset);  
         t.tv_sec = SERIAL_TIMEOUT;  
         t.tv_usec = 0;  
   
         i = select(fd+1, &fdset, NULL, NULL, &t);  
         if (i == -1)  
382          {          {
383                  DEBUG_CRITICAL2("select: %s", strerror(errno));                  /* use select() to, eventually, timeout */
384                  return -1;                  FD_ZERO(&fdset);
385          }                  FD_SET(fd, &fdset);
386          else                  t.tv_sec = SERIAL_TIMEOUT;
387                  if (i == 0)                  t.tv_usec = 0;
388    
389                    i = select(fd+1, &fdset, NULL, NULL, &t);
390                    if (i == -1)
391                  {                  {
392                          DEBUG_COMM2("Timeout! (%d sec)", SERIAL_TIMEOUT);                          DEBUG_CRITICAL2("select: %s", strerror(errno));
393                          return -1;                          return -1;
394                  }                  }
395                    else
396                            if (i == 0)
397                            {
398                                    DEBUG_COMM2("Timeout! (%d sec)", SERIAL_TIMEOUT);
399                                    return -1;
400                            }
401    
402          rv = read(fd, buffer, buffer_length);                  rv = read(fd, buffer + already_read, buffer_length - already_read);
403          if (rv < 0)                  if (rv < 0)
404          {                  {
405                  DEBUG_COMM2("read error: %s", strerror(errno));                          DEBUG_COMM2("read error: %s", strerror(errno));
406                  return -1;                          return -1;
407          }                  }
   
         if ((rv == 1) && (buffer[0] >= 0x80))  
         {  
                 DEBUG_COMM2("Time request: 0x%02X", buffer[0]);  
                 goto time_request;  
         }  
408    
         /* too short */  
         if (rv < min_length)  
         {  
409  #ifdef DEBUG_LEVEL_COMM  #ifdef DEBUG_LEVEL_COMM
410                  DEBUG_XXD(debug_header, buffer, rv);                  DEBUG_XXD(debug_header, buffer + already_read, rv);
411  #endif  #endif
                 DEBUG_COMM3("ReadSerial: only %d byte(s) read, needed %d", rv,  
                         min_length);  
412    
413                  return -1;                  already_read += rv;
414                    DEBUG_COMM3("read: %d, to read: %d", already_read,
415                            min_length);
416          }          }
417    
418          return rv;          return already_read;
419  } /* ReadChunk */  } /* ReadChunk */
420    
421    
# Line 411  status_t OpenSerial(int lun, int channel Line 431  status_t OpenSerial(int lun, int channel
431          int i;          int i;
432          int reader = LunToReaderIndex(lun);          int reader = LunToReaderIndex(lun);
433    
434          DEBUG_COMM3("OpenSerial: Lun: %X, Channel: %d", lun, channel);          DEBUG_COMM3("Lun: %X, Channel: %d", lun, channel);
435    
436          /*          /*
437           * Conversion of old-style ifd-hanler 1.0 CHANNELID           * Conversion of old-style ifd-hanler 1.0 CHANNELID
# Line 503  status_t OpenSerial(int lun, int channel Line 523  status_t OpenSerial(int lun, int channel
523          serialDevice[reader].ccid.dwMaxCCIDMessageLength = 271;          serialDevice[reader].ccid.dwMaxCCIDMessageLength = 271;
524          serialDevice[reader].ccid.dwFeatures = 0x00010230;          serialDevice[reader].ccid.dwFeatures = 0x00010230;
525    
526            serialDevice[reader].buffer_offset = 0;
527            serialDevice[reader].buffer_offset_last = 0;
528    
529          ccid_open_hack(lun);          ccid_open_hack(lun);
530    
531          return STATUS_SUCCESS;          return STATUS_SUCCESS;

Legend:
Removed from v.414  
changed lines
  Added in v.608

  ViewVC Help
Powered by ViewVC 1.1.5