Subversion Repositories group.electronics

Rev

Rev 114 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
113 pfowler 1
#include <stdio.h>
2
#include <conio.h>
3
#include <stdlib.h>
4
#include <string.h>
5
#include <windows.h>
6
#include <tchar.h>
7
#include <strsafe.h>
8
 
9
#include <lusb0_usb.h>
10
#include "SimConnect.h"
11
 
12
 
13
#define sbi(sfr, bit)   ((sfr) |= 1 << bit)
14
#define cbi(sfr, bit)   ((sfr) &= ~(1 << bit))
15
#define xbi(sfr, bit)   ((sfr) ^= 1 << bit)
16
#define rbi(sfr, bit)   (((sfr) >> (bit)) & 0x01)
17
 
18
// Same as in main.c
19
#define USB_STATE_READ 	100
20
#define USB_LEDS_SET  	101
21
 
22
// Console symbols
23
char* M_OUT = "  :<- ";	// message outbound
24
char* M_IN = "->:   ";	// message inbound
25
char* M_NO = "  :   ";	// not a message
26
char* I_SP = " ";		// space
27
char* I_OK = "+";		// ignored, ok
28
char* I_NEX = "?";		// ignored, unexpected
29
char* I_EXE = "e";		// exception
30
char* X_SEP = "|";		// separator
31
 
32
 
33
 
34
#define		BUTTONS_COM1	0
35
#define		BUTTONS_COM2	1
36
#define		BUTTONS_BOTH	2
37
#define		BUTTONS_NAV1	3
38
#define		BUTTONS_NAV2	4
39
#define		BUTTONS_MKR 	5
40
#define		BUTTONS_DME 	6
41
#define		BUTTONS_ADF 	7
42
 
43
 
44
 
45
 
46
static usb_dev_handle * usbOpenDevice(int vendor, char *vendorName, int product, char *productName);
47
static int usbGetDescriptorString(usb_dev_handle *dev, int index, int langid, char *buf, int buflen);
48
void testCode(usb_dev_handle *handle);
49
 
50
void CALLBACK dispatchProc(SIMCONNECT_RECV *pData, DWORD cbData, void *pContext);
51
 
52
void Process_Exception(SIMCONNECT_RECV* pData);
53
void Process_Connected();
54
void Process_FSX_Quit();
55
 
56
char* Exception_String(DWORD Recv_Exception);
57
char* RECV_Msg_String(DWORD Recv_ID);
58
char* Format_SOL(char* Direction);
59
char* Format_SOL(char* Direction, char* Importance);
60
 
61
void Connect();
62
void Disconnect();
63
 
64
 
65
HANDLE  hSimConnect = NULL;
66
bool    keep_going = false;
67
bool	Paused = true;
68
char string[80];
69
bool	updateLeds = true;
70
char ledStatus = 0x00;
71
 
72
struct Struct1 {
73
	double	comm_1;
74
	double	comm_2;
75
	double	comm_both;
76
};
77
 
78
static enum DATA_DEFINE_ID {
79
    DEFINITION_1,
80
};
81
 
82
static enum DATA_REQUEST_ID {
83
    REQUEST_1,
84
};
85
 
86
 
87
 
88
static enum EVENT_ID {
89
    EVENT_BRAKES,
90
	EVENT_COM1_TRANSMIT_SELECT,
91
	EVENT_COM2_TRANSMIT_SELECT,
92
	EVENT_COM_RECEIVE_ALL_TOGGLE,
93
};
94
 
95
static enum INPUT_ID {
96
    INPUT0,
97
};
98
 
99
static enum GROUP_ID {
100
    GROUP0,
101
};
102
 
103
 
104
 
105
 
106
int main(int argc, char **argv) {
107
	usb_dev_handle *handle = NULL;
108
 
109
    handle = usbOpenDevice(0x4242, "newioit.com.au", 0xe231, "Comm Selector");
110
 
111
    if(handle == NULL) {
112
            fprintf(stderr, "Could not find USB device\n");
113
            exit(1);
114
    }
115
 
116
	//testCode(handle);
117
 
118
	Connect();
119
 
120
	if (hSimConnect != NULL) {
121
		keep_going = true;
122
 
123
		int nBytes = 0;
124
		char buffer[16];
125
		int i;
126
		for (i = 0; i < 15; i++) {
127
			buffer[i] = 0x00;
128
		}
129
 
130
 
131
		nBytes = usb_control_msg(handle,
132
						USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
133
						USB_LEDS_SET, ledStatus, 0, (char *)buffer, sizeof(buffer), 5000);
134
 
135
		unsigned int lastButtons = 0;
136
 
137
		while(keep_going)
138
        {
139
			HRESULT hr;
140
 
141
			// Put this in an 'On Event' thing later...
142
			hr = SimConnect_RequestDataOnSimObjectType(hSimConnect, REQUEST_1, DEFINITION_1, 0, SIMCONNECT_SIMOBJECT_TYPE_USER);
143
 
144
			nBytes = usb_control_msg(handle,
145
					USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
146
					USB_LEDS_SET, ledStatus, 0, (char *)buffer, sizeof(buffer), 5000);
147
 
148
			nBytes = usb_control_msg(handle,
149
						USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
150
						USB_STATE_READ, 0, 0, (char *)buffer, sizeof(buffer), 5000);
151
			unsigned int buttons = (unsigned int)buffer[1];
152
 
153
			if (rbi(buttons, BUTTONS_COM1) && rbi(lastButtons, BUTTONS_COM1) == 0) {
154
				sbi(lastButtons, BUTTONS_COM1);
155
				hr = SimConnect_TransmitClientEvent(hSimConnect,0,EVENT_COM1_TRANSMIT_SELECT, 1 ,SIMCONNECT_GROUP_PRIORITY_STANDARD,SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY);
156
				printf("\n%s COM1 pressed.", Format_SOL(M_IN));
157
			}
158
 
159
			if (rbi(lastButtons, BUTTONS_COM1) && rbi(buttons, BUTTONS_COM1) == 0x00) {
160
				cbi(lastButtons, BUTTONS_COM1);
161
				printf("\n%s COM1 released.", Format_SOL(M_IN));
162
			}
163
 
164
			if (rbi(buttons, BUTTONS_COM2) && rbi(lastButtons, BUTTONS_COM2) == 0) {
165
				sbi(lastButtons, BUTTONS_COM2);
166
				hr = SimConnect_TransmitClientEvent(hSimConnect,0,EVENT_COM2_TRANSMIT_SELECT, 1 ,SIMCONNECT_GROUP_PRIORITY_STANDARD,SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY);
167
				printf("\n%s COM2 pressed.", Format_SOL(M_IN));
168
			}
169
 
170
			if (rbi(lastButtons, BUTTONS_COM2) && rbi(buttons, BUTTONS_COM2) == 0x00) {
171
				cbi(lastButtons, BUTTONS_COM2);
172
				printf("\n%s COM2 released.", Format_SOL(M_IN));
173
			}
174
 
175
			if (rbi(buttons, BUTTONS_BOTH) && rbi(lastButtons, BUTTONS_BOTH) == 0) {
176
				sbi(lastButtons, BUTTONS_BOTH);
177
				hr = SimConnect_TransmitClientEvent(hSimConnect,0,EVENT_COM_RECEIVE_ALL_TOGGLE, 1 ,SIMCONNECT_GROUP_PRIORITY_STANDARD,SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY);
178
				printf("\n%s BOTH pressed.", Format_SOL(M_IN));
179
			}
180
 
181
			if (rbi(lastButtons, BUTTONS_BOTH) && rbi(buttons, BUTTONS_BOTH) == 0x00) {
182
				cbi(lastButtons, BUTTONS_BOTH);
183
				printf("\n%s BOTH released.", Format_SOL(M_IN));
184
			}
185
 
186
 
187
 
188
			if (rbi(buttons, BUTTONS_NAV1) && rbi(lastButtons, BUTTONS_NAV1) == 0) {
189
				sbi(lastButtons, BUTTONS_NAV1);
190
				hr = SimConnect_TransmitClientEvent(hSimConnect,0,EVENT_BRAKES, 1 ,SIMCONNECT_GROUP_PRIORITY_STANDARD,SIMCONNECT_EVENT_FLAG_GROUPID_IS_PRIORITY);
191
				printf("\n%s Buttons brakes engaged.", Format_SOL(M_IN));
192
			}
193
 
194
			if (rbi(lastButtons, BUTTONS_NAV1) && rbi(buttons, BUTTONS_NAV1) == 0x00) {
195
				cbi(lastButtons, BUTTONS_NAV1);
196
				printf("\n%s Button brakes released.", Format_SOL(M_IN));
197
			}
198
 
199
 
200
            SimConnect_CallDispatch(hSimConnect, dispatchProc, NULL);
201
            Sleep(1);
202
        }
203
        Disconnect();
204
    } else {
205
		// Implement retrying
206
	}
207
 
208
	// Keeps the window open. Uses old test code
209
	//  so state of CommSelector can be tested
210
	//testCode(handle);
211
 
212
    usb_close(handle);
213
 
214
	return 0;
215
}
216
 
217
void Connect() {
218
	HRESULT hr;
219
 
220
	if (hSimConnect == NULL) {
221
		printf("\n%s Requesting a SimConnect connection", Format_SOL(M_OUT));
222
		hr = SimConnect_Open(&hSimConnect, "Comm Selector", NULL, 0, 0, 0);
223
		if (hr != S_OK)	{
224
			// FSX may be inactive, or there is a problem
225
			printf("\n%s Error %d while opening a SimConnect connection", Format_SOL(M_IN, I_OK), hr);
226
		} else {
227
			// Connection to FSX initialized
228
			printf("\n%s Connection request being processed (%d)", Format_SOL(M_IN), hSimConnect);
229
			// (When connection process completes, an OPEN message will be received)
230
		}
231
	}
232
}
233
 
234
void Disconnect() {
235
	HRESULT hr;
236
 
237
	if (hSimConnect != NULL) {
238
		if (hr = SimConnect_Close(hSimConnect) == S_OK) {
239
			printf("\n%s Connection closed (%d).", Format_SOL(M_IN), hSimConnect);
240
		} else {
241
			printf("\n%s Error (%d) while closing the connection (%d).", Format_SOL(M_IN, I_NEX), hr, hSimConnect);
242
		}		
243
	}
244
 
245
	printf("\n%s Resetting internal status to disconnected.", Format_SOL(M_NO));
246
	hSimConnect = NULL;
247
	keep_going = false;
248
}
249
 
250
void CALLBACK dispatchProc(SIMCONNECT_RECV* pData, DWORD cbData, void *pContext)
251
{
252
    switch(pData->dwID)
253
    {
254
 
255
 
256
		case SIMCONNECT_RECV_ID_QUIT:
257
			// FSX is stopping
258
			Process_FSX_Quit();
259
            break;
260
 
261
		case SIMCONNECT_RECV_ID_OPEN:
262
			// the connection process is complete
263
			Process_Connected();
264
			break;
265
 
266
		case SIMCONNECT_RECV_ID_EVENT: {
267
			SIMCONNECT_RECV_EVENT *evt = (SIMCONNECT_RECV_EVENT*)pData;
268
			switch (evt->uEventID) {
269
				case EVENT_BRAKES:
270
					printf("\nEvent brakes: %d", evt->dwData);
271
					break;
272
				case EVENT_COM1_TRANSMIT_SELECT:
273
					printf("\nEvent com1 select: %d", evt->dwData);
274
					break;
275
				case EVENT_COM2_TRANSMIT_SELECT:
276
					printf("\nEvent com1 select: %d", evt->dwData);
277
					break;
278
				case EVENT_COM_RECEIVE_ALL_TOGGLE:
279
					printf("\nEvent com1 select: %d", evt->dwData);
280
					break;
281
				default:
282
					break;
283
			}
284
			break;
285
		}
286
 
287
 
288
		case SIMCONNECT_RECV_ID_SIMOBJECT_DATA_BYTYPE: {
289
			SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE *pObjData = (SIMCONNECT_RECV_SIMOBJECT_DATA_BYTYPE*)pData;
290
 
291
			switch(pObjData->dwRequestID) {
292
				case REQUEST_1: {
293
					DWORD ObjectID = pObjData->dwObjectID;
294
					Struct1 *pS = (Struct1*)&pObjData->dwData;
295
					if (pS->comm_1)
296
						sbi(ledStatus, BUTTONS_COM1);
297
					else
298
						cbi(ledStatus, BUTTONS_COM1);
299
					if (pS->comm_2)
300
						sbi(ledStatus, BUTTONS_COM2);
301
					else
302
						cbi(ledStatus, BUTTONS_COM2);
303
					if (pS->comm_both)
304
						sbi(ledStatus, BUTTONS_BOTH);
305
					else
306
						cbi(ledStatus, BUTTONS_BOTH);
307
					updateLeds = true;
308
					//printf("\nTransmit Both: %f", pS->comm_both );
309
					break;
310
				}
311
			}
312
			break;
313
		}
314
 
315
 
316
		/* Not found
317
		case SIMCONNECT_RECV_ID_EVENT_WEATHER_MODE:
318
		case SIMCONNECT_RECV_ID_AIRPORT_LIST:
319
		case SIMCONNECT_RECV_ID_VOR_LIST:
320
		case SIMCONNECT_RECV_ID_NDB_LIST:
321
		case SIMCONNECT_RECV_ID_WAYPOINT_LIST:
322
		case SIMCONNECT_RECV_ID_EVENT_MULTIPLAYER_SERVER_STARTED:
323
		case SIMCONNECT_RECV_ID_EVENT_MULTIPLAYER_CLIENT_STARTED:
324
		case SIMCONNECT_RECV_ID_EVENT_MULTIPLAYER_SESSION_ENDED:
325
		case SIMCONNECT_RECV_ID_EVENT_RACE_END:
326
		case SIMCONNECT_RECV_ID_EVENT_RACE_LAP: */
327
 
328
		case SIMCONNECT_RECV_ID_EXCEPTION:
329
        case SIMCONNECT_RECV_ID_EVENT_OBJECT_ADDREMOVE:
330
		case SIMCONNECT_RECV_ID_SIMOBJECT_DATA:
331
		case SIMCONNECT_RECV_ID_ASSIGNED_OBJECT_ID:
332
		case SIMCONNECT_RECV_ID_EVENT_FILENAME:
333
		case SIMCONNECT_RECV_ID_EVENT_FRAME:
334
		case SIMCONNECT_RECV_ID_WEATHER_OBSERVATION:
335
		case SIMCONNECT_RECV_ID_CLOUD_STATE:
336
		case SIMCONNECT_RECV_ID_RESERVED_KEY:
337
		case SIMCONNECT_RECV_ID_CUSTOM_ACTION:
338
		case SIMCONNECT_RECV_ID_SYSTEM_STATE:
339
		case SIMCONNECT_RECV_ID_CLIENT_DATA:
340
 
341
			printf("\n%s Message ignored (%s)", RECV_Msg_String(pData->dwID), Format_SOL(M_IN, I_OK));
342
			break;
343
		default:
344
			printf("\n%s Message ignored (Unknown RECV_ID)", Format_SOL(M_IN, I_NEX));
345
    }
346
}
347
 
348
void Process_Exception(SIMCONNECT_RECV* pData) {
349
	// SimConnect cannot process a client request
350
	SIMCONNECT_RECV_EXCEPTION *except = (SIMCONNECT_RECV_EXCEPTION *) pData;
351
	printf("\n%s Exception %d on packet %d, argument %d. (%s)", Format_SOL(M_IN, I_EXE),
352
		except->dwException, except->dwSendID, except->dwIndex,
353
		Exception_String(except->dwException));
354
}
355
 
356
void Process_Connected() {
357
	printf("\n%s Connected.", Format_SOL(M_IN));
358
 
359
		HRESULT hr;
360
    hr = SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_BRAKES, "brakes");
361
	hr = SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_COM1_TRANSMIT_SELECT, "COM1_TRANSMIT_SELECT");
362
	hr = SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_COM2_TRANSMIT_SELECT, "COM2_TRANSMIT_SELECT");
363
	hr = SimConnect_MapClientEventToSimEvent(hSimConnect, EVENT_COM_RECEIVE_ALL_TOGGLE, "COM_RECEIVE_ALL_TOGGLE");
364
 
365
 
366
    hr = SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, EVENT_BRAKES);
367
	hr = SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, EVENT_COM1_TRANSMIT_SELECT);
368
	hr = SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, EVENT_COM2_TRANSMIT_SELECT);
369
	hr = SimConnect_AddClientEventToNotificationGroup(hSimConnect, GROUP0, EVENT_COM_RECEIVE_ALL_TOGGLE);
370
    hr = SimConnect_SetNotificationGroupPriority(hSimConnect, GROUP0, SIMCONNECT_GROUP_PRIORITY_HIGHEST);
371
 
372
	hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "COM TRANSMIT:1", "Bool");
373
	hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "COM TRANSMIT:2", "Bool");
374
	hr = SimConnect_AddToDataDefinition(hSimConnect, DEFINITION_1, "COM RECIEVE ALL", "Bool");
375
 
376
 
377
    //hr = SimConnect_MapInputEventToClientEvent(hSimConnect, INPUT0, "shift+ctrl+u", EVENT_BRAKES);
378
    //hr = SimConnect_SetInputGroupState(hSimConnect, INPUT0, SIMCONNECT_STATE_ON);
379
}
380
 
381
void Process_FSX_Quit() {
382
	// Stop sending commands to SimConnect
383
	keep_going = false;
384
}
385
 
386
char* Exception_String(DWORD Recv_Exception)
387
{
388
	// Names of Exceptions
389
	char* EXCEPTION_String[] =
390
	{
391
		"NONE", "ERROR", "SIZE_MISMATCH", "UNRECOGNIZED_ID", "UNOPENED", "VERSION_MISMATCH", "TOO_MANY_GROUPS",
392
		"NAME_UNRECOGNIZED", "TOO_MANY_EVENT_NAMES", "EVENT_ID_DUPLICATE", "TOO_MANY_MAPS",
393
		"TOO_MANY_OBJECTS", "TOO_MANY_REQUESTS", "WEATHER_INVALID_PORT", "WEATHER_INVALID_METAR",
394
		"WEATHER_UNABLE_TO_GET_OBSERVATION", "WEATHER_UNABLE_TO_CREATE_STATION",
395
		"WEATHER_UNABLE_TO_REMOVE_STATION", "INVALID_DATA_TYPE", "INVALID_DATA_SIZE",
396
		"DATA_ERROR", "INVALID_ARRAY", "CREATE_OBJECT_FAILED", "LOAD_FLIGHTPLAN_FAILED",
397
		"OPERATION_INVALID_FOR_OBJECT_TYPE", "ILLEGAL_OPERATION", "ALREADY_SUBSCRIBED", "INVALID_ENUM",
398
		"DEFINITION_ERROR", "DUPLICATE_ID", "DATUM_ID", "OUT_OF_BOUNDS", "ALREADY_CREATED",
399
		"OBJECT_OUTSIDE_REALITY_BUBBLE", "OBJECT_CONTAINER", "OBJECT_AI", "OBJECT_ATC", "OBJECT_SCHEDULE",
400
	};
401
	// Return "unknown" if out of bound
402
	return Recv_Exception > ARRAYSIZE(EXCEPTION_String) ? "unknown" : EXCEPTION_String[Recv_Exception];
403
}
404
// Format the beginning of the console line, one argument
405
char* Format_SOL(char* Direction)
406
{
407
	return Format_SOL(Direction, I_SP);
408
}
409
// Format the beginning of the console line, two arguments
410
char* Format_SOL(char* Direction, char* Importance)
411
{
412
	strcpy_s( string, Direction);
413
	strcat_s( string, Importance);
414
	strcat_s( string, X_SEP);
415
	return string;
416
}
417
 
418
// Returns the description of the message id
419
char* RECV_Msg_String(DWORD Recv_ID)
420
{
421
	// Names of RECV_ID (type of message to dispatch)
422
	char* RECV_ID_String[] =
423
	{
424
		"RECV_ID_EXCEPTION", "RECV_ID_OPEN", "RECV_ID_QUIT", "RECV_ID_EVENT",
425
		"RECV_ID_EVENT_OBJECT_ADDREMOVE", "RECV_ID_EVENT_FILENAME", "RECV_ID_EVENT_FRAME",
426
		"RECV_ID_SIMOBJECT_DATA", "RECV_ID_SIMOBJECT_DATA_BYTYPE",
427
		"RECV_ID_WEATHER_OBSERVATION", "RECV_ID_CLOUD_STATE", "RECV_ID_ASSIGNED_OBJECT_ID",
428
		"RECV_ID_RESERVED_KEY", "RECV_ID_CUSTOM_ACTION", "RECV_ID_SYSTEM_STATE",
429
		"RECV_ID_CLIENT_DATA", "RECV_ID_EVENT_WEATHER_MODE", "RECV_ID_AIRPORT_LIST",
430
		"RECV_ID_VOR_LIST", "RECV_ID_NDB_LIST", "RECV_ID_WAYPOINT_LIST",
431
		"RECV_ID_EVENT_MULTIPLAYER_SERVER_STARTED", "RECV_ID_EVENT_MULTIPLAYER_CLIENT_STARTED",
432
		"RECV_ID_EVENT_MULTIPLAYER_SESSION_ENDED", "RECV_ID_EVENT_RACE_END",
433
		"RECV_ID_EVENT_RACE_LAP",
434
	};
435
	// Return "unknown" if out of bound
436
	return Recv_ID > ARRAYSIZE(RECV_ID_String) ? "unknown" : RECV_ID_String[Recv_ID];
437
}
438
 
439
 
440
/*
441
 *	*** USB Functions ***
442
 */
443
 
444
static int usbGetDescriptorString(usb_dev_handle *dev, int index, int langid,
445
                                  char *buf, int buflen) {
446
    char buffer[256];
447
    int rval, i;
448
 
449
        // make standard request GET_DESCRIPTOR, type string and given index
450
    // (e.g. dev->iProduct)
451
        rval = usb_control_msg(dev,
452
        USB_TYPE_STANDARD | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
453
        USB_REQ_GET_DESCRIPTOR, (USB_DT_STRING << 8) + index, langid,
454
        buffer, sizeof(buffer), 1000);
455
 
456
    if(rval < 0) // error
457
                return rval;
458
 
459
    // rval should be bytes read, but buffer[0] contains the actual response size
460
        if((unsigned char)buffer[0] < rval)
461
                rval = (unsigned char)buffer[0]; // string is shorter than bytes read
462
 
463
        if(buffer[1] != USB_DT_STRING) // second byte is the data type
464
                return 0; // invalid return type
465
 
466
        // we're dealing with UTF-16LE here so actual chars is half of rval,
467
        // and index 0 doesn't count
468
        rval /= 2;
469
 
470
        /* lossy conversion to ISO Latin1 */
471
        for(i = 1; i < rval && i < buflen; i++) {
472
                if(buffer[2 * i + 1] == 0)
473
                        buf[i-1] = buffer[2 * i];
474
                else
475
                        buf[i-1] = '?'; /* outside of ISO Latin1 range */
476
        }
477
        buf[i-1] = 0;
478
 
479
        return i-1;
480
}
481
 
482
static usb_dev_handle * usbOpenDevice(int vendor, char *vendorName,
483
                                      int product, char *productName) {
484
        struct usb_bus *bus;
485
        struct usb_device *dev;
486
        char devVendor[256], devProduct[256];
487
 
488
        usb_dev_handle * handle = NULL;
489
 
490
        usb_init();
491
        usb_find_busses();
492
        usb_find_devices();
493
 
494
        for(bus=usb_get_busses(); bus; bus=bus->next) {
495
                for(dev=bus->devices; dev; dev=dev->next) {
496
 
497
                        if(dev->descriptor.idVendor != vendor ||
498
                                dev->descriptor.idProduct != product)
499
                                continue;
500
 
501
                         /* we need to open the device in order to query strings */
502
                        if(!(handle = usb_open(dev))) {
503
                                fprintf(stderr, "Warning: cannot open USB device: %s\n",
504
                                usb_strerror());
505
                                continue;
506
                        }
507
 
508
                        /* get vendor name */
509
                        if(usbGetDescriptorString(handle, dev->descriptor.iManufacturer, 0x4242, devVendor, sizeof(devVendor)) < 0) {
510
                                fprintf(stderr, "Warning: cannot query manufacturer for device: %s\n", usb_strerror());
511
                                usb_close(handle);
512
                                continue;
513
                        }
514
 
515
                        /* get product name */
516
                        if(usbGetDescriptorString(handle, dev->descriptor.iProduct, 0xe231, devProduct, sizeof(devVendor)) < 0) {
517
                                fprintf(stderr, "Warning: cannot query product for device: %s\n", usb_strerror()); usb_close(handle);
518
                                continue;
519
                        }
520
 
521
                        if(strcmp(devVendor, vendorName) == 0 && strcmp(devProduct, productName) == 0)
522
                                return handle;
523
                        else
524
                                usb_close(handle);
525
                }
526
        }
527
 
528
        return NULL;
529
}
530
 
531
void testCode(usb_dev_handle *handle) {
532
 
533
	int nBytes = 0;
534
    char buffer[16];
535
	int i;
536
	for (i = 0; i < 15; i++) {
537
		buffer[i] = 0x00;
538
	}
539
 
540
	char ledStatus = 0;
541
	char keyIn = 0x00;
542
 
543
	// Return to old testing code
544
	printf("Press ESC to continue... (Or debug codes)\n");
545
	while (1) {
546
		keyIn = _getch();
547
		printf("Key: %02X\n", keyIn);
548
		if (keyIn == 0x1B || keyIn == 0x71) {
549
			return;
550
		} else if (keyIn >= 0x31 && keyIn <= 0x38) {
551
				char j = atoi(&keyIn);
552
				xbi(ledStatus, (j-1) );				
553
 
554
				nBytes = usb_control_msg(handle,
555
						USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
556
						USB_LEDS_SET, ledStatus, 0, (char *)buffer, sizeof(buffer), 5000);
557
		} else if(keyIn == 'r') {
558
				nBytes = usb_control_msg(handle,
559
						USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
560
						USB_STATE_READ, 0, 0, (char *)buffer, sizeof(buffer), 5000);
561
			printf("Got %d bytes: %s\n", nBytes, buffer);
562
			for (i = 0; i < 15; i++) {
563
				if (i>0) printf(":");
564
				printf("%02X",  buffer[i]);
565
			}
566
			printf("\n");
567
		}
568
		Sleep(1);
569
	}
570
}