def cb_irq(transfer): global state global irq_transfer irqtype = ct.cast(transfer.buffer[0], ct.c_ubyte) if transfer.status != usb.LIBUSB_TRANSFER_COMPLETED: print("irq transfer status {}?".format(transfer.status), file=sys.stderr) request_exit(2) usb.free_transfer(transfer) irq_transfer = ct.POINTER(usb.transfer)() return print("IRQ callback {:02x}".format(irqtype)) if state == STATE_AWAIT_IRQ_FINGER_DETECTED: if irqtype == 0x01: if next_state() < 0: request_exit(2) return else: print("finger-on-sensor detected in wrong state!") elif state == STATE_AWAIT_IRQ_FINGER_REMOVED: if irqtype == 0x02: if next_state() < 0: request_exit(2) return else: print("finger-on-sensor detected in wrong state!") if usb.submit_transfer(irq_transfer) < 0: request_exit(2)
def cb_img(transfer): global imgbuf global img_transfer if transfer.status != usb.LIBUSB_TRANSFER_COMPLETED: print("img transfer status {}?".format(transfer.status), file=sys.stderr) request_exit(2) usb.free_transfer(transfer) img_transfer = ct.POINTER(usb.transfer)() return print("Image callback") save_to_file(imgbuf) if next_state() < 0: request_exit(2) return if usb.submit_transfer(img_transfer) < 0: request_exit(2)
def cb_xfr(xfr): global num_bytes global num_xfer xfr = xfr[0] if xfr.status != usb.LIBUSB_TRANSFER_COMPLETED: print("transfer status {}".format(xfr.status), file=sys.stderr) usb.free_transfer(ct.byref(xfr)) sys.exit(3) if xfr.type == usb.LIBUSB_TRANSFER_TYPE_ISOCHRONOUS: for i in range(xfr.num_iso_packets): pack = xfr.iso_packet_desc[i] if pack.status != usb.LIBUSB_TRANSFER_COMPLETED: print("Error: pack {} status {}".format(i, pack.status), file=sys.stderr) sys.exit(5) print("pack{} length:{}, actual_length:{}".format( i, pack.length, pack.actual_length)) print("length:{}, actual_length:{}".format(xfr.length, xfr.actual_length)) for i in range(xfr.actual_length): print("{:02x}".format(xfr.buffer[i]), end="") if i % 16: print() elif i % 8: print(" ", end="") else: print(" ", end="") num_bytes += xfr.actual_length num_xfer += 1 rc = usb.submit_transfer(ct.byref(xfr)) if rc < 0: print("error re-submitting URB", file=sys.stderr) sys.exit(1)
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)