• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright (c) 2012 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5"""Class to control the AsusAP router."""
6
7from __future__ import print_function
8
9import logging
10from selenium.common.exceptions import TimeoutException
11
12import dynamic_ap_configurator
13import ap_spec
14
15
16class AsusAPConfigurator(
17        dynamic_ap_configurator.DynamicAPConfigurator):
18    """Base class for Asus RT-N56U router."""
19
20
21    def _set_authentication(self, authentication, wait_for_xpath=None):
22        """Sets the authentication method in the popup.
23
24        Args:
25          authentication: The authentication method to select.
26          wait_for_path: An item to wait for before returning.
27        """
28        auth = '//select[@name="rt_auth_mode"]'
29        if self.current_band == ap_spec.BAND_5GHZ:
30            auth = '//select[@name="wl_auth_mode"]'
31        self.select_item_from_popup_by_xpath(authentication, auth,
32            wait_for_xpath, alert_handler=self._invalid_security_handler)
33
34
35    def _invalid_security_handler(self, alert):
36        text = alert.text
37        # This tweaks encryption but is more of a warning, so we can dismiss.
38        if text.find('will change WEP or TKIP encryption to AES') != -1:
39            alert.accept()
40        else:
41            raise RuntimeError('You have entered an invalid configuration: '
42                               '%s' % text)
43
44
45    def get_number_of_pages(self):
46        return 2
47
48
49    def get_supported_bands(self):
50        return [{'band': ap_spec.BAND_2GHZ,
51                 'channels': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]},
52                {'band': ap_spec.BAND_5GHZ,
53                 'channels': [36, 40, 44, 48, 149, 153, 157, 161]}]
54
55
56    def get_supported_modes(self):
57        return [{'band': ap_spec.BAND_2GHZ,
58                 'modes': [ap_spec.MODE_B, ap_spec.MODE_N, ap_spec.MODE_B |
59                           ap_spec.MODE_G, ap_spec.MODE_B]},
60                {'band': ap_spec.BAND_5GHZ,
61                 'modes': [ap_spec.MODE_N, ap_spec.MODE_A]}]
62
63
64    def is_security_mode_supported(self, security_mode):
65        """
66        Returns if a given security_type is supported.
67
68        @param security_mode: one security modes defined in the APSpec
69
70        @return True if the security mode is supported; False otherwise.
71
72        """
73        return security_mode in (ap_spec.SECURITY_TYPE_DISABLED,
74                                 ap_spec.SECURITY_TYPE_WPAPSK,
75                                 ap_spec.SECURITY_TYPE_WPA2PSK,
76                                 ap_spec.SECURITY_TYPE_WEP)
77
78
79    def navigate_to_page(self, page_number):
80        """
81        Navigates to the page corresponding to the given page number.
82
83        This method performs the translation between a page number and a url to
84        load. This is used internally by apply_settings.
85
86        @param page_number: page number of the page to load
87
88        """
89        # The page is determined by what band we are using. We ignore the input.
90        admin_url = self.admin_interface_url
91        if self.current_band == ap_spec.BAND_2GHZ:
92            self.get_url('%s/Advanced_Wireless2g_Content.asp' % admin_url,
93                         page_title='2.4G')
94        elif self.current_band == ap_spec.BAND_5GHZ:
95            self.get_url('%s/Advanced_Wireless_Content.asp' % admin_url,
96                         page_title='5G')
97        else:
98            raise RuntimeError('Invalid page number passed.  Number of pages '
99                               '%d, page value sent was %d' %
100                               (self.get_number_of_pages(), page_number))
101
102
103    def save_page(self, page_number):
104        """
105        Saves the given page.
106
107        @param page_number: Page number of the page to save.
108
109        """
110        button = self.driver.find_element_by_id('applyButton')
111        button.click()
112        menu_id = 'menu_body' #  id of the table with the main content
113        try:
114            self.wait_for_object_by_id(menu_id)
115        except TimeoutException as e:
116            raise RuntimeError('Unable to find the object by id: %s\n '
117                               'WebDriver exception: %s' % (menu_id, str(e)))
118        self.navigate_to_page(page_number)
119
120
121    def set_mode(self, mode, band=None):
122        #  To avoid the modal dialog
123        self.add_item_to_command_list(self._set_security_disabled, (), 1, 799)
124        self.add_item_to_command_list(self._set_mode, (mode, band), 1, 800)
125
126
127    def _set_mode(self, mode, band=None):
128        xpath = '//select[@name="rt_gmode"]'
129        #  Create the mode to popup item mapping
130        mode_mapping = {ap_spec.MODE_B: 'b Only', ap_spec.MODE_G: 'g Only',
131                        ap_spec.MODE_B | ap_spec.MODE_G: 'b/g Only',
132                        ap_spec.MODE_N: 'n Only', ap_spec.MODE_A: 'a Only'}
133        mode_name = ''
134        if self.current_band == ap_spec.BAND_5GHZ or band == ap_spec.BAND_5GHZ:
135            xpath = '//select[@name="wl_gmode"]'
136        if mode in mode_mapping.keys():
137            mode_name = mode_mapping[mode]
138            if ((mode & ap_spec.MODE_A) and
139                (self.current_band == ap_spec.BAND_2GHZ)):
140                #  a mode only in 5Ghz
141                logging.debug('Mode \'a\' is not available for 2.4Ghz band.')
142                return
143            elif ((mode & (ap_spec.MODE_B | ap_spec.MODE_G) ==
144                  (ap_spec.MODE_B | ap_spec.MODE_G)) or
145                 (mode & ap_spec.MODE_B == ap_spec.MODE_B) or
146                 (mode & ap_spec.MODE_G == ap_spec.MODE_G)) and \
147                 (self.current_band != ap_spec.BAND_2GHZ):
148                #  b/g, b, g mode only in 2.4Ghz
149                logging.debug('Mode \'%s\' is not available for 5Ghz band.',
150                             mode_name)
151                return
152        else:
153            raise RuntimeError('The mode selected \'%s\' is not supported by '
154                               'router %s.' % mode_name, self.name)
155        self.select_item_from_popup_by_xpath(mode_name, xpath,
156            alert_handler=self._invalid_security_handler)
157
158
159    def set_radio(self, enabled):
160        #  We cannot turn off radio on ASUS.
161        return None
162
163
164    def set_ssid(self, ssid):
165        self.add_item_to_command_list(self._set_ssid, (ssid,), 1, 900)
166
167
168    def _set_ssid(self, ssid):
169        xpath = '//input[@maxlength="32" and @name="rt_ssid"]'
170        if self.current_band == ap_spec.BAND_5GHZ:
171            xpath = '//input[@maxlength="32" and @name="wl_ssid"]'
172        self.set_content_of_text_field_by_xpath(ssid, xpath)
173        self._ssid = ssid
174
175
176    def set_channel(self, channel):
177        self.add_item_to_command_list(self._set_channel, (channel,), 1, 900)
178
179
180    def _set_channel(self, channel):
181        position = self._get_channel_popup_position(channel)
182        channel_choices = range(1, 12)
183        xpath = '//select[@name="rt_channel"]'
184        if self.current_band == ap_spec.BAND_5GHZ:
185            xpath = '//select[@name="wl_channel"]'
186            channel_choices = ['36', '40', '44', '48', '149', '153',
187                               '157', '161']
188        self.select_item_from_popup_by_xpath(str(channel_choices[position]),
189                                             xpath)
190
191
192    def set_band(self, band):
193        if band == ap_spec.BAND_2GHZ:
194            self.current_band = ap_spec.BAND_2GHZ
195        elif band == ap_spec.BAND_5GHZ:
196            self.current_band = ap_spec.BAND_5GHZ
197        else:
198            raise RuntimeError('Invalid band sent %s' % band)
199
200
201    def set_security_disabled(self):
202        self.add_item_to_command_list(self._set_security_disabled, (), 1, 1000)
203
204
205    def _set_security_disabled(self):
206        popup = '//select[@name="rt_wep_x"]'
207        if self.current_band == ap_spec.BAND_5GHZ:
208            popup = '//select[@name="wl_wep_x"]'
209        self._set_authentication('Open System', wait_for_xpath=popup)
210        self.select_item_from_popup_by_xpath('None', popup)
211
212
213    def set_security_wep(self, key_value, authentication):
214        self.add_item_to_command_list(self._set_security_wep,
215                                      (key_value, authentication), 1, 1000)
216
217
218    def _set_security_wep(self, key_value, authentication):
219        popup = '//select[@name="rt_wep_x"]'
220        text_field = '//input[@name="rt_phrase_x"]'
221        if self.current_band == ap_spec.BAND_5GHZ:
222            popup = '//select[@name="wl_wep_x"]'
223            text_field = '//input[@name="wl_phrase_x"]'
224        self._set_authentication('Open System', wait_for_xpath=popup)
225        self.select_item_from_popup_by_xpath('WEP-64bits', popup,
226                                             wait_for_xpath=text_field,
227                                             alert_handler=
228                                             self._invalid_security_handler)
229        self.set_content_of_text_field_by_xpath(key_value, text_field,
230                                                abort_check=True)
231
232
233    def set_security_wpapsk(self, security, shared_key, update_interval=1800):
234        #  Asus does not support TKIP (wpapsk) encryption in 'n' mode.
235        #  So we will use AES (wpa2psk) to avoid conflicts and modal dialogs.
236        self.add_item_to_command_list(self._set_security_wpapsk,
237                                      (security, shared_key, update_interval),
238                                       1, 900)
239
240
241    def _set_security_wpapsk(self, security, shared_key, update_interval):
242        key_field = '//input[@name="rt_wpa_psk"]'
243        interval_field = '//input[@name="rt_wpa_gtk_rekey"]'
244        if self.current_band == ap_spec.BAND_5GHZ:
245            key_field = '//input[@name="wl_wpa_psk"]'
246            interval_field = '//input[@name="wl_wpa_gtk_rekey"]'
247        if security == ap_spec.SECURITY_TYPE_WPAPSK:
248            self._set_authentication('WPA-Personal',
249                                     wait_for_xpath=key_field)
250        else:
251            self._set_authentication('WPA2-Personal',
252                                     wait_for_xpath=key_field)
253        self.set_content_of_text_field_by_xpath(shared_key, key_field)
254        self.set_content_of_text_field_by_xpath(str(update_interval),
255                                                interval_field)
256
257
258    def set_visibility(self, visible=True):
259        self.add_item_to_command_list(self._set_visibility,(visible,), 1, 900)
260
261
262    def _set_visibility(self, visible=True):
263        #  value=0 is visible; value=1 is invisible
264        value = 0 if visible else 1
265        xpath = '//input[@name="rt_closed" and @value="%s"]' % value
266        if self.current_band == ap_spec.BAND_5GHZ:
267            xpath = '//input[@name="wl_closed" and @value="%s"]' % value
268        self.click_button_by_xpath(xpath,
269                                   alert_handler=self._invalid_security_handler)
270