Commit | Line | Data |
---|---|---|
81447b5b PP |
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 | |
f192fc47 | 24 | import bt2.clock_snapshot |
81447b5b PP |
25 | import bt2.packet |
26 | import bt2.stream | |
27 | import bt2.event | |
28 | import bt2 | |
29 | ||
30 | ||
31 | def _create_from_ptr(ptr): | |
fa4c33e3 | 32 | msg_type = native_bt.message_get_type(ptr) |
81447b5b | 33 | |
fa4c33e3 SM |
34 | if msg_type not in _MESSAGE_TYPE_TO_CLS: |
35 | raise bt2.Error('unknown message type: {}'.format(msg_type)) | |
81447b5b | 36 | |
fa4c33e3 | 37 | return _MESSAGE_TYPE_TO_CLS[msg_type]._create_from_ptr(ptr) |
81447b5b PP |
38 | |
39 | ||
c3044a97 | 40 | class _Message(object._SharedObject): |
27d97a3f SM |
41 | _get_ref = staticmethod(native_bt.message_get_ref) |
42 | _put_ref = staticmethod(native_bt.message_put_ref) | |
81447b5b | 43 | |
77037b2b PP |
44 | @staticmethod |
45 | def _check_has_default_clock_class(clock_class): | |
46 | if clock_class is None: | |
47 | raise bt2.NonexistentClockSnapshot('cannot get default clock snapshot: stream class has no default clock class') | |
48 | ||
81447b5b | 49 | |
0010c8b0 SM |
50 | class _MessageWithDefaultClockSnapshot: |
51 | def _get_default_clock_snapshot(self, borrow_clock_snapshot_ptr): | |
0010c8b0 | 52 | snapshot_ptr = borrow_clock_snapshot_ptr(self._ptr) |
f6a5e476 | 53 | |
0010c8b0 SM |
54 | return bt2.clock_snapshot._ClockSnapshot._create_from_ptr_and_get_ref( |
55 | snapshot_ptr, self._ptr, self._get_ref, self._put_ref) | |
f6a5e476 | 56 | |
0010c8b0 SM |
57 | |
58 | class _EventMessage(_Message, _MessageWithDefaultClockSnapshot): | |
59 | _borrow_default_clock_snapshot_ptr = staticmethod(native_bt.message_event_borrow_default_clock_snapshot_const) | |
60 | ||
0010c8b0 SM |
61 | @property |
62 | def default_clock_snapshot(self): | |
c88be1c8 | 63 | self._check_has_default_clock_class(self.event.packet.stream.cls.default_clock_class) |
0010c8b0 | 64 | return self._get_default_clock_snapshot(self._borrow_default_clock_snapshot_ptr) |
f6a5e476 | 65 | |
27d97a3f SM |
66 | @property |
67 | def event(self): | |
68 | event_ptr = native_bt.message_event_borrow_event(self._ptr) | |
69 | assert event_ptr is not None | |
70 | return bt2.event._Event._create_from_ptr_and_get_ref( | |
71 | event_ptr, self._ptr, self._get_ref, self._put_ref) | |
81447b5b | 72 | |
81447b5b | 73 | |
0010c8b0 | 74 | class _PacketMessage(_Message, _MessageWithDefaultClockSnapshot): |
81447b5b | 75 | @property |
0010c8b0 | 76 | def default_clock_snapshot(self): |
c88be1c8 | 77 | self._check_has_default_clock_class(self.packet.stream.cls.default_clock_class) |
0010c8b0 | 78 | return self._get_default_clock_snapshot(self._borrow_default_clock_snapshot_ptr) |
81447b5b | 79 | |
81447b5b PP |
80 | @property |
81 | def packet(self): | |
0010c8b0 SM |
82 | packet_ptr = self._borrow_packet_ptr(self._ptr) |
83 | assert packet_ptr is not None | |
84 | return bt2.packet._Packet._create_from_ptr_and_get_ref(packet_ptr) | |
f6a5e476 | 85 | |
f6a5e476 | 86 | |
0010c8b0 SM |
87 | class _PacketBeginningMessage(_PacketMessage): |
88 | _borrow_packet_ptr = staticmethod(native_bt.message_packet_beginning_borrow_packet) | |
89 | _borrow_default_clock_snapshot_ptr = staticmethod(native_bt.message_packet_beginning_borrow_default_clock_snapshot_const) | |
f6a5e476 | 90 | |
f6a5e476 | 91 | |
0010c8b0 SM |
92 | class _PacketEndMessage(_PacketMessage): |
93 | _borrow_packet_ptr = staticmethod(native_bt.message_packet_end_borrow_packet) | |
94 | _borrow_default_clock_snapshot_ptr = staticmethod(native_bt.message_packet_end_borrow_default_clock_snapshot_const) | |
f6a5e476 | 95 | |
81447b5b | 96 | |
0010c8b0 | 97 | class _StreamMessage(_Message): |
81447b5b PP |
98 | @property |
99 | def stream(self): | |
0010c8b0 SM |
100 | stream_ptr = self._borrow_stream_ptr(self._ptr) |
101 | assert stream_ptr | |
102 | return bt2.stream._Stream._create_from_ptr_and_get_ref(stream_ptr) | |
f6a5e476 | 103 | |
f6a5e476 | 104 | |
0010c8b0 SM |
105 | class _StreamBeginningMessage(_StreamMessage): |
106 | _borrow_stream_ptr = staticmethod(native_bt.message_stream_beginning_borrow_stream) | |
f6a5e476 | 107 | |
f6a5e476 | 108 | |
0010c8b0 SM |
109 | class _StreamEndMessage(_StreamMessage): |
110 | _borrow_stream_ptr = staticmethod(native_bt.message_stream_end_borrow_stream) | |
f6a5e476 | 111 | |
81447b5b | 112 | |
dcd94213 PP |
113 | # Specific type to pass an unknown clock snapshot when creating a stream |
114 | # beginning/end message. | |
115 | class _StreamActivityMessageUnknownClockSnapshot: | |
116 | pass | |
117 | ||
118 | ||
119 | # Specific type to pass an infinite clock snapshot when creating a | |
120 | # stream beginning/end message. | |
121 | class _StreamActivityMessageInfiniteClockSnapshot: | |
122 | pass | |
123 | ||
124 | ||
0010c8b0 | 125 | class _StreamActivityMessage(_Message): |
81447b5b | 126 | @property |
0010c8b0 | 127 | def default_clock_snapshot(self): |
0010c8b0 | 128 | status, snapshot_ptr = self._borrow_default_clock_snapshot_ptr(self._ptr) |
81447b5b | 129 | |
0010c8b0 | 130 | if status == native_bt.MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN: |
dcd94213 PP |
131 | cs_type = bt2.clock_snapshot._ClockSnapshot |
132 | assert snapshot_ptr is not None | |
133 | return cs_type._create_from_ptr_and_get_ref(snapshot_ptr, self._ptr, | |
134 | self._get_ref, self._put_ref) | |
0010c8b0 | 135 | elif status == native_bt.MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN: |
dcd94213 | 136 | return bt2.clock_snapshot._UnknownClockSnapshot() |
0010c8b0 | 137 | elif status == native_bt.MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE: |
dcd94213 | 138 | return bt2.clock_snapshot._InfiniteClockSnapshot() |
f6a5e476 | 139 | else: |
0010c8b0 | 140 | raise bt2.Error('cannot borrow default clock snapshot from message') |
81447b5b | 141 | |
0010c8b0 | 142 | def _default_clock_snapshot(self, value): |
dcd94213 PP |
143 | if type(value) is _StreamActivityMessageUnknownClockSnapshot: |
144 | self._set_default_clock_snapshot_state(self._ptr, native_bt.MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN) | |
145 | elif type(value) is _StreamActivityMessageInfiniteClockSnapshot: | |
146 | self._set_default_clock_snapshot_state(self._ptr, native_bt.MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE) | |
147 | else: | |
148 | assert utils._is_uint64(value) | |
a9ee0e7b | 149 | self._set_default_clock_snapshot(self._ptr, value) |
81447b5b | 150 | |
0010c8b0 | 151 | _default_clock_snapshot = property(fset=_default_clock_snapshot) |
f6a5e476 | 152 | |
8ae92ae2 | 153 | @property |
0010c8b0 SM |
154 | def stream(self): |
155 | stream_ptr = self._borrow_stream_ptr(self._ptr) | |
156 | assert stream_ptr | |
157 | return bt2.stream._Stream._create_from_ptr_and_get_ref(stream_ptr) | |
f6a5e476 PP |
158 | |
159 | ||
0010c8b0 SM |
160 | class _StreamActivityBeginningMessage(_StreamActivityMessage): |
161 | _borrow_default_clock_snapshot_ptr = staticmethod(native_bt.message_stream_activity_beginning_borrow_default_clock_snapshot_const) | |
a9ee0e7b | 162 | _set_default_clock_snapshot = staticmethod(native_bt.message_stream_activity_beginning_set_default_clock_snapshot) |
dcd94213 | 163 | _set_default_clock_snapshot_state = staticmethod(native_bt.message_stream_activity_beginning_set_default_clock_snapshot_state) |
0010c8b0 | 164 | _borrow_stream_ptr = staticmethod(native_bt.message_stream_activity_beginning_borrow_stream) |
f6a5e476 | 165 | |
f6a5e476 | 166 | |
0010c8b0 SM |
167 | class _StreamActivityEndMessage(_StreamActivityMessage): |
168 | _borrow_default_clock_snapshot_ptr = staticmethod(native_bt.message_stream_activity_end_borrow_default_clock_snapshot_const) | |
a9ee0e7b | 169 | _set_default_clock_snapshot = staticmethod(native_bt.message_stream_activity_end_set_default_clock_snapshot) |
dcd94213 | 170 | _set_default_clock_snapshot_state = staticmethod(native_bt.message_stream_activity_end_set_default_clock_snapshot_state) |
0010c8b0 | 171 | _borrow_stream_ptr = staticmethod(native_bt.message_stream_activity_end_borrow_stream) |
f6a5e476 PP |
172 | |
173 | ||
0010c8b0 | 174 | class _MessageIteratorInactivityMessage(_Message, _MessageWithDefaultClockSnapshot): |
0010c8b0 | 175 | _borrow_default_clock_snapshot_ptr = staticmethod(native_bt.message_message_iterator_inactivity_borrow_default_clock_snapshot_const) |
f6a5e476 PP |
176 | |
177 | @property | |
0010c8b0 | 178 | def default_clock_snapshot(self): |
77037b2b PP |
179 | # This kind of message always has a default clock class: no |
180 | # need to call self._check_has_default_clock_class() here. | |
0010c8b0 SM |
181 | return self._get_default_clock_snapshot(self._borrow_default_clock_snapshot_ptr) |
182 | ||
f6a5e476 | 183 | |
0010c8b0 | 184 | class _DiscardedMessage(_Message, _MessageWithDefaultClockSnapshot): |
f6a5e476 PP |
185 | @property |
186 | def stream(self): | |
0010c8b0 SM |
187 | stream_ptr = self._borrow_stream_ptr(self._ptr) |
188 | assert stream_ptr | |
189 | return bt2.stream._Stream._create_from_ptr_and_get_ref(stream_ptr) | |
f6a5e476 | 190 | |
f6a5e476 PP |
191 | @property |
192 | def count(self): | |
0010c8b0 SM |
193 | avail, count = self._get_count(self._ptr) |
194 | if avail is native_bt.PROPERTY_AVAILABILITY_AVAILABLE: | |
195 | return count | |
f6a5e476 | 196 | |
0010c8b0 SM |
197 | def _set_count(self, count): |
198 | utils._check_uint64(count) | |
199 | self._set_count(self._ptr, count) | |
200 | ||
201 | _count = property(fset=_set_count) | |
f6a5e476 | 202 | |
77037b2b PP |
203 | def _check_has_default_clock_snapshots(self): |
204 | if not self._has_default_clock_snapshots: | |
205 | raise bt2.NonexistentClockSnapshot('cannot get default clock snapshot: such a message has no clock snapshots for this stream class') | |
206 | ||
f6a5e476 | 207 | @property |
0010c8b0 | 208 | def beginning_default_clock_snapshot(self): |
77037b2b | 209 | self._check_has_default_clock_snapshots() |
0010c8b0 | 210 | return self._get_default_clock_snapshot(self._borrow_beginning_clock_snapshot_ptr) |
f6a5e476 | 211 | |
0010c8b0 SM |
212 | @property |
213 | def end_default_clock_snapshot(self): | |
77037b2b | 214 | self._check_has_default_clock_snapshots() |
0010c8b0 | 215 | return self._get_default_clock_snapshot(self._borrow_end_clock_snapshot_ptr) |
f6a5e476 | 216 | |
f6a5e476 | 217 | |
0010c8b0 SM |
218 | class _DiscardedEventsMessage(_DiscardedMessage): |
219 | _borrow_stream_ptr = staticmethod(native_bt.message_discarded_events_borrow_stream_const) | |
220 | _get_count = staticmethod(native_bt.message_discarded_events_get_count) | |
221 | _set_count = staticmethod(native_bt.message_discarded_events_set_count) | |
5ef34326 PP |
222 | _borrow_beginning_clock_snapshot_ptr = staticmethod(native_bt.message_discarded_events_borrow_beginning_default_clock_snapshot_const) |
223 | _borrow_end_clock_snapshot_ptr = staticmethod(native_bt.message_discarded_events_borrow_end_default_clock_snapshot_const) | |
f6a5e476 | 224 | |
77037b2b PP |
225 | @property |
226 | def _has_default_clock_snapshots(self): | |
c88be1c8 | 227 | return self.stream.cls.discarded_events_have_default_clock_snapshots |
77037b2b | 228 | |
f6a5e476 | 229 | |
0010c8b0 SM |
230 | class _DiscardedPacketsMessage(_DiscardedMessage): |
231 | _borrow_stream_ptr = staticmethod(native_bt.message_discarded_packets_borrow_stream_const) | |
232 | _get_count = staticmethod(native_bt.message_discarded_packets_get_count) | |
233 | _set_count = staticmethod(native_bt.message_discarded_packets_set_count) | |
5ef34326 PP |
234 | _borrow_beginning_clock_snapshot_ptr = staticmethod(native_bt.message_discarded_packets_borrow_beginning_default_clock_snapshot_const) |
235 | _borrow_end_clock_snapshot_ptr = staticmethod(native_bt.message_discarded_packets_borrow_end_default_clock_snapshot_const) | |
81447b5b | 236 | |
77037b2b PP |
237 | @property |
238 | def _has_default_clock_snapshots(self): | |
c88be1c8 | 239 | return self.stream.cls.discarded_packets_have_default_clock_snapshots |
77037b2b | 240 | |
81447b5b | 241 | |
fa4c33e3 | 242 | _MESSAGE_TYPE_TO_CLS = { |
27d97a3f | 243 | native_bt.MESSAGE_TYPE_EVENT: _EventMessage, |
0010c8b0 | 244 | native_bt.MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: _MessageIteratorInactivityMessage, |
27d97a3f | 245 | native_bt.MESSAGE_TYPE_STREAM_BEGINNING: _StreamBeginningMessage, |
871a292a | 246 | native_bt.MESSAGE_TYPE_STREAM_END: _StreamEndMessage, |
0010c8b0 SM |
247 | native_bt.MESSAGE_TYPE_PACKET_BEGINNING: _PacketBeginningMessage, |
248 | native_bt.MESSAGE_TYPE_PACKET_END: _PacketEndMessage, | |
249 | native_bt.MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING: _StreamActivityBeginningMessage, | |
250 | native_bt.MESSAGE_TYPE_STREAM_ACTIVITY_END: _StreamActivityEndMessage, | |
fa4c33e3 | 251 | native_bt.MESSAGE_TYPE_DISCARDED_EVENTS: _DiscardedEventsMessage, |
0010c8b0 | 252 | native_bt.MESSAGE_TYPE_DISCARDED_PACKETS: _DiscardedPacketsMessage, |
81447b5b | 253 | } |