Debian Codegraph @

samples/iso-resource

raw
1 #!/usr/bin/env python3
2
3 import gi
4 gi.require_versions({'GLib': '2.0', 'Hinoko': '1.0'})
5 from gi.repository import GLib, Hinoko
6
7 from threading import Thread
8 from time import sleep
9
10 bytes_per_payload = (2 + 8 * 3) * 4
11 use_channel = 10
12 use_bandwidth = Hinoko.FwIsoResource.calculate_bandwidth(bytes_per_payload,
13 Hinoko.FwScode.S400)
14
15 ctx = GLib.MainContext.new()
16 dispatcher = GLib.MainLoop.new(ctx, False)
17 th = Thread(target=lambda d: d.run(), args=(dispatcher,))
18 th.start()
19
20 def handle_event(res, channel, bandwidth, exception, ev_name):
21 print('event:', ev_name)
22 if exception is None:
23 print(' ', channel, bandwidth, res.get_property('generation'))
24 elif exception.matches(Hinoko.fw_iso_resource_error_quark(), Hinoko.FwIsoResourceError.EVENT):
25 print(' ', exception)
26
27 # For Once mode. The allocation is bound to current generation of the bus and
28 # lost automatically by bus reset without notification.
29 res = Hinoko.FwIsoResourceOnce.new()
30 res.open('/dev/fw0', 0)
31 res.connect('allocated', handle_event, 'allocated')
32 res.connect('deallocated', handle_event, 'deallocated')
33
34 _, src = res.create_source()
35 src.attach(ctx)
36
37 for i in range(2):
38 try:
39 res.allocate_wait((use_channel, ), use_bandwidth, 100)
40 except GLib.Error as e:
41 if e.matches(Hinoko.fw_iso_resource_error_quark(), Hinoko.FwIsoResourceError.EVENT):
42 print(e)
43 else:
44 print('result: allocate')
45 print(' ', use_channel, use_bandwidth, res.get_property('generation'))
46
47 sleep(2)
48
49 try:
50 res.deallocate_wait(use_channel, use_bandwidth, 100)
51 except GLib.Error as e:
52 if e.matches(Hinoko.fw_iso_resource_error_quark(), Hinoko.FwIsoResourceError.EVENT):
53 print(e)
54 else:
55 print('result: deallocate')
56 print(' ', use_channel, use_bandwidth, res.get_property('generation'))
57
58 sleep(2)
59
60 src.destroy()
61 del res
62
63 # For Auto mode. The allocation is kept by Linux FireWire subsystem as long as
64 # the lifetime of file descriptor. The instance of object represents state
65 # machine.
66 res = Hinoko.FwIsoResourceAuto.new()
67 res.open('/dev/fw0', 0)
68 res.connect('allocated', handle_event, 'allocated')
69 res.connect('deallocated', handle_event, 'deallocated')
70
71 _, src = res.create_source()
72 src.attach(ctx)
73
74 def print_props(res):
75 for prop in ('is-allocated', 'channel', 'bandwidth', 'generation'):
76 print(' {}: {}'.format(prop, res.get_property(prop)))
77
78 for i in range(2):
79 print_props(res)
80
81 try:
82 res.allocate_wait([use_channel], use_bandwidth, 100)
83 except GLib.Error as e:
84 if e.matches(Hinoko.fw_iso_resource_error_quark(), Hinoko.FwIsoResourceError.EVENT):
85 print(e)
86 else:
87 print('result: allocate')
88 print_props(res)
89
90 sleep(1)
91
92 try:
93 res.deallocate_wait(100)
94 except GLib.Error as e:
95 if e.matches(Hinoko.fw_iso_resource_error_quark(), Hinoko.FwIsoResourceError.EVENT):
96 print(e)
97 else:
98 print('result: deallocate')
99 print_props(res)
100
101 sleep(1)
102
103 dispatcher.quit()
104 th.join()