Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix grgsm_scanner -l #646

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Fix grgsm_scanner -l #646

wants to merge 1 commit into from

Conversation

0xxstone
Copy link

@0xxstone 0xxstone commented Oct 26, 2024

When running the command grgsm_scanner -l, the following exception is raised:

Traceback (most recent call last):
  File "/usr/bin/grgsm_scanner", line 797, in <module>
    main()
  File "/usr/bin/grgsm_scanner", line 774, in main
    gsm.device.print_devices(options.args)
  File "/usr/lib/python3/dist-packages/gnuradio/gsm/device.py", line 72, in print_devices
    devices = exclude(get_devices(hint))
              ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/gnuradio/gsm/device.py", line 52, in exclude
    return [dev for dev in devices if not match(dev, filters)]
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/gnuradio/gsm/device.py", line 52, in <listcomp>
    return [dev for dev in devices if not match(dev, filters)]
                                          ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/gnuradio/gsm/device.py", line 45, in match
    if (k not in dev or dev[k] != v):
        ^^^^^^^^^^^^
TypeError: argument of type 'osmosdr.osmosdr_python.device_t' is not iterable

By looking at the error we can see the that issue is that dev is used as a if it was a dict but it's actually a device_t. This pull request adds a helper function to convert a device_t to a dict so the existing code can work as expected.

The conversion is done by using the to_string method which is available on objects of type device_t which can give strings like this: "label='RFSPACE SDR-IQ Receiver',sdr-iq=/dev/ttyUSB0" or this "default_input=False,default_output=False,device_id=3,driver=audio,label='Monitor of USB Audio',soapy=2". Then the string is parsed into pairs of key/value using parse_qsl and finally the values, which are still strings at this point, are evaluated with literal_eval .


def get_devices(hint=""):
return osmosdr.device_find(osmosdr.device_t(hint))

def device_to_dict(dev):
dev_dict = {}
for k, v in parse_qsl(dev.to_string(), separator=","):
Copy link
Author

@0xxstone 0xxstone Oct 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to avoid the import of parse_qsl, we can also do it this way:

def device_to_dict(dev):
    dev_dict = {}
    for pair in dev.split(","):
        k, v = pair.split("=")
        try:
            dev_dict[k] = literal_eval(v)
        except (ValueError, SyntaxError):
            dev_dict[k] = literal_eval(f"'{v}'")
    return dev_dict

Comment on lines +35 to +38
try:
dev_dict[k] = literal_eval(v)
except (ValueError, SyntaxError):
dev_dict[k] = literal_eval(f"'{v}'")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This try/except is required to handle the cases where the values returned by to_string are strings without quotes or strings without quotes starting with / like /dev/ttyUSB0 for example.

@0xxstone
Copy link
Author

This PR should also fix #636

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant