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 | |
f192fc47 | 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): | |
fa4c33e3 | 32 | msg_type = native_bt.message_get_type(ptr) |
81447b5b | 33 | |
fa4c33e3 SM |
34 | if msg_type not in _MESSAGE_TYPE_TO_CLS: |
35 | raise bt2.Error('unknown message type: {}'.format(msg_type)) | |
81447b5b | 36 | |
fa4c33e3 | 37 | return _MESSAGE_TYPE_TO_CLS[msg_type]._create_from_ptr(ptr) |
81447b5b PP |
38 | |
39 | ||
c3044a97 | 40 | class _Message(object._SharedObject): |
27d97a3f SM |
41 | _get_ref = staticmethod(native_bt.message_get_ref) |
42 | _put_ref = staticmethod(native_bt.message_put_ref) | |
81447b5b PP |
43 | |
44 | ||
0010c8b0 SM |
45 | class _MessageWithDefaultClockSnapshot: |
46 | def _get_default_clock_snapshot(self, borrow_clock_snapshot_ptr): | |
47 | if not self._has_default_clock_class: | |
48 | raise bt2.NoDefaultClockClass('cannot get default clock snapshot, stream class has no default clock class') | |
f6a5e476 | 49 | |
0010c8b0 | 50 | snapshot_ptr = borrow_clock_snapshot_ptr(self._ptr) |
f6a5e476 | 51 | |
0010c8b0 SM |
52 | return bt2.clock_snapshot._ClockSnapshot._create_from_ptr_and_get_ref( |
53 | snapshot_ptr, self._ptr, self._get_ref, self._put_ref) | |
f6a5e476 | 54 | |
0010c8b0 SM |
55 | |
56 | class _EventMessage(_Message, _MessageWithDefaultClockSnapshot): | |
57 | _borrow_default_clock_snapshot_ptr = staticmethod(native_bt.message_event_borrow_default_clock_snapshot_const) | |
58 | ||
59 | @property | |
60 | def _has_default_clock_class(self): | |
61 | return self.event.packet.stream.stream_class.default_clock_class is not None | |
62 | ||
63 | @property | |
64 | def default_clock_snapshot(self): | |
65 | return self._get_default_clock_snapshot(self._borrow_default_clock_snapshot_ptr) | |
f6a5e476 | 66 | |
27d97a3f SM |
67 | @property |
68 | def event(self): | |
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) | |
81447b5b | 73 | |
81447b5b | 74 | |
0010c8b0 | 75 | class _PacketMessage(_Message, _MessageWithDefaultClockSnapshot): |
f6a5e476 | 76 | @property |
0010c8b0 SM |
77 | def _has_default_clock_class(self): |
78 | return self.packet.stream.stream_class.default_clock_class is not None | |
81447b5b | 79 | |
81447b5b | 80 | @property |
0010c8b0 SM |
81 | def default_clock_snapshot(self): |
82 | return self._get_default_clock_snapshot(self._borrow_default_clock_snapshot_ptr) | |
81447b5b | 83 | |
81447b5b PP |
84 | @property |
85 | def packet(self): | |
0010c8b0 SM |
86 | packet_ptr = self._borrow_packet_ptr(self._ptr) |
87 | assert packet_ptr is not None | |
88 | return bt2.packet._Packet._create_from_ptr_and_get_ref(packet_ptr) | |
f6a5e476 | 89 | |
f6a5e476 | 90 | |
0010c8b0 SM |
91 | class _PacketBeginningMessage(_PacketMessage): |
92 | _borrow_packet_ptr = staticmethod(native_bt.message_packet_beginning_borrow_packet) | |
93 | _borrow_default_clock_snapshot_ptr = staticmethod(native_bt.message_packet_beginning_borrow_default_clock_snapshot_const) | |
f6a5e476 | 94 | |
f6a5e476 | 95 | |
0010c8b0 SM |
96 | class _PacketEndMessage(_PacketMessage): |
97 | _borrow_packet_ptr = staticmethod(native_bt.message_packet_end_borrow_packet) | |
98 | _borrow_default_clock_snapshot_ptr = staticmethod(native_bt.message_packet_end_borrow_default_clock_snapshot_const) | |
f6a5e476 | 99 | |
81447b5b | 100 | |
0010c8b0 | 101 | class _StreamMessage(_Message): |
81447b5b PP |
102 | @property |
103 | def stream(self): | |
0010c8b0 SM |
104 | stream_ptr = self._borrow_stream_ptr(self._ptr) |
105 | assert stream_ptr | |
106 | return bt2.stream._Stream._create_from_ptr_and_get_ref(stream_ptr) | |
f6a5e476 | 107 | |
f6a5e476 | 108 | |
0010c8b0 SM |
109 | class _StreamBeginningMessage(_StreamMessage): |
110 | _borrow_stream_ptr = staticmethod(native_bt.message_stream_beginning_borrow_stream) | |
f6a5e476 | 111 | |
f6a5e476 | 112 | |
0010c8b0 SM |
113 | class _StreamEndMessage(_StreamMessage): |
114 | _borrow_stream_ptr = staticmethod(native_bt.message_stream_end_borrow_stream) | |
f6a5e476 | 115 | |
81447b5b | 116 | |
0010c8b0 | 117 | class _StreamActivityMessage(_Message): |
81447b5b | 118 | @property |
0010c8b0 SM |
119 | def default_clock_snapshot(self): |
120 | if self.stream.stream_class.default_clock_class is None: | |
121 | raise bt2.NoDefaultClockClass('cannot get default clock snapshot, stream class has no default clock class') | |
8ae92ae2 | 122 | |
0010c8b0 | 123 | status, snapshot_ptr = self._borrow_default_clock_snapshot_ptr(self._ptr) |
81447b5b | 124 | |
0010c8b0 SM |
125 | if status == native_bt.MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_KNOWN: |
126 | snapshot_type = bt2.clock_snapshot._ClockSnapshot | |
127 | elif status == native_bt.MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_UNKNOWN: | |
128 | snapshot_type = bt2.clock_snapshot._UnknownClockSnapshot | |
129 | elif status == native_bt.MESSAGE_STREAM_ACTIVITY_CLOCK_SNAPSHOT_STATE_INFINITE: | |
130 | snapshot_type = bt2.clock_snapshot._InfiniteClockSnapshot | |
f6a5e476 | 131 | else: |
0010c8b0 | 132 | raise bt2.Error('cannot borrow default clock snapshot from message') |
81447b5b | 133 | |
0010c8b0 | 134 | assert snapshot_ptr is not None |
81447b5b | 135 | |
0010c8b0 SM |
136 | return snapshot_type._create_from_ptr_and_get_ref( |
137 | snapshot_ptr, self._ptr, self._get_ref, self._put_ref) | |
81447b5b | 138 | |
0010c8b0 SM |
139 | def _default_clock_snapshot(self, value): |
140 | self._set_default_clock_snapshot_ptr(self._ptr, value) | |
81447b5b | 141 | |
0010c8b0 | 142 | _default_clock_snapshot = property(fset=_default_clock_snapshot) |
f6a5e476 | 143 | |
8ae92ae2 | 144 | @property |
0010c8b0 SM |
145 | def stream(self): |
146 | stream_ptr = self._borrow_stream_ptr(self._ptr) | |
147 | assert stream_ptr | |
148 | return bt2.stream._Stream._create_from_ptr_and_get_ref(stream_ptr) | |
f6a5e476 PP |
149 | |
150 | ||
0010c8b0 SM |
151 | class _StreamActivityBeginningMessage(_StreamActivityMessage): |
152 | _borrow_default_clock_snapshot_ptr = staticmethod(native_bt.message_stream_activity_beginning_borrow_default_clock_snapshot_const) | |
153 | _set_default_clock_snapshot_ptr = staticmethod(native_bt.message_stream_activity_beginning_set_default_clock_snapshot) | |
154 | _borrow_stream_ptr = staticmethod(native_bt.message_stream_activity_beginning_borrow_stream) | |
f6a5e476 | 155 | |
f6a5e476 | 156 | |
0010c8b0 SM |
157 | class _StreamActivityEndMessage(_StreamActivityMessage): |
158 | _borrow_default_clock_snapshot_ptr = staticmethod(native_bt.message_stream_activity_end_borrow_default_clock_snapshot_const) | |
159 | _set_default_clock_snapshot_ptr = staticmethod(native_bt.message_stream_activity_end_set_default_clock_snapshot) | |
160 | _borrow_stream_ptr = staticmethod(native_bt.message_stream_activity_end_borrow_stream) | |
f6a5e476 PP |
161 | |
162 | ||
0010c8b0 SM |
163 | class _MessageIteratorInactivityMessage(_Message, _MessageWithDefaultClockSnapshot): |
164 | # This kind of message always has a default clock class. | |
165 | _has_default_clock_class = True | |
166 | _borrow_default_clock_snapshot_ptr = staticmethod(native_bt.message_message_iterator_inactivity_borrow_default_clock_snapshot_const) | |
f6a5e476 PP |
167 | |
168 | @property | |
0010c8b0 SM |
169 | def default_clock_snapshot(self): |
170 | return self._get_default_clock_snapshot(self._borrow_default_clock_snapshot_ptr) | |
171 | ||
f6a5e476 | 172 | |
0010c8b0 | 173 | class _DiscardedMessage(_Message, _MessageWithDefaultClockSnapshot): |
f6a5e476 PP |
174 | @property |
175 | def stream(self): | |
0010c8b0 SM |
176 | stream_ptr = self._borrow_stream_ptr(self._ptr) |
177 | assert stream_ptr | |
178 | return bt2.stream._Stream._create_from_ptr_and_get_ref(stream_ptr) | |
f6a5e476 PP |
179 | |
180 | @property | |
0010c8b0 SM |
181 | def _has_default_clock_class(self): |
182 | return self.default_clock_class is not None | |
f6a5e476 PP |
183 | |
184 | @property | |
0010c8b0 SM |
185 | def default_clock_class(self): |
186 | cc_ptr = self._borrow_clock_class_ptr(self._ptr) | |
187 | if cc_ptr is not None: | |
188 | return bt2.clock_class._ClockClass._create_from_ptr_and_get_ref(cc_ptr) | |
f6a5e476 PP |
189 | |
190 | @property | |
191 | def count(self): | |
0010c8b0 SM |
192 | avail, count = self._get_count(self._ptr) |
193 | if avail is native_bt.PROPERTY_AVAILABILITY_AVAILABLE: | |
194 | return count | |
f6a5e476 | 195 | |
0010c8b0 SM |
196 | def _set_count(self, count): |
197 | utils._check_uint64(count) | |
198 | self._set_count(self._ptr, count) | |
199 | ||
200 | _count = property(fset=_set_count) | |
f6a5e476 PP |
201 | |
202 | @property | |
0010c8b0 SM |
203 | def beginning_default_clock_snapshot(self): |
204 | return self._get_default_clock_snapshot(self._borrow_beginning_clock_snapshot_ptr) | |
f6a5e476 | 205 | |
0010c8b0 SM |
206 | @property |
207 | def end_default_clock_snapshot(self): | |
208 | return self._get_default_clock_snapshot(self._borrow_end_clock_snapshot_ptr) | |
f6a5e476 | 209 | |
f6a5e476 | 210 | |
0010c8b0 SM |
211 | class _DiscardedEventsMessage(_DiscardedMessage): |
212 | _borrow_stream_ptr = staticmethod(native_bt.message_discarded_events_borrow_stream_const) | |
213 | _get_count = staticmethod(native_bt.message_discarded_events_get_count) | |
214 | _set_count = staticmethod(native_bt.message_discarded_events_set_count) | |
215 | _borrow_clock_class_ptr = staticmethod(native_bt.message_discarded_events_borrow_stream_class_default_clock_class_const) | |
216 | _borrow_beginning_clock_snapshot_ptr = staticmethod(native_bt.message_discarded_events_borrow_default_beginning_clock_snapshot_const) | |
217 | _borrow_end_clock_snapshot_ptr = staticmethod(native_bt.message_discarded_events_borrow_default_end_clock_snapshot_const) | |
f6a5e476 | 218 | |
f6a5e476 | 219 | |
0010c8b0 SM |
220 | class _DiscardedPacketsMessage(_DiscardedMessage): |
221 | _borrow_stream_ptr = staticmethod(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_clock_class_ptr = staticmethod(native_bt.message_discarded_packets_borrow_stream_class_default_clock_class_const) | |
225 | _borrow_beginning_clock_snapshot_ptr = staticmethod(native_bt.message_discarded_packets_borrow_default_beginning_clock_snapshot_const) | |
226 | _borrow_end_clock_snapshot_ptr = staticmethod(native_bt.message_discarded_packets_borrow_default_end_clock_snapshot_const) | |
81447b5b PP |
227 | |
228 | ||
fa4c33e3 | 229 | _MESSAGE_TYPE_TO_CLS = { |
27d97a3f | 230 | native_bt.MESSAGE_TYPE_EVENT: _EventMessage, |
0010c8b0 | 231 | native_bt.MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: _MessageIteratorInactivityMessage, |
27d97a3f | 232 | native_bt.MESSAGE_TYPE_STREAM_BEGINNING: _StreamBeginningMessage, |
871a292a | 233 | native_bt.MESSAGE_TYPE_STREAM_END: _StreamEndMessage, |
0010c8b0 SM |
234 | native_bt.MESSAGE_TYPE_PACKET_BEGINNING: _PacketBeginningMessage, |
235 | native_bt.MESSAGE_TYPE_PACKET_END: _PacketEndMessage, | |
236 | native_bt.MESSAGE_TYPE_STREAM_ACTIVITY_BEGINNING: _StreamActivityBeginningMessage, | |
237 | native_bt.MESSAGE_TYPE_STREAM_ACTIVITY_END: _StreamActivityEndMessage, | |
fa4c33e3 | 238 | native_bt.MESSAGE_TYPE_DISCARDED_EVENTS: _DiscardedEventsMessage, |
0010c8b0 | 239 | native_bt.MESSAGE_TYPE_DISCARDED_PACKETS: _DiscardedPacketsMessage, |
81447b5b | 240 | } |