|
21 | 21 | #include <sys/utsname.h> |
22 | 22 | #include <fcntl.h> |
23 | 23 | #include <errno.h> |
| 24 | +#include <assert.h> |
24 | 25 |
|
25 | 26 | #include <libusb-1.0/libusb.h> |
26 | 27 |
|
|
48 | 49 |
|
49 | 50 | static bool gIsLibUSBInitialized = false; |
50 | 51 | static struct libusb_context *gLJContext = NULL; |
| 52 | +static libusb_hotplug_callback_handle gHotPlugCallbackHandle = 0; |
| 53 | +static LJUSB_HotPlugConnectedCallback gHotPlugConnectedCallback = NULL; |
| 54 | +static LJUSB_HotPlugDisconnectedCallback gHotPlugDisconnectedCallback = NULL; |
| 55 | +static void *gHotPlugUserContext = NULL; |
51 | 56 |
|
52 | 57 | enum LJUSB_TRANSFER_OPERATION { LJUSB_WRITE, LJUSB_READ, LJUSB_STREAM }; |
53 | 58 |
|
@@ -717,6 +722,149 @@ int LJUSB_OpenAllDevicesOfProductId(UINT productId, HANDLE **devHandles) |
717 | 722 | return successCount; |
718 | 723 | } |
719 | 724 |
|
| 725 | +static int LJUSB_HotPlugCallback(libusb_context *ctx, |
| 726 | + libusb_device *dev, |
| 727 | + libusb_hotplug_event event, |
| 728 | + void *user_data) |
| 729 | +{ |
| 730 | + assert(ctx == gLJContext); |
| 731 | + (void)ctx; |
| 732 | + assert(dev); |
| 733 | + (void)user_data; |
| 734 | + |
| 735 | + // NB: this callback may, or may not, be called from a private libusb thread! |
| 736 | + |
| 737 | + if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED) { |
| 738 | + fprintf(stderr, "LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED occurred\n"); |
| 739 | + |
| 740 | + HANDLE handle = NULL; |
| 741 | + |
| 742 | + struct libusb_device_descriptor desc; |
| 743 | + int r = libusb_get_device_descriptor(dev, &desc); |
| 744 | + if (r < 0) { |
| 745 | + fprintf(stderr, "LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED failed to get device descriptor due to:\n"); |
| 746 | + LJUSB_libusbError(r); |
| 747 | + } |
| 748 | + else { |
| 749 | + handle = LJUSB_OpenSpecificDevice(dev, &desc); |
| 750 | + if (!handle) { |
| 751 | + fprintf(stderr, "LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED failed to open device\n"); |
| 752 | + } |
| 753 | + } |
| 754 | + gHotPlugConnectedCallback(handle, gHotPlugUserContext); |
| 755 | + } |
| 756 | + else if (event == LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT) { |
| 757 | + fprintf(stderr, "LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT occurred\n"); |
| 758 | + |
| 759 | + gHotPlugDisconnectedCallback(dev, gHotPlugUserContext); |
| 760 | + } |
| 761 | + else { |
| 762 | + fprintf(stderr, "unknown hotplug event type received\n"); |
| 763 | + } |
| 764 | + |
| 765 | + // Always return 0 to indicate we want additional events. |
| 766 | + return 0; |
| 767 | +} |
| 768 | + |
| 769 | +bool LJUSB_RegisterHotPlug(unsigned long productID, |
| 770 | + LJUSB_HotPlugConnectedCallback connectedCallback, |
| 771 | + LJUSB_HotPlugDisconnectedCallback disconnectedCallback, |
| 772 | + void *context) |
| 773 | +{ |
| 774 | + assert(connectedCallback); |
| 775 | + assert(disconnectedCallback); |
| 776 | + |
| 777 | + int r = 1; |
| 778 | + |
| 779 | + if (!LJUSB_libusb_initialize()) { |
| 780 | + return false; |
| 781 | + } |
| 782 | + |
| 783 | + if (!libusb_has_capability(LIBUSB_CAP_HAS_HOTPLUG)) { |
| 784 | + fprintf(stderr, "libusb hotplug not supported on this platform\n"); |
| 785 | + LJUSB_libusbError(LIBUSB_ERROR_NOT_SUPPORTED); |
| 786 | + return false; |
| 787 | + } |
| 788 | + |
| 789 | + int productID = (productID == 0) ? LIBUSB_HOTPLUG_MATCH_ANY : (int)productID; |
| 790 | + |
| 791 | + // Store the callbacks now, since LIBUSB_HOTPLUG_ENUMERATE below could result in them being call before libusb_hotplug_register_callback() even returns. |
| 792 | + gHotPlugConnectedCallback = connectedCallback; |
| 793 | + gHotPlugDisconnectedCallback = disconnectedCallback; |
| 794 | + gHotPlugUserContext = context; |
| 795 | + |
| 796 | + r = libusb_hotplug_register_callback(gLJContext, |
| 797 | + LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, |
| 798 | + LIBUSB_HOTPLUG_ENUMERATE, |
| 799 | + LJ_VENDOR_ID, |
| 800 | + productID, |
| 801 | + LIBUSB_HOTPLUG_MATCH_ANY, |
| 802 | + &LJUSB_HotPlugCallback, |
| 803 | + NULL, |
| 804 | + &gHotPlugCallbackHandle); |
| 805 | + |
| 806 | + if (r < 0) { |
| 807 | + gHotPlugConnectedCallback = NULL; |
| 808 | + gHotPlugDisconnectedCallback = NULL; |
| 809 | + gHotPlugUserContext = NULL; |
| 810 | + gHotPlugCallbackHandle = 0; |
| 811 | + |
| 812 | + fprintf(stderr, "failed to register hot plug callback due to:\n"); |
| 813 | + LJUSB_libusbError(r); |
| 814 | + return false; |
| 815 | + } |
| 816 | + |
| 817 | + return true; |
| 818 | +} |
| 819 | + |
| 820 | + |
| 821 | +void LJUSB_DeregisterHotPlug(void) |
| 822 | +{ |
| 823 | + assert(gIsLibUSBInitialized); |
| 824 | + |
| 825 | + if (gLJContext && gHotPlugCallbackHandle) { |
| 826 | + libusb_hotplug_deregister_callback(gLJContext, gHotPlugCallbackHandle); |
| 827 | + |
| 828 | + gHotPlugConnectedCallback = NULL; |
| 829 | + gHotPlugDisconnectedCallback = NULL; |
| 830 | + gHotPlugUserContext = NULL; |
| 831 | + gHotPlugCallbackHandle = 0; |
| 832 | + } |
| 833 | + else { |
| 834 | + fprintf(stderr, "LJUSB_DeregisterHotPlug: nothing to do\n"); |
| 835 | + } |
| 836 | +} |
| 837 | + |
| 838 | + |
| 839 | +int LJUSB_HandleEventsTimeoutCompleted(struct timeval *tv, int *completed) |
| 840 | +{ |
| 841 | + if (!LJUSB_libusb_initialize()) { |
| 842 | + return false; |
| 843 | + } |
| 844 | + |
| 845 | + return libusb_handle_events_timeout_completed(gLJContext, tv, completed); |
| 846 | +} |
| 847 | + |
| 848 | +DEVICE LJUSB_DeviceFromHandle(HANDLE hDevice) |
| 849 | +{ |
| 850 | + if (!hDevice) { |
| 851 | + return NULL; |
| 852 | + } |
| 853 | + |
| 854 | + libusb_device *device = libusb_get_device(hDevice); |
| 855 | + return device; |
| 856 | +} |
| 857 | + |
| 858 | +void LJUSB_RefCountIncrement(DEVICE device) |
| 859 | +{ |
| 860 | + libusb_ref_device(device); |
| 861 | +} |
| 862 | + |
| 863 | +void LJUSB_RefCountDecrement(DEVICE device) |
| 864 | +{ |
| 865 | + libusb_unref_device(device); |
| 866 | +} |
| 867 | + |
720 | 868 | bool LJUSB_ResetConnection(HANDLE hDevice) |
721 | 869 | { |
722 | 870 | int r; |
|
0 commit comments