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"""Baseclass to control Netgear single band routers."""
6
7import logging
8import urlparse
9import dynamic_ap_configurator
10import ap_spec
11
12from selenium.common.exceptions import TimeoutException as \
13    SeleniumTimeoutException
14
15class NetgearSingleBandAPConfigurator(
16        dynamic_ap_configurator.DynamicAPConfigurator):
17    """Baseclass to control Netgear single band routers."""
18
19
20    def _alert_handler(self, alert):
21        """Checks for any modal dialogs which popup to alert the user and
22        either raises a RuntimeError or ignores the alert.
23
24        Args:
25          alert: The modal dialog's contents.
26        """
27        text = alert.text
28        raise RuntimeError('We have an unhandeled alert. %s' % text)
29
30
31    def get_number_of_pages(self):
32        return 1
33
34
35    def is_update_interval_supported(self):
36        """
37        Returns True if setting the PSK refresh interval is supported.
38
39        @return True is supported; False otherwise
40        """
41        return False
42
43
44    def get_supported_bands(self):
45        return [{'band': ap_spec.BAND_2GHZ,
46                 'channels': ['Auto', 1, 2, 3, 4, 5, 6, 7, 8, 9 , 10, 11]}]
47
48
49    def get_supported_modes(self):
50        return [{'band': ap_spec.BAND_2GHZ,
51                 'modes': [ap_spec.MODE_G, ap_spec.MODE_B | ap_spec.MODE_G]}]
52
53
54    def is_security_mode_supported(self, security_mode):
55        return security_mode in (ap_spec.SECURITY_TYPE_DISABLED,
56                                 ap_spec.SECURITY_TYPE_WPAPSK,
57                                 ap_spec.SECURITY_TYPE_WPA2PSK,
58                                 ap_spec.SECURITY_TYPE_WEP)
59
60
61    def navigate_to_page(self, page_number):
62        self.get_url(urlparse.urljoin(self.admin_interface_url,
63                     'WLG_wireless.htm'))
64        try:
65            self.wait_for_object_by_xpath('//input[@name="ssid"]')
66        except SeleniumTimeoutException, e:
67            raise SeleniumTimeoutException('Unable to navigate to settings '
68                                           'page. WebDriver exception:%s', e)
69
70
71    def save_page(self, page_number):
72        self.click_button_by_xpath('//input[@name="Apply"]',
73                                   alert_handler=self._alert_handler)
74
75
76    def set_radio(self, enabled=True):
77        logging.debug('set_radio is not supported in this router.')
78        return None
79
80
81    def set_ssid(self, ssid):
82        self.add_item_to_command_list(self._set_ssid, (ssid,), 1, 900)
83
84
85    def _set_ssid(self, ssid):
86        xpath = '//input[@maxlength="32" and @name="ssid"]'
87        self.set_content_of_text_field_by_xpath(ssid, xpath, abort_check=True)
88        self._ssid = ssid
89
90
91    def set_channel(self, channel):
92        self.add_item_to_command_list(self._set_channel, (channel,), 1, 900)
93
94
95    def _set_channel(self, channel):
96        position = self._get_channel_popup_position(channel)
97        channel_choices = ['Auto', '01', '02', '03', '04', '05', '06',
98                           '07', '08', '09', '10', '11']
99        xpath = '//select[@name="w_channel"]'
100        self.select_item_from_popup_by_xpath(channel_choices[position], xpath)
101
102
103    def set_mode(self, mode, band=None):
104        self.add_item_to_command_list(self._set_mode, (mode,), 1, 900)
105
106
107    def _set_mode(self, mode, band=None):
108        if mode == ap_spec.MODE_G:
109            mode_popup = 'g only'
110        elif mode == (ap_spec.MODE_G | ap_spec.MODE_B):
111            mode_popup = 'b and g'
112        else:
113            raise RuntimeError('Invalid mode passed %x.' % mode)
114        xpath = '//select[@name="opmode"]'
115        self.select_item_from_popup_by_xpath(mode_popup, xpath)
116
117
118    def set_band(self, band):
119        logging.debug('The router has just one band.')
120        return None
121
122
123    def set_security_disabled(self):
124        self.add_item_to_command_list(self._set_security_disabled, (), 1, 900)
125
126
127    def _set_security_disabled(self):
128        xpath = '//input[@name="security_type" and @value="Disable"]'
129        self.click_button_by_xpath(xpath, alert_handler=self._alert_handler)
130
131
132    def set_security_wep(self, value, authentication):
133        self.add_item_to_command_list(self._set_security_wep,
134                                     (value, authentication), 1, 900)
135
136
137    def _set_security_wep(self, value, authentication):
138        xpath = '//input[@name="security_type" and @value="WEP"]'
139        try:
140            self.click_button_by_xpath(xpath, alert_handler=self._alert_handler)
141        except Exception, e:
142            raise RuntimeError('We got an exception: "%s". Check the mode. '
143                               'It should be \'Up to 54 Mbps\'.' % str(e))
144        xpath = '//input[@name="passphraseStr"]'
145        self.set_content_of_text_field_by_xpath(value, xpath, abort_check=True)
146        xpath = '//input[@value="Generate"]'
147        self.click_button_by_xpath(xpath, alert_handler=self._alert_handler)
148
149
150    def set_security_wpapsk(self, security, key, update_interval=None):
151        self.add_item_to_command_list(self._set_security_wpapsk, (security,
152                                      key,), 1, 900)
153
154
155    def _set_security_wpapsk(self, security, key, update_interval=None):
156        # Update Interval is not supported.
157        if security == ap_spec.SECURITY_TYPE_WPAPSK:
158            xpath = '//input[@name="security_type" and @value="WPA-PSK"]'
159        else:
160            xpath = '//input[@name="security_type" and @value="WPA2-PSK"]'
161        self.click_button_by_xpath(xpath, alert_handler=self._alert_handler)
162        xpath = '//input[@name="passphrase"]'
163        self.set_content_of_text_field_by_xpath(key, xpath, abort_check=True)
164
165
166    def is_visibility_supported(self):
167        """
168        Returns if AP supports setting the visibility (SSID broadcast).
169
170        @return True if supported; False otherwise.
171        """
172        return False
173