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
5"""Class to control the Dlink_DIR505LAP router."""
6
7import urlparse
8import logging
9import time
10
11import dlink_ap_configurator
12import ap_spec
13
14class DLinkDIR505lAPConfigurator(
15        dlink_ap_configurator.DLinkAPConfigurator):
16    """Derived class to control the Dlink_DIR505lAP router."""
17
18
19    def _alert_handler(self, alert):
20        """Checks for any modal dialogs which popup to alert the user and
21        either raises a RuntimeError or ignores the alert.
22
23        Args:
24          alert: The modal dialog's contents.
25        """
26        text = alert.text
27        if 'Nothing has changed, save anyway?' in text:
28            alert.accept()
29        elif 'To hidden SSID will be disabled the WPS, Are you sure ?' in text:
30            alert.accept()
31        elif 'Open mode configuration is not secure.' in text:
32            alert.accept()
33        else:
34            raise RuntimeError('We have an unhandled alert: %s' % text)
35
36
37    def get_number_of_pages(self):
38        return 1
39
40
41    def get_supported_bands(self):
42        return [{'band': ap_spec.BAND_2GHZ,
43                 'channels': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]}]
44
45
46    def get_supported_modes(self):
47        return [{'band': ap_spec.BAND_2GHZ,
48                 'modes': [ap_spec.MODE_N, ap_spec.MODE_G,
49                           ap_spec.MODE_N | ap_spec.MODE_G,
50                           ap_spec.MODE_N | ap_spec.MODE_G | ap_spec.MODE_B]}]
51
52
53    def is_security_mode_supported(self, security_mode):
54        """
55        Returns if a given security_type is supported.
56
57        @param security_mode: one security modes defined in the APSpec
58
59        @return True if the security mode is supported; False otherwise.
60
61        """
62        return security_mode in (ap_spec.SECURITY_TYPE_DISABLED,
63                                 ap_spec.SECURITY_TYPE_WPAPSK,
64                                 ap_spec.SECURITY_TYPE_WPA2PSK)
65
66
67    def navigate_to_page(self, page_number):
68        """
69        Navigates to the page corresponding to the given page number.
70
71        This method performs the translation between a page number and a url to
72        load. This is used internally by apply_settings.
73
74        @param page_number: page number of the page to load
75
76        """
77        page_url = urlparse.urljoin(self.admin_interface_url, 'Wireless.htm')
78        self.get_url(page_url, page_title='D-LINK SYSTEMS')
79        if not self.object_by_xpath_exist('//input[@id="user_pwd"]'):
80            # We are at the config page, done.
81            return
82        self.set_content_of_text_field_by_id('password', 'user_pwd')
83        self.click_button_by_id('login')
84
85
86    def save_page(self, page_number):
87        """
88        Saves the given page.
89
90        @param page_number: Page number of the page to save.
91
92        """
93        # All settings are on the same page, we can ignore page_number
94        self.click_button_by_id('apply_btn', alert_handler=self._alert_handler)
95        # Second alert may pop-up, so we must send it to our alert handler
96        try:
97            alert = self.driver.switch_to_alert()
98            self._handler(self._alert_handler)
99        except:
100            logging.debug("No alert present")
101        timer = self.wait_for_object_by_id('show_second', wait_time=20)
102        while (self.object_by_id_exist('show_second') and
103               int(timer.text) > 5):
104            time.sleep(1)
105        self.click_button_by_id('back_btn', alert_handler=self._alert_handler)
106
107
108    def set_mode(self, mode, band=None):
109        # Mode overrides the band.  So if a band change is made after a mode
110        # change it may make an incompatible pairing.
111        self.add_item_to_command_list(self._set_mode, (mode, band), 1, 800)
112
113
114    def _set_mode(self, mode, band=None):
115        # Create the mode to popup item mapping
116        mode_mapping = {ap_spec.MODE_N: '802.11n only',
117            ap_spec.MODE_G: 'Mixed 802.11n, 802.11g',
118            ap_spec.MODE_N | ap_spec.MODE_G: 'Mixed 802.11n, 802.11g',
119            ap_spec.MODE_N | ap_spec.MODE_G | ap_spec.MODE_B:
120            'Mixed 802.11n, 802.11g and 802.11b'}
121        if mode in mode_mapping.keys():
122            popup_value = mode_mapping[mode]
123        else:
124            raise RuntimeError('The mode selected %s is not '
125                               'supported by router %s.' %
126                               (hex(mode), self.name))
127        self.select_item_from_popup_by_id(popup_value, 'dot11_mode')
128
129
130    def set_radio(self, enabled=True):
131        # If we are enabling we are activating all other UI components, do
132        # it first. Otherwise we are turning everything off so do it last.
133        logging.debug('This router (%s) does not support enabling/disabling '
134                      'wireless', self.name)
135        return None
136
137
138    def set_ssid(self, ssid):
139        # Can be done as long as it is enabled
140        self.add_item_to_command_list(self._set_ssid, (ssid,), 1, 900)
141
142
143    def _set_ssid(self, ssid):
144        self.set_content_of_text_field_by_id(ssid, 'ssid')
145        self._ssid = ssid
146
147
148    def set_channel(self, channel):
149        self.add_item_to_command_list(self._set_channel, (channel,), 1, 900)
150
151
152    def _set_channel(self, channel):
153        position = self._get_channel_popup_position(channel)
154        channel_choices = ['2.412 GHz - CH 1', '2.417 GHz - CH 2',
155                           '2.422 GHz - CH 3', '2.427 GHz - CH 4',
156                           '2.432 GHz - CH 5', '2.437 GHz - CH 6',
157                           '2.442 GHz - CH 7', '2.447 GHz - CH 8',
158                           '2.452 GHz - CH 9', '2.457 GHz - CH 10',
159                           '2.462 GHz - CH 11']
160        channel_popup = self.driver.find_element_by_id('channel')
161        if channel_popup.get_attribute('disabled') == 'true':
162            self.set_check_box_selected_by_id('autochann', selected=False)
163        self.select_item_from_popup_by_id(channel_choices[position], 'channel')
164
165
166    def set_band(self, band):
167        logging.debug('This router (%s) does not support multiple bands.',
168                       self.name)
169        return None
170
171
172    def set_security_disabled(self):
173        self.add_item_to_command_list(self._set_security_disabled, (), 1, 900)
174
175
176    def _set_security_disabled(self):
177        self.select_item_from_popup_by_id('None', 'security_mode')
178
179
180    def set_security_wep(self, key_value, authentication):
181        logging.debug('This router (%s) does not support WEP', self.name)
182        return None
183
184
185    def set_security_wpapsk(self, security, shared_key, update_interval=1800):
186        self.add_item_to_command_list(self._set_security_wpapsk,
187                                     (security, shared_key, update_interval),
188                                      1, 900)
189
190
191    def _set_security_wpapsk(self, security, shared_key, update_interval=1800):
192        self.select_item_from_popup_by_id('WPA-Personal', 'security_mode')
193        if security == ap_spec.SECURITY_TYPE_WPAPSK:
194            wpa_item = 'WPA Only'
195        else:
196            wpa_item = 'WPA2 Only'
197        self.select_item_from_popup_by_id(wpa_item, 'wpa_mode')
198        self.set_content_of_text_field_by_id(shared_key, 'pre_shared_key')
199
200
201    def set_visibility(self, visible=True):
202        self.add_item_to_command_list(self._set_visibility, (visible,), 1, 900)
203
204
205    def _set_visibility(self, visible=True):
206        if visible:
207            xpath = '//input[@value="1" and @name="ssid_broadcast"]'
208        else:
209            xpath = '//input[@value="0" and @name="ssid_broadcast"]'
210        self.click_button_by_xpath(xpath)
211