Commit | Line | Data |
---|---|---|
0235b0db | 1 | # SPDX-License-Identifier: MIT |
81447b5b PP |
2 | # |
3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> | |
81447b5b | 4 | |
5995b304 | 5 | from bt2 import event as bt2_event |
e5914347 | 6 | from bt2 import utils as bt2_utils |
5995b304 | 7 | from bt2 import object as bt2_object |
3fb99a22 PP |
8 | from bt2 import packet as bt2_packet |
9 | from bt2 import stream as bt2_stream | |
5995b304 SM |
10 | from bt2 import native_bt |
11 | from bt2 import clock_snapshot as bt2_clock_snapshot | |
81447b5b PP |
12 | |
13 | ||
14 | def _create_from_ptr(ptr): | |
5602ef81 | 15 | msg_type = native_bt.message_get_type(ptr) |
5602ef81 | 16 | return _MESSAGE_TYPE_TO_CLS[msg_type]._create_from_ptr(ptr) |
81447b5b PP |
17 | |
18 | ||
e5914347 | 19 | class _MessageConst(bt2_object._SharedObject): |
9dee90bd SM |
20 | @staticmethod |
21 | def _get_ref(ptr): | |
22 | native_bt.message_get_ref(ptr) | |
23 | ||
24 | @staticmethod | |
25 | def _put_ref(ptr): | |
26 | native_bt.message_put_ref(ptr) | |
81447b5b | 27 | |
2e90378a PP |
28 | @staticmethod |
29 | def _check_has_default_clock_class(clock_class): | |
30 | if clock_class is None: | |
1153eccb | 31 | raise ValueError( |
f5567ea8 | 32 | "cannot get default clock snapshot: stream class has no default clock class" |
cfbd7cf3 | 33 | ) |
2e90378a | 34 | |
81447b5b | 35 | |
f0a42b33 FD |
36 | class _Message(_MessageConst): |
37 | pass | |
38 | ||
39 | ||
9ec609ec SM |
40 | class _MessageWithDefaultClockSnapshot: |
41 | def _get_default_clock_snapshot(self, borrow_clock_snapshot_ptr): | |
9ec609ec | 42 | snapshot_ptr = borrow_clock_snapshot_ptr(self._ptr) |
811644b8 | 43 | |
eddea575 | 44 | return bt2_clock_snapshot._ClockSnapshotConst._create_from_ptr_and_get_ref( |
cfbd7cf3 FD |
45 | snapshot_ptr, self._ptr, self._get_ref, self._put_ref |
46 | ) | |
811644b8 | 47 | |
9ec609ec | 48 | |
f0a42b33 FD |
49 | class _EventMessageConst(_MessageConst, _MessageWithDefaultClockSnapshot): |
50 | _borrow_default_clock_snapshot = staticmethod( | |
cfbd7cf3 FD |
51 | native_bt.message_event_borrow_default_clock_snapshot_const |
52 | ) | |
f0a42b33 FD |
53 | _borrow_event = staticmethod(native_bt.message_event_borrow_event_const) |
54 | _event_pycls = property(lambda _: bt2_event._EventConst) | |
9ec609ec | 55 | |
9ec609ec SM |
56 | @property |
57 | def default_clock_snapshot(self): | |
26fc5aed | 58 | self._check_has_default_clock_class(self.event.stream.cls.default_clock_class) |
f0a42b33 | 59 | return self._get_default_clock_snapshot(self._borrow_default_clock_snapshot) |
811644b8 | 60 | |
2ae9f48c SM |
61 | @property |
62 | def event(self): | |
f0a42b33 | 63 | event_ptr = self._borrow_event(self._ptr) |
2ae9f48c | 64 | assert event_ptr is not None |
f0a42b33 | 65 | return self._event_pycls._create_from_ptr_and_get_ref( |
cfbd7cf3 FD |
66 | event_ptr, self._ptr, self._get_ref, self._put_ref |
67 | ) | |
81447b5b | 68 | |
81447b5b | 69 | |
f0a42b33 FD |
70 | class _EventMessage(_EventMessageConst, _Message): |
71 | _borrow_event = staticmethod(native_bt.message_event_borrow_event) | |
72 | _stream_pycls = property(lambda _: bt2_stream._Stream) | |
73 | _event_pycls = property(lambda _: bt2_event._Event) | |
74 | ||
75 | ||
76 | class _PacketMessageConst(_MessageConst, _MessageWithDefaultClockSnapshot): | |
77 | _packet_pycls = bt2_packet._PacketConst | |
78 | ||
81447b5b | 79 | @property |
9ec609ec | 80 | def default_clock_snapshot(self): |
e8ac1aae | 81 | self._check_has_default_clock_class(self.packet.stream.cls.default_clock_class) |
9ec609ec | 82 | return self._get_default_clock_snapshot(self._borrow_default_clock_snapshot_ptr) |
81447b5b | 83 | |
81447b5b PP |
84 | @property |
85 | def packet(self): | |
f0a42b33 | 86 | packet_ptr = self._borrow_packet(self._ptr) |
9ec609ec | 87 | assert packet_ptr is not None |
f0a42b33 | 88 | return self._packet_pycls._create_from_ptr_and_get_ref(packet_ptr) |
811644b8 | 89 | |
811644b8 | 90 | |
f0a42b33 FD |
91 | class _PacketMessage(_PacketMessageConst, _Message): |
92 | _packet_pycls = bt2_packet._Packet | |
93 | ||
94 | ||
95 | class _PacketBeginningMessageConst(_PacketMessageConst): | |
96 | _borrow_packet = staticmethod( | |
97 | native_bt.message_packet_beginning_borrow_packet_const | |
98 | ) | |
cfbd7cf3 FD |
99 | _borrow_default_clock_snapshot_ptr = staticmethod( |
100 | native_bt.message_packet_beginning_borrow_default_clock_snapshot_const | |
101 | ) | |
811644b8 | 102 | |
811644b8 | 103 | |
f0a42b33 FD |
104 | class _PacketBeginningMessage(_PacketMessage): |
105 | _borrow_packet = staticmethod(native_bt.message_packet_beginning_borrow_packet) | |
106 | ||
107 | ||
108 | class _PacketEndMessageConst(_PacketMessageConst): | |
109 | _borrow_packet = staticmethod(native_bt.message_packet_end_borrow_packet_const) | |
cfbd7cf3 FD |
110 | _borrow_default_clock_snapshot_ptr = staticmethod( |
111 | native_bt.message_packet_end_borrow_default_clock_snapshot_const | |
112 | ) | |
811644b8 | 113 | |
81447b5b | 114 | |
f0a42b33 FD |
115 | class _PacketEndMessage(_PacketMessage): |
116 | _borrow_packet = staticmethod(native_bt.message_packet_end_borrow_packet) | |
117 | ||
118 | ||
119 | class _StreamMessageConst(_MessageConst, _MessageWithDefaultClockSnapshot): | |
120 | _stream_pycls = property(lambda _: bt2_stream._StreamConst) | |
121 | ||
81447b5b PP |
122 | @property |
123 | def stream(self): | |
9ec609ec SM |
124 | stream_ptr = self._borrow_stream_ptr(self._ptr) |
125 | assert stream_ptr | |
f0a42b33 | 126 | return self._stream_pycls._create_from_ptr_and_get_ref(stream_ptr) |
811644b8 | 127 | |
81447b5b | 128 | @property |
9ec609ec | 129 | def default_clock_snapshot(self): |
188edac1 SM |
130 | self._check_has_default_clock_class(self.stream.cls.default_clock_class) |
131 | ||
9ec609ec | 132 | status, snapshot_ptr = self._borrow_default_clock_snapshot_ptr(self._ptr) |
81447b5b | 133 | |
188edac1 | 134 | if status == native_bt.MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN: |
3fb99a22 | 135 | return bt2_clock_snapshot._UnknownClockSnapshot() |
81447b5b | 136 | |
eddea575 | 137 | return bt2_clock_snapshot._ClockSnapshotConst._create_from_ptr_and_get_ref( |
cfbd7cf3 FD |
138 | snapshot_ptr, self._ptr, self._get_ref, self._put_ref |
139 | ) | |
811644b8 | 140 | |
f0a42b33 FD |
141 | |
142 | class _StreamMessage(_StreamMessageConst, _Message): | |
188edac1 | 143 | def _default_clock_snapshot(self, raw_value): |
e5914347 | 144 | bt2_utils._check_uint64(raw_value) |
188edac1 | 145 | self._set_default_clock_snapshot(self._ptr, raw_value) |
811644b8 | 146 | |
f0a42b33 FD |
147 | _default_clock_snapshot = property( |
148 | fget=_StreamMessageConst.default_clock_snapshot.fget, | |
149 | fset=_default_clock_snapshot, | |
150 | ) | |
151 | _stream_pycls = property(lambda _: bt2_stream._Stream) | |
811644b8 | 152 | |
811644b8 | 153 | |
f0a42b33 FD |
154 | class _StreamBeginningMessageConst(_StreamMessageConst): |
155 | _borrow_stream_ptr = staticmethod( | |
156 | native_bt.message_stream_beginning_borrow_stream_const | |
157 | ) | |
cfbd7cf3 FD |
158 | _borrow_default_clock_snapshot_ptr = staticmethod( |
159 | native_bt.message_stream_beginning_borrow_default_clock_snapshot_const | |
160 | ) | |
f0a42b33 FD |
161 | |
162 | ||
163 | class _StreamBeginningMessage(_StreamMessage): | |
164 | _borrow_stream_ptr = staticmethod(native_bt.message_stream_beginning_borrow_stream) | |
cfbd7cf3 FD |
165 | _set_default_clock_snapshot = staticmethod( |
166 | native_bt.message_stream_beginning_set_default_clock_snapshot | |
167 | ) | |
811644b8 | 168 | |
188edac1 | 169 | |
f0a42b33 FD |
170 | class _StreamEndMessageConst(_StreamMessageConst): |
171 | _borrow_stream_ptr = staticmethod(native_bt.message_stream_end_borrow_stream_const) | |
cfbd7cf3 FD |
172 | _borrow_default_clock_snapshot_ptr = staticmethod( |
173 | native_bt.message_stream_end_borrow_default_clock_snapshot_const | |
174 | ) | |
f0a42b33 FD |
175 | |
176 | ||
177 | class _StreamEndMessage(_StreamMessage): | |
178 | _borrow_stream_ptr = staticmethod(native_bt.message_stream_end_borrow_stream) | |
cfbd7cf3 FD |
179 | _set_default_clock_snapshot = staticmethod( |
180 | native_bt.message_stream_end_set_default_clock_snapshot | |
181 | ) | |
811644b8 PP |
182 | |
183 | ||
f0a42b33 FD |
184 | class _MessageIteratorInactivityMessageConst( |
185 | _MessageConst, _MessageWithDefaultClockSnapshot | |
186 | ): | |
60d02328 PP |
187 | _borrow_clock_snapshot_ptr = staticmethod( |
188 | native_bt.message_message_iterator_inactivity_borrow_clock_snapshot_const | |
cfbd7cf3 | 189 | ) |
811644b8 PP |
190 | |
191 | @property | |
60d02328 PP |
192 | def clock_snapshot(self): |
193 | # This kind of message always has a clock class: no | |
2e90378a | 194 | # need to call self._check_has_default_clock_class() here. |
60d02328 | 195 | return self._get_default_clock_snapshot(self._borrow_clock_snapshot_ptr) |
9ec609ec | 196 | |
811644b8 | 197 | |
f0a42b33 FD |
198 | class _MessageIteratorInactivityMessage( |
199 | _MessageIteratorInactivityMessageConst, _Message | |
200 | ): | |
201 | pass | |
202 | ||
203 | ||
204 | class _DiscardedMessageConst(_MessageConst, _MessageWithDefaultClockSnapshot): | |
205 | _stream_pycls = property(lambda _: bt2_stream._StreamConst) | |
206 | ||
811644b8 PP |
207 | @property |
208 | def stream(self): | |
9ec609ec SM |
209 | stream_ptr = self._borrow_stream_ptr(self._ptr) |
210 | assert stream_ptr | |
f0a42b33 | 211 | return self._stream_pycls._create_from_ptr_and_get_ref(stream_ptr) |
811644b8 | 212 | |
811644b8 PP |
213 | @property |
214 | def count(self): | |
9ec609ec SM |
215 | avail, count = self._get_count(self._ptr) |
216 | if avail is native_bt.PROPERTY_AVAILABILITY_AVAILABLE: | |
217 | return count | |
811644b8 | 218 | |
2e90378a PP |
219 | def _check_has_default_clock_snapshots(self): |
220 | if not self._has_default_clock_snapshots: | |
1153eccb | 221 | raise ValueError( |
f5567ea8 | 222 | "cannot get default clock snapshot: such a message has no clock snapshots for this stream class" |
cfbd7cf3 | 223 | ) |
2e90378a | 224 | |
811644b8 | 225 | @property |
9ec609ec | 226 | def beginning_default_clock_snapshot(self): |
2e90378a | 227 | self._check_has_default_clock_snapshots() |
cfbd7cf3 FD |
228 | return self._get_default_clock_snapshot( |
229 | self._borrow_beginning_clock_snapshot_ptr | |
230 | ) | |
811644b8 | 231 | |
9ec609ec SM |
232 | @property |
233 | def end_default_clock_snapshot(self): | |
2e90378a | 234 | self._check_has_default_clock_snapshots() |
9ec609ec | 235 | return self._get_default_clock_snapshot(self._borrow_end_clock_snapshot_ptr) |
811644b8 | 236 | |
811644b8 | 237 | |
f0a42b33 FD |
238 | class _DiscardedMessage(_DiscardedMessageConst, _Message): |
239 | _stream_pycls = property(lambda _: bt2_stream._Stream) | |
240 | ||
241 | def _set_count(self, count): | |
e5914347 | 242 | bt2_utils._check_uint64(count) |
e5a41ca3 SM |
243 | |
244 | if count == 0: | |
f5567ea8 | 245 | raise ValueError("discarded {} count is 0".format(self._item_name)) |
e5a41ca3 | 246 | |
f0a42b33 FD |
247 | self._set_count(self._ptr, count) |
248 | ||
249 | _count = property(fget=_DiscardedMessageConst.count.fget, fset=_set_count) | |
250 | ||
251 | ||
252 | class _DiscardedEventsMessageConst(_DiscardedMessageConst): | |
cfbd7cf3 FD |
253 | _borrow_stream_ptr = staticmethod( |
254 | native_bt.message_discarded_events_borrow_stream_const | |
255 | ) | |
9ec609ec | 256 | _get_count = staticmethod(native_bt.message_discarded_events_get_count) |
cfbd7cf3 FD |
257 | _borrow_beginning_clock_snapshot_ptr = staticmethod( |
258 | native_bt.message_discarded_events_borrow_beginning_default_clock_snapshot_const | |
259 | ) | |
260 | _borrow_end_clock_snapshot_ptr = staticmethod( | |
261 | native_bt.message_discarded_events_borrow_end_default_clock_snapshot_const | |
262 | ) | |
811644b8 | 263 | |
2e90378a PP |
264 | @property |
265 | def _has_default_clock_snapshots(self): | |
e8ac1aae | 266 | return self.stream.cls.discarded_events_have_default_clock_snapshots |
2e90378a | 267 | |
811644b8 | 268 | |
dd26af5b | 269 | class _DiscardedEventsMessage(_DiscardedEventsMessageConst, _DiscardedMessage): |
f0a42b33 FD |
270 | _borrow_stream_ptr = staticmethod(native_bt.message_discarded_events_borrow_stream) |
271 | _set_count = staticmethod(native_bt.message_discarded_events_set_count) | |
f5567ea8 | 272 | _item_name = "event" |
f0a42b33 FD |
273 | |
274 | ||
275 | class _DiscardedPacketsMessageConst(_DiscardedMessageConst): | |
cfbd7cf3 FD |
276 | _borrow_stream_ptr = staticmethod( |
277 | native_bt.message_discarded_packets_borrow_stream_const | |
278 | ) | |
9ec609ec | 279 | _get_count = staticmethod(native_bt.message_discarded_packets_get_count) |
cfbd7cf3 FD |
280 | _borrow_beginning_clock_snapshot_ptr = staticmethod( |
281 | native_bt.message_discarded_packets_borrow_beginning_default_clock_snapshot_const | |
282 | ) | |
283 | _borrow_end_clock_snapshot_ptr = staticmethod( | |
284 | native_bt.message_discarded_packets_borrow_end_default_clock_snapshot_const | |
285 | ) | |
81447b5b | 286 | |
2e90378a PP |
287 | @property |
288 | def _has_default_clock_snapshots(self): | |
e8ac1aae | 289 | return self.stream.cls.discarded_packets_have_default_clock_snapshots |
2e90378a | 290 | |
81447b5b | 291 | |
f0a42b33 FD |
292 | class _DiscardedPacketsMessage(_DiscardedPacketsMessageConst, _DiscardedMessage): |
293 | _borrow_stream_ptr = staticmethod(native_bt.message_discarded_packets_borrow_stream) | |
294 | _set_count = staticmethod(native_bt.message_discarded_packets_set_count) | |
f5567ea8 | 295 | _item_name = "packet" |
f0a42b33 FD |
296 | |
297 | ||
5602ef81 | 298 | _MESSAGE_TYPE_TO_CLS = { |
2ae9f48c | 299 | native_bt.MESSAGE_TYPE_EVENT: _EventMessage, |
9ec609ec | 300 | native_bt.MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: _MessageIteratorInactivityMessage, |
2ae9f48c | 301 | native_bt.MESSAGE_TYPE_STREAM_BEGINNING: _StreamBeginningMessage, |
5f25509b | 302 | native_bt.MESSAGE_TYPE_STREAM_END: _StreamEndMessage, |
9ec609ec SM |
303 | native_bt.MESSAGE_TYPE_PACKET_BEGINNING: _PacketBeginningMessage, |
304 | native_bt.MESSAGE_TYPE_PACKET_END: _PacketEndMessage, | |
5602ef81 | 305 | native_bt.MESSAGE_TYPE_DISCARDED_EVENTS: _DiscardedEventsMessage, |
9ec609ec | 306 | native_bt.MESSAGE_TYPE_DISCARDED_PACKETS: _DiscardedPacketsMessage, |
81447b5b | 307 | } |
f0a42b33 FD |
308 | |
309 | _MESSAGE_TYPE_TO_CLS = { | |
310 | native_bt.MESSAGE_TYPE_EVENT: _EventMessageConst, | |
311 | native_bt.MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: _MessageIteratorInactivityMessageConst, | |
312 | native_bt.MESSAGE_TYPE_STREAM_BEGINNING: _StreamBeginningMessageConst, | |
313 | native_bt.MESSAGE_TYPE_STREAM_END: _StreamEndMessageConst, | |
314 | native_bt.MESSAGE_TYPE_PACKET_BEGINNING: _PacketBeginningMessageConst, | |
315 | native_bt.MESSAGE_TYPE_PACKET_END: _PacketEndMessageConst, | |
316 | native_bt.MESSAGE_TYPE_DISCARDED_EVENTS: _DiscardedEventsMessageConst, | |
317 | native_bt.MESSAGE_TYPE_DISCARDED_PACKETS: _DiscardedPacketsMessageConst, | |
318 | } |