1 # The MIT License (MIT)
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
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:
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
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
23 from bt2
import native_bt
, object, utils
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
30 def _create_from_ptr(ptr
):
31 msg_type
= native_bt
.message_get_type(ptr
)
32 return _MESSAGE_TYPE_TO_CLS
[msg_type
]._create
_from
_ptr
(ptr
)
35 class _MessageConst(object._SharedObject
):
36 _get_ref
= staticmethod(native_bt
.message_get_ref
)
37 _put_ref
= staticmethod(native_bt
.message_put_ref
)
40 def _check_has_default_clock_class(clock_class
):
41 if clock_class
is None:
43 'cannot get default clock snapshot: stream class has no default clock class'
47 class _Message(_MessageConst
):
51 class _MessageWithDefaultClockSnapshot
:
52 def _get_default_clock_snapshot(self
, borrow_clock_snapshot_ptr
):
53 snapshot_ptr
= borrow_clock_snapshot_ptr(self
._ptr
)
55 return bt2_clock_snapshot
._ClockSnapshotConst
._create
_from
_ptr
_and
_get
_ref
(
56 snapshot_ptr
, self
._ptr
, self
._get
_ref
, self
._put
_ref
60 class _EventMessageConst(_MessageConst
, _MessageWithDefaultClockSnapshot
):
61 _borrow_default_clock_snapshot
= staticmethod(
62 native_bt
.message_event_borrow_default_clock_snapshot_const
64 _borrow_event
= staticmethod(native_bt
.message_event_borrow_event_const
)
65 _event_pycls
= property(lambda _
: bt2_event
._EventConst
)
68 def default_clock_snapshot(self
):
69 self
._check
_has
_default
_clock
_class
(self
.event
.stream
.cls
.default_clock_class
)
70 return self
._get
_default
_clock
_snapshot
(self
._borrow
_default
_clock
_snapshot
)
74 event_ptr
= self
._borrow
_event
(self
._ptr
)
75 assert event_ptr
is not None
76 return self
._event
_pycls
._create
_from
_ptr
_and
_get
_ref
(
77 event_ptr
, self
._ptr
, self
._get
_ref
, self
._put
_ref
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
)
87 class _PacketMessageConst(_MessageConst
, _MessageWithDefaultClockSnapshot
):
88 _packet_pycls
= bt2_packet
._PacketConst
91 def default_clock_snapshot(self
):
92 self
._check
_has
_default
_clock
_class
(self
.packet
.stream
.cls
.default_clock_class
)
93 return self
._get
_default
_clock
_snapshot
(self
._borrow
_default
_clock
_snapshot
_ptr
)
97 packet_ptr
= self
._borrow
_packet
(self
._ptr
)
98 assert packet_ptr
is not None
99 return self
._packet
_pycls
._create
_from
_ptr
_and
_get
_ref
(packet_ptr
)
102 class _PacketMessage(_PacketMessageConst
, _Message
):
103 _packet_pycls
= bt2_packet
._Packet
106 class _PacketBeginningMessageConst(_PacketMessageConst
):
107 _borrow_packet
= staticmethod(
108 native_bt
.message_packet_beginning_borrow_packet_const
110 _borrow_default_clock_snapshot_ptr
= staticmethod(
111 native_bt
.message_packet_beginning_borrow_default_clock_snapshot_const
115 class _PacketBeginningMessage(_PacketMessage
):
116 _borrow_packet
= staticmethod(native_bt
.message_packet_beginning_borrow_packet
)
119 class _PacketEndMessageConst(_PacketMessageConst
):
120 _borrow_packet
= staticmethod(native_bt
.message_packet_end_borrow_packet_const
)
121 _borrow_default_clock_snapshot_ptr
= staticmethod(
122 native_bt
.message_packet_end_borrow_default_clock_snapshot_const
126 class _PacketEndMessage(_PacketMessage
):
127 _borrow_packet
= staticmethod(native_bt
.message_packet_end_borrow_packet
)
130 class _StreamMessageConst(_MessageConst
, _MessageWithDefaultClockSnapshot
):
131 _stream_pycls
= property(lambda _
: bt2_stream
._StreamConst
)
135 stream_ptr
= self
._borrow
_stream
_ptr
(self
._ptr
)
137 return self
._stream
_pycls
._create
_from
_ptr
_and
_get
_ref
(stream_ptr
)
140 def default_clock_snapshot(self
):
141 self
._check
_has
_default
_clock
_class
(self
.stream
.cls
.default_clock_class
)
143 status
, snapshot_ptr
= self
._borrow
_default
_clock
_snapshot
_ptr
(self
._ptr
)
145 if status
== native_bt
.MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN
:
146 return bt2_clock_snapshot
._UnknownClockSnapshot
()
148 return bt2_clock_snapshot
._ClockSnapshotConst
._create
_from
_ptr
_and
_get
_ref
(
149 snapshot_ptr
, self
._ptr
, self
._get
_ref
, self
._put
_ref
153 class _StreamMessage(_StreamMessageConst
, _Message
):
154 def _default_clock_snapshot(self
, raw_value
):
155 utils
._check
_uint
64(raw_value
)
156 self
._set
_default
_clock
_snapshot
(self
._ptr
, raw_value
)
158 _default_clock_snapshot
= property(
159 fget
=_StreamMessageConst
.default_clock_snapshot
.fget
,
160 fset
=_default_clock_snapshot
,
162 _stream_pycls
= property(lambda _
: bt2_stream
._Stream
)
165 class _StreamBeginningMessageConst(_StreamMessageConst
):
166 _borrow_stream_ptr
= staticmethod(
167 native_bt
.message_stream_beginning_borrow_stream_const
169 _borrow_default_clock_snapshot_ptr
= staticmethod(
170 native_bt
.message_stream_beginning_borrow_default_clock_snapshot_const
174 class _StreamBeginningMessage(_StreamMessage
):
175 _borrow_stream_ptr
= staticmethod(native_bt
.message_stream_beginning_borrow_stream
)
176 _set_default_clock_snapshot
= staticmethod(
177 native_bt
.message_stream_beginning_set_default_clock_snapshot
181 class _StreamEndMessageConst(_StreamMessageConst
):
182 _borrow_stream_ptr
= staticmethod(native_bt
.message_stream_end_borrow_stream_const
)
183 _borrow_default_clock_snapshot_ptr
= staticmethod(
184 native_bt
.message_stream_end_borrow_default_clock_snapshot_const
188 class _StreamEndMessage(_StreamMessage
):
189 _borrow_stream_ptr
= staticmethod(native_bt
.message_stream_end_borrow_stream
)
190 _set_default_clock_snapshot
= staticmethod(
191 native_bt
.message_stream_end_set_default_clock_snapshot
195 class _MessageIteratorInactivityMessageConst(
196 _MessageConst
, _MessageWithDefaultClockSnapshot
198 _borrow_clock_snapshot_ptr
= staticmethod(
199 native_bt
.message_message_iterator_inactivity_borrow_clock_snapshot_const
203 def clock_snapshot(self
):
204 # This kind of message always has a clock class: no
205 # need to call self._check_has_default_clock_class() here.
206 return self
._get
_default
_clock
_snapshot
(self
._borrow
_clock
_snapshot
_ptr
)
209 class _MessageIteratorInactivityMessage(
210 _MessageIteratorInactivityMessageConst
, _Message
215 class _DiscardedMessageConst(_MessageConst
, _MessageWithDefaultClockSnapshot
):
216 _stream_pycls
= property(lambda _
: bt2_stream
._StreamConst
)
220 stream_ptr
= self
._borrow
_stream
_ptr
(self
._ptr
)
222 return self
._stream
_pycls
._create
_from
_ptr
_and
_get
_ref
(stream_ptr
)
226 avail
, count
= self
._get
_count
(self
._ptr
)
227 if avail
is native_bt
.PROPERTY_AVAILABILITY_AVAILABLE
:
230 def _check_has_default_clock_snapshots(self
):
231 if not self
._has
_default
_clock
_snapshots
:
233 'cannot get default clock snapshot: such a message has no clock snapshots for this stream class'
237 def beginning_default_clock_snapshot(self
):
238 self
._check
_has
_default
_clock
_snapshots
()
239 return self
._get
_default
_clock
_snapshot
(
240 self
._borrow
_beginning
_clock
_snapshot
_ptr
244 def end_default_clock_snapshot(self
):
245 self
._check
_has
_default
_clock
_snapshots
()
246 return self
._get
_default
_clock
_snapshot
(self
._borrow
_end
_clock
_snapshot
_ptr
)
249 class _DiscardedMessage(_DiscardedMessageConst
, _Message
):
250 _stream_pycls
= property(lambda _
: bt2_stream
._Stream
)
252 def _set_count(self
, count
):
253 utils
._check
_uint
64(count
)
254 self
._set
_count
(self
._ptr
, count
)
256 _count
= property(fget
=_DiscardedMessageConst
.count
.fget
, fset
=_set_count
)
259 class _DiscardedEventsMessageConst(_DiscardedMessageConst
):
260 _borrow_stream_ptr
= staticmethod(
261 native_bt
.message_discarded_events_borrow_stream_const
263 _get_count
= staticmethod(native_bt
.message_discarded_events_get_count
)
264 _borrow_beginning_clock_snapshot_ptr
= staticmethod(
265 native_bt
.message_discarded_events_borrow_beginning_default_clock_snapshot_const
267 _borrow_end_clock_snapshot_ptr
= staticmethod(
268 native_bt
.message_discarded_events_borrow_end_default_clock_snapshot_const
272 def _has_default_clock_snapshots(self
):
273 return self
.stream
.cls
.discarded_events_have_default_clock_snapshots
276 class _DiscardedEventsMessage(_DiscardedMessage
):
277 _borrow_stream_ptr
= staticmethod(native_bt
.message_discarded_events_borrow_stream
)
278 _set_count
= staticmethod(native_bt
.message_discarded_events_set_count
)
281 class _DiscardedPacketsMessageConst(_DiscardedMessageConst
):
282 _borrow_stream_ptr
= staticmethod(
283 native_bt
.message_discarded_packets_borrow_stream_const
285 _get_count
= staticmethod(native_bt
.message_discarded_packets_get_count
)
286 _borrow_beginning_clock_snapshot_ptr
= staticmethod(
287 native_bt
.message_discarded_packets_borrow_beginning_default_clock_snapshot_const
289 _borrow_end_clock_snapshot_ptr
= staticmethod(
290 native_bt
.message_discarded_packets_borrow_end_default_clock_snapshot_const
294 def _has_default_clock_snapshots(self
):
295 return self
.stream
.cls
.discarded_packets_have_default_clock_snapshots
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
)
303 _MESSAGE_TYPE_TO_CLS
= {
304 native_bt
.MESSAGE_TYPE_EVENT
: _EventMessage
,
305 native_bt
.MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
: _MessageIteratorInactivityMessage
,
306 native_bt
.MESSAGE_TYPE_STREAM_BEGINNING
: _StreamBeginningMessage
,
307 native_bt
.MESSAGE_TYPE_STREAM_END
: _StreamEndMessage
,
308 native_bt
.MESSAGE_TYPE_PACKET_BEGINNING
: _PacketBeginningMessage
,
309 native_bt
.MESSAGE_TYPE_PACKET_END
: _PacketEndMessage
,
310 native_bt
.MESSAGE_TYPE_DISCARDED_EVENTS
: _DiscardedEventsMessage
,
311 native_bt
.MESSAGE_TYPE_DISCARDED_PACKETS
: _DiscardedPacketsMessage
,
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
,