Skip to content

Commit

Permalink
Apply Hook25's suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
seankingyang committed Jun 17, 2024
1 parent 6d3913f commit 471f92e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 30 deletions.
54 changes: 25 additions & 29 deletions providers/resource/bin/WIFI_phy.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ def parse_iw_dev_output() -> List[Tuple[str, str]]:
"""
Parses the output of "iw dev" to extract PHY and interface mappings.
"""
cmd = "iw dev"
output = check_output(cmd, shell=True, universal_newlines=True)
output = check_output(["iw", "dev"], universal_newlines=True)
iw_dev_ptn = r"(phy.*)[\s\S]*?Interface (\w+)"
iw_dev_compile = re.compile(iw_dev_ptn)
return iw_dev_compile.findall(output)
return re.findall(iw_dev_ptn, output)


def parse_phy_info_output(output: str) -> Dict[str, List[str]]:
Expand All @@ -41,22 +39,21 @@ def parse_phy_info_output(output: str) -> Dict[str, List[str]]:
bands_data = output.split("Supported commands:")[0]
bands = {}
for band in bands_data.split("Band "):
if band:
band_ptn = r"(\d+):\s+([\s\S]*)"
band_compile = re.compile(band_ptn)
band_res = band_compile.match(band)
if band_res:
band_num = band_res.groups()[0]
band_content = band_res.groups()[1]
freqs_raw = band_content.split("Frequencies:")[1]
freq_ptn = r"(\d+ MHz.*)"
freq_compile = re.compile(freq_ptn)
freqs = [
freq
for freq in freq_compile.findall(freqs_raw)
if "disabled" not in freq
]
bands[band_num] = freqs
if not band:
continue

Check warning on line 43 in providers/resource/bin/WIFI_phy.py

View check run for this annotation

Codecov / codecov/patch

providers/resource/bin/WIFI_phy.py#L43

Added line #L43 was not covered by tests
band_ptn = r"(\d+):"
band_res = re.match(band_ptn, band)
if not band_res:
continue
band_num, band_content = band.split(":", 1)
# get Frequencies paragraph
freqs_raw = band_content.split("Frequencies:", 1)[1].split(":", 1)[0]
freqs = [
freq.strip()
for freq in freqs_raw.split("*")
if "disabled" not in freq and "MHz" in freq
]
bands[band_num] = freqs
return bands


Expand All @@ -80,14 +77,12 @@ def check_freq_support(bands: Dict[str, List[str]]) -> Dict[str, str]:
Checks if supported frequency (2.4GHz, 5GHz, 6GHz) are present based on
band number
"""
# Ex. the frequency 2.4GHz is band 1, 5GHz is band 2, 6GHz is band 4
# so if bands[1] is not empty, the device supports 2.4GHz.
supported_freqs = {"2.4GHz": "1", "5GHz": "2", "6GHz": "4"}

freq_supported = {
freq: (
"supported"
if band in bands and len(bands[band]) > 0
else "unsupported"
)
freq: ("supported" if bands.get(band) else "unsupported")
for freq, band in supported_freqs.items()
}
return freq_supported
Expand All @@ -100,9 +95,7 @@ def create_phy_interface_mapping(phy_interface: List[Tuple[str, str]]) -> Dict:
phy_interface_mapping = {}
for phy, interface in phy_interface:
cmd = "iw {} info".format(phy)
phy_info_output = check_output(
cmd, shell=True, universal_newlines=True
)
phy_info_output = check_output(cmd, universal_newlines=True)
bands = parse_phy_info_output(phy_info_output)
freq_supported = check_freq_support(bands)
sta_supported = check_sta_support(phy_info_output)
Expand All @@ -125,7 +118,10 @@ def main():
# Print interface summary with detailed information on separate lines
for interface, content in phy_interface_mapping.items():
for freq, ret in content["FREQ_Supported"].items():
print("{}_{}: {}".format(interface, freq, ret))
# replace . with _ to support resource expressions for 2.4GHz
# as . is not a valid character in resource expression names
# i.e.: wifi.wlan0_2_4GHz is valid, wifi.wlan0_2.4GHz is not
print("{}_{}: {}".format(interface, freq.replace(".", "_"), ret))
for sta, ret in content["STA_Supported"].items():
print("{}_{}: {}".format(interface, sta.lower(), ret))

Expand Down
2 changes: 1 addition & 1 deletion providers/resource/tests/test_WIFI_phy.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def test_main(self, mock_check_output, mock_print):
mock_check_output.side_effect = [iw_dev_output, phy_info_output]
main()
expected_calls = [
unittest.mock.call("wlp2s0_2.4GHz: supported"),
unittest.mock.call("wlp2s0_2_4GHz: supported"),
unittest.mock.call("wlp2s0_5GHz: supported"),
unittest.mock.call("wlp2s0_6GHz: unsupported"),
unittest.mock.call("wlp2s0_be: unsupported"),
Expand Down

0 comments on commit 471f92e

Please sign in to comment.