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_DIR300AP router."""
6
7import urlparse
8import logging
9
10import dlink_ap_configurator
11import ap_spec
12
13class DLinkDIR300APConfigurator(
14        dlink_ap_configurator.DLinkAPConfigurator):
15    """Derived class to control the Dlink_DIR300AP router."""
16
17
18    def get_number_of_pages(self):
19        return 1;
20
21
22    def get_supported_bands(self):
23        return [{'band': ap_spec.BAND_2GHZ,
24                 'channels': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]}]
25
26
27    def get_supported_modes(self):
28        return [{'band': ap_spec.BAND_2GHZ,
29                 'modes': [ap_spec.MODE_G]}]
30
31
32    def is_security_mode_supported(self, security_mode):
33        """
34        Returns if a given security_type is supported.
35
36        @param security_mode: one security modes defined in the APSpec
37
38        @return True if the security mode is supported; False otherwise.
39
40        """
41        return security_mode in (ap_spec.SECURITY_TYPE_DISABLED,
42                                 ap_spec.SECURITY_TYPE_WEP,
43                                 ap_spec.SECURITY_TYPE_WPAPSK,
44                                 ap_spec.SECURITY_TYPE_WPA2PSK)
45
46
47    def navigate_to_page(self, page_number):
48        """
49        Navigates to the page corresponding to the given page number.
50
51        This method performs the translation between a page number and a url to
52        load. This is used internally by apply_settings.
53
54        @param page_number: page number of the page to load
55
56        """
57        page_url = urlparse.urljoin(self.admin_interface_url, 'bsc_wlan.php')
58        self.get_url(page_url, page_title='D-LINK SYSTEMS, INC | '
59                    'WIRELESS ROUTER | HOME')
60        pwd = '//input[@name="LOGIN_PASSWD"]'
61        if not self.object_by_xpath_exist(pwd):
62            # We are at the config page, done.
63            return
64        xpath = '//input[@name="LOGIN_USER"]'
65        self.set_content_of_text_field_by_xpath('admin', xpath,
66                                                abort_check=True)
67        self.set_content_of_text_field_by_xpath('password', pwd,
68                                                abort_check=True)
69        self.click_button_by_xpath('//input[@name="login"]')
70
71
72    def save_page(self, page_number):
73        """
74        Saves the given page.
75
76        @param page_number: Page number of the page to save.
77
78        """
79        # All settings are on the same page, we can ignore page_number
80        button = self.driver.find_element_by_xpath('//input[@name="apply"]')
81        button.click()
82        # If we did not make changes we are sent to the continue screen.
83        button_xpath = '//input[@name="bt"]'
84        if self.object_by_xpath_exist(button_xpath):
85            button = self.driver.find_element_by_xpath(button_xpath)
86            button.click()
87        # We will be returned to the landing page when complete
88        self.wait_for_object_by_id('enable')
89
90
91    def set_mode(self, mode, band=None):
92        # Mode overrides the band.  So if a band change is made after a mode
93        # change it may make an incompatible pairing.
94        logging.debug('This router (%s) does not support multiple modes.',
95                      self.name)
96        return None
97
98
99    def set_radio(self, enabled=True):
100        # If we are enabling we are activating all other UI components, do
101        # it first. Otherwise we are turning everything off so do it last.
102        if enabled:
103            weight = 1
104        else:
105            weight = 1000
106        self.add_item_to_command_list(self._set_radio, (enabled,), 1, weight)
107
108
109    def _set_radio(self, enabled=True):
110        # The radio checkbox for this router always has a value of 1. So we need
111        # to use other methods to determine if the radio is on or not. Check if
112        # the ssid textfield is disabled.
113        ssid = self.driver.find_element_by_xpath('//input[@id="ssid"]')
114        if ssid.get_attribute('disabled') == 'true':
115            radio_enabled = False
116        else:
117            radio_enabled = True
118        if radio_enabled == enabled:
119            # Nothing to do
120            return
121        self.set_check_box_selected_by_id('enable', selected=False)
122
123
124    def set_ssid(self, ssid):
125        # Can be done as long as it is enabled
126        self.add_item_to_command_list(self._set_ssid, (ssid,), 1, 900)
127
128
129    def _set_ssid(self, ssid):
130        self._set_radio(enabled=True)
131        self._ssid = ssid
132        self.set_content_of_text_field_by_id(ssid, 'ssid')
133
134
135    def set_channel(self, channel):
136        self.add_item_to_command_list(self._set_channel, (channel,), 1, 900)
137
138
139    def _set_channel(self, channel):
140        position = self._get_channel_popup_position(channel)
141        self._set_radio(enabled=True)
142        channel_choices = ['1 ', '2', '3', '4', '5', '6', '7', '8', '9', '10',
143                           '11']
144        channel_popup = self.driver.find_element_by_id('channel')
145        if channel_popup.get_attribute('disabled') == 'true':
146            self.set_check_box_selected_by_id('autochann', selected=False)
147        self.select_item_from_popup_by_id(channel_choices[position], 'channel')
148
149
150    def set_band(self, band):
151        logging.debug('This router (%s) does not support multiple bands.',
152                      self.name)
153        return None
154
155
156    def set_security_disabled(self):
157        self.add_item_to_command_list(self._set_security_disabled, (), 1, 900)
158
159
160    def _set_security_disabled(self):
161        self._set_radio(enabled=True)
162        security_disabled = 'Disable Wireless Security (not recommended)'
163        self.select_item_from_popup_by_id(security_disabled, 'security_type')
164
165
166    def set_security_wep(self, key_value, authentication):
167        self.add_item_to_command_list(self._set_security_wep,
168                                      (key_value, authentication), 1, 900)
169
170
171    def _set_security_wep(self, key_value, authentication):
172        self._set_radio(enabled=True)
173        security_wep = 'Enable WEP Wireless Security (basic)'
174        self.select_item_from_popup_by_id(security_wep, 'security_type',
175                                          wait_for_xpath='id("wepkey_64")')
176        self.select_item_from_popup_by_id('64Bit', 'wep_key_len',
177                                          wait_for_xpath='id("wepkey_64")')
178        self.set_content_of_text_field_by_id(key_value, 'wepkey_64')
179
180
181    def set_security_wpapsk(self, security, shared_key, update_interval=1800):
182        self.add_item_to_command_list(self._set_security_wpapsk,
183                                     (security, shared_key, update_interval),
184                                      1, 900)
185
186
187    def _set_security_wpapsk(self, security, shared_key, update_interval=1800):
188        self._set_radio(enabled=True)
189        if security == ap_spec.SECURITY_TYPE_WPAPSK:
190            wpa_item = 'Enable WPA Only Wireless Security (enhanced)'
191        else:
192            wpa_item = 'Enable WPA2 Only Wireless Security (enhanced)'
193        self.select_item_from_popup_by_id(wpa_item, 'security_type')
194        self.select_item_from_popup_by_id('PSK', 'psk_eap')
195        self.set_content_of_text_field_by_id(shared_key, 'wpapsk1')
196
197
198    def set_visibility(self, visible=True):
199        self.add_item_to_command_list(self._set_visibility, (visible,), 1, 900)
200
201
202    def _set_visibility(self, visible=True):
203        self._set_radio(enabled=True)
204        found_id = self.wait_for_objects_by_id(['aphidden'], wait_time=20)
205        if ('aphidden' in found_id) and visible:
206            self.set_check_box_selected_by_id('aphidden', selected=True)
207        elif ('aphidden' in found_id) and not visible:
208            self.set_check_box_selected_by_id('aphidden', selected=False)
209        else:
210            raise RuntimeError('Unable to set visibility.')
211