MCPcopy Create free account
hub / github.com/Atarity/Lightpack / get_usage

Function get_usage

Software/src/hidapi/linux/hid-libusb.c:184–252  ·  view source on GitHub ↗

Retrieves the device's Usage Page and Usage from the report descriptor. The algorithm is simple, as it just returns the first Usage and Usage Page that it finds in the descriptor. The return value is 0 on success and -1 on failure. */

Source from the content-addressed store, hash-verified

182 Usage and Usage Page that it finds in the descriptor.
183 The return value is 0 on success and -1 on failure. */
184static int get_usage(uint8_t *report_descriptor, size_t size,
185 unsigned short *usage_page, unsigned short *usage)
186{
187 int i = 0;
188 int size_code;
189 int data_len, key_size;
190 int usage_found = 0, usage_page_found = 0;
191
192 while (i < size) {
193 int key = report_descriptor[i];
194 int key_cmd = key & 0xfc;
195
196 //printf("key: %02hhx\n", key);
197
198 if ((key & 0xf0) == 0xf0) {
199 /* This is a Long Item. The next byte contains the
200 length of the data section (value) for this key.
201 See the HID specification, version 1.11, section
202 6.2.2.3, titled "Long Items." */
203 if (i+1 < size)
204 data_len = report_descriptor[i+1];
205 else
206 data_len = 0; /* malformed report */
207 key_size = 3;
208 }
209 else {
210 /* This is a Short Item. The bottom two bits of the
211 key contain the size code for the data section
212 (value) for this key. Refer to the HID
213 specification, version 1.11, section 6.2.2.2,
214 titled "Short Items." */
215 size_code = key & 0x3;
216 switch (size_code) {
217 case 0:
218 case 1:
219 case 2:
220 data_len = size_code;
221 break;
222 case 3:
223 data_len = 4;
224 break;
225 default:
226 /* Can't ever happen since size_code is & 0x3 */
227 data_len = 0;
228 break;
229 };
230 key_size = 1;
231 }
232
233 if (key_cmd == 0x4) {
234 *usage_page = get_bytes(report_descriptor, size, data_len, i);
235 usage_page_found = 1;
236 //printf("Usage Page: %x\n", (uint32_t)*usage_page);
237 }
238 if (key_cmd == 0x8) {
239 *usage = get_bytes(report_descriptor, size, data_len, i);
240 usage_found = 1;
241 //printf("Usage: %x\n", (uint32_t)*usage);

Callers 1

hid_enumerateFunction · 0.70

Calls 1

get_bytesFunction · 0.85

Tested by

no test coverage detected