-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlibusbx_hotplug_test.pp
95 lines (75 loc) · 2.24 KB
/
libusbx_hotplug_test.pp
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
{based on information at http://libusb.sourceforge.net/api-1.0/hotplug.html
tested on Linux CentOS 7}
program libusbx_hotplug_test;
{S. V. Pantazi ([email protected]), 08/27/2019}
{$mode objfpc}{$H+}
uses
heaptrc,{to hunt for memory leaks}
{$IFDEF UNIX}
{$DEFINE UseCThreads}
{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
sysutils,
libusbx,
libusbhid,
hid_testing_utils;
var
count: integer= 0;
function hotplug_callback_test(ctx:Plibusb_context; device: Plibusb_device; event:libusb_hotplug_event; user_data:pointer):longint;
var
libusb_handle:Plibusb_device_handle=nil;
desc:libusb_device_descriptor;
res:integer;
begin
libusb_get_device_descriptor(device, @desc);
case event of
LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED:
begin
WriteLn('hotplug callback - device arrived');
res:= libusb_open(device, @libusb_handle);
if (LIBUSB_SUCCESS <> res) then WriteLn('Could not open USB device')
end;
LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT:
begin
WriteLn('hotplug callback - device left');
if libusb_handle<>nil then
begin
libusb_close(libusb_handle);
libusb_handle:=nil;
end
end;
else WriteLn(Format('Unhandled hotplug event %d',[ event]));
end;
Inc(count);
Result:=0;
end;
procedure libusbhid_device_hotplug_test(vid,pid:word);
var
res:longint;
hotplug_handle:libusb_hotplug_callback_handle;
begin
res:=libusb_init(nil);
if res < LIBUSB_SUCCESS then
begin
{$ifdef DEBUG_MSG}DBG_MSG('Cannot open libusb 1.0 library. You really need this..');{$endif}
end
else
begin
res:=libusb_hotplug_register_callback(nil, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED or LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, {LIBUSB_HOTPLUG_NO_FLAGS}LIBUSB_HOTPLUG_ENUMERATE, vid,pid,-1{=LIBUSB_HOTPLUG_MATCH_ANY}, @hotplug_callback_test, nil,@hotplug_handle);
if res=LIBUSB_SUCCESS then WriteLn('Callback registered');
while (count < 3) do
begin
libusb_handle_events_completed(nil, nil);
WriteLn('hotplug waiting....',count);
Sleep(1000);
end;
libusb_hotplug_deregister_callback(nil, @hotplug_handle);
WriteLn('Callback de-registered');
{we're done, must exit libusb as well}
libusb_exit(nil);
end;
end;
begin
libusbhid_device_hotplug_test($056a, $00de);
end.