| 1 | # SPDX-License-Identifier: MIT |
| 2 | # |
| 3 | # Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com> |
| 4 | |
| 5 | import bt2 |
| 6 | from bt2 import logging as bt2_logging |
| 7 | from bt2 import native_bt |
| 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): |
| 32 | raise TypeError( |
| 33 | "'{}' is not a '{}' object".format(o.__class__.__name__, expected_type) |
| 34 | ) |
| 35 | |
| 36 | |
| 37 | def _is_in_int64_range(v): |
| 38 | assert isinstance(v, int) |
| 39 | return v >= -(2 ** 63) and v <= (2 ** 63 - 1) |
| 40 | |
| 41 | |
| 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): |
| 50 | assert isinstance(v, int) |
| 51 | return v >= 0 and v <= (2 ** 64 - 1) |
| 52 | |
| 53 | |
| 54 | def _is_uint64(v): |
| 55 | if not isinstance(v, int): |
| 56 | return False |
| 57 | |
| 58 | return _is_in_uint64_range(v) |
| 59 | |
| 60 | |
| 61 | def _check_int64(v, msg=None): |
| 62 | _check_int(v) |
| 63 | |
| 64 | if not _is_in_int64_range(v): |
| 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): |
| 73 | _check_int(v) |
| 74 | |
| 75 | if not _is_in_uint64_range(v): |
| 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 | |
| 94 | if not _is_pow2(a): |
| 95 | raise ValueError('{} is not a power of two'.format(a)) |
| 96 | |
| 97 | |
| 98 | def _check_log_level(log_level): |
| 99 | _check_int(log_level) |
| 100 | |
| 101 | log_levels = ( |
| 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, |
| 109 | ) |
| 110 | |
| 111 | if log_level not in log_levels: |
| 112 | raise ValueError("'{}' is not a valid logging level".format(log_level)) |
| 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 | |
| 120 | if status == native_bt.__BT_FUNC_STATUS_ERROR: |
| 121 | assert msg is not None |
| 122 | raise bt2._Error(msg) |
| 123 | elif status == native_bt.__BT_FUNC_STATUS_MEMORY_ERROR: |
| 124 | assert msg is not None |
| 125 | raise bt2._MemoryError(msg) |
| 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) |
| 136 | elif status == native_bt.__BT_FUNC_STATUS_OVERFLOW_ERROR: |
| 137 | if msg is None: |
| 138 | raise bt2._OverflowError |
| 139 | else: |
| 140 | raise bt2._OverflowError(msg) |
| 141 | elif status == native_bt.__BT_FUNC_STATUS_UNKNOWN_OBJECT: |
| 142 | if msg is None: |
| 143 | raise bt2.UnknownObject |
| 144 | else: |
| 145 | raise bt2.UnknownObject(msg) |
| 146 | else: |
| 147 | assert False |
| 148 | |
| 149 | |
| 150 | class _ListenerHandle: |
| 151 | def __init__(self, addr): |
| 152 | self._addr = addr |
| 153 | self._listener_id = None |
| 154 | |
| 155 | def _set_listener_id(self, listener_id): |
| 156 | self._listener_id = listener_id |
| 157 | |
| 158 | def _invalidate(self): |
| 159 | self._listener_id = None |