Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Defines a simple interface to perform IO with USB devices using libusb.
- *
- * @file USBCom.c
- * @author
- * @author
- */
- #include <stdio.h>
- #include <libusb-1.0/libusb.h>
- #include "USBCom.h"
- /**
- * NOTE: Used internally. Not likely used by client programmer.
- *
- * Searches for a USB device with a matching vendorID. If found, the corresponding
- * libusb device is returned. If not, null is returned.
- *
- * @param vendorID The vendor ID of the device
- * @return The device handle of a device if a device matching the provided vendor ID is found. Null otherwise.
- */
- libusb_device_handle * USBCom_findDevice(int vendorID)
- {
- libusb_device ** list;
- libusb_device * found = NULL;
- libusb_device * device;
- libusb_device_handle * handle;
- struct libusb_device_descriptor descriptor;
- ssize_t count = libusb_get_device_list(NULL, &list);
- ssize_t i = 0;
- int err = 0;
- if(count < 0)
- return -1;
- for(i = 0; i < count; i++) {
- device = list[i];
- libusb_get_device_descriptor(device, &descriptor);
- if(descriptor.idVendor == vendorID) {
- printf("Found!\n");
- found = device;
- break;
- }
- }
- if(found) {
- err = libusb_open(found, &handle);
- if(err)
- return -1;
- }
- libusb_free_device_list(list, 1);
- return handle;
- }
- /**
- * Initializes libusb and claims the device from the Operating system. This function
- * MUST be called before any subsequent actions are performed on the USB device.
- *
- * @todo Implement contexts (allows IO with multiple USB devices)
- * @param dev USBDevice struct that describes the connected device. Used throughout library.
- * @return 0 on Success
- */
- int USBCom_setup(USBCom_USBDevice * dev)
- {
- int error;
- libusb_init(NULL);
- dev->device = USBCom_findDevice(dev->vendorID);
- if(libusb_kernel_driver_active(dev->device, 0) == 1)
- error = libusb_detach_kernel_driver(dev->device, 0);
- //printf("Error code: %d\n", error);
- libusb_claim_interface(dev->device, 0);
- //printf("Error code: %d\n", error);
- return error;
- }
- /**
- * Clean up and exit libusb. Call this function after all IO tasks have finished.
- *
- * @todo Implement contexts
- * @param dev The device descriptor
- * @ return 0 on Success
- */
- int USBCom_cleanup(USBCom_USBDevice * dev)
- {
- int error;
- error = libusb_attach_kernel_driver(dev->device, 0);
- libusb_exit(NULL);
- return error;
- }
- /**
- * Perform a bulk transfer on a USB device. On success, the buffer is filled
- * and the number of bytes transferred is returned. A call to USBCom_setup must precede
- * this function.
- *
- * @param buffer The buffer to which data is transfered.
- * @param bufferLength The length of the data buffer (number supplied to malloc())
- * @param dev The usb device descriptor
- * @retuen The number of bytes transfered.
- */
- int USBCom_bulkGet(char * buffer, int bufferLength, USBCom_USBDevice * dev)
- {
- int numBytes, error = 0;
- error = libusb_bulk_transfer(dev->device, (dev->inEndpoint | LIBUSB_ENDPOINT_IN), buffer, bufferLength, &numBytes, 0);
- return numBytes;
- }
- int USBCom_bulkSend(char * buffer, int bufferLength, USBCom_USBDevice * dev)
- {
- int numBytes, error = 0;
- error = libusb_bulk_transfer(dev->device, (dev->outEndpoint | LIBUSB_ENDPOINT_OUT), buffer, bufferLength, &numBytes, 0);
- return numBytes;
- }
Advertisement
Add Comment
Please, Sign In to add comment