1# Copyright (c) 2013 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 Asus QIS router."""
6
7import ap_spec
8import asus_ap_configurator
9
10
11class AsusQISAPConfigurator(asus_ap_configurator.AsusAPConfigurator):
12    """Derived class for Asus routers with the Quick Internet Setup UI."""
13
14
15    def _set_authentication(self, authentication, wait_for_xpath=None):
16        """Sets the authentication method in the popup.
17
18        @param authentication: authentication method to select.
19        @param wait_for_path: item to wait for before returning.
20        """
21        auth = '//select[@name="wl_auth_mode_x"]'
22        self.select_item_from_popup_by_xpath(authentication, auth,
23            wait_for_xpath, alert_handler=self._invalid_security_handler)
24
25
26    def navigate_to_page(self, page_number):
27        """Navigates to the given page number.
28
29        @param page_number: integer of page to navigate to
30        """
31        self.get_url('%s/Advanced_Wireless_Content.asp' %
32                     self.admin_interface_url, page_title='General')
33
34
35    def get_number_of_pages(self):
36        return 1
37
38
39    def save_page(self, page_number):
40        """Save the given page.
41
42        @param page_number: integer of the page to save
43        """
44        self.click_button_by_id('applyButton')
45        ssid = '//input[@name="wl_ssid"]'
46        self.wait_for_objects_by_xpath([ssid])
47
48
49    def get_supported_bands(self):
50        return [{'band': ap_spec.BAND_2GHZ,
51                 'channels': ['Auto', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]},
52                {'band': ap_spec.BAND_5GHZ,
53                 'channels': ['Auto', 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_N, ap_spec.MODE_AUTO]},
59                {'band': ap_spec.BAND_5GHZ,
60                 'modes': [ap_spec.MODE_N, ap_spec.MODE_AUTO]}]
61
62
63    def set_mode(self, mode, band=None):
64        # To avoid the modal dialog.
65        self.add_item_to_command_list(self._set_security_disabled, (), 1, 799)
66        self.add_item_to_command_list(self._set_mode, (mode, band), 1, 800)
67
68
69    def _set_mode(self, mode, band=None):
70        if band:
71            self._set_band(band)
72        if mode == ap_spec.MODE_AUTO:
73            mode_popup = 'Auto'
74        elif mode == ap_spec.MODE_N:
75            mode_popup = 'N Only'
76        else:
77            raise RuntimeError('Invalid mode passed %x' % mode)
78        xpath = '//select[@name="wl_nmode_x"]'
79        self.wait_for_object_by_xpath(xpath)
80        self.select_item_from_popup_by_xpath(mode_popup, xpath,
81             alert_handler=self._invalid_security_handler)
82
83
84    def set_ssid(self, ssid):
85        self.add_item_to_command_list(self._set_ssid, (ssid,), 1, 900)
86
87
88    def _set_ssid(self, ssid):
89        xpath = '//input[@maxlength="32" and @name="wl_ssid"]'
90        self.set_content_of_text_field_by_xpath(ssid, xpath)
91        self._ssid = ssid
92
93
94    def set_channel(self, channel):
95        self.add_item_to_command_list(self._set_channel, (channel,), 1, 900)
96
97
98    def _set_channel(self, channel):
99        position = self._get_channel_popup_position(channel)
100        channel_choices = ['Auto', '1', '2', '3', '4', '5', '6',
101                           '7', '8', '9', '10', '11']
102        xpath = '//select[@name="wl_channel"]'
103        if self.current_band == ap_spec.BAND_5GHZ:
104            channel_choices = ['Auto', '36', '40', '44', '48', '149', '153',
105                               '157', '161']
106        self.select_item_from_popup_by_xpath(str(channel_choices[position]),
107                                             xpath)
108
109
110    def set_band(self, band):
111        if band == ap_spec.BAND_2GHZ:
112            self.current_band = ap_spec.BAND_2GHZ
113            band_popup = '2.4GHz'
114        elif band == ap_spec.BAND_5GHZ:
115            self.current_band = ap_spec.BAND_5GHZ
116            band_popup = '5GHz'
117        else:
118            raise RuntimeError('Invalid band sent %s' % band)
119        self.add_item_to_command_list(self._set_band, (band_popup,), 1, 800)
120
121
122    def _set_band(self, band):
123        xpath = '//select[@name="wl_unit"]'
124        self.select_item_from_popup_by_xpath(band, xpath)
125
126
127    def set_security_disabled(self):
128        self.add_item_to_command_list(self._set_security_disabled, (), 1, 1000)
129
130
131    def _set_security_disabled(self):
132        self._set_authentication('Open System')
133
134
135    def set_security_wep(self, key_value, authentication):
136        self.add_item_to_command_list(self._set_security_wep,
137                                      (key_value, authentication), 1, 1000)
138
139
140    def _set_security_wep(self, key_value, authentication):
141        popup = '//select[@name="wl_wep_x"]'
142        text_field = '//input[@name="wl_phrase_x"]'
143        self._set_authentication('Shared Key', wait_for_xpath=popup)
144        self.select_item_from_popup_by_xpath('WEP-64bits', popup,
145                                             wait_for_xpath=text_field,
146                           alert_handler=self._invalid_security_handler)
147        self.set_content_of_text_field_by_xpath(key_value, text_field,
148                                                abort_check=True)
149
150    def set_security_wpapsk(self, security, shared_key, update_interval=1800):
151        #  Asus does not support TKIP (wpapsk) encryption in 'n' mode.
152        #  Therefore security will fall back to wpa2psk in 'n' mode.
153        self.add_item_to_command_list(self._set_security_wpapsk,
154                                      (security, shared_key, update_interval),
155                                       1, 1000)
156
157
158    def _set_security_wpapsk(self, security, shared_key, update_interval):
159        popup = '//select[@name="wl_crypto"]'
160        key_field = '//input[@name="wl_wpa_psk"]'
161        interval_field = '//input[@name="wl_wpa_gtk_rekey"]'
162        if security == ap_spec.SECURITY_TYPE_WPAPSK:
163            self._set_authentication('WPA-Personal', wait_for_xpath=key_field,
164                                   alert_handler=self._invalid_security_handler)
165        else:
166            self._set_authentication('WPA2-Personal', wait_for_xpath=key_field,
167                                   alert_handler=self._invalid_security_handler)
168        self.set_content_of_text_field_by_xpath(shared_key, key_field)
169        self.set_content_of_text_field_by_xpath(str(update_interval),
170                                                interval_field)
171
172
173    def set_visibility(self, visible=True):
174        self.add_item_to_command_list(self._set_visibility,(visible,), 1, 900)
175
176
177    def _set_visibility(self, visible=True):
178        value = 0 if visible else 1
179        xpath = '//input[@name="wl_closed" and @value="%s"]' % value
180        self.click_button_by_xpath(xpath)
181