| 1 |
#!/usr/bin/env python3 |
| 2 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 3 |
# Copyright (C) 2018 Takashi Sakamoto |
| 4 |
|
| 5 |
import gi |
| 6 |
gi.require_version('GLib', '2.0') |
| 7 |
gi.require_version('Hinawa', '4.0') |
| 8 |
from gi.repository import GLib, Hinawa |
| 9 |
|
| 10 |
from hinawa_utils.bebob.plug_parser import PlugParser |
| 11 |
|
| 12 |
from sys import argv, exit |
| 13 |
from pathlib import Path |
| 14 |
from threading import Thread |
| 15 |
import json |
| 16 |
from pprint import PrettyPrinter |
| 17 |
|
| 18 |
def print_help(reason=""): |
| 19 |
if len(reason) > 0: |
| 20 |
print(reason) |
| 21 |
print('') |
| 22 |
print('Usage:') |
| 23 |
print(' {} CDEV MODE'.format(argv[0])) |
| 24 |
print('') |
| 25 |
print(' where:') |
| 26 |
print(' CDEV: the path to special file for Linux FireWire character device (e.g. /dev/fw[0-9]+)') |
| 27 |
print(' MODE: dump mode (0: id-only, 1: whole as json, 2: pretty print)') |
| 28 |
|
| 29 |
def dump_plug_info_to_stdio_as_json(fcp): |
| 30 |
unit_plugs = PlugParser.parse_unit_plugs(fcp) |
| 31 |
subunit_plugs = PlugParser.parse_subunit_plugs(fcp) |
| 32 |
function_block_plugs = PlugParser.parse_function_block_plugs(fcp, subunit_plugs) |
| 33 |
stream_formats = PlugParser.parse_stream_formats(fcp, unit_plugs) |
| 34 |
|
| 35 |
info = { |
| 36 |
'units': unit_plugs, |
| 37 |
'subunits': subunit_plugs, |
| 38 |
'function-blocks': function_block_plugs, |
| 39 |
'stream-formats': stream_formats, |
| 40 |
} |
| 41 |
print(json.dumps(info)) |
| 42 |
|
| 43 |
|
| 44 |
def dump_plug_info_to_stdio_as_ids_only(fcp): |
| 45 |
unit_plugs = PlugParser.parse_unit_plugs(fcp) |
| 46 |
subunit_plugs = PlugParser.parse_subunit_plugs(fcp) |
| 47 |
function_block_plugs = PlugParser.parse_function_block_plugs(fcp, subunit_plugs) |
| 48 |
stream_formats = PlugParser.parse_stream_formats(fcp, unit_plugs) |
| 49 |
|
| 50 |
for type, dir_plugs in unit_plugs.items(): |
| 51 |
for dir, plugs in dir_plugs.items(): |
| 52 |
for num, plug in plugs.items(): |
| 53 |
print(type, dir, num, plug['name']) |
| 54 |
|
| 55 |
for type, dir_plugs in subunit_plugs.items(): |
| 56 |
for id, id_plugs in dir_plugs.items(): |
| 57 |
for dir, dir_plugs in id_plugs.items(): |
| 58 |
for plug_id, attrs in dir_plugs.items(): |
| 59 |
print(type, id, dir, plug_id, attrs['name']) |
| 60 |
|
| 61 |
for type, type_plugs in function_block_plugs.items(): |
| 62 |
for id, id_plugs in type_plugs.items(): |
| 63 |
for fb_type, fb_type_plugs in id_plugs.items(): |
| 64 |
for fb_id, fb_id_plugs in fb_type_plugs.items(): |
| 65 |
for plug_id, attrs in fb_id_plugs['outputs'].items(): |
| 66 |
print(type, id, fb_type, fb_id, 'output', plug_id, |
| 67 |
attrs['name']) |
| 68 |
for plug_id, attrs in fb_id_plugs['inputs'].items(): |
| 69 |
print(type, id, fb_type, fb_id, 'input', plug_id, |
| 70 |
attrs['name']) |
| 71 |
|
| 72 |
for type, dir_fmts in stream_formats.items(): |
| 73 |
for dir, fmts in dir_fmts.items(): |
| 74 |
for i, entries in fmts.items(): |
| 75 |
for j, entry in enumerate(entries): |
| 76 |
print(type, dir, i, j, entry['type']) |
| 77 |
|
| 78 |
def dump_plug_info_to_stdio_by_pprinter(fcp): |
| 79 |
unit_plugs = PlugParser.parse_unit_plugs(fcp) |
| 80 |
subunit_plugs = PlugParser.parse_subunit_plugs(fcp) |
| 81 |
function_block_plugs = PlugParser.parse_function_block_plugs(fcp, subunit_plugs) |
| 82 |
stream_formats = PlugParser.parse_stream_formats(fcp, unit_plugs) |
| 83 |
|
| 84 |
pp = PrettyPrinter() |
| 85 |
pp.pprint(unit_plugs) |
| 86 |
pp.pprint(subunit_plugs) |
| 87 |
pp.pprint(function_block_plugs) |
| 88 |
pp.pprint(stream_formats) |
| 89 |
|
| 90 |
ops = { |
| 91 |
'0': dump_plug_info_to_stdio_as_ids_only, |
| 92 |
'1': dump_plug_info_to_stdio_as_json, |
| 93 |
'2': dump_plug_info_to_stdio_by_pprinter, |
| 94 |
} |
| 95 |
|
| 96 |
if len(argv) < 3: |
| 97 |
print_help("Missing arguments") |
| 98 |
exit(1) |
| 99 |
|
| 100 |
path = Path(argv[1]) |
| 101 |
if not path.exists(): |
| 102 |
print_help("{} is not found.¥n".format(path)) |
| 103 |
exit(1) |
| 104 |
elif not path.is_char_device(): |
| 105 |
print_help("{} is not special file for character device.".format(path)) |
| 106 |
exit(1) |
| 107 |
fullpath = str(path) |
| 108 |
|
| 109 |
mode = argv[2] |
| 110 |
if mode not in ops: |
| 111 |
print_help('Invalid mode') |
| 112 |
exit(1) |
| 113 |
op = ops[mode] |
| 114 |
|
| 115 |
node = Hinawa.FwNode.new() |
| 116 |
fcp = Hinawa.FwFcp.new() |
| 117 |
ctx = GLib.MainContext.new() |
| 118 |
dispatcher = GLib.MainLoop.new(ctx, False) |
| 119 |
|
| 120 |
try: |
| 121 |
node.open(fullpath, 0) |
| 122 |
_ = fcp.bind(node) |
| 123 |
|
| 124 |
_, src = node.create_source() |
| 125 |
src.attach(ctx) |
| 126 |
th = Thread(target=lambda d: d.run(), args=(dispatcher,)) |
| 127 |
th.start() |
| 128 |
|
| 129 |
op(fcp) |
| 130 |
except Exception as e: |
| 131 |
print(e) |
| 132 |
finally: |
| 133 |
fcp.unbind() |
| 134 |
dispatcher.quit() |
| 135 |
th.join() |