From 85dc6a7e5602bceec7d4e8a6ce6d83e0aa39ac30 Mon Sep 17 00:00:00 2001 From: Sven Sager Date: Sun, 20 Apr 2025 15:37:26 +0200 Subject: [PATCH] feat(dbus): Add RevPiConfig class for device information handling This commit introduces a new `RevPiConfig` class to manage RevPi device configuration details, such as model, serial, compute module type, WiFi, and ConBridge detection. It parses CPU info from `/proc/cpuinfo` and leverages helper methods for hardware-specific checks, enhancing the middleware's ability to identify and manage hardware features. --- .../system_config/revpi_config.py | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 src/revpi_middleware/dbus_middleware1/system_config/revpi_config.py diff --git a/src/revpi_middleware/dbus_middleware1/system_config/revpi_config.py b/src/revpi_middleware/dbus_middleware1/system_config/revpi_config.py new file mode 100644 index 0000000..12ef5ee --- /dev/null +++ b/src/revpi_middleware/dbus_middleware1/system_config/revpi_config.py @@ -0,0 +1,92 @@ +# -*- coding: utf-8 -*- +# SPDX-FileCopyrightText: 2025 KUNBUS GmbH +# SPDX-License-Identifier: GPL-2.0-or-later +from enum import IntEnum +from logging import getLogger + +from ..dbus_helper import grep + +log = getLogger(__name__) + + +class ComputeModuleTypes(IntEnum): + UNKNOWN = 0 + CM1 = 6 + CM3 = 10 + CM4 = 20 + CM4S = 21 + CM5 = 24 + + +class RevPiConfig: + + def __init__(self): + self._cm_type = ComputeModuleTypes.UNKNOWN + self._cm_with_wifi = False + + self._revpi_with_con_bridge = False + + self.serial = "" + self.model = "" + + self._init_device_info() + + def _init_device_info(self): + dc_cpuinfo = {} + + # Extract CPU information + with open("/proc/cpuinfo", "r") as f: + line = "\n" + while line: + line = f.readline() + if line.startswith(("Revision", "Serial", "Model")): + key, value = line.split(":", 1) + key = key.strip().lower() + value = value.strip() + dc_cpuinfo[key] = value + + self.model = dc_cpuinfo.get("model", "") + self.serial = dc_cpuinfo.get("serial", "") + + # Detect Compute Module type + revision = dc_cpuinfo.get("revision", "") + if revision: + revision = int(revision, 16) + mask = 4080 # 0xFF0 in dezimal + try: + self._cm_type = ComputeModuleTypes((revision & mask) >> 4) + except ValueError: + pass + + # Detect WiFi + could_have_wifi = self._cm_type in (ComputeModuleTypes.CM4, ComputeModuleTypes.CM5) + if could_have_wifi: + lst_grep = grep("DRIVER=brcmfmac", "/sys/class/ieee80211/phy0/device/uevent") + self._cm_with_wifi = len(lst_grep) > 0 and self._cm_type in (ComputeModuleTypes) + + # Detect ConBridge + could_have_con_bridge = self._cm_type in (ComputeModuleTypes.CM3, ComputeModuleTypes.CM4S) + if could_have_con_bridge: + lst_grep = grep("kunbus,revpi-connect", "/proc/device-tree/compatible") + self._revpi_with_con_bridge = len(lst_grep) > 0 + + @property + def cm_type(self) -> ComputeModuleTypes: + return self._cm_type + + @property + def with_con_bridge(self) -> bool: + return self._revpi_with_con_bridge + + @property + def with_wifi(self) -> bool: + return self._cm_with_wifi + + +if __name__ == "__main__": + rc = RevPiConfig() + print("Model:", rc.model) + print("Serial: ", rc.serial) + print("CM Type: ", rc.cm_type.name) + print("With wifi: ", rc.with_wifi) + print("With con-bridge:", rc.with_con_bridge)