#include <hid.h>
#include <stdio.h>
#include <string.h>

// bool match_serial_number(struct usb_dev_handle* usbdev, void* custom, unsigned int len)
// {
//   bool ret;
//   char* buffer = (char*)malloc(len);
//   usb_get_string_simple(usbdev, usb_device(usbdev)->descriptor.iSerialNumber,
//       buffer, len);
//   ret = strncmp(buffer, (char*)custom, len) == 0;
//   free(buffer);
//   return ret;
// }

int main(void)
{
  HIDInterface* hid;
  hid_return ret;

  HIDInterfaceMatcher matcher = { 0x04b4, 0x0f1f, NULL, NULL, 0 };

  ret = hid_init();
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_init failed with return code %d\n", ret);
    return 1;
  }

  hid = hid_new_HIDInterface();
  if (hid == 0) {
    fprintf(stderr, "hid_new_HIDInterface() failed, out of memory?\n");
    return 1;
  }

  ret = hid_force_open(hid, 0, &matcher, 3);



  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_force_open failed with return code %d\n", ret);
    return 1;
  }

  int const WRITE_PACKET_LEN = 33;
  char write_packet[33] = { 0x10,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
                            0x00,0x00,0x00 };
  int const READ_PACKET_LEN = 33;
  char read_packet[33];

  ret = hid_interrupt_write(hid,2,write_packet,WRITE_PACKET_LEN,1);
  sleep(5);
  ret = hid_close(hid);
  ret = hid_cleanup();
//   ret = hid_interrupt_read(hid,USB_ENDPOINT_IN+1,read_packet,READ_PACKET_LEN,0);
  return 0;
}


