/** * u2fh_devs_done: * @devs: device handle, from u2fh_devs_init(). * * Release all resources associated with @devs. This function must be * called when you are finished with a device handle. */ void u2fh_devs_done (u2fh_devs * devs) { if (devs == NULL) return; close_devices (devs); free (devs->devs); hid_exit (); free (devs); }
//Minipro replacement functions int open_devices(GUID guid, int *devices) { printf("Open devices.\n"); close_devices(); device_handle[0] = NULL; device_handle[1] = NULL; device_handle[2] = NULL; device_handle[3] = NULL; devs = NULL; libusb_init(&ctx);//initialize a new session libusb_set_debug(ctx, 3);//set verbosity level *usb_handle = INVALID_HANDLE_VALUE; *(usb_handle + 1) = INVALID_HANDLE_VALUE; *(usb_handle + 2) = INVALID_HANDLE_VALUE; *(usb_handle + 3) = INVALID_HANDLE_VALUE; int devices_found = 0, i, ret; struct libusb_device_descriptor desc; int count = libusb_get_device_list(ctx, &devs); if(count < 0) { return -1; } for(i = 0; i < count; i++) { ret = libusb_get_device_descriptor(devs[i], &desc); if (ret < 0) { return 0; } if(TL866_PID == desc.idProduct && TL866_VID == desc.idVendor) { if (libusb_open(devs[i], &device_handle[devices_found]) == 0) { *(usb_handle+devices_found) = (HANDLE)devices_found; devices_found++; if (devices_found == 4) return 1; } } } return 1; }
VrpnGenericServerObject::~VrpnGenericServerObject() { close_devices(); delete m_devices; m_devices = nullptr; }
void *notifier_function() { struct udev *udev; struct udev_monitor *mon; struct udev_device *dev; cancel = FALSE; const GUID guid = {0x85980D83,0x32B9,0x4BA1,{0x8F,0xDF,0x12,0xA7,0x11,0xB9,0x9C,0xA2}}; DEV_BROADCAST_DEVICEINTERFACE_W DevBi; DevBi.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE_W); DevBi.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE; DevBi.dbcc_classguid = guid; udev = udev_new(); if (!udev) { printf("Can't create udev\n"); return; } mon = udev_monitor_new_from_netlink(udev, "udev"); if(!mon) { printf("NetLink not available!\n"); return; } int count = get_device_count(); if(count == -1) { printf("udev error.\n"); return; } udev_monitor_filter_add_match_subsystem_devtype(mon, "usb", NULL); udev_monitor_enable_receiving(mon); int fd = udev_monitor_get_fd(mon); while (!cancel) { fd_set fds; struct timeval tv; int ret; FD_ZERO(&fds); FD_SET(fd, &fds); tv.tv_sec = 0; tv.tv_usec = 0; ret = select(fd+1, &fds, NULL, NULL, &tv); if (ret > 0 && FD_ISSET(fd, &fds)) { dev = udev_monitor_receive_device(mon); if(dev && !strcmp(udev_device_get_devtype(dev),"usb_device")){ int count_new; if(!strcmp(udev_device_get_action(dev), "add")) { count_new = get_device_count(); if(count != count_new) { count = count_new; //printf("device added.\n"); close_devices(); usleep(100000); SendMessageW(hWnd, WM_DEVICECHANGE, DBT_DEVICEARRIVAL, (LPARAM)&DevBi); RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE); } } else if(!strcmp(udev_device_get_action(dev), "remove")) { count_new = get_device_count(); if(count != count_new) { count = count_new; //printf("device removed.\n"); close_devices(); usleep(100000); SendMessageW(hWnd, WM_DEVICECHANGE, DBT_DEVICEREMOVECOMPLETE, (LPARAM)&DevBi); RedrawWindow(hWnd, NULL, NULL, RDW_INVALIDATE); } } udev_device_unref(dev); } } usleep(10000); } }