Commit | Line | Data |
---|---|---|
0235b0db | 1 | # SPDX-License-Identifier: MIT |
81447b5b | 2 | # |
811644b8 | 3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> |
81447b5b PP |
4 | |
5 | import bt2 | |
3fb99a22 | 6 | from bt2 import logging as bt2_logging |
d24d5663 | 7 | from bt2 import native_bt |
81447b5b PP |
8 | |
9 | ||
10 | def _check_bool(o): | |
11 | if not isinstance(o, bool): | |
12 | raise TypeError("'{}' is not a 'bool' object".format(o.__class__.__name__)) | |
13 | ||
14 | ||
15 | def _check_int(o): | |
16 | if not isinstance(o, int): | |
17 | raise TypeError("'{}' is not an 'int' object".format(o.__class__.__name__)) | |
18 | ||
19 | ||
20 | def _check_float(o): | |
21 | if not isinstance(o, float): | |
22 | raise TypeError("'{}' is not a 'float' object".format(o.__class__.__name__)) | |
23 | ||
24 | ||
25 | def _check_str(o): | |
26 | if not isinstance(o, str): | |
27 | raise TypeError("'{}' is not a 'str' object".format(o.__class__.__name__)) | |
28 | ||
29 | ||
30 | def _check_type(o, expected_type): | |
31 | if not isinstance(o, expected_type): | |
cfbd7cf3 FD |
32 | raise TypeError( |
33 | "'{}' is not a '{}' object".format(o.__class__.__name__, expected_type) | |
34 | ) | |
81447b5b PP |
35 | |
36 | ||
c6af194f | 37 | def _is_in_int64_range(v): |
cfbd7cf3 FD |
38 | assert isinstance(v, int) |
39 | return v >= -(2 ** 63) and v <= (2 ** 63 - 1) | |
81447b5b PP |
40 | |
41 | ||
c6af194f PP |
42 | def _is_int64(v): |
43 | if not isinstance(v, int): | |
44 | return False | |
45 | ||
46 | return _is_in_int64_range(v) | |
47 | ||
48 | ||
49 | def _is_in_uint64_range(v): | |
cfbd7cf3 FD |
50 | assert isinstance(v, int) |
51 | return v >= 0 and v <= (2 ** 64 - 1) | |
81447b5b PP |
52 | |
53 | ||
c6af194f PP |
54 | def _is_uint64(v): |
55 | if not isinstance(v, int): | |
56 | return False | |
57 | ||
58 | return _is_in_uint64_range(v) | |
59 | ||
60 | ||
81447b5b | 61 | def _check_int64(v, msg=None): |
c6af194f PP |
62 | _check_int(v) |
63 | ||
64 | if not _is_in_int64_range(v): | |
81447b5b PP |
65 | if msg is None: |
66 | msg = 'expecting a signed 64-bit integral value' | |
67 | ||
68 | msg += ' (got {})'.format(v) | |
69 | raise ValueError(msg) | |
70 | ||
71 | ||
72 | def _check_uint64(v, msg=None): | |
c6af194f PP |
73 | _check_int(v) |
74 | ||
75 | if not _is_in_uint64_range(v): | |
81447b5b PP |
76 | if msg is None: |
77 | msg = 'expecting an unsigned 64-bit integral value' | |
78 | ||
79 | msg += ' (got {})'.format(v) | |
80 | raise ValueError(msg) | |
81 | ||
82 | ||
83 | def _is_m1ull(v): | |
84 | return v == 18446744073709551615 | |
85 | ||
86 | ||
87 | def _is_pow2(v): | |
88 | return v != 0 and ((v & (v - 1)) == 0) | |
89 | ||
90 | ||
91 | def _check_alignment(a): | |
92 | _check_uint64(a) | |
93 | ||
81447b5b PP |
94 | if not _is_pow2(a): |
95 | raise ValueError('{} is not a power of two'.format(a)) | |
96 | ||
97 | ||
e874da19 PP |
98 | def _check_log_level(log_level): |
99 | _check_int(log_level) | |
100 | ||
101 | log_levels = ( | |
3fb99a22 PP |
102 | bt2_logging.LoggingLevel.TRACE, |
103 | bt2_logging.LoggingLevel.DEBUG, | |
104 | bt2_logging.LoggingLevel.INFO, | |
105 | bt2_logging.LoggingLevel.WARNING, | |
106 | bt2_logging.LoggingLevel.ERROR, | |
107 | bt2_logging.LoggingLevel.FATAL, | |
108 | bt2_logging.LoggingLevel.NONE, | |
e874da19 PP |
109 | ) |
110 | ||
111 | if log_level not in log_levels: | |
112 | raise ValueError("'{}' is not a valid logging level".format(log_level)) | |
d24d5663 PP |
113 | |
114 | ||
115 | def _handle_func_status(status, msg=None): | |
116 | if status == native_bt.__BT_FUNC_STATUS_OK: | |
117 | # no error | |
118 | return | |
119 | ||
4acc866e | 120 | if status == native_bt.__BT_FUNC_STATUS_ERROR: |
ce4923b0 | 121 | assert msg is not None |
694c792b | 122 | raise bt2._Error(msg) |
4acc866e SM |
123 | elif status == native_bt.__BT_FUNC_STATUS_MEMORY_ERROR: |
124 | assert msg is not None | |
694c792b | 125 | raise bt2._MemoryError(msg) |
d24d5663 PP |
126 | elif status == native_bt.__BT_FUNC_STATUS_END: |
127 | if msg is None: | |
128 | raise bt2.Stop | |
129 | else: | |
130 | raise bt2.Stop(msg) | |
131 | elif status == native_bt.__BT_FUNC_STATUS_AGAIN: | |
132 | if msg is None: | |
133 | raise bt2.TryAgain | |
134 | else: | |
135 | raise bt2.TryAgain(msg) | |
520cdc82 | 136 | elif status == native_bt.__BT_FUNC_STATUS_OVERFLOW_ERROR: |
d24d5663 | 137 | if msg is None: |
cb06aa27 | 138 | raise bt2._OverflowError |
d24d5663 | 139 | else: |
cb06aa27 | 140 | raise bt2._OverflowError(msg) |
76b6c2f7 | 141 | elif status == native_bt.__BT_FUNC_STATUS_UNKNOWN_OBJECT: |
d24d5663 | 142 | if msg is None: |
76b6c2f7 | 143 | raise bt2.UnknownObject |
d24d5663 | 144 | else: |
76b6c2f7 | 145 | raise bt2.UnknownObject(msg) |
d24d5663 PP |
146 | else: |
147 | assert False | |
2a52034a PP |
148 | |
149 | ||
150 | class _ListenerHandle: | |
67de22ba SM |
151 | def __init__(self, addr): |
152 | self._addr = addr | |
153 | self._listener_id = None | |
154 | ||
155 | def _set_listener_id(self, listener_id): | |
2a52034a | 156 | self._listener_id = listener_id |
67de22ba SM |
157 | |
158 | def _invalidate(self): | |
159 | self._listener_id = None |