1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 from bt2
import native_bt
, object, utils
6 from bt2
import value
as bt2_value
10 class ClockClassOffset
:
11 def __init__(self
, seconds
=0, cycles
=0):
12 utils
._check
_int
64(seconds
)
13 utils
._check
_int
64(cycles
)
14 self
._seconds
= seconds
25 def __eq__(self
, other
):
26 if not isinstance(other
, self
.__class
__):
27 # not comparing apples to apples
30 return (self
.seconds
, self
.cycles
) == (other
.seconds
, other
.cycles
)
33 class _ClockClassConst(object._SharedObject
):
36 native_bt
.clock_class_get_ref(ptr
)
40 native_bt
.clock_class_put_ref(ptr
)
42 _create_value_from_ptr_and_get_ref
= staticmethod(
43 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
45 _borrow_user_attributes_ptr
= staticmethod(
46 native_bt
.clock_class_borrow_user_attributes_const
50 def user_attributes(self
):
51 ptr
= self
._borrow
_user
_attributes
_ptr
(self
._ptr
)
52 assert ptr
is not None
53 return self
._create
_value
_from
_ptr
_and
_get
_ref
(ptr
)
57 return native_bt
.clock_class_get_name(self
._ptr
)
60 def description(self
):
61 return native_bt
.clock_class_get_description(self
._ptr
)
65 return native_bt
.clock_class_get_frequency(self
._ptr
)
69 precision
= native_bt
.clock_class_get_precision(self
._ptr
)
74 offset_s
, offset_cycles
= native_bt
.clock_class_get_offset(self
._ptr
)
75 return ClockClassOffset(offset_s
, offset_cycles
)
78 def origin_is_unix_epoch(self
):
79 return native_bt
.clock_class_origin_is_unix_epoch(self
._ptr
)
83 uuid_bytes
= native_bt
.clock_class_get_uuid(self
._ptr
)
85 if uuid_bytes
is None:
88 return uuidp
.UUID(bytes
=uuid_bytes
)
90 def cycles_to_ns_from_origin(self
, cycles
):
91 utils
._check
_uint
64(cycles
)
92 status
, ns
= native_bt
.clock_class_cycles_to_ns_from_origin(self
._ptr
, cycles
)
93 error_msg
= "cannot convert clock value to nanoseconds from origin for given clock class"
94 utils
._handle
_func
_status
(status
, error_msg
)
98 class _ClockClass(_ClockClassConst
):
99 _create_value_from_ptr_and_get_ref
= staticmethod(
100 bt2_value
._create
_from
_ptr
_and
_get
_ref
102 _borrow_user_attributes_ptr
= staticmethod(
103 native_bt
.clock_class_borrow_user_attributes
106 def _user_attributes(self
, user_attributes
):
107 value
= bt2_value
.create_value(user_attributes
)
108 utils
._check
_type
(value
, bt2_value
.MapValue
)
109 native_bt
.clock_class_set_user_attributes(self
._ptr
, value
._ptr
)
111 _user_attributes
= property(fset
=_user_attributes
)
113 def _name(self
, name
):
114 utils
._check
_str
(name
)
115 status
= native_bt
.clock_class_set_name(self
._ptr
, name
)
116 utils
._handle
_func
_status
(status
, "cannot set clock class object's name")
118 _name
= property(fset
=_name
)
120 def _description(self
, description
):
121 utils
._check
_str
(description
)
122 status
= native_bt
.clock_class_set_description(self
._ptr
, description
)
123 utils
._handle
_func
_status
(status
, "cannot set clock class object's description")
125 _description
= property(fset
=_description
)
127 def _frequency(self
, frequency
):
128 utils
._check
_uint
64(frequency
)
129 native_bt
.clock_class_set_frequency(self
._ptr
, frequency
)
131 _frequency
= property(fset
=_frequency
)
133 def _precision(self
, precision
):
134 utils
._check
_uint
64(precision
)
135 native_bt
.clock_class_set_precision(self
._ptr
, precision
)
137 _precision
= property(fset
=_precision
)
139 def _offset(self
, offset
):
140 utils
._check
_type
(offset
, ClockClassOffset
)
141 native_bt
.clock_class_set_offset(self
._ptr
, offset
.seconds
, offset
.cycles
)
143 _offset
= property(fset
=_offset
)
145 def _origin_is_unix_epoch(self
, origin_is_unix_epoch
):
146 utils
._check
_bool
(origin_is_unix_epoch
)
147 native_bt
.clock_class_set_origin_is_unix_epoch(
148 self
._ptr
, int(origin_is_unix_epoch
)
151 _origin_is_unix_epoch
= property(fset
=_origin_is_unix_epoch
)
153 def _uuid(self
, uuid
):
154 utils
._check
_type
(uuid
, uuidp
.UUID
)
155 native_bt
.clock_class_set_uuid(self
._ptr
, uuid
.bytes
)
157 _uuid
= property(fset
=_uuid
)