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
5import os
6
7import ap_spec
8import trendnet_ap_configurator
9
10from selenium.common.exceptions import WebDriverException
11
12class Trendnet692grAPConfigurator(trendnet_ap_configurator.
13                                  TrendnetAPConfigurator):
14    """Derived class to control the Trendnet TEW-692GR."""
15
16
17    def _alert_handler(self, alert):
18        """Checks for any modal dialogs which popup to alert the user and
19        either raises a RuntimeError or ignores the alert.
20
21        @param alert: The modal dialog's contents.
22        """
23        text = alert.text
24        if 'Please input 5 or 13 characters of WEP key1' in text:
25            alert.accept()
26            raise RuntimeError(text)
27
28
29    def get_number_of_pages(self):
30        return 2
31
32
33    def save_page(self, page_number):
34        """Save the given page.
35
36        @param page_number: the page to save.
37        """
38        xpath = ('//input[@class="button_submit" and @value="Apply"]')
39        element = self.wait_for_object_by_xpath(xpath)
40        if element and element.is_displayed():
41            self.click_button_by_xpath(xpath, alert_handler=self._alert_handler)
42            # Reboot the device.
43            reboot_button = '//input[@value="Reboot the Device"]'
44            if self.object_by_xpath_exist(reboot_button):
45                self.click_button_by_xpath(reboot_button)
46                self.wait_for_progress_bar()
47        else:
48            pass
49
50
51    def get_supported_bands(self):
52        return [{'band': ap_spec.BAND_2GHZ,
53                 'channels': range(1, 12)},
54                {'band': ap_spec.BAND_5GHZ,
55                 'channels':[36, 40, 44, 48, 149, 153, 157, 161]}]
56
57
58    def get_supported_modes(self):
59        return [{'band': ap_spec.BAND_2GHZ,
60                 'modes': [ap_spec.MODE_N,
61                           ap_spec.MODE_B | ap_spec.MODE_G,
62                           ap_spec.MODE_B | ap_spec.MODE_G | ap_spec.MODE_N]},
63                {'band': ap_spec.BAND_5GHZ,
64                 'modes': [ap_spec.MODE_A | ap_spec.MODE_N, ap_spec.MODE_A]}]
65
66
67    def navigate_to_page(self, page_number):
68        """Navigates to the given page.
69
70        @param page_number: the page to navigate to.
71        """
72        # All settings are on the same page, so we always open the config page
73        if self.current_band == ap_spec.BAND_2GHZ:
74            if page_number == 1:
75                page_url = os.path.join(self.admin_interface_url ,
76                                        'wireless/basic.asp')
77            elif page_number == 2:
78                page_url = os.path.join(self.admin_interface_url ,
79                                        'wireless/security.asp')
80            else:
81                raise RuntimeError('Invalid page number passed. Number of pages'
82                                   '%d, page value sent was %d' %
83                                   (self.get_number_of_pages(), page_number))
84        elif self.current_band == ap_spec.BAND_5GHZ:
85            if page_number == 1:
86                page_url = os.path.join(self.admin_interface_url ,
87                                        'wireless2/basic.asp')
88            elif page_number == 2:
89                page_url = os.path.join(self.admin_interface_url ,
90                                        'wireless2/security.asp')
91            else:
92                raise RuntimeError('Invalid page number passed. Number of pages'
93                                   '%d, page value sent was %d' %
94                                    (self.get_number_of_pages(), page_number))
95        else:
96            raise RuntimeError('Incorrect band band = %s' % self.current_band)
97        try:
98            self.get_url(page_url, page_title='TEW-692GR')
99        except WebDriverException, e:
100            if "unexpected alert open" in str(e):
101                alert = self.driver.switch_to_alert()
102                self._alert_handler(alert)
103            else:
104                raise WebDriverException(str(e))
105
106
107    def is_update_interval_supported(self):
108        """
109        Returns True if setting the PSK refresh interval is supported.
110
111        @return True is supported; False otherwise
112        """
113        return True
114
115
116    def _set_mode(self, mode, band=None):
117        # Create the mode to popup item mapping
118        mode_mapping = {ap_spec.MODE_B | ap_spec.MODE_G | ap_spec.MODE_N:
119                        '2.4GHz 802.11 b/g/n mixed mode',
120                        ap_spec.MODE_B | ap_spec.MODE_G:
121                        '2.4GHz 802.11 b/g mixed mode',
122                        ap_spec.MODE_N: '2.4GHz 802.11 n only'}
123
124        if self.current_band == ap_spec.BAND_5GHZ:
125            mode_mapping = {ap_spec.MODE_A: '5GHz 802.11 a only',
126                            ap_spec.MODE_A | ap_spec.MODE_N:
127                            '5GHz 802.11 a/n mixed mode'}
128        mode_name = ''
129        if mode in mode_mapping.keys():
130            mode_name = mode_mapping[mode]
131        else:
132            raise RuntimeError('The mode selected %d is not supported by router'
133                               ' %s.', hex(mode), self.name)
134        self.select_item_from_popup_by_id(mode_name, 'wirelessmode',
135                                          wait_for_xpath='id("wds_mode")')
136
137
138    def set_band(self, band):
139        if band == ap_spec.BAND_5GHZ:
140            self.current_band = ap_spec.BAND_5GHZ
141        elif band == ap_spec.BAND_2GHZ:
142            self.current_band = ap_spec.BAND_2GHZ
143        else:
144            raise RuntimeError('Invalid band sent %s' % band)
145
146
147    def set_radio(self, enabled=True):
148        # TODO: Implement the RADIO button for this AP
149        return None
150
151
152    def _set_channel(self, channel):
153        position = self._get_channel_popup_position(channel)
154        channel_choices_2GHZ = ['2412MHz (Channel 1)', '2417MHz (Channel 2)',
155                                '2422MHz (Channel 3)', '2427MHz (Channel 4)',
156                                '2432MHz (Channel 5)', '2437MHz (Channel 6)',
157                                '2442MHz (Channel 7)', '2447MHz (Channel 8)',
158                                '2452MHz (Channel 9)', '2457MHz (Channel 10)',
159                                '2462MHz (Channel 11)']
160        channel_choices_5GHZ = ['5180MHz (Channel 36)', '5200MHz (Channel 40)',
161                                '5220MHz (Channel 44)', '5240MHz (Channel 48)',
162                                '5745MHz (Channel 149)',
163                                '5765MHz (Channel 153)',
164                                '5785MHz (Channel 157)',
165                                '5805MHz (Channel 161)']
166        if self.current_band == ap_spec.BAND_2GHZ:
167            self.select_item_from_popup_by_id(channel_choices_2GHZ[position],
168                                              'sz11gChannel')
169        else:
170            self.select_item_from_popup_by_id(channel_choices_5GHZ[position],
171                                              'sz11aChannel')
172
173
174    def set_security_disabled(self):
175        self.add_item_to_command_list(self._set_security_disabled, (), 2, 1000)
176
177
178    def _set_security_disabled(self):
179        self.wait_for_object_by_id('security_mode')
180        self.select_item_from_popup_by_id('Disable', 'security_mode')
181
182
183    def _set_security_wpapsk(self, security, shared_key, update_interval=1800):
184        self.wait_for_object_by_id('security_mode')
185        if security == ap_spec.SECURITY_TYPE_WPAPSK:
186            wpa_item = 'WPA-PSK'
187        else:
188            wpa_item = 'WPA2-PSK'
189        self.select_item_from_popup_by_id(wpa_item, 'security_mode',
190                                          wait_for_xpath='id("passphrase")')
191        self.set_content_of_text_field_by_id(shared_key, 'passphrase')
192        self.set_content_of_text_field_by_id(update_interval,
193                                             'keyRenewalInterval')
194
195
196    def set_security_wep(self, key_value, authentication):
197        self.add_item_to_command_list(self._set_security_wep,
198                                      (key_value, authentication), 2, 900)
199
200
201    def _set_security_wep(self, key_value, authentication):
202        self.wait_for_object_by_id('security_mode')
203        self.select_item_from_popup_by_id('WEP-OPEN', 'security_mode')
204        self.select_item_from_popup_by_id('ASCII', 'WEP1Select')
205        self.set_content_of_text_field_by_id(key_value, 'WEP1',
206                                             abort_check=True)
207