title: libusb data transfer
body: <p>Hii I have to make a code to read data from usb drive which can be pendrive and later a data acquisition card . i have written this much of code which detects all usb connection n print their info. Altough i don't know how to proceed further . I m also confused as to reading data from say pendrive means as in opening some files or what? Please also tell how to find endpoint of device currently i'm jsut using hit n trial to find it .
  PLEASE help me out .I have read whole documentation on synchronous and asynchronous I/O.</p>
  
  <pre><code>enter code here    
  
    #include &lt;stdlib.h&gt;
    #include &lt;stdio.h&gt;
    #include &lt;string.h&gt;
    #include &lt;libusb-1.0/libusb.h&gt;
  
   //=========================================================================
   // This program detects usb and print out their details
   //=========================================================================
  
    int main (int argc, char *argv)
     {
  libusb_device                    **devList = NULL;
  libusb_device                    *devPtr = NULL;
  libusb_device_handle             *devHandle = NULL;
  libusb_context                   *ctx = NULL;            //a libusb session
  struct libusb_device_descriptor  devDesc;
  
  unsigned char              strDesc[256];
  ssize_t                    numUsbDevs = 0;      // pre-initialized scalars
  ssize_t                    idx = 0;
   int                        retVal = 0;
  
  //========================================================================
  // test out the libusb functions
  //========================================================================
  
   printf ("*************************\n USB Detection             Program:\n*************************\n");
   retVal = libusb_init (&amp;ctx);
  
   if(retVal &lt; 0) {
      printf ("%d",retVal," Init Error "); //there was an error
          return 1;
            }
  
    //========================================================================
   // Get the list of USB devices visible to the system.
   //========================================================================
  
     numUsbDevs = libusb_get_device_list (ctx, &amp;devList);
  
  //========================================================================
  // Loop through the list, looking for the device 
  //========================================================================
  
   while (idx &lt; numUsbDevs)
    {
    printf ("\n\n[%d]\n", idx+1);
  
    //=====================================================================
    // Get next device pointer out of the list, use it to open the device.
    //=====================================================================
  
    devPtr = devList[idx];
  
    retVal = libusb_open (devPtr, &amp;devHandle);
    if (retVal != LIBUSB_SUCCESS)
       break;
  
    //=====================================================================
    // Get the device descriptor for this device.
    //=====================================================================
  
    retVal = libusb_get_device_descriptor (devPtr, &amp;devDesc);
    if (retVal != LIBUSB_SUCCESS)
       break;
  
    //=====================================================================
    // Get the string associated with iManufacturer index.
    //=====================================================================
  
    printf ("iManufacturer = %d", devDesc.iManufacturer);
    if (devDesc.iManufacturer &gt; 0)
    {
       retVal = libusb_get_string_descriptor_ascii
                (devHandle, devDesc.iManufacturer, strDesc, 256);
       if (retVal &lt; 0)
          break;
  
       printf ("     string = %s",  strDesc);
    }
  
    //========================================================================
    // Get string associated with iProduct index.
    //========================================================================
  
    printf ("  \niProduct      = %d", devDesc.iProduct);
    if (devDesc.iProduct &gt; 0)
    {
       retVal = libusb_get_string_descriptor_ascii
                (devHandle, devDesc.iProduct, strDesc, 256);
       if (retVal &lt; 0)
          break;
  
       printf ("     string = %s", strDesc);
    }
  
    //==================================================================
    // Get string associated with iSerialNumber index.
    //==================================================================
  
    printf ("  \niSerialNumber = %d", devDesc.iSerialNumber);
    if (devDesc.iSerialNumber &gt; 0)
    {
       retVal = libusb_get_string_descriptor_ascii
                (devHandle, devDesc.iSerialNumber, strDesc, 256);
       if (retVal &lt; 0)
          break;
  
       printf ("     string = %s", strDesc);
    }
  
    //==================================================================
    // Print product id and Vendor id
    //==================================================================
  
      printf ("  \nProductid     = %d", devDesc.idProduct);
      printf ("  \nVendorid      = %d", devDesc.idVendor);
  
    //========================================================================
    // Close and try next one.
    //========================================================================
  
    libusb_close (devHandle);
    devHandle = NULL;
    idx++;
  
    //========================================================================
    // Selection of device by user
    //========================================================================
  
       if(idx==numUsbDevs) 
              { printf("\n\nselect the device :  ");
                scanf("%d",&amp;idx);
           if(idx &gt; numUsbDevs)
               {printf("Invalid input, Quitting.............");
                break; }    
  
         devPtr = devList[idx-1];
  
         retVal = libusb_open (devPtr, &amp;devHandle);
             if (retVal != LIBUSB_SUCCESS)
             break;
  
        retVal = libusb_get_device_descriptor (devPtr, &amp;devDesc);
             if (retVal != LIBUSB_SUCCESS)
             break;
                 printf ("  \nProductid     = %d", devDesc.idProduct);
                 printf ("  \nVendorid      = %d", devDesc.idVendor);
  
           unsigned char data[4] ; //data to read
  
           //data[0]='a';data[1]='b';data[2]='c';data[3]='d'; //some dummy values
  
            int r; //for return values 
            r = libusb_claim_interface(devHandle, 1); //claim interface 0 (the first) of   device
  
        if(r &lt; 0) {
  
      printf("\nCannot Claim Interface");
  
      return 1;
                  }
  
      printf("\nClaimed Interface");
  
     int actual_length; //used to find out how many bytes were written
  
  
   r = libusb_bulk_transfer(devHandle,LIBUSB_ENDPOINT_IN, data, 2, &amp;actual_length, 0);
  
        if (r == 0 &amp;&amp; actual_length == sizeof(data)) {
  
                // results of the transaction can now be found in the data buffer
               // parse them here and report button press
            } 
        else {
               error();
             } 
  
  
   r = libusb_release_interface(devHandle, 1); //release the claimed interface
  
     if(r!=0) {
  
              printf("\nCannot Release Interface");
  
              return 1;
             }
  
      printf("\nReleased Interface");
  
  
              idx=numUsbDevs +2;
              }
  
     }  // end of while loop
  
     if (devHandle != NULL)
     {
    //========================================================================
    // Close device if left open due to break out of loop on error.
    //========================================================================
  
    libusb_close (devHandle);
   }   
  
  
     libusb_exit (ctx); //close the session
  
     printf ("\n*************************\n        Done\n*************************\n");
     return 0;
     }
  
  //==========================================
  // EOF
  //====================
  </code></pre>