Subversion Repositories group.electronics

Rev

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