Guest User

Untitled

a guest
Apr 12th, 2011
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.56 KB | None | 0 0
  1.  /**
  2.  * Defines a simple interface to perform IO with USB devices using libusb.
  3.  *
  4.  * @file USBCom.c
  5.  * @author
  6.  * @author
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <libusb-1.0/libusb.h>
  11. #include "USBCom.h"
  12.  
  13. /**
  14.  * NOTE: Used internally. Not likely used by client programmer.
  15.  *
  16.  * Searches for a USB device with a matching vendorID.  If found, the corresponding
  17.  * libusb device is returned.  If not, null is returned.
  18.  *
  19.  * @param vendorID The vendor ID of the device
  20.  * @return The device handle of a device if a device matching the provided vendor ID is found.  Null otherwise.
  21.  */
  22. libusb_device_handle * USBCom_findDevice(int vendorID)
  23. {
  24.  
  25.     libusb_device ** list;
  26.     libusb_device * found = NULL;
  27.     libusb_device * device;
  28.     libusb_device_handle * handle;
  29.     struct libusb_device_descriptor descriptor;
  30.  
  31.     ssize_t count = libusb_get_device_list(NULL, &list);
  32.     ssize_t i = 0;
  33.     int err = 0;
  34.  
  35.     if(count < 0)
  36.         return -1;
  37.  
  38.     for(i = 0; i < count; i++) {
  39.         device = list[i];
  40.        
  41.         libusb_get_device_descriptor(device, &descriptor);
  42.         if(descriptor.idVendor == vendorID) {
  43.             printf("Found!\n");
  44.             found = device;
  45.  
  46.             break;
  47.         }
  48.            
  49.     }
  50.  
  51.     if(found) {
  52.         err = libusb_open(found, &handle);
  53.        
  54.         if(err)
  55.             return -1;
  56.     }
  57.    
  58.     libusb_free_device_list(list, 1);
  59.    
  60.     return handle;
  61. }
  62.  
  63. /**
  64.  * Initializes libusb and claims the device from the Operating system.  This function
  65.  * MUST be called before any subsequent actions are performed on the USB device.
  66.  *
  67.  * @todo Implement contexts (allows IO with multiple USB devices)
  68.  * @param dev USBDevice struct that describes the connected device.  Used throughout library.
  69.  * @return 0 on Success
  70.  */
  71. int USBCom_setup(USBCom_USBDevice * dev)
  72. {
  73.     int error;
  74.    
  75.     libusb_init(NULL);
  76.    
  77.     dev->device = USBCom_findDevice(dev->vendorID);
  78.    
  79.     if(libusb_kernel_driver_active(dev->device, 0) == 1)
  80.         error = libusb_detach_kernel_driver(dev->device, 0);
  81.        
  82.     //printf("Error code: %d\n", error);
  83.     libusb_claim_interface(dev->device, 0);
  84.     //printf("Error code: %d\n", error);
  85.    
  86.     return error;
  87.    
  88. }
  89.  
  90. /**
  91.  * Clean up and exit libusb.  Call this function after all IO tasks have finished.
  92.  *
  93.  * @todo Implement contexts
  94.  * @param dev The device descriptor
  95.  * @ return 0 on Success
  96.  */
  97. int USBCom_cleanup(USBCom_USBDevice * dev)
  98. {
  99.     int error;
  100.    
  101.     error = libusb_attach_kernel_driver(dev->device, 0);
  102.     libusb_exit(NULL);
  103.    
  104.     return error;
  105. }
  106.  
  107. /**
  108.  * Perform a bulk transfer on a USB device.  On success, the buffer is filled
  109.  * and the number of bytes transferred is returned.  A call to USBCom_setup must precede
  110.  * this function.
  111.  *
  112.  * @param buffer The buffer to which data is transfered.
  113.  * @param bufferLength The length of the data buffer (number supplied to malloc())
  114.  * @param dev The usb device descriptor
  115.  * @retuen The number of bytes transfered.
  116.  */
  117. int USBCom_bulkGet(char * buffer, int bufferLength, USBCom_USBDevice * dev)
  118. {
  119.     int numBytes, error = 0;
  120.  
  121.     error = libusb_bulk_transfer(dev->device, (dev->inEndpoint | LIBUSB_ENDPOINT_IN), buffer, bufferLength, &numBytes, 0);
  122.    
  123.     return numBytes;
  124. }
  125.  
  126.  
  127. int USBCom_bulkSend(char * buffer, int bufferLength, USBCom_USBDevice * dev)
  128. {
  129.     int numBytes, error = 0;
  130.    
  131.     error = libusb_bulk_transfer(dev->device, (dev->outEndpoint | LIBUSB_ENDPOINT_OUT), buffer, bufferLength, &numBytes, 0);
  132.    
  133.     return numBytes;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment