-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathuusblibusbimplementation.inc
194 lines (172 loc) · 5.08 KB
/
uusblibusbimplementation.inc
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
{ TUSBControllerAsync }
procedure TUSBControllerAsync.DoCheck;
begin
if (usb_find_busses() > 0)
or (usb_find_devices() > 0) then
FParent.Enumerate;
end;
constructor TUSBControllerAsync.Create(aParent : TUSBController);
begin
FParent := aParent;
inherited Create(False);
end;
procedure TUSBControllerAsync.Execute;
begin
while not Terminated do
begin
Synchronize(@DoCheck);
sleep(2000);
end;
end;
{ TUSBController }
procedure TUSBController.RefreshList;
var
Bus : PUSBBus;
aHostController: TUSBHostController = nil;
i: Integer;
aRootHub: TUSBHub;
procedure EnumHubDevices(Hub : TUSBHub);
var
aDevice,
aChildren: PUSBDevice;
a,i : Integer;
buff : array[0..511] of char;
DevHandle: PUSBDevHandle;
Found: Boolean;
begin
aDevice := Bus^.devices;
a := 0;
for i := 0 to Hub.Count-1 do
TUSBGenericDevice(Hub.Items[i]).Tag := 0;
while Assigned(aDevice) do
begin
Found := False;
for i := 0 to Hub.Count-1 do
if TUSBGenericDevice(Hub.Items[i]).Path = aDevice^.filename then
begin
Found := True;
TUSBGenericDevice(Hub.Items[i]).Tag := 1;
end;
if not Found then
begin
a := Hub.Add(TUSBDevice.Create(aDevice^.filename,Hub,Self,dsConnected));
TUSBDevice(Hub.Items[a]).Tag := 1;
TUSBDevice(Hub.Items[a]).FDeviceID := aDevice^.descriptor.idProduct;
TUSBDevice(Hub.Items[a]).FVendorID := aDevice^.descriptor.idVendor;
DevHandle := usb_open(aDevice);
if Assigned(DevHandle) then
begin
usb_get_string_simple(DevHandle, aDevice^.descriptor.iManufacturer, buff, 512);
TUSBDevice(Hub.Items[a]).FVendor := buff;
usb_get_string_simple(DevHandle, aDevice^.descriptor.iProduct, buff, 512);
TUSBDevice(Hub.Items[a]).FDeviceDescription := buff;
usb_close(DevHandle);
end;
if Assigned(OnUSBArrival) then
OnUSBArrival(TUSBGenericDevice(Hub.Items[a]));
end;
inc(a);
aDevice := aDevice^.next;
end;
i := 0;
while i < Hub.Count do
begin
if TUSBGenericDevice(Hub.Items[i]).Tag = 0 then
begin
TUSBGenericDevice(Hub.Items[i]).Free;
Hub.Delete(i);
end
else inc(i);
end;
{ libusb dont support topologic enumeration
aChildren := @aDevice^.children;
while Hub.Count < aDevice^.num_children do
Hub.Add(nil);
for a := 0 to aDevice^.num_children-1 do
begin
if not Assigned(aChildren) then break;
Showmessage(Hub.Path+'/'+aChildren^.filename);
if aChildren^.num_children = 0 then
begin
Hub.Items[a] := TUSBDevice.Create(Hub.Path+'/'+aChildren^.filename,Hub,Self,dsConnected);
end
else
begin
Hub.Items[a] := TUSBHub.Create(Hub.Path+'/'+aChildren^.filename,Hub,Self,dsConnected);
EnumHubDevices(TUSBHub(Hub.Items[a]));
end;
aChildren := aChildren+sizeof(aChildren);
end;
}
end;
begin
usb_find_busses();
usb_find_devices();
bus := usb_get_busses;
while Assigned(Bus) do
begin
aHostController := nil;
for i := 0 to FBusList.Count-1 do
if TUSBHostController(FBusList[i]).Path = Bus^.dirname then
begin
TUSBHostController(FBusList[i]).Tag := 1;
aHostController := TUSBHostController(FBusList[i]);
end;
if not Assigned(aHostController) then
begin
aHostController := TUSBHostController.Create(Bus^.dirname);
aHostController.Tag := 1;
FBusList.Add(aHostController);
end;
if Assigned(aHostController) then
begin
if (aHostController.Count > 0) then
aRootHub := TUSBHub(aHostController.Devices[0])
else if Bus^.root_dev <> nil then
begin
aRootHub := TUSBHub.Create(Bus^.dirname+'/'+Bus^.root_dev^.filename,nil,Self,dsConnected);
aHostController.Add(aRootHub);
end;
EnumHubDevices(aRootHub);
end;
Bus := Bus^.next;
end;
end;
function TUSBController.GetUSBDeviceClass(VendorID, DeviceID: word
): TUSBDeviceClass;
begin
end;
constructor TUSBController.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FBusList := TList.Create;
FDeviceList := TList.Create;
usb_init();
Async := TUSBControllerAsync.Create(Self);
end;
destructor TUSBController.Destroy;
begin
Async.Free;
FBusList.Free;
FDeviceList.Free;
inherited Destroy;
end;
{ TUSBDevice }
function TUSBDevice.OpenDevice: Boolean;
begin
end;
procedure TUSBDevice.CloseDevice;
begin
end;
constructor TUSBDevice.Create(aDeviceHandle: string;
aParent: TUSBGenericDevice; aController: TUSBController;
aStatus: TUSBDeviceStatus);
begin
inherited Create(aDeviceHandle,aParent,aController,aStatus);
end;
destructor TUSBDevice.Destroy;
begin
if Assigned(Controller.OnUSBRemove) then
Controller.OnUSBRemove(Self);
inherited Destroy;
end;