| 1 |
"""WLAN module.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from collections.abc import Awaitable, Callable |
| 6 |
from enum import Enum, IntEnum |
| 7 |
from typing import Any |
| 8 |
|
| 9 |
from asusrouter.modules.const import MapValueType |
| 10 |
from asusrouter.tools.converters import ( |
| 11 |
get_arguments, |
| 12 |
safe_bool, |
| 13 |
safe_int, |
| 14 |
safe_unpack_key, |
| 15 |
) |
| 16 |
from asusrouter.tools.writers import nvram |
| 17 |
|
| 18 |
|
| 19 |
class Wlan(str, Enum): |
| 20 |
"""WLAN type class.""" |
| 21 |
|
| 22 |
FREQ_2G = "2ghz" |
| 23 |
FREQ_5G = "5ghz" |
| 24 |
FREQ_5G2 = "5ghz2" |
| 25 |
FREQ_6G = "6ghz" |
| 26 |
UNKNOWN = "unknown" |
| 27 |
|
| 28 |
|
| 29 |
# A map to correspond possible values to the WlanType |
| 30 |
# E.g. `wlc_0` -> FREQ_2G, `wlc_1` -> FREQ_5G, etc. |
| 31 |
# But also `wl0` -> FREQ_2G, `wl1` -> FREQ_5G, etc. |
| 32 |
WLAN_TYPE: dict[str | int, Wlan] = { |
| 33 |
0: Wlan.FREQ_2G, |
| 34 |
1: Wlan.FREQ_5G, |
| 35 |
2: Wlan.FREQ_5G2, |
| 36 |
3: Wlan.FREQ_6G, |
| 37 |
"2.4G": Wlan.FREQ_2G, |
| 38 |
"5G": Wlan.FREQ_5G, |
| 39 |
"5G-2": Wlan.FREQ_5G2, |
| 40 |
"wifi6e": Wlan.FREQ_6G, |
| 41 |
"wlc_0": Wlan.FREQ_2G, |
| 42 |
"wlc_1": Wlan.FREQ_5G, |
| 43 |
"wlc_2": Wlan.FREQ_5G2, |
| 44 |
"wlc_3": Wlan.FREQ_6G, |
| 45 |
"wl0": Wlan.FREQ_2G, |
| 46 |
"wl1": Wlan.FREQ_5G, |
| 47 |
"wl2": Wlan.FREQ_5G2, |
| 48 |
"wl3": Wlan.FREQ_6G, |
| 49 |
} |
| 50 |
|
| 51 |
# A map of NVRAM values for a GWLAN |
| 52 |
MAP_GWLAN: list[MapValueType] = [ |
| 53 |
("wl{}_akm"), |
| 54 |
("wl{}_ap_isolate"), |
| 55 |
("wl{}_auth"), |
| 56 |
("wl{}_auth_mode"), |
| 57 |
("wl{}_auth_mode_x"), |
| 58 |
("wl{}_bridge"), |
| 59 |
("wl{}_bss_enabled", safe_bool), |
| 60 |
("wl{}_bss_maxassoc", safe_int), |
| 61 |
("wl{}_bw_dl", safe_int), # Bandwidth limit download |
| 62 |
("wl{}_bw_enabled", safe_bool), # Bandwidth limit switch |
| 63 |
("wl{}_bw_ul", safe_int), # Bandwidth limit upload |
| 64 |
("wl{}_closed", safe_bool), |
| 65 |
("wl{}_crypto"), |
| 66 |
("wl{}_expire", safe_int), # Expire time in s |
| 67 |
("wl{}_expire_tmp", safe_int), # Expire time left in s |
| 68 |
("wl{}_gn_wbl_enable"), |
| 69 |
("wl{}_gn_wbl_rule"), |
| 70 |
("wl{}_gn_wbl_type"), |
| 71 |
("wl{}_hwaddr"), # MAC address |
| 72 |
("wl{}_ifname"), # Interface name |
| 73 |
("wl{}_infra"), |
| 74 |
("wl{}_key"), |
| 75 |
("wl{}_key1"), |
| 76 |
("wl{}_key2"), |
| 77 |
("wl{}_key3"), |
| 78 |
("wl{}_key4"), |
| 79 |
("wl{}_lanaccess", safe_bool), # LAN access |
| 80 |
("wl{}_maclist"), |
| 81 |
("wl{}_macmode"), |
| 82 |
("wl{}_maxassoc", safe_int), |
| 83 |
("wl{}_mbss"), |
| 84 |
("wl{}_mfp"), |
| 85 |
("wl{}_mode"), |
| 86 |
("wl{}_net_reauth", safe_int), |
| 87 |
("wl{}_preauth"), |
| 88 |
("wl{}_radio", safe_bool), |
| 89 |
("wl{}_radius_ipaddr"), |
| 90 |
("wl{}_radius_key"), |
| 91 |
("wl{}_radius_port", safe_int), |
| 92 |
("wl{}_sae_anti_clog_threshold"), |
| 93 |
("wl{}_sae_groups"), |
| 94 |
("wl{}_sae_sync"), |
| 95 |
("wl{}_ssid"), # SSID |
| 96 |
("wl{}_sta_retry_time"), |
| 97 |
("wl{}_sync_node", safe_bool), # Sync AiMesh nodes |
| 98 |
("wl{}_unit"), # GWLAN unit id |
| 99 |
("wl{}_wep", safe_bool), |
| 100 |
("wl{}_wep_x", safe_bool), |
| 101 |
("wl{}_wfi_enable", safe_bool), |
| 102 |
("wl{}_wfi_pinmode"), |
| 103 |
("wl{}_wme"), |
| 104 |
("wl{}_wme_bss_disable", safe_bool), |
| 105 |
("wl{}_wpa_gtk_rekey"), |
| 106 |
("wl{}_wpa_psk"), # Password |
| 107 |
("wl{}_wps_mode"), |
| 108 |
] |
| 109 |
|
| 110 |
|
| 111 |
# A map of NVRAM values for a WLAN |
| 112 |
MAP_WLAN: list[MapValueType] = [ |
| 113 |
("wl{}_auth_mode_x"), |
| 114 |
("wl{}_bw"), |
| 115 |
("wl{}_channel"), |
| 116 |
("wl{}_chanspec"), |
| 117 |
("wl{}_closed", safe_bool), |
| 118 |
("wl{}_country_code"), |
| 119 |
("wl{}_crypto"), |
| 120 |
("wl{}_gmode_check"), |
| 121 |
("wl{}_maclist_x"), |
| 122 |
("wl{}_macmode"), |
| 123 |
("wl{}_mbo_enable"), |
| 124 |
("wl{}_mfp"), |
| 125 |
("wl{}_nmode_x"), |
| 126 |
("wl{}_optimizexbox_ckb"), |
| 127 |
("wl{}_radio", safe_bool), |
| 128 |
("wl{}_radius_ipaddr"), |
| 129 |
("wl{}_radius_key"), |
| 130 |
("wl{}_radius_port", safe_int), |
| 131 |
("wl{}_ssid"), |
| 132 |
("wl{}_wpa_gtk_rekey"), |
| 133 |
("wl{}_wpa_psk"), |
| 134 |
] |
| 135 |
|
| 136 |
|
| 137 |
class AsusWLAN(IntEnum): |
| 138 |
"""Asus WLAN state.""" |
| 139 |
|
| 140 |
UNKNOWN = -999 |
| 141 |
OFF = 0 |
| 142 |
ON = 1 |
| 143 |
|
| 144 |
|
| 145 |
def _nvram_request( |
| 146 |
wlan: list[Wlan] | None, mapping: list[MapValueType], guest: bool = False |
| 147 |
) -> str | None: |
| 148 |
"""Create an NVRAM request.""" |
| 149 |
|
| 150 |
if not wlan: |
| 151 |
return None |
| 152 |
|
| 153 |
request = [] |
| 154 |
|
| 155 |
for interface in wlan: |
| 156 |
for pair in mapping: |
| 157 |
key, _ = safe_unpack_key(pair) |
| 158 |
index = list(Wlan).index(interface) |
| 159 |
if guest: |
| 160 |
request.extend( |
| 161 |
[key.format(f"{index}.{gid}") for gid in range(1, 4)] |
| 162 |
) |
| 163 |
else: |
| 164 |
request.append(key.format(index)) |
| 165 |
|
| 166 |
return nvram(request) |
| 167 |
|
| 168 |
|
| 169 |
def wlan_nvram_request(wlan: list[Wlan] | None) -> str | None: |
| 170 |
"""Create an NVRAM request for WLAN.""" |
| 171 |
|
| 172 |
return _nvram_request(wlan, MAP_WLAN) |
| 173 |
|
| 174 |
|
| 175 |
def gwlan_nvram_request(wlan: list[Wlan] | None) -> str | None: |
| 176 |
"""Create an NVRAM request for GWLAN.""" |
| 177 |
|
| 178 |
return _nvram_request(wlan, MAP_GWLAN, guest=True) |
| 179 |
|
| 180 |
|
| 181 |
async def set_state( |
| 182 |
callback: Callable[..., Awaitable[bool]], |
| 183 |
state: AsusWLAN, |
| 184 |
**kwargs: Any, |
| 185 |
) -> bool: |
| 186 |
"""Set the parental control state.""" |
| 187 |
|
| 188 |
# Get the arguments |
| 189 |
api_type, api_id = get_arguments(("api_type", "api_id"), **kwargs) |
| 190 |
|
| 191 |
# Check if the api_type and api_id are available |
| 192 |
if api_type is None or api_id is None: |
| 193 |
return False |
| 194 |
|
| 195 |
# Define the service and callback arguments for each api_type |
| 196 |
api_values = { |
| 197 |
"wlan": { |
| 198 |
"service": "restart_wireless", |
| 199 |
"callback_arguments": { |
| 200 |
f"wl{api_id}_radio": 1 if state == AsusWLAN.ON else 0 |
| 201 |
}, |
| 202 |
}, |
| 203 |
"gwlan": { |
| 204 |
"service": "restart_wireless;restart_firewall", |
| 205 |
"callback_arguments": { |
| 206 |
f"wl{api_id}_bss_enabled": 1 if state == AsusWLAN.ON else 0, |
| 207 |
**({f"wl{api_id}_expire": 0} if state == AsusWLAN.ON else {}), |
| 208 |
}, |
| 209 |
}, |
| 210 |
} |
| 211 |
|
| 212 |
# Get the service and callback arguments for the given api_type |
| 213 |
api_value = api_values.get(api_type) |
| 214 |
if not api_value: |
| 215 |
return False |
| 216 |
|
| 217 |
# Call the service |
| 218 |
return await callback( |
| 219 |
api_value["service"], |
| 220 |
arguments=api_value["callback_arguments"], |
| 221 |
apply=True, |
| 222 |
expect_modify=kwargs.get("expect_modify", False), |
| 223 |
) |