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
5import urlparse
6
7import ap_spec
8import linksyse_dual_band_configurator
9
10
11class Linksyse2500APConfigurator(linksyse_dual_band_configurator.
12                                 LinksyseDualBandAPConfigurator):
13    """Derived class to control Linksys E2500 router."""
14
15
16    def get_number_of_pages(self):
17        return 2
18
19
20    def get_supported_modes(self):
21        return [{'band': ap_spec.BAND_2GHZ,
22                 'modes': [ap_spec.MODE_B, ap_spec.MODE_N, ap_spec.MODE_B |
23                           ap_spec.MODE_G, ap_spec.MODE_G, ap_spec.MODE_M]},
24                {'band': ap_spec.BAND_5GHZ,
25                 'modes': [ap_spec.MODE_A, ap_spec.MODE_N, ap_spec.MODE_M]}]
26
27
28    def is_security_mode_supported(self, security_mode):
29        """Returns if the passed in security mode is supported.
30
31        @param security_mode: one of the supported security methods defined
32                              in APSpec.
33
34        @returns True is suppported; False otherwise.
35        """
36        if self.current_band == ap_spec.BAND_5GHZ:
37            return security_mode in (ap_spec.SECURITY_TYPE_DISABLED,
38                                     ap_spec.SECURITY_TYPE_WPAPSK,
39                                     ap_spec.SECURITY_TYPE_WPA2PSK)
40        return security_mode in (ap_spec.SECURITY_TYPE_DISABLED,
41                                 ap_spec.SECURITY_TYPE_WPAPSK,
42                                 ap_spec.SECURITY_TYPE_WPA2PSK,
43                                 ap_spec.SECURITY_TYPE_WEP)
44
45
46    def navigate_to_page(self, page_number):
47        """Navigate to the passed in page.
48
49        @param page_number: the page number as an integer
50        """
51        if page_number == 1:
52            page_url = urlparse.urljoin(self.admin_interface_url,
53                                        'Wireless_Basic.asp')
54            self.get_url(page_url, page_title='Settings')
55        elif page_number == 2:
56            page_url = urlparse.urljoin(self.admin_interface_url,
57                                        'WL_WPATable.asp')
58            self.get_url(page_url, page_title='Security')
59        else:
60            raise RuntimeError('Invalid page number passed. Number of pages '
61                               '%d, page value sent was %d' %
62                               (self.get_number_of_pages(), page_number))
63
64
65    def _set_mode(self, mode, band=None):
66        mode_mapping = {ap_spec.MODE_B: 'Wireless-B Only',
67                        ap_spec.MODE_G: 'Wireless-G Only',
68                        ap_spec.MODE_B | ap_spec.MODE_G: 'Wireless-B/G Only',
69                        ap_spec.MODE_N: 'Wireless-N Only',
70                        ap_spec.MODE_A: 'Wireless-A Only',
71                        ap_spec.MODE_M: 'Mixed'}
72        xpath = '//select[@name="net_mode_24g"]'
73        if self.current_band == ap_spec.BAND_5GHZ or band == ap_spec.BAND_5GHZ:
74            self.current_band = ap_spec.BAND_5GHZ
75            xpath = '//select[@name="net_mode_5g"]'
76        mode_name = ''
77        if mode in mode_mapping.keys():
78            mode_name = mode_mapping[mode]
79        else:
80            raise RuntimeError('The mode selected %d is not supported by router'
81                               ' %s.', hex(mode), self.name)
82        self.select_item_from_popup_by_xpath(mode_name, xpath,
83                                             alert_handler=self._alert_handler)
84
85
86    def _set_ssid(self, ssid):
87        xpath = '//input[@maxlength="32" and @name="ssid_24g"]'
88        if self.current_band == ap_spec.BAND_5GHZ:
89            xpath = '//input[@maxlength="32" and @name="ssid_5g"]'
90        self.set_content_of_text_field_by_xpath(ssid, xpath)
91        self._ssid = ssid
92
93
94    def _set_channel(self, channel):
95        position = self._get_channel_popup_position(channel)
96        channel_choices = ['Auto',
97                           '1 - 2.412GHZ', '2 - 2.417GHZ', '3 - 2.422GHZ',
98                           '4 - 2.427GHZ', '5 - 2.432GHZ', '6 - 2.437GHZ',
99                           '7 - 2.442GHZ', '8 - 2.447GHZ', '9 - 2.452GHZ',
100                           '10 - 2.457GHZ', '11 - 2.462GHZ']
101        xpath = '//select[@name="_wl0_channel"]'
102        if self.current_band == ap_spec.BAND_5GHZ:
103            xpath = '//select[@name="_wl1_channel"]'
104            channel_choices = ['Auto (DFS)',
105                               '36 - 5.180GHz', '40 - 5.200GHz',
106                               '44 - 5.220GHz', '48 - 5.240GHz',
107                               '149 - 5.745GHz', '153 - 5.765GHz',
108                               '157 - 5.785GHz', '161 - 5.805GHz']
109        self.select_item_from_popup_by_xpath(channel_choices[position], xpath,
110                                             alert_handler=self._alert_handler)
111
112
113    def set_security_disabled(self):
114        self.add_item_to_command_list(self._set_security_disabled, (), 2, 900)
115
116
117    def set_security_wep(self, key_value, authentication):
118        self.add_item_to_command_list(self._set_security_wep,
119                                      (key_value, authentication), 2, 900)
120
121
122    def set_security_wpapsk(self, security, shared_key, update_interval=None):
123        self.add_item_to_command_list(self._set_security_wpapsk,
124                                      (security, shared_key, update_interval),
125                                       2, 900)
126
127
128    def _set_visibility(self, visible=True):
129        button = 'closed_24g'
130        if self.current_band == ap_spec.BAND_5GHZ:
131            button = 'closed_5g'
132        int_value = 0 if visible else 1
133        xpath = ('//input[@value="%d" and @name="%s"]' % (int_value, button))
134        self.click_button_by_xpath(xpath)
135