- /*
- * File: main.c
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include "libusb-1.0/libusb.h"
- #define CM15A_VENDOR_ID 0x0bc7
- #define CM15A_DEVICE_ID 0x0001
- /*
- *
- */
- void print_endpoint_descriptor(
- const struct libusb_endpoint_descriptor *currentDescriptor)
- {
- printf("ENDPOINT DESCRIPTOR\n");
- printf("\tLength: %d\n", currentDescriptor->bLength);
- printf("\tType: %d\n", currentDescriptor->bDescriptorType);
- printf("\tAddress: %d\n", currentDescriptor->bEndpointAddress);
- printf("\tAttributes: %d\n", currentDescriptor->bmAttributes);
- printf("\tMax Packet Size: %d\n", currentDescriptor->wMaxPacketSize);
- printf("\tInterval: %d\n", currentDescriptor->bInterval);
- printf("\tRefresh: %d\n", currentDescriptor->bRefresh);
- printf("\tSynch Address: %d\n", currentDescriptor->bSynchAddress);
- printf("\tExtra descriptors: %s\n", currentDescriptor->extra);
- }
- void print_interface_descriptor(
- const struct libusb_interface_descriptor *currentDescriptor)
- {
- int currentEndpointIndex = 0;
- printf("INTERFACE DESCRIPTOR\n");
- printf("\tLength: %d\n", currentDescriptor->bLength);
- printf("\tType: %d\n", currentDescriptor->bDescriptorType);
- printf("\tNumber: %d\n", currentDescriptor->bInterfaceNumber);
- printf("\tAlt Setting: %d\n", currentDescriptor->bAlternateSetting);
- printf("\tNumber of Enpoints: %d\n", currentDescriptor->bNumEndpoints);
- printf("\tClass: %d\n", currentDescriptor->bInterfaceClass);
- printf("\tSubclass: %d\n", currentDescriptor->bInterfaceSubClass);
- printf("\tProtocol: %d\n", currentDescriptor->bInterfaceProtocol);
- printf("\tInterface: %d\n", currentDescriptor->iInterface);
- printf("\tExtra Descriptors: %s\n", currentDescriptor->extra);
- for (currentEndpointIndex = 0;
- currentEndpointIndex < currentDescriptor->bNumEndpoints;
- currentEndpointIndex++)
- {
- print_endpoint_descriptor(
- ¤tDescriptor->endpoint[currentEndpointIndex]);
- }
- }
- void print_interface(
- const struct libusb_interface *currentInterface)
- {
- printf("INTERFACE\n");
- printf("\tNumber of alt settings: %d\n", currentInterface->num_altsetting);
- print_interface_descriptor(currentInterface->altsetting);
- }
- void print_config_descriptor(
- const struct libusb_config_descriptor *configDescriptor)
- {
- int currentInterfaceIndex = 0;
- printf("CONFIG DESCRIPTOR\n");
- printf("\tLength: %d\n", configDescriptor->bLength);
- printf("\tType: %d\n", configDescriptor->bDescriptorType);
- printf("\tTotal Length: %d\n", configDescriptor->wTotalLength);
- printf("\tNumber of Interfacnes: %d\n", configDescriptor->bNumInterfaces);
- printf("\tConfiguration Value: %d\n", configDescriptor->bConfigurationValue);
- printf("\tConfiguration: %d\n", configDescriptor->iConfiguration);
- printf("\tAttributes: %d\n", configDescriptor->bmAttributes);
- printf("\tMax Power: %d\n", configDescriptor->MaxPower);
- printf("\tExtra descriptors: %s\n", configDescriptor->extra);
- for (currentInterfaceIndex = 0;
- currentInterfaceIndex < configDescriptor->bNumInterfaces;
- currentInterfaceIndex++)
- {
- print_interface(&configDescriptor->interface[currentInterfaceIndex]);
- }
- }
- int main(int argc, char** argv)
- {
- int retval = 0;
- int iterator = 0;
- int numDevices = 0;
- int foundDevice = 0;
- int currentConfigIndex = 0;
- int isKernelDriverActive = 0;
- enum libusb_error libusbError = 0;
- libusb_context *libusbContext = NULL;
- libusb_device **libusbDeviceList = NULL;
- libusb_device *currentDevice = NULL;
- libusb_device_handle *currentDeviceHandle = NULL;
- struct libusb_config_descriptor *currentConfigDescriptor = NULL;
- libusbError = libusb_init(&libusbContext);
- if (libusbError != 0)
- {
- printf("Error: Unable to init libusb, returned error code %d.\n",
- libusbError);
- return libusbError;
- }
- libusb_set_debug(libusbContext, 3);
- numDevices = libusb_get_device_list(libusbContext, &libusbDeviceList);
- printf("There are %d devices attached to the system.\n", numDevices);
- for (iterator = 0; iterator < numDevices; iterator++)
- {
- struct libusb_device_descriptor currentDeviceDescriptor;
- if (libusbDeviceList[iterator] != NULL)
- {
- currentDevice = libusbDeviceList[iterator];
- libusbError = libusb_get_device_descriptor(
- currentDevice, ¤tDeviceDescriptor);
- if (libusbError != 0)
- {
- printf("Error: Unable to get device descriptor for device "
- "(Error Code: %d).\n", libusbError);
- return 1;
- }
- else
- {
- if (currentDeviceDescriptor.idVendor == CM15A_VENDOR_ID &&
- currentDeviceDescriptor.idProduct == CM15A_DEVICE_ID)
- {
- printf("Found Device!\n");
- foundDevice = 1;
- }
- /*
- printf("Device #%d:\n", iterator);
- printf("\tVendor Id: %04x\n",
- currentDeviceDescriptor.idVendor);
- printf("\tDevice Id: %04x\n",
- currentDeviceDescriptor.idProduct);
- */
- }
- }
- else
- {
- printf("Error: Invalid device in list.\n");
- return 1;
- }
- }
- if (!foundDevice)
- {
- printf("Error: Unable to find device.\n");
- return 1;
- }
- /* Open */
- libusbError = libusb_open(
- currentDevice, ¤tDeviceHandle);
- isKernelDriverActive = libusb_kernel_driver_active(
- currentDeviceHandle, 0);
- if (isKernelDriverActive)
- {
- printf("Kernel Driver is active. I'll try to detach.\n");
- libusbError = libusb_detach_kernel_driver(
- currentDeviceHandle, 0);
- if (libusbError != 0)
- {
- printf("Error: Unable to detach kernel driver for interface 0 "
- "(%d)\n", libusbError);
- return 1;
- }
- }
- else
- {
- printf("Kernel Driver is NOT active.\n");
- }
- if (libusbError != 0)
- {
- printf("Error: Unable to open device (%d)\n",
- libusbError);
- return 1;
- }
- /* Get Configuration Number */
- libusbError = libusb_get_configuration(
- currentDeviceHandle, ¤tConfigIndex);
- if (libusbError != 0 || currentConfigIndex == 0)
- {
- printf("Error: Unable to get configuration (%d)\n",
- libusbError);
- return 1;
- }
- printf("Current configuration: %d\n", currentConfigIndex);
- /* Get active config descriptor */
- libusbError = libusb_get_active_config_descriptor(
- currentDevice, ¤tConfigDescriptor);
- if (libusbError != 0)
- {
- printf("Error: Unable to get active config descriptor (%d)\n",
- libusbError);
- return 1;
- }
- print_config_descriptor(currentConfigDescriptor);
- if (libusbError != 0)
- {
- printf("Error: Unable to reset device (%d).\n", libusbError);
- return 1;
- }
- /* Claim Interface */
- libusbError = libusb_claim_interface(
- currentDeviceHandle, 0);
- if (libusbError != 0)
- {
- printf("Error: Unable to claim interface 0 (%d)\n",
- libusbError);
- return 1;
- }
- /* Read */
- unsigned char *data = malloc(500);
- int actualLength = 0;
- int i = 0;
- while (1)
- {
- libusbError = libusb_bulk_transfer(
- currentDeviceHandle,
- 129,
- data,
- 500,
- &actualLength,
- 2000);
- if (libusbError != 0 && libusbError != LIBUSB_ERROR_TIMEOUT)
- {
- printf("Error: Unable to read (%d)\n",
- libusbError);
- return 1;
- }
- for (i = 0; i < actualLength; i++)
- {
- printf("%02x", data[i]);
- }
- printf("\n");
- }
- libusb_close(currentDeviceHandle);
- libusb_free_device_list(libusbDeviceList, 1);
- libusb_exit(libusbContext);
- return (EXIT_SUCCESS);
- }