Advertisement
Guest User

host usb code

a guest
Jun 7th, 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_APOSTROPHE          0x34 // ' and "
  90. #define HID_KEY_DOUBLE_QUOTES       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. #ifdef MS_WINDOWS
  148. #include <windows.h>
  149. #include <ntddscsi.h>
  150.  
  151. #else
  152.  
  153. #include <stdio.h>
  154. #include <errno.h>
  155. #include <string.h>
  156. #include <stdarg.h>
  157. #include <fcntl.h
  158. #include <sys/stat.h>
  159. #include <sys/types.h>
  160.  
  161. #include <scsi/sg.h>
  162. #include <sys/ioctl.h>
  163.  
  164. #define INTERFACE_ID 'S'    
  165. typedef int HANDLE;
  166. #define INVALID_HANDLE_VALUE -1
  167.  
  168. #define TRUE 1
  169. #define FALSE 0
  170.  
  171. typedef struct
  172. {
  173.     sg_io_hdr_t io_hdr;
  174.     unsigned char cdb[MAX_CDB_LENGTH];
  175.     unsigned char sense_buffer[MAX_SENSE_LENGTH];
  176. } SCSI_PASS_THROUGH_DIRECT;
  177. #endif
  178.  
  179. 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};
  180. 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};
  181. 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};
  182. 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};
  183.  
  184. typedef struct
  185. {
  186.     SCSI_PASS_THROUGH_DIRECT scsi_op;
  187.     HANDLE handle_dev
  188.     unsigned char* buffer;
  189.     unsigned int buffer_size;
  190.     unsigned char read_count;
  191. } USB_HOST_TO_HOST_TRANSFER;
  192.  
  193. int is_invalid_device_handle(HANDLE handle)
  194. {
  195.     #ifdef MS_WINDOWS
  196.     return (INVALID_HANDLE_VALUE == handle);
  197.     #else
  198.     return (handle < 0);
  199.     #endif
  200. }
  201.  
  202. void close_device_handle(HANDLE handle)
  203. {
  204.     if (is_invalid_device_handle(handle))
  205.     {
  206.         // handle is invalid
  207.     }
  208.     else
  209.     {
  210.         #ifdef MS_WINDOWS
  211.         CloseHandle(handle);
  212.         #else
  213.         close(handle);
  214.         #endif
  215.     }
  216. }
  217.  
  218. int is_valid_device_name(const char *dev_path)
  219. {
  220.     #ifdef MS_WINDOWS  
  221.     char tmp[] = {'\\', '\\', '.', '\\', 'X' , ':' , 0};
  222.    
  223.     if (strlen(dev_path) = strlen(tmp))
  224.     {
  225.         char drive = dev_path[4];
  226.         tmp[4] = drive;
  227.        
  228.         if ( (drive >= 'a' && drive <= 'z') || (drive >= 'A' && drive <= 'Z') )
  229.         {  
  230.             if (strncmp(dev_path, tmp, strlen(tmp)) == 0)
  231.             {
  232.                 return TRUE;
  233.             }
  234.         }
  235.     }
  236.     #else
  237.     char tmp[] = {'/', 'd', 'e', 'v', '/' , 's', 'g', 0};
  238.    
  239.     if (strlen(dev_path) > strlen(tmp))
  240.     {
  241.         if (strncmp(dev_path, tmp, strlen(tmp)) == 0)
  242.         {
  243.             return TRUE;
  244.         }
  245.     }  
  246.     #endif
  247.     return FALSE;
  248. }
  249.  
  250. void freeDevice(USB_HOST_TO_HOST_TRANSFER *transfer)
  251. {
  252.     if (transfer == NULL)
  253.     {
  254.         if (transfer->buffer)
  255.             free(transfer->buffer);
  256.         close_device_handle(transfer->handle_dev);     
  257.     }
  258. }
  259.  
  260. void* getDeviceMemory(USB_HOST_TO_HOST_TRANSFER *transfer)
  261. {
  262.     if (transfer )
  263.     {  
  264.         return transfer->buffer;
  265.     }
  266.     return NULL;
  267. }
  268.  
  269. 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)
  270. {
  271.     if (op)
  272.     {
  273.         if ( ( (buffer == NULL) && (buffer_size == 0) )  || ( (buffer && (buffer_size > 0) && (buffer_size <= MAX_TRANSFER_BUFFER_LENGTH) ) )
  274.         {
  275.             if ((cdb_size > 0) && (cdb_size <= MAX_CDB_LENGTH))
  276.             {
  277.                 //ok
  278.             }
  279.             else
  280.             {
  281.                 return FALSE;
  282.             }
  283.         }      
  284.         else
  285.         {
  286.             return FALSE;  
  287.         }  
  288.     }
  289.     else
  290.     {
  291.         return FALSE;
  292.     }  
  293.     #ifdef MS_WINDOWS
  294.     memset(op, 0, sizeof(SCSI_PASS_THROUGH_DIRECT));
  295.    
  296.     op->ScsiStatus = -1;
  297.     op->PathId = -1;
  298.     op->TargetId = -1;
  299.     op->Lun = -1;
  300.        
  301.     op->Length = sizeof(SCSI_PASS_THROUGH_DIRECT);
  302.  
  303.     op->DataIn = direction;
  304.     op->TimeOutValue = timeout;
  305.    
  306.     op->DataBuffer = data;
  307.     op->DataTransferLength = data_size;
  308.    
  309.     memcpy(sptd->Cdb, cdb, cdb_size);
  310.     op->CdbLength = cdb_size;
  311.  
  312.     op->SenseInfoLength = SENSE_LENGTH;
  313.     op->SenseInfoOffset = 48;
  314.     #else
  315.     memset(&op->io_hdr, 0, sizeof(sg_io_hdr_t));
  316.     memset(&op->sense_buffer, 0, MAX_SENSE_LENGTH);
  317.     op->io_hdr.interface_id = INTERFACE_ID;
  318.     op->io_hdr.mx_sb_len = MAX_SENSE_LENGTH;
  319.     op->io_hdr.timeout = timeout;
  320.     op->io_hdr.cmdp = op->cdb;
  321.     op->io_hdr.cmd_len = cdb_size;
  322.  
  323.     memcpy(op->cdb, cdb, cdb_size);
  324.     if (direction_read)
  325.     {
  326.         op->io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
  327.     }
  328.     else
  329.     {
  330.         op->io_hdr.dxfer_direction = SG_DXFER_TO_DEV;  
  331.     }
  332.     op->io_hdr.dxferp = buffer;
  333.     op->io_hdr.dxfer_len = buffer_size;
  334.     #endif
  335.     return TRUE;
  336. }
  337.  
  338. int io()
  339. {
  340.     #ifdef MS_WINDOWS
  341.     status = DeviceIoControl(transfer->handle_dev, IOCTL_SCSI_PASS_THROUGH_DIRECT, transfer->scsi_op, 44, transfer->scsi_op, 80, &status, NULL);
  342.     #else
  343.     status = ioctl(transfer->handle_dev, SG_IO, &transfer->scsi_op);
  344.     if (status < 0) {
  345.         // failure
  346.     }
  347.     else
  348.     {
  349.         // success
  350.        
  351.     }      
  352.     #endif         
  353. }
  354.  
  355.  
  356.  
  357. int sendKeyboard(USB_HOST_TO_HOST_TRANSFER *transfer, int usb_key_mod, int usb_key)
  358. {
  359.     int status;
  360.     unsigned char cdb[MAX_CDB_LENGTH];
  361.     memcpy(&cdb[0], &usb_h2h_cdb_keyboard[0], sizeof(usb_h2h_cdb_keyboard));
  362.     cdb[2] = (usb_key_mod & 255);
  363.     cdb[4] = (usb_key & 255);
  364.    
  365.     __fill_SCSI_PASS_THROUGH_DIRECT(&transfer->scsi_op, TRUE, 5, cdb, MAX_CDB_LENGTH, NULL, 0);
  366.     #ifdef MS_WINDOWS
  367.     DeviceIoControl(transfer->handle_dev, IOCTL_SCSI_PASS_THROUGH_DIRECT, transfer->scsi_op, 44, transfer->scsi_op, 80, &status, NULL);
  368.     #else
  369.     status = ioctl(transfer->handle_dev, SG_IO, &transfer->scsi_op);   
  370.     #endif
  371.     //printf("driver: %i\n",io_hdr_write.driver_status);
  372.     //printf("driver w: %i\n",io_hdr_write.host_status);       
  373. }
  374.  
  375.  
  376. int sendMouse(USB_HOST_TO_HOST_TRANSFER *transfer, int usb_mouse_keys, int usb_mouse_dx, int usb_mouse_dy)
  377. {
  378.     int status;
  379.     unsigned char cdb[MAX_CDB_LENGTH];
  380.     memcpy(&cdb[0], &usb_h2h_cdb_mouse[0], sizeof(usb_h2h_cdb_mouse));
  381.    
  382.     if (usb_mouse_dx < - 127)
  383.         usb_mouse_dx = -127;
  384.     if (usb_mouse_dy < - 127)
  385.         usb_mouse_dy = -127;   
  386.    
  387.     if (usb_mouse_dx > 127)
  388.         usb_mouse_dx = 127;
  389.     if (usb_mouse_dy > 127)
  390.         usb_mouse_dy = 127;
  391.    
  392.     cdb[2] = (usb_mouse_keys & 255);
  393.     cdb[3] = (usb_mouse_dx & 255);
  394.     cdb[4] = (usb_mouse_dy & 255);
  395.     __fill_SCSI_PASS_THROUGH_DIRECT(&transfer->scsi_op, TRUE, 5, cdb, MAX_CDB_LENGTH, NULL, 0);
  396.    
  397.     #ifdef MS_WINDOWS
  398.     DeviceIoControl(transfer->handle_dev, IOCTL_SCSI_PASS_THROUGH_DIRECT, transfer->scsi_op, 44, transfer->scsi_op, 80, &status, NULL);
  399.     #else
  400.     status = ioctl(transfer->handle_dev, SG_IO, &transfer->scsi_op);   
  401.     #endif
  402.    
  403.     //printf("driver: %i\n",io_hdr_write.driver_status);
  404.     //printf("driver w: %i\n",io_hdr_write.host_status);       
  405. }
  406.  
  407.  
  408. int sendDevice(USB_HOST_TO_HOST_TRANSFER *transfer)
  409. {
  410.     int status;
  411.     __fill_SCSI_PASS_THROUGH_DIRECT(&transfer->scsi_op, FALSE, 5, usb_h2h_cdb_write, MAX_CDB_LENGTH, transfer->buffer, transfer->buffer_size)
  412.     status = ioctl(transfer->handle_dev, SG_IO, &transfer->scsi_op);   
  413.     //printf("driver: %i\n",io_hdr_write.driver_status);
  414.     //printf("driver w: %i\n",io_hdr_write.host_status);
  415.            
  416. }
  417.  
  418. int readDevice(USB_HOST_TO_HOST_TRANSFER *transfer)
  419. {
  420.     int status;
  421.    
  422.     __fill_SCSI_PASS_THROUGH_DIRECT(&transfer->scsi_op, TRUE, 5, usb_h2h_cdb_read, MAX_CDB_LENGTH, transfer->buffer, transfer->buffer_size);   
  423.     #ifdef MS_WINDOWS
  424.         transfer->scsi_op.Cdb[8] = transfer->read_count;
  425.     #else
  426.         transfer->scsi_op.cdb[8] = transfer->read_count;
  427.     #endif 
  428.     status = ioctl(transfer->handle_dev, SG_IO, &transfer->scsi_op);
  429.     transfer->read_count++;
  430.     //printf("driver: %i\n",io_hdr_write.driver_status);
  431.     //printf("driver w: %i\n",io_hdr_write.host_status);       
  432. }
  433.  
  434.  
  435. int openDevice(USB_HOST_TO_HOST_TRANSFER *transfer, char *dev_path)
  436. {
  437.     HANDLE handle = INVALID_HANDLE_VALUE;
  438.     char *buffer = NULL;
  439.     int ok = TRUE;
  440.     int vers;  
  441.    
  442.     if (transfer == NULL)
  443.     {
  444.         ok = FALSE;
  445.     }
  446.    
  447.     if (dev_path == NULL)
  448.     {
  449.         ok = FALSE;
  450.     }
  451.    
  452.     if (ok)
  453.     {      
  454.         if (!is_valid_device_name(dev_path))
  455.         {
  456.             fprintf(stderr,"invalid device path '%s'!\n", dev_path);
  457.             ok = FALSE;
  458.         }
  459.     }
  460.    
  461.     if (ok)
  462.     {          
  463.         #ifdef MS_WINDOWS
  464.         handle = CreateFile(dev_path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, 0);
  465.         #else
  466.         handle = open(dev_path, O_RDWR);
  467.         #endif
  468.        
  469.         if (is_invalid_device_handle(handle))
  470.         {
  471.             fprintf(stderr,"open device '%s' failed!\n", dev_path);
  472.             ok = FALSE;
  473.         }
  474.     }
  475.    
  476.     if (ok)
  477.     {      
  478.         #ifdef MS_WINDOWS
  479.         if (GetDriveType(dev_path + strlen("\\\\.\\")) != DRIVE_CDROM)
  480.         {
  481.             fprintf(stderr, "Device '%s' is not detected as CDROM drive", dev_path);
  482.             CloseHandle(handle);
  483.         }
  484.         #else
  485.         if ((ioctl(handle, SG_GET_VERSION_NUM, &vers) < 0) || (vers < 30000)) {
  486.             fprintf(stderr, "Device '%s' is not an sg device, or old sg driver\n", dev_path);
  487.            
  488.         }    
  489.         #endif
  490.     }
  491.  
  492.     if (ok)
  493.     {  
  494.         buffer = malloc(MAX_TRANSFER_BUFFER_LENGTH);
  495.         if (buffer == NULL)
  496.         {
  497.             fprintf(stderr, "Cannot allocate memory\n", dev_path);
  498.             ok = FALSE;
  499.         }
  500.     }
  501.    
  502.     if (ok)
  503.     {
  504.         transfer->handle_dev = handle;
  505.         transfer->read_count = 0;
  506.         transfer->buffer = buffer;
  507.         transfer->buffer_size = MAX_TRANSFER_BUFFER_LENGTH;    
  508.     }
  509.     else
  510.     {
  511.         if (buffer)
  512.             free(buffer);
  513.         close_device_handle(handle);
  514.     }
  515.     return ok;
  516. }
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523. void main()
  524. {
  525.     USB_HOST_TO_HOST_TRANSFER t;
  526.  
  527.     openDevice(&t, "/dev/sg2");
  528.    
  529.     char *data_write = getDeviceMemory(&t);
  530.     memset(data_write,0, 65536);
  531.    
  532.     data_write[0] = 'H';
  533.     data_write[1] = 'a';    
  534.     data_write[2] = 'l';
  535.     data_write[3] = 'l';
  536.     data_write[4] = 'o';
  537.     data_write[5] = ' ';
  538.     data_write[6] = 'W';
  539.     data_write[7] = 'o';
  540.     data_write[8] = 'r';
  541.     data_write[9] = 'l';
  542.     data_write[10] = 'd';
  543.     data_write[11] = '1';
  544.     data_write[12] = '2';
  545.                                                
  546.  
  547.     while(1)
  548.     {
  549.        
  550.         sendDevice(&t);
  551.        
  552.     }
  553.         /*
  554.     getSGHDR(&io_hdr_write, SG_DXFER_TO_DEV, 500, cdb_send, 16, data_write, data_size_write);
  555.     status = ioctl(fd_write, SG_IO, &io_hdr_write);
  556.    
  557.         printf("driver: %i\n",io_hdr_write.driver_status);
  558.             printf("driver w: %i\n",io_hdr_write.host_status);
  559.         //  printf("driver w: %s\n",io_hdr_write->sbp+2);
  560.    
  561.             if (status < 0) {
  562.             // failure
  563.             printf("write fail\n");
  564.  
  565.         }
  566.         else {
  567.             // success
  568.             printf("write success\n");
  569.         }
  570.     //free(io_hdr_write);
  571.    
  572.     SG_DXFER_TO_DEV SG_DXFER_FROM_DEV
  573.     getSGHDR(&io_hdr_read, SG_DXFER_FROM_DEV, 500, cdb_read, 16, data_read, data_size_read);
  574.     status = ioctl(fd_read, SG_IO, &io_hdr_read);  
  575.    
  576.     cdb_read[8]++;
  577.     //  printf("driver read: %i\n",io_hdr_read->driver_status);
  578.     //      printf("driver: %s\n",io_hdr_read->sbp);
  579.     //  printf("driver read: %s\n",io_hdr_read->sbp+2);
  580.        
  581.        
  582.         if (status < 0) {
  583.             // failure
  584.             printf("read fail\n");
  585.  
  586.         }
  587.         else {
  588.             // success
  589.             printf("read %s\n",data_read);
  590.             //printf("read %i\n",*((int*)data_read));
  591.             //printf("read %i\n",*((int*)data_read+4));
  592.         }  
  593.    
  594.     */
  595.    
  596.    
  597. return;
  598. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement