bt2: Add `*ValueConst` classes and adapt tests
[babeltrace.git] / src / 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
3fb99a22
PP
24from bt2 import clock_snapshot as bt2_clock_snapshot
25from bt2 import packet as bt2_packet
26from bt2 import stream as bt2_stream
27from bt2 import event as bt2_event
81447b5b
PP
28
29
30def _create_from_ptr(ptr):
5602ef81 31 msg_type = native_bt.message_get_type(ptr)
5602ef81 32 return _MESSAGE_TYPE_TO_CLS[msg_type]._create_from_ptr(ptr)
81447b5b
PP
33
34
78288f58 35class _Message(object._SharedObject):
2ae9f48c
SM
36 _get_ref = staticmethod(native_bt.message_get_ref)
37 _put_ref = staticmethod(native_bt.message_put_ref)
81447b5b 38
2e90378a
PP
39 @staticmethod
40 def _check_has_default_clock_class(clock_class):
41 if clock_class is None:
1153eccb 42 raise ValueError(
cfbd7cf3
FD
43 'cannot get default clock snapshot: stream class has no default clock class'
44 )
2e90378a 45
81447b5b 46
9ec609ec
SM
47class _MessageWithDefaultClockSnapshot:
48 def _get_default_clock_snapshot(self, borrow_clock_snapshot_ptr):
9ec609ec 49 snapshot_ptr = borrow_clock_snapshot_ptr(self._ptr)
811644b8 50
3fb99a22 51 return bt2_clock_snapshot._ClockSnapshot._create_from_ptr_and_get_ref(
cfbd7cf3
FD
52 snapshot_ptr, self._ptr, self._get_ref, self._put_ref
53 )
811644b8 54
9ec609ec
SM
55
56class _EventMessage(_Message, _MessageWithDefaultClockSnapshot):
cfbd7cf3
FD
57 _borrow_default_clock_snapshot_ptr = staticmethod(
58 native_bt.message_event_borrow_default_clock_snapshot_const
59 )
9ec609ec 60
9ec609ec
SM
61 @property
62 def default_clock_snapshot(self):
26fc5aed 63 self._check_has_default_clock_class(self.event.stream.cls.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
3fb99a22 70 return bt2_event._Event._create_from_ptr_and_get_ref(
cfbd7cf3
FD
71 event_ptr, self._ptr, self._get_ref, self._put_ref
72 )
81447b5b 73
81447b5b 74
9ec609ec 75class _PacketMessage(_Message, _MessageWithDefaultClockSnapshot):
81447b5b 76 @property
9ec609ec 77 def default_clock_snapshot(self):
e8ac1aae 78 self._check_has_default_clock_class(self.packet.stream.cls.default_clock_class)
9ec609ec 79 return self._get_default_clock_snapshot(self._borrow_default_clock_snapshot_ptr)
81447b5b 80
81447b5b
PP
81 @property
82 def packet(self):
9ec609ec
SM
83 packet_ptr = self._borrow_packet_ptr(self._ptr)
84 assert packet_ptr is not None
3fb99a22 85 return bt2_packet._Packet._create_from_ptr_and_get_ref(packet_ptr)
811644b8 86
811644b8 87
9ec609ec
SM
88class _PacketBeginningMessage(_PacketMessage):
89 _borrow_packet_ptr = staticmethod(native_bt.message_packet_beginning_borrow_packet)
cfbd7cf3
FD
90 _borrow_default_clock_snapshot_ptr = staticmethod(
91 native_bt.message_packet_beginning_borrow_default_clock_snapshot_const
92 )
811644b8 93
811644b8 94
9ec609ec
SM
95class _PacketEndMessage(_PacketMessage):
96 _borrow_packet_ptr = staticmethod(native_bt.message_packet_end_borrow_packet)
cfbd7cf3
FD
97 _borrow_default_clock_snapshot_ptr = staticmethod(
98 native_bt.message_packet_end_borrow_default_clock_snapshot_const
99 )
811644b8 100
81447b5b 101
188edac1 102class _StreamMessage(_Message, _MessageWithDefaultClockSnapshot):
81447b5b
PP
103 @property
104 def stream(self):
9ec609ec
SM
105 stream_ptr = self._borrow_stream_ptr(self._ptr)
106 assert stream_ptr
3fb99a22 107 return bt2_stream._Stream._create_from_ptr_and_get_ref(stream_ptr)
811644b8 108
81447b5b 109 @property
9ec609ec 110 def default_clock_snapshot(self):
188edac1
SM
111 self._check_has_default_clock_class(self.stream.cls.default_clock_class)
112
9ec609ec 113 status, snapshot_ptr = self._borrow_default_clock_snapshot_ptr(self._ptr)
81447b5b 114
188edac1 115 if status == native_bt.MESSAGE_STREAM_CLOCK_SNAPSHOT_STATE_UNKNOWN:
3fb99a22 116 return bt2_clock_snapshot._UnknownClockSnapshot()
81447b5b 117
3fb99a22 118 return bt2_clock_snapshot._ClockSnapshot._create_from_ptr_and_get_ref(
cfbd7cf3
FD
119 snapshot_ptr, self._ptr, self._get_ref, self._put_ref
120 )
811644b8 121
188edac1
SM
122 def _default_clock_snapshot(self, raw_value):
123 utils._check_uint64(raw_value)
124 self._set_default_clock_snapshot(self._ptr, raw_value)
811644b8 125
188edac1 126 _default_clock_snapshot = property(fset=_default_clock_snapshot)
811644b8 127
811644b8 128
188edac1
SM
129class _StreamBeginningMessage(_StreamMessage):
130 _borrow_stream_ptr = staticmethod(native_bt.message_stream_beginning_borrow_stream)
cfbd7cf3
FD
131 _borrow_default_clock_snapshot_ptr = staticmethod(
132 native_bt.message_stream_beginning_borrow_default_clock_snapshot_const
133 )
134 _set_default_clock_snapshot = staticmethod(
135 native_bt.message_stream_beginning_set_default_clock_snapshot
136 )
811644b8 137
188edac1
SM
138
139class _StreamEndMessage(_StreamMessage):
140 _borrow_stream_ptr = staticmethod(native_bt.message_stream_end_borrow_stream)
cfbd7cf3
FD
141 _borrow_default_clock_snapshot_ptr = staticmethod(
142 native_bt.message_stream_end_borrow_default_clock_snapshot_const
143 )
144 _set_default_clock_snapshot = staticmethod(
145 native_bt.message_stream_end_set_default_clock_snapshot
146 )
811644b8
PP
147
148
9ec609ec 149class _MessageIteratorInactivityMessage(_Message, _MessageWithDefaultClockSnapshot):
cfbd7cf3
FD
150 _borrow_default_clock_snapshot_ptr = staticmethod(
151 native_bt.message_message_iterator_inactivity_borrow_default_clock_snapshot_const
152 )
811644b8
PP
153
154 @property
9ec609ec 155 def default_clock_snapshot(self):
2e90378a
PP
156 # This kind of message always has a default clock class: no
157 # need to call self._check_has_default_clock_class() here.
9ec609ec
SM
158 return self._get_default_clock_snapshot(self._borrow_default_clock_snapshot_ptr)
159
811644b8 160
9ec609ec 161class _DiscardedMessage(_Message, _MessageWithDefaultClockSnapshot):
811644b8
PP
162 @property
163 def stream(self):
9ec609ec
SM
164 stream_ptr = self._borrow_stream_ptr(self._ptr)
165 assert stream_ptr
3fb99a22 166 return bt2_stream._Stream._create_from_ptr_and_get_ref(stream_ptr)
811644b8 167
811644b8
PP
168 @property
169 def count(self):
9ec609ec
SM
170 avail, count = self._get_count(self._ptr)
171 if avail is native_bt.PROPERTY_AVAILABILITY_AVAILABLE:
172 return count
811644b8 173
9ec609ec
SM
174 def _set_count(self, count):
175 utils._check_uint64(count)
176 self._set_count(self._ptr, count)
177
178 _count = property(fset=_set_count)
811644b8 179
2e90378a
PP
180 def _check_has_default_clock_snapshots(self):
181 if not self._has_default_clock_snapshots:
1153eccb 182 raise ValueError(
cfbd7cf3
FD
183 'cannot get default clock snapshot: such a message has no clock snapshots for this stream class'
184 )
2e90378a 185
811644b8 186 @property
9ec609ec 187 def beginning_default_clock_snapshot(self):
2e90378a 188 self._check_has_default_clock_snapshots()
cfbd7cf3
FD
189 return self._get_default_clock_snapshot(
190 self._borrow_beginning_clock_snapshot_ptr
191 )
811644b8 192
9ec609ec
SM
193 @property
194 def end_default_clock_snapshot(self):
2e90378a 195 self._check_has_default_clock_snapshots()
9ec609ec 196 return self._get_default_clock_snapshot(self._borrow_end_clock_snapshot_ptr)
811644b8 197
811644b8 198
9ec609ec 199class _DiscardedEventsMessage(_DiscardedMessage):
cfbd7cf3
FD
200 _borrow_stream_ptr = staticmethod(
201 native_bt.message_discarded_events_borrow_stream_const
202 )
9ec609ec
SM
203 _get_count = staticmethod(native_bt.message_discarded_events_get_count)
204 _set_count = staticmethod(native_bt.message_discarded_events_set_count)
cfbd7cf3
FD
205 _borrow_beginning_clock_snapshot_ptr = staticmethod(
206 native_bt.message_discarded_events_borrow_beginning_default_clock_snapshot_const
207 )
208 _borrow_end_clock_snapshot_ptr = staticmethod(
209 native_bt.message_discarded_events_borrow_end_default_clock_snapshot_const
210 )
811644b8 211
2e90378a
PP
212 @property
213 def _has_default_clock_snapshots(self):
e8ac1aae 214 return self.stream.cls.discarded_events_have_default_clock_snapshots
2e90378a 215
811644b8 216
9ec609ec 217class _DiscardedPacketsMessage(_DiscardedMessage):
cfbd7cf3
FD
218 _borrow_stream_ptr = staticmethod(
219 native_bt.message_discarded_packets_borrow_stream_const
220 )
9ec609ec
SM
221 _get_count = staticmethod(native_bt.message_discarded_packets_get_count)
222 _set_count = staticmethod(native_bt.message_discarded_packets_set_count)
cfbd7cf3
FD
223 _borrow_beginning_clock_snapshot_ptr = staticmethod(
224 native_bt.message_discarded_packets_borrow_beginning_default_clock_snapshot_const
225 )
226 _borrow_end_clock_snapshot_ptr = staticmethod(
227 native_bt.message_discarded_packets_borrow_end_default_clock_snapshot_const
228 )
81447b5b 229
2e90378a
PP
230 @property
231 def _has_default_clock_snapshots(self):
e8ac1aae 232 return self.stream.cls.discarded_packets_have_default_clock_snapshots
2e90378a 233
81447b5b 234
5602ef81 235_MESSAGE_TYPE_TO_CLS = {
2ae9f48c 236 native_bt.MESSAGE_TYPE_EVENT: _EventMessage,
9ec609ec 237 native_bt.MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: _MessageIteratorInactivityMessage,
2ae9f48c 238 native_bt.MESSAGE_TYPE_STREAM_BEGINNING: _StreamBeginningMessage,
5f25509b 239 native_bt.MESSAGE_TYPE_STREAM_END: _StreamEndMessage,
9ec609ec
SM
240 native_bt.MESSAGE_TYPE_PACKET_BEGINNING: _PacketBeginningMessage,
241 native_bt.MESSAGE_TYPE_PACKET_END: _PacketEndMessage,
5602ef81 242 native_bt.MESSAGE_TYPE_DISCARDED_EVENTS: _DiscardedEventsMessage,
9ec609ec 243 native_bt.MESSAGE_TYPE_DISCARDED_PACKETS: _DiscardedPacketsMessage,
81447b5b 244}
This page took 0.059601 seconds and 4 git commands to generate.