Advertisement
Guest User

USB Host C

a guest
Jul 3rd, 2018
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define MAX_SENSE_LENGTH 24
  2. #define MAX_CDB_LENGTH 16
  3. #define MAX_TRANSFER_BUFFER_LENGTH 1 << 16
  4.  
  5. #define HID_MOUSE_NO_BUTTON         0x00
  6. #define HID_MOUSE_RIGHT_BUTTON      0x01
  7. #define HID_MOUSE_LEFT_BUTTON       0x02
  8.  
  9. #define HID_KEY_MOD_LEFT_CTRL       0x01
  10. #define HID_KEY_MOD_LEFT_SHIFT      0x02
  11. #define HID_KEY_MOD_LEFT_ALT        0x04
  12. #define HID_KEY_MOD_LEFT_META       0x08
  13. #define HID_KEY_MOD_RIGHT_CTRL      0x10
  14. #define HID_KEY_MOD_RIGHT_SHIFT     0x20
  15. #define HID_KEY_MOD_RIGHT_ALT       0x40
  16. #define HID_KEY_MOD_RIGHT_META      0x80
  17.  
  18. #define HID_KEY_NONE                0x00
  19. #define HID_KEY_ERROR_ROLLOVER      0x01
  20. #define HID_KEY_ERROR_POSTFAIL      0x02
  21. #define HID_KEY_ERROR_UNDEFINED     0x03
  22.  
  23. #define HID_KEY_A                   0x04
  24. #define HID_KEY_B                   0x05
  25. #define HID_KEY_C                   0x06
  26. #define HID_KEY_D                   0x07
  27. #define HID_KEY_E                   0x08
  28. #define HID_KEY_F                   0x09
  29. #define HID_KEY_G                   0x0a
  30. #define HID_KEY_H                   0x0b
  31. #define HID_KEY_I                   0x0c
  32. #define HID_KEY_J                   0x0d
  33. #define HID_KEY_K                   0x0e
  34. #define HID_KEY_L                   0x0f
  35. #define HID_KEY_M                   0x10
  36. #define HID_KEY_N                   0x11
  37. #define HID_KEY_O                   0x12
  38. #define HID_KEY_P                   0x13
  39. #define HID_KEY_Q                   0x14
  40. #define HID_KEY_R                   0x15
  41. #define HID_KEY_S                   0x16
  42. #define HID_KEY_T                   0x17
  43. #define HID_KEY_U                   0x18
  44. #define HID_KEY_V                   0x19
  45. #define HID_KEY_W                   0x1a
  46. #define HID_KEY_X                   0x1b
  47. #define HID_KEY_Y                   0x1c
  48. #define HID_KEY_Z                   0x1d
  49.  
  50. #define HID_KEY_1                   0x1e // 1 and !
  51. #define HID_KEY_2                   0x1f // 2 and @
  52. #define HID_KEY_3                   0x20 // 3 and #
  53. #define HID_KEY_4                   0x21 // 4 and $
  54. #define HID_KEY_5                   0x22 // 5 and %
  55. #define HID_KEY_6                   0x23 // 6 and ^
  56. #define HID_KEY_7                   0x24 // 7 and &
  57. #define HID_KEY_8                   0x25 // 8 and *
  58. #define HID_KEY_9                   0x26 // 9 and (
  59. #define HID_KEY_0                   0x27 // 0 and )
  60.  
  61. #define HID_KEY_EXCLAMATION         0x1e // 1 and !
  62. #define HID_KEY_AT                  0x1f // 2 and @
  63. #define HID_KEY_HASH                0x20 // 3 and #
  64. #define HID_KEY_DOLLAR              0x21 // 4 and $
  65. #define HID_KEY_PERCENT             0x22 // 5 and %
  66. #define HID_KEY_CIRCUMFLEX          0x23 // 6 and ^
  67. #define HID_KEY_AMPERSAND           0x24 // 7 and &
  68. #define HID_KEY_ASTERISK            0x25 // 8 and *
  69. #define HID_KEY_LEFTPARENTTHESIS    0x26 // 9 and (
  70. #define HID_KEY_RIGHTPARENTTHESIS   0x27 // 0 and )
  71.  
  72. #define HID_KEY_RETURN              0x28 // RETURN
  73. #define HID_KEY_ESC                 0x29 // ESCAPE
  74. #define HID_KEY_BACKSPACE           0x2a // Backspace
  75. #define HID_KEY_TAB                 0x2b // Tab
  76. #define HID_KEY_SPACE               0x2c // Spacebar
  77. #define HID_KEY_MINUS               0x2d // - and _
  78. #define HID_KEY_UNDERSCORE          0x2d // - and _
  79. #define HID_KEY_EQUAL               0x2e // = and +
  80. #define HID_KEY_PLUS                0x2e // = and +
  81. #define HID_KEY_LEFTBRACE           0x2f // [ and {
  82. #define HID_KEY_RIGHTBRACE          0x30 // ] and }
  83.  
  84. #define HID_KEY_BACKSLASH           0x31 // \ and |
  85. #define HID_KEY_PIPE                0x31 // \ and |
  86. #define HID_KEY_HASHTILDE           0x32 // Non-US # and ~
  87. #define HID_KEY_SEMICOLON           0x33 // ; and :
  88. #define HID_KEY_COLON               0x33 // ; and :
  89. #define HID_KEY_QUOTE               0x34 // ' and "
  90. #define HID_KEY_DOUBLE_QUOTE        0x34 // ' and "
  91. #define HID_KEY_GRAVE               0x35 // ` and ~
  92. #define HID_KEY_TILDE               0x35 // ` and ~
  93.  
  94. #define HID_KEY_COMMA               0x36 // , and <
  95. #define HID_KEY_LESSTHAN            0x36 // , and <
  96. #define HID_KEY_DOT                 0x37 // . and >
  97. #define HID_KEY_GREATETHAN          0x37 // . and >
  98. #define HID_KEY_SLASH               0x38 // / and ?
  99. #define HID_KEY_QUESTIONMARK        0x38 // / and ?
  100. #define HID_KEY_CAPSLOCK            0x39 // Caps Lock
  101.  
  102. #define HID_KEY_F1                  0x3a // F1
  103. #define HID_KEY_F2                  0x3b // F2
  104. #define HID_KEY_F3                  0x3c // F3
  105. #define HID_KEY_F4                  0x3d // F4
  106. #define HID_KEY_F5                  0x3e // F5
  107. #define HID_KEY_F6                  0x3f // F6
  108. #define HID_KEY_F7                  0x40 // F7
  109. #define HID_KEY_F8                  0x41 // F8
  110. #define HID_KEY_F9                  0x42 // F9
  111. #define HID_KEY_F10                 0x43 // F10
  112. #define HID_KEY_F11                 0x44 // F11
  113. #define HID_KEY_F12                 0x45 // F12
  114.  
  115. #define HID_KEY_PRINT               0x46 // Print Screen
  116. #define HID_KEY_SCROLLLOCK          0x47 // Scroll Lock
  117. #define HID_KEY_PAUSE               0x48 // Pause
  118. #define HID_KEY_INSERT              0x49 // Insert
  119. #define HID_KEY_HOME                0x4a // Home
  120. #define HID_KEY_PAGEUP              0x4b // Page Up
  121. #define HID_KEY_DELETE              0x4c // Delete Forward
  122. #define HID_KEY_END                 0x4d // End
  123. #define HID_KEY_PAGEDOWN            0x4e // Page Down
  124. #define HID_KEY_RIGHT               0x4f // Right Arrow
  125. #define HID_KEY_LEFT                0x50 // Left Arrow
  126. #define HID_KEY_DOWN                0x51 // Down Arrow
  127. #define HID_KEY_UP                  0x52 // Up Arrow
  128.  
  129. #define HID_KEY_NUMLOCK             0x53 // Num Lock and Clear
  130. #define HID_KEY_KEYPAD_SLASH        0x54 // Keypad /
  131. #define HID_KEY_KEYPAD_ASTERISK     0x55 // Keypad *
  132. #define HID_KEY_KEYPAD_MINUS        0x56 // Keypad -
  133. #define HID_KEY_KEYPAD_PLUS         0x57 // Keypad +
  134. #define HID_KEY_KEYPAD_ENTER        0x58 // Keypad ENTER
  135. #define HID_KEY_KEYPAD_1            0x59 // Keypad 1 and End
  136. #define HID_KEY_KEYPAD_2            0x5a // Keypad 2 and Down Arrow
  137. #define HID_KEY_KEYPAD_3            0x5b // Keypad 3 and PageDn
  138. #define HID_KEY_KEYPAD_4            0x5c // Keypad 4 and Left Arrow
  139. #define HID_KEY_KEYPAD_5            0x5d // Keypad 5
  140. #define HID_KEY_KEYPAD_6            0x5e // Keypad 6 and Right Arrow
  141. #define HID_KEY_KEYPAD_7            0x5f // Keypad 7 and Home
  142. #define HID_KEY_KEYPAD_8            0x60 // Keypad 8 and Up Arrow
  143. #define HID_KEY_KEYPAD_9            0x61 // Keypad 9 and Page Up
  144. #define HID_KEY_KEYPAD_0            0x62 // Keypad 0 and Insert
  145. #define HID_KEY_KEYPAD_DOT          0x63 // Keypad ./, and Delete
  146.  
  147. #define USB_HOST_TO_HOST_TIMEOUT 5
  148.  
  149.  
  150.  
  151. #ifdef MS_WINDOWS //WIN32
  152. #include <windows.h>
  153. #include <ntddscsi.h>
  154.  
  155. #else
  156. #include <stdlib.h>
  157. #include <stdio.h>
  158. #include <errno.h>
  159. #include <string.h>
  160. #include <stdarg.h>
  161. #include <fcntl.h>
  162. #include <sys/stat.h>
  163. #include <sys/types.h>
  164.  
  165. #include <scsi/sg.h>
  166. #include <sys/ioctl.h>
  167.  
  168. #define INTERFACE_ID (int)'S'    
  169. typedef int HANDLE;
  170. #define INVALID_HANDLE_VALUE -1
  171.  
  172. #define TRUE 1
  173. #define FALSE 0
  174.  
  175. typedef struct
  176. {
  177.     sg_io_hdr_t io_hdr;
  178.     unsigned char cdb[MAX_CDB_LENGTH];
  179.     unsigned char sense_buffer[MAX_SENSE_LENGTH];
  180. } SCSI_PASS_THROUGH_DIRECT;
  181. #endif
  182.  
  183. #ifdef _POSIX_C_SOURCE >= 199309L
  184. #include <time.h>   // use nanosleep
  185. #else
  186. #include <unistd.h> // use usleep
  187. #endif
  188.  
  189. unsigned char usb_h2h_cdb_read[MAX_CDB_LENGTH]     = {217,40,100,0,0,0,0,0,0,0,0,0,0,0,79,84};
  190. unsigned char usb_h2h_cdb_write[MAX_CDB_LENGTH]    = {217,42,255,0,0,0,0,0,0,0,0,0,0,0,79,84};
  191. unsigned char usb_h2h_cdb_keyboard[MAX_CDB_LENGTH] = {217,52,0,0,0,0,0,0,0,0,0,0,0,0,79,84};
  192. unsigned char usb_h2h_cdb_mouse[MAX_CDB_LENGTH]    = {217,51,0,0,0,0,0,0,0,0,0,0,0,0,79,84};
  193.  
  194. typedef struct
  195. {
  196.     SCSI_PASS_THROUGH_DIRECT scsi_op;
  197.     HANDLE handle_dev
  198.     unsigned char* buffer;
  199.     unsigned int buffer_size;
  200.     unsigned char read_count;
  201. } USB_HOST_TO_HOST_TRANSFER;
  202.  
  203.  
  204. void __ms_sleep(long milliseconds)
  205. {
  206.     if (milliseconds >= 0)
  207.     {
  208.     #ifdef MS_WINDOWS
  209.         Sleep(milliseconds);
  210.     #elif _POSIX_C_SOURCE >= 199309L
  211.         struct timespec req;
  212.         req.tv_sec = milliseconds / 1000;
  213.         req.tv_nsec = (milliseconds % 1000) * 1000000;
  214.         nanosleep(&req, NULL);
  215.     #else
  216.         usleep(milliseconds * 1000);
  217.     #endif
  218.     }
  219. }
  220.  
  221. int __is_invalid_device_handle(HANDLE handle)
  222. {
  223.     #ifdef MS_WINDOWS
  224.     return (INVALID_HANDLE_VALUE == handle);
  225.     #else
  226.     return (handle < 0);
  227.     #endif
  228. }
  229.  
  230. void __close_device_handle(HANDLE handle)
  231. {
  232.     if (__is_invalid_device_handle(handle))
  233.     {
  234.         // handle is invalid
  235.     }
  236.     else
  237.     {
  238.         #ifdef MS_WINDOWS
  239.         CloseHandle(handle);
  240.         #else
  241.         close(handle);
  242.         #endif
  243.     }
  244. }
  245.  
  246. int __is_valid_device_name(const char *dev_path)
  247. {
  248.     #ifdef MS_WINDOWS  
  249.     char tmp[] = {'\\', '\\', '.', '\\', 'X' , ':' , 0};
  250.    
  251.     if (strlen(dev_path) = strlen(tmp))
  252.     {
  253.         char drive = dev_path[4];
  254.         tmp[4] = drive;
  255.        
  256.         if ( (drive >= 'a' && drive <= 'z') || (drive >= 'A' && drive <= 'Z') )
  257.         {  
  258.             if (strncmp(dev_path, tmp, strlen(tmp)) == 0)
  259.             {
  260.                 return TRUE;
  261.             }
  262.         }
  263.     }
  264.     #else
  265.     char tmp[] = {'/', 'd', 'e', 'v', '/' , 's', 'g', 0};
  266.    
  267.     if (strlen(dev_path) > strlen(tmp))
  268.     {
  269.         if (strncmp(dev_path, tmp, strlen(tmp)) == 0)
  270.         {
  271.             return TRUE;
  272.         }
  273.     }  
  274.     #endif
  275.     return FALSE;
  276. }
  277.  
  278. int __scsi_io(USB_HOST_TO_HOST_TRANSFER *transfer)
  279. {
  280.     int result = FALSE;
  281.     int status;
  282.     if (transfer != NULL)
  283.     {
  284.     #ifdef MS_WINDOWS
  285.         int bytesReturned = 0;
  286.         DeviceIoControl(transfer->handle_dev, IOCTL_SCSI_PASS_THROUGH_DIRECT, transfer->scsi_op, sizeof(SCSI_PASS_THROUGH_DIRECT), transfer->scsi_op, 80, &bytesReturned, NULL);
  287.         if (status != 0)
  288.         {
  289.             //success
  290.             result = TRUE;
  291.         }
  292.     #else
  293.         status = ioctl(transfer->handle_dev, SG_IO, &transfer->scsi_op);   
  294.         if (status == 0)
  295.         {
  296.             //success
  297.             result = TRUE;
  298.         }
  299.         //printf("driver status: %i\n",io_hdr_write.driver_status);
  300.         //printf("host status: %i\n",io_hdr_write.host_status);    
  301.     #endif 
  302.     }
  303. }
  304.  
  305. int __fill_SCSI_PASS_THROUGH_DIRECT(SCSI_PASS_THROUGH_DIRECT *op, int direction_read, unsigned int timeout, unsigned char *cdb, unsigned int cdb_size, void *buffer, unsigned int buffer_size)
  306. {
  307.     if (op)
  308.     {
  309.         if ( ( (buffer == NULL) && (buffer_size == 0) )  || ( (buffer && (buffer_size > 0) && (buffer_size <= MAX_TRANSFER_BUFFER_LENGTH) ) ) )
  310.         {
  311.             if ((cdb_size > 0) && (cdb_size <= MAX_CDB_LENGTH))
  312.             {
  313.                 //ok
  314.             }
  315.             else
  316.             {
  317.                 return FALSE;
  318.             }
  319.         }      
  320.         else
  321.         {
  322.             return FALSE;  
  323.         }  
  324.     }
  325.     else
  326.     {
  327.         return FALSE;
  328.     }  
  329.     #ifdef MS_WINDOWS
  330.     memset(op, 0, sizeof(SCSI_PASS_THROUGH_DIRECT));
  331.    
  332.     op->ScsiStatus = -1;
  333.     op->PathId = -1;
  334.     op->TargetId = -1;
  335.     op->Lun = -1;
  336.        
  337.     op->Length = sizeof(SCSI_PASS_THROUGH_DIRECT);
  338.  
  339.     op->DataIn = direction;
  340.     op->TimeOutValue = timeout;
  341.    
  342.     op->DataBuffer = buffer;
  343.     op->DataTransferLength = buffer_size;
  344.    
  345.     memcpy(op->scsi_op.Cdb, cdb, cdb_size);
  346.     op->CdbLength = cdb_size;
  347.  
  348.     op->SenseInfoLength = MAX_SENSE_LENGTH;
  349.     op->SenseInfoOffset = 48;
  350.    
  351.     #else
  352.    
  353.     memset(&op->io_hdr, 0, sizeof(sg_io_hdr_t));
  354.     memset(&op->sense_buffer, 0, MAX_SENSE_LENGTH);
  355.     op->io_hdr.interface_id = INTERFACE_ID;
  356.     op->io_hdr.mx_sb_len = MAX_SENSE_LENGTH;
  357.     op->io_hdr.sbp = op->sense_buffer;
  358.     op->io_hdr.timeout = timeout;
  359.    
  360.     op->io_hdr.cmdp = op->cdb;
  361.     op->io_hdr.cmd_len = cdb_size;
  362.     memcpy(op->cdb, cdb, cdb_size);
  363.    
  364.     if (direction_read)
  365.     {
  366.         op->io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
  367.     }
  368.     else
  369.     {
  370.         op->io_hdr.dxfer_direction = SG_DXFER_TO_DEV;  
  371.     }
  372.     op->io_hdr.dxferp = buffer;
  373.     op->io_hdr.dxfer_len = buffer_size;
  374.    
  375.     #endif
  376.     return TRUE;
  377. }
  378.  
  379. void usb_kmcable_free_device(USB_HOST_TO_HOST_TRANSFER *transfer)
  380. {
  381.     if (transfer == NULL)
  382.     {
  383.         if (transfer->buffer)
  384.             free(transfer->buffer);
  385.         __close_device_handle(transfer->handle_dev);       
  386.     }
  387. }
  388.  
  389. void* usb_kmcable_get_device_memory(USB_HOST_TO_HOST_TRANSFER *transfer)
  390. {
  391.     if (transfer)
  392.     {  
  393.         return transfer->buffer;
  394.     }
  395.     return NULL;
  396. }
  397.  
  398.  
  399. int usb_kmcable_send_keyboard(USB_HOST_TO_HOST_TRANSFER *transfer, int usb_key_mod, int usb_key)
  400. {
  401.     unsigned char cdb[MAX_CDB_LENGTH];
  402.     int result = FALSE;
  403.     if (transfer != NULL)
  404.     {  
  405.         memcpy(&cdb[0], &usb_h2h_cdb_keyboard[0], sizeof(usb_h2h_cdb_keyboard));
  406.         cdb[2] = (usb_key_mod & 255);
  407.         cdb[4] = (usb_key & 255);
  408.         __fill_SCSI_PASS_THROUGH_DIRECT(&transfer->scsi_op, TRUE, USB_HOST_TO_HOST_TIMEOUT, cdb, MAX_CDB_LENGTH, NULL, 0);
  409.         result = __scsi_io(transfer);  
  410.     }
  411.     return result
  412. }
  413.  
  414. int usb_kmcable_send_keyboard_text(USB_HOST_TO_HOST_TRANSFER *transfer, char* text)
  415. {
  416.     int result = FALSE;
  417.     if (transfer != NULL)
  418.     {      
  419.         while (text[0] != 0)
  420.         {
  421.             result = usb_kmcable_send_keyboard(transfer, ....);
  422.             if (result == FALSE)
  423.             {
  424.                 break;
  425.             }
  426.             __ms_sleep(USB_HOST_TO_HOST_TIMEOUT);
  427.             text++;
  428.         }
  429.     }
  430.     return result;         
  431. }
  432.  
  433.  
  434.  
  435. int usb_kmcable_send_mouse(USB_HOST_TO_HOST_TRANSFER *transfer, int usb_mouse_keys, int usb_mouse_dx, int usb_mouse_dy)
  436. {
  437.     unsigned char cdb[MAX_CDB_LENGTH];
  438.     int result = FALSE;
  439.     if (transfer != NULL)
  440.     {  
  441.         memcpy(&cdb[0], &usb_h2h_cdb_mouse[0], sizeof(usb_h2h_cdb_mouse));
  442.        
  443.         if (usb_mouse_dx < - 127)
  444.             usb_mouse_dx = -127;
  445.         if (usb_mouse_dy < - 127)
  446.             usb_mouse_dy = -127;   
  447.        
  448.         if (usb_mouse_dx > 127)
  449.             usb_mouse_dx = 127;
  450.         if (usb_mouse_dy > 127)
  451.             usb_mouse_dy = 127;
  452.        
  453.         cdb[2] = (usb_mouse_keys & 255);
  454.         cdb[3] = (usb_mouse_dx & 255);
  455.         cdb[4] = (usb_mouse_dy & 255);
  456.         __fill_SCSI_PASS_THROUGH_DIRECT(&transfer->scsi_op, TRUE, USB_HOST_TO_HOST_TIMEOUT, cdb, MAX_CDB_LENGTH, NULL, 0)
  457.         result = __scsi_io(transfer);  
  458.     }
  459.     return result;     
  460. }
  461.  
  462. int usb_kmcable_send_data(USB_HOST_TO_HOST_TRANSFER *transfer)
  463. {
  464.     int result = FALSE;
  465.     if (transfer != NULL)
  466.     {
  467.         __fill_SCSI_PASS_THROUGH_DIRECT(&transfer->scsi_op, FALSE, USB_HOST_TO_HOST_TIMEOUT, usb_h2h_cdb_write, MAX_CDB_LENGTH, transfer->buffer, transfer->buffer_size);  
  468.         result = __scsi_io(transfer);  
  469.     }
  470.     return result;     
  471. }
  472.  
  473. int usb_kmcable_read_data(USB_HOST_TO_HOST_TRANSFER *transfer)
  474. {
  475.     int result = FALSE;
  476.     if (transfer != NULL)
  477.     {  
  478.         __fill_SCSI_PASS_THROUGH_DIRECT(&transfer->scsi_op, TRUE, USB_HOST_TO_HOST_TIMEOUT, usb_h2h_cdb_read, MAX_CDB_LENGTH, transfer->buffer, transfer->buffer_size);
  479.         #ifdef MS_WINDOWS
  480.             transfer->scsi_op.Cdb[8] = transfer->read_count;
  481.         #else
  482.             transfer->scsi_op.cdb[8] = transfer->read_count;
  483.         #endif 
  484.         result = __scsi_io(transfer);  
  485.         transfer->read_count++;
  486.     }
  487.     return result;
  488. }
  489.  
  490. int usb_kmcable_open_device(USB_HOST_TO_HOST_TRANSFER *transfer, char *dev_path)
  491. {
  492.     HANDLE handle = INVALID_HANDLE_VALUE;
  493.     char *buffer = NULL;
  494.     int ok = TRUE;
  495.     int vers;  
  496.    
  497.     if (transfer == NULL)
  498.     {
  499.         ok = FALSE;
  500.     }
  501.    
  502.     if (dev_path == NULL)
  503.     {
  504.         ok = FALSE;
  505.     }
  506.    
  507.     if (ok)
  508.     {      
  509.         if (!__is_valid_device_name(dev_path))
  510.         {
  511.             fprintf(stderr,"invalid device path '%s'!\n", dev_path);
  512.             ok = FALSE;
  513.         }
  514.     }
  515.    
  516.     if (ok)
  517.     {          
  518.         #ifdef MS_WINDOWS
  519.         handle = CreateFile(dev_path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, 0);
  520.         #else
  521.         handle = open(dev_path, O_RDWR);
  522.         #endif
  523.        
  524.         if (__is_invalid_device_handle(handle))
  525.         {
  526.             fprintf(stderr,"open device '%s' failed!\n", dev_path);
  527.             ok = FALSE;
  528.         }
  529.     }
  530.    
  531.     if (ok)
  532.     {      
  533.         #ifdef MS_WINDOWS
  534.         if (GetDriveType(dev_path + strlen("\\\\.\\")) != DRIVE_CDROM)
  535.         {
  536.             fprintf(stderr, "Device '%s' is not detected as CDROM drive", dev_path);
  537.             CloseHandle(handle);
  538.         }
  539.         #else
  540.         if ((ioctl(handle, SG_GET_VERSION_NUM, &vers) < 0) || (vers < 30000)) {
  541.             fprintf(stderr, "Device '%s' is not an sg device, or old sg driver\n", dev_path);
  542.            
  543.         }    
  544.         #endif
  545.     }
  546.  
  547.     if (ok)
  548.     {  
  549.         buffer = malloc(MAX_TRANSFER_BUFFER_LENGTH);
  550.         if (buffer == NULL)
  551.         {
  552.             fprintf(stderr, "Cannot allocate memory\n", dev_path);
  553.             ok = FALSE;
  554.         }
  555.     }
  556.    
  557.     if (ok)
  558.     {
  559.         transfer->handle_dev = handle;
  560.         transfer->read_count = 0;
  561.         transfer->buffer = buffer;
  562.         transfer->buffer_size = MAX_TRANSFER_BUFFER_LENGTH;    
  563.     }
  564.     else
  565.     {
  566.         if (buffer)
  567.             free(buffer);
  568.         __close_device_handle(handle);
  569.     }
  570.     return ok;
  571. }
  572.  
  573.  
  574.  
  575. void main()
  576. {
  577.     USB_HOST_TO_HOST_TRANSFER t;
  578.  
  579.     usb_kmcable_open_device(&t, "/dev/sg5");
  580.    
  581.     char *data_write = usb_kmcable_get_device_memory(&t);
  582.     memset(data_write,0, 65536);
  583.    
  584.     data_write[0] = 'H';
  585.     data_write[1] = 'a';    
  586.     data_write[2] = 'l';
  587.     data_write[3] = 'l';
  588.     data_write[4] = 'o';
  589.     data_write[5] = ' ';
  590.     data_write[6] = 'W';
  591.     data_write[7] = 'o';
  592.     data_write[8] = 'r';
  593.     data_write[9] = 'l';
  594.     data_write[10] = 'd';
  595.     data_write[11] = '1';
  596.     data_write[12] = '2';
  597.                                                
  598.     while(1)
  599.     {
  600.         printf("send\n");
  601.         usb_kmcable_send_mouse(&t, 0, 1, 1);
  602.         usb_kmcable_send_keyboard(&t,0,HID_KEY_A);
  603.         sleep(1);
  604.         usb_kmcable_send_keyboard(&t,0,0);
  605.         sleep(1);
  606.         //sendDevice(&t);
  607.        
  608.     }
  609.     return;
  610. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement