Advertisement
Tanure

cypress usbdebug userspace test

Oct 22nd, 2020
1,174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <libusb-1.0/libusb.h>
  3. #include <stdint.h>
  4. #include <string.h>
  5.  
  6. int main(int argc, char*argv[])
  7. {
  8.     int res                      = 0;
  9.     libusb_device_handle* handle = 0;
  10.     int kernelDriverDetached     = 0;
  11.     int numBytes                 = 0;
  12.     int a,b,i;
  13.     unsigned char str[128];
  14.     unsigned char buffer[128];
  15.  
  16.     res = libusb_init(0);
  17.     if (res != 0) {
  18.         fprintf(stderr, "Error initialising libusb.\n");
  19.         return 1;
  20.     }
  21.  
  22.     handle = libusb_open_device_with_vid_pid(0, 0x04b4, 0x00f0);
  23.     if (!handle) {
  24.         fprintf(stderr, "Unable to open device.\n");
  25.         return 1;
  26.     }
  27.  
  28.     if (libusb_kernel_driver_active(handle, 0)) {
  29.         res = libusb_detach_kernel_driver(handle, 0);
  30.         if (res == 0) {
  31.           kernelDriverDetached = 1;
  32.         } else {
  33.           fprintf(stderr, "Error detaching kernel driver.\n");
  34.           return 1;
  35.         }
  36.     }
  37.  
  38.     res = libusb_claim_interface(handle, 0);
  39.     if (res != 0) {
  40.         fprintf(stderr, "Error claiming interface.\n");
  41.         return 1;
  42.     }
  43.  
  44.     for (a=0; a<100; a++) {
  45.         memset(str, 0, 128);
  46.         res = libusb_interrupt_transfer(handle, 0x81, str, 128, &numBytes, 1000);
  47.         if (0 == res) {
  48.             b = 0;
  49.             for(i=0; i<32; i++) {
  50.                 b += sprintf(&buffer[b], "%.2x ",str[i]);
  51.             }
  52.             buffer[b++] = '\n';
  53.             buffer[b++] = 0;
  54.             printf("%s",buffer);
  55.         } else {
  56.            fprintf(stderr, "Error receiving message %d.\n", a);
  57.         }
  58.     }
  59.  
  60.     res = libusb_release_interface(handle, 0);
  61.     if (0 != res)
  62.       fprintf(stderr, "Error releasing interface.\n");
  63.  
  64.     if (kernelDriverDetached)
  65.         libusb_attach_kernel_driver(handle, 0);
  66.  
  67.     libusb_exit(0);
  68.  
  69.     return 0;
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement