Subversion Repositories group.electronics

Rev

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