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