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