/*
 * Receive USB info from development board
 *
 * This program is free software; you can
 * redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by
 * the Free Software Foundation, version 2 of the
 * License.
 *
 */
/* #include <stdio.h>  */
/* #include <string.h> */
#include <usb.h>
#include <limits.h> 
#include <sys/time.h>
#define NONE    0x00
#define USB_VENDOR_ID   0x10c4
#define USB_PRODUCT_ID  0x0000

/* Data types */
struct usb_device;
struct usb_bus;

/*
 * To maintain compatibility with applications already built with libusb,
 * we must only add entries to the end of this structure. NEVER delete or
 * move members and only change types if you really know what you're doing.
 */
struct usb_device {
  struct usb_device *next, *prev;

  char filename[PATH_MAX + 1];

  struct usb_bus *bus;

  struct usb_device_descriptor descriptor;
  struct usb_config_descriptor *config;

  void *dev;            /* Darwin support */

  u_int8_t devnum;

  unsigned char num_children;
  struct usb_device **children;
};

struct usb_bus {
  struct usb_bus *next, *prev;

  char dirname[PATH_MAX + 1];

  struct usb_device *devices;
  u_int32_t location;

  struct usb_device *root_dev;
};

struct usb_dev_handle;
typedef struct usb_dev_handle usb_dev_handle;




extern struct usb_bus *usb_busses;


static struct usb_device *device_init(void)
{
    struct usb_bus *usb_bus;
    struct usb_device *dev;
    usb_init();
    usb_find_busses();
    usb_find_devices();
    for (usb_bus = usb_busses;
         usb_bus;
         usb_bus = usb_bus->next) {
        for (dev = usb_bus->devices;
             dev;
             dev = dev->next) {
            if ((dev->descriptor.idVendor
                  == USB_VENDOR_ID) &&
                (dev->descriptor.idProduct
                  == USB_PRODUCT_ID))
                return dev;
        }
    }
    return NULL;
}
int main(int argc, char **argv)
{
    struct usb_device *usb_dev;
    struct usb_dev_handle *usb_handle;
    int retval = 1;
    int i, bytesc;
    char bytesi[10], byteso[10];	
    unsigned char color = NONE;
    usb_dev = device_init();
    if (usb_dev == NULL) {
        fprintf(1, "Device not foundn\n");
        goto exit;
    }
    usb_handle = usb_open(usb_dev);
    if (usb_handle == NULL) {
        fprintf(1, "Cannot open\n");
        goto exit;
    }
/*    usb_handle = usb_claim_interface(usb_dev, 1);
    if (usb_handle == NULL) {
        fprintf(1,
             "Not able to claim the USB device\n");
        goto exit;
    } */
    if (argc == 1) {
    }
    
/* int usb_interrupt_write(usb_dev_handle *dev, int ep, char *bytes, int size, int timeout); */

/* int usb_get_descriptor_by_endpoint(usb_dev_handle *dev, int ep, unsigned char type, unsigned char index, void *buf, int size); */



bytesc= usb_interrupt_read(usb_handle , 1, bytesi, 8 , 1000);
    printf ("bytes in %d\n", bytesc);

for (i=0; i<5; i++) {
	printf("id : %d, byte :%x, %d\n", i, bytesi[i], bytesi[i]);
}

printf ("input : %s, %s, %s ", argv[1], argv[2], argv[3]);

for (i=0; i<8; i++) {
	byteso[i]=(char)0;
}

if (strcasecmp(argv[1], "1") == 0) { byteso[0] = 1; }
if (strcasecmp(argv[2], "1") == 0) { byteso[1] = 1; }
if (strcasecmp(argv[3], "1") == 0) { byteso[2] = 255; }

bytesc=usb_clear_halt(usb_handle, 2); 
/*    printf ("clear ep2 : %d\n", bytesc); */

bytesc=usb_interrupt_write(usb_handle, 2, byteso, 8, 1000); 
/*    printf ("bytes out %d\n", bytesc); */

/* for (i=0; i<8; i++) {
	printf("id : %d, byte :%x, %d\n", i, byteso[i], byteso[i]);
}*/


    retval = 0;
exit:
    usb_close(usb_handle);
    return retval;
}

