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"""Subclass of the APConfigurator"""
6
7import logging
8import urlparse
9import dynamic_ap_configurator
10import ap_spec
11
12
13class Linksyse2000APConfigurator(
14        dynamic_ap_configurator.DynamicAPConfigurator):
15    """Derived class to control Linksyse2000 AP"""
16
17    def _sec_alert(self, alert):
18        text = alert.text
19        if 'Your wireless security mode is not compatible with' in text:
20            alert.accept()
21        elif 'WARNING: Your Wireless-N devices will only operate' in text:
22            alert.accept()
23        else:
24            raise RuntimeError('Unhandled alert : %s' % text)
25
26
27    def get_number_of_pages(self):
28        return 2
29
30
31    def get_supported_modes(self):
32        return [{'band': ap_spec.BAND_2GHZ,
33                 'modes': [ap_spec.MODE_M, ap_spec.MODE_B | ap_spec.MODE_G,
34                           ap_spec.MODE_G, ap_spec.MODE_B, ap_spec.MODE_N,
35                           ap_spec.MODE_D]},
36                {'band': ap_spec.BAND_5GHZ,
37                 'modes': [ap_spec.MODE_M, ap_spec.MODE_A, ap_spec.MODE_N,
38                           ap_spec.MODE_D]}]
39
40
41    def get_supported_bands(self):
42        return [{'band': ap_spec.BAND_2GHZ,
43                 'channels': ['Auto', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]},
44                {'band': ap_spec.BAND_5GHZ,
45                 'channels': ['Auto', 36, 40, 44, 48, 149, 153, 157, 161]}]
46
47
48    def is_security_mode_supported(self, security_mode):
49        return security_mode in (ap_spec.SECURITY_TYPE_DISABLED,
50                                 ap_spec.SECURITY_TYPE_WPAPSK,
51                                 ap_spec.SECURITY_TYPE_WPA2PSK,
52                                 ap_spec.SECURITY_TYPE_WEP)
53
54
55    def navigate_to_page(self, page_number):
56        if page_number == 1:
57            page_url = urlparse.urljoin(self.admin_interface_url,
58                                        'Wireless_Basic.asp')
59            self.get_url(page_url, page_title='Settings')
60        elif page_number == 2:
61            page_url = urlparse.urljoin(self.admin_interface_url,
62                                        'WL_WPATable.asp')
63            self.get_url(page_url, page_title='Security')
64        else:
65            raise RuntimeError('Invalid page number passed. Number of pages '
66                               '%d, page value sent was %d' %
67                               (self.get_number_of_pages(), page_number))
68
69
70    def save_page(self, page_number):
71        xpath = '//a[text()="Save Settings"]'
72        button = self.driver.find_element_by_xpath(xpath)
73        button.click()
74        button_xpath = '//input[@name="btaction"]'
75        if self.wait_for_object_by_xpath(button_xpath):
76            button = self.driver.find_element_by_xpath(button_xpath)
77            button.click()
78
79
80    def set_mode(self, mode, band=None):
81        if band:
82            self.add_item_to_command_list(self._set_band, (band,), 1, 700)
83        self.add_item_to_command_list(self._set_mode, (mode, band), 1, 800)
84
85
86    def _set_mode(self, mode, band=None):
87        xpath = '//select[@name="wl_net_mode_24g"]'
88        mode_mapping = {ap_spec.MODE_M:'Mixed',
89                        ap_spec.MODE_B | ap_spec.MODE_G:'BG-Mixed',
90                        ap_spec.MODE_G:'Wireless-G Only',
91                        ap_spec.MODE_B:'Wireless-B Only',
92                        ap_spec.MODE_N:'Wireless-N Only',
93                        ap_spec.MODE_D:'Disabled',
94                        ap_spec.MODE_A:'Wireless-A Only'}
95        if self.current_band == ap_spec.BAND_5GHZ or band == ap_spec.BAND_5GHZ:
96            xpath = '//select[@name="wl_net_mode_5g"]'
97        mode_name = ''
98        if mode in mode_mapping.keys():
99            mode_name = mode_mapping[mode]
100            if ((mode & ap_spec.MODE_A) and
101                (self.current_band == ap_spec.BAND_2GHZ)):
102               #  a mode only in 5Ghz
103                logging.debug('Mode \'a\' is not available for 2.4Ghz band.')
104                return
105            elif ((mode & (ap_spec.MODE_B | ap_spec.MODE_G) ==
106                  (ap_spec.MODE_B | ap_spec.MODE_G)) or
107                  (mode & ap_spec.MODE_B == ap_spec.MODE_B) or
108                  (mode & ap_spec.MODE_G == ap_spec.MODE_G)) and \
109                  (self.current_band != ap_spec.BAND_2GHZ):
110               #  b/g, b, g mode only in 2.4Ghz
111                logging.debug('Mode \'%s\' is not available for 5Ghz band.',
112                               mode_name)
113                return
114        else:
115            raise RuntimeError("The mode %s is not supported" % mode_name)
116        self.select_item_from_popup_by_xpath(mode_name, xpath,
117                                             alert_handler=self._sec_alert)
118
119
120    def set_ssid(self, ssid):
121        self.add_item_to_command_list(self._set_ssid, (ssid,), 1, 900)
122
123
124    def _set_ssid(self, ssid):
125        xpath = '//input[@maxlength="32" and @name="wl_ssid_24g"]'
126        mode = '//select[@name="wl_net_mode_24g"]'
127        if self.current_band == ap_spec.BAND_5GHZ:
128            xpath = '//input[@maxlength="32" and @name="wl_ssid_5g"]'
129            mode = '//select[@name="wl_net_mode_5g"]'
130        ssid_field = self.driver.find_element_by_xpath(xpath)
131        if ssid_field.get_attribute('disabled') == 'true':
132            # This means the mode is disabled, so we have to set it to something
133            # so we can fill in the SSID
134            self.select_item_from_popup_by_xpath('Mixed', mode,
135                                                 alert_handler=self._sec_alert)
136        self.set_content_of_text_field_by_xpath(ssid, xpath)
137        self._ssid = ssid
138
139
140    def set_channel(self, channel):
141        self.add_item_to_command_list(self._set_channel, (channel,), 1, 900)
142
143
144    def _set_channel(self, channel):
145        position = self._get_channel_popup_position(channel)
146        xpath = '//select[@name="wl_schannel"]'
147        channels = ['Auto', '1', '2', '3', '4', '5', '6', '7', '8', '9',
148                    '10', '11']
149        if self.current_band == ap_spec.BAND_5GHZ:
150            xpath = '//select[@name="wl_schannel"]'
151            channels = ['Auto', '36', '40', '44', '48', '149', '153',
152                        '157', '161']
153        self.select_item_from_popup_by_xpath(channels[position], xpath)
154
155
156    def set_ch_width(self, channel_wid):
157        """
158        Set the channel width
159
160        @param channel_wid: the required channel width
161        """
162        self.add_item_to_command_list(self._set_ch_width,(channel_wid,),
163                                      1, 900)
164
165
166    def _set_ch_width(self, channel_wid):
167        channel_width_choice = ['Auto (20MHz or 40MHz)', '20MHz only']
168        xpath = '//select[@name="_wl_nbw"]'
169        if self.current_band == ap_spec.BAND_5GHZ:
170            channel_width_choice = ['Auto (20MHz or 40MHz)', '20MHz only',
171                                    '40MHz only']
172            xpath = '//select[@name="_wl_nbw"]'
173        self.select_item_from_popup_by_xpath(channel_width_choice[channel_wid],
174                                             xpath)
175
176
177    def set_band(self, band):
178        self.add_item_to_command_list(self._set_band, (band,), 1, 800)
179
180
181    def _set_band(self, band):
182        if band == ap_spec.BAND_2GHZ:
183            self.current_band = ap_spec.BAND_2GHZ
184            xpath = '//input[@name="wl_sband" and @value="24g"]'
185            element = self.driver.find_element_by_xpath(xpath)
186            element.click()
187        elif band == ap_spec.BAND_5GHZ:
188            self.current_band = ap_spec.BAND_5GHZ
189            xpath = '//input[@name="wl_sband" and @value="5g"]'
190            element = self.driver.find_element_by_xpath(xpath)
191            element.click()
192        else:
193            raise RuntimeError('Invalid band %s' % band)
194
195
196    def set_radio(self, enabled=True):
197        return None
198
199
200    def set_security_disabled(self):
201        self.add_item_to_command_list(self._set_security_disabled, (), 2, 1000)
202
203
204    def _set_security_disabled(self):
205        xpath = '//select[@name="security_mode2"]'
206        self.select_item_from_popup_by_xpath('Disabled', xpath)
207
208
209    def set_security_wep(self, key_value, authentication):
210        self.add_item_to_command_list(self._set_security_wep,
211                                      (key_value, authentication), 2, 1000)
212
213
214    def _set_security_wep(self, key_value, authentication):
215        # WEP and WPA-Personal are not supported for Wireless-N only mode
216        # and Mixed mode.
217        # WEP and WPA-Personal do not show up in the list, no alert is thrown.
218        popup = '//select[@name="security_mode2"]'
219        if not self.item_in_popup_by_xpath_exist('WEP', popup):
220            raise RuntimeError ('Unable to find wep security item in popup.  '
221                                'Is the mode set to N?')
222        self.select_item_from_popup_by_xpath('WEP', popup,
223                                             alert_handler=self._sec_alert)
224        text = '//input[@name="wl_passphrase"]'
225        self.set_content_of_text_field_by_xpath(key_value, text,
226                                                abort_check=True)
227        xpath = '//input[@value="Generate"]'
228        self.click_button_by_xpath(xpath, alert_handler=self._sec_alert)
229
230
231    def set_security_wpapsk(self, security, shared_key, update_interval=None):
232        # WEP and WPA-Personal are not supported for Wireless-N only mode,
233        self.add_item_to_command_list(self._set_security_wpapsk,
234                                      (security, shared_key,), 2, 900)
235
236
237    def _set_security_wpapsk(self, security, shared_key):
238        popup = '//select[@name="security_mode2"]'
239        if security == ap_spec.SECURITY_TYPE_WPAPSK:
240            wpa_item = 'WPA Personal'
241        else:
242            wpa_item = 'WPA2 Personal'
243        self.select_item_from_popup_by_xpath(wpa_item, popup,
244                                             alert_handler=self._sec_alert)
245        text = '//input[@name="wl_wpa_psk"]'
246        self.set_content_of_text_field_by_xpath(shared_key, text,
247                                                abort_check=True)
248
249
250    def is_update_interval_supported(self):
251        """
252        Returns True if setting the PSK refresh interval is supported.
253
254        @return True is supported; False otherwise
255        """
256        return False
257
258
259    def set_visibility(self, visible=True):
260        self.add_item_to_command_list(self._set_visibility, (visible,), 1, 900)
261
262
263    def _set_visibility(self, visible=True):
264        value = 0 if visible else 1
265        xpath = '//input[@name="wl_closed_24g" and @value="%s"]' % value
266        if self.current_band == ap_spec.BAND_5GHZ:
267            xpath = '//input[@name="wl_closed_5g" and @value="%s"]' % value
268        self.click_button_by_xpath(xpath)
269