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