| 1 | # The MIT License (MIT) |
| 2 | # |
| 3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> |
| 4 | # |
| 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | # of this software and associated documentation files (the "Software"), to deal |
| 7 | # in the Software without restriction, including without limitation the rights |
| 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | # copies of the Software, and to permit persons to whom the Software is |
| 10 | # furnished to do so, subject to the following conditions: |
| 11 | # |
| 12 | # The above copyright notice and this permission notice shall be included in |
| 13 | # all copies or substantial portions of the Software. |
| 14 | # |
| 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | # THE SOFTWARE. |
| 22 | |
| 23 | from bt2 import native_bt, object, utils |
| 24 | import bt2.clock_snapshot |
| 25 | import collections |
| 26 | import bt2.packet |
| 27 | import bt2.stream |
| 28 | import bt2.event |
| 29 | import copy |
| 30 | import bt2 |
| 31 | |
| 32 | |
| 33 | def _create_from_ptr(ptr): |
| 34 | msg_type = native_bt.message_get_type(ptr) |
| 35 | cls = None |
| 36 | |
| 37 | if msg_type not in _MESSAGE_TYPE_TO_CLS: |
| 38 | raise bt2.Error('unknown message type: {}'.format(msg_type)) |
| 39 | |
| 40 | return _MESSAGE_TYPE_TO_CLS[msg_type]._create_from_ptr(ptr) |
| 41 | |
| 42 | |
| 43 | def _msg_types_from_msg_classes(message_types): |
| 44 | if message_types is None: |
| 45 | msg_types = None |
| 46 | else: |
| 47 | for msg_cls in message_types: |
| 48 | if msg_cls not in _MESSAGE_TYPE_TO_CLS.values(): |
| 49 | raise ValueError("'{}' is not a message class".format(msg_cls)) |
| 50 | |
| 51 | msg_types = [msg_cls._TYPE for msg_cls in message_types] |
| 52 | |
| 53 | return msg_types |
| 54 | |
| 55 | |
| 56 | class _Message(object._SharedObject): |
| 57 | pass |
| 58 | |
| 59 | |
| 60 | class _CopyableMessage(_Message): |
| 61 | def __copy__(self): |
| 62 | return self._copy(lambda obj: obj) |
| 63 | |
| 64 | def __deepcopy__(self, memo): |
| 65 | cpy = self._copy(copy.deepcopy) |
| 66 | memo[id(self)] = cpy |
| 67 | return cpy |
| 68 | |
| 69 | |
| 70 | class EventMessage(_CopyableMessage): |
| 71 | _TYPE = native_bt.MESSAGE_TYPE_EVENT |
| 72 | |
| 73 | def __init__(self, event, cc_prio_map=None): |
| 74 | utils._check_type(event, bt2.event._Event) |
| 75 | |
| 76 | if cc_prio_map is not None: |
| 77 | utils._check_type(cc_prio_map, bt2.clock_class_priority_map.ClockClassPriorityMap) |
| 78 | cc_prio_map_ptr = cc_prio_map._ptr |
| 79 | else: |
| 80 | cc_prio_map_ptr = None |
| 81 | |
| 82 | ptr = native_bt.message_event_create(event._ptr, cc_prio_map_ptr) |
| 83 | |
| 84 | if ptr is None: |
| 85 | raise bt2.CreationError('cannot create event message object') |
| 86 | |
| 87 | super().__init__(ptr) |
| 88 | |
| 89 | @property |
| 90 | def event(self): |
| 91 | event_ptr = native_bt.message_event_get_event(self._ptr) |
| 92 | assert(event_ptr) |
| 93 | return bt2.event._create_from_ptr(event_ptr) |
| 94 | |
| 95 | @property |
| 96 | def clock_class_priority_map(self): |
| 97 | cc_prio_map_ptr = native_bt.message_event_get_clock_class_priority_map(self._ptr) |
| 98 | assert(cc_prio_map_ptr) |
| 99 | return bt2.clock_class_priority_map.ClockClassPriorityMap._create_from_ptr(cc_prio_map_ptr) |
| 100 | |
| 101 | def __eq__(self, other): |
| 102 | if type(other) is not type(self): |
| 103 | return False |
| 104 | |
| 105 | if self.addr == other.addr: |
| 106 | return True |
| 107 | |
| 108 | self_props = ( |
| 109 | self.event, |
| 110 | self.clock_class_priority_map, |
| 111 | ) |
| 112 | other_props = ( |
| 113 | other.event, |
| 114 | other.clock_class_priority_map, |
| 115 | ) |
| 116 | return self_props == other_props |
| 117 | |
| 118 | def _copy(self, copy_func): |
| 119 | # We can always use references here because those properties are |
| 120 | # frozen anyway if they are part of a message. Since the |
| 121 | # user cannot modify them after copying the message, it's |
| 122 | # useless to copy/deep-copy them. |
| 123 | return EventMessage(self.event, self.clock_class_priority_map) |
| 124 | |
| 125 | |
| 126 | class PacketBeginningMessage(_CopyableMessage): |
| 127 | _TYPE = native_bt.MESSAGE_TYPE_PACKET_BEGINNING |
| 128 | |
| 129 | def __init__(self, packet): |
| 130 | utils._check_type(packet, bt2.packet._Packet) |
| 131 | ptr = native_bt.message_packet_begin_create(packet._ptr) |
| 132 | |
| 133 | if ptr is None: |
| 134 | raise bt2.CreationError('cannot create packet beginning message object') |
| 135 | |
| 136 | super().__init__(ptr) |
| 137 | |
| 138 | @property |
| 139 | def packet(self): |
| 140 | packet_ptr = native_bt.message_packet_begin_get_packet(self._ptr) |
| 141 | assert(packet_ptr) |
| 142 | return bt2.packet._Packet._create_from_ptr(packet_ptr) |
| 143 | |
| 144 | def __eq__(self, other): |
| 145 | if type(other) is not type(self): |
| 146 | return False |
| 147 | |
| 148 | if self.addr == other.addr: |
| 149 | return True |
| 150 | |
| 151 | return self.packet == other.packet |
| 152 | |
| 153 | def _copy(self, copy_func): |
| 154 | # We can always use references here because those properties are |
| 155 | # frozen anyway if they are part of a message. Since the |
| 156 | # user cannot modify them after copying the message, it's |
| 157 | # useless to copy/deep-copy them. |
| 158 | return PacketBeginningMessage(self.packet) |
| 159 | |
| 160 | |
| 161 | class PacketEndMessage(_CopyableMessage): |
| 162 | _TYPE = native_bt.MESSAGE_TYPE_PACKET_END |
| 163 | |
| 164 | def __init__(self, packet): |
| 165 | utils._check_type(packet, bt2.packet._Packet) |
| 166 | ptr = native_bt.message_packet_end_create(packet._ptr) |
| 167 | |
| 168 | if ptr is None: |
| 169 | raise bt2.CreationError('cannot create packet end message object') |
| 170 | |
| 171 | super().__init__(ptr) |
| 172 | |
| 173 | @property |
| 174 | def packet(self): |
| 175 | packet_ptr = native_bt.message_packet_end_get_packet(self._ptr) |
| 176 | assert(packet_ptr) |
| 177 | return bt2.packet._Packet._create_from_ptr(packet_ptr) |
| 178 | |
| 179 | def __eq__(self, other): |
| 180 | if type(other) is not type(self): |
| 181 | return False |
| 182 | |
| 183 | if self.addr == other.addr: |
| 184 | return True |
| 185 | |
| 186 | return self.packet == other.packet |
| 187 | |
| 188 | def _copy(self, copy_func): |
| 189 | # We can always use references here because those properties are |
| 190 | # frozen anyway if they are part of a message. Since the |
| 191 | # user cannot modify them after copying the message, it's |
| 192 | # useless to copy/deep-copy them. |
| 193 | return PacketEndMessage(self.packet) |
| 194 | |
| 195 | |
| 196 | class StreamBeginningMessage(_CopyableMessage): |
| 197 | _TYPE = native_bt.MESSAGE_TYPE_STREAM_BEGINNING |
| 198 | |
| 199 | def __init__(self, stream): |
| 200 | utils._check_type(stream, bt2.stream._Stream) |
| 201 | ptr = native_bt.message_stream_begin_create(stream._ptr) |
| 202 | |
| 203 | if ptr is None: |
| 204 | raise bt2.CreationError('cannot create stream beginning message object') |
| 205 | |
| 206 | super().__init__(ptr) |
| 207 | |
| 208 | @property |
| 209 | def stream(self): |
| 210 | stream_ptr = native_bt.message_stream_begin_get_stream(self._ptr) |
| 211 | assert(stream_ptr) |
| 212 | return bt2.stream._create_from_ptr(stream_ptr) |
| 213 | |
| 214 | def __eq__(self, other): |
| 215 | if type(other) is not type(self): |
| 216 | return False |
| 217 | |
| 218 | if self.addr == other.addr: |
| 219 | return True |
| 220 | |
| 221 | return self.stream == other.stream |
| 222 | |
| 223 | def _copy(self, copy_func): |
| 224 | # We can always use references here because those properties are |
| 225 | # frozen anyway if they are part of a message. Since the |
| 226 | # user cannot modify them after copying the message, it's |
| 227 | # useless to copy/deep-copy them. |
| 228 | return StreamBeginningMessage(self.stream) |
| 229 | |
| 230 | |
| 231 | class StreamEndMessage(_CopyableMessage): |
| 232 | _TYPE = native_bt.MESSAGE_TYPE_STREAM_END |
| 233 | |
| 234 | def __init__(self, stream): |
| 235 | utils._check_type(stream, bt2.stream._Stream) |
| 236 | ptr = native_bt.message_stream_end_create(stream._ptr) |
| 237 | |
| 238 | if ptr is None: |
| 239 | raise bt2.CreationError('cannot create stream end message object') |
| 240 | |
| 241 | super().__init__(ptr) |
| 242 | |
| 243 | @property |
| 244 | def stream(self): |
| 245 | stream_ptr = native_bt.message_stream_end_get_stream(self._ptr) |
| 246 | assert(stream_ptr) |
| 247 | return bt2.stream._create_from_ptr(stream_ptr) |
| 248 | |
| 249 | def __eq__(self, other): |
| 250 | if type(other) is not type(self): |
| 251 | return False |
| 252 | |
| 253 | if self.addr == other.addr: |
| 254 | return True |
| 255 | |
| 256 | return self.stream == other.stream |
| 257 | |
| 258 | def _copy(self, copy_func): |
| 259 | # We can always use references here because those properties are |
| 260 | # frozen anyway if they are part of a message. Since the |
| 261 | # user cannot modify them after copying the message, it's |
| 262 | # useless to copy/deep-copy them. |
| 263 | return StreamEndMessage(self.stream) |
| 264 | |
| 265 | |
| 266 | class _InactivityMessageClockSnapshotsIterator(collections.abc.Iterator): |
| 267 | def __init__(self, msg_clock_snapshots): |
| 268 | self._msg_clock_snapshots = msg_clock_snapshots |
| 269 | self._clock_classes = list(msg_clock_snapshots._msg.clock_class_priority_map) |
| 270 | self._at = 0 |
| 271 | |
| 272 | def __next__(self): |
| 273 | if self._at == len(self._clock_classes): |
| 274 | raise StopIteration |
| 275 | |
| 276 | self._at += 1 |
| 277 | return self._clock_classes[at] |
| 278 | |
| 279 | |
| 280 | class _InactivityMessageClockSnapshots(collections.abc.Mapping): |
| 281 | def __init__(self, msg): |
| 282 | self._msg = msg |
| 283 | |
| 284 | def __getitem__(self, clock_class): |
| 285 | utils._check_type(clock_class, bt2.ClockClass) |
| 286 | clock_snapshot_ptr = native_bt.message_inactivity_get_clock_snapshot(self._msg._ptr, |
| 287 | clock_class._ptr) |
| 288 | |
| 289 | if clock_snapshot_ptr is None: |
| 290 | return |
| 291 | |
| 292 | clock_snapshot = bt2.clock_snapshot._create_clock_snapshot_from_ptr(clock_snapshot_ptr) |
| 293 | return clock_snapshot |
| 294 | |
| 295 | def add(self, clock_snapshot): |
| 296 | utils._check_type(clock_snapshot, bt2.clock_snapshot._ClockSnapshot) |
| 297 | ret = native_bt.message_inactivity_set_clock_snapshot(self._msg._ptr, |
| 298 | clock_snapshot._ptr) |
| 299 | utils._handle_ret(ret, "cannot set inactivity message object's clock value") |
| 300 | |
| 301 | def __len__(self): |
| 302 | return len(self._msg.clock_class_priority_map) |
| 303 | |
| 304 | def __iter__(self): |
| 305 | return _InactivityMessageClockSnapshotsIterator(self) |
| 306 | |
| 307 | |
| 308 | class InactivityMessage(_CopyableMessage): |
| 309 | _TYPE = native_bt.MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY |
| 310 | |
| 311 | def __init__(self, cc_prio_map=None): |
| 312 | if cc_prio_map is not None: |
| 313 | utils._check_type(cc_prio_map, bt2.clock_class_priority_map.ClockClassPriorityMap) |
| 314 | cc_prio_map_ptr = cc_prio_map._ptr |
| 315 | else: |
| 316 | cc_prio_map_ptr = None |
| 317 | |
| 318 | ptr = native_bt.message_inactivity_create(cc_prio_map_ptr) |
| 319 | |
| 320 | if ptr is None: |
| 321 | raise bt2.CreationError('cannot create inactivity message object') |
| 322 | |
| 323 | super().__init__(ptr) |
| 324 | |
| 325 | @property |
| 326 | def clock_class_priority_map(self): |
| 327 | cc_prio_map_ptr = native_bt.message_inactivity_get_clock_class_priority_map(self._ptr) |
| 328 | assert(cc_prio_map_ptr) |
| 329 | return bt2.clock_class_priority_map.ClockClassPriorityMap._create_from_ptr(cc_prio_map_ptr) |
| 330 | |
| 331 | @property |
| 332 | def clock_snapshots(self): |
| 333 | return _InactivityMessageClockSnapshots(self) |
| 334 | |
| 335 | def _get_clock_snapshots(self): |
| 336 | clock_snapshots = {} |
| 337 | |
| 338 | for clock_class, clock_snapshot in self.clock_snapshots.items(): |
| 339 | if clock_snapshot is None: |
| 340 | continue |
| 341 | |
| 342 | clock_snapshots[clock_class] = clock_snapshot |
| 343 | |
| 344 | return clock_snapshots |
| 345 | |
| 346 | def __eq__(self, other): |
| 347 | if type(other) is not type(self): |
| 348 | return False |
| 349 | |
| 350 | if self.addr == other.addr: |
| 351 | return True |
| 352 | |
| 353 | self_props = ( |
| 354 | self.clock_class_priority_map, |
| 355 | self._get_clock_snapshots(), |
| 356 | ) |
| 357 | other_props = ( |
| 358 | other.clock_class_priority_map, |
| 359 | other._get_clock_snapshots(), |
| 360 | ) |
| 361 | return self_props == other_props |
| 362 | |
| 363 | def __copy__(self): |
| 364 | cpy = InactivityMessage(self.clock_class_priority_map) |
| 365 | |
| 366 | for clock_class, clock_snapshot in self.clock_snapshots.items(): |
| 367 | if clock_snapshot is None: |
| 368 | continue |
| 369 | |
| 370 | cpy.clock_snapshots.add(clock_snapshot) |
| 371 | |
| 372 | return cpy |
| 373 | |
| 374 | def __deepcopy__(self, memo): |
| 375 | cc_prio_map_cpy = copy.deepcopy(self.clock_class_priority_map) |
| 376 | cpy = InactivityMessage(cc_prio_map_cpy) |
| 377 | |
| 378 | # copy clock values |
| 379 | for orig_clock_class in self.clock_class_priority_map: |
| 380 | orig_clock_snapshot = self.clock_snapshot(orig_clock_class) |
| 381 | |
| 382 | if orig_clock_snapshot is None: |
| 383 | continue |
| 384 | |
| 385 | # find equivalent, copied clock class in CC priority map copy |
| 386 | for cpy_clock_class in cc_prio_map_cpy: |
| 387 | if cpy_clock_class == orig_clock_class: |
| 388 | break |
| 389 | |
| 390 | # create copy of clock value from copied clock class |
| 391 | clock_snapshot_cpy = cpy_clock_class(orig_clock_snapshot.cycles) |
| 392 | |
| 393 | # set copied clock value in message copy |
| 394 | cpy.clock_snapshots.add(clock_snapshot_cpy) |
| 395 | |
| 396 | memo[id(self)] = cpy |
| 397 | return cpy |
| 398 | |
| 399 | |
| 400 | class _DiscardedElementsMessage(_Message): |
| 401 | def __eq__(self, other): |
| 402 | if type(other) is not type(self): |
| 403 | return False |
| 404 | |
| 405 | if self.addr == other.addr: |
| 406 | return True |
| 407 | |
| 408 | self_props = ( |
| 409 | self.count, |
| 410 | self.stream, |
| 411 | self.beginning_clock_snapshot, |
| 412 | self.end_clock_snapshot, |
| 413 | ) |
| 414 | other_props = ( |
| 415 | other.count, |
| 416 | other.stream, |
| 417 | other.beginning_clock_snapshot, |
| 418 | other.end_clock_snapshot, |
| 419 | ) |
| 420 | return self_props == other_props |
| 421 | |
| 422 | |
| 423 | class _DiscardedPacketsMessage(_DiscardedElementsMessage): |
| 424 | _TYPE = native_bt.MESSAGE_TYPE_DISCARDED_PACKETS |
| 425 | |
| 426 | @property |
| 427 | def count(self): |
| 428 | count = native_bt.message_discarded_packets_get_count(self._ptr) |
| 429 | assert(count >= 0) |
| 430 | return count |
| 431 | |
| 432 | @property |
| 433 | def stream(self): |
| 434 | stream_ptr = native_bt.message_discarded_packets_get_stream(self._ptr) |
| 435 | assert(stream_ptr) |
| 436 | return bt2.stream._create_from_ptr(stream_ptr) |
| 437 | |
| 438 | @property |
| 439 | def beginning_clock_snapshot(self): |
| 440 | clock_snapshot_ptr = native_bt.message_discarded_packets_get_begin_clock_snapshot(self._ptr) |
| 441 | |
| 442 | if clock_snapshot_ptr is None: |
| 443 | return |
| 444 | |
| 445 | clock_snapshot = bt2.clock_snapshot._create_clock_snapshot_from_ptr(clock_snapshot_ptr) |
| 446 | return clock_snapshot |
| 447 | |
| 448 | @property |
| 449 | def end_clock_snapshot(self): |
| 450 | clock_snapshot_ptr = native_bt.message_discarded_packets_get_end_clock_snapshot(self._ptr) |
| 451 | |
| 452 | if clock_snapshot_ptr is None: |
| 453 | return |
| 454 | |
| 455 | clock_snapshot = bt2.clock_snapshot._create_clock_snapshot_from_ptr(clock_snapshot_ptr) |
| 456 | return clock_snapshot |
| 457 | |
| 458 | |
| 459 | class _DiscardedEventsMessage(_DiscardedElementsMessage): |
| 460 | _TYPE = native_bt.MESSAGE_TYPE_DISCARDED_EVENTS |
| 461 | |
| 462 | @property |
| 463 | def count(self): |
| 464 | count = native_bt.message_discarded_events_get_count(self._ptr) |
| 465 | assert(count >= 0) |
| 466 | return count |
| 467 | |
| 468 | @property |
| 469 | def stream(self): |
| 470 | stream_ptr = native_bt.message_discarded_events_get_stream(self._ptr) |
| 471 | assert(stream_ptr) |
| 472 | return bt2.stream._create_from_ptr(stream_ptr) |
| 473 | |
| 474 | @property |
| 475 | def beginning_clock_snapshot(self): |
| 476 | clock_snapshot_ptr = native_bt.message_discarded_events_get_begin_clock_snapshot(self._ptr) |
| 477 | |
| 478 | if clock_snapshot_ptr is None: |
| 479 | return |
| 480 | |
| 481 | clock_snapshot = bt2.clock_snapshot._create_clock_snapshot_from_ptr(clock_snapshot_ptr) |
| 482 | return clock_snapshot |
| 483 | |
| 484 | @property |
| 485 | def end_clock_snapshot(self): |
| 486 | clock_snapshot_ptr = native_bt.message_discarded_events_get_end_clock_snapshot(self._ptr) |
| 487 | |
| 488 | if clock_snapshot_ptr is None: |
| 489 | return |
| 490 | |
| 491 | clock_snapshot = bt2.clock_snapshot._create_clock_snapshot_from_ptr(clock_snapshot_ptr) |
| 492 | return clock_snapshot |
| 493 | |
| 494 | |
| 495 | _MESSAGE_TYPE_TO_CLS = { |
| 496 | native_bt.MESSAGE_TYPE_EVENT: EventMessage, |
| 497 | native_bt.MESSAGE_TYPE_PACKET_BEGINNING: PacketBeginningMessage, |
| 498 | native_bt.MESSAGE_TYPE_PACKET_END: PacketEndMessage, |
| 499 | native_bt.MESSAGE_TYPE_STREAM_BEGINNING: StreamBeginningMessage, |
| 500 | native_bt.MESSAGE_TYPE_STREAM_END: StreamEndMessage, |
| 501 | native_bt.MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: InactivityMessage, |
| 502 | native_bt.MESSAGE_TYPE_DISCARDED_PACKETS: _DiscardedPacketsMessage, |
| 503 | native_bt.MESSAGE_TYPE_DISCARDED_EVENTS: _DiscardedEventsMessage, |
| 504 | } |