summaryrefslogtreecommitdiff
path: root/test/test.c
blob: 536360c347c14b7b9d141cb486ba82553119a260 (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
// ======================================================================
// JVdG USB Gadget libusb test program
//
// Copyright 2011 Julien Viard de Galbert
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software 
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
// MA  02110-1301, USA.
// ======================================================================

#include <stdio.h>
#include <libusb.h>


enum
{
	// Generic requests
	USBTINY_ECHO,		// echo test
	USBTINY_READ,		// read byte
	USBTINY_WRITE,		// write byte
	USBTINY_CLR,		// clear bit 
	USBTINY_SET,		// set bit
	//
	USBTINY_KEY_READ	// read key status
};

#define USB_TIMEOUT 500

libusb_device_handle * usb_device;

int main(int argc, char ** argv)
{
	libusb_init(NULL);

	printf("USB gadget test program (c) 2010 Julien VdG\n");

	usb_device = libusb_open_device_with_vid_pid (NULL, 0x6666, 0x0001);
	if(NULL == usb_device) {
		fprintf(stderr, "ERROR: USB device not found !\n");
		libusb_exit(NULL);
		return 2;
	}

	{
		unsigned char res[4];
		int st;
		st = libusb_control_transfer(usb_device, 
			LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
			USBTINY_READ,
			0,
			0,
			res,
			1,
			USB_TIMEOUT);
		printf("Read LEDs: %x (%d)\n",res[0], st);

		st = libusb_control_transfer(usb_device, 
			LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
			USBTINY_WRITE,
			0xf & (res[0]+1),
			1,
			NULL,
			0,
			USB_TIMEOUT);

		st = libusb_control_transfer(usb_device, 
			LIBUSB_ENDPOINT_IN | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE,
			USBTINY_KEY_READ,
			0,
			0,
			res,
			1,
			USB_TIMEOUT);
		printf("Read Keys: %x (%d)\n",res[0], st);

	}


	libusb_close(usb_device);
	libusb_exit(NULL);
	return 0;
}