Vasil Velichkov | 54580d1 | 2019-04-17 18:28:46 +0300 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # @file |
| 4 | # @author (C) 2019 by Vasil Velichkov <vvvelichkov@gmail.com> |
| 5 | # @section LICENSE |
| 6 | # |
| 7 | # Gr-gsm is free software; you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as published by |
| 9 | # the Free Software Foundation; either version 3, or (at your option) |
| 10 | # any later version. |
| 11 | # |
| 12 | # Gr-gsm is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License |
| 18 | # along with gr-gsm; see the file COPYING. If not, write to |
| 19 | # the Free Software Foundation, Inc., 51 Franklin Street, |
| 20 | # Boston, MA 02110-1301, USA. |
| 21 | # |
| 22 | # |
| 23 | |
| 24 | import osmosdr |
| 25 | import os |
| 26 | |
| 27 | def get_devices(hint=""): |
| 28 | return osmosdr.device_find(osmosdr.device_t(hint)) |
| 29 | |
| 30 | def match(dev, filters): |
| 31 | for f in filters: |
| 32 | for k, v in f.items(): |
| 33 | if (k not in dev or dev[k] != v): |
| 34 | break |
| 35 | else: |
| 36 | return True |
| 37 | return False |
| 38 | |
| 39 | def exclude(devices, filters = ({'driver': 'audio'},)): |
| 40 | return [dev for dev in devices if not match(dev, filters)] |
| 41 | |
| 42 | def get_all_args(hint="nofake"): |
Vasil Velichkov | 46c90be | 2019-09-02 04:06:41 +0300 | [diff] [blame] | 43 | return list(map(lambda dev: dev.to_string(), exclude(get_devices(hint)))) |
Vasil Velichkov | 54580d1 | 2019-04-17 18:28:46 +0300 | [diff] [blame] | 44 | |
| 45 | def get_default_args(args): |
| 46 | # The presence of GRC_BLOCKS_PATH environment variable indicates that |
| 47 | # gnuradio-companion compiles a flowgraph and in this case no exception |
| 48 | # have to be thrown otherwise the generaged python script will be invalid. |
| 49 | # This allows compilation of flowgraphs without an SDR device. |
| 50 | if args or os.getenv("GRC_BLOCKS_PATH"): |
| 51 | return args |
| 52 | |
| 53 | devices = get_all_args("nofake") |
| 54 | if not devices: |
| 55 | raise RuntimeError("Unable to find any supported SDR devices") |
| 56 | |
| 57 | return devices[0] |
| 58 | |
| 59 | def print_devices(hint=""): |
| 60 | devices = exclude(get_devices(hint)) |
| 61 | if devices: |
| 62 | print("\n".join(map(lambda dev: dev.to_string(), devices))) |
| 63 | else: |
| 64 | print("Unable to find any supported SDR devices") |