Don't like ads? PRO users don't see any ads ;-)
Guest

libusb_testCode

By: gstlouis on Aug 27th, 2011  |  syntax: None  |  size: 8.61 KB  |  hits: 53  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.  * File:   main.c
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. #include "libusb-1.0/libusb.h"
  9.  
  10. #define CM15A_VENDOR_ID 0x0bc7
  11. #define CM15A_DEVICE_ID 0x0001
  12.  
  13. /*
  14.  *
  15.  */
  16.  
  17. void print_endpoint_descriptor(
  18.         const struct libusb_endpoint_descriptor *currentDescriptor)
  19. {
  20.     printf("ENDPOINT DESCRIPTOR\n");
  21.     printf("\tLength: %d\n", currentDescriptor->bLength);
  22.     printf("\tType: %d\n", currentDescriptor->bDescriptorType);
  23.     printf("\tAddress: %d\n", currentDescriptor->bEndpointAddress);
  24.     printf("\tAttributes: %d\n", currentDescriptor->bmAttributes);
  25.     printf("\tMax Packet Size: %d\n", currentDescriptor->wMaxPacketSize);
  26.     printf("\tInterval: %d\n", currentDescriptor->bInterval);
  27.     printf("\tRefresh: %d\n", currentDescriptor->bRefresh);
  28.     printf("\tSynch Address: %d\n", currentDescriptor->bSynchAddress);
  29.     printf("\tExtra descriptors: %s\n", currentDescriptor->extra);
  30. }
  31.  
  32. void print_interface_descriptor(
  33.         const struct libusb_interface_descriptor *currentDescriptor)
  34. {
  35.     int currentEndpointIndex = 0;
  36.  
  37.     printf("INTERFACE DESCRIPTOR\n");
  38.     printf("\tLength: %d\n", currentDescriptor->bLength);
  39.     printf("\tType: %d\n", currentDescriptor->bDescriptorType);
  40.     printf("\tNumber: %d\n", currentDescriptor->bInterfaceNumber);
  41.     printf("\tAlt Setting: %d\n", currentDescriptor->bAlternateSetting);
  42.     printf("\tNumber of Enpoints: %d\n", currentDescriptor->bNumEndpoints);
  43.     printf("\tClass: %d\n", currentDescriptor->bInterfaceClass);
  44.     printf("\tSubclass: %d\n", currentDescriptor->bInterfaceSubClass);
  45.     printf("\tProtocol: %d\n", currentDescriptor->bInterfaceProtocol);
  46.     printf("\tInterface: %d\n", currentDescriptor->iInterface);
  47.     printf("\tExtra Descriptors: %s\n", currentDescriptor->extra);
  48.  
  49.     for (currentEndpointIndex = 0;
  50.             currentEndpointIndex < currentDescriptor->bNumEndpoints;
  51.             currentEndpointIndex++)
  52.     {
  53.         print_endpoint_descriptor(
  54.                 &currentDescriptor->endpoint[currentEndpointIndex]);
  55.     }
  56. }
  57.  
  58. void print_interface(
  59.         const struct libusb_interface *currentInterface)
  60. {
  61.     printf("INTERFACE\n");
  62.     printf("\tNumber of alt settings: %d\n", currentInterface->num_altsetting);
  63.     print_interface_descriptor(currentInterface->altsetting);
  64. }
  65.  
  66. void print_config_descriptor(
  67.         const struct libusb_config_descriptor *configDescriptor)
  68. {
  69.     int currentInterfaceIndex = 0;
  70.  
  71.     printf("CONFIG DESCRIPTOR\n");
  72.     printf("\tLength: %d\n", configDescriptor->bLength);
  73.     printf("\tType: %d\n", configDescriptor->bDescriptorType);
  74.     printf("\tTotal Length: %d\n", configDescriptor->wTotalLength);
  75.     printf("\tNumber of Interfacnes: %d\n", configDescriptor->bNumInterfaces);
  76.     printf("\tConfiguration Value: %d\n", configDescriptor->bConfigurationValue);
  77.     printf("\tConfiguration: %d\n", configDescriptor->iConfiguration);
  78.     printf("\tAttributes: %d\n", configDescriptor->bmAttributes);
  79.     printf("\tMax Power: %d\n", configDescriptor->MaxPower);
  80.     printf("\tExtra descriptors: %s\n", configDescriptor->extra);
  81.  
  82.     for (currentInterfaceIndex = 0;
  83.             currentInterfaceIndex < configDescriptor->bNumInterfaces;
  84.             currentInterfaceIndex++)
  85.     {
  86.         print_interface(&configDescriptor->interface[currentInterfaceIndex]);
  87.     }
  88. }
  89.  
  90. int main(int argc, char** argv)
  91. {
  92.     int retval               = 0;
  93.     int iterator             = 0;
  94.     int numDevices           = 0;
  95.     int foundDevice          = 0;
  96.     int currentConfigIndex   = 0;
  97.     int isKernelDriverActive = 0;
  98.  
  99.     enum libusb_error libusbError = 0;
  100.  
  101.     libusb_context *libusbContext                     = NULL;
  102.  
  103.     libusb_device **libusbDeviceList                  = NULL;
  104.     libusb_device *currentDevice                      = NULL;
  105.  
  106.     libusb_device_handle *currentDeviceHandle         = NULL;
  107.  
  108.     struct libusb_config_descriptor *currentConfigDescriptor = NULL;
  109.  
  110.     libusbError = libusb_init(&libusbContext);
  111.  
  112.     if (libusbError != 0)
  113.     {
  114.         printf("Error: Unable to init libusb, returned error code %d.\n",
  115.                 libusbError);
  116.         return libusbError;
  117.     }
  118.  
  119.     libusb_set_debug(libusbContext, 3);
  120.  
  121.     numDevices = libusb_get_device_list(libusbContext, &libusbDeviceList);
  122.  
  123.     printf("There are %d devices attached to the system.\n", numDevices);
  124.  
  125.     for (iterator = 0; iterator < numDevices; iterator++)
  126.     {
  127.         struct libusb_device_descriptor currentDeviceDescriptor;
  128.  
  129.         if (libusbDeviceList[iterator] != NULL)
  130.         {
  131.             currentDevice = libusbDeviceList[iterator];
  132.  
  133.             libusbError = libusb_get_device_descriptor(
  134.                     currentDevice, &currentDeviceDescriptor);
  135.  
  136.             if (libusbError != 0)
  137.             {
  138.                 printf("Error: Unable to get device descriptor for device "
  139.                         "(Error Code: %d).\n", libusbError);
  140.                 return 1;
  141.             }
  142.             else
  143.             {
  144.                 if (currentDeviceDescriptor.idVendor == CM15A_VENDOR_ID &&
  145.                         currentDeviceDescriptor.idProduct == CM15A_DEVICE_ID)
  146.                 {
  147.                     printf("Found Device!\n");
  148.                     foundDevice = 1;
  149.                 }
  150.  
  151.                 /*
  152.                                 printf("Device #%d:\n", iterator);
  153.                                 printf("\tVendor Id: %04x\n",
  154.                                         currentDeviceDescriptor.idVendor);
  155.                                 printf("\tDevice Id: %04x\n",
  156.                                         currentDeviceDescriptor.idProduct);
  157.                  */
  158.             }
  159.         }
  160.         else
  161.         {
  162.             printf("Error: Invalid device in list.\n");
  163.             return 1;
  164.         }
  165.     }
  166.  
  167.     if (!foundDevice)
  168.     {
  169.         printf("Error: Unable to find device.\n");
  170.         return 1;
  171.     }
  172.  
  173.     /* Open */
  174.     libusbError = libusb_open(
  175.             currentDevice, &currentDeviceHandle);
  176.  
  177.     isKernelDriverActive = libusb_kernel_driver_active(
  178.             currentDeviceHandle, 0);
  179.  
  180.     if (isKernelDriverActive)
  181.     {
  182.         printf("Kernel Driver is active. I'll try to detach.\n");
  183.  
  184.         libusbError = libusb_detach_kernel_driver(
  185.                 currentDeviceHandle, 0);
  186.  
  187.  
  188.         if (libusbError != 0)
  189.         {
  190.             printf("Error: Unable to detach kernel driver for interface 0 "
  191.                     "(%d)\n", libusbError);
  192.             return 1;
  193.         }
  194.     }
  195.     else
  196.     {
  197.         printf("Kernel Driver is NOT active.\n");
  198.     }
  199.  
  200.     if (libusbError != 0)
  201.     {
  202.         printf("Error: Unable to open device (%d)\n",
  203.                 libusbError);
  204.         return 1;
  205.     }
  206.  
  207.     /* Get Configuration Number */
  208.     libusbError = libusb_get_configuration(
  209.             currentDeviceHandle, &currentConfigIndex);
  210.  
  211.     if (libusbError != 0 || currentConfigIndex == 0)
  212.     {
  213.         printf("Error: Unable to get configuration (%d)\n",
  214.                 libusbError);
  215.         return 1;
  216.     }
  217.  
  218.     printf("Current configuration: %d\n", currentConfigIndex);
  219.  
  220.     /* Get active config descriptor */
  221.     libusbError = libusb_get_active_config_descriptor(
  222.             currentDevice, &currentConfigDescriptor);
  223.  
  224.     if (libusbError != 0)
  225.     {
  226.         printf("Error: Unable to get active config descriptor (%d)\n",
  227.                 libusbError);
  228.         return 1;
  229.     }
  230.  
  231.     print_config_descriptor(currentConfigDescriptor);
  232.  
  233.     if (libusbError != 0)
  234.     {
  235.         printf("Error: Unable to reset device (%d).\n", libusbError);
  236.         return 1;
  237.     }
  238.  
  239.     /* Claim Interface */
  240.     libusbError = libusb_claim_interface(
  241.             currentDeviceHandle, 0);
  242.  
  243.     if (libusbError != 0)
  244.     {
  245.         printf("Error: Unable to claim interface 0 (%d)\n",
  246.                 libusbError);
  247.         return 1;
  248.     }
  249.  
  250.     /* Read */
  251.     unsigned char *data = malloc(500);
  252.     int actualLength = 0;
  253.     int i = 0;
  254.  
  255.     while (1)
  256.     {
  257.         libusbError = libusb_bulk_transfer(
  258.                 currentDeviceHandle,
  259.                 129,
  260.                 data,
  261.                 500,
  262.                 &actualLength,
  263.                 2000);
  264.  
  265.         if (libusbError != 0 && libusbError != LIBUSB_ERROR_TIMEOUT)
  266.         {
  267.             printf("Error: Unable to read (%d)\n",
  268.                     libusbError);
  269.             return 1;
  270.         }
  271.  
  272.         for (i = 0; i < actualLength; i++)
  273.         {
  274.             printf("%02x", data[i]);
  275.         }
  276.         printf("\n");
  277.     }
  278.  
  279.     libusb_close(currentDeviceHandle);
  280.  
  281.     libusb_free_device_list(libusbDeviceList, 1);
  282.  
  283.     libusb_exit(libusbContext);
  284.  
  285.     return (EXIT_SUCCESS);
  286. }