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