lib: make discarded events/packets support and clock snapshots optional
[babeltrace.git] / bindings / python / bt2 / bt2 / message.py
CommitLineData
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
23from bt2 import native_bt, object, utils
4b552f8b 24import bt2.clock_snapshot
81447b5b
PP
25import bt2.packet
26import bt2.stream
27import bt2.event
28import bt2
29
30
31def _create_from_ptr(ptr):
5602ef81 32 msg_type = native_bt.message_get_type(ptr)
81447b5b 33
5602ef81
SM
34 if msg_type not in _MESSAGE_TYPE_TO_CLS:
35 raise bt2.Error('unknown message type: {}'.format(msg_type))
81447b5b 36
5602ef81 37 return _MESSAGE_TYPE_TO_CLS[msg_type]._create_from_ptr(ptr)
81447b5b
PP
38
39
78288f58 40class _Message(object._SharedObject):
2ae9f48c
SM
41 _get_ref = staticmethod(native_bt.message_get_ref)
42 _put_ref = staticmethod(native_bt.message_put_ref)
81447b5b 43
2e90378a
PP
44 @staticmethod
45 def _check_has_default_clock_class(clock_class):
46 if clock_class is None:
47 raise bt2.NonexistentClockSnapshot('cannot get default clock snapshot: stream class has no default clock class')
48
81447b5b 49
9ec609ec
SM
50class _MessageWithDefaultClockSnapshot:
51 def _get_default_clock_snapshot(self, borrow_clock_snapshot_ptr):
9ec609ec 52 snapshot_ptr = borrow_clock_snapshot_ptr(self._ptr)
811644b8 53
9ec609ec
SM
54 return bt2.clock_snapshot._ClockSnapshot._create_from_ptr_and_get_ref(
55 snapshot_ptr, self._ptr, self._get_ref, self._put_ref)
811644b8 56
9ec609ec
SM
57
58class _EventMessage(_Message, _MessageWithDefaultClockSnapshot):
59 _borrow_default_clock_snapshot_ptr = staticmethod(native_bt.message_event_borrow_default_clock_snapshot_const)
60
9ec609ec
SM
61 @property
62 def default_clock_snapshot(self):
2e90378a 63 self._check_has_default_clock_class(self.event.packet.stream.stream_class.default_clock_class)
9ec609ec 64 return self._get_default_clock_snapshot(self._borrow_default_clock_snapshot_ptr)
811644b8 65
2ae9f48c
SM
66 @property
67 def event(self):
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)
81447b5b 72
81447b5b 73
9ec609ec 74class _PacketMessage(_Message, _MessageWithDefaultClockSnapshot):
81447b5b 75 @property
9ec609ec 76 def default_clock_snapshot(self):
2e90378a 77 self._check_has_default_clock_class(self.packet.stream.stream_class.default_clock_class)
9ec609ec 78 return self._get_default_clock_snapshot(self._borrow_default_clock_snapshot_ptr)
81447b5b 79
81447b5b
PP
80 @property
81 def packet(self):
9ec609ec
SM
82 packet_ptr = self._borrow_packet_ptr(self._ptr)
83 assert packet_ptr is not None
84 return bt2.packet._Packet._create_from_ptr_and_get_ref(packet_ptr)
811644b8 85
811644b8 86
9ec609ec
SM
87class _PacketBeginningMessage(_PacketMessage):
88 _borrow_packet_ptr = staticmethod(native_bt.message_packet_beginning_borrow_packet)
89 _borrow_default_clock_snapshot_ptr = staticmethod(native_bt.message_packet_beginning_borrow_default_clock_snapshot_const)
811644b8 90
811644b8 91
9ec609ec
SM
92class _PacketEndMessage(_PacketMessage):
93 _borrow_packet_ptr = staticmethod(native_bt.message_packet_end_borrow_packet)
94 _borrow_default_clock_snapshot_ptr = staticmethod(native_bt.message_packet_end_borrow_default_clock_snapshot_const)
811644b8 95
81447b5b 96
9ec609ec 97class _StreamMessage(_Message):
81447b5b
PP
98 @property
99 def stream(self):
9ec609ec
SM
100 stream_ptr = self._borrow_stream_ptr(self._ptr)
101 assert stream_ptr
102 return bt2.stream._Stream._create_from_ptr_and_get_ref(stream_ptr)
811644b8 103
811644b8 104
9ec609ec
SM
105class _StreamBeginningMessage(_StreamMessage):
106 _borrow_stream_ptr = staticmethod(native_bt.message_stream_beginning_borrow_stream)
811644b8 107
811644b8 108
9ec609ec
SM
109class _StreamEndMessage(_StreamMessage):
110 _borrow_stream_ptr = staticmethod(native_bt.message_stream_end_borrow_stream)
811644b8 111
81447b5b 112
9ec609ec 113class _StreamActivityMessage(_Message):
81447b5b 114 @property
9ec609ec 115 def default_clock_snapshot(self):
2e90378a 116 self._check_has_default_clock_class(self.stream.stream_class.default_clock_class)
9ec609ec 117 status, snapshot_ptr = self._borrow_default_clock_snapshot_ptr(self._ptr)
81447b5b 118
9ec609ec
SM
119 if status == native_bt.MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN:
120 snapshot_type = bt2.clock_snapshot._ClockSnapshot
121 elif status == native_bt.MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN:
122 snapshot_type = bt2.clock_snapshot._UnknownClockSnapshot
123 elif status == native_bt.MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE:
124 snapshot_type = bt2.clock_snapshot._InfiniteClockSnapshot
811644b8 125 else:
9ec609ec 126 raise bt2.Error('cannot borrow default clock snapshot from message')
81447b5b 127
9ec609ec 128 assert snapshot_ptr is not None
81447b5b 129
9ec609ec
SM
130 return snapshot_type._create_from_ptr_and_get_ref(
131 snapshot_ptr, self._ptr, self._get_ref, self._put_ref)
81447b5b 132
9ec609ec
SM
133 def _default_clock_snapshot(self, value):
134 self._set_default_clock_snapshot_ptr(self._ptr, value)
81447b5b 135
9ec609ec 136 _default_clock_snapshot = property(fset=_default_clock_snapshot)
811644b8 137
71fd6f52 138 @property
9ec609ec
SM
139 def stream(self):
140 stream_ptr = self._borrow_stream_ptr(self._ptr)
141 assert stream_ptr
142 return bt2.stream._Stream._create_from_ptr_and_get_ref(stream_ptr)
811644b8
PP
143
144
9ec609ec
SM
145class _StreamActivityBeginningMessage(_StreamActivityMessage):
146 _borrow_default_clock_snapshot_ptr = staticmethod(native_bt.message_stream_activity_beginning_borrow_default_clock_snapshot_const)
147 _set_default_clock_snapshot_ptr = staticmethod(native_bt.message_stream_activity_beginning_set_default_clock_snapshot)
148 _borrow_stream_ptr = staticmethod(native_bt.message_stream_activity_beginning_borrow_stream)
811644b8 149
811644b8 150
9ec609ec
SM
151class _StreamActivityEndMessage(_StreamActivityMessage):
152 _borrow_default_clock_snapshot_ptr = staticmethod(native_bt.message_stream_activity_end_borrow_default_clock_snapshot_const)
153 _set_default_clock_snapshot_ptr = staticmethod(native_bt.message_stream_activity_end_set_default_clock_snapshot)
154 _borrow_stream_ptr = staticmethod(native_bt.message_stream_activity_end_borrow_stream)
811644b8
PP
155
156
9ec609ec 157class _MessageIteratorInactivityMessage(_Message, _MessageWithDefaultClockSnapshot):
9ec609ec 158 _borrow_default_clock_snapshot_ptr = staticmethod(native_bt.message_message_iterator_inactivity_borrow_default_clock_snapshot_const)
811644b8
PP
159
160 @property
9ec609ec 161 def default_clock_snapshot(self):
2e90378a
PP
162 # This kind of message always has a default clock class: no
163 # need to call self._check_has_default_clock_class() here.
9ec609ec
SM
164 return self._get_default_clock_snapshot(self._borrow_default_clock_snapshot_ptr)
165
811644b8 166
9ec609ec 167class _DiscardedMessage(_Message, _MessageWithDefaultClockSnapshot):
811644b8
PP
168 @property
169 def stream(self):
9ec609ec
SM
170 stream_ptr = self._borrow_stream_ptr(self._ptr)
171 assert stream_ptr
172 return bt2.stream._Stream._create_from_ptr_and_get_ref(stream_ptr)
811644b8 173
811644b8
PP
174 @property
175 def count(self):
9ec609ec
SM
176 avail, count = self._get_count(self._ptr)
177 if avail is native_bt.PROPERTY_AVAILABILITY_AVAILABLE:
178 return count
811644b8 179
9ec609ec
SM
180 def _set_count(self, count):
181 utils._check_uint64(count)
182 self._set_count(self._ptr, count)
183
184 _count = property(fset=_set_count)
811644b8 185
2e90378a
PP
186 def _check_has_default_clock_snapshots(self):
187 if not self._has_default_clock_snapshots:
188 raise bt2.NonexistentClockSnapshot('cannot get default clock snapshot: such a message has no clock snapshots for this stream class')
189
811644b8 190 @property
9ec609ec 191 def beginning_default_clock_snapshot(self):
2e90378a 192 self._check_has_default_clock_snapshots()
9ec609ec 193 return self._get_default_clock_snapshot(self._borrow_beginning_clock_snapshot_ptr)
811644b8 194
9ec609ec
SM
195 @property
196 def end_default_clock_snapshot(self):
2e90378a 197 self._check_has_default_clock_snapshots()
9ec609ec 198 return self._get_default_clock_snapshot(self._borrow_end_clock_snapshot_ptr)
811644b8 199
811644b8 200
9ec609ec
SM
201class _DiscardedEventsMessage(_DiscardedMessage):
202 _borrow_stream_ptr = staticmethod(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)
9ec609ec
SM
205 _borrow_beginning_clock_snapshot_ptr = staticmethod(native_bt.message_discarded_events_borrow_default_beginning_clock_snapshot_const)
206 _borrow_end_clock_snapshot_ptr = staticmethod(native_bt.message_discarded_events_borrow_default_end_clock_snapshot_const)
811644b8 207
2e90378a
PP
208 @property
209 def _has_default_clock_snapshots(self):
210 return self.stream.stream_class.discarded_events_have_default_clock_snapshots
211
811644b8 212
9ec609ec
SM
213class _DiscardedPacketsMessage(_DiscardedMessage):
214 _borrow_stream_ptr = staticmethod(native_bt.message_discarded_packets_borrow_stream_const)
215 _get_count = staticmethod(native_bt.message_discarded_packets_get_count)
216 _set_count = staticmethod(native_bt.message_discarded_packets_set_count)
9ec609ec
SM
217 _borrow_beginning_clock_snapshot_ptr = staticmethod(native_bt.message_discarded_packets_borrow_default_beginning_clock_snapshot_const)
218 _borrow_end_clock_snapshot_ptr = staticmethod(native_bt.message_discarded_packets_borrow_default_end_clock_snapshot_const)
81447b5b 219
2e90378a
PP
220 @property
221 def _has_default_clock_snapshots(self):
222 return self.stream.stream_class.discarded_packets_have_default_clock_snapshots
223
81447b5b 224
5602ef81 225_MESSAGE_TYPE_TO_CLS = {
2ae9f48c 226 native_bt.MESSAGE_TYPE_EVENT: _EventMessage,
9ec609ec 227 native_bt.MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: _MessageIteratorInactivityMessage,
2ae9f48c 228 native_bt.MESSAGE_TYPE_STREAM_BEGINNING: _StreamBeginningMessage,
5f25509b 229 native_bt.MESSAGE_TYPE_STREAM_END: _StreamEndMessage,
9ec609ec
SM
230 native_bt.MESSAGE_TYPE_PACKET_BEGINNING: _PacketBeginningMessage,
231 native_bt.MESSAGE_TYPE_PACKET_END: _PacketEndMessage,
232 native_bt.MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING: _StreamActivityBeginningMessage,
233 native_bt.MESSAGE_TYPE_STREAM_ACTIVITY_END: _StreamActivityEndMessage,
5602ef81 234 native_bt.MESSAGE_TYPE_DISCARDED_EVENTS: _DiscardedEventsMessage,
9ec609ec 235 native_bt.MESSAGE_TYPE_DISCARDED_PACKETS: _DiscardedPacketsMessage,
81447b5b 236}
This page took 0.048114 seconds and 4 git commands to generate.