Advertisement
Guest User

C getData Driver code

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