| 1 | #include <stdio.h>
|
|---|
| 2 | #include <string.h>
|
|---|
| 3 | #include <stdlib.h>
|
|---|
| 4 |
|
|---|
| 5 | #include <libusb-1.0/libusb.h>
|
|---|
| 6 |
|
|---|
| 7 | int handle_dev(libusb_device *dev, uint8_t ep_out, uint8_t ep_in) {
|
|---|
| 8 | libusb_device_handle *handle;
|
|---|
| 9 | uint8_t data_in[16*1024 + 1];
|
|---|
| 10 | int size_in;
|
|---|
| 11 | uint8_t data_out[] = {0x55, 0x53, 0x42, 0x43, 0x01, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x80, 0x00, 0x06, 0x12, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
|
|---|
| 12 | int r = 0;
|
|---|
| 13 |
|
|---|
| 14 | libusb_open(dev, &handle);
|
|---|
| 15 |
|
|---|
| 16 | printf("Send data\n");
|
|---|
| 17 | r = libusb_bulk_transfer(handle,
|
|---|
| 18 | ep_out, data_out, sizeof(data_out),
|
|---|
| 19 | NULL, 3*1000);
|
|---|
| 20 |
|
|---|
| 21 | if (r < 0)
|
|---|
| 22 | goto exit;
|
|---|
| 23 |
|
|---|
| 24 | size_in = sizeof(data_in);
|
|---|
| 25 | printf("Get data\n");
|
|---|
| 26 | r = libusb_bulk_transfer(handle,
|
|---|
| 27 | ep_in, data_in, size_in,
|
|---|
| 28 | &size_in, 3*1000);
|
|---|
| 29 |
|
|---|
| 30 | if (r < 0)
|
|---|
| 31 | goto exit;
|
|---|
| 32 |
|
|---|
| 33 | size_in = sizeof(data_in);
|
|---|
| 34 | printf("Get data\n");
|
|---|
| 35 | r = libusb_bulk_transfer(handle,
|
|---|
| 36 | ep_in, data_in, size_in,
|
|---|
| 37 | &size_in, 3*1000);
|
|---|
| 38 |
|
|---|
| 39 | if (r < 0)
|
|---|
| 40 | goto exit;
|
|---|
| 41 |
|
|---|
| 42 | exit:
|
|---|
| 43 | libusb_close(handle);
|
|---|
| 44 |
|
|---|
| 45 | return r;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | static libusb_device* get_device(libusb_device **devs, int idVendor, int idProduct)
|
|---|
| 49 | {
|
|---|
| 50 | libusb_device *dev;
|
|---|
| 51 | int i = 0, j = 0;
|
|---|
| 52 | uint8_t path[8];
|
|---|
| 53 |
|
|---|
| 54 | while ((dev = devs[i++]) != NULL) {
|
|---|
| 55 | struct libusb_device_descriptor desc;
|
|---|
| 56 | int r = libusb_get_device_descriptor(dev, &desc);
|
|---|
| 57 | if (r < 0) {
|
|---|
| 58 | fprintf(stderr, "failed to get device descriptor");
|
|---|
| 59 | return NULL;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | if (desc.idVendor == idVendor && desc.idProduct == idProduct)
|
|---|
| 63 | return dev;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | return NULL;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | int main(int argc, char* argv[]) {
|
|---|
| 70 | libusb_device **devs;
|
|---|
| 71 | libusb_device *dev;
|
|---|
| 72 | ssize_t cnt;
|
|---|
| 73 | uint8_t ep_out = 0x02;
|
|---|
| 74 | uint8_t ep_in = 0x81;
|
|---|
| 75 |
|
|---|
| 76 | int r = 0;
|
|---|
| 77 |
|
|---|
| 78 | if (argc < 3) {
|
|---|
| 79 | printf("idVend or idProd doesn't specified\n");
|
|---|
| 80 | r = 1;
|
|---|
| 81 | goto exit;
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | if (argc == 5) {
|
|---|
| 85 | ep_out = strtol(argv[3], NULL, 16);
|
|---|
| 86 | ep_in = strtol(argv[4], NULL, 16);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | printf("Libusb init\n");
|
|---|
| 90 | r = libusb_init(NULL);
|
|---|
| 91 | if (r < 0)
|
|---|
| 92 | goto exit;
|
|---|
| 93 |
|
|---|
| 94 | printf("Get device list\n");
|
|---|
| 95 | cnt = r = libusb_get_device_list(NULL, &devs);
|
|---|
| 96 | printf("Number of devices: %ld\n", cnt);
|
|---|
| 97 | if (cnt < 0) {
|
|---|
| 98 | goto free_libusb;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | dev = get_device(devs, strtol(argv[1], NULL, 16), strtol(argv[2], NULL, 16));
|
|---|
| 102 | if (dev == NULL) {
|
|---|
| 103 | r = 1;
|
|---|
| 104 | goto free_libusb;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | r = handle_dev(dev, ep_out, ep_in);
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 | free_libusb:
|
|---|
| 111 | printf("Libusb exit\n");
|
|---|
| 112 | libusb_exit(NULL);
|
|---|
| 113 |
|
|---|
| 114 | exit:
|
|---|
| 115 | if (r != 0)
|
|---|
| 116 | printf("Error\n");
|
|---|
| 117 | else
|
|---|
| 118 | printf("Success\n");
|
|---|
| 119 |
|
|---|
| 120 | return r;
|
|---|
| 121 | }
|
|---|