refactor(revpiconfig: Change Wi-Fi detection and rfkill index logic
Replaced inline rfkill index detection with a standalone `get_rfkill_index` function for improved modularity. Removed `_cm_with_wifi` and `_wlan_rfkill_index` attributes, utilizing `_wlan_class_path` for Wi-Fi presence checks. Adjusted property and output logic to incorporate the new function.
This commit is contained in:
@@ -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
|
||||
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<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)
|
||||
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<index>\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()
|
||||
|
||||
Reference in New Issue
Block a user