feat(revpiconfig): Enhance Wi-Fi detection and add rfkill index support

Improved logic to detect built-in and third-party Wi-Fi interfaces,
including integration of `rfkill` index retrieval for Wi-Fi devices.
This enhances support for various hardware setups and allows better
control over Wi-Fi functionality.
This commit is contained in:
2025-04-21 09:04:10 +02:00
parent fe614d026a
commit 18e6fb3d14

View File

@@ -6,10 +6,11 @@ import shutil
import subprocess
from collections import namedtuple
from enum import Enum, IntEnum
from glob import glob
from logging import getLogger
from os import X_OK, access
from os.path import exists
from typing import List
from os.path import exists, join
from typing import List, Optional
from pydbus import SystemBus
@@ -19,6 +20,7 @@ log = getLogger(__name__)
ConfigVariable = namedtuple("ConfigVariable", ["name", "value", "line_index"])
LINUX_WIFI_CLASS_PATH = "/sys/class/ieee80211"
CONFIG_TXT_LOCATIONS = ("/boot/firmware/config.txt", "/boot/config.txt")
@@ -45,6 +47,8 @@ class RevPiConfig:
self._cm_with_wifi = False
self._revpi_with_con_bridge = False
self._wlan_class_path = ""
self._wlan_rfkill_index = None
self.serial = ""
self.model = ""
@@ -81,8 +85,26 @@ class RevPiConfig:
# Detect WiFi on CM module
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)
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 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"):
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<index>\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)
@@ -94,6 +116,10 @@ class RevPiConfig:
def cm_type(self) -> ComputeModuleTypes:
return self._cm_type
@property
def rfkill_index(self) -> Optional[int]:
return self._wlan_rfkill_index
@property
def with_con_bridge(self) -> bool:
return self._revpi_with_con_bridge
@@ -331,6 +357,8 @@ if __name__ == "__main__":
print("Serial: ", rc.serial)
print("CM Type: ", rc.cm_type.name)
print("With wifi: ", rc.with_wifi)
if rc.with_wifi:
print(" rfkill index: ", rc.rfkill_index)
print("With con-bridge:", rc.with_con_bridge)
config_txt = ConfigTxt()