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