1 # SPDX-License-Identifier: MIT
3 # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
7 from bt2
import utils
as bt2_utils
8 from bt2
import value
as bt2_value
9 from bt2
import object as bt2_object
10 from bt2
import native_bt
13 class ClockClassOffset
:
14 def __init__(self
, seconds
=0, cycles
=0):
15 bt2_utils
._check
_int
64(seconds
)
16 bt2_utils
._check
_int
64(cycles
)
17 self
._seconds
= seconds
28 def __eq__(self
, other
):
29 if not isinstance(other
, self
.__class
__):
30 # not comparing apples to apples
33 return (self
.seconds
, self
.cycles
) == (other
.seconds
, other
.cycles
)
36 class _ClockClassConst(bt2_object
._SharedObject
):
39 native_bt
.clock_class_get_ref(ptr
)
43 native_bt
.clock_class_put_ref(ptr
)
45 _create_value_from_ptr_and_get_ref
= staticmethod(
46 bt2_value
._create
_from
_const
_ptr
_and
_get
_ref
48 _borrow_user_attributes_ptr
= staticmethod(
49 native_bt
.clock_class_borrow_user_attributes_const
53 def user_attributes(self
):
54 ptr
= self
._borrow
_user
_attributes
_ptr
(self
._ptr
)
55 assert ptr
is not None
56 return self
._create
_value
_from
_ptr
_and
_get
_ref
(ptr
)
60 return native_bt
.clock_class_get_name(self
._ptr
)
63 def description(self
):
64 return native_bt
.clock_class_get_description(self
._ptr
)
68 return native_bt
.clock_class_get_frequency(self
._ptr
)
72 precision
= native_bt
.clock_class_get_precision(self
._ptr
)
77 offset_s
, offset_cycles
= native_bt
.clock_class_get_offset(self
._ptr
)
78 return ClockClassOffset(offset_s
, offset_cycles
)
81 def origin_is_unix_epoch(self
):
82 return native_bt
.clock_class_origin_is_unix_epoch(self
._ptr
)
86 uuid_bytes
= native_bt
.clock_class_get_uuid(self
._ptr
)
88 if uuid_bytes
is None:
91 return uuidp
.UUID(bytes
=uuid_bytes
)
93 def cycles_to_ns_from_origin(self
, cycles
):
94 bt2_utils
._check
_uint
64(cycles
)
95 status
, ns
= native_bt
.clock_class_cycles_to_ns_from_origin(self
._ptr
, cycles
)
96 error_msg
= "cannot convert clock value to nanoseconds from origin for given clock class"
97 bt2_utils
._handle
_func
_status
(status
, error_msg
)
101 class _ClockClass(_ClockClassConst
):
102 _create_value_from_ptr_and_get_ref
= staticmethod(
103 bt2_value
._create
_from
_ptr
_and
_get
_ref
105 _borrow_user_attributes_ptr
= staticmethod(
106 native_bt
.clock_class_borrow_user_attributes
109 def _user_attributes(self
, user_attributes
):
110 value
= bt2_value
.create_value(user_attributes
)
111 bt2_utils
._check
_type
(value
, bt2_value
.MapValue
)
112 native_bt
.clock_class_set_user_attributes(self
._ptr
, value
._ptr
)
114 _user_attributes
= property(fset
=_user_attributes
)
116 def _name(self
, name
):
117 bt2_utils
._check
_str
(name
)
118 status
= native_bt
.clock_class_set_name(self
._ptr
, name
)
119 bt2_utils
._handle
_func
_status
(status
, "cannot set clock class object's name")
121 _name
= property(fset
=_name
)
123 def _description(self
, description
):
124 bt2_utils
._check
_str
(description
)
125 status
= native_bt
.clock_class_set_description(self
._ptr
, description
)
126 bt2_utils
._handle
_func
_status
(
127 status
, "cannot set clock class object's description"
130 _description
= property(fset
=_description
)
132 def _frequency(self
, frequency
):
133 bt2_utils
._check
_uint
64(frequency
)
134 native_bt
.clock_class_set_frequency(self
._ptr
, frequency
)
136 _frequency
= property(fset
=_frequency
)
138 def _precision(self
, precision
):
139 bt2_utils
._check
_uint
64(precision
)
140 native_bt
.clock_class_set_precision(self
._ptr
, precision
)
142 _precision
= property(fset
=_precision
)
144 def _offset(self
, offset
):
145 bt2_utils
._check
_type
(offset
, ClockClassOffset
)
146 native_bt
.clock_class_set_offset(self
._ptr
, offset
.seconds
, offset
.cycles
)
148 _offset
= property(fset
=_offset
)
150 def _origin_is_unix_epoch(self
, origin_is_unix_epoch
):
151 bt2_utils
._check
_bool
(origin_is_unix_epoch
)
152 native_bt
.clock_class_set_origin_is_unix_epoch(
153 self
._ptr
, int(origin_is_unix_epoch
)
156 _origin_is_unix_epoch
= property(fset
=_origin_is_unix_epoch
)
158 def _uuid(self
, uuid
):
159 bt2_utils
._check
_type
(uuid
, uuidp
.UUID
)
160 native_bt
.clock_class_set_uuid(self
._ptr
, uuid
.bytes
)
162 _uuid
= property(fset
=_uuid
)