From ac5ddbd93bd35a22a9dd9105965f97ad59e51711 Mon Sep 17 00:00:00 2001 From: Charlie Shao Date: Fri, 28 Nov 2025 13:23:38 +0100 Subject: [PATCH] Add support for binary file as input to the parser Signed-off-by: Charlie Shao --- scripts/nrf70_fw_stats_parser.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/scripts/nrf70_fw_stats_parser.py b/scripts/nrf70_fw_stats_parser.py index 55cc5e3..9566484 100755 --- a/scripts/nrf70_fw_stats_parser.py +++ b/scripts/nrf70_fw_stats_parser.py @@ -249,9 +249,9 @@ def parse_rpu_sys_fw_stats(self, blob_data: bytes, endianness: str = '<'): logging.debug(f" Match: {'✓' if total_expected == len(blob_data) else '✗'}") def main(): - parser = argparse.ArgumentParser(description='Parse rpu_sys_fw_stats from hex blob using header file') + parser = argparse.ArgumentParser(description='Parse rpu_sys_fw_stats from hex blob or binary file using header file') parser.add_argument('header_file', help='Path to header file containing struct definitions') - parser.add_argument('hex_blob', help='Hex blob data to parse') + parser.add_argument('hex_blob', help='Hex blob data string or path to binary file') parser.add_argument('-d', '--debug', action='store_true', help='Enable debug output') args = parser.parse_args() @@ -266,8 +266,25 @@ def main(): print(f"Error: Header file '{args.header_file}' not found") sys.exit(1) - # Convert hex string to binary - blob_data = bytes.fromhex(args.hex_blob.replace(' ', '')) + # Check if blob is a file + if os.path.isfile(args.blob): + try: + with open(args.blob, 'rb') as f: + blob_data = f.read() + logging.debug(f"Read {len(blob_data)} bytes from file '{args.blob}'") + except Exception as e: + print(f"Error reading file '{args.blob}': {e}") + sys.exit(1) + else: + # Assume it's a hex string + try: + # Remove potential whitespace and 0x prefix + clean_hex = args.blob.replace(' ', '').replace('0x', '').replace('\n', '') + blob_data = bytes.fromhex(clean_hex) + logging.debug(f"Parsed {len(blob_data)} bytes from hex string") + except ValueError: + print(f"Error: Argument '{args.blob}' is neither a valid file path nor a valid hex string.") + sys.exit(1) # Parse using header file struct_parser = StructParser(args.header_file, debug=args.debug)