Commit | Line | Data |
---|---|---|
0235b0db | 1 | # SPDX-License-Identifier: MIT |
81447b5b | 2 | # |
811644b8 | 3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> |
81447b5b | 4 | |
5995b304 SM |
5 | import uuid as uuidp |
6 | ||
e5914347 | 7 | from bt2 import utils as bt2_utils |
5783664e | 8 | from bt2 import value as bt2_value |
5995b304 SM |
9 | from bt2 import object as bt2_object |
10 | from bt2 import native_bt | |
81447b5b | 11 | |
0b03f63e | 12 | |
81447b5b PP |
13 | class ClockClassOffset: |
14 | def __init__(self, seconds=0, cycles=0): | |
e5914347 SM |
15 | bt2_utils._check_int64(seconds) |
16 | bt2_utils._check_int64(cycles) | |
81447b5b PP |
17 | self._seconds = seconds |
18 | self._cycles = cycles | |
19 | ||
20 | @property | |
21 | def seconds(self): | |
22 | return self._seconds | |
23 | ||
24 | @property | |
25 | def cycles(self): | |
26 | return self._cycles | |
27 | ||
28 | def __eq__(self, other): | |
29 | if not isinstance(other, self.__class__): | |
30 | # not comparing apples to apples | |
31 | return False | |
32 | ||
33 | return (self.seconds, self.cycles) == (other.seconds, other.cycles) | |
34 | ||
35 | ||
e5914347 | 36 | class _ClockClassConst(bt2_object._SharedObject): |
9dee90bd SM |
37 | @staticmethod |
38 | def _get_ref(ptr): | |
39 | native_bt.clock_class_get_ref(ptr) | |
40 | ||
41 | @staticmethod | |
42 | def _put_ref(ptr): | |
43 | native_bt.clock_class_put_ref(ptr) | |
44 | ||
eddea575 FD |
45 | _create_value_from_ptr_and_get_ref = staticmethod( |
46 | bt2_value._create_from_const_ptr_and_get_ref | |
47 | ) | |
2550c437 SM |
48 | _borrow_user_attributes_ptr = staticmethod( |
49 | native_bt.clock_class_borrow_user_attributes_const | |
50 | ) | |
3cdfbaea | 51 | |
5783664e PP |
52 | @property |
53 | def user_attributes(self): | |
2550c437 | 54 | ptr = self._borrow_user_attributes_ptr(self._ptr) |
5783664e | 55 | assert ptr is not None |
eddea575 FD |
56 | return self._create_value_from_ptr_and_get_ref(ptr) |
57 | ||
58 | @property | |
59 | def name(self): | |
60 | return native_bt.clock_class_get_name(self._ptr) | |
61 | ||
62 | @property | |
63 | def description(self): | |
64 | return native_bt.clock_class_get_description(self._ptr) | |
65 | ||
66 | @property | |
67 | def frequency(self): | |
68 | return native_bt.clock_class_get_frequency(self._ptr) | |
69 | ||
70 | @property | |
71 | def precision(self): | |
72 | precision = native_bt.clock_class_get_precision(self._ptr) | |
73 | return precision | |
74 | ||
75 | @property | |
76 | def offset(self): | |
77 | offset_s, offset_cycles = native_bt.clock_class_get_offset(self._ptr) | |
78 | return ClockClassOffset(offset_s, offset_cycles) | |
79 | ||
80 | @property | |
81 | def origin_is_unix_epoch(self): | |
82 | return native_bt.clock_class_origin_is_unix_epoch(self._ptr) | |
83 | ||
84 | @property | |
85 | def uuid(self): | |
86 | uuid_bytes = native_bt.clock_class_get_uuid(self._ptr) | |
87 | ||
88 | if uuid_bytes is None: | |
89 | return | |
90 | ||
91 | return uuidp.UUID(bytes=uuid_bytes) | |
92 | ||
93 | def cycles_to_ns_from_origin(self, cycles): | |
e5914347 | 94 | bt2_utils._check_uint64(cycles) |
eddea575 FD |
95 | status, ns = native_bt.clock_class_cycles_to_ns_from_origin(self._ptr, cycles) |
96 | error_msg = "cannot convert clock value to nanoseconds from origin for given clock class" | |
e5914347 | 97 | bt2_utils._handle_func_status(status, error_msg) |
eddea575 FD |
98 | return ns |
99 | ||
100 | ||
101 | class _ClockClass(_ClockClassConst): | |
102 | _create_value_from_ptr_and_get_ref = staticmethod( | |
103 | bt2_value._create_from_ptr_and_get_ref | |
104 | ) | |
2550c437 SM |
105 | _borrow_user_attributes_ptr = staticmethod( |
106 | native_bt.clock_class_borrow_user_attributes | |
107 | ) | |
5783664e PP |
108 | |
109 | def _user_attributes(self, user_attributes): | |
110 | value = bt2_value.create_value(user_attributes) | |
e5914347 | 111 | bt2_utils._check_type(value, bt2_value.MapValue) |
5783664e PP |
112 | native_bt.clock_class_set_user_attributes(self._ptr, value._ptr) |
113 | ||
114 | _user_attributes = property(fset=_user_attributes) | |
115 | ||
be7bbff9 | 116 | def _name(self, name): |
e5914347 | 117 | bt2_utils._check_str(name) |
d24d5663 | 118 | status = native_bt.clock_class_set_name(self._ptr, name) |
e5914347 | 119 | bt2_utils._handle_func_status(status, "cannot set clock class object's name") |
81447b5b | 120 | |
be7bbff9 SM |
121 | _name = property(fset=_name) |
122 | ||
be7bbff9 | 123 | def _description(self, description): |
e5914347 | 124 | bt2_utils._check_str(description) |
d24d5663 | 125 | status = native_bt.clock_class_set_description(self._ptr, description) |
e5914347 SM |
126 | bt2_utils._handle_func_status( |
127 | status, "cannot set clock class object's description" | |
128 | ) | |
81447b5b | 129 | |
be7bbff9 SM |
130 | _description = property(fset=_description) |
131 | ||
be7bbff9 | 132 | def _frequency(self, frequency): |
e5914347 | 133 | bt2_utils._check_uint64(frequency) |
be7bbff9 SM |
134 | native_bt.clock_class_set_frequency(self._ptr, frequency) |
135 | ||
136 | _frequency = property(fset=_frequency) | |
81447b5b | 137 | |
be7bbff9 | 138 | def _precision(self, precision): |
e5914347 | 139 | bt2_utils._check_uint64(precision) |
be7bbff9 SM |
140 | native_bt.clock_class_set_precision(self._ptr, precision) |
141 | ||
142 | _precision = property(fset=_precision) | |
81447b5b | 143 | |
be7bbff9 | 144 | def _offset(self, offset): |
e5914347 | 145 | bt2_utils._check_type(offset, ClockClassOffset) |
be7bbff9 SM |
146 | native_bt.clock_class_set_offset(self._ptr, offset.seconds, offset.cycles) |
147 | ||
148 | _offset = property(fset=_offset) | |
81447b5b | 149 | |
be7bbff9 | 150 | def _origin_is_unix_epoch(self, origin_is_unix_epoch): |
e5914347 | 151 | bt2_utils._check_bool(origin_is_unix_epoch) |
cfbd7cf3 FD |
152 | native_bt.clock_class_set_origin_is_unix_epoch( |
153 | self._ptr, int(origin_is_unix_epoch) | |
154 | ) | |
be7bbff9 SM |
155 | |
156 | _origin_is_unix_epoch = property(fset=_origin_is_unix_epoch) | |
81447b5b | 157 | |
be7bbff9 | 158 | def _uuid(self, uuid): |
e5914347 | 159 | bt2_utils._check_type(uuid, uuidp.UUID) |
be7bbff9 SM |
160 | native_bt.clock_class_set_uuid(self._ptr, uuid.bytes) |
161 | ||
162 | _uuid = property(fset=_uuid) |