-
Notifications
You must be signed in to change notification settings - Fork 2
/
USBHIDLister.pas
169 lines (147 loc) · 5.16 KB
/
USBHIDLister.pas
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
program USBHIDLister;
{$mode objfpc}{$H+}
{ Raspberry Pi 3 Application }
{ Add your program code below, add additional units to the "uses" section if }
{ required and create new units by selecting File, New Unit from the menu. }
{ }
{ To compile your program select Run, Compile (or Run, Build) from the menu. }
{$define use_tftp}
uses
RaspberryPi3,
GlobalConfig,
GlobalConst,
GlobalTypes,
Platform,
Threads,
SysUtils,
Classes,
uLog,
USB,
Devices,
uHIDUsage,
{$ifdef use_tftp}
uTFTP, Winsock2,
{$endif}
Ultibo, Console
{ Add additional units here };
var
Console1, Console2, Console3 : TWindowHandle;
{$ifdef use_tftp}
IPAddress : string;
{$endif}
USBDriver : PUSBDriver;
procedure Log1 (s : string);
begin
ConsoleWindowWriteLn (Console1, s);
end;
procedure Log2 (s : string);
begin
ConsoleWindowWriteLn (Console2, s);
end;
procedure Log3 (s : string);
begin
ConsoleWindowWriteLn (Console3, s);
end;
procedure Msg3 (Sender : TObject; s : string);
begin
Log3 (s);
end;
{$ifdef use_tftp}
function WaitForIPComplete : string;
var
TCP : TWinsock2TCPClient;
begin
TCP := TWinsock2TCPClient.Create;
Result := TCP.LocalAddress;
if (Result = '') or (Result = '0.0.0.0') or (Result = '255.255.255.255') then
begin
while (Result = '') or (Result = '0.0.0.0') or (Result = '255.255.255.255') do
begin
sleep (1000);
Result := TCP.LocalAddress;
end;
end;
TCP.Free;
end;
{$endif}
procedure WaitForSDDrive;
begin
while not DirectoryExists ('C:\') do sleep (500);
end;
function USBDriverBind (Device : PUSBDevice; Interrface : PUSBInterface) : LongWord;
var
j : integer;
isHID : boolean;
buff : array [0..255] of byte;
res : LongWord;
begin
Result := USB_STATUS_DEVICE_UNSUPPORTED;
if Device = nil then exit;
if Interrface <> nil then exit;
isHID := false;
if Device^.Descriptor^.bDeviceClass = USB_CLASS_CODE_INTERFACE_SPECIFIC then
begin
for j := low (Device^.Configuration^.Interfaces) to high (Device^.Configuration^.Interfaces) do
if Device^.Configuration^.Interfaces[j]^.Descriptor^.bInterfaceClass = USB_CLASS_CODE_HID then
isHID := true;
end;
ConsoleWindowClear (Console1);
if not isHID then
begin
Log ('Not a HID Device.');
exit;
end;
Log ('VID : ' + Device^.Descriptor^.idVendor.ToHexString (4));
Log ('PID : ' + Device^.Descriptor^.idProduct.ToHexString (4));
Log ('Product : ' + Device^.Product);
Log ('SNo : ' + Device^.SerialNumber);
Log ('Configurations : ' + length (Device^.Configurations).ToString);
Log ('First Configuration ----');
Log (' Interfaces : ' + length (Device^.Configuration^.Interfaces).ToString);
for j := low (Device^.Configuration^.Interfaces) to high (Device^.Configuration^.Interfaces) do
begin
Log (' ---- Interface ' + j.ToString + ' ---- ');
if Device^.Configuration^.Interfaces[j]^.Descriptor^.bInterfaceClass <> USB_CLASS_CODE_HID then continue;
Log (' SubClass : ' + Device^.Configuration^.Interfaces[j]^.Descriptor^.bInterfaceSubClass.ToHexString (2));
Log (' Protocol : ' + Device^.Configuration^.Interfaces[j]^.Descriptor^.bInterfaceProtocol.ToHexString (2));
end;
Log ('Usage ----');
FillChar (buff, 256, 0);
res := USBControlRequestEx (Device, nil,
USB_DEVICE_REQUEST_GET_DESCRIPTOR,
USB_BMREQUESTTYPE_DIR_IN or
USB_BMREQUESTTYPE_TYPE_STANDARD or
USB_BMREQUESTTYPE_RECIPIENT_INTERFACE,
$22 shl 8, // $22 shl 8, // for Report Descriptor
0,
@buff[0],
255,
INFINITE, true);
if res = USB_STATUS_SUCCESS then PrintHIDUsage (buff)
else Log (' Error : ' + USBStatusToString (res));
end;
function USBDriverUnbind (Device : PUSBDevice; Interrface : PUSBInterface) : LongWord;
begin
Result := USB_STATUS_DEVICE_UNSUPPORTED;
end;
begin
Console1 := ConsoleWindowCreate (ConsoleDeviceGetDefault, CONSOLE_POSITION_LEFT, true);
Console2 := ConsoleWindowCreate (ConsoleDeviceGetDefault, CONSOLE_POSITION_TOPRIGHT, false);
Console3 := ConsoleWindowCreate (ConsoleDeviceGetDefault, CONSOLE_POSITION_BOTTOMRIGHT, false);
SetLogProc (@Log1);
Log2 ('HID USB Lister.');
Log2 ('Plug in device to list information.');
Log2 ('');
{$ifdef use_tftp}
IPAddress := WaitForIPComplete;
Log3 ('TFTP Usage : tftp -i ' + IPAddress + ' put kernel7.img');
SetOnMsg (@Msg3);
{$endif}
USBDriver := USBDriverCreate;
if USBDriver = nil then ThreadHalt (0);
USBDriver^.Driver.DriverName := 'GENERIC USB';
USBDriver^.DriverBind := @USBDriverBind;
USBDriver^.DriverUnbind := @USBDriverUnbind;
USBDriverRegister (USBDriver);
ThreadHalt (0);
end.