1"""Class to configure wireless settings.""" 2 3import time 4 5from acts.controllers.ap_lib import hostapd_constants 6from acts.controllers.openwrt_lib.network_settings import SERVICE_DNSMASQ 7from acts.controllers.openwrt_lib.network_settings import ServiceManager 8 9 10LEASE_FILE = "/tmp/dhcp.leases" 11OPEN_SECURITY = "none" 12PSK1_SECURITY = "psk" 13PSK_SECURITY = "psk2" 14WEP_SECURITY = "wep" 15ENT_SECURITY = "wpa2" 16OWE_SECURITY = "owe" 17SAE_SECURITY = "sae" 18ENABLE_RADIO = "0" 19DISABLE_RADIO = "1" 20ENABLE_HIDDEN = "1" 21 22 23class WirelessSettingsApplier(object): 24 """Class for wireless settings. 25 26 Attributes: 27 ssh: ssh object for the AP. 28 service_manager: Object manage service configuration 29 wireless_configs: a list of 30 acts.controllers.openwrt_lib.wireless_config.WirelessConfig. 31 channel_2g: channel for 2G band. 32 channel_5g: channel for 5G band. 33 """ 34 35 def __init__(self, ssh, configs, channel_2g, channel_5g): 36 """Initialize wireless settings. 37 38 Args: 39 ssh: ssh connection object. 40 configs: a list of 41 acts.controllers.openwrt_lib.wireless_config.WirelessConfig. 42 channel_2g: channel for 2G band. 43 channel_5g: channel for 5G band. 44 """ 45 self.ssh = ssh 46 self.service_manager = ServiceManager(ssh) 47 self.wireless_configs = configs 48 self.channel_2g = channel_2g 49 self.channel_5g = channel_5g 50 51 def apply_wireless_settings(self): 52 """Configure wireless settings from a list of configs.""" 53 54 # set channels for 2G and 5G bands 55 self.ssh.run("uci set wireless.radio1.channel='%s'" % self.channel_2g) 56 self.ssh.run("uci set wireless.radio0.channel='%s'" % self.channel_5g) 57 if self.channel_5g == 165: 58 self.ssh.run("uci set wireless.radio0.htmode='VHT20'") 59 elif self.channel_5g == 132 or self.channel_5g == 136: 60 self.ssh.run("iw reg set ZA") 61 self.ssh.run("uci set wireless.radio0.htmode='VHT40'") 62 63 if self.channel_2g == 13: 64 self.ssh.run("iw reg set AU") 65 66 # disable default OpenWrt SSID 67 self.ssh.run("uci set wireless.default_radio1.disabled='%s'" % 68 DISABLE_RADIO) 69 self.ssh.run("uci set wireless.default_radio0.disabled='%s'" % 70 DISABLE_RADIO) 71 72 # Enable radios 73 self.ssh.run("uci set wireless.radio1.disabled='%s'" % ENABLE_RADIO) 74 self.ssh.run("uci set wireless.radio0.disabled='%s'" % ENABLE_RADIO) 75 76 for config in self.wireless_configs: 77 78 # configure open network 79 if config.security == OPEN_SECURITY: 80 if config.band == hostapd_constants.BAND_2G: 81 self.ssh.run("uci set wireless.default_radio1.ssid='%s'" % 82 config.ssid) 83 self.ssh.run("uci set wireless.default_radio1.disabled='%s'" % 84 ENABLE_RADIO) 85 if config.hidden: 86 self.ssh.run("uci set wireless.default_radio1.hidden='%s'" % 87 ENABLE_HIDDEN) 88 elif config.band == hostapd_constants.BAND_5G: 89 self.ssh.run("uci set wireless.default_radio0.ssid='%s'" % 90 config.ssid) 91 self.ssh.run("uci set wireless.default_radio0.disabled='%s'" % 92 ENABLE_RADIO) 93 if config.hidden: 94 self.ssh.run("uci set wireless.default_radio0.hidden='%s'" % 95 ENABLE_HIDDEN) 96 continue 97 98 self.ssh.run("uci set wireless.%s='wifi-iface'" % config.name) 99 if config.band == hostapd_constants.BAND_2G: 100 self.ssh.run("uci set wireless.%s.device='radio1'" % config.name) 101 else: 102 self.ssh.run("uci set wireless.%s.device='radio0'" % config.name) 103 self.ssh.run("uci set wireless.%s.network='%s'" % 104 (config.name, config.iface)) 105 self.ssh.run("uci set wireless.%s.mode='ap'" % config.name) 106 self.ssh.run("uci set wireless.%s.ssid='%s'" % 107 (config.name, config.ssid)) 108 self.ssh.run("uci set wireless.%s.encryption='%s'" % 109 (config.name, config.security)) 110 if config.security == PSK_SECURITY or config.security == SAE_SECURITY\ 111 or config.security == PSK1_SECURITY: 112 self.ssh.run("uci set wireless.%s.key='%s'" % 113 (config.name, config.password)) 114 elif config.security == WEP_SECURITY: 115 self.ssh.run("uci set wireless.%s.key%s='%s'" % 116 (config.name, config.wep_key_num, config.wep_key)) 117 self.ssh.run("uci set wireless.%s.key='%s'" % 118 (config.name, config.wep_key_num)) 119 elif config.security == ENT_SECURITY: 120 self.ssh.run("uci set wireless.%s.auth_secret='%s'" % 121 (config.name, config.radius_server_secret)) 122 self.ssh.run("uci set wireless.%s.auth_server='%s'" % 123 (config.name, config.radius_server_ip)) 124 self.ssh.run("uci set wireless.%s.auth_port='%s'" % 125 (config.name, config.radius_server_port)) 126 if config.ieee80211w: 127 self.ssh.run("uci set wireless.%s.ieee80211w='%s'" % 128 (config.name, config.ieee80211w)) 129 if config.hidden: 130 self.ssh.run("uci set wireless.%s.hidden='%s'" % 131 (config.name, ENABLE_HIDDEN)) 132 133 self.ssh.run("uci commit wireless") 134 self.ssh.run("cp %s %s.tmp" % (LEASE_FILE, LEASE_FILE)) 135 136 def cleanup_wireless_settings(self): 137 """Reset wireless settings to default.""" 138 self.ssh.run("wifi down") 139 self.ssh.run("rm -f /etc/config/wireless") 140 self.ssh.run("wifi config") 141 if self.channel_5g == 132: 142 self.ssh.run("iw reg set US") 143 self.ssh.run("cp %s.tmp %s" % (LEASE_FILE, LEASE_FILE)) 144 self.service_manager.restart(SERVICE_DNSMASQ) 145 time.sleep(9) 146