Instrument Neutral Distributed Interface INDI 2.2.5
hid_libusb.c
Go to the documentation of this file.
1/*
2 HIDAPI - Multi-Platform library for communication with HID devices.
3
4 Copyright (c) 2009 by Alan Ott, Signal 11 Software (8/22/2009)
5 All Rights Reserved.
6
7 Linux Version - 6/2/2010
8 Libusb Version - 8/13/2010
9 FreeBSD Version - 11/1/2011
10
11 Changes for use with SX Filter Wheel INDI Driver by CloudMakers - 11/6/2012
12
13 This program is free software; you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by the Free
15 Software Foundation; either version 2 of the License, or (at your option)
16 any later version.
17
18 This program is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
21 more details.
22
23 You should have received a copy of the GNU General Public License along with
24 this program; if not, write to the Free Software Foundation, Inc., 59
25 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26
27 The full GNU General Public License is included in this distribution in the
28 file called LICENSE.
29
30 These files may also be found in the public source code repository located:
31 http://github.com/signal11/hidapi
32*/
33
34#define _GNU_SOURCE // needed for wcsdup() before glibc 2.10
35
36#include "indi_hidapi.h"
37#include "locale_compat.h"
38
39/* GNU / LibUSB */
40#include <libusb.h>
41#include "iconv.h"
42
43/* C */
44#include <stdio.h>
45#include <string.h>
46#include <stdlib.h>
47#include <ctype.h>
48#include <errno.h>
49
50/* Unix */
51#include <pthread.h>
52
53#ifdef __cplusplus
54extern "C" {
55#endif
56
57#define DEBUG_PRINTF
58
59#ifdef DEBUG_PRINTF
60#define LOG(...) fprintf(stderr, __VA_ARGS__)
61#else
62#define LOG(...) \
63 do \
64 { \
65 } while (0)
66#endif
67
68#if !defined(__FreeBSD__) && !defined(__OpenBSD__)
69#define DETACH_KERNEL_DRIVER
70#endif
71
72/* Uncomment to enable the retrieval of Usage and Usage Page in
73hid_enumerate(). Warning, on platforms different from FreeBSD
74this is very invasive as it requires the detach
75and re-attach of the kernel driver. See comments inside hid_enumerate().
76libusb HIDAPI programs are encouraged to use the interface number
77instead to differentiate between interfaces on a composite HID device. */
78
79// 2018-01-13 JM: It seems that on ARM systems, we need to detach and reattach the kernel driver otherwise we get LIBUSB_ERROR_BUSY
80// So for now we use INVASIVE_GET_USAGE
81#ifdef __arm__
82#define INVASIVE_GET_USAGE
83#endif
84
85/* Linked List of input reports received from the device. */
87{
88 uint8_t *data;
89 size_t len;
91};
92
94{
95 /* Handle to the actual device. */
96 libusb_device_handle *device_handle;
97
98 /* Endpoint information */
102
103 /* The interface number of the HID */
105
106 /* Indexes of Strings */
110
111 /* Whether blocking reads are used */
112 int blocking; /* boolean */
113
114 /* Read thread objects */
115 pthread_t thread;
116 pthread_mutex_t mutex; /* Protects input_reports */
117 pthread_cond_t condition;
118 pthread_barrier_t barrier; /* Ensures correct startup sequence */
120 /* Set by read_callback() when the read transfer is no longer in flight (it was cancelled, the device is
121 gone, or it wasn't resubmitted). hid_close() must not free the transfer before that. */
123 struct libusb_transfer *transfer;
124
125 /* List of received input reports. */
127};
128
129static libusb_context *usb_context = NULL;
130
132static int return_data(hid_device *dev, unsigned char *data, size_t length);
133
134static hid_device *new_hid_device(void)
135{
136 hid_device *dev = calloc(1, sizeof(hid_device));
137 dev->blocking = 1;
138
139 pthread_mutex_init(&dev->mutex, NULL);
140 pthread_cond_init(&dev->condition, NULL);
141 pthread_barrier_init(&dev->barrier, NULL, 2);
142
143 return dev;
144}
145
146static void free_hid_device(hid_device *dev)
147{
148 /* Clean up the thread objects */
149 pthread_barrier_destroy(&dev->barrier);
150 pthread_cond_destroy(&dev->condition);
151 pthread_mutex_destroy(&dev->mutex);
152
153 /* Free the device itself */
154 free(dev);
155}
156
157#if 0
158//TODO: Implement this function on hidapi/libusb..
159static void register_error(hid_device * device, const char * op)
160{
161
162}
163#endif
164
165#ifdef INVASIVE_GET_USAGE
166/* Get bytes from a HID Report Descriptor.
167 Only call with a num_bytes of 0, 1, 2, or 4. */
168static uint32_t get_bytes(uint8_t *rpt, size_t len, size_t num_bytes, size_t cur)
169{
170 /* Return if there aren't enough bytes. */
171 if (cur + num_bytes >= len)
172 return 0;
173
174 if (num_bytes == 0)
175 return 0;
176 else if (num_bytes == 1)
177 {
178 return rpt[cur + 1];
179 }
180 else if (num_bytes == 2)
181 {
182 return (rpt[cur + 2] * 256 + rpt[cur + 1]);
183 }
184 else if (num_bytes == 4)
185 {
186 return (rpt[cur + 4] * 0x01000000 + rpt[cur + 3] * 0x00010000 + rpt[cur + 2] * 0x00000100 +
187 rpt[cur + 1] * 0x00000001);
188 }
189 else
190 return 0;
191}
192
193/* Retrieves the device's Usage Page and Usage from the report
194 descriptor. The algorithm is simple, as it just returns the first
195 Usage and Usage Page that it finds in the descriptor.
196 The return value is 0 on success and -1 on failure. */
197static int get_usage(uint8_t *report_descriptor, size_t size, unsigned short *usage_page, unsigned short *usage)
198{
199 size_t i = 0;
200 int size_code;
201 int data_len, key_size;
202 int usage_found = 0, usage_page_found = 0;
203
204 while (i < size)
205 {
206 int key = report_descriptor[i];
207 int key_cmd = key & 0xfc;
208
209 //printf("key: %02hhx\n", key);
210
211 if ((key & 0xf0) == 0xf0)
212 {
213 /* This is a Long Item. The next byte contains the
214 length of the data section (value) for this key.
215 See the HID specification, version 1.11, section
216 6.2.2.3, titled "Long Items." */
217 if (i + 1 < size)
218 data_len = report_descriptor[i + 1];
219 else
220 data_len = 0; /* malformed report */
221 key_size = 3;
222 }
223 else
224 {
225 /* This is a Short Item. The bottom two bits of the
226 key contain the size code for the data section
227 (value) for this key. Refer to the HID
228 specification, version 1.11, section 6.2.2.2,
229 titled "Short Items." */
230 size_code = key & 0x3;
231 switch (size_code)
232 {
233 case 0:
234 case 1:
235 case 2:
236 data_len = size_code;
237 break;
238 case 3:
239 data_len = 4;
240 break;
241 default:
242 /* Can't ever happen since size_code is & 0x3 */
243 data_len = 0;
244 break;
245 };
246 key_size = 1;
247 }
248
249 if (key_cmd == 0x4)
250 {
251 *usage_page = get_bytes(report_descriptor, size, data_len, i);
252 usage_page_found = 1;
253 //printf("Usage Page: %x\n", (uint32_t)*usage_page);
254 }
255 if (key_cmd == 0x8)
256 {
257 *usage = get_bytes(report_descriptor, size, data_len, i);
258 usage_found = 1;
259 //printf("Usage: %x\n", (uint32_t)*usage);
260 }
261
262 if (usage_page_found && usage_found)
263 return 0; /* success */
264
265 /* Skip over this key and it's associated data */
266 i += data_len + key_size;
267 }
268
269 return -1; /* failure */
270}
271#endif // INVASIVE_GET_USAGE
272
273/* Get the first language the device says it reports. This comes from
274 USB string #0. */
275static uint16_t get_first_language(libusb_device_handle *dev)
276{
277 uint16_t buf[32];
278 int len;
279
280 /* Get the string from libusb. */
281 len = libusb_get_string_descriptor(dev, 0x0, /* String ID */
282 0x0, /* Language */
283 (unsigned char *)buf, sizeof(buf));
284 if (len < 4)
285 return 0x0;
286
287 return buf[1]; // First two bytes are len and descriptor type.
288}
289
290static int is_language_supported(libusb_device_handle *dev, uint16_t lang)
291{
292 uint16_t buf[32];
293 int len;
294 int i;
295
296 /* Get the string from libusb. */
297 len = libusb_get_string_descriptor(dev, 0x0, /* String ID */
298 0x0, /* Language */
299 (unsigned char *)buf, sizeof(buf));
300 if (len < 4)
301 return 0x0;
302
303 len /= 2; /* language IDs are two-bytes each. */
304 /* Start at index 1 because there are two bytes of protocol data. */
305 for (i = 1; i < len; i++)
306 {
307 if (buf[i] == lang)
308 return 1;
309 }
310
311 return 0;
312}
313
314/* This function returns a newly allocated wide string containing the USB
315 device string numbered by the index. The returned string must be freed
316 by using free(). */
317static wchar_t *get_usb_string(libusb_device_handle *dev, uint8_t idx)
318{
319 char buf[512];
320 int len;
321 wchar_t *str = NULL;
322 wchar_t wbuf[256];
323
324 /* iconv variables */
325 iconv_t ic;
326 size_t inbytes;
327 size_t outbytes;
328 size_t res;
329 char *inptr;
330 char *outptr;
331
332 /* Determine which language to use. */
333 uint16_t lang;
335 if (!is_language_supported(dev, lang))
336 lang = get_first_language(dev);
337
338 /* Get the string from libusb. */
339 len = libusb_get_string_descriptor(dev, idx, lang, (unsigned char *)buf, sizeof(buf));
340 if (len < 0)
341 return NULL;
342
343 /* buf does not need to be explicitly NULL-terminated because
344 it is only passed into iconv() which does not need it. */
345
346 /* Initialize iconv. */
347 ic = iconv_open("WCHAR_T", "UTF-16LE");
348 if (ic == (iconv_t)-1)
349 {
350 LOG("iconv_open() failed\n");
351 return NULL;
352 }
353
354 /* Convert to native wchar_t (UTF-32 on glibc/BSD systems).
355 Skip the first character (2-bytes). */
356 inptr = buf + 2;
357 inbytes = len - 2;
358 outptr = (char *)wbuf;
359 outbytes = sizeof(wbuf);
360 res = iconv(ic, &inptr, &inbytes, &outptr, &outbytes);
361 if (res == (size_t)-1)
362 {
363 LOG("iconv() failed\n");
364 goto err;
365 }
366
367 /* Write the terminating NULL. */
368 wbuf[sizeof(wbuf) / sizeof(wbuf[0]) - 1] = 0x00000000;
369 if (outbytes >= sizeof(wbuf[0]))
370 *((wchar_t *)outptr) = 0x00000000;
371
372 /* Allocate and copy the string. */
373 str = wcsdup(wbuf);
374
375err:
376 iconv_close(ic);
377
378 return str;
379}
380
381static char *make_path(libusb_device *dev, int interface_number)
382{
383 char str[64];
384 snprintf(str, sizeof(str), "%04x:%04x:%02x", libusb_get_bus_number(dev), libusb_get_device_address(dev),
385 interface_number);
386 str[sizeof(str) - 1] = '\0';
387
388 return strdup(str);
389}
390
392{
393 if (!usb_context)
394 {
395 locale_char_t *locale;
396
397 /* Init Libusb */
398 if (libusb_init(&usb_context))
399 return -1;
400
401 /* Set the locale if it's not set. */
402 locale = indi_setlocale(LC_CTYPE, NULL);
403 if (!locale)
404 indi_setlocale(LC_CTYPE, INDI_LOCALE(""));
405 }
406
407 return 0;
408}
409
411{
412 if (usb_context)
413 {
414 libusb_exit(usb_context);
415 usb_context = NULL;
416 }
417
418 return 0;
419}
420
421struct hid_device_info HID_API_EXPORT *hid_enumerate(unsigned short vendor_id, unsigned short product_id)
422{
423 libusb_device **devs;
424 libusb_device *dev;
425 libusb_device_handle *handle;
426 ssize_t num_devs;
427 int i = 0;
428
429 LOG("Searching for HID Device VID: %#04x PID: %#04x\n", vendor_id, product_id);
430
431 struct hid_device_info *root = NULL; // return object
432 struct hid_device_info *cur_dev = NULL;
433
434 hid_init();
435
436 num_devs = libusb_get_device_list(usb_context, &devs);
437 if (num_devs < 0)
438 return NULL;
439
440 while ((dev = devs[i++]) != NULL)
441 {
442 struct libusb_device_descriptor desc;
443 struct libusb_config_descriptor *conf_desc = NULL;
444 int j, k;
445 int interface_num = 0;
446
447 int res = libusb_get_device_descriptor(dev, &desc);
448 unsigned short dev_vid = desc.idVendor;
449 unsigned short dev_pid = desc.idProduct;
450
451 /* HID's are defined at the interface level. */
452 if (desc.bDeviceClass != LIBUSB_CLASS_PER_INTERFACE)
453 continue;
454
455 res = libusb_get_active_config_descriptor(dev, &conf_desc);
456 if (res < 0)
457 libusb_get_config_descriptor(dev, 0, &conf_desc);
458 if (conf_desc)
459 {
460 for (j = 0; j < conf_desc->bNumInterfaces; j++)
461 {
462 const struct libusb_interface *intf = &conf_desc->interface[j];
463 for (k = 0; k < intf->num_altsetting; k++)
464 {
465 const struct libusb_interface_descriptor *intf_desc;
466 intf_desc = &intf->altsetting[k];
467 if (intf_desc->bInterfaceClass == LIBUSB_CLASS_HID)
468 {
469 interface_num = intf_desc->bInterfaceNumber;
470
471 /* Check the VID/PID against the arguments */
472 if ((vendor_id == 0x0 && product_id == 0x0) || (vendor_id == dev_vid && product_id == dev_pid))
473 {
474 struct hid_device_info *tmp;
475
476 /* VID/PID match. Create the record. */
477 tmp = calloc(1, sizeof(struct hid_device_info));
478 if (cur_dev)
479 {
480 cur_dev->next = tmp;
481 }
482 else
483 {
484 root = tmp;
485 }
486 cur_dev = tmp;
487
488 /* Fill out the record */
489 cur_dev->next = NULL;
490 cur_dev->path = make_path(dev, interface_num);
491
492 res = libusb_open(dev, &handle);
493
494 if (res >= 0)
495 {
496 /* Serial Number */
497 if (desc.iSerialNumber > 0)
498 cur_dev->serial_number = get_usb_string(handle, desc.iSerialNumber);
499
500 /* Manufacturer and Product strings */
501 if (desc.iManufacturer > 0)
502 cur_dev->manufacturer_string = get_usb_string(handle, desc.iManufacturer);
503 if (desc.iProduct > 0)
504 cur_dev->product_string = get_usb_string(handle, desc.iProduct);
505
506#ifdef INVASIVE_GET_USAGE
507 /*
508 This section is removed because it is too
509 invasive on the system. Getting a Usage Page
510 and Usage requires parsing the HID Report
511 descriptor. Getting a HID Report descriptor
512 involves claiming the interface. Claiming the
513 interface involves detaching the kernel driver.
514 Detaching the kernel driver is hard on the system
515 because it will unclaim interfaces (if another
516 app has them claimed) and the re-attachment of
517 the driver will sometimes change /dev entry names.
518 It is for these reasons that this section is
519 #if 0. For composite devices, use the interface
520 field in the hid_device_info struct to distinguish
521 between interfaces. */
522 unsigned char data[256];
523#ifdef DETACH_KERNEL_DRIVER
524 int detached = 0;
525 /* Usage Page and Usage */
526 res = libusb_kernel_driver_active(handle, interface_num);
527 if (res == 1)
528 {
529 res = libusb_detach_kernel_driver(handle, interface_num);
530 if (res < 0)
531 LOG("Couldn't detach kernel driver, even though a kernel driver was attached.");
532 else
533 detached = 1;
534 }
535#endif
536 res = libusb_claim_interface(handle, interface_num);
537 if (res >= 0)
538 {
539 /* Get the HID Report Descriptor. */
540 res = libusb_control_transfer(
541 handle, LIBUSB_ENDPOINT_IN | LIBUSB_RECIPIENT_INTERFACE,
542 LIBUSB_REQUEST_GET_DESCRIPTOR, (LIBUSB_DT_REPORT << 8) | interface_num, 0, data,
543 sizeof(data), 5000);
544 if (res >= 0)
545 {
546 unsigned short page = 0, usage = 0;
547 /* Parse the usage and usage page
548 out of the report descriptor. */
549 get_usage(data, res, &page, &usage);
550 cur_dev->usage_page = page;
551 cur_dev->usage = usage;
552 }
553 else
554 LOG("libusb_control_transfer() for getting the HID report failed with %d\n",
555 res);
556
557 /* Release the interface */
558 res = libusb_release_interface(handle, interface_num);
559 if (res < 0)
560 LOG("Can't release the interface.\n");
561 }
562 else
563 LOG("Can't claim interface %d\n", res);
564#ifdef DETACH_KERNEL_DRIVER
565 /* Re-attach kernel driver if necessary. */
566 if (detached)
567 {
568 res = libusb_attach_kernel_driver(handle, interface_num);
569 if (res < 0)
570 LOG("Couldn't re-attach kernel driver.\n");
571 }
572#endif
573
574#endif // INVASIVE_GET_USAGE
575
576 libusb_close(handle);
577 }
578 /* VID/PID */
579 cur_dev->vendor_id = dev_vid;
580 cur_dev->product_id = dev_pid;
581
582 /* Release Number */
583 cur_dev->release_number = desc.bcdDevice;
584
585 /* Interface Number */
586 cur_dev->interface_number = interface_num;
587 }
588 }
589 } /* altsettings */
590 } /* interfaces */
591 libusb_free_config_descriptor(conf_desc);
592 }
593 }
594
595 libusb_free_device_list(devs, 1);
596
597 return root;
598}
599
601{
602 struct hid_device_info *d = devs;
603 while (d)
604 {
605 struct hid_device_info *next = d->next;
606 free(d->path);
607 free(d->serial_number);
608 free(d->manufacturer_string);
609 free(d->product_string);
610 free(d);
611 d = next;
612 }
613}
614
615hid_device *hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
616{
617 struct hid_device_info *devs, *cur_dev;
618 const char *path_to_open = NULL;
619 hid_device *handle = NULL;
620
622 cur_dev = devs;
623 while (cur_dev)
624 {
625 if (cur_dev->vendor_id == vendor_id && cur_dev->product_id == product_id)
626 {
627 if (serial_number)
628 {
629 if (wcscmp(serial_number, cur_dev->serial_number) == 0)
630 {
631 path_to_open = cur_dev->path;
632 break;
633 }
634 }
635 else
636 {
637 path_to_open = cur_dev->path;
638 break;
639 }
640 }
641 cur_dev = cur_dev->next;
642 }
643
644 if (path_to_open)
645 {
646 /* Open the device */
647 handle = hid_open_path(path_to_open);
648 }
649
651
652 return handle;
653}
654
655static void read_callback(struct libusb_transfer *transfer)
656{
657 hid_device *dev = transfer->user_data;
658 int res=0;
659
660 if (transfer->status == LIBUSB_TRANSFER_COMPLETED)
661 {
662 struct input_report *rpt = malloc(sizeof(*rpt));
663 rpt->data = malloc(transfer->actual_length);
664 memcpy(rpt->data, transfer->buffer, transfer->actual_length);
665 rpt->len = transfer->actual_length;
666 rpt->next = NULL;
667
668 pthread_mutex_lock(&dev->mutex);
669
670 /* Attach the new report object to the end of the list. */
671 if (dev->input_reports == NULL)
672 {
673 /* The list is empty. Put it at the root. */
674 dev->input_reports = rpt;
675 pthread_cond_signal(&dev->condition);
676 }
677 else
678 {
679 /* Find the end of the list and attach. */
680 struct input_report *cur = dev->input_reports;
681 int num_queued = 0;
682 while (cur->next != NULL)
683 {
684 cur = cur->next;
685 num_queued++;
686 }
687 cur->next = rpt;
688
689 /* Pop one off if we've reached 30 in the queue. This
690 way we don't grow forever if the user never reads
691 anything from the device. */
692 if (num_queued > 30)
693 {
694 return_data(dev, NULL, 0);
695 }
696 }
697 pthread_mutex_unlock(&dev->mutex);
698 }
699 else if (transfer->status == LIBUSB_TRANSFER_CANCELLED)
700 {
701 dev->shutdown_thread = 1;
702 dev->transfer_loop_finished = 1;
703 return;
704 }
705 else if (transfer->status == LIBUSB_TRANSFER_NO_DEVICE)
706 {
707 dev->shutdown_thread = 1;
708 dev->transfer_loop_finished = 1;
709 return;
710 }
711 else if (transfer->status == LIBUSB_TRANSFER_TIMED_OUT)
712 {
713 //LOG("Timeout (normal)\n");
714 }
715 else
716 {
717 LOG("Unknown transfer code: #%d %s\n", transfer->status, libusb_error_name(res));
718 }
719
720 /* Don't re-submit once hid_close() asked the read thread to stop: the transfer would be in flight again
721 while hid_close() frees it. */
722 if (dev->shutdown_thread)
723 {
724 dev->transfer_loop_finished = 1;
725 return;
726 }
727
728 /* Re-submit the transfer object. */
729 res = libusb_submit_transfer(transfer);
730 if (res != 0)
731 {
732 LOG("Unable to submit URB. libusb error code: #%d %s\n", res, libusb_error_name(res));
733 dev->shutdown_thread = 1;
734 dev->transfer_loop_finished = 1;
735 }
736}
737
738static void *read_thread(void *param)
739{
740 hid_device *dev = param;
741 unsigned char *buf;
742 const size_t length = dev->input_ep_max_packet_size;
743
744 /* Set up the transfer object. */
745 buf = malloc(length);
746 dev->transfer = libusb_alloc_transfer(0);
747 libusb_fill_interrupt_transfer(dev->transfer, dev->device_handle, dev->input_endpoint, buf, length, read_callback,
748 dev, 5000 /*timeout*/);
749
750 /* Make the first submission. Further submissions are made
751 from inside read_callback() */
752 if (libusb_submit_transfer(dev->transfer) != 0)
753 {
754 /* Nothing in flight, so there is no callback to wait for when the thread stops. */
755 dev->transfer_loop_finished = 1;
756 }
757
758 // Notify the main thread that the read thread is up and running.
759 pthread_barrier_wait(&dev->barrier);
760
761 /* Handle all the events. */
762 while (!dev->shutdown_thread)
763 {
764 int res;
765 res = libusb_handle_events(usb_context);
766 if (res < 0)
767 {
768 /* There was an error. */
769 LOG("read_thread(): libusb reports error #%d %s\n", res, libusb_error_name(res));
770
771 /* Break out of this loop only on fatal error.*/
772 if (res != LIBUSB_ERROR_BUSY && res != LIBUSB_ERROR_TIMEOUT && res != LIBUSB_ERROR_OVERFLOW &&
773 res != LIBUSB_ERROR_INTERRUPTED)
774 {
775 break;
776 }
777 }
778 }
779
780 /* Cancel any transfer that may be pending. This call fails if no transfer is pending, or if hid_close()
781 already cancelled it, but that's OK. Then wait until the transfer is really no longer in flight: its
782 callback reports that. A single libusb_handle_events() call wasn't enough, and it was skipped entirely
783 when hid_close() had cancelled first, so hid_close() could free a transfer that libusb still had in its
784 list of transfers in flight, and libusb_close() then aborted on the freed transfer's mutex. */
785 libusb_cancel_transfer(dev->transfer);
786 while (!dev->transfer_loop_finished)
787 libusb_handle_events_completed(usb_context, &dev->transfer_loop_finished);
788
789 /* Now that the read thread is stopping, Wake any threads which are
790 waiting on data (in hid_read_timeout()). Do this under a mutex to
791 make sure that a thread which is about to go to sleep waiting on
792 the condition actually will go to sleep before the condition is
793 signaled. */
794 pthread_mutex_lock(&dev->mutex);
795 pthread_cond_broadcast(&dev->condition);
796 pthread_mutex_unlock(&dev->mutex);
797
798 /* The dev->transfer->buffer and dev->transfer objects are cleaned up
799 in hid_close(). They are not cleaned up here because this thread
800 could end either due to a disconnect or due to a user
801 call to hid_close(). In both cases the objects can be safely
802 cleaned up after the call to pthread_join() (in hid_close()), but
803 since hid_close() calls libusb_cancel_transfer(), on these objects,
804 they can not be cleaned up here. */
805
806 return NULL;
807}
808
810{
811 hid_device *dev = NULL;
812
813 dev = new_hid_device();
814
815 libusb_device **devs;
816 libusb_device *usb_dev;
817 ssize_t num_devs;
818 int res;
819 int d = 0;
820 int good_open = 0;
821
822 hid_init();
823
824 num_devs = libusb_get_device_list(usb_context, &devs);
825 if (num_devs < 0)
826 return NULL;
827
828 while ((usb_dev = devs[d++]) != NULL)
829 {
830 struct libusb_device_descriptor desc;
831 struct libusb_config_descriptor *conf_desc = NULL;
832 int i, j, k;
833 libusb_get_device_descriptor(usb_dev, &desc);
834
835 if (libusb_get_active_config_descriptor(usb_dev, &conf_desc) < 0)
836 continue;
837 for (j = 0; j < conf_desc->bNumInterfaces; j++)
838 {
839 const struct libusb_interface *intf = &conf_desc->interface[j];
840 for (k = 0; k < intf->num_altsetting; k++)
841 {
842 const struct libusb_interface_descriptor *intf_desc;
843 intf_desc = &intf->altsetting[k];
844 if (intf_desc->bInterfaceClass == LIBUSB_CLASS_HID)
845 {
846 char *dev_path = make_path(usb_dev, intf_desc->bInterfaceNumber);
847 if (!strcmp(dev_path, path))
848 {
849 /* Matched Paths. Open this device */
850
851 // OPEN HERE //
852 res = libusb_open(usb_dev, &dev->device_handle);
853 if (res < 0)
854 {
855 LOG("can't open device: %s\n", libusb_error_name(res));
856 free(dev_path);
857 break;
858 }
859 good_open = 1;
860#ifdef DETACH_KERNEL_DRIVER
861 /* Detach the kernel driver, but only if the
862 device is managed by the kernel */
863 if (libusb_kernel_driver_active(dev->device_handle, intf_desc->bInterfaceNumber) == 1)
864 {
865 res = libusb_detach_kernel_driver(dev->device_handle, intf_desc->bInterfaceNumber);
866 if (res < 0)
867 {
868 libusb_close(dev->device_handle);
869 LOG("Unable to detach Kernel Driver: %s\n", libusb_error_name(res));
870 free(dev_path);
871 good_open = 0;
872 break;
873 }
874 }
875#endif
876 res = libusb_claim_interface(dev->device_handle, intf_desc->bInterfaceNumber);
877 if (res < 0)
878 {
879 LOG("can't claim interface %d: %d %s\n", intf_desc->bInterfaceNumber, res, libusb_error_name(res));
880 free(dev_path);
881 libusb_close(dev->device_handle);
882 good_open = 0;
883 break;
884 }
885
886 /* Store off the string descriptor indexes */
887 dev->manufacturer_index = desc.iManufacturer;
888 dev->product_index = desc.iProduct;
889 dev->serial_index = desc.iSerialNumber;
890
891 /* Store off the interface number */
892 dev->interface = intf_desc->bInterfaceNumber;
893
894 /* Find the INPUT and OUTPUT endpoints. An
895 OUTPUT endpoint is not required. */
896 for (i = 0; i < intf_desc->bNumEndpoints; i++)
897 {
898 const struct libusb_endpoint_descriptor *ep = &intf_desc->endpoint[i];
899
900 /* Determine the type and direction of this
901 endpoint. */
902 int is_interrupt =
903 (ep->bmAttributes & LIBUSB_TRANSFER_TYPE_MASK) == LIBUSB_TRANSFER_TYPE_INTERRUPT;
904 int is_output = (ep->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_OUT;
905 int is_input = (ep->bEndpointAddress & LIBUSB_ENDPOINT_DIR_MASK) == LIBUSB_ENDPOINT_IN;
906
907 /* Decide whether to use it for input or output. */
908 if (dev->input_endpoint == 0 && is_interrupt && is_input)
909 {
910 /* Use this endpoint for INPUT */
911 dev->input_endpoint = ep->bEndpointAddress;
912 dev->input_ep_max_packet_size = ep->wMaxPacketSize;
913 }
914 if (dev->output_endpoint == 0 && is_interrupt && is_output)
915 {
916 /* Use this endpoint for OUTPUT */
917 dev->output_endpoint = ep->bEndpointAddress;
918 }
919 }
920
921 pthread_create(&dev->thread, NULL, read_thread, dev);
922
923 // Wait here for the read thread to be initialized.
924 pthread_barrier_wait(&dev->barrier);
925 }
926 free(dev_path);
927 }
928 }
929 }
930 libusb_free_config_descriptor(conf_desc);
931 }
932
933 libusb_free_device_list(devs, 1);
934
935 // If we have a good handle, return it.
936 if (good_open)
937 {
938 return dev;
939 }
940 else
941 {
942 // Unable to open any devices.
943 free_hid_device(dev);
944 return NULL;
945 }
946}
947
948int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t length)
949{
950 int res;
951 int report_number = data[0];
952 int skipped_report_id = 0;
953
954 if (report_number == 0x0)
955 {
956 data++;
957 length--;
958 skipped_report_id = 1;
959 }
960
961 if (dev->output_endpoint <= 0)
962 {
963 /* No interrupt out endpoint. Use the Control Endpoint */
964 res = libusb_control_transfer(dev->device_handle,
965 LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE | LIBUSB_ENDPOINT_OUT,
966 0x09 /*HID Set_Report*/, (2 /*HID output*/ << 8) | report_number, dev->interface,
967 (unsigned char *)data, length, 1000 /*timeout millis*/);
968
969 if (res < 0)
970 return -1;
971
972 if (skipped_report_id)
973 length++;
974
975 return length;
976 }
977 else
978 {
979 /* Use the interrupt out endpoint */
980 int actual_length;
981 res = libusb_interrupt_transfer(dev->device_handle, dev->output_endpoint, (unsigned char *)data, length,
982 &actual_length, 1000);
983
984 if (res < 0)
985 return -1;
986
987 if (skipped_report_id)
988 actual_length++;
989
990 return actual_length;
991 }
992}
993
994/* Helper function, to simplify hid_read().
995 This should be called with dev->mutex locked. */
996static int return_data(hid_device *dev, unsigned char *data, size_t length)
997{
998 /* Copy the data out of the linked list item (rpt) into the
999 return buffer (data), and delete the liked list item. */
1000 struct input_report *rpt = dev->input_reports;
1001 size_t len = (length < rpt->len) ? length : rpt->len;
1002 if (len > 0)
1003 memcpy(data, rpt->data, len);
1004 dev->input_reports = rpt->next;
1005 free(rpt->data);
1006 free(rpt);
1007 return len;
1008}
1009
1010static void cleanup_mutex(void *param)
1011{
1012 hid_device *dev = param;
1013 pthread_mutex_unlock(&dev->mutex);
1014}
1015
1016int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
1017{
1018 volatile int bytes_read = 0;
1019
1020#if 0
1021 int transferred;
1022 int res = libusb_interrupt_transfer(dev->device_handle, dev->input_endpoint, data, length, &transferred, 5000);
1023 LOG("transferred: %d\n", transferred);
1024 return transferred;
1025#endif
1026
1027 pthread_mutex_lock(&dev->mutex);
1028 pthread_cleanup_push(&cleanup_mutex, dev);
1029
1030 /* There's an input report queued up. Return it. */
1031 if (dev->input_reports)
1032 {
1033 /* Return the first one */
1034 bytes_read = return_data(dev, data, length);
1035 goto ret;
1036 }
1037
1038 if (dev->shutdown_thread)
1039 {
1040 /* This means the device has been disconnected.
1041 An error code of -1 should be returned. */
1042 bytes_read = -1;
1043 goto ret;
1044 }
1045
1046 if (milliseconds == -1)
1047 {
1048 /* Blocking */
1049 while (!dev->input_reports && !dev->shutdown_thread)
1050 {
1051 pthread_cond_wait(&dev->condition, &dev->mutex);
1052 }
1053 if (dev->input_reports)
1054 {
1055 bytes_read = return_data(dev, data, length);
1056 }
1057 }
1058 else if (milliseconds > 0)
1059 {
1060 /* Non-blocking, but called with timeout. */
1061 int res;
1062 struct timespec ts;
1063 clock_gettime(CLOCK_REALTIME, &ts);
1064 ts.tv_sec += milliseconds / 1000;
1065 ts.tv_nsec += (milliseconds % 1000) * 1000000;
1066 if (ts.tv_nsec >= 1000000000L)
1067 {
1068 ts.tv_sec++;
1069 ts.tv_nsec -= 1000000000L;
1070 }
1071
1072 while (!dev->input_reports && !dev->shutdown_thread)
1073 {
1074 res = pthread_cond_timedwait(&dev->condition, &dev->mutex, &ts);
1075 if (res == 0)
1076 {
1077 if (dev->input_reports)
1078 {
1079 bytes_read = return_data(dev, data, length);
1080 break;
1081 }
1082
1083 /* If we're here, there was a spurious wake up
1084 or the read thread was shutdown. Run the
1085 loop again (ie: don't break). */
1086 }
1087 else if (res == ETIMEDOUT)
1088 {
1089 /* Timed out. */
1090 bytes_read = 0;
1091 break;
1092 }
1093 else
1094 {
1095 /* Error. */
1096 bytes_read = -1;
1097 break;
1098 }
1099 }
1100 }
1101 else
1102 {
1103 /* Purely non-blocking */
1104 bytes_read = 0;
1105 }
1106
1107ret:
1108 pthread_mutex_unlock(&dev->mutex);
1109 pthread_cleanup_pop(0);
1110
1111 return bytes_read;
1112}
1113
1114int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
1115{
1116 return hid_read_timeout(dev, data, length, dev->blocking ? -1 : 0);
1117}
1118
1120{
1121 dev->blocking = !nonblock;
1122
1123 return 0;
1124}
1125
1126int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char *data, size_t length)
1127{
1128 int res = -1;
1129 int skipped_report_id = 0;
1130 int report_number = data[0];
1131
1132 if (report_number == 0x0)
1133 {
1134 data++;
1135 length--;
1136 skipped_report_id = 1;
1137 }
1138
1139 res = libusb_control_transfer(dev->device_handle,
1140 LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE | LIBUSB_ENDPOINT_OUT,
1141 0x09 /*HID set_report*/, (3 /*HID feature*/ << 8) | report_number, dev->interface,
1142 (unsigned char *)data, length, 1000 /*timeout millis*/);
1143
1144 if (res < 0)
1145 return -1;
1146
1147 /* Account for the report ID */
1148 if (skipped_report_id)
1149 length++;
1150
1151 return length;
1152}
1153
1154int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length)
1155{
1156 int res = -1;
1157 int skipped_report_id = 0;
1158 int report_number = data[0];
1159
1160 if (report_number == 0x0)
1161 {
1162 /* Offset the return buffer by 1, so that the report ID
1163 will remain in byte 0. */
1164 data++;
1165 length--;
1166 skipped_report_id = 1;
1167 }
1168 res = libusb_control_transfer(dev->device_handle,
1169 LIBUSB_REQUEST_TYPE_CLASS | LIBUSB_RECIPIENT_INTERFACE | LIBUSB_ENDPOINT_IN,
1170 0x01 /*HID get_report*/, (3 /*HID feature*/ << 8) | report_number, dev->interface,
1171 (unsigned char *)data, length, 1000 /*timeout millis*/);
1172
1173 if (res < 0)
1174 return -1;
1175
1176 if (skipped_report_id)
1177 res++;
1178
1179 return res;
1180}
1181
1183{
1184 if (!dev)
1185 return;
1186
1187 /* Cause read_thread() to stop. */
1188 dev->shutdown_thread = 1;
1189 libusb_cancel_transfer(dev->transfer);
1190
1191 /* Wait for read_thread() to end. */
1192 pthread_join(dev->thread, NULL);
1193
1194 /* Clean up the Transfer objects allocated in read_thread(). */
1195 free(dev->transfer->buffer);
1196 libusb_free_transfer(dev->transfer);
1197
1198 /* release the interface */
1199 libusb_release_interface(dev->device_handle, dev->interface);
1200
1201 /* Close the handle */
1202 libusb_close(dev->device_handle);
1203
1204 /* Clear out the queue of received reports. */
1205 pthread_mutex_lock(&dev->mutex);
1206 while (dev->input_reports)
1207 {
1208 return_data(dev, NULL, 0);
1209 }
1210 pthread_mutex_unlock(&dev->mutex);
1211
1212 free_hid_device(dev);
1213}
1214
1215int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *dev, wchar_t *string, size_t maxlen)
1216{
1217 return hid_get_indexed_string(dev, dev->manufacturer_index, string, maxlen);
1218}
1219
1220int HID_API_EXPORT_CALL hid_get_product_string(hid_device *dev, wchar_t *string, size_t maxlen)
1221{
1222 return hid_get_indexed_string(dev, dev->product_index, string, maxlen);
1223}
1224
1225int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *string, size_t maxlen)
1226{
1227 return hid_get_indexed_string(dev, dev->serial_index, string, maxlen);
1228}
1229
1230int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index, wchar_t *string, size_t maxlen)
1231{
1232 wchar_t *str;
1233
1234 str = get_usb_string(dev->device_handle, string_index);
1235 if (str)
1236 {
1237 wcsncpy(string, str, maxlen);
1238 string[maxlen - 1] = L'\0';
1239 free(str);
1240 return 0;
1241 }
1242 else
1243 return -1;
1244}
1245
1247{
1248 (void)dev;
1249 return NULL;
1250}
1251
1253{
1254 const char *name;
1255 const char *string_code;
1256 uint16_t usb_code;
1257};
1258
1259#define LANG(name, code, usb_code) \
1260 { \
1261 name, code, usb_code \
1262 }
1263static struct lang_map_entry lang_map[] = {
1264 LANG("Afrikaans", "af", 0x0436),
1265 LANG("Albanian", "sq", 0x041C),
1266 LANG("Arabic - United Arab Emirates", "ar_ae", 0x3801),
1267 LANG("Arabic - Bahrain", "ar_bh", 0x3C01),
1268 LANG("Arabic - Algeria", "ar_dz", 0x1401),
1269 LANG("Arabic - Egypt", "ar_eg", 0x0C01),
1270 LANG("Arabic - Iraq", "ar_iq", 0x0801),
1271 LANG("Arabic - Jordan", "ar_jo", 0x2C01),
1272 LANG("Arabic - Kuwait", "ar_kw", 0x3401),
1273 LANG("Arabic - Lebanon", "ar_lb", 0x3001),
1274 LANG("Arabic - Libya", "ar_ly", 0x1001),
1275 LANG("Arabic - Morocco", "ar_ma", 0x1801),
1276 LANG("Arabic - Oman", "ar_om", 0x2001),
1277 LANG("Arabic - Qatar", "ar_qa", 0x4001),
1278 LANG("Arabic - Saudi Arabia", "ar_sa", 0x0401),
1279 LANG("Arabic - Syria", "ar_sy", 0x2801),
1280 LANG("Arabic - Tunisia", "ar_tn", 0x1C01),
1281 LANG("Arabic - Yemen", "ar_ye", 0x2401),
1282 LANG("Armenian", "hy", 0x042B),
1283 LANG("Azeri - Latin", "az_az", 0x042C),
1284 LANG("Azeri - Cyrillic", "az_az", 0x082C),
1285 LANG("Basque", "eu", 0x042D),
1286 LANG("Belarusian", "be", 0x0423),
1287 LANG("Bulgarian", "bg", 0x0402),
1288 LANG("Catalan", "ca", 0x0403),
1289 LANG("Chinese - China", "zh_cn", 0x0804),
1290 LANG("Chinese - Hong Kong SAR", "zh_hk", 0x0C04),
1291 LANG("Chinese - Macau SAR", "zh_mo", 0x1404),
1292 LANG("Chinese - Singapore", "zh_sg", 0x1004),
1293 LANG("Chinese - Taiwan", "zh_tw", 0x0404),
1294 LANG("Croatian", "hr", 0x041A),
1295 LANG("Czech", "cs", 0x0405),
1296 LANG("Danish", "da", 0x0406),
1297 LANG("Dutch - Netherlands", "nl_nl", 0x0413),
1298 LANG("Dutch - Belgium", "nl_be", 0x0813),
1299 LANG("English - Australia", "en_au", 0x0C09),
1300 LANG("English - Belize", "en_bz", 0x2809),
1301 LANG("English - Canada", "en_ca", 0x1009),
1302 LANG("English - Caribbean", "en_cb", 0x2409),
1303 LANG("English - Ireland", "en_ie", 0x1809),
1304 LANG("English - Jamaica", "en_jm", 0x2009),
1305 LANG("English - New Zealand", "en_nz", 0x1409),
1306 LANG("English - Phillippines", "en_ph", 0x3409),
1307 LANG("English - Southern Africa", "en_za", 0x1C09),
1308 LANG("English - Trinidad", "en_tt", 0x2C09),
1309 LANG("English - Great Britain", "en_gb", 0x0809),
1310 LANG("English - United States", "en_us", 0x0409),
1311 LANG("Estonian", "et", 0x0425),
1312 LANG("Farsi", "fa", 0x0429),
1313 LANG("Finnish", "fi", 0x040B),
1314 LANG("Faroese", "fo", 0x0438),
1315 LANG("French - France", "fr_fr", 0x040C),
1316 LANG("French - Belgium", "fr_be", 0x080C),
1317 LANG("French - Canada", "fr_ca", 0x0C0C),
1318 LANG("French - Luxembourg", "fr_lu", 0x140C),
1319 LANG("French - Switzerland", "fr_ch", 0x100C),
1320 LANG("Gaelic - Ireland", "gd_ie", 0x083C),
1321 LANG("Gaelic - Scotland", "gd", 0x043C),
1322 LANG("German - Germany", "de_de", 0x0407),
1323 LANG("German - Austria", "de_at", 0x0C07),
1324 LANG("German - Liechtenstein", "de_li", 0x1407),
1325 LANG("German - Luxembourg", "de_lu", 0x1007),
1326 LANG("German - Switzerland", "de_ch", 0x0807),
1327 LANG("Greek", "el", 0x0408),
1328 LANG("Hebrew", "he", 0x040D),
1329 LANG("Hindi", "hi", 0x0439),
1330 LANG("Hungarian", "hu", 0x040E),
1331 LANG("Icelandic", "is", 0x040F),
1332 LANG("Indonesian", "id", 0x0421),
1333 LANG("Italian - Italy", "it_it", 0x0410),
1334 LANG("Italian - Switzerland", "it_ch", 0x0810),
1335 LANG("Japanese", "ja", 0x0411),
1336 LANG("Korean", "ko", 0x0412),
1337 LANG("Latvian", "lv", 0x0426),
1338 LANG("Lithuanian", "lt", 0x0427),
1339 LANG("F.Y.R.O. Macedonia", "mk", 0x042F),
1340 LANG("Malay - Malaysia", "ms_my", 0x043E),
1341 LANG("Malay - Brunei", "ms_bn", 0x083E),
1342 LANG("Maltese", "mt", 0x043A),
1343 LANG("Marathi", "mr", 0x044E),
1344 LANG("Norwegian - Bokml", "no_no", 0x0414),
1345 LANG("Norwegian - Nynorsk", "no_no", 0x0814),
1346 LANG("Polish", "pl", 0x0415),
1347 LANG("Portuguese - Portugal", "pt_pt", 0x0816),
1348 LANG("Portuguese - Brazil", "pt_br", 0x0416),
1349 LANG("Raeto-Romance", "rm", 0x0417),
1350 LANG("Romanian - Romania", "ro", 0x0418),
1351 LANG("Romanian - Republic of Moldova", "ro_mo", 0x0818),
1352 LANG("Russian", "ru", 0x0419),
1353 LANG("Russian - Republic of Moldova", "ru_mo", 0x0819),
1354 LANG("Sanskrit", "sa", 0x044F),
1355 LANG("Serbian - Cyrillic", "sr_sp", 0x0C1A),
1356 LANG("Serbian - Latin", "sr_sp", 0x081A),
1357 LANG("Setsuana", "tn", 0x0432),
1358 LANG("Slovenian", "sl", 0x0424),
1359 LANG("Slovak", "sk", 0x041B),
1360 LANG("Sorbian", "sb", 0x042E),
1361 LANG("Spanish - Spain (Traditional)", "es_es", 0x040A),
1362 LANG("Spanish - Argentina", "es_ar", 0x2C0A),
1363 LANG("Spanish - Bolivia", "es_bo", 0x400A),
1364 LANG("Spanish - Chile", "es_cl", 0x340A),
1365 LANG("Spanish - Colombia", "es_co", 0x240A),
1366 LANG("Spanish - Costa Rica", "es_cr", 0x140A),
1367 LANG("Spanish - Dominican Republic", "es_do", 0x1C0A),
1368 LANG("Spanish - Ecuador", "es_ec", 0x300A),
1369 LANG("Spanish - Guatemala", "es_gt", 0x100A),
1370 LANG("Spanish - Honduras", "es_hn", 0x480A),
1371 LANG("Spanish - Mexico", "es_mx", 0x080A),
1372 LANG("Spanish - Nicaragua", "es_ni", 0x4C0A),
1373 LANG("Spanish - Panama", "es_pa", 0x180A),
1374 LANG("Spanish - Peru", "es_pe", 0x280A),
1375 LANG("Spanish - Puerto Rico", "es_pr", 0x500A),
1376 LANG("Spanish - Paraguay", "es_py", 0x3C0A),
1377 LANG("Spanish - El Salvador", "es_sv", 0x440A),
1378 LANG("Spanish - Uruguay", "es_uy", 0x380A),
1379 LANG("Spanish - Venezuela", "es_ve", 0x200A),
1380 LANG("Southern Sotho", "st", 0x0430),
1381 LANG("Swahili", "sw", 0x0441),
1382 LANG("Swedish - Sweden", "sv_se", 0x041D),
1383 LANG("Swedish - Finland", "sv_fi", 0x081D),
1384 LANG("Tamil", "ta", 0x0449),
1385 LANG("Tatar", "tt", 0X0444),
1386 LANG("Thai", "th", 0x041E),
1387 LANG("Turkish", "tr", 0x041F),
1388 LANG("Tsonga", "ts", 0x0431),
1389 LANG("Ukrainian", "uk", 0x0422),
1390 LANG("Urdu", "ur", 0x0420),
1391 LANG("Uzbek - Cyrillic", "uz_uz", 0x0843),
1392 LANG("Uzbek - Latin", "uz_uz", 0x0443),
1393 LANG("Vietnamese", "vi", 0x042A),
1394 LANG("Xhosa", "xh", 0x0434),
1395 LANG("Yiddish", "yi", 0x043D),
1396 LANG("Zulu", "zu", 0x0435),
1397 LANG(NULL, NULL, 0x0),
1398};
1399
1401{
1402 locale_char_t *locale;
1403 char search_string[64];
1404 char *ptr;
1405
1406 /* Get the current locale. */
1407 locale = indi_setlocale(0, NULL);
1408 if (!locale)
1409 return 0x0;
1410
1411 /* Make a copy of the current locale string. */
1412 strncpy(search_string, locale, sizeof(search_string));
1413 search_string[sizeof(search_string) - 1] = '\0';
1414
1415 /* Chop off the encoding part, and make it lower case. */
1416 ptr = search_string;
1417 while (*ptr)
1418 {
1419 *ptr = tolower(*ptr);
1420 if (*ptr == '.')
1421 {
1422 *ptr = '\0';
1423 break;
1424 }
1425 ptr++;
1426 }
1427
1428 /* Find the entry which matches the string code of our locale. */
1429 struct lang_map_entry *lang = lang_map;
1430 while (lang->string_code)
1431 {
1432 if (!strcmp(lang->string_code, search_string))
1433 {
1434 return lang->usb_code;
1435 }
1436 lang++;
1437 }
1438
1439 /* There was no match. Find with just the language only. */
1440 /* Chop off the variant. Chop it off at the '_'. */
1441 ptr = search_string;
1442 while (*ptr)
1443 {
1444 *ptr = tolower(*ptr);
1445 if (*ptr == '_')
1446 {
1447 *ptr = '\0';
1448 break;
1449 }
1450 ptr++;
1451 }
1452
1453#if 0 // TODO: Do we need this?
1454 /* Find the entry which matches the string code of our language. */
1455 lang = lang_map;
1456 while (lang->string_code)
1457 {
1458 if (!strcmp(lang->string_code, search_string))
1459 {
1460 return lang->usb_code;
1461 }
1462 lang++;
1463 }
1464#endif
1465
1466 /* Found nothing. */
1467 return 0x0;
1468}
1469
1470#ifdef __cplusplus
1471}
1472#endif
hid_device * device
hid_device * hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number)
Open a HID device using a Vendor ID (VID), Product ID (PID) and optionally a serial number.
Definition hid_libusb.c:615
int HID_API_EXPORT hid_read(hid_device *dev, unsigned char *data, size_t length)
Read an Input report from a HID device.
int HID_API_EXPORT hid_init(void)
Initialize the HIDAPI library.
Definition hid_libusb.c:391
void HID_API_EXPORT hid_free_enumeration(struct hid_device_info *devs)
Free an enumeration Linked List.
Definition hid_libusb.c:600
int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *dev, int string_index, wchar_t *string, size_t maxlen)
Get a string from a HID device, based on its string index.
int HID_API_EXPORT hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds)
Read an Input report from a HID device with timeout.
uint16_t get_usb_code_for_current_locale(void)
int HID_API_EXPORT hid_write(hid_device *dev, const unsigned char *data, size_t length)
Write an Output report to a HID device.
Definition hid_libusb.c:948
int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *string, size_t maxlen)
Get The Serial Number String from a HID device.
int HID_API_EXPORT hid_get_feature_report(hid_device *dev, unsigned char *data, size_t length)
Get a feature report from a HID device.
#define LOG(...)
Definition hid_libusb.c:60
hid_device *HID_API_EXPORT hid_open_path(const char *path)
Open a HID device by its path name.
Definition hid_libusb.c:809
int HID_API_EXPORT_CALL hid_get_product_string(hid_device *dev, wchar_t *string, size_t maxlen)
Get The Product String from a HID device.
int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *dev, wchar_t *string, size_t maxlen)
Get The Manufacturer String from a HID device.
void HID_API_EXPORT hid_close(hid_device *dev)
Close a HID device.
struct hid_device_info HID_API_EXPORT * hid_enumerate(unsigned short vendor_id, unsigned short product_id)
Enumerate the HID Devices.
Definition hid_libusb.c:421
int HID_API_EXPORT hid_send_feature_report(hid_device *dev, const unsigned char *data, size_t length)
Send a Feature report to the device.
HID_API_EXPORT const wchar_t *HID_API_CALL hid_error(hid_device *dev)
Get a string describing the last error which occurred.
int HID_API_EXPORT hid_set_nonblocking(hid_device *dev, int nonblock)
Set the device handle to be non-blocking.
int HID_API_EXPORT hid_exit(void)
Finalize the HIDAPI library.
Definition hid_libusb.c:410
#define LANG(name, code, usb_code)
struct pthread_barrier pthread_barrier_t
#define HID_API_EXPORT_CALL
Definition indi_hidapi.h:46
#define HID_API_EXPORT
Definition indi_hidapi.h:42
struct hid_device_ hid_device
Definition indi_hidapi.h:52
#define HID_API_CALL
Definition indi_hidapi.h:43
void usage(void)
const char *LIBUSB_CALL libusb_error_name(int errcode)
#define INDI_LOCALE(s)
char locale_char_t
json_pointer res
pthread_barrier_t barrier
Definition hid_libusb.c:118
int transfer_loop_finished
Definition hid_libusb.c:122
libusb_device_handle * device_handle
Definition hid_libusb.c:96
int input_ep_max_packet_size
Definition hid_libusb.c:101
int input_endpoint
Definition hid_libusb.c:99
struct input_report * input_reports
Definition hid_libusb.c:126
int product_index
Definition hid_libusb.c:108
int shutdown_thread
Definition hid_libusb.c:119
pthread_mutex_t mutex
Definition hid_libusb.c:116
int output_endpoint
Definition hid_libusb.c:100
int manufacturer_index
Definition hid_libusb.c:107
struct libusb_transfer * transfer
Definition hid_libusb.c:123
pthread_cond_t condition
Definition hid_libusb.c:117
pthread_t thread
Definition hid_libusb.c:115
unsigned short product_id
Definition indi_hidapi.h:62
struct hid_device_info * next
Definition indi_hidapi.h:85
unsigned short usage
Definition indi_hidapi.h:77
wchar_t * manufacturer_string
Definition indi_hidapi.h:69
unsigned short vendor_id
Definition indi_hidapi.h:60
unsigned short release_number
Definition indi_hidapi.h:67
wchar_t * serial_number
Definition indi_hidapi.h:64
unsigned short usage_page
Definition indi_hidapi.h:74
wchar_t * product_string
Definition indi_hidapi.h:71
uint8_t * data
Definition hid_libusb.c:88
struct input_report * next
Definition hid_libusb.c:90
uint16_t usb_code
const char * name
const char * string_code