home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / usbd0906.zip / usb_20020906.zip / usbcalls / samples / Cardreader / usbserial.cpp < prev    next >
C/C++ Source or Header  |  2001-11-13  |  2KB  |  93 lines

  1. /***********************************************************
  2. /       Title: usbserial.c
  3. /       Author: David Corcoran
  4. /      Purpose: Abstracts usb API to serial like calls
  5. ************************************************************/
  6.  
  7. #include <stdio.h>
  8. #include <pcscdefines.h>
  9. #include <usbserial.h>
  10. #include <stdlib.h>
  11. #include <usblinux.h>
  12.  
  13. // #define USBDEBUG    1
  14. #define USBWRITE_PIPE   0x06
  15. #define USBREAD_PIPE    0x85
  16. #define USBMAX_READERS  4
  17.  
  18. /* Change the following to uniquely match your reader. */
  19. enum {
  20.     kMyVendorID            = 0x08E6,   /* change to match your reader */
  21.     kMyProductID        = 0x0430,   /* change to match your reader */
  22. };
  23.  
  24. static int usbDevice = 0;
  25.  
  26. RESPONSECODE OpenUSB( DWORD lun )
  27. {
  28.   int rv;
  29.  
  30.   usbDevice = open_linux_usb_dev( kMyVendorID, kMyProductID, lun );
  31.  
  32.   if ( usbDevice <= 0 ) {
  33.     return STATUS_UNSUCCESSFUL;
  34.   }
  35.  
  36.   return STATUS_SUCCESS;  
  37. }
  38.  
  39. RESPONSECODE WriteUSB( DWORD lun, DWORD length, unsigned char *buffer )
  40. {
  41.   int rv, len;
  42.   int i;
  43.   
  44. #ifdef USBDEBUG
  45.   printf("-> ");
  46.   for (i=0; i < length; i++ ) {
  47.     printf("%x ", buffer[i]);
  48.   } printf("\n");
  49. #endif  
  50.  
  51.   len = length;
  52.   
  53.   rv = bulk_linux_usb_dev( usbDevice, USBWRITE_PIPE, buffer, 
  54.                &len, 100000 );
  55.   
  56.   
  57.   if ( rv < 0 ) {
  58.     return STATUS_UNSUCCESSFUL;
  59.   }
  60.  
  61.   return STATUS_SUCCESS;
  62. }
  63.  
  64. RESPONSECODE ReadUSB( DWORD lun, DWORD *length, unsigned char *buffer )
  65. {
  66.   int rv, len, i;
  67.  
  68.   len = 256;
  69.  
  70.   rv = bulk_linux_usb_dev( usbDevice, USBREAD_PIPE, buffer, 
  71.                &len, 10000 );
  72.   *length = len;
  73.  
  74.   if ( rv < 0 ) {
  75.     return STATUS_UNSUCCESSFUL;
  76.   }
  77.  
  78. #ifdef USBDEBUG
  79.   printf("<- ");
  80.   for (i=0; i < len; i++ ) {
  81.     printf("%x ", buffer[i]);
  82.   } printf("\n");
  83. #endif  
  84.  
  85.   return STATUS_SUCCESS;
  86. }
  87.  
  88. RESPONSECODE CloseUSB( DWORD lun )
  89. {
  90.     close_linux_usb_dev( usbDevice );
  91.     return STATUS_SUCCESS;
  92. }
  93.