| 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 uuid as uuidp |
| 25 | import numbers |
| 26 | import bt2 |
| 27 | import bt2.clock_value as clock_value |
| 28 | |
| 29 | |
| 30 | class ClockClassOffset: |
| 31 | def __init__(self, seconds=0, cycles=0): |
| 32 | utils._check_int64(seconds) |
| 33 | utils._check_int64(cycles) |
| 34 | self._seconds = seconds |
| 35 | self._cycles = cycles |
| 36 | |
| 37 | @property |
| 38 | def seconds(self): |
| 39 | return self._seconds |
| 40 | |
| 41 | @property |
| 42 | def cycles(self): |
| 43 | return self._cycles |
| 44 | |
| 45 | def __hash__(self): |
| 46 | return hash((self.seconds, self.cycles)) |
| 47 | |
| 48 | def __eq__(self, other): |
| 49 | if not isinstance(other, self.__class__): |
| 50 | # not comparing apples to apples |
| 51 | return False |
| 52 | |
| 53 | return (self.seconds, self.cycles) == (other.seconds, other.cycles) |
| 54 | |
| 55 | |
| 56 | class ClockClass(object._Object): |
| 57 | def __init__(self, name, frequency, description=None, precision=None, |
| 58 | offset=None, is_absolute=None, uuid=None): |
| 59 | utils._check_str(name) |
| 60 | utils._check_uint64(frequency) |
| 61 | ptr = native_bt.clock_class_create(name, frequency) |
| 62 | |
| 63 | if ptr is None: |
| 64 | raise bt2.CreationError('cannot create clock class object') |
| 65 | |
| 66 | super().__init__(ptr) |
| 67 | |
| 68 | if description is not None: |
| 69 | self.description = description |
| 70 | |
| 71 | if frequency is not None: |
| 72 | self.frequency = frequency |
| 73 | |
| 74 | if precision is not None: |
| 75 | self.precision = precision |
| 76 | |
| 77 | if offset is not None: |
| 78 | self.offset = offset |
| 79 | |
| 80 | if is_absolute is not None: |
| 81 | self.is_absolute = is_absolute |
| 82 | |
| 83 | if uuid is not None: |
| 84 | self.uuid = uuid |
| 85 | |
| 86 | def __eq__(self, other): |
| 87 | if type(self) is not type(other): |
| 88 | # not comparing apples to apples |
| 89 | return False |
| 90 | |
| 91 | self_props = ( |
| 92 | self.name, |
| 93 | self.description, |
| 94 | self.frequency, |
| 95 | self.precision, |
| 96 | self.offset, |
| 97 | self.is_absolute, |
| 98 | self.uuid |
| 99 | ) |
| 100 | other_props = ( |
| 101 | other.name, |
| 102 | other.description, |
| 103 | other.frequency, |
| 104 | other.precision, |
| 105 | other.offset, |
| 106 | other.is_absolute, |
| 107 | other.uuid |
| 108 | ) |
| 109 | return self_props == other_props |
| 110 | |
| 111 | def __copy__(self): |
| 112 | return ClockClass(name=self.name, description=self.description, |
| 113 | frequency=self.frequency, precision=self.precision, |
| 114 | offset=self.offset, is_absolute=self.is_absolute, |
| 115 | uuid=self.uuid) |
| 116 | |
| 117 | def __deepcopy__(self, memo): |
| 118 | cpy = self.__copy__() |
| 119 | memo[id(self)] = cpy |
| 120 | return cpy |
| 121 | |
| 122 | def __hash__(self): |
| 123 | return hash(( |
| 124 | self.name, |
| 125 | self.description, |
| 126 | self.frequency, |
| 127 | self.precision, |
| 128 | self.offset.seconds, |
| 129 | self.offset.cycles, |
| 130 | self.is_absolute, |
| 131 | self.uuid)) |
| 132 | |
| 133 | @property |
| 134 | def name(self): |
| 135 | name = native_bt.clock_class_get_name(self._ptr) |
| 136 | assert(name is not None) |
| 137 | return name |
| 138 | |
| 139 | @name.setter |
| 140 | def name(self, name): |
| 141 | utils._check_str(name) |
| 142 | ret = native_bt.clock_class_set_name(self._ptr, name) |
| 143 | utils._handle_ret(ret, "cannot set clock class object's name") |
| 144 | |
| 145 | @property |
| 146 | def description(self): |
| 147 | return native_bt.clock_class_get_description(self._ptr) |
| 148 | |
| 149 | @description.setter |
| 150 | def description(self, description): |
| 151 | utils._check_str(description) |
| 152 | ret = native_bt.clock_class_set_description(self._ptr, description) |
| 153 | utils._handle_ret(ret, "cannot set clock class object's description") |
| 154 | |
| 155 | @property |
| 156 | def frequency(self): |
| 157 | frequency = native_bt.clock_class_get_frequency(self._ptr) |
| 158 | assert(frequency >= 1) |
| 159 | return frequency |
| 160 | |
| 161 | @frequency.setter |
| 162 | def frequency(self, frequency): |
| 163 | utils._check_uint64(frequency) |
| 164 | ret = native_bt.clock_class_set_frequency(self._ptr, frequency) |
| 165 | utils._handle_ret(ret, "cannot set clock class object's frequency") |
| 166 | |
| 167 | @property |
| 168 | def precision(self): |
| 169 | precision = native_bt.clock_class_get_precision(self._ptr) |
| 170 | assert(precision >= 0) |
| 171 | return precision |
| 172 | |
| 173 | @precision.setter |
| 174 | def precision(self, precision): |
| 175 | utils._check_uint64(precision) |
| 176 | ret = native_bt.clock_class_set_precision(self._ptr, precision) |
| 177 | utils._handle_ret(ret, "cannot set clock class object's precision") |
| 178 | |
| 179 | @property |
| 180 | def offset(self): |
| 181 | ret, offset_s = native_bt.clock_class_get_offset_s(self._ptr) |
| 182 | assert(ret == 0) |
| 183 | ret, offset_cycles = native_bt.clock_class_get_offset_cycles(self._ptr) |
| 184 | assert(ret == 0) |
| 185 | return ClockClassOffset(offset_s, offset_cycles) |
| 186 | |
| 187 | @offset.setter |
| 188 | def offset(self, offset): |
| 189 | utils._check_type(offset, ClockClassOffset) |
| 190 | ret = native_bt.clock_class_set_offset_s(self._ptr, offset.seconds) |
| 191 | utils._handle_ret(ret, "cannot set clock class object's offset (seconds)") |
| 192 | ret = native_bt.clock_class_set_offset_cycles(self._ptr, offset.cycles) |
| 193 | utils._handle_ret(ret, "cannot set clock class object's offset (cycles)") |
| 194 | |
| 195 | @property |
| 196 | def is_absolute(self): |
| 197 | is_absolute = native_bt.clock_class_is_absolute(self._ptr) |
| 198 | assert(is_absolute >= 0) |
| 199 | return is_absolute > 0 |
| 200 | |
| 201 | @is_absolute.setter |
| 202 | def is_absolute(self, is_absolute): |
| 203 | utils._check_bool(is_absolute) |
| 204 | ret = native_bt.clock_class_set_is_absolute(self._ptr, int(is_absolute)) |
| 205 | utils._handle_ret(ret, "cannot set clock class object's absoluteness") |
| 206 | |
| 207 | @property |
| 208 | def uuid(self): |
| 209 | uuid_bytes = native_bt.clock_class_get_uuid(self._ptr) |
| 210 | |
| 211 | if uuid_bytes is None: |
| 212 | return |
| 213 | |
| 214 | return uuidp.UUID(bytes=uuid_bytes) |
| 215 | |
| 216 | @uuid.setter |
| 217 | def uuid(self, uuid): |
| 218 | utils._check_type(uuid, uuidp.UUID) |
| 219 | ret = native_bt.clock_class_set_uuid(self._ptr, uuid.bytes) |
| 220 | utils._handle_ret(ret, "cannot set clock class object's UUID") |
| 221 | |
| 222 | def __call__(self, cycles): |
| 223 | return clock_value._ClockValue(self._ptr, cycles) |