8 #include <libusb-1.0/libusb.h>
10 #include "../firmware/interface.h"
12 struct libusb_context *usb_ctxt;
13 struct libusb_device_handle *devh;
15 static libusb_device *find_device(void)
17 libusb_device **devlist;
18 ssize_t devn = libusb_get_device_list(usb_ctxt, &devlist);
21 fprintf(stderr, "Cannot enumerate USB devices: error %d\n", (int) devn);
25 for (ssize_t i=0; i<devn; i++)
27 struct libusb_device_descriptor desc;
28 libusb_device *dev = devlist[i];
29 if (!libusb_get_device_descriptor(dev, &desc))
31 if (desc.idVendor == DMX_USB_VENDOR && desc.idProduct == DMX_USB_PRODUCT)
33 printf("Found device at usb%d.%d\n", libusb_get_bus_number(dev), libusb_get_device_address(dev));
34 // FIXME: Free device list
40 libusb_free_device_list(devlist, 1);
41 fprintf(stderr, "Device not found\n");
45 int main(int argc, char **argv)
48 die("Usage: test <val1> <val2> <val3> <val4>");
49 int a1 = atoi(argv[1]);
50 int a2 = atoi(argv[2]);
51 int a3 = atoi(argv[3]);
52 int a4 = atoi(argv[4]);
55 if (err = libusb_init(&usb_ctxt))
56 die("Cannot initialize libusb: error %d", err);
58 libusb_device *dev = find_device();
60 if (err = libusb_open(dev, &devh))
61 die("Cannot open device: error %d", err);
62 // libusb_reset_device(devh);
63 if (err = libusb_claim_interface(devh, 0))
64 die("Cannot claim interface: error %d", err);
66 byte packet[5] = { 0, a1, a2, a3, a4 };
67 int len = sizeof(packet);
69 if (err = libusb_bulk_transfer(devh, 0x01, packet, len, &transferred, 1000))
70 die("Transfer failed: error %d\n", err);
71 if (transferred != len)
72 die("Short transfer: %d out of %d bytes", transferred, len);