1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
7 from bt2
import utils
as bt2_utils
8 from bt2
import value
as bt2_value
9 from bt2
import object as bt2_object
10 from bt2
import native_bt
11 from bt2
import clock_class
as bt2_clock_class
12 from bt2
import event_class
as bt2_event_class
13 from bt2
import field_class
as bt2_field_class
16 def _bt2_trace_class():
17 from bt2
import trace_class
as bt2_trace_class
19 return bt2_trace_class
22 class _StreamClassConst(bt2_object
._SharedObject
, collections
.abc
.Mapping
):
25 native_bt
.stream_class_get_ref(ptr
)
29 native_bt
.stream_class_put_ref(ptr
)
31 _borrow_event_class_ptr_by_id
= staticmethod(
32 native_bt
.stream_class_borrow_event_class_by_id_const
34 _borrow_event_class_ptr_by_index
= staticmethod(
35 native_bt
.stream_class_borrow_event_class_by_index_const
37 _borrow_trace_class_ptr
= staticmethod(
38 native_bt
.stream_class_borrow_trace_class_const
40 _borrow_packet_context_field_class_ptr
= staticmethod(
41 native_bt
.stream_class_borrow_packet_context_field_class_const
43 _borrow_event_common_context_field_class_ptr
= staticmethod(
44 native_bt
.stream_class_borrow_event_common_context_field_class_const
46 _borrow_default_clock_class_ptr
= staticmethod(
47 native_bt
.stream_class_borrow_default_clock_class_const
49 _borrow_user_attributes_ptr
= staticmethod(
50 native_bt
.stream_class_borrow_user_attributes_const
53 _event_class_cls
= property(lambda _
: bt2_event_class
._EventClassConst
)
54 _trace_class_cls
= property(lambda _
: _bt2_trace_class()._TraceClassConst
)
55 _clock_class_cls
= property(lambda _
: bt2_clock_class
._ClockClassConst
)
57 def __getitem__(self
, key
):
58 bt2_utils
._check
_int
64(key
)
59 ec_ptr
= self
._borrow
_event
_class
_ptr
_by
_id
(self
._ptr
, key
)
64 return self
._event
_class
_cls
._create
_from
_ptr
_and
_get
_ref
(ec_ptr
)
67 count
= native_bt
.stream_class_get_event_class_count(self
._ptr
)
72 for idx
in range(len(self
)):
73 ec_ptr
= self
._borrow
_event
_class
_ptr
_by
_index
(self
._ptr
, idx
)
74 assert ec_ptr
is not None
76 id = native_bt
.event_class_get_id(ec_ptr
)
82 def trace_class(self
):
83 tc_ptr
= self
._borrow
_trace
_class
_ptr
(self
._ptr
)
85 if tc_ptr
is not None:
86 return self
._trace
_class
_cls
._create
_from
_ptr
_and
_get
_ref
(tc_ptr
)
89 def user_attributes(self
):
90 ptr
= self
._borrow
_user
_attributes
_ptr
(self
._ptr
)
91 assert ptr
is not None
92 return bt2_value
._create
_from
_ptr
_and
_get
_ref
(ptr
)
96 return native_bt
.stream_class_get_name(self
._ptr
)
99 def assigns_automatic_event_class_id(self
):
100 return native_bt
.stream_class_assigns_automatic_event_class_id(self
._ptr
)
103 def assigns_automatic_stream_id(self
):
104 return native_bt
.stream_class_assigns_automatic_stream_id(self
._ptr
)
107 def supports_packets(self
):
108 return native_bt
.stream_class_supports_packets(self
._ptr
)
111 def packets_have_beginning_default_clock_snapshot(self
):
112 return native_bt
.stream_class_packets_have_beginning_default_clock_snapshot(
117 def packets_have_end_default_clock_snapshot(self
):
118 return native_bt
.stream_class_packets_have_end_default_clock_snapshot(self
._ptr
)
121 def supports_discarded_events(self
):
122 return native_bt
.stream_class_supports_discarded_events(self
._ptr
)
125 def discarded_events_have_default_clock_snapshots(self
):
126 return native_bt
.stream_class_discarded_events_have_default_clock_snapshots(
131 def supports_discarded_packets(self
):
132 return native_bt
.stream_class_supports_discarded_packets(self
._ptr
)
135 def discarded_packets_have_default_clock_snapshots(self
):
136 return native_bt
.stream_class_discarded_packets_have_default_clock_snapshots(
142 id = native_bt
.stream_class_get_id(self
._ptr
)
150 def packet_context_field_class(self
):
151 fc_ptr
= self
._borrow
_packet
_context
_field
_class
_ptr
(self
._ptr
)
156 return bt2_field_class
._create
_field
_class
_from
_ptr
_and
_get
_ref
(fc_ptr
)
159 def event_common_context_field_class(self
):
160 fc_ptr
= self
._borrow
_event
_common
_context
_field
_class
_ptr
(self
._ptr
)
165 return bt2_field_class
._create
_field
_class
_from
_ptr
_and
_get
_ref
(fc_ptr
)
168 def default_clock_class(self
):
169 cc_ptr
= self
._borrow
_default
_clock
_class
_ptr
(self
._ptr
)
173 return self
._clock
_class
_cls
._create
_from
_ptr
_and
_get
_ref
(cc_ptr
)
176 class _StreamClass(_StreamClassConst
):
179 native_bt
.stream_class_get_ref(ptr
)
183 native_bt
.stream_class_put_ref(ptr
)
185 _borrow_event_class_ptr_by_id
= staticmethod(
186 native_bt
.stream_class_borrow_event_class_by_id
188 _borrow_event_class_ptr_by_index
= staticmethod(
189 native_bt
.stream_class_borrow_event_class_by_index
191 _borrow_trace_class_ptr
= staticmethod(native_bt
.stream_class_borrow_trace_class
)
192 _borrow_packet_context_field_class_ptr
= staticmethod(
193 native_bt
.stream_class_borrow_packet_context_field_class
195 _borrow_event_common_context_field_class_ptr
= staticmethod(
196 native_bt
.stream_class_borrow_event_common_context_field_class
198 _borrow_default_clock_class_ptr
= staticmethod(
199 native_bt
.stream_class_borrow_default_clock_class
201 _borrow_user_attributes_ptr
= staticmethod(
202 native_bt
.stream_class_borrow_user_attributes
205 _event_class_cls
= property(lambda s
: bt2_event_class
._EventClass
)
206 _trace_class_cls
= property(lambda s
: _bt2_trace_class()._TraceClass
)
207 _clock_class_cls
= property(lambda s
: bt2_clock_class
._ClockClass
)
209 def create_event_class(
213 user_attributes
=None,
216 specific_context_field_class
=None,
217 payload_field_class
=None,
219 # Validate parameters before we create the object.
220 bt2_event_class
._EventClass
._validate
_create
_params
(
225 specific_context_field_class
,
229 if self
.assigns_automatic_event_class_id
:
232 "id provided, but stream class assigns automatic event class ids"
235 ec_ptr
= native_bt
.event_class_create(self
._ptr
)
239 "id not provided, but stream class does not assign automatic event class ids"
242 bt2_utils
._check
_uint
64(id)
243 ec_ptr
= native_bt
.event_class_create_with_id(self
._ptr
, id)
245 event_class
= bt2_event_class
._EventClass
._create
_from
_ptr
(ec_ptr
)
248 event_class
._name
= name
250 if user_attributes
is not None:
251 event_class
._user
_attributes
= user_attributes
253 if log_level
is not None:
254 event_class
._log
_level
= log_level
256 if emf_uri
is not None:
257 event_class
._emf
_uri
= emf_uri
259 if specific_context_field_class
is not None:
260 event_class
._specific
_context
_field
_class
= specific_context_field_class
262 if payload_field_class
is not None:
263 event_class
._payload
_field
_class
= payload_field_class
267 def _user_attributes(self
, user_attributes
):
268 value
= bt2_value
.create_value(user_attributes
)
269 native_bt
.stream_class_set_user_attributes(self
._ptr
, value
._ptr
)
271 _user_attributes
= property(fset
=_user_attributes
)
273 def _name(self
, name
):
274 status
= native_bt
.stream_class_set_name(self
._ptr
, name
)
275 bt2_utils
._handle
_func
_status
(status
, "cannot set stream class object's name")
277 _name
= property(fset
=_name
)
279 def _assigns_automatic_event_class_id(self
, auto_id
):
280 native_bt
.stream_class_set_assigns_automatic_event_class_id(self
._ptr
, auto_id
)
282 _assigns_automatic_event_class_id
= property(fset
=_assigns_automatic_event_class_id
)
284 def _assigns_automatic_stream_id(self
, auto_id
):
285 native_bt
.stream_class_set_assigns_automatic_stream_id(self
._ptr
, auto_id
)
287 _assigns_automatic_stream_id
= property(fset
=_assigns_automatic_stream_id
)
289 def _set_supports_packets(self
, supports
, with_begin_cs
=False, with_end_cs
=False):
290 native_bt
.stream_class_set_supports_packets(
291 self
._ptr
, supports
, with_begin_cs
, with_end_cs
294 def _set_supports_discarded_events(self
, supports
, with_cs
=False):
295 native_bt
.stream_class_set_supports_discarded_events(
296 self
._ptr
, supports
, with_cs
299 _supports_discarded_events
= property(fset
=_set_supports_discarded_events
)
301 def _set_supports_discarded_packets(self
, supports
, with_cs
):
302 native_bt
.stream_class_set_supports_discarded_packets(
303 self
._ptr
, supports
, with_cs
306 _supports_discarded_packets
= property(fset
=_set_supports_discarded_packets
)
308 def _packet_context_field_class(self
, packet_context_field_class
):
309 status
= native_bt
.stream_class_set_packet_context_field_class(
310 self
._ptr
, packet_context_field_class
._ptr
312 bt2_utils
._handle
_func
_status
(
313 status
, "cannot set stream class' packet context field class"
316 _packet_context_field_class
= property(fset
=_packet_context_field_class
)
318 def _event_common_context_field_class(self
, event_common_context_field_class
):
319 set_context_fn
= native_bt
.stream_class_set_event_common_context_field_class
320 status
= set_context_fn(self
._ptr
, event_common_context_field_class
._ptr
)
321 bt2_utils
._handle
_func
_status
(
322 status
, "cannot set stream class' event context field type"
325 _event_common_context_field_class
= property(fset
=_event_common_context_field_class
)
327 def _default_clock_class(self
, clock_class
):
328 native_bt
.stream_class_set_default_clock_class(self
._ptr
, clock_class
._ptr
)
330 _default_clock_class
= property(fset
=_default_clock_class
)
333 def _validate_create_params(
337 packet_context_field_class
,
338 event_common_context_field_class
,
340 assigns_automatic_event_class_id
,
341 assigns_automatic_stream_id
,
343 packets_have_beginning_default_clock_snapshot
,
344 packets_have_end_default_clock_snapshot
,
345 supports_discarded_events
,
346 discarded_events_have_default_clock_snapshots
,
347 supports_discarded_packets
,
348 discarded_packets_have_default_clock_snapshots
,
352 bt2_utils
._check
_str
(name
)
355 if user_attributes
is not None:
356 value
= bt2_value
.create_value(user_attributes
)
357 bt2_utils
._check
_type
(value
, bt2_value
.MapValue
)
359 # Packet context field class
360 if packet_context_field_class
is not None:
361 if not supports_packets
:
363 "cannot have a packet context field class without supporting packets"
366 bt2_utils
._check
_type
(
367 packet_context_field_class
, bt2_field_class
._StructureFieldClass
370 # Event common context field class
371 if event_common_context_field_class
is not None:
372 bt2_utils
._check
_type
(
373 event_common_context_field_class
, bt2_field_class
._StructureFieldClass
376 # Default clock class
377 if default_clock_class
is not None:
378 bt2_utils
._check
_type
(default_clock_class
, bt2_clock_class
._ClockClass
)
380 # Assigns automatic event class id
381 bt2_utils
._check
_bool
(assigns_automatic_event_class_id
)
383 # Assigns automatic stream id
384 bt2_utils
._check
_bool
(assigns_automatic_stream_id
)
387 bt2_utils
._check
_bool
(supports_packets
)
388 bt2_utils
._check
_bool
(packets_have_beginning_default_clock_snapshot
)
389 bt2_utils
._check
_bool
(packets_have_end_default_clock_snapshot
)
391 if not supports_packets
:
392 if packets_have_beginning_default_clock_snapshot
:
394 "cannot not support packets, but have packet beginning default clock snapshot"
396 if packets_have_end_default_clock_snapshot
:
398 "cannot not support packets, but have packet end default clock snapshots"
402 bt2_utils
._check
_bool
(supports_discarded_events
)
403 bt2_utils
._check
_bool
(discarded_events_have_default_clock_snapshots
)
405 if discarded_events_have_default_clock_snapshots
:
406 if not supports_discarded_events
:
408 "cannot not support discarded events, but have default clock snapshots for discarded event messages"
411 if default_clock_class
is None:
413 "cannot have no default clock class, but have default clock snapshots for discarded event messages"
417 bt2_utils
._check
_bool
(supports_discarded_packets
)
418 bt2_utils
._check
_bool
(discarded_packets_have_default_clock_snapshots
)
420 if supports_discarded_packets
and not supports_packets
:
422 "cannot support discarded packets, but not support packets"
425 if discarded_packets_have_default_clock_snapshots
:
426 if not supports_discarded_packets
:
428 "cannot not support discarded packets, but have default clock snapshots for discarded packet messages"
431 if default_clock_class
is None:
433 "cannot have no default clock class, but have default clock snapshots for discarded packet messages"