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 import bt2
.clock_snapshot
31 def _create_from_ptr(ptr
):
32 msg_type
= native_bt
.message_get_type(ptr
)
33 return _MESSAGE_TYPE_TO_CLS
[msg_type
]._create
_from
_ptr
(ptr
)
36 class _Message(object._SharedObject
):
37 _get_ref
= staticmethod(native_bt
.message_get_ref
)
38 _put_ref
= staticmethod(native_bt
.message_put_ref
)
41 def _check_has_default_clock_class(clock_class
):
42 if clock_class
is None:
44 'cannot get default clock snapshot: stream class has no default clock class'
48 class _MessageWithDefaultClockSnapshot
:
49 def _get_default_clock_snapshot(self
, borrow_clock_snapshot_ptr
):
50 snapshot_ptr
= borrow_clock_snapshot_ptr(self
._ptr
)
52 return bt2
.clock_snapshot
._ClockSnapshot
._create
_from
_ptr
_and
_get
_ref
(
53 snapshot_ptr
, self
._ptr
, self
._get
_ref
, self
._put
_ref
57 class _EventMessage(_Message
, _MessageWithDefaultClockSnapshot
):
58 _borrow_default_clock_snapshot_ptr
= staticmethod(
59 native_bt
.message_event_borrow_default_clock_snapshot_const
63 def default_clock_snapshot(self
):
64 self
._check
_has
_default
_clock
_class
(self
.event
.stream
.cls
.default_clock_class
)
65 return self
._get
_default
_clock
_snapshot
(self
._borrow
_default
_clock
_snapshot
_ptr
)
69 event_ptr
= native_bt
.message_event_borrow_event(self
._ptr
)
70 assert event_ptr
is not None
71 return bt2
.event
._Event
._create
_from
_ptr
_and
_get
_ref
(
72 event_ptr
, self
._ptr
, self
._get
_ref
, self
._put
_ref
76 class _PacketMessage(_Message
, _MessageWithDefaultClockSnapshot
):
78 def default_clock_snapshot(self
):
79 self
._check
_has
_default
_clock
_class
(self
.packet
.stream
.cls
.default_clock_class
)
80 return self
._get
_default
_clock
_snapshot
(self
._borrow
_default
_clock
_snapshot
_ptr
)
84 packet_ptr
= self
._borrow
_packet
_ptr
(self
._ptr
)
85 assert packet_ptr
is not None
86 return bt2
.packet
._Packet
._create
_from
_ptr
_and
_get
_ref
(packet_ptr
)
89 class _PacketBeginningMessage(_PacketMessage
):
90 _borrow_packet_ptr
= staticmethod(native_bt
.message_packet_beginning_borrow_packet
)
91 _borrow_default_clock_snapshot_ptr
= staticmethod(
92 native_bt
.message_packet_beginning_borrow_default_clock_snapshot_const
96 class _PacketEndMessage(_PacketMessage
):
97 _borrow_packet_ptr
= staticmethod(native_bt
.message_packet_end_borrow_packet
)
98 _borrow_default_clock_snapshot_ptr
= staticmethod(
99 native_bt
.message_packet_end_borrow_default_clock_snapshot_const
103 class _StreamMessage(_Message
, _MessageWithDefaultClockSnapshot
):
106 stream_ptr
= self
._borrow
_stream
_ptr
(self
._ptr
)
108 return bt2
.stream
._Stream
._create
_from
_ptr
_and
_get
_ref
(stream_ptr
)
111 def default_clock_snapshot(self
):
112 self
._check
_has
_default
_clock
_class
(self
.stream
.cls
.default_clock_class
)
114 status
, snapshot_ptr
= self
._borrow
_default
_clock
_snapshot
_ptr
(self
._ptr
)
116 if status
== native_bt
.MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN
:
117 return bt2
.clock_snapshot
._UnknownClockSnapshot
()
119 return bt2
.clock_snapshot
._ClockSnapshot
._create
_from
_ptr
_and
_get
_ref
(
120 snapshot_ptr
, self
._ptr
, self
._get
_ref
, self
._put
_ref
123 def _default_clock_snapshot(self
, raw_value
):
124 utils
._check
_uint
64(raw_value
)
125 self
._set
_default
_clock
_snapshot
(self
._ptr
, raw_value
)
127 _default_clock_snapshot
= property(fset
=_default_clock_snapshot
)
130 class _StreamBeginningMessage(_StreamMessage
):
131 _borrow_stream_ptr
= staticmethod(native_bt
.message_stream_beginning_borrow_stream
)
132 _borrow_default_clock_snapshot_ptr
= staticmethod(
133 native_bt
.message_stream_beginning_borrow_default_clock_snapshot_const
135 _set_default_clock_snapshot
= staticmethod(
136 native_bt
.message_stream_beginning_set_default_clock_snapshot
140 class _StreamEndMessage(_StreamMessage
):
141 _borrow_stream_ptr
= staticmethod(native_bt
.message_stream_end_borrow_stream
)
142 _borrow_default_clock_snapshot_ptr
= staticmethod(
143 native_bt
.message_stream_end_borrow_default_clock_snapshot_const
145 _set_default_clock_snapshot
= staticmethod(
146 native_bt
.message_stream_end_set_default_clock_snapshot
150 class _MessageIteratorInactivityMessage(_Message
, _MessageWithDefaultClockSnapshot
):
151 _borrow_default_clock_snapshot_ptr
= staticmethod(
152 native_bt
.message_message_iterator_inactivity_borrow_default_clock_snapshot_const
156 def default_clock_snapshot(self
):
157 # This kind of message always has a default clock class: no
158 # need to call self._check_has_default_clock_class() here.
159 return self
._get
_default
_clock
_snapshot
(self
._borrow
_default
_clock
_snapshot
_ptr
)
162 class _DiscardedMessage(_Message
, _MessageWithDefaultClockSnapshot
):
165 stream_ptr
= self
._borrow
_stream
_ptr
(self
._ptr
)
167 return bt2
.stream
._Stream
._create
_from
_ptr
_and
_get
_ref
(stream_ptr
)
171 avail
, count
= self
._get
_count
(self
._ptr
)
172 if avail
is native_bt
.PROPERTY_AVAILABILITY_AVAILABLE
:
175 def _set_count(self
, count
):
176 utils
._check
_uint
64(count
)
177 self
._set
_count
(self
._ptr
, count
)
179 _count
= property(fset
=_set_count
)
181 def _check_has_default_clock_snapshots(self
):
182 if not self
._has
_default
_clock
_snapshots
:
184 'cannot get default clock snapshot: such a message has no clock snapshots for this stream class'
188 def beginning_default_clock_snapshot(self
):
189 self
._check
_has
_default
_clock
_snapshots
()
190 return self
._get
_default
_clock
_snapshot
(
191 self
._borrow
_beginning
_clock
_snapshot
_ptr
195 def end_default_clock_snapshot(self
):
196 self
._check
_has
_default
_clock
_snapshots
()
197 return self
._get
_default
_clock
_snapshot
(self
._borrow
_end
_clock
_snapshot
_ptr
)
200 class _DiscardedEventsMessage(_DiscardedMessage
):
201 _borrow_stream_ptr
= staticmethod(
202 native_bt
.message_discarded_events_borrow_stream_const
204 _get_count
= staticmethod(native_bt
.message_discarded_events_get_count
)
205 _set_count
= staticmethod(native_bt
.message_discarded_events_set_count
)
206 _borrow_beginning_clock_snapshot_ptr
= staticmethod(
207 native_bt
.message_discarded_events_borrow_beginning_default_clock_snapshot_const
209 _borrow_end_clock_snapshot_ptr
= staticmethod(
210 native_bt
.message_discarded_events_borrow_end_default_clock_snapshot_const
214 def _has_default_clock_snapshots(self
):
215 return self
.stream
.cls
.discarded_events_have_default_clock_snapshots
218 class _DiscardedPacketsMessage(_DiscardedMessage
):
219 _borrow_stream_ptr
= staticmethod(
220 native_bt
.message_discarded_packets_borrow_stream_const
222 _get_count
= staticmethod(native_bt
.message_discarded_packets_get_count
)
223 _set_count
= staticmethod(native_bt
.message_discarded_packets_set_count
)
224 _borrow_beginning_clock_snapshot_ptr
= staticmethod(
225 native_bt
.message_discarded_packets_borrow_beginning_default_clock_snapshot_const
227 _borrow_end_clock_snapshot_ptr
= staticmethod(
228 native_bt
.message_discarded_packets_borrow_end_default_clock_snapshot_const
232 def _has_default_clock_snapshots(self
):
233 return self
.stream
.cls
.discarded_packets_have_default_clock_snapshots
236 _MESSAGE_TYPE_TO_CLS
= {
237 native_bt
.MESSAGE_TYPE_EVENT
: _EventMessage
,
238 native_bt
.MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
: _MessageIteratorInactivityMessage
,
239 native_bt
.MESSAGE_TYPE_STREAM_BEGINNING
: _StreamBeginningMessage
,
240 native_bt
.MESSAGE_TYPE_STREAM_END
: _StreamEndMessage
,
241 native_bt
.MESSAGE_TYPE_PACKET_BEGINNING
: _PacketBeginningMessage
,
242 native_bt
.MESSAGE_TYPE_PACKET_END
: _PacketEndMessage
,
243 native_bt
.MESSAGE_TYPE_DISCARDED_EVENTS
: _DiscardedEventsMessage
,
244 native_bt
.MESSAGE_TYPE_DISCARDED_PACKETS
: _DiscardedPacketsMessage
,