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