Advertisement
Guest User

che

a guest
Feb 22nd, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. /*
  2. * usbswitcher.c, by @joystick and @rpaleari
  3. *
  4. * Search for an attached USB Samsung device and switch it to a specific USB
  5. * configuration (#2). Requires libusb [1] (use version 0.1).
  6. *
  7. * References:
  8. * [1] http://libusb.org/
  9. */
  10.  
  11. #include <assert.h>
  12. #include <stdarg.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15.  
  16. #include "usb.h"
  17.  
  18. /* This VID/PID pair is OK for all the Samsung phones we tested. However, you
  19. may need to tune them for other device models. */
  20. #define SAMSUNG_VENDOR_ID 0x04e8
  21. #define SAMSUNG_PRODUCT_ID 0x6860
  22.  
  23. void _log(char tag, const char *fmt, ...) {
  24. char msg[1024];
  25.  
  26. va_list ap;
  27. va_start(ap, fmt);
  28. vsnprintf(msg, sizeof(msg), fmt, ap);
  29. va_end(ap);
  30.  
  31. fprintf(stderr, "[%c] %s\n", tag, msg);
  32. }
  33.  
  34. #define error(fmt, ...) _log('!', fmt, ## __VA_ARGS__)
  35. #define info(fmt, ...) _log('*', fmt, ## __VA_ARGS__)
  36.  
  37. struct usb_device *find_device() {
  38. struct usb_bus *bus;
  39. struct usb_device *dev;
  40. for (bus=usb_busses; bus != NULL; bus=bus->next) {
  41. for (dev=bus->devices; dev; dev=dev->next) {
  42. if ((dev->descriptor.idVendor == SAMSUNG_VENDOR_ID) &&
  43. (dev->descriptor.idProduct == SAMSUNG_PRODUCT_ID))
  44. return dev;
  45. }
  46. }
  47. return NULL;
  48. }
  49.  
  50. int main() {
  51. int r;
  52. struct usb_device *dev;
  53. usb_dev_handle *udev;
  54.  
  55. usb_init();
  56. usb_find_busses();
  57. usb_find_devices();
  58.  
  59. dev = find_device();
  60. if (!dev) {
  61. error("Device not found");
  62. exit(-1);
  63. }
  64. info("Device found, %d configuration(s)", dev->descriptor.bNumConfigurations);
  65.  
  66. assert(dev->descriptor.bNumConfigurations == 2);
  67.  
  68. udev = usb_open(dev);
  69. if (!udev) {
  70. error("Can't open device");
  71. exit(-1);
  72. }
  73. info("Device opened, Switching to configuration #2");
  74.  
  75. usb_reset(udev);
  76. r = usb_set_configuration(udev, 2);
  77. if (r != 0) {
  78. error("Configuration switch failed");
  79. usb_close(udev);
  80. exit(-1);
  81. }
  82.  
  83. info("Configuration switched!");
  84. usb_close(udev);
  85.  
  86. return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement