# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import barectf.version as barectf_version
-from typing import Optional, Any, FrozenSet, Mapping, Iterator, Set, Union
+from typing import Optional, Any, FrozenSet, Mapping, Iterator, Set, Union, Callable
import typing
from barectf.typing import Count, Alignment, _OptStr, Id
import collections.abc
class _BitArrayFieldType(_FieldType):
- def __init__(self, size: Count, byte_order: Optional[ByteOrder] = None,
- alignment: Alignment = Alignment(1)):
+ def __init__(self, size: Count, byte_order: ByteOrder, alignment: Alignment = Alignment(1)):
self._size = size
self._byte_order = byte_order
self._alignment = alignment
return self._size
@property
- def byte_order(self) -> Optional[ByteOrder]:
+ def byte_order(self) -> ByteOrder:
return self._byte_order
@property
class _IntegerFieldType(_BitArrayFieldType):
- def __init__(self, size: Count, byte_order: Optional[ByteOrder] = None,
- alignment: Optional[Alignment] = None,
+ def __init__(self, size: Count, byte_order: ByteOrder, alignment: Optional[Alignment] = None,
preferred_display_base: DisplayBase = DisplayBase.DECIMAL):
if alignment is None:
alignment = Alignment(8 if size % 8 == 0 else 1)
class _EnumerationFieldType(_IntegerFieldType):
- def __init__(self, size: Count, byte_order: Optional[ByteOrder] = None,
- alignment: Optional[Alignment] = None,
+ def __init__(self, size: Count, byte_order: ByteOrder, alignment: Optional[Alignment] = None,
preferred_display_base: DisplayBase = DisplayBase.DECIMAL,
mappings: Optional[_EnumFtMappings] = None):
super().__init__(size, byte_order, alignment, preferred_display_base)
content_size_field_type: _DefaultableUIntFt = DEFAULT_FIELD_TYPE,
beginning_time_field_type: _OptDefaultableUIntFt = None,
end_time_field_type: _OptDefaultableUIntFt = None,
- discarded_events_counter_field_type: _OptDefaultableUIntFt = None):
+ discarded_events_counter_field_type: _OptDefaultableUIntFt = None,
+ default_byte_order: Optional[ByteOrder] = None):
def get_ft(user_ft: _OptDefaultableUIntFt) -> _OptUIntFt:
if user_ft == DEFAULT_FIELD_TYPE:
- return UnsignedIntegerFieldType(64)
+ assert default_byte_order is not None
+ return UnsignedIntegerFieldType(64, typing.cast(ByteOrder, default_byte_order))
return typing.cast(_OptUIntFt, user_ft)
class StreamTypeEventFeatures:
def __init__(self, type_id_field_type: _OptDefaultableUIntFt = DEFAULT_FIELD_TYPE,
- time_field_type: _OptDefaultableUIntFt = None):
+ time_field_type: _OptDefaultableUIntFt = None,
+ default_byte_order: Optional[ByteOrder] = None):
def get_ft(user_ft: _OptDefaultableUIntFt) -> _OptUIntFt:
if user_ft == DEFAULT_FIELD_TYPE:
- return UnsignedIntegerFieldType(64)
+ assert default_byte_order is not None
+ return UnsignedIntegerFieldType(64, typing.cast(ByteOrder, default_byte_order))
return typing.cast(_OptUIntFt, user_ft)
class StreamTypeFeatures:
def __init__(self, packet_features: Optional[StreamTypePacketFeatures] = None,
- event_features: Optional[StreamTypeEventFeatures] = None):
- self._packet_features = StreamTypePacketFeatures()
-
- if packet_features is not None:
+ event_features: Optional[StreamTypeEventFeatures] = None,
+ default_byte_order: Optional[ByteOrder] = None):
+ if packet_features is None:
+ self._packet_features = StreamTypePacketFeatures(default_byte_order=default_byte_order)
+ else:
self._packet_features = packet_features
- self._event_features = StreamTypeEventFeatures()
- if event_features is not None:
+ if event_features is None:
+ self._event_features = StreamTypeEventFeatures(default_byte_order=default_byte_order)
+ else:
self._event_features = event_features
@property
def __init__(self, name: str, event_types: Set[EventType],
default_clock_type: Optional[ClockType] = None,
features: Optional[StreamTypeFeatures] = None,
+ default_feature_field_type_byte_order: Optional[ByteOrder] = None,
packet_context_field_type_extra_members: Optional[_StructFtMembers] = None,
event_common_context_field_type: _OptStructFt = None):
self._id: Optional[Id] = None
assert ev_type._id is None
ev_type._id = Id(index)
- self._set_features(features)
+ self._set_features(features, default_feature_field_type_byte_order)
self._packet_context_field_type_extra_members = StructureFieldTypeMembers({})
if packet_context_field_type_extra_members is not None:
self._set_pkt_ctx_ft()
self._set_ev_header_ft()
- def _set_features(self, features: Optional[StreamTypeFeatures]):
+ def _set_features(self, features: Optional[StreamTypeFeatures],
+ default_byte_order: Optional[ByteOrder]):
if features is not None:
self._features = features
return None
pkt_end_time_ft = DEFAULT_FIELD_TYPE
self._features = StreamTypeFeatures(StreamTypePacketFeatures(beginning_time_field_type=pkt_beginning_time_ft,
- end_time_field_type=pkt_end_time_ft),
- StreamTypeEventFeatures(time_field_type=ev_time_ft))
+ end_time_field_type=pkt_end_time_ft,
+ default_byte_order=default_byte_order),
+ StreamTypeEventFeatures(time_field_type=ev_time_ft,
+ default_byte_order=default_byte_order))
def _set_ft_mapped_clk_type_name(self, ft: Optional[UnsignedIntegerFieldType]):
if ft is None:
class TraceTypeFeatures:
def __init__(self, magic_field_type: _OptDefaultableUIntFt = DEFAULT_FIELD_TYPE,
uuid_field_type: _OptUuidFt = None,
- stream_type_id_field_type: _OptDefaultableUIntFt = DEFAULT_FIELD_TYPE):
- def get_field_type(user_ft: Optional[Union[str, _FieldType]], default_ft: _FieldType) -> _OptFt:
+ stream_type_id_field_type: _OptDefaultableUIntFt = DEFAULT_FIELD_TYPE,
+ default_byte_order: Optional[ByteOrder] = None):
+ def get_field_type(user_ft: Optional[Union[str, _FieldType]],
+ create_default_ft: Callable[[], _FieldType]) -> _OptFt:
if user_ft == DEFAULT_FIELD_TYPE:
- return default_ft
+ return create_default_ft()
return typing.cast(_OptFt, user_ft)
- self._magic_field_type = typing.cast(_OptUIntFt, get_field_type(magic_field_type,
- UnsignedIntegerFieldType(32)))
- self._uuid_field_type = typing.cast(Optional[StaticArrayFieldType], get_field_type(uuid_field_type,
- StaticArrayFieldType(Count(16),
- UnsignedIntegerFieldType(8))))
- self._stream_type_id_field_type = typing.cast(_OptUIntFt, get_field_type(stream_type_id_field_type,
- UnsignedIntegerFieldType(64)))
+ def create_default_magic_ft():
+ assert default_byte_order is not None
+ return UnsignedIntegerFieldType(32, default_byte_order)
+
+ def create_default_uuid_ft():
+ assert default_byte_order is not None
+ return StaticArrayFieldType(Count(16), UnsignedIntegerFieldType(8, default_byte_order))
+
+ def create_default_stream_type_id_ft():
+ assert default_byte_order is not None
+ return UnsignedIntegerFieldType(64, default_byte_order)
+
+ self._magic_field_type = typing.cast(_OptUIntFt, get_field_type(magic_field_type, create_default_magic_ft))
+ self._uuid_field_type = typing.cast(Optional[StaticArrayFieldType],
+ get_field_type(uuid_field_type, create_default_uuid_ft))
+ self._stream_type_id_field_type = typing.cast(_OptUIntFt,
+ get_field_type(stream_type_id_field_type,
+ create_default_stream_type_id_ft))
@property
def magic_field_type(self) -> _OptUIntFt:
class TraceType:
- def __init__(self, stream_types: Set[StreamType], default_byte_order: ByteOrder,
- uuid: _OptUuid = None, features: Optional[TraceTypeFeatures] = None):
- self._default_byte_order = default_byte_order
+ def __init__(self, stream_types: Set[StreamType], uuid: _OptUuid = None,
+ features: Optional[TraceTypeFeatures] = None,
+ default_feature_field_type_byte_order: Optional[ByteOrder] = None):
self._stream_types = frozenset(stream_types)
# assign unique IDs
stream_type._id = Id(index)
self._uuid = uuid
- self._set_features(features)
+ self._set_features(features, default_feature_field_type_byte_order)
self._set_pkt_header_ft()
- self._set_fts_effective_byte_order()
- def _set_features(self, features: Optional[TraceTypeFeatures]):
+ def _set_features(self, features: Optional[TraceTypeFeatures],
+ default_byte_order: Optional[ByteOrder]):
if features is not None:
self._features = features
return
# automatic UUID field type because the trace type has a UUID
uuid_ft = None if self._uuid is None else DEFAULT_FIELD_TYPE
- self._features = TraceTypeFeatures(uuid_field_type=uuid_ft)
+ self._features = TraceTypeFeatures(uuid_field_type=uuid_ft,
+ default_byte_order=default_byte_order)
def _set_pkt_header_ft(self):
members = collections.OrderedDict()
add_member_if_exists('stream_id', self._features.stream_type_id_field_type)
self._pkt_header_ft = StructureFieldType(8, members)
- def _set_fts_effective_byte_order(self):
- def set_ft_effective_byte_order(ft: _OptFt):
- if ft is None:
- return
-
- if isinstance(ft, _BitArrayFieldType):
- if ft._byte_order is None:
- assert self._default_byte_order is not None
- ft._byte_order = self._default_byte_order
- elif isinstance(ft, StaticArrayFieldType):
- set_ft_effective_byte_order(ft.element_field_type)
- elif isinstance(ft, StructureFieldType):
- for member in ft.members.values():
- set_ft_effective_byte_order(member.field_type)
-
- # packet header field type
- set_ft_effective_byte_order(self._pkt_header_ft)
-
- # stream type field types
- for stream_type in self._stream_types:
- set_ft_effective_byte_order(stream_type._pkt_ctx_ft)
- set_ft_effective_byte_order(stream_type._ev_header_ft)
- set_ft_effective_byte_order(stream_type._event_common_context_field_type)
-
- # event type field types
- for ev_type in stream_type.event_types:
- set_ft_effective_byte_order(ev_type._specific_context_field_type)
- set_ft_effective_byte_order(ev_type._payload_field_type)
-
- @property
- def default_byte_order(self) -> ByteOrder:
- return self._default_byte_order
-
@property
def uuid(self) -> _OptUuid:
return self._uuid
class Configuration:
- def __init__(self, trace: Trace, options: Optional[ConfigurationOptions] = None):
+ def __init__(self, trace: Trace, target_byte_order: ByteOrder,
+ options: Optional[ConfigurationOptions] = None):
self._trace = trace
self._options = ConfigurationOptions()
+ self._target_byte_order = target_byte_order
if options is not None:
self._options = options
def trace(self) -> Trace:
return self._trace
+ @property
+ def target_byte_order(self):
+ return self._target_byte_order
+
@property
def options(self) -> ConfigurationOptions:
return self._options
pkt_content_size_ft,
pkt_beginning_time_ft,
pkt_end_time_ft,
- pkt_discarded_events_counter_ft)
- ev_features = barectf_config.StreamTypeEventFeatures(ev_type_id_ft, ev_time_ft)
+ pkt_discarded_events_counter_ft,
+ default_byte_order=self._target_byte_order)
+ ev_features = barectf_config.StreamTypeEventFeatures(ev_type_id_ft, ev_time_ft,
+ default_byte_order=self._target_byte_order)
features = barectf_config.StreamTypeFeatures(pkt_features, ev_features)
# create packet context (structure) field type extra members
ev_types.add(self._create_ev_type(ev_name, ev_type_node, ev_header_common_ctx_member_count))
# create stream type
- return barectf_config.StreamType(name, ev_types, def_clk_type, features,
+ return barectf_config.StreamType(name, ev_types, def_clk_type, features, None,
pkt_ctx_ft_extra_members,
self._try_create_struct_ft(stream_type_node,
ev_common_ctx_ft_prop_name))
except _ConfigurationParseError as exc:
_append_error_ctx(exc, '`$features` property')
- features = barectf_config.TraceTypeFeatures(magic_ft, uuid_ft, stream_type_id_ft)
+ features = barectf_config.TraceTypeFeatures(magic_ft, uuid_ft, stream_type_id_ft,
+ default_byte_order=self._target_byte_order)
# create stream types
stream_types = set()
stream_types.add(self._create_stream_type(stream_name, stream_type_node))
# create trace type
- return barectf_config.TraceType(stream_types, self._default_byte_order,
- trace_type_uuid, features)
+ return barectf_config.TraceType(stream_types, trace_type_uuid, features)
except _ConfigurationParseError as exc:
_append_error_ctx(exc, 'Trace type')
opts = barectf_config.ConfigurationOptions(cg_opts)
# create configuration
- self._config = barectf_config.Configuration(trace, opts)
+ self._config = barectf_config.Configuration(trace, self._target_byte_order, opts)
# Expands the field type aliases found in the trace type node.
#
#
# 2. Chooses a specific `class` property value.
#
- # 3. Chooses a specific `byte-order`/`$default-byte-order` property
+ # 3. Chooses a specific `byte-order`/`target-byte-order` property
# value.
#
# 4. Chooses a specific `preferred-display-base` property value.
trace_node = self.config_node['trace']
trace_type_node = trace_node['type']
- prop_name = '$default-byte-order'
-
- if prop_name in trace_type_node and type(trace_type_node[prop_name]) is str:
- normalize_byte_order_prop(trace_type_node, prop_name)
+ normalize_byte_order_prop(self.config_node, 'target-byte-order')
for parent_node, key in self._trace_type_props():
node = parent_node[key]
if node is None:
del trace_node[prop_name]
- # Substitutes missing/`None` `byte-order` properties with the trace
- # type node's default byte order (`$default-byte-order` property),
- # if any.
+ # Substitutes missing/`None` `byte-order` properties with the
+ # configuration node's target byte order (`target-byte-order`
+ # property).
def _sub_ft_nodes_byte_order(self):
ba_ft_class_names = {
'unsigned-integer',
byte_order_node = ft_node.get(prop_name)
if byte_order_node is None:
- if default_byte_order_node is None:
- raise _ConfigurationParseError(f'`{key}` property`',
- '`byte-order` property is not set or null, but trace type has no `$default-byte-order` property')
-
- ft_node[prop_name] = default_byte_order_node
+ ft_node[prop_name] = self._target_byte_order_node
members_node = ft_node.get('members')
except _ConfigurationParseError as exc:
_append_error_ctx(exc, f'Structure field type member `{member_name}`')
- default_byte_order_node = self._trace_type_node.get('$default-byte-order')
-
- self._default_byte_order = None
-
- if default_byte_order_node is not None:
- self._default_byte_order = self._byte_order_from_node(default_byte_order_node)
-
+ self._target_byte_order_node = self.config_node['target-byte-order']
+ self._target_byte_order = self._byte_order_from_node(self._target_byte_order_node)
features_prop_name = '$features'
features_node = self._trace_type_node.get(features_prop_name)
# Set `byte-order` properties of bit array field type nodes
# missing one.
- #
- # This process also removes the `$default-byte-order` property
- # from the trace type node.
self._sub_ft_nodes_byte_order()
# Create a barectf configuration object from the configuration