|
| 1 | +# |
| 2 | +# Copyright (c) 2025 Project CHIP Authors |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +"""Conversion utilities for Matter testing infrastructure. |
| 19 | +
|
| 20 | +This module provides utility functions for converting between different data |
| 21 | +representations commonly used in Matter testing. |
| 22 | +""" |
| 23 | + |
| 24 | +from binascii import hexlify, unhexlify |
| 25 | + |
| 26 | +import chip.clusters as Clusters |
| 27 | + |
| 28 | + |
| 29 | +def bytes_from_hex(hex: str) -> bytes: |
| 30 | + """ Converts hex string to bytes, handling various formats (colons, spaces, newlines). |
| 31 | +
|
| 32 | + Examples: |
| 33 | + >>> bytes_from_hex("01:ab:cd") |
| 34 | + b'\\x01\\xab\\xcd' |
| 35 | + >>> bytes_from_hex("01 ab cd") |
| 36 | + b'\\x01\\xab\\xcd' |
| 37 | + >>> bytes_from_hex("01abcd") |
| 38 | + b'\\x01\\xab\\xcd' |
| 39 | + >>> bytes_from_hex("01\\nab\\ncd") |
| 40 | + b'\\x01\\xab\\xcd' |
| 41 | + """ |
| 42 | + return unhexlify("".join(hex.replace(":", "").replace(" ", "").split())) |
| 43 | + |
| 44 | + |
| 45 | +def hex_from_bytes(b: bytes) -> str: |
| 46 | + """ Converts a bytes object to a hexadecimal string. |
| 47 | +
|
| 48 | + This function performs the inverse operation of bytes_from_hex(). It converts |
| 49 | + a bytes object into a continuous hexadecimal string without any separators. |
| 50 | +
|
| 51 | + Args: |
| 52 | + b: bytes, the bytes object to convert to hexadecimal |
| 53 | +
|
| 54 | + Returns: |
| 55 | + str: A string containing the hexadecimal representation of the bytes, |
| 56 | + using lowercase letters a-f for hex digits |
| 57 | +
|
| 58 | + Examples: |
| 59 | + >>> hex_from_bytes(b'\\x01\\xab\\xcd') |
| 60 | + '01abcd' |
| 61 | + >>> hex_from_bytes(bytes([1, 171, 205])) # Same bytes, different notation |
| 62 | + '01abcd' |
| 63 | + """ |
| 64 | + return hexlify(b).decode("utf-8") |
| 65 | + |
| 66 | + |
| 67 | +def format_decimal_and_hex(number): |
| 68 | + """ Formats a number showing both decimal and hexadecimal representations. |
| 69 | +
|
| 70 | + Creates a string representation of a number showing both its decimal value |
| 71 | + and its hex representation in parentheses. |
| 72 | +
|
| 73 | + Args: |
| 74 | + number: int, the number to format |
| 75 | +
|
| 76 | + Returns: |
| 77 | + str: A formatted string like "123 (0x7b)" |
| 78 | +
|
| 79 | + Examples: |
| 80 | + >>> format_decimal_and_hex(123) |
| 81 | + '123 (0x7b)' |
| 82 | + >>> format_decimal_and_hex(0) |
| 83 | + '0 (0x00)' |
| 84 | + >>> format_decimal_and_hex(255) |
| 85 | + '255 (0xff)' |
| 86 | + """ |
| 87 | + return f'{number} (0x{number:02x})' |
| 88 | + |
| 89 | + |
| 90 | +def cluster_id_with_name(id): |
| 91 | + """ Formats a Matter cluster ID with its name and numeric representation. |
| 92 | +
|
| 93 | + Uses format_decimal_and_hex() for numeric formatting and looks up cluster name from registry. |
| 94 | + Falls back to "Unknown cluster" if ID not recognized. |
| 95 | +
|
| 96 | + Args: |
| 97 | + id: int, the Matter cluster identifier |
| 98 | +
|
| 99 | + Returns: |
| 100 | + str: A formatted string containing the ID and cluster name |
| 101 | +
|
| 102 | + Examples: |
| 103 | + >>> cluster_id_with_name(6) # OnOff cluster |
| 104 | + '6 (0x06) OnOff' |
| 105 | + >>> cluster_id_with_name(999999) # Unknown cluster |
| 106 | + '999999 (0xf423f) Unknown cluster' |
| 107 | + >>> cluster_id_with_name("invalid") # Invalid input |
| 108 | + 'HERE IS THE PROBLEM' |
| 109 | + """ |
| 110 | + if id in Clusters.ClusterObjects.ALL_CLUSTERS.keys(): |
| 111 | + s = Clusters.ClusterObjects.ALL_CLUSTERS[id].__name__ |
| 112 | + else: |
| 113 | + s = "Unknown cluster" |
| 114 | + try: |
| 115 | + return f'{format_decimal_and_hex(id)} {s}' |
| 116 | + except (TypeError, ValueError): |
| 117 | + return 'HERE IS THE PROBLEM' |
| 118 | + |
| 119 | + |
| 120 | +if __name__ == "__main__": |
| 121 | + import doctest |
| 122 | + doctest.testmod() |
0 commit comments