| 1 | # SPDX-License-Identifier: MIT |
| 2 | # |
| 3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> |
| 4 | |
| 5 | from bt2 import native_bt, object, utils |
| 6 | from bt2 import value as bt2_value |
| 7 | import uuid as uuidp |
| 8 | |
| 9 | |
| 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 | |
| 33 | class _ClockClassConst(object._SharedObject): |
| 34 | _get_ref = staticmethod(native_bt.clock_class_get_ref) |
| 35 | _put_ref = staticmethod(native_bt.clock_class_put_ref) |
| 36 | _create_value_from_ptr_and_get_ref = staticmethod( |
| 37 | bt2_value._create_from_const_ptr_and_get_ref |
| 38 | ) |
| 39 | _borrow_user_attributes_ptr = staticmethod( |
| 40 | native_bt.clock_class_borrow_user_attributes_const |
| 41 | ) |
| 42 | |
| 43 | @property |
| 44 | def user_attributes(self): |
| 45 | ptr = self._borrow_user_attributes_ptr(self._ptr) |
| 46 | assert ptr is not None |
| 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 | ) |
| 96 | _borrow_user_attributes_ptr = staticmethod( |
| 97 | native_bt.clock_class_borrow_user_attributes |
| 98 | ) |
| 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 | |
| 107 | def _name(self, name): |
| 108 | utils._check_str(name) |
| 109 | status = native_bt.clock_class_set_name(self._ptr, name) |
| 110 | utils._handle_func_status(status, "cannot set clock class object's name") |
| 111 | |
| 112 | _name = property(fset=_name) |
| 113 | |
| 114 | def _description(self, description): |
| 115 | utils._check_str(description) |
| 116 | status = native_bt.clock_class_set_description(self._ptr, description) |
| 117 | utils._handle_func_status(status, "cannot set clock class object's description") |
| 118 | |
| 119 | _description = property(fset=_description) |
| 120 | |
| 121 | def _frequency(self, frequency): |
| 122 | utils._check_uint64(frequency) |
| 123 | native_bt.clock_class_set_frequency(self._ptr, frequency) |
| 124 | |
| 125 | _frequency = property(fset=_frequency) |
| 126 | |
| 127 | def _precision(self, precision): |
| 128 | utils._check_uint64(precision) |
| 129 | native_bt.clock_class_set_precision(self._ptr, precision) |
| 130 | |
| 131 | _precision = property(fset=_precision) |
| 132 | |
| 133 | def _offset(self, offset): |
| 134 | utils._check_type(offset, ClockClassOffset) |
| 135 | native_bt.clock_class_set_offset(self._ptr, offset.seconds, offset.cycles) |
| 136 | |
| 137 | _offset = property(fset=_offset) |
| 138 | |
| 139 | def _origin_is_unix_epoch(self, origin_is_unix_epoch): |
| 140 | utils._check_bool(origin_is_unix_epoch) |
| 141 | native_bt.clock_class_set_origin_is_unix_epoch( |
| 142 | self._ptr, int(origin_is_unix_epoch) |
| 143 | ) |
| 144 | |
| 145 | _origin_is_unix_epoch = property(fset=_origin_is_unix_epoch) |
| 146 | |
| 147 | def _uuid(self, uuid): |
| 148 | utils._check_type(uuid, uuidp.UUID) |
| 149 | native_bt.clock_class_set_uuid(self._ptr, uuid.bytes) |
| 150 | |
| 151 | _uuid = property(fset=_uuid) |