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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253 | -- 64-byte message services using the raw HID services from libhidapi
-- Copyright (C)2018-2023, Philip Munts dba Munts Technologies.
--
-- Redistribution and use in source and binary forms, with or without
-- modification, are permitted provided that the following conditions are met:
--
-- * Redistributions of source code must retain the above copyright notice,
-- this list of conditions and the following disclaimer.
--
-- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
-- AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-- IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-- ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
-- LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
-- CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
-- SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
-- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
-- CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
-- POSSIBILITY OF SUCH DAMAGE.
WITH Ada.Characters.Handling;
WITH System;
WITH Messaging;
USE TYPE System.Address;
PACKAGE BODY HID.hidapi IS
-- Constructor
FUNCTION Create
(vid : HID.Vendor;
pid : HID.Product;
serial : String := "";
timeoutms : Integer := 1000) RETURN Message64.Messenger IS
m : MessengerSubclass;
BEGIN
Initialize(m, vid, pid, serial, timeoutms);
RETURN NEW MessengerSubclass'(m);
END Create;
-- Initializer
PROCEDURE Initialize
(Self : IN OUT MessengerSubclass;
vid : HID.Vendor;
pid : HID.Product;
serial : String := "";
timeoutms : Integer := 1000) IS
handle : System.Address;
-- Convert the serial number from string to wchar_t, as needed for hidapi
wserial : Interfaces.C.wchar_array(0 .. serial'Length) :=
Interfaces.C.To_C(Ada.Characters.Handling.To_Wide_String(serial));
BEGIN
Self.Destroy;
-- Validate parameters
IF serial'Length > 126 THEN
RAISE HID_Error WITH "serial number parameter is too long";
END IF;
IF timeoutms < -1 THEN
RAISE HID_Error WITH "timeoutms parameter is out of range";
END IF;
-- Initialize hidapi library
IF hid_init /= 0 THEN
RAISE HID_Error WITH "hid_init() failed";
END IF;
-- Open device
IF serial = "" THEN
-- hid_open() wants a null pointer rather than an empty string
handle := hid_open(Interfaces.C.unsigned_short(vid),
Interfaces.C.unsigned_short(pid), System.Null_Address);
ELSE
handle := hid_open(Interfaces.C.unsigned_short(vid),
Interfaces.C.unsigned_short(pid), wserial'Address);
END IF;
IF handle = System.Null_Address THEN
RAISE HID_Error WITH "hid_open() failed";
END IF;
Self := MessengerSubclass'(handle, timeoutms);
END Initialize;
-- Destroyer
PROCEDURE Destroy(Self : IN OUT MessengerSubclass) IS
BEGIN
IF Self = Destroyed THEN
RETURN;
END IF;
hid_close(Self.handle);
Self := Destroyed;
END Destroy;
-- Send a message
PROCEDURE Send
(Self : MessengerSubclass;
msg : Message64.Message) IS
outbuf : Messaging.Buffer(0 .. msg'Length);
status : Integer;
BEGIN
Self.CheckDestroyed;
-- Prepend the report ID byte
outbuf(0) := 0;
outbuf(1 .. msg'Length) := msg;
-- Send the report
status := hid_write(Self.handle, outbuf'Address, outbuf'Length);
IF status /= outbuf'Length THEN
RAISE HID_Error WITH "hid_write() failed, status =" &
Integer'Image(status);
END IF;
END Send;
-- Receive a message
PROCEDURE Receive
(Self : MessengerSubclass;
msg : OUT Message64.Message) IS
status : Integer;
inbuf : Messaging.Buffer(0 .. msg'Length);
BEGIN
Self.CheckDestroyed;
status := hid_read_timeout(Self.handle, inbuf'Address, inbuf'Length,
Self.timeout);
-- Handle the various outcomes
IF status = inbuf'Length THEN
msg := inbuf(1 .. msg'Length);
ELSIF status = msg'Length THEN
msg := inbuf(0 .. msg'Length - 1);
ELSIF status = 0 THEN
RAISE Messaging.Timeout_Error WITH "hid_read_timeout() timed out";
ELSE
RAISE HID_Error WITH "hid_read_timeout() failed, status =" &
Integer'Image(status);
END IF;
END Receive;
-- Get HID device name string
FUNCTION Name(Self : MessengerSubclass) RETURN String IS
BEGIN
Self.CheckDestroyed;
RETURN Self.Manufacturer & " " & Self.Product;
END Name;
-- Get HID device manufacturer string
FUNCTION Manufacturer
(Self : MessengerSubclass) RETURN String IS
status : Integer;
buf : Interfaces.C.wchar_array(0 .. 255);
BEGIN
Self.CheckDestroyed;
status := hid_get_manufacturer_string(Self.handle, buf, buf'Length);
IF status /= 0 THEN
RAISE HID_Error WITH "hid_get_manufacturer_string() failed, status =" &
Integer'Image(status);
END IF;
RETURN Ada.Characters.Handling.To_String(Interfaces.C.To_Ada(buf));
END Manufacturer;
-- Get HID device product string
FUNCTION Product
(Self : MessengerSubclass) RETURN String IS
status : Integer;
buf : Interfaces.C.wchar_array(0 .. 255);
BEGIN
Self.CheckDestroyed;
status := hid_get_product_string(Self.handle, buf, buf'Length);
IF status /= 0 THEN
RAISE HID_Error WITH "hid_get_product_string() failed, status =" &
Integer'Image(status);
END IF;
RETURN Ada.Characters.Handling.To_String(Interfaces.C.To_Ada(buf));
END Product;
-- Get HID device serial number string
FUNCTION SerialNumber
(Self : MessengerSubclass) RETURN String IS
status : Integer;
buf : Interfaces.C.wchar_array(0 .. 255);
BEGIN
Self.CheckDestroyed;
status := hid_get_serial_number_string(Self.handle, buf, buf'Length);
IF status /= 0 THEN
RAISE HID_Error WITH "hid_get_serial_number_string() failed, status =" &
Integer'Image(status);
END IF;
RETURN Ada.Characters.Handling.To_String(Interfaces.C.To_Ada(buf));
END SerialNumber;
-- Check whether the HID device has been destroyed
PROCEDURE CheckDestroyed(Self : MessengerSubclass) IS
BEGIN
IF Self = Destroyed THEN
RAISE HID_Error WITH "HID device has been destroyed";
END IF;
END CheckDestroyed;
END HID.hidapi;
|