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