atlas0fd00m / rfcat

RfCat - swiss-army knife of ISM band radio
Other
554 stars 116 forks source link

NoBackendError #111

Closed rickgator closed 8 months ago

rickgator commented 3 years ago

I'm on MacOS Big Sur trying to run rfcat with a YardStick One. I am running anaconda as well and I have set up an environment with Python2.7 when I originally got errors with Python3.x. I have downloaded the pyusb, pyreadline, ipython, PySide2, and the libusb dependancies. Libusb seems to be giving me the most trouble. I keep getting the following error:

Error in resetup():NoBackendError('No backend available',)
Error in resetup():NoBackendError('No backend available',)
Error in resetup():NoBackendError('No backend available',)
^CTraceback (most recent call last):
  File "/opt/anaconda3/envs/rftools/bin/rfcat", line 4, in <module>
    __import__('pkg_resources').run_script('rfcat==1.9.5', 'rfcat')
  File "/opt/anaconda3/envs/rftools/lib/python2.7/site-packages/pkg_resources/__init__.py", line 666, in run_script
    self.require(requires)[0].run_script(script_name, ns)
  File "/opt/anaconda3/envs/rftools/lib/python2.7/site-packages/pkg_resources/__init__.py", line 1469, in run_script
    exec(script_code, namespace, namespace)
  File "/opt/anaconda3/envs/rftools/lib/python2.7/site-packages/rfcat-1.9.5-py2.7.egg/EGG-INFO/scripts/rfcat", line 63, in <module>

  File "build/bdist.macosx-10.7-x86_64/egg/rflib/__init__.py", line 208, in interactive
  File "build/bdist.macosx-10.7-x86_64/egg/rflib/chipcon_nic.py", line 103, in __init__
  File "build/bdist.macosx-10.7-x86_64/egg/rflib/chipcon_usb.py", line 93, in __init__
  File "build/bdist.macosx-10.7-x86_64/egg/rflib/chipcon_usb.py", line 238, in resetup
KeyboardInterrupt

From my research so far, backend is how pyusb refers to libusb, libusb1 or openusb. It is unable to find the libusb within the environment. I did a little tracking and found that ultimately, the find_library() function is found in ctypes in util.py. It refers to the executable path for MacOS with '@executable_path/../lib/libusb%s.....' I tried to put libusb into a folder on my executable path to hopefully match this functions search, and still got the same errors. I then found instructions on inputting a custom path for the backend for pyusb here. This appears to be a method where you input the device and backend information at the beginning of your program. The code I inserted is as follows:

import usb.core
import usb.backend.libusb1 as libusb1

backend = libusb1.get_backend(find_library=lambda x: "/path/to/file/lib/libusb-1.0.0.dylib")
dev = usb.core.find(idVendor=0x1d50, idProduct=0x605b, backend=backend)

This induced a similar error but with a different traceback when I placed the code in rflib.init and the rfcat codes:

Traceback (most recent call last):
  File "/opt/anaconda3/envs/rftools/bin/rfcat", line 4, in <module>
    __import__('pkg_resources').run_script('rfcat==1.9.5', 'rfcat')
  File "/opt/anaconda3/envs/rftools/lib/python2.7/site-packages/pkg_resources/__init__.py", line 666, in run_script
    self.require(requires)[0].run_script(script_name, ns)
  File "/opt/anaconda3/envs/rftools/lib/python2.7/site-packages/pkg_resources/__init__.py", line 1469, in run_script
    exec(script_code, namespace, namespace)
  File "/opt/anaconda3/envs/rftools/lib/python2.7/site-packages/rfcat-1.9.5-py2.7.egg/EGG-INFO/scripts/rfcat", line 12, in <module>

  File "build/bdist.macosx-10.7-x86_64/egg/rflib/__init__.py", line 15, in <module>
  File "/opt/anaconda3/envs/rftools/lib/python2.7/site-packages/usb/core.py", line 1304, in find
    raise NoBackendError('No backend available')
usb.core.NoBackendError: No backend available

I have since reset things back to how I started and am still getting the original error listed above.

I think this largely has to do with the anaconda environment, which I can of course remove. I want to try to find a way to make this work though. Is there a better method to help rfcat find the libusb as required?

EDITS: just cleaning up my code blocks.

Loumiakas commented 3 years ago

Which approach did you take in regards to installing rfcat? Did you attempt to install it through Anaconda package installer, through pip or did you clone git repository? I have gone through setup process of Python 2.7.18 :: Anaconda, Inc., with future/pyusb installed through pip (after activating anaconda environment), libusb/libusb-compat installed through homebrew and invoked rfcat executable after cloning git repository - rfcat seems to be working just fine...

Here is the list of libs installed on my conda environment:

# Name                    Version                   Build  Channel
ca-certificates           2021.5.25            hecd8cb5_1
certifi                   2020.6.20          pyhd3eb1b0_3
future                    0.18.2                   pypi_0    pypi
libcxx                    10.0.0                        1
libffi                    3.3                  hb1e8313_2
ncurses                   6.2                  h0a44026_1
pip                       19.3.1                   py27_0
python                    2.7.18               h47d645e_1
pyusb                     1.1.1                    pypi_0    pypi
readline                  8.1                  h9ed2024_0
setuptools                44.0.0                   py27_0
sqlite                    3.35.4               hce871da_0
tk                        8.6.10               hb0a8c7a_0
wheel                     0.36.2             pyhd3eb1b0_0
zlib                      1.2.11               h1de35cc_3`

Also, Python 2 has reached end-of-life, so I would recommend that you use Python 3 instead. You can install rfcat through python package manager (from my experience, it should work out of the box),

rickgator commented 3 years ago

I reset my environment and installed everything in the exact manner you just mentioned. I first ran conda install pip, then I installed pyusb, then brew install libusb-compat, and then rfcat through a clone of the git repository currently in my ~/Downloads folder. I received the same error. I then removed rrfcat and installed it with pip and still got the error. When I deactivated the base environment and installed everything, I was able to get rfcat to work. This makes me pretty sure it has something to do with the anaconda, I'm just not sure what I'm doing wrong. rfcat is moved into the environments /bin folder.

Is it possible installing it from the ~/Downloads folder is causing an issue?

Could it be anaconda doesn’t have permissions to communicate with homebrew libs? I’ll update if I can access this somewhere.

Loumiakas commented 3 years ago

I doubt that issue would be caused due to the fact that you are installing from ~/Downloads directory.

When you say that you moved 'rfcat' into environment's /bin folder, you mean the executable itself and libs from the directory? Generally, I tend install rfcat by cloning repository and installing it with pip install <PATH_TO_RFCAT_DIR>. If you are just moving rfcat executable, it is unlikely that python executable would find supporting code/libraries for rfcat to work properly.

rickgator commented 3 years ago

I should have clarified. I install with the command sudo python setup.py install. This has automatically installed the executable command into the /bin.

Did you install anaconda from homebrew?

Loumiakas commented 3 years ago

Yea, I usually install anaconda from homebrew (miniconda to be exact, as it does not bloat my environment with libraries that I would never use).

In regards to rfcat - it's a strange one indeed. I believe this is happening because of multiple rfcat instances installed on your system. Based on what you've posted above, it seems like you have atleast one other instance /opt/anaconda3/envs/rftools/lib/python2.7/site-packages/rfcat-1.9.5-py2.7.egg/EGG-INFO/scripts/rfcat of rfcat installed (unless the same executable is symbolically linked in /bin directory).

If rfcat does work without anaconda activated environment, then the culprit is most likely outdated lib inside conda environment. Would you be willing to post output of conda list after activating environment, where rfcat is installed?

rickgator commented 3 years ago

I reset everything, removed rfcat , libusb-compat, and the rftools env and everything associated. I then created a new environment "rf" and installed everything in the same way you explained.

conda activate rf
brew install libusb-compat
conda install pip
pip install pyusb
pip install ~/Downloads/rfcat/
pip install PySide2

I'm getting the same error, but I'll repost so that you can see locations in the Traceback:

 ~  sudo rfcat -r                                        ok | rf py | 08:46:05
Password:
Error in resetup():NoBackendError('No backend available')
Error in resetup():NoBackendError('No backend available')
Error in resetup():NoBackendError('No backend available')
^CTraceback (most recent call last):
  File "/opt/homebrew/anaconda3/envs/rf/lib/python3.9/site-packages/rflib/chipcon_usb.py", line 225, in resetup
    self.setup(console, copyDongle)
  File "/opt/homebrew/anaconda3/envs/rf/lib/python3.9/site-packages/rflib/chipcon_usb.py", line 141, in setup
    self._internal_select_dongle(console)
  File "/opt/homebrew/anaconda3/envs/rf/lib/python3.9/site-packages/rflib/chipcon_usb.py", line 152, in _internal_select_dongle
    for dev in getRfCatDevices():
  File "/opt/homebrew/anaconda3/envs/rf/lib/python3.9/site-packages/rflib/chipcon_usb.py", line 41, in getRfCatDevices
    for bus in usb.busses():
  File "/opt/homebrew/anaconda3/envs/rf/lib/python3.9/site-packages/usb/legacy.py", line 355, in busses
    sorted(core.find(find_all=True), key=lambda d: d.bus),
  File "/opt/homebrew/anaconda3/envs/rf/lib/python3.9/site-packages/usb/core.py", line 1299, in find
    raise NoBackendError('No backend available')
usb.core.NoBackendError: No backend available

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/homebrew/anaconda3/envs/rf/bin/rfcat", line 62, in <module>
    interactive(ifo.index, DongleClass=RfCat, intro=intro, safemode=ifo.safemode)
  File "/opt/homebrew/anaconda3/envs/rf/lib/python3.9/site-packages/rflib/__init__.py", line 208, in interactive
    d = DongleClass(idx=idx, debug=safemode, safemode=safemode)
  File "/opt/homebrew/anaconda3/envs/rf/lib/python3.9/site-packages/rflib/chipcon_nic.py", line 103, in __init__
    USBDongle.__init__(self, idx, debug, copyDongle, RfMode, safemode=safemode)
  File "/opt/homebrew/anaconda3/envs/rf/lib/python3.9/site-packages/rflib/chipcon_usb.py", line 93, in __init__
    self.resetup(copyDongle=copyDongle)
  File "/opt/homebrew/anaconda3/envs/rf/lib/python3.9/site-packages/rflib/chipcon_usb.py", line 238, in resetup
    time.sleep(1)
KeyboardInterrupt

This is my current conda list with this environment:

# packages in environment at /opt/homebrew/anaconda3/envs/rf:
#
# Name                    Version                   Build  Channel
appnope                   0.1.2                    pypi_0    pypi
backcall                  0.2.0                    pypi_0    pypi
ca-certificates           2021.5.25            hecd8cb5_1
certifi                   2021.5.30        py39hecd8cb5_0
decorator                 5.0.9                    pypi_0    pypi
future                    0.18.2                   pypi_0    pypi
ipython                   7.24.1                   pypi_0    pypi
ipython-genutils          0.2.0                    pypi_0    pypi
jedi                      0.18.0                   pypi_0    pypi
libcxx                    10.0.0                        1
libffi                    3.3                  hb1e8313_2
matplotlib-inline         0.1.2                    pypi_0    pypi
ncurses                   6.2                  h0a44026_1
numpy                     1.20.3                   pypi_0    pypi
openssl                   1.1.1k               h9ed2024_0
parso                     0.8.2                    pypi_0    pypi
pexpect                   4.8.0                    pypi_0    pypi
pickleshare               0.7.5                    pypi_0    pypi
pip                       21.1.1           py39hecd8cb5_0
prompt-toolkit            3.0.18                   pypi_0    pypi
ptyprocess                0.7.0                    pypi_0    pypi
pygments                  2.9.0                    pypi_0    pypi
pyserial                  3.5                      pypi_0    pypi
python                    3.9.5                h88f2d9e_3
pyusb                     1.1.1                    pypi_0    pypi
readline                  8.1                  h9ed2024_0
rfcat                     1.9.5                    pypi_0    pypi
setuptools                52.0.0           py39hecd8cb5_0
sqlite                    3.35.4               hce871da_0
tk                        8.6.10               hb0a8c7a_0
traitlets                 5.0.5                    pypi_0    pypi
tzdata                    2020f                h52ac0ba_0
wcwidth                   0.2.5                    pypi_0    pypi
wheel                     0.36.2             pyhd3eb1b0_0
xz                        5.2.5                h1de35cc_0
zlib                      1.2.11               h1de35cc_3
Loumiakas commented 3 years ago

All libraries seem to be there, but I see that you are invoking rfcat with elevated priveleges i.e. sudo. Did you try running it without it, as I don't think it's necessary to do so on OSX?

rickgator commented 3 years ago

Yes, I still get the same error. I just did it to make sure there was no error in pyusb accessing libraries. I'm going to keep looking at the find_library() function in util.py. It appears to make a list with '@executable_path/' directing the system where to look with the dyld_find() function from dyld.py. I'm going to keep digging to see if I can replicate this and get it to spit out the path it's finding.

UPDATE: I think I have found the issue. I have a Mac with the M1 Apple Silicon chip. Unfortunately, the architecture for the M1 chip requires Arm64 and Anaconda doesn't fully support this at the moment. I found information on builds with conda-forge here. I have provided the reason I have arrived at this conclusion below.

I went and made a test python file that replicated the dyld function. This helped me to find where it was looking for the files. The testing.py looked like this:

from ctypes.macholib.dyld import dyld_find
path = dyld_find('@executable_path/../lib/libusb-1.0.dylib')
print(path)

I pulled the '@executable_path/../lib/libusb-1.0.dylib' from the following the trace of the input to the find_library() function coming from core.py. I now how the libusb*.dylib files in a location that it could find which ended up being: /opt/homebrew/anaconda3/envs/rf/bin/../lib/libusb-1.0.dylib This is because this is the location of the rfcat executable.

This still did not resolve the error. After I got installed the libusb-1.0.dylib into the correct location, the location worked, but the _cytpes dlopen() function was unable to open the file giving me the following error:

OSError: dlopen(/opt/homebrew/anaconda3/envs/rf/bin/../lib/libusb-1.0.dylib, 6): no suitable image found.  Did find:
    /opt/homebrew/anaconda3/envs/rf/bin/../lib/libusb-1.0.dylib: mach-o, but wrong architecture
    /opt/homebrew/anaconda3/envs/rf/bin/../lib/libusb-1.0.dylib: stat() failed with errno=35

This led me down the architecture route. I was able to set the architecture of the env to Arm64 with conda-forge, but I kept getting the same error. Unfortunately, it looks like I may need to wait for Anaconda to provide better support for the architecture.

For those interested, running the following command after activating the environment is as follows: conda env config vars set CONDA_SUBDIR=osx-arm64

atlas0fd00m commented 8 months ago

cleaning up.