Subversion Repositories group.electronics

Rev

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