diff --git a/src/revpi_middleware/dbus_middleware1/system_config/revpi_config.py b/src/revpi_middleware/dbus_middleware1/system_config/revpi_config.py index fdd25a3..da1832c 100644 --- a/src/revpi_middleware/dbus_middleware1/system_config/revpi_config.py +++ b/src/revpi_middleware/dbus_middleware1/system_config/revpi_config.py @@ -44,11 +44,9 @@ class RevPiConfig: def __init__(self): self._cm_type = ComputeModuleTypes.UNKNOWN - self._cm_with_wifi = False self._revpi_with_con_bridge = False self._wlan_class_path = "" - self._wlan_rfkill_index = None self.serial = "" self.model = "" @@ -86,26 +84,16 @@ class RevPiConfig: could_have_wifi = self._cm_type in (ComputeModuleTypes.CM4, ComputeModuleTypes.CM5) if could_have_wifi: wifi_interface = join(LINUX_WIFI_CLASS_PATH, "phy0") - lst_grep = grep("DRIVER=brcmfmac", join(wifi_interface, "device", "uevent")) - self._cm_with_wifi = len(lst_grep) > 0 - self._wlan_class_path = wifi_interface + if grep("DRIVER=brcmfmac", join(wifi_interface, "device", "uevent")): + self._wlan_class_path = wifi_interface # If no build in Wi-Fi on the CM, detect third party Wi-Fi on RevPi Flat - if not self._cm_with_wifi and grep("revpi-flat", "/proc/device-tree/compatible"): + if not self._wlan_class_path and grep("revpi-flat", "/proc/device-tree/compatible"): lst_wifi_interfaces = glob("/sys/class/ieee80211/*") for wifi_interface in lst_wifi_interfaces: if grep("DRIVER=mwifiex_sdio", join(wifi_interface, "device", "uevent")): - self._cm_with_wifi = True self._wlan_class_path = wifi_interface - # Detect rfkill index of the integrated Wi-Fi device - if self._wlan_class_path: - for rfkill_path in glob(join(self._wlan_class_path, "rfkill*")): - match_index = re.match(r"^/.+/rfkill(?P\d+)$", rfkill_path) - if match_index: - self._wlan_rfkill_index = int(match_index.group("index")) - break - # Detect ConBridge could_have_con_bridge = self._cm_type in (ComputeModuleTypes.CM3, ComputeModuleTypes.CM4S) if could_have_con_bridge: @@ -113,12 +101,12 @@ class RevPiConfig: self._revpi_with_con_bridge = len(lst_grep) > 0 @property - def cm_type(self) -> ComputeModuleTypes: - return self._cm_type + def class_path_wifi(self) -> str: + return self._wlan_class_path @property - def rfkill_index(self) -> Optional[int]: - return self._wlan_rfkill_index + def cm_type(self) -> ComputeModuleTypes: + return self._cm_type @property def with_con_bridge(self) -> bool: @@ -126,7 +114,7 @@ class RevPiConfig: @property def with_wifi(self) -> bool: - return self._cm_with_wifi + return bool(self._wlan_class_path) class ConfigTxt: @@ -318,21 +306,24 @@ def configure_wifi(action: ConfigActions): revpi = RevPiConfig() if action is ConfigActions.ENABLE: - if revpi.rfkill_index is not None: - subprocess.call(["rfkill", "unblock", str(revpi.rfkill_index)]) + if revpi.with_wifi: + wifi_rfkill_index = get_rfkill_index(revpi.class_path_wifi) + subprocess.call(["rfkill", "unblock", str(wifi_rfkill_index)]) elif action is ConfigActions.DISABLE: - if revpi.rfkill_index is not None: - subprocess.call(["rfkill", "block", str(revpi.rfkill_index)]) + if revpi.with_wifi: + wifi_rfkill_index = get_rfkill_index(revpi.class_path_wifi) + subprocess.call(["rfkill", "block", str(wifi_rfkill_index)]) elif action is ConfigActions.AVAILABLE: - return revpi.with_wifi and revpi.rfkill_index is not None + return revpi.with_wifi elif action is ConfigActions.STATUS: - if revpi.rfkill_index is None: + if not revpi.with_wifi: return False - with open(f"/sys/class/rfkill/rfkill{revpi.rfkill_index}/soft", "r") as f: + wifi_rfkill_index = get_rfkill_index(revpi.class_path_wifi) + with open(f"/sys/class/rfkill/rfkill{wifi_rfkill_index}/soft", "r") as f: buffer = f.read().strip() return buffer == "0" @@ -342,6 +333,16 @@ def configure_wifi(action: ConfigActions): return None +def get_rfkill_index(device_class_path: str) -> Optional[int]: + re_rfkill_index = re.compile(r"^/.+/rfkill(?P\d+)$") + for rfkill_path in glob(join(device_class_path, "rfkill*")): + match_index = re_rfkill_index.match(rfkill_path) + if match_index: + return int(match_index.group("index")) + + return None + + def simple_systemd(action: ConfigActions, unit: str): bus = SystemBus() systemd_manager = bus.get(".systemd1") @@ -386,7 +387,8 @@ if __name__ == "__main__": print("CM Type: ", rc.cm_type.name) print("With wifi: ", rc.with_wifi) if rc.with_wifi: - print(" rfkill index: ", rc.rfkill_index) + print(" class path: ", rc.class_path_wifi) + print(" rfkill index: ", get_rfkill_index(rc.class_path_wifi)) print("With con-bridge:", rc.with_con_bridge) config_txt = ConfigTxt()