def main(argv=sys.argv): global handle, done hp = [usb.hotplug_callback_handle() for i in range(2)] vendor_id = int(argv[1]) if len(argv) > 1 else 0x045a product_id = int(argv[2]) if len(argv) > 2 else 0x5005 class_id = int(argv[3]) if len(argv) > 3 else usb.LIBUSB_HOTPLUG_MATCH_ANY rc = usb.init(None) if rc < 0: print("failed to initialise libusb: {}".format(usb.error_name(rc))) return 1 try: if not usb.has_capability(usb.LIBUSB_CAP_HAS_HOTPLUG): print("Hotplug capabilites are not supported on this platform") return 1 rc = usb.hotplug_register_callback( None, usb.LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED, 0, vendor_id, product_id, class_id, hotplug_callback, None, ct.byref(hp[0])) if rc != usb.LIBUSB_SUCCESS: print("Error registering callback 0", file=sys.stderr) return 1 rc = usb.hotplug_register_callback( None, usb.LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT, 0, vendor_id, product_id, class_id, hotplug_callback_detach, None, ct.byref(hp[1])) if rc != usb.LIBUSB_SUCCESS: print("Error registering callback 1", file=sys.stderr) return 1 while done < 2: rc = usb.handle_events(None) if rc < 0: print("libusb.handle_events() failed: {}".format( usb.error_name(rc))) finally: if handle: usb.close(handle) usb.exit(None) return 0
def main(): global devh global VID, PID global do_exit #sigact = struct_sigaction() #sigact.sa_handler = sig_hdlr #sigemptyset(ct.byref(sigact.sa_mask)) #sigact.sa_flags = 0 signal.signal(signal.SIGINT, sig_hdlr) rc = usb.init(None) if rc < 0: print("Error initializing libusb: {}".format(usb.error_name(rc)), file=sys.stderr) sys.exit(1) try: devh = usb.open_device_with_vid_pid(None, VID, PID) if not devh: print("Error finding USB device", file=sys.stderr) return rc rc = usb.claim_interface(devh, 2) if rc < 0: print("Error claiming interface: {}".format(usb.error_name(rc)), file=sys.stderr) return rc benchmark_in(EP_ISO_IN) while not do_exit: rc = usb.handle_events(None) if rc != usb.LIBUSB_SUCCESS: break # Measurement has already been done by the signal handler. usb.release_interface(devh, 0) finally: if devh: usb.close(devh) usb.exit(None) return rc
def init_capture(): global state global img_transfer global irq_transfer r = usb.submit_transfer(irq_transfer) if r < 0: return r r = usb.submit_transfer(img_transfer) if r < 0: usb.cancel_transfer(irq_transfer) while irq_transfer: if usb.handle_events(None) < 0: break return r # start state machine state = STATE_AWAIT_IRQ_FINGER_REMOVED return next_state()
def main(): global devh global img_transfer global irq_transfer global do_exit exit_sem = sem_open(SEM_NAME, O_CREAT, 0) if not exit_sem: print("failed to initialise semaphore error {}".format(errno), file=sys.stderr) sys.exit(1) # only using this semaphore in this process so go ahead and unlink it now sem_unlink(SEM_NAME) r = usb.init(None) if r < 0: print("failed to initialise libusb", file=sys.stderr) sys.exit(1) r = find_dpfp_device() try: if r < 0: print("Could not find/open device", file=sys.stderr) return abs(r) r = usb.claim_interface(devh, 0) if r < 0: print("usb_claim_interface error {} {}".format(r, usb.strerror(usb.error(r))), file=sys.stderr) return abs(r) print("claimed interface") r = print_f0_data() if r < 0: usb.release_interface(devh, 0) return abs(r) r = do_init() try: if r < 0: return abs(r) # async from here onwards #sigact = struct_sigaction() #sigact.sa_handler = sighandler #sigemptyset(ct.byref(sigact.sa_mask)) #sigact.sa_flags = 0 signal.signal(signal.SIGINT, sighandler) signal.signal(signal.SIGTERM, sighandler) if hasattr(signal, "SIGQUIT"): signal.signal(signal.SIGQUIT, sighandler) r = pthread_create(ct.byref(poll_thread), NULL, poll_thread_main, NULL) if r: return abs(r) r = alloc_transfers() if r < 0: request_exit(1) pthread_join(poll_thread, NULL) return abs(r) r = init_capture() if r < 0: request_exit(1) pthread_join(poll_thread, NULL) return abs(r) while not do_exit: sem_wait(exit_sem) print("shutting down...") pthread_join(poll_thread, NULL) r = usb.cancel_transfer(irq_transfer) if r < 0: request_exit(1) return abs(r) r = usb.cancel_transfer(img_transfer) if r < 0: request_exit(1) return abs(r) while irq_transfer or img_transfer: if usb.handle_events(None) < 0: break r = 0 if do_exit == 1 else 1 finally: usb.free_transfer(img_transfer) usb.free_transfer(irq_transfer) set_mode(0); set_hwstat(0x80) usb.release_interface(devh, 0) finally: usb.close(devh) usb.exit(None) return abs(r)
def main(argv=sys.argv): global devh global img_transfer global irq_transfer global do_exit r = usb.init(None) if r < 0: print("failed to initialise libusb", file=sys.stderr) sys.exit(1) r = find_dpfp_device() try: if r < 0: print("Could not find/open device", file=sys.stderr) return abs(r) r = usb.claim_interface(devh, 0) if r < 0: print("usb_claim_interface error {}".format(r), file=sys.stderr) return abs(r) print("claimed interface") r = print_f0_data() if r < 0: usb.release_interface(devh, 0) return abs(r) r = do_init() try: if r < 0: return abs(r) # async from here onwards r = alloc_transfers() if r < 0: return abs(r) r = init_capture() if r < 0: return abs(r) #sigact = struct_sigaction() #sigact.sa_handler = sighandler #sigemptyset(ct.byref(sigact.sa_mask)) #sigact.sa_flags = 0 signal.signal(signal.SIGINT, sighandler) signal.signal(signal.SIGTERM, sighandler) if hasattr(signal, "SIGQUIT"): signal.signal(signal.SIGQUIT, sighandler) while not do_exit: r = usb.handle_events(None) if r < 0: return abs(r) print("shutting down...") if irq_transfer: r = usb.cancel_transfer(irq_transfer) if r < 0: return abs(r) if img_transfer: r = usb.cancel_transfer(img_transfer) if r < 0: return abs(r) while irq_transfer or img_transfer: if usb.handle_events(None) < 0: break r = 0 if do_exit == 1 else 1 finally: usb.free_transfer(img_transfer) usb.free_transfer(irq_transfer) set_mode(0); set_hwstat(0x80) usb.release_interface(devh, 0) finally: usb.close(devh) usb.exit(None) return abs(r)