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