| 1 | # SPDX-License-Identifier: MIT |
| 2 | # |
| 3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> |
| 4 | |
| 5 | from bt2 import native_bt |
| 6 | from bt2 import object as bt2_object |
| 7 | from bt2 import utils as bt2_utils |
| 8 | from bt2 import field_class as bt2_field_class |
| 9 | from bt2 import event_class as bt2_event_class |
| 10 | from bt2 import clock_class as bt2_clock_class |
| 11 | from bt2 import value as bt2_value |
| 12 | import collections.abc |
| 13 | |
| 14 | |
| 15 | def _bt2_trace_class(): |
| 16 | from bt2 import trace_class as bt2_trace_class |
| 17 | |
| 18 | return bt2_trace_class |
| 19 | |
| 20 | |
| 21 | class _StreamClassConst(bt2_object._SharedObject, collections.abc.Mapping): |
| 22 | @staticmethod |
| 23 | def _get_ref(ptr): |
| 24 | native_bt.stream_class_get_ref(ptr) |
| 25 | |
| 26 | @staticmethod |
| 27 | def _put_ref(ptr): |
| 28 | native_bt.stream_class_put_ref(ptr) |
| 29 | |
| 30 | _borrow_event_class_ptr_by_id = staticmethod( |
| 31 | native_bt.stream_class_borrow_event_class_by_id_const |
| 32 | ) |
| 33 | _borrow_event_class_ptr_by_index = staticmethod( |
| 34 | native_bt.stream_class_borrow_event_class_by_index_const |
| 35 | ) |
| 36 | _borrow_trace_class_ptr = staticmethod( |
| 37 | native_bt.stream_class_borrow_trace_class_const |
| 38 | ) |
| 39 | _borrow_packet_context_field_class_ptr = staticmethod( |
| 40 | native_bt.stream_class_borrow_packet_context_field_class_const |
| 41 | ) |
| 42 | _borrow_event_common_context_field_class_ptr = staticmethod( |
| 43 | native_bt.stream_class_borrow_event_common_context_field_class_const |
| 44 | ) |
| 45 | _borrow_default_clock_class_ptr = staticmethod( |
| 46 | native_bt.stream_class_borrow_default_clock_class_const |
| 47 | ) |
| 48 | _borrow_user_attributes_ptr = staticmethod( |
| 49 | native_bt.stream_class_borrow_user_attributes_const |
| 50 | ) |
| 51 | |
| 52 | _event_class_cls = property(lambda _: bt2_event_class._EventClassConst) |
| 53 | _trace_class_cls = property(lambda _: _bt2_trace_class()._TraceClassConst) |
| 54 | _clock_class_cls = property(lambda _: bt2_clock_class._ClockClassConst) |
| 55 | |
| 56 | def __getitem__(self, key): |
| 57 | bt2_utils._check_int64(key) |
| 58 | ec_ptr = self._borrow_event_class_ptr_by_id(self._ptr, key) |
| 59 | |
| 60 | if ec_ptr is None: |
| 61 | raise KeyError(key) |
| 62 | |
| 63 | return self._event_class_cls._create_from_ptr_and_get_ref(ec_ptr) |
| 64 | |
| 65 | def __len__(self): |
| 66 | count = native_bt.stream_class_get_event_class_count(self._ptr) |
| 67 | assert count >= 0 |
| 68 | return count |
| 69 | |
| 70 | def __iter__(self): |
| 71 | for idx in range(len(self)): |
| 72 | ec_ptr = self._borrow_event_class_ptr_by_index(self._ptr, idx) |
| 73 | assert ec_ptr is not None |
| 74 | |
| 75 | id = native_bt.event_class_get_id(ec_ptr) |
| 76 | assert id >= 0 |
| 77 | |
| 78 | yield id |
| 79 | |
| 80 | @property |
| 81 | def trace_class(self): |
| 82 | tc_ptr = self._borrow_trace_class_ptr(self._ptr) |
| 83 | |
| 84 | if tc_ptr is not None: |
| 85 | return self._trace_class_cls._create_from_ptr_and_get_ref(tc_ptr) |
| 86 | |
| 87 | @property |
| 88 | def user_attributes(self): |
| 89 | ptr = self._borrow_user_attributes_ptr(self._ptr) |
| 90 | assert ptr is not None |
| 91 | return bt2_value._create_from_ptr_and_get_ref(ptr) |
| 92 | |
| 93 | @property |
| 94 | def name(self): |
| 95 | return native_bt.stream_class_get_name(self._ptr) |
| 96 | |
| 97 | @property |
| 98 | def assigns_automatic_event_class_id(self): |
| 99 | return native_bt.stream_class_assigns_automatic_event_class_id(self._ptr) |
| 100 | |
| 101 | @property |
| 102 | def assigns_automatic_stream_id(self): |
| 103 | return native_bt.stream_class_assigns_automatic_stream_id(self._ptr) |
| 104 | |
| 105 | @property |
| 106 | def supports_packets(self): |
| 107 | return native_bt.stream_class_supports_packets(self._ptr) |
| 108 | |
| 109 | @property |
| 110 | def packets_have_beginning_default_clock_snapshot(self): |
| 111 | return native_bt.stream_class_packets_have_beginning_default_clock_snapshot( |
| 112 | self._ptr |
| 113 | ) |
| 114 | |
| 115 | @property |
| 116 | def packets_have_end_default_clock_snapshot(self): |
| 117 | return native_bt.stream_class_packets_have_end_default_clock_snapshot(self._ptr) |
| 118 | |
| 119 | @property |
| 120 | def supports_discarded_events(self): |
| 121 | return native_bt.stream_class_supports_discarded_events(self._ptr) |
| 122 | |
| 123 | @property |
| 124 | def discarded_events_have_default_clock_snapshots(self): |
| 125 | return native_bt.stream_class_discarded_events_have_default_clock_snapshots( |
| 126 | self._ptr |
| 127 | ) |
| 128 | |
| 129 | @property |
| 130 | def supports_discarded_packets(self): |
| 131 | return native_bt.stream_class_supports_discarded_packets(self._ptr) |
| 132 | |
| 133 | @property |
| 134 | def discarded_packets_have_default_clock_snapshots(self): |
| 135 | return native_bt.stream_class_discarded_packets_have_default_clock_snapshots( |
| 136 | self._ptr |
| 137 | ) |
| 138 | |
| 139 | @property |
| 140 | def id(self): |
| 141 | id = native_bt.stream_class_get_id(self._ptr) |
| 142 | |
| 143 | if id < 0: |
| 144 | return |
| 145 | |
| 146 | return id |
| 147 | |
| 148 | @property |
| 149 | def packet_context_field_class(self): |
| 150 | fc_ptr = self._borrow_packet_context_field_class_ptr(self._ptr) |
| 151 | |
| 152 | if fc_ptr is None: |
| 153 | return |
| 154 | |
| 155 | return bt2_field_class._create_field_class_from_ptr_and_get_ref(fc_ptr) |
| 156 | |
| 157 | @property |
| 158 | def event_common_context_field_class(self): |
| 159 | fc_ptr = self._borrow_event_common_context_field_class_ptr(self._ptr) |
| 160 | |
| 161 | if fc_ptr is None: |
| 162 | return |
| 163 | |
| 164 | return bt2_field_class._create_field_class_from_ptr_and_get_ref(fc_ptr) |
| 165 | |
| 166 | @property |
| 167 | def default_clock_class(self): |
| 168 | cc_ptr = self._borrow_default_clock_class_ptr(self._ptr) |
| 169 | if cc_ptr is None: |
| 170 | return |
| 171 | |
| 172 | return self._clock_class_cls._create_from_ptr_and_get_ref(cc_ptr) |
| 173 | |
| 174 | |
| 175 | class _StreamClass(_StreamClassConst): |
| 176 | @staticmethod |
| 177 | def _get_ref(ptr): |
| 178 | native_bt.stream_class_get_ref(ptr) |
| 179 | |
| 180 | @staticmethod |
| 181 | def _put_ref(ptr): |
| 182 | native_bt.stream_class_put_ref(ptr) |
| 183 | |
| 184 | _borrow_event_class_ptr_by_id = staticmethod( |
| 185 | native_bt.stream_class_borrow_event_class_by_id |
| 186 | ) |
| 187 | _borrow_event_class_ptr_by_index = staticmethod( |
| 188 | native_bt.stream_class_borrow_event_class_by_index |
| 189 | ) |
| 190 | _borrow_trace_class_ptr = staticmethod(native_bt.stream_class_borrow_trace_class) |
| 191 | _borrow_packet_context_field_class_ptr = staticmethod( |
| 192 | native_bt.stream_class_borrow_packet_context_field_class |
| 193 | ) |
| 194 | _borrow_event_common_context_field_class_ptr = staticmethod( |
| 195 | native_bt.stream_class_borrow_event_common_context_field_class |
| 196 | ) |
| 197 | _borrow_default_clock_class_ptr = staticmethod( |
| 198 | native_bt.stream_class_borrow_default_clock_class |
| 199 | ) |
| 200 | _borrow_user_attributes_ptr = staticmethod( |
| 201 | native_bt.stream_class_borrow_user_attributes |
| 202 | ) |
| 203 | |
| 204 | _event_class_cls = property(lambda s: bt2_event_class._EventClass) |
| 205 | _trace_class_cls = property(lambda s: _bt2_trace_class()._TraceClass) |
| 206 | _clock_class_cls = property(lambda s: bt2_clock_class._ClockClass) |
| 207 | |
| 208 | def create_event_class( |
| 209 | self, |
| 210 | id=None, |
| 211 | name=None, |
| 212 | user_attributes=None, |
| 213 | log_level=None, |
| 214 | emf_uri=None, |
| 215 | specific_context_field_class=None, |
| 216 | payload_field_class=None, |
| 217 | ): |
| 218 | # Validate parameters before we create the object. |
| 219 | bt2_event_class._EventClass._validate_create_params( |
| 220 | name, |
| 221 | user_attributes, |
| 222 | log_level, |
| 223 | emf_uri, |
| 224 | specific_context_field_class, |
| 225 | payload_field_class, |
| 226 | ) |
| 227 | |
| 228 | if self.assigns_automatic_event_class_id: |
| 229 | if id is not None: |
| 230 | raise ValueError( |
| 231 | "id provided, but stream class assigns automatic event class ids" |
| 232 | ) |
| 233 | |
| 234 | ec_ptr = native_bt.event_class_create(self._ptr) |
| 235 | else: |
| 236 | if id is None: |
| 237 | raise ValueError( |
| 238 | "id not provided, but stream class does not assign automatic event class ids" |
| 239 | ) |
| 240 | |
| 241 | bt2_utils._check_uint64(id) |
| 242 | ec_ptr = native_bt.event_class_create_with_id(self._ptr, id) |
| 243 | |
| 244 | event_class = bt2_event_class._EventClass._create_from_ptr(ec_ptr) |
| 245 | |
| 246 | if name is not None: |
| 247 | event_class._name = name |
| 248 | |
| 249 | if user_attributes is not None: |
| 250 | event_class._user_attributes = user_attributes |
| 251 | |
| 252 | if log_level is not None: |
| 253 | event_class._log_level = log_level |
| 254 | |
| 255 | if emf_uri is not None: |
| 256 | event_class._emf_uri = emf_uri |
| 257 | |
| 258 | if specific_context_field_class is not None: |
| 259 | event_class._specific_context_field_class = specific_context_field_class |
| 260 | |
| 261 | if payload_field_class is not None: |
| 262 | event_class._payload_field_class = payload_field_class |
| 263 | |
| 264 | return event_class |
| 265 | |
| 266 | def _user_attributes(self, user_attributes): |
| 267 | value = bt2_value.create_value(user_attributes) |
| 268 | native_bt.stream_class_set_user_attributes(self._ptr, value._ptr) |
| 269 | |
| 270 | _user_attributes = property(fset=_user_attributes) |
| 271 | |
| 272 | def _name(self, name): |
| 273 | status = native_bt.stream_class_set_name(self._ptr, name) |
| 274 | bt2_utils._handle_func_status(status, "cannot set stream class object's name") |
| 275 | |
| 276 | _name = property(fset=_name) |
| 277 | |
| 278 | def _assigns_automatic_event_class_id(self, auto_id): |
| 279 | native_bt.stream_class_set_assigns_automatic_event_class_id(self._ptr, auto_id) |
| 280 | |
| 281 | _assigns_automatic_event_class_id = property(fset=_assigns_automatic_event_class_id) |
| 282 | |
| 283 | def _assigns_automatic_stream_id(self, auto_id): |
| 284 | native_bt.stream_class_set_assigns_automatic_stream_id(self._ptr, auto_id) |
| 285 | |
| 286 | _assigns_automatic_stream_id = property(fset=_assigns_automatic_stream_id) |
| 287 | |
| 288 | def _set_supports_packets(self, supports, with_begin_cs=False, with_end_cs=False): |
| 289 | native_bt.stream_class_set_supports_packets( |
| 290 | self._ptr, supports, with_begin_cs, with_end_cs |
| 291 | ) |
| 292 | |
| 293 | def _set_supports_discarded_events(self, supports, with_cs=False): |
| 294 | native_bt.stream_class_set_supports_discarded_events( |
| 295 | self._ptr, supports, with_cs |
| 296 | ) |
| 297 | |
| 298 | _supports_discarded_events = property(fset=_set_supports_discarded_events) |
| 299 | |
| 300 | def _set_supports_discarded_packets(self, supports, with_cs): |
| 301 | native_bt.stream_class_set_supports_discarded_packets( |
| 302 | self._ptr, supports, with_cs |
| 303 | ) |
| 304 | |
| 305 | _supports_discarded_packets = property(fset=_set_supports_discarded_packets) |
| 306 | |
| 307 | def _packet_context_field_class(self, packet_context_field_class): |
| 308 | status = native_bt.stream_class_set_packet_context_field_class( |
| 309 | self._ptr, packet_context_field_class._ptr |
| 310 | ) |
| 311 | bt2_utils._handle_func_status( |
| 312 | status, "cannot set stream class' packet context field class" |
| 313 | ) |
| 314 | |
| 315 | _packet_context_field_class = property(fset=_packet_context_field_class) |
| 316 | |
| 317 | def _event_common_context_field_class(self, event_common_context_field_class): |
| 318 | set_context_fn = native_bt.stream_class_set_event_common_context_field_class |
| 319 | status = set_context_fn(self._ptr, event_common_context_field_class._ptr) |
| 320 | bt2_utils._handle_func_status( |
| 321 | status, "cannot set stream class' event context field type" |
| 322 | ) |
| 323 | |
| 324 | _event_common_context_field_class = property(fset=_event_common_context_field_class) |
| 325 | |
| 326 | def _default_clock_class(self, clock_class): |
| 327 | native_bt.stream_class_set_default_clock_class(self._ptr, clock_class._ptr) |
| 328 | |
| 329 | _default_clock_class = property(fset=_default_clock_class) |
| 330 | |
| 331 | @classmethod |
| 332 | def _validate_create_params( |
| 333 | cls, |
| 334 | name, |
| 335 | user_attributes, |
| 336 | packet_context_field_class, |
| 337 | event_common_context_field_class, |
| 338 | default_clock_class, |
| 339 | assigns_automatic_event_class_id, |
| 340 | assigns_automatic_stream_id, |
| 341 | supports_packets, |
| 342 | packets_have_beginning_default_clock_snapshot, |
| 343 | packets_have_end_default_clock_snapshot, |
| 344 | supports_discarded_events, |
| 345 | discarded_events_have_default_clock_snapshots, |
| 346 | supports_discarded_packets, |
| 347 | discarded_packets_have_default_clock_snapshots, |
| 348 | ): |
| 349 | # Name |
| 350 | if name is not None: |
| 351 | bt2_utils._check_str(name) |
| 352 | |
| 353 | # User attributes |
| 354 | if user_attributes is not None: |
| 355 | value = bt2_value.create_value(user_attributes) |
| 356 | bt2_utils._check_type(value, bt2_value.MapValue) |
| 357 | |
| 358 | # Packet context field class |
| 359 | if packet_context_field_class is not None: |
| 360 | if not supports_packets: |
| 361 | raise ValueError( |
| 362 | "cannot have a packet context field class without supporting packets" |
| 363 | ) |
| 364 | |
| 365 | bt2_utils._check_type( |
| 366 | packet_context_field_class, bt2_field_class._StructureFieldClass |
| 367 | ) |
| 368 | |
| 369 | # Event common context field class |
| 370 | if event_common_context_field_class is not None: |
| 371 | bt2_utils._check_type( |
| 372 | event_common_context_field_class, bt2_field_class._StructureFieldClass |
| 373 | ) |
| 374 | |
| 375 | # Default clock class |
| 376 | if default_clock_class is not None: |
| 377 | bt2_utils._check_type(default_clock_class, bt2_clock_class._ClockClass) |
| 378 | |
| 379 | # Assigns automatic event class id |
| 380 | bt2_utils._check_bool(assigns_automatic_event_class_id) |
| 381 | |
| 382 | # Assigns automatic stream id |
| 383 | bt2_utils._check_bool(assigns_automatic_stream_id) |
| 384 | |
| 385 | # Packets |
| 386 | bt2_utils._check_bool(supports_packets) |
| 387 | bt2_utils._check_bool(packets_have_beginning_default_clock_snapshot) |
| 388 | bt2_utils._check_bool(packets_have_end_default_clock_snapshot) |
| 389 | |
| 390 | if not supports_packets: |
| 391 | if packets_have_beginning_default_clock_snapshot: |
| 392 | raise ValueError( |
| 393 | "cannot not support packets, but have packet beginning default clock snapshot" |
| 394 | ) |
| 395 | if packets_have_end_default_clock_snapshot: |
| 396 | raise ValueError( |
| 397 | "cannot not support packets, but have packet end default clock snapshots" |
| 398 | ) |
| 399 | |
| 400 | # Discarded events |
| 401 | bt2_utils._check_bool(supports_discarded_events) |
| 402 | bt2_utils._check_bool(discarded_events_have_default_clock_snapshots) |
| 403 | |
| 404 | if discarded_events_have_default_clock_snapshots: |
| 405 | if not supports_discarded_events: |
| 406 | raise ValueError( |
| 407 | "cannot not support discarded events, but have default clock snapshots for discarded event messages" |
| 408 | ) |
| 409 | |
| 410 | if default_clock_class is None: |
| 411 | raise ValueError( |
| 412 | "cannot have no default clock class, but have default clock snapshots for discarded event messages" |
| 413 | ) |
| 414 | |
| 415 | # Discarded packets |
| 416 | bt2_utils._check_bool(supports_discarded_packets) |
| 417 | bt2_utils._check_bool(discarded_packets_have_default_clock_snapshots) |
| 418 | |
| 419 | if supports_discarded_packets and not supports_packets: |
| 420 | raise ValueError( |
| 421 | "cannot support discarded packets, but not support packets" |
| 422 | ) |
| 423 | |
| 424 | if discarded_packets_have_default_clock_snapshots: |
| 425 | if not supports_discarded_packets: |
| 426 | raise ValueError( |
| 427 | "cannot not support discarded packets, but have default clock snapshots for discarded packet messages" |
| 428 | ) |
| 429 | |
| 430 | if default_clock_class is None: |
| 431 | raise ValueError( |
| 432 | "cannot have no default clock class, but have default clock snapshots for discarded packet messages" |
| 433 | ) |