Advertisement
Guest User

Untitled

a guest
Mar 6th, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.30 KB | None | 0 0
  1. int main(int argc, const char * argv[])
  2. {
  3.    
  4. #pragma mark - Constants
  5.     static const UInt32 GDHEM6310FProductID = 0x0094;
  6.     static const UInt32 GDHEM6310FVendorID = 0x0590;
  7.     static const size_t AccessEndOkResonposeSize = 9;
  8.     static const size_t AccessStartResonposeSize = 25;
  9.    
  10. #pragma mark - Variables
  11.     int response;
  12.     bool found = FALSE;
  13.     unsigned char buf[64];
  14.     hid_device *device;
  15.     int i;
  16.     size_t buffer_length = 0;
  17.    
  18. #pragma mark - Params (When converted to a function)
  19.     unsigned char buffer[2850];
  20.     int user = 1;
  21.    
  22. #pragma mark - Device Lookup
  23.     struct hid_device_info *devs, *cur_dev;
  24.     devs = hid_enumerate(GDHEM6310FVendorID, GDHEM6310FProductID);
  25.     cur_dev = devs;
  26.    
  27.     hid_free_enumeration(devs);
  28.    
  29.     // Open the device using the VID, PID, last param optional
  30.     device = hid_open(GDHEM6310FVendorID, GDHEM6310FProductID, NULL);
  31.     if (device == NULL) {
  32.         printf("NFC Tray not found\n");
  33.         return 1;
  34.     }
  35.    
  36.     hid_set_nonblocking(device, 0);
  37.    
  38. #pragma mark - Access Start Command
  39.     for (i = 0; i < 24; i++)
  40.     {
  41.         printf("\nSending AccessStart Command\n");
  42.        
  43.         buf[0] = 0x02; // reportID
  44.         buf[1] = 0x08;
  45.         buf[2] = 0x00;
  46.         buf[3] = 0x00;
  47.         buf[4] = 0x00;
  48.         buf[5] = 0x00;
  49.         buf[6] = 0x10;
  50.         buf[7] = 0x00;
  51.         buf[8] = 0x18;
  52.        
  53.         printf("\nBytes to Write \n");
  54.         for (size_t i = 0; i < sizeof(buf); i++) {
  55.             printf("%02X ", buf[i]);
  56.         }
  57.        
  58.         //send acces start command
  59.         bool result = hid_write(device, buf, 9);
  60.         if (!result) {
  61.             printf("Device Error");
  62.             printf("Error: %ls\n", hid_error(device));
  63.             return 1;
  64.         }
  65.        
  66.         // Read start command response from device
  67.         response = hid_read(device, buf, AccessStartResonposeSize);
  68.        
  69.         // Log out our response
  70.         printf("\n");
  71.         printf("\nResponse Size: %i\n", response);
  72.         for (size_t i = 0; i < sizeof(buf); i++){
  73.             printf("%02X ", buf[i]);
  74.         }
  75.        
  76.         if (response < 0) {
  77.             printf("Unable to read()\n");
  78.         }
  79.        
  80.         if (buf[2] == 0xFF) {
  81.             printf("Device Error buf[1] == 0xFF");
  82.             printf("Error: %ls\n", hid_error(device));
  83.             return 1;
  84.         }
  85.        
  86.         if (buf[1] == 0x08 && buf[7] == 0x00) {
  87.             printf("Device Error buf[0] == 0x08 && buf[6] == 0x00 \n");
  88.             printf("Error: %ls\n", hid_error(device));
  89.             return 1;
  90.         }
  91.        
  92.         if (buf[1] != 0x18) {
  93.             continue;
  94.         }
  95.        
  96.         if (buf[7]  != 0x00 ||
  97.             buf[8]  != 0x00 ||
  98.             buf[9]  != 0x00 ||
  99.             buf[10] != 0x94 ||
  100.             buf[11] != 0x00 ||
  101.             buf[12] != 0x03 )
  102.         {
  103.             printf("unknown model \n");
  104.             printf("Error:  %ls\n", hid_error(device));
  105.         }
  106.        
  107.         found = TRUE;
  108.        
  109.         // Add elements to the buffer array
  110.         memcpy(buffer + buffer_length, buf + 8, 16);
  111.         buffer_length += 16;
  112.        
  113.         break;
  114.     }
  115.    
  116.     if (!found) {
  117.         printf("\nDevice not found \n");
  118.         printf("Error:  %ls\n", hid_error(device));
  119.         return 1;
  120.     }
  121.    
  122. #pragma mark - Read Settings Index
  123.     if (true)
  124.     {
  125.         printf("\nReading setting index \n");
  126.        
  127.         buf[0] = 0x02;
  128.         buf[1] = 0x08;
  129.         buf[2] = 0x01;
  130.         buf[3] = 0x00;
  131.         buf[4] = 0x0F;
  132.         buf[5] = 0x74;
  133.         buf[6] = 0x1E;
  134.         buf[7] = 0x00;
  135.         buf[8] = 0x6C;
  136.        
  137.         // TODO: Check for Cancellation Here
  138.        
  139.         printf("Bytes to write for setting index: \n");
  140.         for (size_t i = 0; i < sizeof(buf); i++) {
  141.             printf("%02X ", buf[i]);
  142.         }
  143.        
  144.         // Write Command
  145.         bool result = hid_write(device, buf, 64);
  146.        
  147.         if (!result) {
  148.             printf("Device Error result from write failed");
  149.             printf("Error: %ls\n", hid_error(device));
  150.             return 1;
  151.         }
  152.        
  153.         // TODO: Check For Cancellation
  154.        
  155.         // Read Report
  156.         response = hid_read(device, buf, 64);
  157.         printf("\n");
  158.         printf("Bytes read from setting index \n");
  159.         for (size_t i = 0; i < 64; i++) {
  160.             printf("%02X ", buf[i]);
  161.         }
  162.        
  163.         // TODO: Check For Cancellation
  164.        
  165.         if (buf[2] == 0xFF) {
  166.             printf("\nDevice Error on setting index read\n");
  167.             printf("Error: %ls\n", hid_error(device));
  168.             return 1;
  169.         }
  170.        
  171.         // Add items to buffer
  172.         memcpy(buffer + buffer_length, buf + 8, 30);
  173.         buffer_length += 30;
  174.     }
  175.    
  176. #pragma mark - Read Data
  177.     for (int address = 0x0370; address < 0x0370 + 1400 + 1400; address += 28)
  178.     {
  179.         printf("\nReading Data: 0x%04x \n", address);
  180.        
  181.         buf[0] = 0x02;
  182.         buf[1] = 0x08;
  183.         buf[2] = 0x01;
  184.         buf[3] = 0x00;
  185.         buf[4] = (address >> 8) & 0xff;
  186.         buf[5] = (address & 0xff);
  187.         buf[6] = 0x1C;
  188.         buf[7] = 0x00;
  189.         buf[8] = (buf[1] ^ buf[2] ^ buf[3] ^ buf[4] ^ buf[5] ^ buf[6] ^ buf[7]);
  190.        
  191.         // TODO: Check For Cancellation
  192.        
  193.         printf("\nBytes to write for reading data: \n");
  194.         for (size_t i = 0; i < 64; i++) {
  195.             printf("%02X ", buf[i]);
  196.         }
  197.        
  198.         // Write Command
  199.         bool result = hid_write(device, buf, 64);
  200.         if (!result) {
  201.             printf("Device Error result from write failed");
  202.             printf("Error: %ls\n", hid_error(device));
  203.             return 1;
  204.         }
  205.        
  206.         // TODO: Check For Cancellation
  207.        
  208.         // Read Report
  209.         response = hid_read(device, buf, 64);
  210.        
  211.         // Log out our response
  212.         printf("\n");
  213.         printf("\nRead Data response size: %i\n", response);
  214.         for (size_t i = 0; i < 64; i++) {
  215.             printf("%02X ", buf[i]);
  216.         }
  217.        
  218.         // TODO: Check For Cancellation
  219.        
  220.         if (buf[2] == 0xFF) {
  221.             printf("read data device error");
  222.             return 1;
  223.         }
  224.        
  225.         // Add elements to buffer array
  226.         memcpy(buffer + buffer_length, buf + 8, 28);
  227.         buffer_length += 28;
  228.         printf("buffer_length = %zu", buffer_length);
  229.     }
  230.    
  231. #pragma mark - User Conditions
  232.     if (user == 1)
  233.     {
  234.         // Zero out setting index, block 0, bytes 3 and 4
  235.         for (size_t i = 16 + 2; i < 16; i++){
  236.             buffer[i] = 0x00;
  237.         }
  238.        
  239.         // Zero out setting index, block 0, bytes 7 and 8
  240.         for (size_t i = 16 + 6; i < 16 + 2; i++){
  241.             buffer[i] = 0x00;
  242.         }
  243.        
  244.         // Zero out setting index, block 2
  245.         for (size_t i = 16 + 8 + 6; i < 16 + 8 + 6 + 6; i++){
  246.             buffer[i] = 0x00;
  247.         }
  248.        
  249.         // Zero out user2 blood pressure data
  250.         for (size_t i = 16 + 30 + 1400; i < 16 + 30 + 1400 + 1400; i++){
  251.             buffer[i] = 0x00;
  252.         }
  253.         // Log out the buffer
  254.         for (size_t i= 0; i < sizeof(buffer); i++) {
  255.             printf("%02X ", buffer[i]);
  256.         }
  257.     }
  258.     else
  259.     {
  260.         // Zero out setting index, block 0, bytes 1 and 2
  261.         for (size_t i = 16; i < 16 + 2; i++){
  262.             buffer[i] = 0x00;
  263.         }
  264.        
  265.         // Zero out setting index, block 0, bytes 5 and 6
  266.         for (size_t i = 16 + 4; i < 16 + 4 +2; i++){
  267.             buffer[i] = 0x00;
  268.         }
  269.        
  270.         // Zero out setting index, block 1
  271.         for (size_t i = 16 + 8; i < 16 + 8 + 6; i++){
  272.             buffer[i] = 0x00;
  273.         }
  274.        
  275.         // Zero out user1 blood pressure data
  276.         for (size_t i = 16 + 30; i < 16 + 30 + 1400; i++){
  277.             buffer[i] = 0x00;
  278.         }
  279.         // Log out the buffer
  280.         for (size_t i= 0; i < sizeof(buffer); i++) {
  281.             printf("%02X ", buffer[i]);
  282.         }
  283.     }
  284.    
  285. # pragma mark - Access End Command
  286.     if (true)
  287.     {
  288.         printf("\nSending AccessEnd Command \n");
  289.        
  290.         buf[0] = 0x02;
  291.         buf[1] = 0x08;
  292.         buf[2] = 0x0f;
  293.         buf[3] = 0x00;
  294.         buf[4] = 0x00;
  295.         buf[5] = 0x00;
  296.         buf[6] = 0x00;
  297.         buf[7] = 0x00;
  298.         buf[8] = 0x07;
  299.        
  300.         printf("\nBytes to write for AccessEnd command \n");
  301.         for (int i = 0; i < 9; i++){
  302.             printf("%02X ", buf[i]);
  303.         }
  304.        
  305.         // Write Commmand
  306.         bool result = hid_write(device, buf, 9);
  307.         if (!result){
  308.             printf("Device Error writing AccessEnd Command");
  309.             return 1;
  310.         }
  311.        
  312.         // Read Response
  313.         response = hid_read(device, buf, AccessEndOkResonposeSize);
  314.        
  315.         // Log Response
  316.         printf("\n");
  317.         printf("\nresponse size: %i\n", response);
  318.         for (int i = 0; i < AccessEndOkResonposeSize; i++){
  319.             printf("%02X ", buf[i]);
  320.         }
  321.        
  322.         if (buf[2] == 0xFF){
  323.             printf("\nDevice error in AccessEnd Command \n");
  324.             return 1;
  325.         }
  326.        
  327.         printf("Closing Device");
  328.         //hid_close(device);
  329.     }
  330.  
  331.     return 0;
  332. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement