]> rtime.felk.cvut.cz Git - fpga/lx-cpu1/lx-rocon.git/blob - host/rocon-send_cmd/rocon-send_cmd.c
A simple host application to talk to ROCON through USB
[fpga/lx-cpu1/lx-rocon.git] / host / rocon-send_cmd / rocon-send_cmd.c
1 #include <usb.h>
2 #include <stdio.h>
3 #include <string.h>
4
5 /* ROCON setup */
6 #define ROCON_VID           0x1669
7 #define ROCON_PID           0x1023
8 #define ROCON_CONFIGURATION 1
9 #define ROCON_INTFERFACE    0
10
11 /* Endpoints */
12 #define EP_IN               0x81
13 #define EP_OUT              0x01
14
15 #define BUFFER_SIZE         1024
16
17 int opening_rocon = 0;
18
19 usb_dev_handle *open_dev(void)
20 {
21         struct usb_bus *bus;
22         struct usb_device *dev;
23
24         for (bus = usb_get_busses(); bus; bus = bus->next)
25         {
26                 for (dev = bus->devices; dev; dev = dev->next)
27                 {
28                         if (dev->descriptor.idVendor == ROCON_VID && dev->descriptor.idProduct == ROCON_PID)
29                         {
30                                 opening_rocon = 1;
31                                 return usb_open(dev);
32                         }
33                 }
34         }
35         return NULL;
36 }
37
38 int main(int argc, char** argv)
39 {
40         usb_dev_handle *dev = NULL;
41         char buffer[BUFFER_SIZE];
42         int ret;
43
44         /* Init */
45         usb_init();
46         usb_find_busses();
47         usb_find_devices();
48
49         /* Open device */
50         if (!(dev = open_dev()))
51         {
52                 if (opening_rocon)
53                         printf("Error opening ROCON:\n%s\n", usb_strerror());
54                 else
55                         printf("ROCON not connected!\n");
56                 
57                 return 1;
58         }
59
60         /* Set configuration */
61         if (usb_set_configuration(dev, ROCON_CONFIGURATION) < 0)
62         {
63                 printf("Error setting configuration #%d:\n%s\n", ROCON_CONFIGURATION, usb_strerror());
64                 usb_close(dev);
65                 return 1;
66         }
67
68         /* Set interface */
69         if (usb_claim_interface(dev, ROCON_INTFERFACE) < 0)
70         {
71                 printf("Error claiming interface #%d:\n%s\n", ROCON_INTFERFACE, usb_strerror());
72                 usb_close(dev);
73                 return 1;
74         }
75
76         /* Write command */
77         snprintf(buffer, sizeof(buffer), "%s\n", argv[1]);
78         ret = usb_bulk_write(dev, EP_OUT, buffer, strlen(buffer), 5000);
79         if (ret < 0)
80         {
81                 printf("Error sending command %s:\n%s\n", argv[1], usb_strerror());
82                 usb_release_interface(dev, 0);
83                 usb_close(dev);
84                 return 1;
85         }
86
87         /* Read response */
88         ret = usb_bulk_read(dev, EP_IN, buffer, sizeof(buffer), 5000);
89         if (ret < 0)
90                 printf("Error reading response:\n%s\n", usb_strerror());
91
92         buffer[ret] = '\0';
93         printf(buffer);
94
95         usb_release_interface(dev, 0);
96         usb_close(dev);
97         return 0;
98 }
99