| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
from threading import Timer |
| 4 |
from struct import unpack |
| 5 |
|
| 6 |
import gi |
| 7 |
gi.require_versions({'GLib': '2.0', 'Hinoko': '1.0'}) |
| 8 |
from gi.repository import GLib, Hinoko |
| 9 |
|
| 10 |
class IsoIrMultiple(Hinoko.FwIsoIrMultiple): |
| 11 |
def new(self, path, bytes_per_packet, syt_interval): |
| 12 |
channels = [i for i in range(4)] |
| 13 |
self.allocate(path, channels) |
| 14 |
|
| 15 |
channels = self.get_property('channels') |
| 16 |
bytes_per_chunk = len(channels) * bytes_per_packet * syt_interval |
| 17 |
|
| 18 |
self.map_buffer(4096, 32) |
| 19 |
|
| 20 |
def destroy(self): |
| 21 |
self.unmap_buffer() |
| 22 |
self.release() |
| 23 |
|
| 24 |
def begin(self, dispatcher, duration): |
| 25 |
_, self.__src = self.create_source() |
| 26 |
self.__src.attach(dispatcher.get_context()) |
| 27 |
self.__dispatcher = dispatcher |
| 28 |
|
| 29 |
# Roughly finish event loop. |
| 30 |
self.__timer = Timer(duration, lambda dispatcher: dispatcher.quit(), |
| 31 |
args=(dispatcher,)) |
| 32 |
|
| 33 |
tags = Hinoko.FwIsoCtxMatchFlag.TAG0 | \ |
| 34 |
Hinoko.FwIsoCtxMatchFlag.TAG1 | \ |
| 35 |
Hinoko.FwIsoCtxMatchFlag.TAG2 | \ |
| 36 |
Hinoko.FwIsoCtxMatchFlag.TAG3 |
| 37 |
|
| 38 |
self.__timer.start() |
| 39 |
self.start(None, 0, tags, 4) |
| 40 |
|
| 41 |
def finish(self): |
| 42 |
self.__src.destroy() |
| 43 |
self.stop() |
| 44 |
|
| 45 |
def do_stopped(self, error): |
| 46 |
self.__dispatcher.quit() |
| 47 |
if error: |
| 48 |
self.__timer.cancel() |
| 49 |
print(error) |
| 50 |
|
| 51 |
@staticmethod |
| 52 |
def __ohci1394_tstamp_to_isoc_cycle(tstamp): |
| 53 |
sec = (tstamp & 0x0000e000) >> 13 |
| 54 |
cycle = tstamp & 0x00001fff |
| 55 |
return (sec, cycle) |
| 56 |
|
| 57 |
def do_interrupted(self, count): |
| 58 |
for i in range(count): |
| 59 |
data = self.get_payload(i) |
| 60 |
|
| 61 |
iso_header = unpack('<I', data[0:4])[0] |
| 62 |
|
| 63 |
frames = unpack('>{0}I'.format(len(data[4:-4]) // 4), data[4:-4]) |
| 64 |
|
| 65 |
tstamp = unpack('<I', data[-4:])[0] |
| 66 |
sec, cycle = self.__ohci1394_tstamp_to_isoc_cycle(tstamp) |
| 67 |
|
| 68 |
print('{0:d},{1:d},{2:08x},{3[0]:08x},{3[1]:08x},{4}'.format(sec, cycle, iso_header, frames, i)) |
| 69 |
for i, frame in enumerate(frames): |
| 70 |
print(' {0:2d}: {1:08x}'.format(i, frame)) |
| 71 |
|
| 72 |
# 2 CIP headers + 19 quadlet per data block * 8 data block + iso header + tstamp |
| 73 |
bytes_per_packet = (2 + 19 * 8 + 2) * 4 |
| 74 |
|
| 75 |
ctx = IsoIrMultiple() |
| 76 |
ctx.new('/dev/fw0', bytes_per_packet, 8) |
| 77 |
|
| 78 |
dispatcher = GLib.MainLoop.new(None, False) |
| 79 |
|
| 80 |
ctx.begin(dispatcher, 4) |
| 81 |
dispatcher.run() |
| 82 |
ctx.finish() |
| 83 |
ctx.destroy() |
| 84 |
|
| 85 |
del dispatcher |
| 86 |
del ctx |