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 _Message(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 _MessageWithDefaultClockSnapshot
:
48 def _get_default_clock_snapshot(self
, borrow_clock_snapshot_ptr
):
49 snapshot_ptr
= borrow_clock_snapshot_ptr(self
._ptr
)
51 return bt2_clock_snapshot
._ClockSnapshotConst
._create
_from
_ptr
_and
_get
_ref
(
52 snapshot_ptr
, self
._ptr
, self
._get
_ref
, self
._put
_ref
56 class _EventMessage(_Message
, _MessageWithDefaultClockSnapshot
):
57 _borrow_default_clock_snapshot_ptr
= staticmethod(
58 native_bt
.message_event_borrow_default_clock_snapshot_const
62 def default_clock_snapshot(self
):
63 self
._check
_has
_default
_clock
_class
(self
.event
.stream
.cls
.default_clock_class
)
64 return self
._get
_default
_clock
_snapshot
(self
._borrow
_default
_clock
_snapshot
_ptr
)
68 event_ptr
= native_bt
.message_event_borrow_event(self
._ptr
)
69 assert event_ptr
is not None
70 return bt2_event
._Event
._create
_from
_ptr
_and
_get
_ref
(
71 event_ptr
, self
._ptr
, self
._get
_ref
, self
._put
_ref
75 class _PacketMessage(_Message
, _MessageWithDefaultClockSnapshot
):
77 def default_clock_snapshot(self
):
78 self
._check
_has
_default
_clock
_class
(self
.packet
.stream
.cls
.default_clock_class
)
79 return self
._get
_default
_clock
_snapshot
(self
._borrow
_default
_clock
_snapshot
_ptr
)
83 packet_ptr
= self
._borrow
_packet
_ptr
(self
._ptr
)
84 assert packet_ptr
is not None
85 return bt2_packet
._Packet
._create
_from
_ptr
_and
_get
_ref
(packet_ptr
)
88 class _PacketBeginningMessage(_PacketMessage
):
89 _borrow_packet_ptr
= staticmethod(native_bt
.message_packet_beginning_borrow_packet
)
90 _borrow_default_clock_snapshot_ptr
= staticmethod(
91 native_bt
.message_packet_beginning_borrow_default_clock_snapshot_const
95 class _PacketEndMessage(_PacketMessage
):
96 _borrow_packet_ptr
= staticmethod(native_bt
.message_packet_end_borrow_packet
)
97 _borrow_default_clock_snapshot_ptr
= staticmethod(
98 native_bt
.message_packet_end_borrow_default_clock_snapshot_const
102 class _StreamMessage(_Message
, _MessageWithDefaultClockSnapshot
):
105 stream_ptr
= self
._borrow
_stream
_ptr
(self
._ptr
)
107 return bt2_stream
._Stream
._create
_from
_ptr
_and
_get
_ref
(stream_ptr
)
110 def default_clock_snapshot(self
):
111 self
._check
_has
_default
_clock
_class
(self
.stream
.cls
.default_clock_class
)
113 status
, snapshot_ptr
= self
._borrow
_default
_clock
_snapshot
_ptr
(self
._ptr
)
115 if status
== native_bt
.MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN
:
116 return bt2_clock_snapshot
._UnknownClockSnapshot
()
118 return bt2_clock_snapshot
._ClockSnapshotConst
._create
_from
_ptr
_and
_get
_ref
(
119 snapshot_ptr
, self
._ptr
, self
._get
_ref
, self
._put
_ref
122 def _default_clock_snapshot(self
, raw_value
):
123 utils
._check
_uint
64(raw_value
)
124 self
._set
_default
_clock
_snapshot
(self
._ptr
, raw_value
)
126 _default_clock_snapshot
= property(fset
=_default_clock_snapshot
)
129 class _StreamBeginningMessage(_StreamMessage
):
130 _borrow_stream_ptr
= staticmethod(native_bt
.message_stream_beginning_borrow_stream
)
131 _borrow_default_clock_snapshot_ptr
= staticmethod(
132 native_bt
.message_stream_beginning_borrow_default_clock_snapshot_const
134 _set_default_clock_snapshot
= staticmethod(
135 native_bt
.message_stream_beginning_set_default_clock_snapshot
139 class _StreamEndMessage(_StreamMessage
):
140 _borrow_stream_ptr
= staticmethod(native_bt
.message_stream_end_borrow_stream
)
141 _borrow_default_clock_snapshot_ptr
= staticmethod(
142 native_bt
.message_stream_end_borrow_default_clock_snapshot_const
144 _set_default_clock_snapshot
= staticmethod(
145 native_bt
.message_stream_end_set_default_clock_snapshot
149 class _MessageIteratorInactivityMessage(_Message
, _MessageWithDefaultClockSnapshot
):
150 _borrow_default_clock_snapshot_ptr
= staticmethod(
151 native_bt
.message_message_iterator_inactivity_borrow_default_clock_snapshot_const
155 def default_clock_snapshot(self
):
156 # This kind of message always has a default clock class: no
157 # need to call self._check_has_default_clock_class() here.
158 return self
._get
_default
_clock
_snapshot
(self
._borrow
_default
_clock
_snapshot
_ptr
)
161 class _DiscardedMessage(_Message
, _MessageWithDefaultClockSnapshot
):
164 stream_ptr
= self
._borrow
_stream
_ptr
(self
._ptr
)
166 return bt2_stream
._Stream
._create
_from
_ptr
_and
_get
_ref
(stream_ptr
)
170 avail
, count
= self
._get
_count
(self
._ptr
)
171 if avail
is native_bt
.PROPERTY_AVAILABILITY_AVAILABLE
:
174 def _set_count(self
, count
):
175 utils
._check
_uint
64(count
)
176 self
._set
_count
(self
._ptr
, count
)
178 _count
= property(fset
=_set_count
)
180 def _check_has_default_clock_snapshots(self
):
181 if not self
._has
_default
_clock
_snapshots
:
183 'cannot get default clock snapshot: such a message has no clock snapshots for this stream class'
187 def beginning_default_clock_snapshot(self
):
188 self
._check
_has
_default
_clock
_snapshots
()
189 return self
._get
_default
_clock
_snapshot
(
190 self
._borrow
_beginning
_clock
_snapshot
_ptr
194 def end_default_clock_snapshot(self
):
195 self
._check
_has
_default
_clock
_snapshots
()
196 return self
._get
_default
_clock
_snapshot
(self
._borrow
_end
_clock
_snapshot
_ptr
)
199 class _DiscardedEventsMessage(_DiscardedMessage
):
200 _borrow_stream_ptr
= staticmethod(
201 native_bt
.message_discarded_events_borrow_stream_const
203 _get_count
= staticmethod(native_bt
.message_discarded_events_get_count
)
204 _set_count
= staticmethod(native_bt
.message_discarded_events_set_count
)
205 _borrow_beginning_clock_snapshot_ptr
= staticmethod(
206 native_bt
.message_discarded_events_borrow_beginning_default_clock_snapshot_const
208 _borrow_end_clock_snapshot_ptr
= staticmethod(
209 native_bt
.message_discarded_events_borrow_end_default_clock_snapshot_const
213 def _has_default_clock_snapshots(self
):
214 return self
.stream
.cls
.discarded_events_have_default_clock_snapshots
217 class _DiscardedPacketsMessage(_DiscardedMessage
):
218 _borrow_stream_ptr
= staticmethod(
219 native_bt
.message_discarded_packets_borrow_stream_const
221 _get_count
= staticmethod(native_bt
.message_discarded_packets_get_count
)
222 _set_count
= staticmethod(native_bt
.message_discarded_packets_set_count
)
223 _borrow_beginning_clock_snapshot_ptr
= staticmethod(
224 native_bt
.message_discarded_packets_borrow_beginning_default_clock_snapshot_const
226 _borrow_end_clock_snapshot_ptr
= staticmethod(
227 native_bt
.message_discarded_packets_borrow_end_default_clock_snapshot_const
231 def _has_default_clock_snapshots(self
):
232 return self
.stream
.cls
.discarded_packets_have_default_clock_snapshots
235 _MESSAGE_TYPE_TO_CLS
= {
236 native_bt
.MESSAGE_TYPE_EVENT
: _EventMessage
,
237 native_bt
.MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
: _MessageIteratorInactivityMessage
,
238 native_bt
.MESSAGE_TYPE_STREAM_BEGINNING
: _StreamBeginningMessage
,
239 native_bt
.MESSAGE_TYPE_STREAM_END
: _StreamEndMessage
,
240 native_bt
.MESSAGE_TYPE_PACKET_BEGINNING
: _PacketBeginningMessage
,
241 native_bt
.MESSAGE_TYPE_PACKET_END
: _PacketEndMessage
,
242 native_bt
.MESSAGE_TYPE_DISCARDED_EVENTS
: _DiscardedEventsMessage
,
243 native_bt
.MESSAGE_TYPE_DISCARDED_PACKETS
: _DiscardedPacketsMessage
,