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
7import urlparse
8
9import ap_spec
10import dynamic_ap_configurator
11
12
13class Buffaloag300hAPConfigurator(dynamic_ap_configurator.
14                                  DynamicAPConfigurator):
15    """Base class for Buffalo WZR AG300H router."""
16
17
18    def get_number_of_pages(self):
19        return 2
20
21
22    def is_update_interval_supported(self):
23        return True
24
25
26    def get_supported_modes(self):
27        return [{'band': ap_spec.BAND_2GHZ,
28                 'modes': [ap_spec.MODE_B, ap_spec.MODE_N, ap_spec.MODE_G,
29                           ap_spec.MODE_N | ap_spec.MODE_G, ap_spec.MODE_M,
30                           ap_spec.MODE_B | ap_spec.MODE_G]},
31                {'band': ap_spec.BAND_5GHZ,
32                 'modes': [ap_spec.MODE_N, ap_spec.MODE_A, ap_spec.MODE_M]}]
33
34
35    def get_supported_bands(self):
36        return [{'band': ap_spec.BAND_2GHZ,
37                 'channels': ['Auto', 1, 2, 3, 4, 5, 6, 7, 8, 9 , 10, 11]},
38                {'band': ap_spec.BAND_5GHZ,
39                 'channels': ['Auto', 36, 40, 44, 48, 149, 153,
40                              157, 161, 165]}]
41
42
43    def is_security_mode_supported(self, security_mode):
44        return security_mode in (ap_spec.SECURITY_TYPE_DISABLED,
45                                 ap_spec.SECURITY_TYPE_WPAPSK,
46                                 ap_spec.SECURITY_TYPE_WPA2PSK,
47                                 ap_spec.SECURITY_TYPE_WEP)
48
49
50    def navigate_to_page(self, page_number):
51        if page_number == 1:
52            page_url = urlparse.urljoin(self.admin_interface_url,
53                                        'Wireless_Basic.asp')
54            self.get_url(page_url, page_title='Wireless')
55        elif page_number == 2:
56            page_url = urlparse.urljoin(self.admin_interface_url,
57                                        'WL_WPATable.asp')
58            self.get_url(page_url, page_title='Security')
59        else:
60            raise RuntimeError('Invalid page number passed. Number of pages '
61                               '%d, page value sent was %d' %
62                               (self.get_number_of_pages(), page_number))
63
64
65    def save_page(self, page_number):
66        apply_set = '//input[@name="save_button"]'
67        self.click_button_by_xpath(apply_set)
68
69
70    def set_mode(self, mode, band=None):
71        self.add_item_to_command_list(self._set_mode, (mode, band,), 1, 900)
72
73
74    def _set_mode(self, mode, band=None):
75        mode_mapping = {ap_spec.MODE_B: 'B-Only',
76                        ap_spec.MODE_G: 'G-Only',
77                        ap_spec.MODE_B | ap_spec.MODE_G: 'BG-Mixed',
78                        ap_spec.MODE_N | ap_spec.MODE_G: 'NG-Mixed',
79                        ap_spec.MODE_N: 'N-Only (2.4 GHz)',
80                        ap_spec.MODE_A: 'A-Only',
81                        ap_spec.MODE_M: 'Mixed'}
82        xpath = '//select[@name="ath0_net_mode"]'
83        if self.current_band == ap_spec.BAND_5GHZ:
84            xpath = '//select[@name="ath1_net_mode"]'
85            mode_mapping[ap_spec.MODE_N] = 'N-Only (5 GHz)'
86        mode_name = ''
87        if mode in mode_mapping.keys():
88            mode_name = mode_mapping[mode]
89        else:
90            raise RuntimeError('The mode selected %d is not supported by router'
91                               ' %s.', hex(mode), self.name)
92        self.select_item_from_popup_by_xpath(mode_name, xpath)
93
94
95    def set_radio(self, enabled=True):
96        #  We cannot turn off radio on Buffalo WZR.
97        logging.debug('This router (%s) does not support radio.' , self.name)
98        return None
99
100
101    def set_ssid(self, ssid):
102        self.add_item_to_command_list(self._set_ssid, (ssid,), 1, 900)
103
104
105    def _set_ssid(self, ssid):
106        xpath = '//input[@name="ath0_ssid"]'
107        if self.current_band == ap_spec.BAND_5GHZ:
108            xpath = '//input[@name="ath1_ssid"]'
109        self.set_content_of_text_field_by_xpath(ssid, xpath)
110        self._ssid = ssid
111
112
113    def set_channel(self, channel):
114        self.add_item_to_command_list(self._set_channel, (channel,), 1, 900)
115
116
117    def _set_channel(self, channel):
118        position = self._get_channel_popup_position(channel)
119        channel_choices = ['Auto',
120                           '1 - 2412 MHz', '2 - 2417 MHz', '3 - 2422 MHz',
121                           '4 - 2427 MHz', '5 - 2432 MHz', '6 - 2437 MHz',
122                           '7 - 2442 MHz', '8 - 2447 MHz', '9 - 2452 MHz',
123                           '10 - 2457 MHz', '11 - 2462 MHz']
124        xpath = '//select[@name="ath0_channel"]'
125        if self.current_band == ap_spec.BAND_5GHZ:
126            xpath = '//select[@name="ath1_channel"]'
127            channel_choices = ['Auto', '36 - 5180 MHz', '40 - 5200 MHz',
128                               '44 - 5220 MHz', '48 - 5240 MHz',
129                               '149 - 5745 MHz', '153 - 5765 MHz',
130                               '157 - 5785 MHz', '161 - 5805 MHz']
131        self.select_item_from_popup_by_xpath(channel_choices[position], xpath)
132
133
134    def set_band(self, band):
135        if band == ap_spec.BAND_5GHZ:
136            self.current_band = ap_spec.BAND_5GHZ
137        elif band == ap_spec.BAND_2GHZ:
138            self.current_band = ap_spec.BAND_2GHZ
139        else:
140            raise RuntimeError('Invalid band sent %s' % band)
141
142
143    def set_security_disabled(self):
144        self.add_item_to_command_list(self._set_security_disabled, (), 2, 1000)
145
146
147    def _set_security(self, mode, wait_for_xpath=None):
148        xpath = '//select[@name="ath0_security_mode"]'
149        if self.current_band == ap_spec.BAND_5GHZ:
150            xpath = '//select[@name="ath1_security_mode"]'
151        self.wait_for_object_by_xpath(xpath)
152        self.select_item_from_popup_by_xpath(mode, xpath,
153                                             wait_for_xpath=wait_for_xpath)
154
155
156    def _set_security_disabled(self):
157        self._set_security('Disabled')
158
159
160    def set_security_wep(self, key_value, authentication):
161        self.add_item_to_command_list(self._set_security_wep,
162                                      (key_value, authentication), 2, 1000)
163
164
165    def _set_security_wep(self, key_value, authentication):
166        text_field = '//input[@name="ath0_passphrase"]'
167        xpath = '//input[@name="ath0_key1"]'
168        if self.current_band == ap_spec.BAND_5GHZ:
169            text_field = '//input[@name="ath1_passphrase"]'
170            xpath = '//input[@name="ath1_key1"]'
171        self._set_security('WEP', wait_for_xpath=text_field)
172        button = '//input[@name="wepGenerate"]'
173        generate_list = self.driver.find_elements_by_xpath(button)
174        generate = generate_list[0]
175        if self.current_band == ap_spec.BAND_5GHZ and len(generate_list) > 1:
176            generate = generate_list[1]
177        self.set_content_of_text_field_by_xpath(key_value, text_field,
178                                                abort_check=True)
179        generate.click()
180        self.wait_for_object_by_xpath(xpath)
181
182
183    def set_security_wpapsk(self, security, shared_key, update_interval=None):
184        self.add_item_to_command_list(self._set_security_wpapsk,
185                                (security, shared_key,update_interval), 2, 900)
186
187
188    def _set_security_wpapsk(self, security, shared_key, update_interval=None):
189        key_field = '//input[@name="ath0_wpa_psk"]'
190        interval = '//input[@name="ath0_wpa_gtk_rekey"]'
191        if self.current_band == ap_spec.BAND_5GHZ:
192            key_field = '//input[@name="ath1_wpa_psk"]'
193            interval = '//input[@name="ath1_wpa_gtk_rekey"]'
194        if security == ap_spec.SECURITY_TYPE_WPAPSK:
195            wpa_item = 'WPA Personal'
196        else:
197            wpa_item = 'WPA2 Personal'
198        self._set_security(wpa_item, wait_for_xpath=key_field)
199        self.set_content_of_text_field_by_xpath(shared_key, key_field,
200                                                abort_check=True)
201        if update_interval:
202            self.set_content_of_text_field_by_xpath(update_interval, interval,
203                                                    abort_check=True)
204
205
206    def set_visibility(self, visible=True):
207        self.add_item_to_command_list(self._set_visibility, (visible,), 1, 900)
208
209
210    def _set_visibility(self, visible=True):
211        button = 'ath0_closed'
212        if self.current_band == ap_spec.BAND_5GHZ:
213            button = 'ath1_closed'
214        int_value = 0 if visible else 1
215        xpath = ('//input[@value="%d" and @name="%s"]' % (int_value, button))
216        self.click_button_by_xpath(xpath)
217