| 1 | # SPDX-License-Identifier: GPL-2.0-only |
| 2 | # Copyright (C) 2020 Geneviève Bastien <gbastien@versatic.net> |
| 3 | |
| 4 | import bt2 |
| 5 | |
| 6 | |
| 7 | class TheIteratorOfProblems(bt2._UserMessageIterator): |
| 8 | def __init__(self, config, port): |
| 9 | tc, sc, ec1, params = port.user_data |
| 10 | trace = tc() |
| 11 | stream = trace.create_stream(sc) |
| 12 | event_value = params['value'] |
| 13 | self._msgs = [] |
| 14 | |
| 15 | self._msgs.append(self._create_stream_beginning_message(stream)) |
| 16 | |
| 17 | ev_msg1 = self._create_event_message(ec1, stream) |
| 18 | ev_msg1.event.payload_field["enum_field"] = event_value |
| 19 | |
| 20 | self._msgs.append(ev_msg1) |
| 21 | |
| 22 | self._msgs.append(self._create_stream_end_message(stream)) |
| 23 | |
| 24 | self._at = 0 |
| 25 | config.can_seek_forward = True |
| 26 | |
| 27 | def _user_seek_beginning(self): |
| 28 | self._at = 0 |
| 29 | |
| 30 | def __next__(self): |
| 31 | if self._at < len(self._msgs): |
| 32 | msg = self._msgs[self._at] |
| 33 | self._at += 1 |
| 34 | return msg |
| 35 | else: |
| 36 | raise StopIteration |
| 37 | |
| 38 | |
| 39 | @bt2.plugin_component_class |
| 40 | class TheSourceOfProblems( |
| 41 | bt2._UserSourceComponent, message_iterator_class=TheIteratorOfProblems |
| 42 | ): |
| 43 | def __init__(self, config, params, obj): |
| 44 | tc = self._create_trace_class() |
| 45 | |
| 46 | enum_values_str = params['enum-values'] |
| 47 | |
| 48 | sc = tc.create_stream_class() |
| 49 | |
| 50 | # Create the enumeration field with the values in parameter |
| 51 | if params['enum-signed']: |
| 52 | enumfc = tc.create_signed_enumeration_field_class() |
| 53 | else: |
| 54 | enumfc = tc.create_unsigned_enumeration_field_class() |
| 55 | |
| 56 | groups = str(enum_values_str).split(' ') |
| 57 | mappings = {} |
| 58 | range_set_type = ( |
| 59 | bt2.SignedIntegerRangeSet |
| 60 | if params['enum-signed'] |
| 61 | else bt2.UnsignedIntegerRangeSet |
| 62 | ) |
| 63 | for group in groups: |
| 64 | label, low, high = group.split(',') |
| 65 | |
| 66 | if label not in mappings.keys(): |
| 67 | mappings[label] = range_set_type() |
| 68 | |
| 69 | mappings[label].add((int(low), int(high))) |
| 70 | |
| 71 | for x, y in mappings.items(): |
| 72 | enumfc.add_mapping(x, y) |
| 73 | |
| 74 | # Create the struct field to contain the enum field class |
| 75 | struct_fc = tc.create_structure_field_class() |
| 76 | struct_fc.append_member('enum_field', enumfc) |
| 77 | |
| 78 | # Create an event class on this stream with the struct field |
| 79 | ec1 = sc.create_event_class(name='with_enum', payload_field_class=struct_fc) |
| 80 | self._add_output_port('out', (tc, sc, ec1, params)) |
| 81 | |
| 82 | |
| 83 | bt2.register_plugin(__name__, 'test-pretty') |