upekuxd.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include <stdio.h>
  2. #include <signal.h>
  3. #include <sys/prctl.h>
  4. #include <unistd.h>
  5. #include <libusb-1.0/libusb.h>
  6. #include "launcherd.h"
  7. extern int daemonEntryPoint( libusb_device_handle* deviceHandle, int launcherdSocket );
  8. static void printDevice( libusb_device_handle* deviceHandle ){
  9. int res;
  10. unsigned char manufacturer[32];
  11. unsigned char product[32];
  12. libusb_device* dev;
  13. struct libusb_device_descriptor desc;
  14. //get device and device descriptor
  15. dev = libusb_get_device( deviceHandle );
  16. libusb_get_device_descriptor( dev, &desc );
  17. //get manufacturer name
  18. res = libusb_get_string_descriptor_ascii( deviceHandle, desc.iManufacturer, manufacturer, sizeof(manufacturer) );
  19. if( res < 1 ) manufacturer[0] = 0x00;
  20. //get product name
  21. res = libusb_get_string_descriptor_ascii( deviceHandle, desc.iProduct, product, sizeof(product) );
  22. if( res < 1 ) product[0] = 0x00;
  23. //print device info
  24. printf( "[usb] Found %04x:%04x %s %s\n", desc.idVendor, desc.idProduct, manufacturer, product );
  25. }
  26. /*************************************************
  27. func usbDeviceOpen
  28. search and open device. Also claim interface 0.
  29. return
  30. device handle on success
  31. NULL on error
  32. **************************************************/
  33. static libusb_device_handle* usbDeviceOpen( uint16_t vendor_id, uint16_t product_id ){
  34. int i;
  35. int err;
  36. libusb_device** deviceList;
  37. libusb_device* found = NULL;
  38. libusb_device_handle* deviceHandle = NULL;
  39. struct libusb_device_descriptor deviceDescriptor;
  40. ssize_t count;
  41. //get usb device list
  42. count = libusb_get_device_list( NULL, &deviceList );
  43. if( count < 0 ){
  44. printf( "[usb] get device list error: %li\n", count );
  45. return NULL;
  46. }
  47. //search our usb device in list
  48. for( i = 0; i < count; i++ ){
  49. //Note since libusb-1.0.16, LIBUSB_API_VERSION >= 0x01000102, this function always succeeds.
  50. libusb_get_device_descriptor( deviceList[i], &deviceDescriptor );
  51. if( deviceDescriptor.idVendor == vendor_id && deviceDescriptor.idProduct == product_id ){
  52. found = deviceList[i];
  53. break;
  54. }
  55. }
  56. //if device not found go to exit
  57. if( !found ) goto l_listFree;
  58. //try to open device
  59. err = libusb_open( found, &deviceHandle );
  60. if( err ){
  61. printf( "[usb] open device error:%i\n", err );
  62. deviceHandle = NULL;
  63. goto l_listFree;
  64. }
  65. //try to claim interface 0
  66. err = libusb_claim_interface( deviceHandle, 0 );
  67. if( err ){
  68. printf( "[usb] Interface 0 claim error: %i\n", err );
  69. libusb_close( deviceHandle );
  70. deviceHandle = NULL;
  71. }
  72. l_listFree:
  73. //we don't need it anymore
  74. libusb_free_device_list( deviceList, 1 );
  75. return deviceHandle;
  76. }
  77. /***************************************
  78. func usbDeviceClose
  79. release interface 0 and close device
  80. ***************************************/
  81. static void usbDeviceClose( libusb_device_handle* deviceHandle ){
  82. libusb_release_interface( deviceHandle, 0 );
  83. libusb_close( deviceHandle );
  84. }
  85. static int searchDevice( int launcherdSocket ){
  86. int err;
  87. libusb_device_handle* deviceHandle = NULL;
  88. uint16_t vendor_id = 0x0483;
  89. uint16_t product_id = 0x2016;
  90. //try to init libusb
  91. err = libusb_init( NULL );
  92. if( err ){
  93. printf( "[usb] libusb init error: %i\n", err );
  94. return err;
  95. }
  96. //search and open device
  97. deviceHandle = usbDeviceOpen( vendor_id, product_id );
  98. //if device found, try to start driver
  99. if( deviceHandle ){
  100. printDevice( deviceHandle );
  101. err = daemonEntryPoint( deviceHandle, launcherdSocket );
  102. usbDeviceClose( deviceHandle );
  103. } else printf( "[usb] Device not found.\n" );
  104. //libusb deinit
  105. libusb_exit( NULL );
  106. return err;
  107. }
  108. int main( int argc, char* argv[] ){
  109. int err = 0;
  110. int launcherdSocket;
  111. //set thread name
  112. prctl( PR_SET_NAME, "main", 0, 0, 0 );
  113. //start launcherd
  114. launcherdSocket = launcherdStart();
  115. if( launcherdSocket < 0 ){
  116. printf( "[main] Launcherd start failed: %i\n", launcherdSocket );
  117. return 1;
  118. }
  119. //make sure the SIGPIPE doesn't disturb us
  120. signal( SIGPIPE, SIG_IGN );
  121. //search device and start daemon if device exist
  122. err = searchDevice( launcherdSocket );
  123. //Close launcherd socket to signal him go to exit
  124. close( launcherdSocket );
  125. printf( "[main] Exiting with code %i\n", err );
  126. return err;
  127. }