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
5"""Class to control the Dlink WBR1310AP router."""
6
7import logging
8import os
9import time
10import urlparse
11
12import dynamic_ap_configurator
13import ap_spec
14
15
16class DLinkwbr1310APConfigurator(
17        dynamic_ap_configurator.DynamicAPConfigurator):
18    """Class to control the DLink wbr1310."""
19
20
21    def _open_landing_page(self):
22        page_url = urlparse.urljoin(self.admin_interface_url,'wireless.htm')
23        self.get_url(page_url, page_title='D-LINK')
24        pwd = '//input[@name="login_pass"]'
25        if not self.object_by_xpath_exist(pwd):
26            # We are at the config page, done.
27            return
28
29        xpath = '//input[@name="login_name"]'
30        self.set_content_of_text_field_by_xpath('admin', xpath,
31                                                abort_check=False)
32        self.set_content_of_text_field_by_xpath('password', pwd,
33                                                abort_check=False)
34        self.click_button_by_xpath('//input[@name="login"]')
35        wlan = '//a[text()="Wireless settings"]'
36        self.wait_for_object_by_xpath(wlan)
37        self.click_button_by_xpath(wlan)
38
39
40    def get_supported_bands(self):
41        return [{'band': ap_spec.BAND_2GHZ,
42                 'channels': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]}]
43
44
45    def get_supported_modes(self):
46        return [{'band': ap_spec.BAND_2GHZ,
47                 'modes': [ap_spec.MODE_G, ap_spec.MODE_B]}]
48
49
50    def get_number_of_pages(self):
51        return 1
52
53
54    def is_security_mode_supported(self, security_mode):
55        """
56        Returns if a given security_type is supported.
57
58        @param security_mode: one security modes defined in the APSpec
59
60        @return True if the security mode is supported; False otherwise.
61
62        """
63        return security_mode in (ap_spec.SECURITY_TYPE_DISABLED,
64                                 ap_spec.SECURITY_TYPE_WPAPSK,
65                                 ap_spec.SECURITY_TYPE_WPA2PSK,
66                                 ap_spec.SECURITY_TYPE_WEP)
67
68
69    def navigate_to_page(self, page_number):
70        """
71        Navigates to the page corresponding to the given page number.
72
73        This method performs the translation between a page number and a url to
74        load. This is used internally by apply_settings.
75
76        @param page_number: page number of the page to load
77
78        """
79        # All settings are on the same page, so we always open the config page
80        self._open_landing_page()
81
82
83    def save_page(self, page_number):
84        """
85        Saves the given page.
86
87        @param page_number: Page number of the page to save.
88
89        """
90        # All settings are on the same page, we can ignore page_number
91        self.click_button_by_xpath('//input[@name="button"]')
92        progress_value = self.wait_for_object_by_id("wTime")
93        # Give the router 40 secs to update.
94        for i in xrange(80):
95            page_name = os.path.basename(self.driver.current_url)
96            time.sleep(0.5)
97            if page_name == 'wireless.htm':
98                break
99
100    def is_update_interval_supported(self):
101        """
102        Returns True if setting the PSK refresh interval is supported.
103
104        @return True is supported; False otherwise
105        """
106        return False
107
108    def set_mode(self, mode_enable=True, band=None):
109        self.add_item_to_command_list(self._set_mode, (mode_enable,), 1, 900)
110
111
112    def _set_mode(self, mode_enable=True):
113        # For dlinkwbr1310, 802.11g is the only available mode.
114        logging.debug('This router (%s) does not support multiple modes.',
115                      self.name)
116        return None
117
118
119    def set_radio(self, enabled=True):
120        logging.debug('This router (%s) does not support radio.', self.name)
121        return None
122
123
124    def set_ssid(self, ssid):
125        self.add_item_to_command_list(self._set_ssid, (ssid,), 1, 900)
126
127
128    def _set_ssid(self, ssid):
129        self.set_content_of_text_field_by_id(ssid, 'ssid')
130        self._ssid = ssid
131
132
133    def set_channel(self, channel):
134        self.add_item_to_command_list(self._set_channel, (channel,), 1, 900)
135
136
137    def _set_channel(self, channel):
138        position = self._get_channel_popup_position(channel)
139        channel_ch = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11']
140        xpath = '//select[@name="channel"]'
141        self.select_item_from_popup_by_xpath(channel_ch[position], xpath)
142
143
144    def set_band(self, band):
145        logging.debug('This router (%s) does not support multiple bands.',
146                      self.name)
147        return None
148
149
150    def set_security_disabled(self):
151        self.add_item_to_command_list(self._set_security_disabled, (), 1, 900)
152
153
154    def _set_security_disabled(self):
155        security_disabled = 'Disable Wireless Security (not recommended)'
156        self.select_item_from_popup_by_id(security_disabled, 'wep_type')
157
158
159    def set_security_wep(self, key_value, authentication):
160        self.add_item_to_command_list(self._set_security_wep,
161                                      (key_value, authentication), 1, 900)
162
163
164    def _set_security_wep(self, key_value, authentication):
165        popup = '//select[@name="wep_type"]'
166        self.wait_for_object_by_xpath(popup)
167        security_wep = 'Enable WEP Wireless Security (basic)'
168        self.select_item_from_popup_by_xpath(security_wep, popup)
169        key_type = '//select[@name="wep_key_type"]'
170        self.select_item_from_popup_by_xpath('ASCII', key_type)
171        text_field = '//input[@name="key1"]'
172        self.set_content_of_text_field_by_xpath(key_value, text_field,
173                                                abort_check=True)
174
175
176    def set_security_wpapsk(self, security, shared_key, update_interval=None):
177        self.add_item_to_command_list(self._set_security_wpapsk,
178                                      (security, shared_key, update_interval),
179                                       1, 900)
180
181
182    def _set_security_wpapsk(self, security, shared_key, update_interval=None):
183        popup = '//select[@name="wep_type"]'
184        self.wait_for_object_by_xpath(popup)
185        key_field1 = '//input[@name="wpapsk1"]'
186        key_field2 = '//input[@name="wpapsk2"]'
187        if security == ap_spec.SECURITY_TYPE_WPAPSK:
188            wpa_item = 'Enable WPA-Personal Wireless Security (enhanced)'
189        else:
190            wpa_item = 'Enable WPA2 Wireless Security (enhanced)'
191        self.select_item_from_popup_by_xpath(wpa_item, popup,
192                                             wait_for_xpath=key_field1)
193        self.set_content_of_text_field_by_xpath(shared_key, key_field1,
194                                                abort_check=False)
195        self.set_content_of_text_field_by_xpath(shared_key, key_field2,
196                                                abort_check=False)
197
198
199    def set_visibility(self, visible=True):
200        self.add_item_to_command_list(self._set_visibility, (visible,), 1, 900)
201
202
203    def _set_visibility(self, visible=True):
204        xpath = '//input[@name="ssidBroadcast"]'
205        self.set_check_box_selected_by_xpath(xpath, selected=True)
206