// test read data from AIWI by using 'hidapi' and gcc on linux-embedded (armv71)
// Linux orangepi zero 6.12.20-current-sunxi (32 bit)
// 20/05/2025
//
// example : https://libusb.info/hidapi/test_8c-example.html
//
// install:
// sudo apt install libhidapi-dev
// 
// compile:
// gcc testhid.c -o testhid    -or-
// gcc testhid.c -o testhid -I/usr/include/hidapi -lhidapi-libusb
//
//
#include <stdio.h> // printf
#include <wchar.h> // wchar_t

#include <hidapi.h>

#define MAX_STR 255

int main(int argc, char* argv[])
{
        int res;
        unsigned char buf[65];
        unsigned char bufRead[33];
        unsigned char bufWrite[33];
        wchar_t wstr[MAX_STR];
        hid_device *handle;
        int i;

        // Initialize the hidapi library
        res = hid_init();

        // Open the device using the VID, PID,
        // and optionally the Serial number.
        handle = hid_open(0x20a0, 0x413a, NULL);
        if (!handle) {
                printf("Unable to open device\n");
                hid_exit();
                return 1;
        }

        // Read the Manufacturer String
        res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
        printf("Manufacturer String: %ls\n", wstr);

        // Read the Product String
        res = hid_get_product_string(handle, wstr, MAX_STR);
        printf("Product String: %ls\n", wstr);

        // Read the Serial Number String
        res = hid_get_serial_number_string(handle, wstr, MAX_STR);
        printf("Serial Number String: (%d) %ls\n", wstr[0], wstr);

        // Read Indexed String 1
        res = hid_get_indexed_string(handle, 1, wstr, MAX_STR);
        printf("Indexed String 1: %ls\n", wstr);
		
		// this is a "RDBL05" command (AES Encoded) for read data block 05
        printf("sending...\n");
        bufWrite[0] = 0x0;
        bufWrite[1] = 0x60;   bufWrite[2] = 0x8D;   bufWrite[3] = 0x68;   bufWrite[4] = 0x80;
        bufWrite[5] = 0xE1;   bufWrite[6] = 0xEC;   bufWrite[7] = 0xE;   bufWrite[8] = 0xC7;
        bufWrite[9] = 0xDB;   bufWrite[10] = 0xCD;   bufWrite[11] = 0x78;   bufWrite[12] = 0xDE;
        bufWrite[13] = 0x6F;   bufWrite[14] = 0x39;   bufWrite[15] = 0x67;   bufWrite[16] = 0xF;
        bufWrite[17] = 0x81;   bufWrite[18] = 0xC7;   bufWrite[19] = 0x7E;   bufWrite[20] = 0x67;
        bufWrite[21] = 0x5C;   bufWrite[22] = 0x7F;   bufWrite[23] = 0x52;   bufWrite[24] = 0xED;
        bufWrite[25] = 0x48;   bufWrite[26] = 0xC1;   bufWrite[27] = 0x19;   bufWrite[28] = 0xE3;
        bufWrite[29] = 0x39;   bufWrite[30] = 0x7;   bufWrite[31] = 0xB1;   bufWrite[32] = 0x58;
        bufWrite[33] = 0x0;
        res = hid_send_feature_report(handle, bufWrite, 33);
        if (res < 0) {
                printf("Unable to send a feature report: %ls\n", hid_error(handle));
        }

        // Read requested state
        printf("receiving...\n");
        bufRead[0] = 0x0;
        res = hid_get_feature_report(handle, bufRead, 33);
        if (res < 0) {
                printf("Unable to get a feature report: %ls\n", hid_error(handle));
        }else{
                printf("data >>\n");
                for (i = 0; i < 33; i++) {
                        printf("[%d] = 0x%02x,  ", i, bufRead[i]);
                        if (i % 4 == 0)
                                printf("\n");
                }
        }

        printf("close device...\n");

        // Close the device
        hid_close(handle);

        // Finalize the hidapi library
        res = hid_exit();

        return 0;
}