Debian Codegraph @

hinawa-bebob-connection-cli

raw
1 #!/usr/bin/env python3
2
3 import gi
4 gi.require_version('GLib', '2.0')
5 gi.require_version('Hinawa', '4.0')
6 from gi.repository import GLib, Hinawa
7
8 from hinawa_utils.bebob.plug_parser import PlugParser
9 from hinawa_utils.bebob.extensions import BcoPlugInfo
10
11 from sys import argv, exit
12 from pathlib import Path
13 from threading import Thread
14
15 def handle_dump_connections(fcp):
16 unit_plug_list = PlugParser.get_unit_plug_list(fcp)
17 subunit_plug_list = PlugParser.get_subunit_plug_list(fcp)
18
19 conns = PlugParser.get_avail_connections(fcp, unit_plug_list, subunit_plug_list)
20
21 if len(conns) == 0:
22 print('nothing avail.')
23 return True
24
25 for dst_seqid, avails in conns.items():
26 if dst_seqid in unit_plug_list:
27 info = unit_plug_list[dst_seqid]
28 elif dst_seqid in subunit_plug_list:
29 info = subunit_plug_list[dst_seqid]
30 else:
31 return False
32
33 dst_spec = PlugParser.get_plug_spec(fcp, info)
34 print('{0} ({1})'.format(dst_spec['name'], dst_seqid))
35
36 for avail in avails:
37 src_seqid, used = avail
38 if src_seqid in unit_plug_list:
39 info = unit_plug_list[src_seqid]
40 elif src_seqid in subunit_plug_list:
41 info = subunit_plug_list[src_seqid]
42 else:
43 return False
44
45 src_spec = PlugParser.get_plug_spec(fcp, info)
46 if used:
47 print(' < {0} ({1})'.format(src_spec['name'], src_seqid))
48 else:
49 print(' * {0} ({1})'.format(src_spec['name'], src_seqid))
50
51 return True
52
53 def handle_graph_connections(fcp):
54 unit_plug_list = PlugParser.get_unit_plug_list(fcp)
55 subunit_plug_list = PlugParser.get_subunit_plug_list(fcp)
56
57 specs = {}
58 for plug_list in (unit_plug_list, subunit_plug_list):
59 for seqid, info in plug_list.items():
60 spec = PlugParser.get_plug_spec(fcp, info)
61 specs[seqid] = spec
62
63 unit_plugs = {}
64 subunit_plugs = {}
65
66 for seqid, info in unit_plug_list.items():
67 dir = info['dir']
68 mode = info['mode']
69 data = info['data']
70 unit_type = data['unit-type']
71 if unit_type not in unit_plugs:
72 unit_plugs[unit_type] = {}
73 if dir not in unit_plugs[unit_type]:
74 unit_plugs[unit_type][dir] = {}
75 unit_plugs[unit_type][dir][seqid] = info
76
77 for seqid, info in subunit_plug_list.items():
78 dir = info['dir']
79 mode = info['mode']
80 data = info['data']
81 subunit_type = (data['subunit-type'], data['subunit-id'])
82 if subunit_type not in subunit_plugs:
83 subunit_plugs[subunit_type] = {}
84 if dir not in subunit_plugs[subunit_type]:
85 subunit_plugs[subunit_type][dir] = {}
86 subunit_plugs[subunit_type][dir][seqid] = info
87
88 print('digraph {')
89 print(' rankdir = "LR"')
90
91 for unit_type, entries in unit_plugs.items():
92 if len(entries) == 0:
93 continue
94
95 for dir, plugs in entries.items():
96 print(' subgraph cluster_unit_{0}_{1} {{'.format(unit_type, dir))
97 print(' label = "{0} {1}"'.format(unit_type, dir))
98 for seqid, info in plugs.items():
99 spec = specs[seqid]
100 label = '{0} ({1})'.format(spec['name'], spec['type'])
101 shape = 'box' if spec['type'] == 'Sync' else 'ellipse'
102 print(' {0} [label = "{1}", shape = "{2}"]'.format(
103 seqid, label, shape))
104 print(' }')
105
106 for subunit_type, entries in subunit_plugs.items():
107 if len(entries) == 0:
108 continue
109
110 print(' subgraph cluster_subunit_{0[0]}_{0[1]} {{'.format(subunit_type))
111 print(' label = "{0[0]} subunit {0[1]}"'.format(subunit_type))
112
113 for dir, plugs in entries.items():
114 print(' subgraph cluster_subunit_{0[0]}_{0[1]}_{1} {{'.format(
115 subunit_type, dir))
116 print(' label = "{0}"'.format(dir))
117 for seqid, info in plugs.items():
118 spec = specs[seqid]
119 label = '{0} ({1})'.format(spec['name'], spec['type'])
120 shape = 'box' if spec['type'] == 'Sync' else 'ellipse'
121 print(' {0} [label = "{1}", shape = "{2}"]'.format(
122 seqid, label, shape))
123 print(' }')
124 print(' }')
125
126 conns = PlugParser.get_avail_connections(fcp, unit_plug_list, subunit_plug_list)
127 for dst_seqid, avails in conns.items():
128 dst_spec = specs[dst_seqid]
129 for avail in avails:
130 src_seqid, used = avail
131 src_spec = specs[src_seqid]
132
133 decorations = []
134 if not used:
135 decorations.append('style=dashed')
136 if dst_spec['type'] == 'Sync' or src_spec['type'] == 'Sync':
137 decorations.append('constraint=false')
138 if len(decorations) > 0:
139 decoration = '[{0}]'.format(' '.join(decorations))
140 else:
141 decoration = ''
142
143 print(' {0} -> {1} {2}'.format(src_seqid, dst_seqid, decoration))
144
145 print('}')
146
147 return True
148
149 cmds = {
150 'dump-connections': handle_dump_connections,
151 'graph-connections': handle_graph_connections,
152 }
153
154 def print_help(reason=""):
155 if len(reason) > 0:
156 print(reason)
157 print("")
158 print("$ {} CDEV MODE".format(argv[0]))
159 print(" CDEV: path to special file for Linux Firewire character device (e.g. /dev/fw1)")
160 print(" MODE: {}".format(" | ".join(cmds)))
161
162 if len(argv) < 3:
163 print_help("Missing arguments")
164 exit(1)
165
166 path = Path(argv[1])
167 if not path.exists():
168 print_help("{} is not found.¥n".format(path))
169 exit(1)
170 elif not path.is_char_device():
171 print_help("{} is not special file for character device.".format(path))
172 exit(1)
173 fullpath = str(path)
174
175 node = Hinawa.FwNode.new()
176 try:
177 node.open(fullpath, 0)
178 except Exception as e:
179 print_help("Fail to operate {}: {}".format(fullpath, e))
180 exit(1)
181
182 mode = argv[2]
183 if mode not in cmds:
184 print_help("{} is not supported.".format(mode))
185 exit(1)
186 op = cmds[mode]
187
188 ctx = GLib.MainContext.new()
189 _, src = node.create_source()
190 src.attach(ctx)
191
192 dispatcher = GLib.MainLoop.new(ctx, False)
193 th = Thread(target = lambda d: d.run(), args=(dispatcher,))
194 th.start()
195
196 try:
197 fcp = Hinawa.FwFcp.new()
198 _ = fcp.bind(node)
199 op(fcp)
200 except Exception as e:
201 print(e)
202 finally:
203 fcp.unbind()
204
205 dispatcher.quit()
206 th.join()