
#include <stdio.h>
#include <string.h>
#include <usb.h>
#include <assert.h>

void checkStatus(int status) {
	if (status) {
   		printf("  Status = %d: %s\n", status, usb_strerror());
	}
}

void printfbuf(char *buf, int size, char *str)
{
    int i;

    printf("%s:", str);

    for (i = 0; i < size; i++)
        printf(" %X", (unsigned char)(buf[i]));

    printf("\n");

}


void checkPacket10_47(char * buf)
{
	// start with 0x3, then bytes 4, 5 must be the same.
	// rest zeros
	int i;	
	int faulty = 0;
	
	for (i = 4; i < 7; i += 2) {
		if (buf[i] != buf[i+1])
			faulty = 1;
	}
	
	if (faulty) {
		printf("Packet not as expected.\n");
		printfbuf(buf, 8, "Packets");
	}
}


void assertSameBuf(char *buf1, char *buf2, int size,
                   char *str)
{
    int i, j;

    for (i = 0; i < size; i++) {
        if (buf1[i] != buf2[i]) {
            printf("AssertSameBuf not same: %s\n", str);
            printf("Buf 1:\n");

            for (j = 0; j < size; j++)
                printf(" %X", (unsigned char) (buf1[j]));

            printf("\nBuf 2:\n");

            for (j = 0; j < size; j++)
                printf(" %X", (unsigned char) (buf2[j]));

            printf("\n");
            return;
        }
    }

}

int checkNBytes(int n, char * msg)
{
  if (n < 0)
    printf("%s: Transfer error\n", msg);
  else
    printf("%s: Transferred %d bytes\n", msg, n);

  return n;
}

int my_usb_interrupt_write(usb_dev_handle *dev, int ep, char *bytes,
			   int size, int timeout)
{
  return checkNBytes(usb_interrupt_write(dev, ep, bytes, size, timeout), "int_write");

}

int my_usb_interrupt_read(usb_dev_handle *dev, int ep, char *bytes,
			  int size, int timeout)
{
  return checkNBytes(usb_interrupt_read(dev, ep, bytes, size, timeout), "int_read");

}

void init_setup(usb_dev_handle * hdev, char * setupbuf)
{
	int i;
	
    struct usb_device *dev = usb_device(hdev);
    int ep =
        dev->config->interface->altsetting->endpoint->bEndpointAddress;
        
    int timeout = 1000;
    int count;

    char buf8[8] = { 0, 0, 0x12, 0, 0, 0, 0, 0 };
	char buf36[36] = {0, 0x03, 0xAB, 0x02, 0, 0x03, 0xAB, 0x02,
					 0, 0, 0, 0, 0, 0, 0, 0,
					 0, 0, 0, 0, 0, 0, 0, 0,
					 0, 0, 0, 0, 0, 0, 0, 0,
					 0, 0, 0, 0};
					 
	char buf36_48[36] = {0, 0, 0, 0, 0, 0, 0, 0,
                         0x3c, 0x1f, 0x83, 0xcb, 0x3c, 0x97, 0x64, 0x58,
				         0xbd, 0x30, 0xab, 0x95, 0xbb, 0xab, 0x4e, 0x1b,
				         0x3d, 0xd5, 0xa9, 0x15, 0xbc, 0xc1, 0x5e, 0x0b,
				         0xbd, 0x0f, 0xcf, 0xf3};
					  
	char buf36_50[36] = {0, 0, 0, 0, 0, 0, 0, 0,
		              0xBA, 0x1B, 0x67, 0x65, 0x3B, 0x6D, 0x6D, 0xA6,
		              0xBB, 0x2D, 0xB2, 0x0A, 0x3D, 0x3E, 0x82, 0x40,
		              0xBD, 0x60, 0x3C, 0xC6, 0x3B, 0x76, 0x59, 0x7B,
		              0xBC, 0x2A, 0xB8, 0xFA};

	char buf36_52[36] = {0, 0, 0, 0, 0, 0, 0, 0,
		              0x3D, 0x3C, 0xCD, 0x00, 0x3D, 0xBE, 0xB7, 0x67,
		              0xBE, 0x1A, 0x35, 0xE7, 0xBD, 0x2C, 0x49, 0xFD,
		              0x3C, 0xE7, 0xAD, 0xD4, 0xBC, 0x01, 0x54, 0x20,
		              0x3A, 0xD9, 0x65, 0xE8};

	char buf36_54[36] = {0x3E, 0xE2, 0x1B, 0x09, 0x3E, 0x1F, 0x28, 0x45,
		              0x3D, 0x67, 0x98, 0x10, 0xB8, 0xC8, 0x9C, 0xC9,
		              0x39, 0x13, 0x0E, 0xC5, 0xB8, 0xA8, 0x99, 0xC9,
		              0xB9, 0x4F, 0xD3, 0xE6, 0x39, 0x5B, 0xAD, 0x0F,
		              0xB6, 0xC7, 0x92, 0x2A};

	char buf36_56[36] = {0xBE, 0x38, 0x8E, 0x8A, 0x3F, 0x67, 0x1B, 0xB7,
		              0x3C, 0xD3, 0x58, 0x39, 0xB9, 0xAF, 0x9C, 0xF8,
		              0x38, 0xD5, 0xCB, 0x7C, 0xB7, 0xED, 0x0F, 0xFA,
		              0xB9, 0x16, 0xFA, 0x7C, 0x39, 0xEB, 0x53, 0x63,
		              0xB8, 0x22, 0xE0, 0x13};

	char buf36_58[36] = {0xBC, 0x33, 0xF2, 0xE4, 0x3C, 0xBB, 0x2F, 0xA9,
					  0x3F, 0x5C, 0xC8, 0xAA, 0xB9, 0x4B, 0xC1, 0x78,
					  0x38, 0x8D, 0xB7, 0xBC, 0xB8, 0x7D, 0x18, 0x44,
					  0x36, 0xCA, 0xDF, 0xB5, 0x39, 0x19, 0x4A, 0x45,
					  0xB8, 0x6F, 0x89, 0x2B};
	
	char buf4_60[4] = {0xc8, 0x02, 0xab, 0x02};
	char buf4_61[4] = {0x02, 0x00, 0x01, 0x30};
	char buf4_62[4] = {0x00, 0x00, 0x00, 0x00};
	char buf4_63[4] = {0x00, 0x01, 0x30, 0x30};
					 
	char buf36b[36] = {0x00, 0x00, 0x00, 0x00, 0x3C, 0xBB, 0x2F, 0xA9,
	 	                0x3F, 0x5C, 0xC8, 0xAA, 0xB9, 0x4B, 0xC1, 0x78,
	                    0x38, 0x8D, 0xB7, 0xBC, 0xB8, 0x7D, 0x18, 0x44,
	                    0x36, 0xCA, 0xDF, 0xB5, 0x39, 0x19, 0x4A, 0x45,
	                    0xB8, 0x6F, 0x89, 0x2B};
	                    
	char buf36_64[36] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	 	                 0x3E, 0x1F, 0xE0, 0xCE, 0x3D, 0xAE, 0x02, 0x96,
	 	                 0xBE, 0x9F, 0x94, 0x35, 0xBE, 0x29, 0xA7, 0xF2,
	 	                 0x3F, 0xB4, 0x25, 0xC9, 0xBE, 0x15, 0x5C, 0xC5,
	 	                 0xBE, 0xAB, 0xC, 0xFF};  
 
 	char buf36_66[36] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	 	                 0xBD, 0x6D, 0x0F, 0xFB, 0x3D, 0x84, 0xA2, 0x96,
	 	                 0xBC, 0xD6, 0x13, 0x40, 0x3E, 0x9F, 0x9F, 0xF8,
	 	                 0xBC, 0xB1, 0xF9, 0xCA, 0xBD, 0x21, 0x21, 0x92,
	 	                 0xBE, 0x40, 0x13, 0xCA};  
	
	char buf36_68[36] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	 	                 0x3E, 0xDC, 0x0F, 0x38, 0x3F, 0x3B, 0x80, 0x27,
	 	                 0xBF, 0xA4, 0xD6, 0x17, 0xBE, 0x8A, 0x60, 0xBA,
	 	                 0x3D, 0xE4, 0xFE, 0x1D, 0xBD, 0x13, 0x37, 0x56,
	 	                 0x3C, 0xDA, 0x8A, 0x49};
	
	char buf36_70[36] = {0x3f, 0x80, 0, 0, 0, 0, 0, 0,
					     0, 0, 0, 0, 0, 0, 0, 0,
					     0, 0, 0, 0, 0, 0, 0, 0,
					     0, 0, 0, 0, 0, 0, 0, 0,
					     0, 0, 0, 0};

	char buf36c[36] = {0, 0, 0, 0, 0, 0, 0, 0,
					 0, 0, 0, 0, 0, 0, 0, 0,
					 0, 0, 0, 0, 0, 0, 0, 0,
					 0, 0, 0, 0, 0, 0, 0, 0,
					 0, 0, 0, 0};
	
	char buf36_72[36] = {0, 0, 0, 0, 0x3f, 0x80, 0, 0,
					 0, 0, 0, 0, 0, 0, 0, 0,
					 0, 0, 0, 0, 0, 0, 0, 0,
					 0, 0, 0, 0, 0, 0, 0, 0,
					 0, 0, 0, 0}; 
					 	    
	char buf36_74[36] = {0, 0, 0, 0, 0, 0, 0, 0,
					 0x3f, 0x80, 0, 0, 0, 0, 0, 0,
					 0, 0, 0, 0, 0, 0, 0, 0,
					 0, 0, 0, 0, 0, 0, 0, 0,
					 0, 0, 0, 0};
	
	char buf8b[8] = {0, 0, 0, 0, 0xc8, 0x02, 0xab, 0x02};
	char buf8_76[8] = {0x30, 0x30, 0x34, 0x30, 0x37, 0x39, 0x31, 0x32};

	char buf8c[8] = {0x0c, 0xcb, 0xa9, 0x02, 0x08, 0, 0, 0};
	
	char buf8_1786[8] = {0, 0, 0, 0, 0, 0, 0, 0};
	char buf8_tmp[8];

	char buf8_1788[8] = {0x70, 0x05, 0x91, 0x7C, 0x3C, 0x0F, 0x00, 0x00};
	char buf8_1790[8] = {0xFD, 0x12, 0x00, 0xA8, 0x9A, 0x83, 0x7C, 0xF8};
	char buf8_1792[8] = {0x25, 0x80, 0x7C, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0};
	char buf8_1794[8] = {0x25, 0x80, 0x7C, 0x32, 0x25, 0x80, 0x7C, 0xA0};
	char buf8_1796[8] = {0x17, 0x99, 0x02, 0xC4, 0xFB, 0x12, 0x00, 0xF3};
	char buf8_1798[8] = {0x3A, 0xA9, 0x02, 0x58, 0x01, 0x00, 0x00, 0x7C};
	char buf8_1800[8] = {0xFB, 0x12, 0x00, 0xF4, 0xFB, 0x12, 0x00, 0x01};
	char buf8_1802[8] = {0x00, 0x00, 0x00, 0xA0, 0x17, 0x99, 0x02, 0xA0};
	char buf8_1804[8] = {0x17, 0x99, 0x02, 0xE4, 0xFB, 0x12, 0x00, 0xF3};
	char buf8_1806[8] = {0x3A, 0xA9, 0x02, 0x58, 0x01, 0x00, 0x00, 0x00};
     
        //printf("init_setup: Endpoint address: %x\n", ep);

	printf("Doing initial set of device -- echo packets.\n");
	
	printf("Doing packet 8\n");
	
	// packet 8
	usb_control_msg(hdev, USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_ENDPOINT_OUT,
    	0xc7, 0, 0, NULL, 0, timeout);

	// These are DEVICE->HOST packets
	printf("Doing packets 10-47\n");
	printf("These packets change on every call\n");
	for (i = 10; i < 47; i += 2) {
	    // packet 10, 11, 12, 13
	    usb_control_msg(hdev, USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_ENDPOINT_IN,
	    	0xc6, 0, 0, buf8, 8, timeout);
		
		//printfbuf(buf8, 8, "buf8");
		
		checkPacket10_47(buf8);
		
	}
	
	printf("Doing packets 48 - 76\n");
	usb_control_msg(hdev, USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_ENDPOINT_IN,
    	0xc4, 0x10, 0x24, buf36, 0x24, timeout);
	assertSameBuf(buf36, buf36_48, 36, "Packet 48");
	
	usb_control_msg(hdev, USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_ENDPOINT_IN,
		0xc4, 0x34, 0x24, buf36, 0x24, timeout);
	assertSameBuf(buf36, buf36_50, 36, "Packet 50");
	
	usb_control_msg(hdev, USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_ENDPOINT_IN,
		0xc4, 0x58, 0x24, buf36, 0x24, timeout);
	assertSameBuf(buf36, buf36_52, 36, "Packet 52");
	
	usb_control_msg(hdev, USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_ENDPOINT_IN,
		0xc4, 0x80, 0x24, buf36, 0x24, timeout);
	assertSameBuf(buf36, buf36_54, 36, "Packet 54");
		
	usb_control_msg(hdev, USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_ENDPOINT_IN,
		0xc4, 0xa4, 0x24, buf36, 0x24, timeout);
	assertSameBuf(buf36, buf36_56, 36, "Packet 56");
	
	usb_control_msg(hdev, USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_ENDPOINT_IN,
		0xc4, 0xc8, 0x24, buf36, 0x24, timeout);
	assertSameBuf(buf36, buf36_58, 36, "Packet 58");
	
	usb_control_msg(hdev, USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_ENDPOINT_IN,
		0xc4, 0x5, 0x4, buf4_60, 0x4, timeout);
	assertSameBuf(buf4_60, buf4_61, 4, "Packet 60");
	
	usb_control_msg(hdev, USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_ENDPOINT_IN,
		0xc4, 0x6, 0x4, buf4_62, 0x4, timeout);
	assertSameBuf(buf4_62, buf4_63, 4, "Packet 62");
	
	usb_control_msg(hdev, USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_ENDPOINT_IN,
		0xc4, 0x100, 0x24, buf36b, 0x24, timeout);
	assertSameBuf(buf36b, buf36_64, 36, "Packet 64");
	
	usb_control_msg(hdev, USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_ENDPOINT_IN,
		0xc4, 0x124, 0x24, buf36b, 0x24, timeout);
	assertSameBuf(buf36b, buf36_66, 36, "Packet 66");
	
	usb_control_msg(hdev, USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_ENDPOINT_IN,
		0xc4, 0x148, 0x24, buf36b, 0x24, timeout);
	assertSameBuf(buf36b, buf36_68, 36, "Packet 68");
	
	usb_control_msg(hdev, USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_ENDPOINT_IN,
		0xc4, 0x180, 0x24, buf36b, 0x24, timeout);
	assertSameBuf(buf36b, buf36_70, 36, "Packet 70");
	
	usb_control_msg(hdev, USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_ENDPOINT_IN,
		0xc4, 0x1a4, 0x24, buf36c, 0x24, timeout);
	assertSameBuf(buf36c, buf36_72, 36, "Packet 72");
	
	usb_control_msg(hdev, USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_ENDPOINT_IN,
		0xc4, 0x1c8, 0x24, buf36c, 0x24, timeout);
	assertSameBuf(buf36c, buf36_74, 36, "Packet 74");
	
	usb_control_msg(hdev, USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_ENDPOINT_IN,
		0xc4, 0x8, 0x8, buf8b, 0x8, timeout);
	assertSameBuf(buf8b, buf8_76, 8, "Packet 76");
	
	// These are HOST->DEVICE packets
	printf("Doing packets 78 - 1783\n");
        count = 0;
	for (i = 78; i < 1783; i += 2) {
		usb_control_msg(hdev, USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_ENDPOINT_OUT,
			0xc0, 0x0, 0x0, setupbuf + count, 0x8, timeout);
                //printfbuf(setupbuf+count, 8, "Setupbuf");

		//assertSameBuf(buf8b, buf8_76, 8, "Packet 76");
                count += 8;
	}
	
	printf("Doing packet 1784\n");
	usb_control_msg(hdev, USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_ENDPOINT_IN,
		0xc6, 0x0, 0x0, buf8c, 0x8, timeout);
	checkPacket10_47(buf8c);
	printfbuf(buf8c, 8, "P1785");
 
	printf("Doing packet 1786\n");
	usb_control_msg(hdev, USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_ENDPOINT_OUT,
		0xc1, 0x100, 0xf4, buf8_1786, 0x8, timeout);

	printf("Doing packets 1788 - 1807\n");
	// These packets differ on every call
		
	// packet 1788
       
	my_usb_interrupt_write(hdev, ep, buf8_1788, 8, timeout);
	my_usb_interrupt_read(hdev, ep, buf8_tmp, 8, timeout);
	printfbuf(buf8_tmp, 8, "P1789");

	///************* packet 1789 is not what is expected
	
	// packet 1790
	my_usb_interrupt_write(hdev, ep, buf8_1790, 8, timeout);
	my_usb_interrupt_read(hdev, ep, buf8_tmp, 8, timeout);
	
	// packet 1792
	my_usb_interrupt_write(hdev, ep, buf8_1792, 8, timeout);
	my_usb_interrupt_read(hdev, ep, buf8_tmp, 8, timeout);
	
	// packet 1794
	my_usb_interrupt_write(hdev, ep, buf8_1794, 8, timeout);
	my_usb_interrupt_read(hdev, ep, buf8_tmp, 8, timeout);
	
	// packet 1796
	my_usb_interrupt_write(hdev, ep, buf8_1796, 8, timeout);
	my_usb_interrupt_read(hdev, ep, buf8_tmp, 8, timeout);
	
	// packet 1798
	my_usb_interrupt_write(hdev, ep, buf8_1798, 8, timeout);
	my_usb_interrupt_read(hdev, ep, buf8_tmp, 8, timeout);

	// packet 1800
	my_usb_interrupt_write(hdev, ep, buf8_1800, 8, timeout);
	my_usb_interrupt_read(hdev, ep, buf8_tmp, 8, timeout);
	
	// packet 1802
	my_usb_interrupt_write(hdev, ep, buf8_1802, 8, timeout);
	my_usb_interrupt_read(hdev, ep, buf8_tmp, 8, timeout);
	
	// packet 1804
	my_usb_interrupt_write(hdev, ep, buf8_1804, 8, timeout);
	my_usb_interrupt_read(hdev, ep, buf8_tmp, 8, timeout);
	
	// packet 1806
	my_usb_interrupt_write(hdev, ep, buf8_1806, 8, timeout);
	my_usb_interrupt_read(hdev, ep, buf8_tmp, 8, timeout);
	
}

void setup_measurement(usb_dev_handle * hdev)
{
    struct usb_device *dev = usb_device(hdev);
    int ep =
        dev->config->interface->altsetting->endpoint->bEndpointAddress;
    int timeout = 1000;

    char buf8_1810[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
    char buf8_1811[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
    char buf8_1812[8] = { 0, 0, 0, 0x64, 0x15, 0xb3, 0x7f, 0xff };
    char buf8_1814[8] = { 0xf8, 0xe0, 0x12, 0, 0xe8, 0x2d, 0xa9, 0x02 };
    char buf8_1815[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
    
    printf("Setting up measurement.\n");
    
    // packet 1808
    usb_control_msg(hdev, USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_ENDPOINT_OUT,
    	0xc2, 0x8b02, 0xa, NULL, 0, timeout);

    // packet 1810 
    my_usb_interrupt_write(hdev, ep, buf8_1810, 8, timeout);

    // packet 1811
    my_usb_interrupt_read(hdev, ep, buf8_1811, 8, timeout);
    printfbuf(buf8_1811, 8, "P1811");

    // packet 1812
    usb_control_msg(hdev, USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_ENDPOINT_OUT,
    	0xc3, 0xdc32, 0xd5, buf8_1812, 8, timeout);

    //packet 1814
    my_usb_interrupt_write(hdev, ep, buf8_1814, 8, timeout);
    //packet 1815
    my_usb_interrupt_read(hdev, ep, buf8_1815, 8, timeout);
	printfbuf(buf8_1815, 8, "P1815");
}

void do_measurement(usb_dev_handle * hdev)
{
	struct usb_device *dev = usb_device(hdev);
    int ep =
        dev->config->interface->altsetting->endpoint->bEndpointAddress;
        
	char buf8_1818[8] = { 0xd6, 0xe5, 0x12, 0, 0x0b, 0, 0, 0 };
	char buf8_1819[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
	char buf8_1820[8] = { 0, 0x04, 0x07, 0, 0x15, 0xb3, 0x7f, 0xff };
	char buf8_1822[8] = { 0x24, 0xe5, 0x12, 0, 0xe8, 0x2d, 0xa9, 0x02 };
	char buf8_1823[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
	char buf8_1824[8] = { 0xE6, 0x12, 0x00, 0xA8, 0x9A, 0x83, 0x7C, 0xF8 };
	char buf8_1825[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
	char buf8_1826[8] = { 0x25, 0x80, 0x7C, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0};
	char buf8_1827[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
	char buf8_1828[8] = { 0x25, 0x80, 0x7C, 0x32, 0x25, 0x80, 0x7C, 0xA0};
	char buf8_1829[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
	char buf8_1830[8] = { 0x17, 0x99, 0x02, 0xEC, 0xE4, 0x12, 0x00, 0xDB};
	char buf8_1831[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
	char buf8_1832[8] = { 0x3C, 0xA9, 0x02, 0x58, 0x01, 0x00, 0x00, 0xC8};
	char buf8_1833[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
	char buf8_1834[8] = { 0xE4, 0x12, 0x00, 0x18, 0xE5, 0x12, 0x00, 0x01};
	char buf8_1835[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
	char buf8_1836[8] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA4};
	char buf8_1837[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
	char buf8_1838[8] = { 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08};
	char buf8_1839[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
	char buf8_1840[8] = { 0x00, 0x00, 0x00, 0xF0, 0xE4, 0x12, 0x00, 0x9B};
	char buf8_1841[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
	
    int timeout = 1000;
    
    printf("Doing measurement.\n");
    
	// packet 1816	
    usb_control_msg(hdev, USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_ENDPOINT_OUT,
    	0xc2, 0xe207, 0x3a, NULL, 0, timeout);

	//packet 1818
    my_usb_interrupt_write(hdev, ep, buf8_1818, 8, timeout);
    
    //1819
    my_usb_interrupt_read(hdev, ep, buf8_1819, 8, timeout);
    
    printfbuf(buf8_1819, 8, "P1819");
    
    //1820
    usb_control_msg(hdev, USB_RECIP_DEVICE | USB_TYPE_VENDOR | USB_ENDPOINT_OUT,
    	0xc1, 0x4b4c, 0x40, buf8_1820, 8, timeout);

	printf("Starting measurement...\n");
	//1822
    my_usb_interrupt_write(hdev, ep, buf8_1822, 8, timeout);
    usleep(5000000);
    
    //1823
    my_usb_interrupt_read(hdev, ep, buf8_1823, 8, timeout);
	printfbuf(buf8_1823, 8, "P1823");
	
	//1824
    my_usb_interrupt_write(hdev, ep, buf8_1824, 8, timeout);
    
    //1825
    my_usb_interrupt_read(hdev, ep, buf8_1825, 8, timeout);
	printfbuf(buf8_1825, 8, "P1825");
	
	//1826
    my_usb_interrupt_write(hdev, ep, buf8_1826, 8, timeout);
    
    //1827
    my_usb_interrupt_read(hdev, ep, buf8_1827, 8, timeout);
	printfbuf(buf8_1827, 8, "P1827");
	
	//1828
    my_usb_interrupt_write(hdev, ep, buf8_1828, 8, timeout);
    
    //1829
    my_usb_interrupt_read(hdev, ep, buf8_1829, 8, timeout);
	printfbuf(buf8_1829, 8, "P1829");
	
	//1830
    my_usb_interrupt_write(hdev, ep, buf8_1830, 8, timeout);
    
    //1831
    my_usb_interrupt_read(hdev, ep, buf8_1831, 8, timeout);
	printfbuf(buf8_1831, 8, "P1831");
	
	//1832
    my_usb_interrupt_write(hdev, ep, buf8_1832, 8, timeout);
    
    //1833
    my_usb_interrupt_read(hdev, ep, buf8_1833, 8, timeout);
	printfbuf(buf8_1825, 8, "P1833");
	    
    //1834
    my_usb_interrupt_write(hdev, ep, buf8_1834, 8, timeout);
    
    //1835
    my_usb_interrupt_read(hdev, ep, buf8_1835, 8, timeout);
    printfbuf(buf8_1825, 8, "P1835");
    
    //1836
    my_usb_interrupt_write(hdev, ep, buf8_1836, 8, timeout);
    
    //1837
    my_usb_interrupt_read(hdev, ep, buf8_1837, 8, timeout);
    printfbuf(buf8_1825, 8, "P1837");
    
    //1838
    my_usb_interrupt_write(hdev, ep, buf8_1838, 8, timeout);
    
    //1839
    my_usb_interrupt_read(hdev, ep, buf8_1839, 8, timeout);
    printfbuf(buf8_1825, 8, "P1839");
    
    //1840
    my_usb_interrupt_write(hdev, ep, buf8_1840, 8, timeout);
    
    //1841
    my_usb_interrupt_read(hdev, ep, buf8_1841, 8, timeout);
	printfbuf(buf8_1825, 8, "P1841");
}


int hexToDec1(char c)
{
    if (c >= '0' && c <= '9')
        return c - '0';
    else if (c >= 'a' && c <= 'f')
        return c - 'a' + 10;
    else if (c >= 'A' && c <= 'F')
        return c - 'A' + 10;
    else
        assert(0);

    return -1;
}

// Reads two characters from s and convert to decimal
int hexToDec(char * s)
{
    return hexToDec1(s[0]) * 16 + hexToDec1(s[1]);
}

char * readSetupInfo(const char * fn)
{
    FILE * file = fopen(fn, "r");
    char aline[100];
    char * buf = (char *) malloc(6824);
    int i, j;

    printf("Reading interrupt transfer setup data.\n");

    for (i = 0; i < 853; i++) {
        fgets(aline, 100, file);
        //printf("%s\n", aline);

        for(j = 0; j < 8; j++) {
            buf[i*8 + j] = hexToDec(aline + j*3);
            //printf("%x ", (unsigned char) (buf[i*8 + j]));
        }
        //printf("\n");
    }

    fclose(file);

    return buf;
    
}

int main(int argc, char *argv[])
{
    struct usb_bus *bus;
    struct usb_device *dev;
    usb_dev_handle *hdev;
    int found = 0;
    int ep;
    char * fn;
    char * setupbuf;
     
    if (argc != 2) {
        printf("Usage: %s setupbin.txt\n", argv[0]);
        return -1;
    }

    fn = argv[1];

    setupbuf = readSetupInfo(fn);

    usb_init();

    usb_find_busses();
    usb_find_devices();

    for (bus = usb_busses; bus; bus = bus->next) {
        for (dev = bus->devices; dev; dev = dev->next)
            if (dev->descriptor.bDeviceClass == USB_CLASS_VENDOR_SPEC &&
                dev->descriptor.bDeviceSubClass == 0x00 &&
                dev->descriptor.bDeviceProtocol == 0x00 &&
                dev->descriptor.idVendor == 0x085c &&
                dev->descriptor.idProduct == 0x0200 &&
                dev->descriptor.bcdDevice == 0x0100 &&
                dev->descriptor.iManufacturer == 0x01 &&
                dev->descriptor.iProduct == 0x02 &&
                dev->descriptor.bNumConfigurations == 0x01) {

                found = 1;
                break;

            }

        if (found)
            break;
    }

    if (!found) {
        printf("Spyder not found.\n");
        return -1;
    }

    usb_set_debug(2);

    printf("Found the Spyder.\n");

    hdev = usb_open(dev);

    if (!hdev) {
        printf("Failed to open the device.\n");
        return -1;
    }

    printf("Successfully opened the device. %x\n", (unsigned int) hdev);
    ep = dev->config->interface->altsetting->endpoint->bEndpointAddress;
    printf("End point address: 0x%x\n", ep);

    //printf("Resetting endpoint\n");
    //usb_clear_halt(hdev, ep);

    // packet 6
    printf("Setting configuration to 1.\n");
    usb_set_configuration(hdev, 1);

    printf("Claiming interface 0.\n");
    usb_claim_interface(hdev, 0);

    printf("Setting alternate interface to 0.\n");
    usb_set_altinterface(hdev, 0);

	
    // initial setup
    init_setup(hdev, setupbuf);

    setup_measurement(hdev);

	do_measurement(hdev);
    
    printf("Releasing interface 0.\n");
	usb_release_interface(hdev, 0);

	
    printf("Closing device.\n");
    
    
    if (hdev)
        usb_close(hdev);

    free(setupbuf);

    return 0;
}

