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
6import ap_spec
7import linksyse_single_band_configurator
8
9
10class LinksysWRT120NAPConfigurator(linksyse_single_band_configurator.
11                                   LinksyseSingleBandAPConfigurator):
12    """Derived class to control Linksys WRT 120N router."""
13
14
15    def navigate_to_page(self, page_number):
16        if page_number == 1:
17            page_url = urlparse.urljoin(self.admin_interface_url,
18                    'index.stm?title=Wireless-Basic%20Wireless%20Settings')
19            self.get_url(page_url, page_title='Settings')
20        elif page_number == 2:
21            page_url = urlparse.urljoin(self.admin_interface_url,
22                    'index.stm?title=Wireless-Wireless%20Security')
23            self.get_url(page_url, page_title='Security')
24        else:
25            raise RuntimeError('Invalid page number passed. Number of pages '
26                               '%d, page value sent was %d' %
27                               (self.get_number_of_pages(), page_number))
28
29
30    def save_page(self, page_number):
31        save_button = '//input[@name="saveButton"]'
32        self.click_button_by_xpath(save_button, alert_handler=self._sec_alert)
33
34
35    def _set_mode(self, mode, band=None):
36        mode_mapping = {ap_spec.MODE_M:'Mixed',
37                        ap_spec.MODE_B | ap_spec.MODE_G:'BG-Mixed',
38                        ap_spec.MODE_G:'Wireless-G Only',
39                        ap_spec.MODE_B:'Wireless-B Only',
40                        ap_spec.MODE_N:'Wireless-N Only',
41                        'Disabled':' Disabled'}
42        mode_name = mode_mapping.get(mode)
43        if not mode_name:
44            raise RuntimeError('The mode %d not supported by router %s. ',
45                               hex(mode), self.name)
46        xpath = '//select[@name="op_mode"]'
47        self.select_item_from_popup_by_xpath(mode_name, xpath,
48                                             alert_handler=self._sec_alert)
49
50
51    def _set_ssid(self, ssid):
52        xpath = '//input[@name="wl_ssid"]'
53        self.set_content_of_text_field_by_xpath(ssid, xpath, abort_check=False)
54        self._ssid = ssid
55
56
57    def _set_channel(self, channel):
58        position = self._get_channel_popup_position(channel)
59        xpath = '//select[@name="channel"]'
60        channels = ['Auto',
61                    '1 - 2.412GHz', '2 - 2.417GHz', '3 - 2.422GHz',
62                    '4 - 2.427GHz', '5 - 2.432GHz', '6 - 2.437GHz',
63                    '7 - 2.442GHz', '8 - 2.447GHz', '9 - 2.452GHz',
64                    '10 - 2.457GHz', '11 - 2.462GHz']
65        self.select_item_from_popup_by_xpath(channels[position], xpath)
66
67
68    def _set_security_disabled(self):
69        xpath = '//select[@name="sec_mode"]'
70        self.select_item_from_popup_by_xpath('Disabled', xpath)
71
72
73    def _set_security_wep(self, key_value, authentication):
74        popup = '//select[@name="sec_mode"]'
75        self.select_item_from_popup_by_xpath(' WEP ', popup,
76                                             alert_handler=self._sec_alert)
77        text = '//input[@name="passPhrase"]'
78        self.set_content_of_text_field_by_xpath(key_value, text,
79                                                abort_check=True)
80        xpath = '//input[@name="generate_key"]'
81        self.click_button_by_xpath(xpath, alert_handler=self._sec_alert)
82
83
84    def _set_security_wpapsk(self, security, shared_key, update_interval=None):
85        popup = '//select[@name="sec_mode"]'
86        self.wait_for_object_by_xpath(popup)
87        if security == ap_spec.SECURITY_TYPE_WPAPSK:
88            wpa_item = 'WPA Personal'
89        else:
90            wpa_item = 'WPA2 Personal'
91        self.select_item_from_popup_by_xpath(wpa_item, popup,
92                                             alert_handler=self._sec_alert)
93        text = '//input[@name="sharedkey"]'
94        self.set_content_of_text_field_by_xpath(shared_key, text,
95                                                abort_check=True)
96        key = '//input[@name="group_key_second"]'
97        if update_interval:
98            self.set_content_of_text_field_by_xpath(shared_key, key,
99                                                    abort_check=True)
100
101
102    def is_update_interval_supported(self):
103        """
104        Returns True if setting the PSK refresh interval is supported.
105
106        @return True is supported; False otherwise
107        """
108        return True
109
110
111    def _set_visibility(self, visible=True):
112        int_value = 1 if visible else 0
113        xpath = ('//input[@value="%d" and @name="wlan_broadcast"]' % int_value)
114        self.click_button_by_xpath(xpath, alert_handler=self._sec_alert)
115