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