bt2: Sync native_bt_field_class.i with field-class-const.h
[babeltrace.git] / bindings / python / bt2 / bt2 / notification.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
811644b8 24import bt2.clock_class_priority_map
15012c4b 25import bt2.clock_value
71fd6f52 26import collections
81447b5b
PP
27import bt2.packet
28import bt2.stream
29import bt2.event
811644b8 30import copy
81447b5b
PP
31import bt2
32
33
34def _create_from_ptr(ptr):
35 notif_type = native_bt.notification_get_type(ptr)
36 cls = None
37
38 if notif_type not in _NOTIF_TYPE_TO_CLS:
39 raise bt2.Error('unknown notification type: {}'.format(notif_type))
40
41 return _NOTIF_TYPE_TO_CLS[notif_type]._create_from_ptr(ptr)
42
43
dc43190b
PP
44def _notif_types_from_notif_classes(notification_types):
45 if notification_types is None:
46 notif_types = None
47 else:
48 for notif_cls in notification_types:
49 if notif_cls not in _NOTIF_TYPE_TO_CLS.values():
50 raise ValueError("'{}' is not a notification class".format(notif_cls))
51
52 notif_types = [notif_cls._TYPE for notif_cls in notification_types]
53
54 return notif_types
55
56
81447b5b
PP
57class _Notification(object._Object):
58 pass
59
60
811644b8
PP
61class _CopyableNotification(_Notification):
62 def __copy__(self):
63 return self._copy(lambda obj: obj)
64
65 def __deepcopy__(self, memo):
66 cpy = self._copy(copy.deepcopy)
67 memo[id(self)] = cpy
68 return cpy
69
70
71class EventNotification(_CopyableNotification):
88f3724d 72 _TYPE = native_bt.MESSAGE_TYPE_EVENT
811644b8
PP
73
74 def __init__(self, event, cc_prio_map=None):
81447b5b 75 utils._check_type(event, bt2.event._Event)
811644b8
PP
76
77 if cc_prio_map is not None:
78 utils._check_type(cc_prio_map, bt2.clock_class_priority_map.ClockClassPriorityMap)
79 cc_prio_map_ptr = cc_prio_map._ptr
80 else:
81 cc_prio_map_ptr = None
82
83 ptr = native_bt.notification_event_create(event._ptr, cc_prio_map_ptr)
81447b5b
PP
84
85 if ptr is None:
811644b8 86 raise bt2.CreationError('cannot create event notification object')
81447b5b
PP
87
88 super().__init__(ptr)
89
90 @property
91 def event(self):
92 event_ptr = native_bt.notification_event_get_event(self._ptr)
811644b8 93 assert(event_ptr)
81447b5b
PP
94 return bt2.event._create_from_ptr(event_ptr)
95
811644b8
PP
96 @property
97 def clock_class_priority_map(self):
98 cc_prio_map_ptr = native_bt.notification_event_get_clock_class_priority_map(self._ptr)
99 assert(cc_prio_map_ptr)
100 return bt2.clock_class_priority_map.ClockClassPriorityMap._create_from_ptr(cc_prio_map_ptr)
101
102 def __eq__(self, other):
103 if type(other) is not type(self):
104 return False
105
106 if self.addr == other.addr:
107 return True
108
109 self_props = (
110 self.event,
111 self.clock_class_priority_map,
112 )
113 other_props = (
114 other.event,
115 other.clock_class_priority_map,
116 )
117 return self_props == other_props
118
119 def _copy(self, copy_func):
120 # We can always use references here because those properties are
121 # frozen anyway if they are part of a notification. Since the
122 # user cannot modify them after copying the notification, it's
123 # useless to copy/deep-copy them.
124 return EventNotification(self.event, self.clock_class_priority_map)
125
126
127class PacketBeginningNotification(_CopyableNotification):
88f3724d 128 _TYPE = native_bt.MESSAGE_TYPE_PACKET_BEGINNING
81447b5b 129
81447b5b
PP
130 def __init__(self, packet):
131 utils._check_type(packet, bt2.packet._Packet)
132 ptr = native_bt.notification_packet_begin_create(packet._ptr)
133
134 if ptr is None:
811644b8 135 raise bt2.CreationError('cannot create packet beginning notification object')
81447b5b
PP
136
137 super().__init__(ptr)
138
139 @property
140 def packet(self):
141 packet_ptr = native_bt.notification_packet_begin_get_packet(self._ptr)
811644b8 142 assert(packet_ptr)
81447b5b
PP
143 return bt2.packet._Packet._create_from_ptr(packet_ptr)
144
811644b8
PP
145 def __eq__(self, other):
146 if type(other) is not type(self):
147 return False
148
149 if self.addr == other.addr:
150 return True
151
152 return self.packet == other.packet
153
154 def _copy(self, copy_func):
155 # We can always use references here because those properties are
156 # frozen anyway if they are part of a notification. Since the
157 # user cannot modify them after copying the notification, it's
158 # useless to copy/deep-copy them.
159 return PacketBeginningNotification(self.packet)
160
161
162class PacketEndNotification(_CopyableNotification):
88f3724d 163 _TYPE = native_bt.MESSAGE_TYPE_PACKET_END
81447b5b 164
81447b5b
PP
165 def __init__(self, packet):
166 utils._check_type(packet, bt2.packet._Packet)
167 ptr = native_bt.notification_packet_end_create(packet._ptr)
168
169 if ptr is None:
811644b8 170 raise bt2.CreationError('cannot create packet end notification object')
81447b5b
PP
171
172 super().__init__(ptr)
173
174 @property
175 def packet(self):
176 packet_ptr = native_bt.notification_packet_end_get_packet(self._ptr)
811644b8 177 assert(packet_ptr)
81447b5b
PP
178 return bt2.packet._Packet._create_from_ptr(packet_ptr)
179
811644b8
PP
180 def __eq__(self, other):
181 if type(other) is not type(self):
182 return False
183
184 if self.addr == other.addr:
185 return True
186
187 return self.packet == other.packet
188
189 def _copy(self, copy_func):
190 # We can always use references here because those properties are
191 # frozen anyway if they are part of a notification. Since the
192 # user cannot modify them after copying the notification, it's
193 # useless to copy/deep-copy them.
194 return PacketEndNotification(self.packet)
195
196
197class StreamBeginningNotification(_CopyableNotification):
88f3724d 198 _TYPE = native_bt.MESSAGE_TYPE_STREAM_BEGINNING
81447b5b 199
81447b5b
PP
200 def __init__(self, stream):
201 utils._check_type(stream, bt2.stream._Stream)
811644b8 202 ptr = native_bt.notification_stream_begin_create(stream._ptr)
81447b5b
PP
203
204 if ptr is None:
811644b8 205 raise bt2.CreationError('cannot create stream beginning notification object')
81447b5b
PP
206
207 super().__init__(ptr)
208
209 @property
210 def stream(self):
811644b8
PP
211 stream_ptr = native_bt.notification_stream_begin_get_stream(self._ptr)
212 assert(stream_ptr)
81447b5b
PP
213 return bt2.stream._create_from_ptr(stream_ptr)
214
811644b8
PP
215 def __eq__(self, other):
216 if type(other) is not type(self):
217 return False
218
219 if self.addr == other.addr:
220 return True
221
222 return self.stream == other.stream
223
224 def _copy(self, copy_func):
225 # We can always use references here because those properties are
226 # frozen anyway if they are part of a notification. Since the
227 # user cannot modify them after copying the notification, it's
228 # useless to copy/deep-copy them.
229 return StreamBeginningNotification(self.stream)
230
231
232class StreamEndNotification(_CopyableNotification):
88f3724d 233 _TYPE = native_bt.MESSAGE_TYPE_STREAM_END
81447b5b 234
811644b8
PP
235 def __init__(self, stream):
236 utils._check_type(stream, bt2.stream._Stream)
237 ptr = native_bt.notification_stream_end_create(stream._ptr)
81447b5b
PP
238
239 if ptr is None:
811644b8 240 raise bt2.CreationError('cannot create stream end notification object')
81447b5b
PP
241
242 super().__init__(ptr)
243
244 @property
811644b8
PP
245 def stream(self):
246 stream_ptr = native_bt.notification_stream_end_get_stream(self._ptr)
247 assert(stream_ptr)
248 return bt2.stream._create_from_ptr(stream_ptr)
249
250 def __eq__(self, other):
251 if type(other) is not type(self):
252 return False
253
254 if self.addr == other.addr:
255 return True
256
257 return self.stream == other.stream
258
259 def _copy(self, copy_func):
260 # We can always use references here because those properties are
261 # frozen anyway if they are part of a notification. Since the
262 # user cannot modify them after copying the notification, it's
263 # useless to copy/deep-copy them.
264 return StreamEndNotification(self.stream)
265
266
71fd6f52
PP
267class _InactivityNotificationClockValuesIterator(collections.abc.Iterator):
268 def __init__(self, notif_clock_values):
269 self._notif_clock_values = notif_clock_values
270 self._clock_classes = list(notif_clock_values._notif.clock_class_priority_map)
271 self._at = 0
272
273 def __next__(self):
274 if self._at == len(self._clock_classes):
275 raise StopIteration
276
277 self._at += 1
278 return self._clock_classes[at]
279
280
281class _InactivityNotificationClockValues(collections.abc.Mapping):
282 def __init__(self, notif):
283 self._notif = notif
284
285 def __getitem__(self, clock_class):
286 utils._check_type(clock_class, bt2.ClockClass)
287 clock_value_ptr = native_bt.notification_inactivity_get_clock_value(self._notif._ptr,
288 clock_class._ptr)
289
290 if clock_value_ptr is None:
291 return
292
293 clock_value = bt2.clock_value._create_clock_value_from_ptr(clock_value_ptr)
294 return clock_value
295
296 def add(self, clock_value):
297 utils._check_type(clock_value, bt2.clock_value._ClockValue)
298 ret = native_bt.notification_inactivity_set_clock_value(self._notif._ptr,
299 clock_value._ptr)
300 utils._handle_ret(ret, "cannot set inactivity notification object's clock value")
301
302 def __len__(self):
303 return len(self._notif.clock_class_priority_map)
304
305 def __iter__(self):
306 return _InactivityNotificationClockValuesIterator(self)
307
308
811644b8 309class InactivityNotification(_CopyableNotification):
88f3724d 310 _TYPE = native_bt.MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY
81447b5b 311
811644b8
PP
312 def __init__(self, cc_prio_map=None):
313 if cc_prio_map is not None:
314 utils._check_type(cc_prio_map, bt2.clock_class_priority_map.ClockClassPriorityMap)
315 cc_prio_map_ptr = cc_prio_map._ptr
316 else:
317 cc_prio_map_ptr = None
81447b5b 318
811644b8 319 ptr = native_bt.notification_inactivity_create(cc_prio_map_ptr)
81447b5b
PP
320
321 if ptr is None:
811644b8 322 raise bt2.CreationError('cannot create inactivity notification object')
81447b5b
PP
323
324 super().__init__(ptr)
325
326 @property
811644b8
PP
327 def clock_class_priority_map(self):
328 cc_prio_map_ptr = native_bt.notification_inactivity_get_clock_class_priority_map(self._ptr)
329 assert(cc_prio_map_ptr)
330 return bt2.clock_class_priority_map.ClockClassPriorityMap._create_from_ptr(cc_prio_map_ptr)
331
71fd6f52
PP
332 @property
333 def clock_values(self):
334 return _InactivityNotificationClockValues(self)
811644b8
PP
335
336 def _get_clock_values(self):
337 clock_values = {}
338
71fd6f52 339 for clock_class, clock_value in self.clock_values.items():
811644b8
PP
340 if clock_value is None:
341 continue
342
343 clock_values[clock_class] = clock_value
344
345 return clock_values
346
347 def __eq__(self, other):
348 if type(other) is not type(self):
349 return False
350
351 if self.addr == other.addr:
352 return True
353
354 self_props = (
355 self.clock_class_priority_map,
356 self._get_clock_values(),
357 )
358 other_props = (
359 other.clock_class_priority_map,
360 other._get_clock_values(),
361 )
362 return self_props == other_props
363
364 def __copy__(self):
365 cpy = InactivityNotification(self.clock_class_priority_map)
366
71fd6f52 367 for clock_class, clock_value in self.clock_values.items():
811644b8
PP
368 if clock_value is None:
369 continue
370
71fd6f52 371 cpy.clock_values.add(clock_value)
811644b8
PP
372
373 return cpy
374
375 def __deepcopy__(self, memo):
376 cc_prio_map_cpy = copy.deepcopy(self.clock_class_priority_map)
377 cpy = InactivityNotification(cc_prio_map_cpy)
378
379 # copy clock values
380 for orig_clock_class in self.clock_class_priority_map:
381 orig_clock_value = self.clock_value(orig_clock_class)
382
383 if orig_clock_value is None:
384 continue
385
386 # find equivalent, copied clock class in CC priority map copy
387 for cpy_clock_class in cc_prio_map_cpy:
388 if cpy_clock_class == orig_clock_class:
389 break
390
391 # create copy of clock value from copied clock class
392 clock_value_cpy = cpy_clock_class(orig_clock_value.cycles)
393
394 # set copied clock value in notification copy
71fd6f52 395 cpy.clock_values.add(clock_value_cpy)
811644b8
PP
396
397 memo[id(self)] = cpy
398 return cpy
399
400
401class _DiscardedElementsNotification(_Notification):
402 def __eq__(self, other):
403 if type(other) is not type(self):
404 return False
405
406 if self.addr == other.addr:
407 return True
408
409 self_props = (
410 self.count,
411 self.stream,
412 self.beginning_clock_value,
413 self.end_clock_value,
414 )
415 other_props = (
416 other.count,
417 other.stream,
418 other.beginning_clock_value,
419 other.end_clock_value,
420 )
421 return self_props == other_props
422
423
424class _DiscardedPacketsNotification(_DiscardedElementsNotification):
88f3724d 425 _TYPE = native_bt.MESSAGE_TYPE_DISCARDED_PACKETS
811644b8
PP
426
427 @property
428 def count(self):
429 count = native_bt.notification_discarded_packets_get_count(self._ptr)
430 assert(count >= 0)
431 return count
432
433 @property
434 def stream(self):
435 stream_ptr = native_bt.notification_discarded_packets_get_stream(self._ptr)
436 assert(stream_ptr)
437 return bt2.stream._create_from_ptr(stream_ptr)
438
439 @property
440 def beginning_clock_value(self):
441 clock_value_ptr = native_bt.notification_discarded_packets_get_begin_clock_value(self._ptr)
442
443 if clock_value_ptr is None:
444 return
445
15012c4b 446 clock_value = bt2.clock_value._create_clock_value_from_ptr(clock_value_ptr)
811644b8
PP
447 return clock_value
448
449 @property
450 def end_clock_value(self):
451 clock_value_ptr = native_bt.notification_discarded_packets_get_end_clock_value(self._ptr)
452
453 if clock_value_ptr is None:
454 return
455
15012c4b 456 clock_value = bt2.clock_value._create_clock_value_from_ptr(clock_value_ptr)
811644b8
PP
457 return clock_value
458
459
460class _DiscardedEventsNotification(_DiscardedElementsNotification):
88f3724d 461 _TYPE = native_bt.MESSAGE_TYPE_DISCARDED_EVENTS
811644b8
PP
462
463 @property
464 def count(self):
465 count = native_bt.notification_discarded_events_get_count(self._ptr)
466 assert(count >= 0)
467 return count
468
469 @property
470 def stream(self):
471 stream_ptr = native_bt.notification_discarded_events_get_stream(self._ptr)
472 assert(stream_ptr)
473 return bt2.stream._create_from_ptr(stream_ptr)
474
475 @property
476 def beginning_clock_value(self):
477 clock_value_ptr = native_bt.notification_discarded_events_get_begin_clock_value(self._ptr)
478
479 if clock_value_ptr is None:
480 return
481
15012c4b 482 clock_value = bt2.clock_value._create_clock_value_from_ptr(clock_value_ptr)
811644b8
PP
483 return clock_value
484
485 @property
486 def end_clock_value(self):
487 clock_value_ptr = native_bt.notification_discarded_events_get_end_clock_value(self._ptr)
488
489 if clock_value_ptr is None:
490 return
491
15012c4b 492 clock_value = bt2.clock_value._create_clock_value_from_ptr(clock_value_ptr)
811644b8 493 return clock_value
81447b5b
PP
494
495
496_NOTIF_TYPE_TO_CLS = {
88f3724d
SM
497 native_bt.MESSAGE_TYPE_EVENT: EventNotification,
498 native_bt.MESSAGE_TYPE_PACKET_BEGINNING: PacketBeginningNotification,
499 native_bt.MESSAGE_TYPE_PACKET_END: PacketEndNotification,
500 native_bt.MESSAGE_TYPE_STREAM_BEGINNING: StreamBeginningNotification,
501 native_bt.MESSAGE_TYPE_STREAM_END: StreamEndNotification,
502 native_bt.MESSAGE_TYPE_MESSAGE_ITERATOR_INACTIVITY: InactivityNotification,
503 native_bt.MESSAGE_TYPE_DISCARDED_PACKETS: _DiscardedPacketsNotification,
504 native_bt.MESSAGE_TYPE_DISCARDED_EVENTS: _DiscardedEventsNotification,
81447b5b 505}
This page took 0.055337 seconds and 4 git commands to generate.