This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

libusb Problem

By: a guest on Nov 15th, 2012  |  syntax: C++  |  size: 7.51 KB  |  views: 66  |  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. #include <errno.h>
  2. #include <signal.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <assert.h>  // for assert()
  7. #include "stdafx.h"
  8. #include <libusb.h>
  9. void print_devs(libusb_device **devs);
  10. libusb_device* verify_device(libusb_device **devs);
  11.  
  12. #include <iostream>
  13.  
  14. int test2();
  15. int do_exit = 0;
  16. using namespace std;
  17.  
  18.  
  19. BOOL WINAPI ConsoleHandler(
  20.     DWORD dwCtrlType   //  control signal type
  21. );
  22.  
  23. BOOL WINAPI ConsoleHandler(DWORD CEvent)
  24. {
  25.     char mesg[128];
  26.  
  27.     switch(CEvent)
  28.     {
  29.     case CTRL_C_EVENT:
  30.         MessageBox(NULL,
  31.             _T("CTRL+C received!"),_T("CEvent"),MB_OK);
  32.                 do_exit = true;
  33.         break;
  34.     case CTRL_BREAK_EVENT:
  35.         MessageBox(NULL,
  36.             _T("CTRL+BREAK received!"),_T("CEvent"),MB_OK);
  37.         break;
  38.     case CTRL_CLOSE_EVENT:
  39.         MessageBox(NULL,
  40.             _T("Program being closed!"),_T("CEvent"),MB_OK);
  41.         break;
  42.     case CTRL_LOGOFF_EVENT:
  43.         MessageBox(NULL,
  44.             _T("User is logging off!"),_T("CEvent"),MB_OK);
  45.         break;
  46.     case CTRL_SHUTDOWN_EVENT:
  47.         MessageBox(NULL,
  48.             _T("User is logging off!"),_T("CEvent"),MB_OK);
  49.         break;
  50.  
  51.     }
  52.     return TRUE;
  53. }
  54.  
  55. int main() {
  56.         //Handle Control+C
  57.         if (SetConsoleCtrlHandler( (PHANDLER_ROUTINE)ConsoleHandler,TRUE)==FALSE)
  58.     {
  59.         printf("Unable to install handler!\n");
  60.         return -1;
  61.     }
  62.         test2();
  63.     return 0;
  64.  
  65. }
  66.  
  67. // OUT-going transfers (OUT from host PC to USB-device)
  68. struct libusb_transfer *transfer_command_out     = NULL;  // find version
  69.  
  70. // IN-coming transfers (IN to host PC from USB-device)
  71. struct libusb_transfer *transfer_response_in  = NULL;
  72.  
  73. extern "C" void LIBUSB_CALL receive(struct libusb_transfer *transfer);
  74. extern "C" void LIBUSB_CALL cb_command_out(struct libusb_transfer *transfer);
  75.  
  76. extern "C" void sighandler(int signum);
  77.  
  78.  
  79. // Setup Endpoints - The endpoints (2,4) are particular to the USB-connected device
  80. #define EP_COMMAND    (0x03 | LIBUSB_ENDPOINT_OUT) // OUT of PC to USB-device //DEVICE_SPECIFIC
  81. #define EP_RESPONSE   (0x83 | LIBUSB_ENDPOINT_IN ) // IN  PC from  USB-device //DEVICE_SPECIFIC
  82.  
  83. unsigned char buffer[1024] = {0};
  84.  
  85. int device_num = 1;
  86.  
  87. int test2()
  88. {
  89.         libusb_device **devs;
  90.         int r;
  91.  
  92.         libusb_context *ctx = NULL; //a libusb session
  93.  
  94.     ssize_t cnt; //holding number of devices in list
  95.  
  96.     r = libusb_init(&ctx); //initialize the library for the session we just declared
  97.  
  98.     if(r < 0) {
  99.  
  100.         cout<<"Init Error "<<r<<endl; //there was an error
  101.         return 1;
  102.     }
  103.  
  104.     libusb_set_debug(ctx, 3);
  105.  
  106.         cnt = libusb_get_device_list(ctx, &devs);
  107.         if (cnt < 0)
  108.                 return (int) cnt;
  109.  
  110.         libusb_device *dev = verify_device(devs);
  111.  
  112.         struct libusb_device_descriptor desc;
  113.         r = libusb_get_device_descriptor(dev, &desc);
  114.         if (r < 0) {
  115.                 fprintf(stderr, "failed to get device descriptor");
  116.                 return -1;
  117.         }
  118.  
  119.         libusb_config_descriptor *config;
  120.  
  121.     libusb_get_config_descriptor(dev, 0, &config);
  122.  
  123.     cout<<"Interfaces: "<<(int)config->bNumInterfaces;
  124.  
  125.     const libusb_interface *inter;
  126.     const libusb_interface_descriptor *interdesc;
  127.     const libusb_endpoint_descriptor *epdesc;
  128.         /*
  129.     for(int i=0; i<(int)config->bNumInterfaces; i++) {
  130.         inter = &config->interface[i];
  131.         cout<<"Number of alternate settings: "<<inter->num_altsetting<<" | ";
  132.  
  133.         for(int j=0; j<inter->num_altsetting; j++) {
  134.             interdesc = &inter->altsetting[j];
  135.             cout<<"Interface Number: "<<(int)interdesc->bInterfaceNumber<<" | ";
  136.             cout<<"Number of endpoints: "<<(int)interdesc->bNumEndpoints<<" | ";
  137.  
  138.             for(int k=0; k<(int)interdesc->bNumEndpoints; k++) {
  139.                 epdesc = &interdesc->endpoint[k];
  140.                 cout<<"Descriptor Type: "<<(int)epdesc->bDescriptorType<<" | ";
  141.                 cout<<"EP Address: "<<(int)epdesc->bEndpointAddress<<" | ";
  142.             }
  143.         }
  144.     }*/
  145.  
  146.     libusb_free_config_descriptor(config);
  147.        
  148.         printf("%04x:%04x (bus %d, device %d)\n",
  149.                 desc.idVendor, desc.idProduct,
  150.                 libusb_get_bus_number(dev), libusb_get_device_address(dev));
  151.  
  152.         libusb_device_handle *handle = NULL;
  153.  
  154.         r = libusb_open(dev, &handle);
  155.        
  156.         printf("libusb_open = %d\n", r);
  157.  
  158.         r = libusb_kernel_driver_active(handle, device_num);
  159.     printf("libusb_kernel_driver_active = %d\n", r);
  160.         if (r == 1) {
  161.                 r = libusb_detach_kernel_driver(handle, device_num);
  162.                 printf("libusb_detach_kernel_driver = %d\n", r);
  163.     }
  164.  
  165.         r = libusb_claim_interface(handle, device_num);
  166.         if (r < 0) {
  167.                 fprintf(stderr, "usb_claim_interface error %d\n", r);
  168.                 return -1;
  169.         }
  170.        
  171.         const int length = 1;
  172.         unsigned char buf[length] = {0};
  173.         buf[0] = 'a';
  174.  
  175.         transfer_response_in  = libusb_alloc_transfer(0);
  176.         libusb_fill_bulk_transfer( transfer_response_in, handle, EP_RESPONSE,
  177.                 buffer,  1024,  // Note: version_string is where input data written.
  178.                 receive, NULL, 0); // no user data
  179.         r = libusb_submit_transfer(transfer_response_in);
  180.         printf("return value from libusb_submit_transfer(for transfer_response_in)= %d\n", r);
  181.  
  182.                 // Define transfer of data OUT (OUT to USB-device from host PC)
  183.         transfer_command_out = libusb_alloc_transfer(0);
  184.  
  185.         libusb_fill_bulk_transfer(transfer_command_out, handle, EP_COMMAND,
  186.                 buf, length,  // version request command, command length
  187.                 cb_command_out, NULL, 0); // callback function, NO callback data
  188.         r = libusb_submit_transfer(transfer_command_out);
  189.         printf("return value from libusb_submit_transfer(for transfer_command_out)=%d\n", r);
  190.  
  191.         while (!do_exit) {
  192.                 struct timeval tv;
  193.                 tv.tv_sec  = 0;     // seconds
  194.                 tv.tv_usec = 100;   // milliseconds  ( .1 sec)
  195.                 //libusb_handle_events(ctx);
  196.                 libusb_handle_events_timeout(ctx, &tv);
  197.         }
  198.  
  199.         libusb_release_interface(handle, device_num);
  200.         libusb_close(handle);
  201.         libusb_free_device_list(devs, 0);
  202.         libusb_exit(ctx);
  203.  
  204.         printf("Exit 0");
  205.         return 0;
  206. }
  207.  
  208. extern "C" void LIBUSB_CALL receive(struct libusb_transfer *transfer)
  209. {       int i;
  210.         fprintf(stderr, "cb_response_in: status =%d, actual_length=%d\n",
  211.                 transfer->status, transfer->actual_length);
  212.         for (i=0; i < transfer->actual_length; i++){
  213.                 printf("buffer[%d]=%x \n", i, buffer[i] );
  214.         }
  215.         return;
  216. }
  217.  
  218. //This is called after command for version is received.
  219. //   - This is ONLY a mechanism to indicate the that version command has been received by libusb.
  220. //     It does NOT signal that the version_string is ready.
  221. //     It does NOT say when the version will be ready.
  222. extern "C" void LIBUSB_CALL cb_command_out(struct libusb_transfer *transfer)
  223. {
  224.         fprintf(stderr, "cb_command_out: status =%d, actual_length=%d\n",
  225.                 transfer->status, transfer->actual_length);
  226.         return;
  227. }
  228.  
  229. libusb_device* verify_device(libusb_device **devs)
  230. {
  231.         libusb_device *dev;
  232.         int i = 0;
  233.         while ((dev = devs[i++]) != NULL) {
  234.                 struct libusb_device_descriptor desc;
  235.                 int r = libusb_get_device_descriptor(dev, &desc);
  236.                 if (r < 0) {
  237.                         fprintf(stderr, "failed to get device descriptor");
  238.                         return NULL;
  239.                 }
  240.                 if(desc.idVendor == 0x04D8 && desc.idProduct == 0x00DF)
  241.                 {
  242.                         puts("Found our Device!");
  243.                         return dev;
  244.                 }
  245.         }
  246.         return NULL;
  247. }
  248.  
  249. void print_devs(libusb_device **devs)
  250. {
  251.         libusb_device *dev;
  252.         int i = 0;
  253.  
  254.         while ((dev = devs[i++]) != NULL) {
  255.                 struct libusb_device_descriptor desc;
  256.                 int r = libusb_get_device_descriptor(dev, &desc);
  257.                 if (r < 0) {
  258.                         fprintf(stderr, "failed to get device descriptor");
  259.                         return;
  260.                 }
  261.  
  262.                 printf("%04x:%04x (bus %d, device %d)\n",
  263.                         desc.idVendor, desc.idProduct,
  264.                         libusb_get_bus_number(dev), libusb_get_device_address(dev));
  265.         }
  266. }
clone this paste RAW Paste Data