vpelletier / python-libusb1

Python ctype-based wrapper around libusb1
GNU Lesser General Public License v2.1
168 stars 65 forks source link

Errors on Windows #97

Open demonguy opened 2 months ago

demonguy commented 2 months ago

I tried to use this library to access android devices via python script. So far I can:

  1. my script works on Ubuntu and Mac
  2. on Windows, my driver and device works fine with native Android adb tool
  3. zadig shows my device driver is WinUSB
  4. And I got errors below

Did I miss anything?

Looking for adb devices ...
Traceback (most recent call last):
  File "C:\Users\cy_ss_000.CY\miTrace\miTrace.py", line 146, in <module>
    devs = init_devices()
  File "C:\Users\cy_ss_000.CY\miTrace\miTrace.py", line 46, in init_devices
    dev.connect()
  File "C:\Users\cy_ss_000.CY\miTrace\lib/adb_shell\adb_shell\adb_device.py", line 675, in connect
    self._available, self._maxdata = self._io_manager.connect(self._banner, rsa_keys, auth_timeout_s, auth_callback, adb_info)
  File "C:\Users\cy_ss_000.CY\miTrace\lib/adb_shell\adb_shell\adb_device.py", line 221, in connect
    self._transport.connect(adb_info.transport_timeout_s)
  File "C:\Users\cy_ss_000.CY\miTrace\lib/adb_shell\adb_shell\transport\usb_transport.py", line 238, in connect
    transport = self._device.open()
  File "C:\Users\cy_ss_000.CY\AppData\Local\Programs\Python\Python39\lib\site-packages\usb1\__init__.py", line 2055, in open
    mayRaiseUSBError(libusb1.libusb_open(self.device_p, byref(handle)))
  File "C:\Users\cy_ss_000.CY\AppData\Local\Programs\Python\Python39\lib\site-packages\usb1\__init__.py", line 127, in mayRaiseUSBError
    __raiseUSBError(value)
  File "C:\Users\cy_ss_000.CY\AppData\Local\Programs\Python\Python39\lib\site-packages\usb1\__init__.py", line 119, in raiseUSBError
    raise __STATUS_TO_EXCEPTION_DICT.get(value, __USBError)(value)
usb1.USBErrorAccess: LIBUSB_ERROR_ACCESS [-3]

image

vpelletier commented 2 months ago

As this is an error reported by libusb_open (so the C library directly), the first level of explanation I can give comes from the libusb1 documentation for libusb_open.

Then, the question becomes: Why does your windows user somehow not have the permission to open that device ?

My first thought was libusb_detach_kernel_driver (exposed as DeviceHandle.detachKernelDriver in this module), especially as you mention ADB may be installed on that system (and could hence somehow maybe take precedence over WinUSB ? I'm not sure), but the libusb1 documentation states it is not available on windows.

Searching a bit, I find https://github.com/libusb/libusb/issues/1300 which suggests to me the 32bits library (and hence main executable ?) may somehow not be allowed to open devices running on a 64bits machine. What bit-width the python interpreter you are using is compiled for ?

vpelletier commented 2 months ago

Another approach to this question: here are two projects which implement the adb command in python, and both happen to use my module to access the USB bus:

https://github.com/google/python-adb (archived)

https://github.com/JeffLIrion/adb_shell (active)

EDIT: ...and I forgot to finish my thought: I'm not suggesting you use these projects' code (though you may decide to), just that they could maybe have bug reports or code which could point you in the direction of how to resolve the present issue.