Commit | Line | Data |
---|---|---|
81447b5b PP |
1 | # The MIT License (MIT) |
2 | # | |
811644b8 | 3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> |
81447b5b PP |
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 | |
5783664e | 24 | from bt2 import value as bt2_value |
be7bbff9 | 25 | import uuid as uuidp |
81447b5b | 26 | |
0b03f63e | 27 | |
81447b5b PP |
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 | ||
eddea575 | 51 | class _ClockClassConst(object._SharedObject): |
3cdfbaea SM |
52 | _get_ref = staticmethod(native_bt.clock_class_get_ref) |
53 | _put_ref = staticmethod(native_bt.clock_class_put_ref) | |
eddea575 FD |
54 | _create_value_from_ptr_and_get_ref = staticmethod( |
55 | bt2_value._create_from_const_ptr_and_get_ref | |
56 | ) | |
2550c437 SM |
57 | _borrow_user_attributes_ptr = staticmethod( |
58 | native_bt.clock_class_borrow_user_attributes_const | |
59 | ) | |
3cdfbaea | 60 | |
5783664e PP |
61 | @property |
62 | def user_attributes(self): | |
2550c437 | 63 | ptr = self._borrow_user_attributes_ptr(self._ptr) |
5783664e | 64 | assert ptr is not None |
eddea575 FD |
65 | return self._create_value_from_ptr_and_get_ref(ptr) |
66 | ||
67 | @property | |
68 | def name(self): | |
69 | return native_bt.clock_class_get_name(self._ptr) | |
70 | ||
71 | @property | |
72 | def description(self): | |
73 | return native_bt.clock_class_get_description(self._ptr) | |
74 | ||
75 | @property | |
76 | def frequency(self): | |
77 | return native_bt.clock_class_get_frequency(self._ptr) | |
78 | ||
79 | @property | |
80 | def precision(self): | |
81 | precision = native_bt.clock_class_get_precision(self._ptr) | |
82 | return precision | |
83 | ||
84 | @property | |
85 | def offset(self): | |
86 | offset_s, offset_cycles = native_bt.clock_class_get_offset(self._ptr) | |
87 | return ClockClassOffset(offset_s, offset_cycles) | |
88 | ||
89 | @property | |
90 | def origin_is_unix_epoch(self): | |
91 | return native_bt.clock_class_origin_is_unix_epoch(self._ptr) | |
92 | ||
93 | @property | |
94 | def uuid(self): | |
95 | uuid_bytes = native_bt.clock_class_get_uuid(self._ptr) | |
96 | ||
97 | if uuid_bytes is None: | |
98 | return | |
99 | ||
100 | return uuidp.UUID(bytes=uuid_bytes) | |
101 | ||
102 | def cycles_to_ns_from_origin(self, cycles): | |
103 | utils._check_uint64(cycles) | |
104 | status, ns = native_bt.clock_class_cycles_to_ns_from_origin(self._ptr, cycles) | |
105 | error_msg = "cannot convert clock value to nanoseconds from origin for given clock class" | |
106 | utils._handle_func_status(status, error_msg) | |
107 | return ns | |
108 | ||
109 | ||
110 | class _ClockClass(_ClockClassConst): | |
111 | _create_value_from_ptr_and_get_ref = staticmethod( | |
112 | bt2_value._create_from_ptr_and_get_ref | |
113 | ) | |
2550c437 SM |
114 | _borrow_user_attributes_ptr = staticmethod( |
115 | native_bt.clock_class_borrow_user_attributes | |
116 | ) | |
5783664e PP |
117 | |
118 | def _user_attributes(self, user_attributes): | |
119 | value = bt2_value.create_value(user_attributes) | |
120 | utils._check_type(value, bt2_value.MapValue) | |
121 | native_bt.clock_class_set_user_attributes(self._ptr, value._ptr) | |
122 | ||
123 | _user_attributes = property(fset=_user_attributes) | |
124 | ||
be7bbff9 | 125 | def _name(self, name): |
81447b5b | 126 | utils._check_str(name) |
d24d5663 | 127 | status = native_bt.clock_class_set_name(self._ptr, name) |
cfbd7cf3 | 128 | utils._handle_func_status(status, "cannot set clock class object's name") |
81447b5b | 129 | |
be7bbff9 SM |
130 | _name = property(fset=_name) |
131 | ||
be7bbff9 | 132 | def _description(self, description): |
81447b5b | 133 | utils._check_str(description) |
d24d5663 | 134 | status = native_bt.clock_class_set_description(self._ptr, description) |
cfbd7cf3 | 135 | utils._handle_func_status(status, "cannot set clock class object's description") |
81447b5b | 136 | |
be7bbff9 SM |
137 | _description = property(fset=_description) |
138 | ||
be7bbff9 | 139 | def _frequency(self, frequency): |
81447b5b | 140 | utils._check_uint64(frequency) |
be7bbff9 SM |
141 | native_bt.clock_class_set_frequency(self._ptr, frequency) |
142 | ||
143 | _frequency = property(fset=_frequency) | |
81447b5b | 144 | |
be7bbff9 | 145 | def _precision(self, precision): |
81447b5b | 146 | utils._check_uint64(precision) |
be7bbff9 SM |
147 | native_bt.clock_class_set_precision(self._ptr, precision) |
148 | ||
149 | _precision = property(fset=_precision) | |
81447b5b | 150 | |
be7bbff9 | 151 | def _offset(self, offset): |
81447b5b | 152 | utils._check_type(offset, ClockClassOffset) |
be7bbff9 SM |
153 | native_bt.clock_class_set_offset(self._ptr, offset.seconds, offset.cycles) |
154 | ||
155 | _offset = property(fset=_offset) | |
81447b5b | 156 | |
be7bbff9 SM |
157 | def _origin_is_unix_epoch(self, origin_is_unix_epoch): |
158 | utils._check_bool(origin_is_unix_epoch) | |
cfbd7cf3 FD |
159 | native_bt.clock_class_set_origin_is_unix_epoch( |
160 | self._ptr, int(origin_is_unix_epoch) | |
161 | ) | |
be7bbff9 SM |
162 | |
163 | _origin_is_unix_epoch = property(fset=_origin_is_unix_epoch) | |
81447b5b | 164 | |
be7bbff9 | 165 | def _uuid(self, uuid): |
81447b5b | 166 | utils._check_type(uuid, uuidp.UUID) |
be7bbff9 SM |
167 | native_bt.clock_class_set_uuid(self._ptr, uuid.bytes) |
168 | ||
169 | _uuid = property(fset=_uuid) |