| 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 | |
| 23 | from bt2 import native_bt, object, utils |
| 24 | import bt2 |
| 25 | import uuid as uuidp |
| 26 | |
| 27 | |
| 28 | class ClockClassOffset: |
| 29 | def __init__(self, seconds=0, cycles=0): |
| 30 | utils._check_int64(seconds) |
| 31 | utils._check_int64(cycles) |
| 32 | self._seconds = seconds |
| 33 | self._cycles = cycles |
| 34 | |
| 35 | @property |
| 36 | def seconds(self): |
| 37 | return self._seconds |
| 38 | |
| 39 | @property |
| 40 | def cycles(self): |
| 41 | return self._cycles |
| 42 | |
| 43 | def __eq__(self, other): |
| 44 | if not isinstance(other, self.__class__): |
| 45 | # not comparing apples to apples |
| 46 | return False |
| 47 | |
| 48 | return (self.seconds, self.cycles) == (other.seconds, other.cycles) |
| 49 | |
| 50 | |
| 51 | class _ClockClass(object._SharedObject): |
| 52 | _get_ref = staticmethod(native_bt.clock_class_get_ref) |
| 53 | _put_ref = staticmethod(native_bt.clock_class_put_ref) |
| 54 | |
| 55 | @property |
| 56 | def name(self): |
| 57 | return native_bt.clock_class_get_name(self._ptr) |
| 58 | |
| 59 | def _name(self, name): |
| 60 | utils._check_str(name) |
| 61 | ret = native_bt.clock_class_set_name(self._ptr, name) |
| 62 | utils._handle_ret(ret, "cannot set clock class object's name") |
| 63 | |
| 64 | _name = property(fset=_name) |
| 65 | |
| 66 | @property |
| 67 | def description(self): |
| 68 | return native_bt.clock_class_get_description(self._ptr) |
| 69 | |
| 70 | def _description(self, description): |
| 71 | utils._check_str(description) |
| 72 | ret = native_bt.clock_class_set_description(self._ptr, description) |
| 73 | utils._handle_ret(ret, "cannot set clock class object's description") |
| 74 | |
| 75 | _description = property(fset=_description) |
| 76 | |
| 77 | @property |
| 78 | def frequency(self): |
| 79 | return native_bt.clock_class_get_frequency(self._ptr) |
| 80 | |
| 81 | def _frequency(self, frequency): |
| 82 | utils._check_uint64(frequency) |
| 83 | native_bt.clock_class_set_frequency(self._ptr, frequency) |
| 84 | |
| 85 | _frequency = property(fset=_frequency) |
| 86 | |
| 87 | @property |
| 88 | def precision(self): |
| 89 | precision = native_bt.clock_class_get_precision(self._ptr) |
| 90 | return precision |
| 91 | |
| 92 | def _precision(self, precision): |
| 93 | utils._check_uint64(precision) |
| 94 | native_bt.clock_class_set_precision(self._ptr, precision) |
| 95 | |
| 96 | _precision = property(fset=_precision) |
| 97 | |
| 98 | @property |
| 99 | def offset(self): |
| 100 | offset_s, offset_cycles = native_bt.clock_class_get_offset(self._ptr) |
| 101 | return ClockClassOffset(offset_s, offset_cycles) |
| 102 | |
| 103 | def _offset(self, offset): |
| 104 | utils._check_type(offset, ClockClassOffset) |
| 105 | native_bt.clock_class_set_offset(self._ptr, offset.seconds, offset.cycles) |
| 106 | |
| 107 | _offset = property(fset=_offset) |
| 108 | |
| 109 | @property |
| 110 | def origin_is_unix_epoch(self): |
| 111 | return native_bt.clock_class_origin_is_unix_epoch(self._ptr) |
| 112 | |
| 113 | def _origin_is_unix_epoch(self, origin_is_unix_epoch): |
| 114 | utils._check_bool(origin_is_unix_epoch) |
| 115 | native_bt.clock_class_set_origin_is_unix_epoch(self._ptr, int(origin_is_unix_epoch)) |
| 116 | |
| 117 | _origin_is_unix_epoch = property(fset=_origin_is_unix_epoch) |
| 118 | |
| 119 | @property |
| 120 | def uuid(self): |
| 121 | uuid_bytes = native_bt.clock_class_get_uuid(self._ptr) |
| 122 | |
| 123 | if uuid_bytes is None: |
| 124 | return |
| 125 | |
| 126 | return uuidp.UUID(bytes=uuid_bytes) |
| 127 | |
| 128 | def _uuid(self, uuid): |
| 129 | utils._check_type(uuid, uuidp.UUID) |
| 130 | native_bt.clock_class_set_uuid(self._ptr, uuid.bytes) |
| 131 | |
| 132 | _uuid = property(fset=_uuid) |
| 133 | |
| 134 | def cycles_to_ns_from_origin(self, cycles): |
| 135 | utils._check_uint64(cycles) |
| 136 | ret, ns = native_bt.clock_class_cycles_to_ns_from_origin(self._ptr, cycles) |
| 137 | |
| 138 | error_msg = "cannot convert clock value to nanoseconds from origin for given clock class" |
| 139 | |
| 140 | if ret == native_bt.CLOCK_CLASS_STATUS_OVERFLOW: |
| 141 | raise OverflowError(error_msg) |
| 142 | |
| 143 | utils._handle_ret(ret, error_msg) |
| 144 | return ns |