adb_shell icon indicating copy to clipboard operation
adb_shell copied to clipboard

Libusb error

Open 5picoseconds opened this issue 5 years ago • 9 comments

I'm getting an error when trying to pull the the correct dll to use for libusb This issue continues off of issue#117

Below is a snippet of the code along with the error it gives:

import os
import sys
os.add_dll_directory('C:\\Users\\Public\\Documents')

from adb_shell.transport.usb_transport import UsbTransport
from adb_shell.adb_device import AdbDeviceTcp, AdbDeviceUsb
from adb_shell.auth.sign_pythonrsa import PythonRSASigner

# Connect via USB (package must be installed via `pip install adb-shell[usb])`
transport = UsbTransport.find_adb()
print(transport)

with open('C:\\Users\\Public\\Documents\\adbkey') as f:
   priv = f.read()
signer = PythonRSASigner('', priv)

# NOTE: This next line is incorrect, see the note at the end of this post
device = AdbDeviceUsb(transport)
device.connect(rsa_keys=[signer], auth_timeout_s=0.1)

# Send a shell command
response3 = device.shell('echo TEST3')
Traceback (most recent call last):
  File "C:/Users/stefan.pompilio/PhoneTestingAdb/main.py", line 24, in <module>
    device = AdbDeviceUsb(transport)
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\adb_shell\adb_device.py", line 1205, in __init__
    transport = UsbTransport.find_adb(serial, port_path, default_transport_timeout_s)
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\adb_shell\transport\usb_transport.py", line 620, in find_adb
    return cls._find(
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\adb_shell\transport\usb_transport.py", line 507, in _find
    return cls._find_first(setting_matcher, device_matcher, usb_info=usb_info, default_transport_timeout_s=default_transport_timeout_s)
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\adb_shell\transport\usb_transport.py", line 597, in _find_first
    return next(cls._find_devices(setting_matcher, device_matcher=device_matcher, usb_info=usb_info, default_transport_timeout_s=default_transport_timeout_s))
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\adb_shell\transport\usb_transport.py", line 565, in _find_devices
    if device_matcher is None or device_matcher(transport):
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\adb_shell\transport\usb_transport.py", line 470, in <lambda>
    return lambda device: device.serial_number == serial
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\adb_shell\transport\usb_transport.py", line 410, in serial_number
    return self._device.getSerialNumber()
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\usb1\__init__.py", line 2136, in getSerialNumber
    return self.open().getSerialNumber()
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\usb1\__init__.py", line 2171, in open
    mayRaiseUSBError(libusb1.libusb_open(self.device_p, byref(handle)))
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\usb1\__init__.py", line 133, in mayRaiseUSBError
    __raiseUSBError(value)
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\usb1\__init__.py", line 125, in raiseUSBError
    raise __STATUS_TO_EXCEPTION_DICT.get(value, __USBError)(value)
usb1.USBErrorNotSupported: LIBUSB_ERROR_NOT_SUPPORTED [-12]

I've tried using zadig to upgrade the driver, but that hasn't worked. Any ideas are appreciated!

Note

The line

device = AdbDeviceUsb(transport)

above is incorrect. The first parameter to AdbDeviceUsb is the device serial number or None, not the transport object itself. See https://github.com/JeffLIrion/adb_shell/issues/118#issuecomment-675007811 for the correct way to instantiate an AdbDeviceUsb object.

5picoseconds avatar Aug 17 '20 16:08 5picoseconds

Try this:

import os
os.add_dll_directory('C:\\Users\\Public\\Documents')

from adb_shell.adb_device import AdbDeviceUsb
from adb_shell.auth.sign_pythonrsa import PythonRSASigner

# Load the public and private keys
adbkey = 'C:\\Users\\Public\\Documents\\adbkey'
with open(adbkey) as f:
   priv = f.read()
with open(adbkey + '.pub') as f:
    pub = f.read()
signer = PythonRSASigner(pub, priv)

# Connect via USB (package must be installed via `pip install adb-shell[usb])`
device = AdbDeviceUsb()
device.connect(rsa_keys=[signer], auth_timeout_s=9.0)

# Send a shell command
response3 = device.shell('echo TEST3')

If that doesn't work, try this:

import os
os.add_dll_directory('C:\\Users\\Public\\Documents')

from adb_shell.transport.usb_transport import UsbTransport
from adb_shell.adb_device import AdbDevice
from adb_shell.auth.sign_pythonrsa import PythonRSASigner

# Load the public and private keys
adbkey = 'C:\\Users\\Public\\Documents\\adbkey'
with open(adbkey) as f:
   priv = f.read()
with open(adbkey + '.pub') as f:
    pub = f.read()
signer = PythonRSASigner(pub, priv)

# Connect via USB (package must be installed via `pip install adb-shell[usb])`
transport = UsbTransport.find_adb()
print(transport)
device = AdbDevice(transport)
device.connect(rsa_keys=[signer], auth_timeout_s=9.0)

# Send a shell command
response3 = device.shell('echo TEST3')

JeffLIrion avatar Aug 17 '20 17:08 JeffLIrion

They both give me similar errors to my code The first one gives:

Traceback (most recent call last):
  File "C:/Users/stefan.pompilio/PhoneTestingAdb/main.py", line 50, in <module>
    device.connect(rsa_keys=[signer], auth_timeout_s=9.0)
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\adb_shell\adb_device.py", line 211, in connect
    self._transport.connect(transport_timeout_s)
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\adb_shell\transport\usb_transport.py", line 233, in connect
    transport = self._device.open()
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\usb1\__init__.py", line 2171, in open
    mayRaiseUSBError(libusb1.libusb_open(self.device_p, byref(handle)))
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\usb1\__init__.py", line 133, in mayRaiseUSBError
    __raiseUSBError(value)
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\usb1\__init__.py", line 125, in raiseUSBError
    raise __STATUS_TO_EXCEPTION_DICT.get(value, __USBError)(value)
usb1.USBErrorNotSupported: LIBUSB_ERROR_NOT_SUPPORTED [-12]

and the second one gives:

Traceback (most recent call last):
  File "C:/Users/stefan.pompilio/PhoneTestingAdb/main.py", line 53, in <module>
    device.connect(rsa_keys=[signer], auth_timeout_s=9.0)
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\adb_shell\adb_device.py", line 211, in connect
    self._transport.connect(transport_timeout_s)
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\adb_shell\transport\usb_transport.py", line 233, in connect
    transport = self._device.open()
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\usb1\__init__.py", line 2171, in open
    mayRaiseUSBError(libusb1.libusb_open(self.device_p, byref(handle)))
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\usb1\__init__.py", line 133, in mayRaiseUSBError
    __raiseUSBError(value)
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\usb1\__init__.py", line 125, in raiseUSBError
    raise __STATUS_TO_EXCEPTION_DICT.get(value, __USBError)(value)
usb1.USBErrorNotSupported: LIBUSB_ERROR_NOT_SUPPORTED [-12]

5picoseconds avatar Aug 18 '20 18:08 5picoseconds

Again, the error LIBUSB_ERROR_NOT_SUPPORTED [-12] appears when the function libusb_open() is called. In the libusb repository itself the issue #273, (literally this problem: libusb_open() returns -12) got solved with using zadig. Could you try it again??

ajuancer avatar Aug 18 '20 21:08 ajuancer

Again, the error LIBUSB_ERROR_NOT_SUPPORTED [-12] appears when the function libusb_open() is called. In the libusb repository itself the issue #273, (literally this problem: libusb_open() returns -12) got solved with using zadig. Could you try it again??

so I tried using zadig again and noticed that I actually have 2 things listed as SAMSUNG_ANDROID for some reason. Possibly just different phones that have been plugged into my laptop. But yeah, I was able to switch the driver to WinUSB (v6.1.7600.16385).

Now the code that @JeffLIrion runs without error, however, nothing shows up on the phone

When running my code, it gives the following:

Traceback (most recent call last):
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\adb_shell\transport\usb_transport.py", line 597, in _find_first
    return next(cls._find_devices(setting_matcher, device_matcher=device_matcher, usb_info=usb_info, default_transport_timeout_s=default_transport_timeout_s))
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/stefan.pompilio/PhoneTestingAdb/main.py", line 24, in <module>
    device = AdbDeviceUsb(transport)
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\adb_shell\adb_device.py", line 1205, in __init__
    transport = UsbTransport.find_adb(serial, port_path, default_transport_timeout_s)
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\adb_shell\transport\usb_transport.py", line 620, in find_adb
    return cls._find(
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\adb_shell\transport\usb_transport.py", line 507, in _find
    return cls._find_first(setting_matcher, device_matcher, usb_info=usb_info, default_transport_timeout_s=default_transport_timeout_s)
  File "C:\Users\stefan.pompilio\PhoneTestingAdb\venv\lib\site-packages\adb_shell\transport\usb_transport.py", line 599, in _find_first
    raise exceptions.UsbDeviceNotFoundError('No device available, or it is in the wrong configuration.')
adb_shell.exceptions.UsbDeviceNotFoundError: No device available, or it is in the wrong configuration.

Which I guess makes sense since I changed the driver. So I'm thinking maybe the overall problem is that samsung's driver and libusb don't like to play nice together? I kind of want to try copying and renaming samsung's driver to libusb to see if that could trick things into working. Think it's worth a shot?

5picoseconds avatar Aug 19 '20 13:08 5picoseconds

You can provide transport to the AdbDevice class, but don't provide it to the AdbDeviceUsb class. I recommend using the first of the two code snippets that I provided.

Nothing in that code snippet is supposed to make anything show up on your phone.

response3 = device.shell('echo TEST3')

response3 should be "TEST3", but it won't be displayed on your phone.

JeffLIrion avatar Aug 19 '20 15:08 JeffLIrion

Is Samsung's driver based on WinUSB? Take note libusb only supports the following drivers under Windows: WinUSB, libusbk, HID, libusb0.sys (buggy) and usbdk.

And we recommend WinUSB whenever possible (unless it is an HID device). https://github.com/libusb/libusb/wiki/Windows#How_to_use_libusb_on_Windows

mcuee avatar Sep 07 '20 09:09 mcuee

Tested. I have Samsung S8 phone, you need uninstall Samsung drivers and with Zadig reinstall WinUSB driver. The issue can be closed.

jcomas avatar Jul 22 '21 13:07 jcomas

@5picoseconds did you get this working? It sounds like you did (https://github.com/JeffLIrion/adb_shell/issues/118#issuecomment-676389565):

Now the code that @JeffLIrion runs without error, however, nothing shows up on the phone*

*The command that was sent doesn't show anything on the device

JeffLIrion avatar Jul 22 '21 15:07 JeffLIrion

This message "No device available, or it is in the wrong configuration." appears when you don't have WinUSB driver installed. Then, you can use Zadig to install.

WinUSB with Samsung phone

jcomas avatar Jul 22 '21 16:07 jcomas