1# Copyright (c) 2014 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
5import urlparse
6
7import ap_spec
8import linksyse_dual_band_configurator
9
10
11class LinksysWRT600APConfigurator(linksyse_dual_band_configurator.
12                                  LinksyseDualBandAPConfigurator):
13    """Derived class to control Linksys wrt600 router."""
14
15
16    def _sec_alert(self, alert):
17        text = alert.text
18        if 'Your wireless security mode is not compatible with' in text:
19            alert.accept()
20        elif 'WARNING: Your Wireless-N devices will only operate' in text:
21            alert.accept()
22        else:
23            self._alert_handler(alert)
24
25
26    def get_number_of_pages(self):
27        return 2
28
29
30    def get_supported_modes(self):
31        return [{'band': ap_spec.BAND_2GHZ,
32                 'modes': [ap_spec.MODE_B, ap_spec.MODE_N,
33                           ap_spec.MODE_G, ap_spec.MODE_M]},
34                {'band': ap_spec.BAND_5GHZ,
35                 'modes': [ap_spec.MODE_A, ap_spec.MODE_N, ap_spec.MODE_M]}]
36
37
38    def is_security_mode_supported(self, security_mode):
39        """Returns if the passed in security mode is supported.
40
41        @param security_mode: one of the supported security methods defined
42                              in APSpec.
43
44        @returns True is suppported; False otherwise.
45        """
46        return security_mode in (ap_spec.SECURITY_TYPE_DISABLED,
47                                 ap_spec.SECURITY_TYPE_WPAPSK,
48                                 ap_spec.SECURITY_TYPE_WPA2PSK,
49                                 ap_spec.SECURITY_TYPE_WEP)
50
51
52    def navigate_to_page(self, page_number):
53        """Navigate to the passed in page.
54
55        @param page_number: the page number as an integer
56        """
57        if page_number == 1:
58            page_url = urlparse.urljoin(self.admin_interface_url,
59                                        'BasicWirelessSettings.htm')
60            self.get_url(page_url, page_title='Settings')
61        elif page_number == 2:
62            page_url = urlparse.urljoin(self.admin_interface_url,
63                                        'WirelessSecurity.htm')
64            self.get_url(page_url, page_title='Security')
65        else:
66            raise RuntimeError('Invalid page number passed. Number of pages '
67                               '%d, page value sent was %d' %
68                               (self.get_number_of_pages(), page_number))
69
70
71    def save_page(self, page_number):
72        self.driver.switch_to_default_content()
73        submit_btn = '//a[@href="javascript:ValidateForm(document.forms[0]);"]'
74        if page_number == 1:
75            submit_btn='//a[@href="javascript:Try_Submit(document.forms[0]);"]'
76        self.click_button_by_xpath(submit_btn,
77                                   alert_handler=self._alert_handler)
78        self.wait_for_object_by_xpath(submit_btn, wait_time=20)
79
80
81    def _set_mode(self, mode, band=None):
82        mode_mapping = {ap_spec.MODE_N: ' Wireless-N Only',
83                        ap_spec.MODE_M: ' Mixed',
84                        'Disabled': ' Disabled'}
85        xpath = '//select[@name="wl_mode"]'
86        if self.current_band == ap_spec.BAND_2GHZ:
87            mode_mapping[ap_spec.MODE_G] = ' Wireless-G Only'
88            mode_mapping[ap_spec.MODE_B] = ' Wireless-B Only'
89        elif self.current_band == ap_spec.BAND_5GHZ:
90            xpath = '//select[@name="wl_mode_1"]'
91            mode_mapping[ap_spec.MODE_A] = 'Wireless-A Only'
92            mode_mapping[ap_spec.MODE_N] = 'Wireless-N Only'
93        mode_name = ''
94        if mode in mode_mapping.keys():
95            mode_name = mode_mapping[mode]
96        else:
97            raise RuntimeError('The mode selected %d is not supported by router'
98                               ' %s.', hex(mode), self.name)
99        self.select_item_from_popup_by_xpath(mode_name, xpath)
100
101
102    def _set_ssid(self, ssid):
103        xpath = '//input[@name="wl_ssid"]'
104        if self.current_band == ap_spec.BAND_5GHZ:
105            xpath = '//input[@name="wl_ssid_1"]'
106        self.set_content_of_text_field_by_xpath(ssid, xpath)
107        self._ssid = ssid
108
109
110    def _set_channel(self, channel):
111        position = self._get_channel_popup_position(channel)
112        channel_choices = ['Auto',
113                           '1 - 2.412GHz', '2 - 2.417GHz', '3 - 2.422GHz',
114                           '4 - 2.427GHz', '5 - 2.432GHz', '6 - 2.437GHz',
115                           '7 - 2.442GHz', '8 - 2.447GHz', '9 - 2.452GHz',
116                           '10 - 2.457GHz', '11 - 2.462GHz']
117        xpath = '//select[@name="wl_channel"]'
118        if self.current_band == ap_spec.BAND_5GHZ:
119            xpath = '//select[@name="wl_channel_1"]'
120            channel_choices = ['Auto (DFS)',
121                               '36 - 5.180GHz', '40 - 5.200GHz',
122                               '44 - 5.220GHz', '48 - 5.240GHz',
123                               '149 - 5.745GHz', '153 - 5.765GHz',
124                               '157 - 5.785GHz', '161 - 5.805GHz']
125        self.select_item_from_popup_by_xpath(channel_choices[position],
126                                             xpath)
127
128
129    def _set_security(self, sec_type, look_for=None):
130        xpath = ('//select[@name="wl_security" and \
131                  @onchange="WirelessSecurityType ( this.value )"]')
132        self.driver.switch_to_default_content()
133        self.driver.switch_to_frame('SSIDAuthMode')
134        if self.current_band == ap_spec.BAND_5GHZ:
135            xpath = ('//select[@name="wl_security" and \
136                      @onchange="WirelessSecurityType1 ( this.value )"]')
137            self.driver.switch_to_default_content()
138            self.driver.switch_to_frame('SSIDAuthMode1')
139        self.wait_for_object_by_xpath(xpath)
140        self.select_item_from_popup_by_xpath(sec_type, xpath,
141                                             wait_for_xpath=look_for,
142                                             alert_handler=self._sec_alert)
143
144
145    def set_security_disabled(self):
146        self.add_item_to_command_list(self._set_security_disabled, (), 2, 900)
147
148
149    def _set_security_disabled(self):
150        self._set_security(' Disabled')
151
152
153    def set_security_wep(self, key_value, authentication):
154        self.add_item_to_command_list(self._set_security_wep,
155                                      (key_value, authentication), 2, 900)
156
157
158    def _set_security_wep(self, key_value, authentication):
159        text_field = '//input[@name="passphrase_key"]'
160        xpath = '//input[@name="GenerateBtn"]'
161        self._set_security(' WEP', look_for=text_field)
162        self.set_content_of_text_field_by_xpath(key_value, text_field,
163                                                abort_check=True)
164        self.click_button_by_xpath(xpath, alert_handler=self._sec_alert)
165        self.wait_for_object_by_xpath('//input[@name="wl_key4"]')
166
167
168    def set_security_wpapsk(self, security, shared_key, update_interval=None):
169        self.add_item_to_command_list(self._set_security_wpapsk,
170                                      (security, shared_key, update_interval),
171                                       2, 900)
172
173
174    def _set_security_wpapsk(self, security, shared_key, update_interval=None):
175        if security == ap_spec.SECURITY_TYPE_WPAPSK:
176            wpa_item = ' WPA-Personal'
177        else:
178            wpa_item = ' WPA2-Personal'
179        key_field = '//input[@name="wl_wpa_psk"]'
180        self._set_security(wpa_item, look_for=key_field)
181        self.set_content_of_text_field_by_xpath(shared_key, key_field,
182                                                abort_check=True)
183        if update_interval:
184            self.set_content_of_text_field_by_xpath(
185                                    '//input[@name="key_renewal"]', key_field,
186                                    abort_check=True)
187
188
189    def is_update_interval_supported(self):
190        """
191        Returns True if setting the PSK refresh interval is supported.
192
193        @return True is supported; False otherwise
194        """
195        return True
196
197
198    def _set_visibility(self, visible=True):
199        int_value = 0 if visible else 1
200        xpath = ('//input[@value="%d" and @name="wl_hide_ssid"]' % int_value)
201        self.click_button_by_xpath(xpath)
202