Subversion Repositories group.electronics

Rev

Rev 89 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
89 pfowler 1
#include <stdio.h>
2
#include <stdlib.h>
3
#include <string.h>
4
 
5
/* this is libusb, see http://libusb.sourceforge.net/ */
6
#include <usb.h>
7
 
8
// Same as in main.c
9
#define USB_LED_OFF 100
10
#define USB_LED_ON  101
11
 
12
/* Used to get descriptor strings for device identification */
13
static int usbGetDescriptorString(usb_dev_handle *dev, int index, int langid, 
14
                                  char *buf, int buflen) {
15
    char buffer[256];
16
    int rval, i;
17
 
18
	// make standard request GET_DESCRIPTOR, type string and given index 
19
    // (e.g. dev->iProduct)
20
	rval = usb_control_msg(dev, 
21
        USB_TYPE_STANDARD | USB_RECIP_DEVICE | USB_ENDPOINT_IN, 
22
        USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) + index, langid, 
23
        buffer, sizeof(buffer), 1000);
24
 
25
    if(rval < 0) // error
26
		return rval;
27
 
28
    // rval should be bytes read, but buffer[0] contains the actual response size
29
	if((unsigned char)buffer[0] < rval)
30
		rval = (unsigned char)buffer[0]; // string is shorter than bytes read
31
 
32
	if(buffer[1] != USB_DT_STRING) // second byte is the data type
33
		return 0; // invalid return type
34
 
35
	// we're dealing with UTF-16LE here so actual chars is half of rval,
36
	// and index 0 doesn't count
37
	rval /= 2;
38
 
39
	/* lossy conversion to ISO Latin1 */
40
	for(i = 1; i < rval && i < buflen; i++) {
41
		if(buffer[2 * i + 1] == 0)
42
			buf[i-1] = buffer[2 * i];
43
		else
44
			buf[i-1] = '?'; /* outside of ISO Latin1 range */
45
	}
46
	buf[i-1] = 0;
47
 
48
	return i-1;
49
}
50
 
51
static usb_dev_handle * usbOpenDevice(int vendor, char *vendorName, 
52
                                      int product, char *productName) {
53
	struct usb_bus *bus;
54
	struct usb_device *dev;
55
	char devVendor[256], devProduct[256];
56
 
57
	usb_dev_handle * handle = NULL;
58
 
59
	usb_init();
60
	usb_find_busses();
61
	usb_find_devices();
62
 
63
	for(bus=usb_get_busses(); bus; bus=bus->next) {
64
		for(dev=bus->devices; dev; dev=dev->next) {			
65
 
66
			if(dev->descriptor.idVendor != vendor ||
67
               			dev->descriptor.idProduct != product)
68
                		continue;
69
 
70
		         /* we need to open the device in order to query strings */
71
            		if(!(handle = usb_open(dev))) {
72
                		fprintf(stderr, "Warning: cannot open USB device: %s\n",
73
                    		usb_strerror());
74
                		continue;
75
            		}
76
 
77
			/* get vendor name */
78
            		if(usbGetDescriptorString(handle, dev->descriptor.iManufacturer, 0x0409, devVendor, sizeof(devVendor)) < 0) {
79
                		fprintf(stderr, "Warning: cannot query manufacturer for device: %s\n", usb_strerror());
80
                		usb_close(handle);
81
                		continue;
82
            		}
83
 
84
            		/* get product name */
85
            		if(usbGetDescriptorString(handle, dev->descriptor.iProduct, 0x0409, devProduct, sizeof(devVendor)) < 0) {
86
                		fprintf(stderr, "Warning: cannot query product for device: %s\n", usb_strerror()); usb_close(handle);
87
                		continue;
88
            		}
89
 
90
            		if(strcmp(devVendor, vendorName) == 0 && strcmp(devProduct, productName) == 0)
91
                		return handle;
92
            		else
93
                		usb_close(handle);
94
		}
95
	}
96
 
97
	return NULL;
98
}
99
 
100
int main(int argc, char **argv) {
101
	usb_dev_handle *handle = NULL;
102
    int nBytes = 0;
103
    char buffer[256];
104
 
105
	if(argc < 2) {
106
		printf("Usage:\n");
107
		printf("usbtext on\n");
108
		printf("usbtext off\n");
109
		exit(1);
110
	}
111
 
112
	handle = usbOpenDevice(0x4242, "newioit.com.au", 0xe231, "Comm Selector");
113
 
114
	if(handle == NULL) {
115
		fprintf(stderr, "Could not find USB device!\n");
116
		exit(1);
117
	}
118
 
119
	if(strcmp(argv[1], "on") == 0) {
120
		nBytes = usb_control_msg(handle, 
121
            USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, 
91 pfowler 122
			USB_LED_ON, atoi(argv[2]), 0, (char *)buffer, sizeof(buffer), 5000);
89 pfowler 123
	} else if(strcmp(argv[1], "off") == 0) {
124
		nBytes = usb_control_msg(handle, 
125
            USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, 
126
			USB_LED_OFF, 0, 0, (char *)buffer, sizeof(buffer), 5000);
127
	}
128
 
129
	if(nBytes < 0)
130
		fprintf(stderr, "USB error: %s\n", usb_strerror());
131
 
132
	usb_close(handle);
133
 
134
	return 0;
135
}