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
6import logging
7
8import ap_spec
9import dynamic_ap_configurator
10
11
12class LevelOneAPConfigurator(
13        dynamic_ap_configurator.DynamicAPConfigurator):
14    """Base class for objects to configure Level one access points
15       using webdriver."""
16
17    def get_number_of_pages(self):
18        return 2
19
20
21    def get_supported_modes(self):
22        return [{'band': ap_spec.BAND_2GHZ,
23                 'modes': [ap_spec.MODE_B | ap_spec.MODE_G | ap_spec.MODE_N,
24                           ap_spec.MODE_G, ap_spec.MODE_B, ap_spec.MODE_N]}]
25
26
27    def get_supported_bands(self):
28        return [{'band': ap_spec.BAND_2GHZ,
29                 'channels': ['auto', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]}]
30
31
32    def is_security_mode_supported(self, security_mode):
33        return security_mode in (ap_spec.SECURITY_TYPE_DISABLED,
34                                 ap_spec.SECURITY_TYPE_WPAPSK,
35                                 ap_spec.SECURITY_TYPE_WPA2PSK,
36                                 ap_spec.SECURITY_TYPE_WEP)
37
38
39    def navigate_to_page(self, page_number):
40        self.get_url(self.admin_interface_url, page_title='LevelOne')
41        self.driver.switch_to_default_content()
42        frame = self.wait_for_object_by_id('idleft')
43        self.driver.switch_to_frame(frame)
44        element = self.driver.find_element_by_link_text('Wireless')
45        element.click()
46        element = self.driver.find_element_by_link_text('Options')
47        element.click()
48        self.driver.switch_to_default_content()
49        frame = self.wait_for_object_by_id('idmain')
50        self.driver.switch_to_frame(frame)
51        self.wait_for_object_by_xpath('//input[@name="ssid"]')
52        if page_number == 2:
53            self.click_button_by_xpath('//input[@name="wsec"]')
54            self.wait_for_object_by_xpath('//select[@name="wsecurity"]')
55
56
57    def save_page(self, page_number):
58        self.click_button_by_xpath('//input[@name="save"]')
59        if page_number == 1:
60            self.wait_for_object_by_xpath('//input[@name="ssid"]')
61        elif page_number == 2:
62            self.wait_for_object_by_xpath('//select[@name="wsecurity"]')
63
64
65    def set_mode(self, mode, band=None):
66        self.add_item_to_command_list(self._set_mode, (mode,), 1, 900)
67
68
69    def _set_mode(self, mode, band=None):
70        mode_mapping = {
71                ap_spec.MODE_B | ap_spec.MODE_G | ap_spec.MODE_N:'11b+g+n',
72                ap_spec.MODE_G:'G Only', ap_spec.MODE_B:'B Only',
73                ap_spec.MODE_N:'N Only', 'Disabled':'Off'}
74        mode_name = mode_mapping.get(mode)
75        if not mode_name:
76            raise RuntimeError('The mode %d not supported by router %s. ',
77                               hex(mode), self.name)
78        xpath = '//select[@name="wire_mode"]'
79        self.select_item_from_popup_by_xpath(mode_name, xpath)
80
81
82    def set_ssid(self, ssid):
83        self.add_item_to_command_list(self._set_ssid, (ssid,), 1, 900)
84
85
86    def _set_ssid(self, ssid):
87        xpath = '//input[@name="ssid"]'
88        self.set_content_of_text_field_by_xpath(ssid, xpath, abort_check=False)
89        self._ssid = ssid
90
91
92    def set_channel(self, channel):
93        self.add_item_to_command_list(self._set_channel, (channel,), 1, 900)
94
95
96    def _set_channel(self, channel):
97        position = self._get_channel_popup_position(channel)
98        xpath = '//select[@name="w_channel"]'
99        channels = ['auto', '01', '02', '03', '04', '05', '06', '07', '08',
100                    '09', '10', '11']
101        self.select_item_from_popup_by_xpath(channels[position], xpath)
102
103
104    def set_radio(self, enabled=True):
105        weight = 1 if enabled else 1000
106        self.add_item_to_command_list(self._set_radio, (enabled,), 1, weight)
107
108
109    def _set_radio(self, enabled=True):
110        if not enabled:
111            self._set_mode('Disabled')
112        else:
113            self._set_mode(ap_spec.MODE_G)
114
115
116    def set_band(self, enabled=True):
117        logging.debug('set_band is not supported in Linksys single band AP.')
118        return None
119
120
121    def set_security_disabled(self):
122        self.add_item_to_command_list(self._set_security_disabled, (), 2, 1000)
123
124
125    def _set_security_disabled(self):
126        xpath = '//select[@name="wsecurity"]'
127        self.select_item_from_popup_by_xpath('Disabled', xpath)
128
129
130    def set_security_wep(self, key_value, authentication):
131        self.add_item_to_command_list(self._set_security_wep,
132                                      (key_value, authentication), 2, 1000)
133
134
135    def _set_security_wep(self, key_value, authentication):
136        popup = '//select[@name="wsecurity"]'
137        if not self.item_in_popup_by_xpath_exist('WEP', popup):
138            raise RuntimeError('The popup %s did not contain the item %s. '
139                               'Is the mode N?' % (popup, self.security_wep))
140        self.select_item_from_popup_by_xpath('WEP', popup)
141        text = '//input[@name="passphrase"]'
142        self.set_content_of_text_field_by_xpath(key_value, text,
143                                                abort_check=True)
144        xpath = '//input[@name="keygen"]'
145        self.click_button_by_xpath(xpath)
146
147
148    def set_security_wpapsk(self, security, shared_key, update_interval=None):
149        self.add_item_to_command_list(self._set_security_wpapsk,
150                                      (security, shared_key, update_interval),
151                                       2, 900)
152
153
154    def _set_security_wpapsk(self, security, shared_key, upadate_interval=None):
155        """Common method to set wpapsk and wpa2psk modes."""
156        popup = '//select[@name="wsecurity"]'
157        self.wait_for_object_by_xpath(popup)
158        if security == ap_spec.SECURITY_TYPE_WPAPSK:
159            wpa_item = 'WPA-PSK'
160            text = '//input[@name="wpa_psk"]'
161        else:
162            wpa_item = 'WPA2-PSK'
163            text = '//input[@name="wpa2_psk"]'
164        self.select_item_from_popup_by_xpath(wpa_item, popup)
165        self.set_content_of_text_field_by_xpath(shared_key, text,
166                                                abort_check=True)
167
168
169    def is_update_interval_supported(self):
170        return False
171
172
173    def is_visibility_supported(self):
174        return False
175