Debian Codegraph @

hinawa-dg00x-common-cli

raw
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: GPL-3.0-or-later
3 # Copyright (C) 2018 Takashi Sakamoto
4
5 import signal
6
7 from gi.repository import GLib
8
9 from hinawa_utils.misc.cli_kit import CliKit
10 from hinawa_utils.dg00x.dg00x_unit import Dg00xUnit
11
12
13 def handle_clock_source(unit, args):
14 ops = ('set', 'get')
15 if len(args) > 0 and args[0] in ops:
16 op = args[0]
17 if op == ops[0] and len(args) == 2:
18 if unit.get_property('is-locked'):
19 print('Packet is-locked started.')
20 return False
21 source = args[1]
22 unit.set_clock_source(source)
23 else:
24 print(unit.get_clock_source())
25 return True
26 print('Arguments for clock-source command:')
27 print(' clock-source OPERATION [SOURCE]')
28 print(' OPERATION: {0}'.format('|'.join(ops)))
29 print(' SOURCE: {0}'.format('|'.join(unit.SUPPORTED_CLOCK_SOURCES)))
30 return False
31
32
33 def handle_sampling_rate(unit, args):
34 ops = ('set', 'get')
35 if len(args) > 0 and args[0] in ops:
36 op = args[0]
37 if op == ops[0] and len(args) == 2:
38 if unit.get_property('is-locked'):
39 print('Packet is-locked started.')
40 return False
41 rate = int(args[1])
42 unit.set_local_sampling_rate(rate)
43 else:
44 print(unit.get_local_sampling_rate())
45 return True
46 print('Arguments for sampling-rate command:')
47 print(' sampling-rate OPERATION [RATE]')
48 print(' OPERATION: {0}'.format('|'.join(ops)))
49 rates = [str(r) for r in unit.SUPPORTED_SAMPLING_RATES]
50 print(' RATE: {0}'.format('|'.join(rates)))
51 return False
52
53
54 def handle_mixer_mode(unit, args):
55 ops = ('set', 'get')
56 if len(args) > 0 and args[0] in ops:
57 op = args[0]
58 if op == ops[0] and len(args) == 2:
59 mode = int(args[1])
60 unit.set_mixer_mode(mode)
61 else:
62 print(unit.get_mixer_mode())
63 return True
64 print('Arguments for mixer-mode command:')
65 print(' sampling-rate OPERATION [MODE]')
66 print(' OPERATION: {0}'.format('|'.join(ops)))
67 print(' MODE: 0, 1')
68 return False
69
70
71 def handle_opt_iface(unit, args):
72 ops = ('set', 'get')
73 if len(args) > 0 and args[0] in ops:
74 op = args[0]
75 if op == ops[0] and len(args) == 2:
76 if unit.get_property('is-locked'):
77 print('Packet is-locked started.')
78 return False
79 mode = args[1]
80 unit.set_opt_iface(mode)
81 else:
82 print(unit.get_opt_iface())
83 return True
84 print('Arguments for optical-interface command:')
85 print(' optical-interface OPERATION [MODE]')
86 print(' OPERATION: {0}'.format('|'.join(ops)))
87 print(' MODE: {0}'.format(
88 '|'.join(unit.SUPPORTED_OPTICAL_INTERFACES)))
89 return False
90
91
92 def handle_listen_message(self, args):
93 loop = GLib.MainLoop()
94
95 def handle_unix_signal():
96 loop.quit()
97
98 def handle_message(unit, message):
99 print('{0:08x}'.format(message))
100
101 def handle_disconnect(unit, loop):
102 loop.quit()
103 self.connect('notified', handle_message)
104 GLib.unix_signal_add(GLib.PRIORITY_HIGH, signal.SIGINT, handle_unix_signal)
105 self.connect('notify::is-disconnected', handle_disconnect, loop)
106 loop.run()
107 return True
108
109
110 cmds = {
111 'clock-source': handle_clock_source,
112 'sampling-rate': handle_sampling_rate,
113 'mixer-mode': handle_mixer_mode,
114 'optical-interface': handle_opt_iface,
115 'listen-message': handle_listen_message,
116 }
117
118 fullpath = CliKit.seek_snd_unit_path()
119 if fullpath:
120 with Dg00xUnit(fullpath) as unit:
121 CliKit.dispatch_command(unit, cmds)