summaryrefslogtreecommitdiff
path: root/pc/usbtest.c
blob: 8ea2a62ce2ca0925940537f01482548c28d50b4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**
 * Project: AVR ATtiny USB Tutorial at http://codeandlife.com/
 * Author: Joonas Pihlajamaa, joonas.pihlajamaa@iki.fi
 * Based on V-USB example code by Christian Starkjohann
 * Copyright: (C) 2012 by Joonas Pihlajamaa
 * License: GNU GPL v3 (see License.txt)
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <usb.h>

// used to get descriptor strings for device identification
static int usbGetDescriptorString(usb_dev_handle *dev, int index, int langid,
                                  char *buf, int buflen)
{
	char buffer[256];
	int rval, i;

	// make standard request GET_DESCRIPTOR, type string and given index
	// (e.g. dev->iProduct)
	rval = usb_control_msg(dev,
	                       USB_TYPE_STANDARD | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
	                       USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) + index, langid,
	                       buffer, sizeof(buffer), 1000);

	if(rval < 0) // error
		return rval;

	// rval should be bytes read, but buffer[0] contains the actual response size
	if((unsigned char)buffer[0] < rval)
		rval = (unsigned char)buffer[0]; // string is shorter than bytes read

	if(buffer[1] != USB_DT_STRING) // second byte is the data type
		return 0; // invalid return type

	// we're dealing with UTF-16LE here so actual chars is half of rval,
	// and index 0 doesn't count
	rval /= 2;

	// lossy conversion to ISO Latin1
	for(i = 1; i < rval && i < buflen; i++)
	{
		if(buffer[2 * i + 1] == 0)
			buf[i-1] = buffer[2 * i];
		else
			buf[i-1] = '?'; // outside of ISO Latin1 range
	}
	buf[i-1] = 0;

	return i-1;
}

static usb_dev_handle * usbOpenDevice(int vendor, char *vendorName,
                                      int product, char *productName)
{
	struct usb_bus *bus;
	struct usb_device *dev;
	char devVendor[256], devProduct[256];

	usb_dev_handle * handle = NULL;

	usb_set_debug(3);

	usb_init();
	usb_find_busses();
	usb_find_devices();

	for(bus=usb_get_busses(); bus; bus=bus->next)
	{
		for(dev=bus->devices; dev; dev=dev->next)
		{
			if(dev->descriptor.idVendor != vendor ||
			        dev->descriptor.idProduct != product)
				continue;

			// we need to open the device in order to query strings
			if(!(handle = usb_open(dev)))
			{
				fprintf(stderr, "Warning: cannot open USB device: %s\n",
				        usb_strerror());
				continue;
			}

			// get vendor name
			if(usbGetDescriptorString(handle, dev->descriptor.iManufacturer, 0x0409, devVendor, sizeof(devVendor)) < 0)
			{
				fprintf(stderr,
				        "Warning: cannot query manufacturer for device: %s\n",
				        usb_strerror());
				usb_close(handle);
				continue;
			}

			// get product name
			if(usbGetDescriptorString(handle, dev->descriptor.iProduct,
			                          0x0409, devProduct, sizeof(devVendor)) < 0)
			{
				fprintf(stderr,
				        "Warning: cannot query product for device: %s\n",
				        usb_strerror());
				usb_close(handle);
				continue;
			}

			if(strcmp(devVendor, vendorName) == 0 &&
			        strcmp(devProduct, productName) == 0)
				return handle;
			else
				usb_close(handle);
		}
	}

	return NULL;
}


void usage(const char* argv0)
{
	printf("Usage: %s read\n"
	       "       %s write <PAGE> <BYTE1> <BYTE2> ... <BYTE32>\n"
	       "                where <PAGE> is 0,1,2,3\n"
	       "                and <BYTEn> is in hex, without 0x.\n"
	       "                example: write 2 13 37 DE AD be ef ...\n\n", argv0, argv0);
	exit(0);
}

int main(int argc, char **argv)
{
	usb_dev_handle *handle = NULL;
	int nBytes = 0;
	char buffer[256];

	int mode;
	#define MODE_READ 0
	#define MODE_WRITE 1

	int write_page = -1; // which page should be written to? contains 0,1,2,3

	if (argc <= 1)
		usage(argv[0]);
	
	if (strcmp(argv[1], "read")==0 && argc == 2)
		mode=MODE_READ;
	else if (strcmp(argv[1], "write")==0 && argc == 2+1+32)
	{
		mode=MODE_WRITE;
		write_page = atoi(argv[2]) - 1;

		if (write_page > 3 || write_page < 0)
		{
			printf("ERROR: invalid page\n\n");
			exit(1);
		}

		for (int i=0; i<32; i++)
			buffer[i] = strtol(argv[3+i],NULL,16);
	}
	else
		usage(argv[0]);


	handle = usbOpenDevice(0x16C0, "windfisch.org", 0x05DC, "DS1992 Dumper");

	if(handle == NULL)
	{
		fprintf(stderr, "Could not find USB device!\n");
		exit(1);
	}

	printf("got device\n");

	if (mode == MODE_READ)
	{
		// read from device
		nBytes = usb_control_msg(handle, 
			USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, 
			0x21, 0, 0, (char *)buffer, sizeof(buffer), 1000);
			printf("Got %d bytes: %s = ", nBytes, buffer);
			for (int i=0; i<nBytes; i++)
			{
				if (i%16 == 0) printf("\n");
				printf("%.2X ",buffer[i] & 0xFF);
			}
			printf("\n");
	}
	else if (mode == MODE_WRITE)
	{
		buffer[32] = write_page;

		// write to device
		nBytes = usb_control_msg(handle, 
			USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT, 
			0x42, 0, 0, buffer, 32+1 /*length*/, 1000);
			printf("Wrote %d bytes\n", nBytes);




		// read from device
		nBytes = usb_control_msg(handle, 
			USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, 
			0x21, 0, 0, (char *)buffer, sizeof(buffer), 1000);
			printf("Got %d bytes: %s = ", nBytes, buffer);
			for (int i=0; i<nBytes; i++)
			{
				if (i%16 == 0) printf("\n");
				printf("%.2X ",buffer[i] & 0xFF);
			}
			printf("\n");
	}

/*

	nBytes = usb_control_msg(handle, 
		USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, 
		0x42, 0,0, 
		(char *)buffer, sizeof(buffer), 5000);
	if(strcmp(argv[1], "on") == 0)
	{
		nBytes = usb_control_msg(handle,
		                         USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
		                         USB_LED_ON, 0, 0, (char *)buffer, sizeof(buffer), 5000);
	}
	else if(strcmp(argv[1], "off") == 0)
	{
		nBytes = usb_control_msg(handle,
		                         USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
		                         USB_LED_OFF, 0, 0, (char *)buffer, sizeof(buffer), 5000);
	}
	else if(strcmp(argv[1], "out") == 0)
	{
		nBytes = usb_control_msg(handle,
		                         USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
		                         USB_DATA_OUT, 0, 0, (char *)buffer, sizeof(buffer), 5000);
		printf("Got %d bytes: %s\n", nBytes, buffer);
	}
	else if(strcmp(argv[1], "write") == 0)
	{
		nBytes = usb_control_msg(handle,
		                         USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
		                         USB_DATA_WRITE, 'T' + ('E' << 8), 'S' + ('T' << 8),
		                         (char *)buffer, sizeof(buffer), 5000);
	}
	else if(strcmp(argv[1], "in") == 0 && argc > 2)
	{
		nBytes = usb_control_msg(handle,
		                         USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_OUT,
		                         USB_DATA_IN, 0, 0, argv[2], strlen(argv[2])+1, 5000);
	}
*/
	if(nBytes < 0)
		fprintf(stderr, "USB error: %s\n", usb_strerror());

	usb_close(handle);

	return 0;
}