1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
5 from bt2
import native_bt
6 from bt2
import object as bt2_object
7 from bt2
import utils
as bt2_utils
8 from bt2
import value
as bt2_value
12 class ClockClassOffset
:
13 def __init__(self
, seconds
=0, cycles
=0):
14 bt2_utils
._check
_int
64(seconds
)
15 bt2_utils
._check
_int
64(cycles
)
16 self
._seconds
= seconds
27 def __eq__(self
, other
):
28 if not isinstance(other
, self
.__class
__):
29 # not comparing apples to apples
32 return (self
.seconds
, self
.cycles
) == (other
.seconds
, other
.cycles
)
35 class _ClockClassConst(bt2_object
._SharedObject
):
38 native_bt
.clock_class_get_ref(ptr
)
42 native_bt
.clock_class_put_ref(ptr
)
44 _create_value_from_ptr_and_get_ref
= staticmethod(
45 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
47 _borrow_user_attributes_ptr
= staticmethod(
48 native_bt
.clock_class_borrow_user_attributes_const
52 def user_attributes(self
):
53 ptr
= self
._borrow
_user
_attributes
_ptr
(self
._ptr
)
54 assert ptr
is not None
55 return self
._create
_value
_from
_ptr
_and
_get
_ref
(ptr
)
59 return native_bt
.clock_class_get_name(self
._ptr
)
62 def description(self
):
63 return native_bt
.clock_class_get_description(self
._ptr
)
67 return native_bt
.clock_class_get_frequency(self
._ptr
)
71 precision
= native_bt
.clock_class_get_precision(self
._ptr
)
76 offset_s
, offset_cycles
= native_bt
.clock_class_get_offset(self
._ptr
)
77 return ClockClassOffset(offset_s
, offset_cycles
)
80 def origin_is_unix_epoch(self
):
81 return native_bt
.clock_class_origin_is_unix_epoch(self
._ptr
)
85 uuid_bytes
= native_bt
.clock_class_get_uuid(self
._ptr
)
87 if uuid_bytes
is None:
90 return uuidp
.UUID(bytes
=uuid_bytes
)
92 def cycles_to_ns_from_origin(self
, cycles
):
93 bt2_utils
._check
_uint
64(cycles
)
94 status
, ns
= native_bt
.clock_class_cycles_to_ns_from_origin(self
._ptr
, cycles
)
95 error_msg
= "cannot convert clock value to nanoseconds from origin for given clock class"
96 bt2_utils
._handle
_func
_status
(status
, error_msg
)
100 class _ClockClass(_ClockClassConst
):
101 _create_value_from_ptr_and_get_ref
= staticmethod(
102 bt2_value
._create
_from
_ptr
_and
_get
_ref
104 _borrow_user_attributes_ptr
= staticmethod(
105 native_bt
.clock_class_borrow_user_attributes
108 def _user_attributes(self
, user_attributes
):
109 value
= bt2_value
.create_value(user_attributes
)
110 bt2_utils
._check
_type
(value
, bt2_value
.MapValue
)
111 native_bt
.clock_class_set_user_attributes(self
._ptr
, value
._ptr
)
113 _user_attributes
= property(fset
=_user_attributes
)
115 def _name(self
, name
):
116 bt2_utils
._check
_str
(name
)
117 status
= native_bt
.clock_class_set_name(self
._ptr
, name
)
118 bt2_utils
._handle
_func
_status
(status
, "cannot set clock class object's name")
120 _name
= property(fset
=_name
)
122 def _description(self
, description
):
123 bt2_utils
._check
_str
(description
)
124 status
= native_bt
.clock_class_set_description(self
._ptr
, description
)
125 bt2_utils
._handle
_func
_status
(
126 status
, "cannot set clock class object's description"
129 _description
= property(fset
=_description
)
131 def _frequency(self
, frequency
):
132 bt2_utils
._check
_uint
64(frequency
)
133 native_bt
.clock_class_set_frequency(self
._ptr
, frequency
)
135 _frequency
= property(fset
=_frequency
)
137 def _precision(self
, precision
):
138 bt2_utils
._check
_uint
64(precision
)
139 native_bt
.clock_class_set_precision(self
._ptr
, precision
)
141 _precision
= property(fset
=_precision
)
143 def _offset(self
, offset
):
144 bt2_utils
._check
_type
(offset
, ClockClassOffset
)
145 native_bt
.clock_class_set_offset(self
._ptr
, offset
.seconds
, offset
.cycles
)
147 _offset
= property(fset
=_offset
)
149 def _origin_is_unix_epoch(self
, origin_is_unix_epoch
):
150 bt2_utils
._check
_bool
(origin_is_unix_epoch
)
151 native_bt
.clock_class_set_origin_is_unix_epoch(
152 self
._ptr
, int(origin_is_unix_epoch
)
155 _origin_is_unix_epoch
= property(fset
=_origin_is_unix_epoch
)
157 def _uuid(self
, uuid
):
158 bt2_utils
._check
_type
(uuid
, uuidp
.UUID
)
159 native_bt
.clock_class_set_uuid(self
._ptr
, uuid
.bytes
)
161 _uuid
= property(fset
=_uuid
)