/[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 461 by rousseau, Fri Sep 19 12:08:09 2003 UTC revision 462 by rousseau, Mon Sep 22 14:24:26 2003 UTC
# Line 204  status_t ReadSerial(int lun, int *length Line 204  status_t ReadSerial(int lun, int *length
204          echo = TRUE;          echo = TRUE;
205    
206  start:  start:
207            DEBUG_COMM("start");
208          if ((rv = get_bytes(lun, &c, 1)) != STATUS_SUCCESS)          if ((rv = get_bytes(lun, &c, 1)) != STATUS_SUCCESS)
209                  return rv;                  return rv;
210    
# Line 214  start: Line 215  start:
215                  goto sync;                  goto sync;
216    
217          if (c >= 0x80)          if (c >= 0x80)
218            {
219                    DEBUG_COMM2("time request: 0x%02X", c);
220                  goto start;                  goto start;
221            }
222    
223          DEBUG_CRITICAL2("Got 0x%02X", c);          DEBUG_CRITICAL2("Got 0x%02X", c);
224          return STATUS_COMM_ERROR;          return STATUS_COMM_ERROR;
225    
226  slot_change:  slot_change:
227            DEBUG_COMM("slot change");
228          if ((rv = get_bytes(lun, &c, 1)) != STATUS_SUCCESS)          if ((rv = get_bytes(lun, &c, 1)) != STATUS_SUCCESS)
229                  return rv;                  return rv;
230    
# Line 233  slot_change: Line 238  slot_change:
238          goto start;          goto start;
239    
240  sync:  sync:
241            DEBUG_COMM("sync");
242          if ((rv = get_bytes(lun, &c, 1)) != STATUS_SUCCESS)          if ((rv = get_bytes(lun, &c, 1)) != STATUS_SUCCESS)
243                  return rv;                  return rv;
244    
# Line 246  sync: Line 252  sync:
252          return STATUS_COMM_ERROR;          return STATUS_COMM_ERROR;
253    
254  nak:  nak:
255            DEBUG_COMM("nak");
256          if ((rv = get_bytes(lun, &c, 1)) != STATUS_SUCCESS)          if ((rv = get_bytes(lun, &c, 1)) != STATUS_SUCCESS)
257                  return rv;                  return rv;
258    
# Line 258  nak: Line 265  nak:
265                  goto start;                  goto start;
266    
267  ack:  ack:
268            DEBUG_COMM("ack");
269          /* normal CCID frame */          /* normal CCID frame */
270          if ((rv = get_bytes(lun, buffer, 5)) != STATUS_SUCCESS)          if ((rv = get_bytes(lun, buffer, 5)) != STATUS_SUCCESS)
271                  return rv;                  return rv;
# Line 265  ack: Line 273  ack:
273          /* total frame size */          /* total frame size */
274          to_read = 10+dw2i(buffer, 1);          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)          if ((rv = get_bytes(lun, buffer+5, to_read-5)) != STATUS_SUCCESS)
278                  return rv;                  return rv;
279    
280    #ifdef DEBUG_LEVEL_COMM
281                    DEBUG_XXD("frame: ", buffer, to_read);
282    #endif
283    
284          /* lrc */          /* lrc */
285            DEBUG_COMM("lrc");
286          if ((rv = get_bytes(lun, &c, 1)) != STATUS_SUCCESS)          if ((rv = get_bytes(lun, &c, 1)) != STATUS_SUCCESS)
287                  return rv;                  return rv;
288    
289            DEBUG_COMM2("lrc: 0x%02X", c);
290          for (i=0; i<to_read; i++)          for (i=0; i<to_read; i++)
291                  c ^= buffer[i];                  c ^= buffer[i];
292    
# Line 301  int get_bytes(int lun, unsigned char *bu Line 316  int get_bytes(int lun, unsigned char *bu
316          int offset = serialDevice[LunToReaderIndex(lun)].buffer_offset;          int offset = serialDevice[LunToReaderIndex(lun)].buffer_offset;
317          int offset_last = serialDevice[LunToReaderIndex(lun)].buffer_offset_last;          int offset_last = serialDevice[LunToReaderIndex(lun)].buffer_offset_last;
318    
319            DEBUG_COMM3("get_bytes: available: %d, needed: %d", offset_last-offset,
320                    length);
321          /* enough data are available */          /* enough data are available */
322          if (offset + length <= offset_last)          if (offset + length <= offset_last)
323          {          {
324                    DEBUG_COMM("get_bytes: data available");
325                  memcpy(buffer, serialDevice[LunToReaderIndex(lun)].buffer + offset, length);                  memcpy(buffer, serialDevice[LunToReaderIndex(lun)].buffer + offset, length);
326                  serialDevice[LunToReaderIndex(lun)].buffer_offset += length;                  serialDevice[LunToReaderIndex(lun)].buffer_offset += length;
327          }          }
# Line 315  int get_bytes(int lun, unsigned char *bu Line 333  int get_bytes(int lun, unsigned char *bu
333                  present = offset_last - offset;                  present = offset_last - offset;
334    
335                  if (present > 0)                  if (present > 0)
336                          memcpy(buffer, serialDevice[LunToReaderIndex(lun)].buffer + offset, present);                  {
337                            DEBUG_COMM2("get_bytes: some data available: %d", present);
338                            memcpy(buffer, serialDevice[LunToReaderIndex(lun)].buffer + offset,
339                                    present);
340                    }
341    
342                  /* get fresh data */                  /* get fresh data */
343                    DEBUG_COMM2("get_bytes: get more data: %d", length - present);
344                  rv = ReadChunk(lun, serialDevice[LunToReaderIndex(lun)].buffer, sizeof(serialDevice[LunToReaderIndex(lun)].buffer), length - present);                  rv = ReadChunk(lun, serialDevice[LunToReaderIndex(lun)].buffer, sizeof(serialDevice[LunToReaderIndex(lun)].buffer), length - present);
345                  if (rv < 0)                  if (rv < 0)
346                          return STATUS_COMM_ERROR;                          return STATUS_COMM_ERROR;
# Line 327  int get_bytes(int lun, unsigned char *bu Line 350  int get_bytes(int lun, unsigned char *bu
350                          length - present);                          length - present);
351                  serialDevice[LunToReaderIndex(lun)].buffer_offset = length - present;                  serialDevice[LunToReaderIndex(lun)].buffer_offset = length - present;
352                  serialDevice[LunToReaderIndex(lun)].buffer_offset_last = rv;                  serialDevice[LunToReaderIndex(lun)].buffer_offset_last = rv;
353                    DEBUG_COMM3("get_bytes: offset: %d, last_offset: %d",
354                            serialDevice[LunToReaderIndex(lun)].buffer_offset,
355                            serialDevice[LunToReaderIndex(lun)].buffer_offset_last);
356          }          }
357    
 DEBUG_XXD("pouet: ", buffer, length);  
   
358          return STATUS_SUCCESS;          return STATUS_SUCCESS;
359  } /* get_bytes */  } /* get_bytes */
360    
# Line 345  int ReadChunk(int lun, unsigned char *bu Line 369  int ReadChunk(int lun, unsigned char *bu
369          int fd = serialDevice[LunToReaderIndex(lun)].fd;          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          /* use select() to, eventually, timeout */          already_read = 0;
381          FD_ZERO(&fdset);          while (already_read < min_length)
         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          }                  }
408    
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
412    
413          /* too short */                  already_read += rv;
414          if (rv < min_length)                  DEBUG_COMM3("ReadChunk, read: %d, to read: %d", already_read,
         {  
                 DEBUG_COMM3("ReadSerial: only %d byte(s) read, needed %d", rv,  
415                          min_length);                          min_length);
   
                 return -1;  
416          }          }
417    
418          return rv;          return already_read;
419  } /* ReadChunk */  } /* ReadChunk */
420    
421    

Legend:
Removed from v.461  
changed lines
  Added in v.462

  ViewVC Help
Powered by ViewVC 1.1.5