1# Copyright (c) 2013 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
5import ap_spec
6import trendnet_ap_configurator
7
8
9class Trendnet731brAPConfigurator(trendnet_ap_configurator.
10                                  TrendnetAPConfigurator):
11    """Derived class to control the Trendnet TEW-731BR."""
12
13    def navigate_to_page(self, page_number):
14        """Navigate to the given page.
15
16        @param page_number: the page to navigate to.
17        """
18        self.navigate_to_login_page()
19        if page_number == 1:
20            page_url = ''.join([self.admin_interface_url,
21                                  "/wireless_basic.htm"])
22            self.get_url(page_url, page_title='TRENDNET')
23        if page_number == 2:
24            page_url = ''.join([self.admin_interface_url,
25                                  "/wireless_auth.htm"])
26            self.get_url(page_url, page_title='TRENDNET')
27
28
29    def navigate_to_login_page(self):
30        """
31        We need to login first in order to configure settings.
32        """
33        self.get_url(self.admin_interface_url, page_title='Login')
34        self.wait_for_object_by_id('login_n')
35        self.set_content_of_text_field_by_id('admin', 'login_n',
36                                                 abort_check=True)
37        self.set_content_of_text_field_by_id('password', 'login_pass',
38                                                abort_check=True)
39        self.click_button_by_xpath('//input[@value="Login"]')
40
41
42    def _set_ssid(self, ssid):
43        self.set_content_of_text_field_by_id(ssid, 'show_ssid')
44        self._ssid = ssid
45
46
47    def save_page(self, page_number):
48        """Save the settings of the passed in page.
49
50        @param page_number: the page to save.
51        """
52        if page_number == 1:
53            xpath = ('//input[@type="button" and @value="Apply"]')
54        elif page_number == 2:
55            xpath = ('//input[@type="button" and @value="Apply"]')
56        self.click_button_by_xpath(xpath, alert_handler=self._alert_handler)
57        xpath = ('//input[@type="button" and @value="Continue"]')
58        self.click_button_by_xpath(xpath, alert_handler=self._alert_handler)
59
60
61    def _set_security_disabled(self):
62        self.wait_for_object_by_xpath('//select[@name="wep_type"]')
63        self.select_item_from_popup_by_id(' Disable ','wep_type')
64
65
66    def get_supported_modes(self):
67        return [{'band': ap_spec.BAND_2GHZ,
68                 'modes': [ap_spec.MODE_B,
69                           ap_spec.MODE_G,
70                           ap_spec.MODE_N,
71                           ap_spec.MODE_B | ap_spec.MODE_G,
72                           ap_spec.MODE_B | ap_spec.MODE_G | ap_spec.MODE_N]}]
73
74
75    def _set_mode(self, mode, band=None):
76        # Different bands are not supported so we ignore.
77        # Create the mode to popup item mapping
78        mode_mapping = {ap_spec.MODE_B | ap_spec.MODE_G | ap_spec.MODE_N:
79                        '2.4Ghz 802.11b/g/n mixed mode',
80                        ap_spec.MODE_N: '2.4Ghz 802.11n only mode',
81                        ap_spec.MODE_B: '2.4Ghz 802.11b only mode',
82                        ap_spec.MODE_G: '2.4Ghz 802.11g only mode',
83                        ap_spec.MODE_B | ap_spec.MODE_G:
84                        '2.4Ghz 802.11b/g mixed mode'}
85        mode_name = ''
86        if mode in mode_mapping.keys():
87            mode_name = mode_mapping[mode]
88        else:
89            raise RuntimeError('The mode selected %d is not supported by router'
90                               ' %s.', hex(mode), self.name)
91        self.select_item_from_popup_by_id(mode_name, 'dot11_mode')
92
93    def get_supported_bands(self):
94        return [{'band': ap_spec.BAND_2GHZ, 'channels': range(1, 12)}]
95
96
97    def _set_channel(self, channel):
98        position = self._get_channel_popup_position(channel)
99        channel_choices = ['1', '2', '3', '4', '5',
100                           '6', '7', '8', '9', '10', '11']
101        self.select_item_from_popup_by_id(channel_choices[position],
102                                          'wlan0_channel_t')
103
104    def _set_visibility(self, visible=True):
105        # value=1 is visible; value=0 is invisible
106        int_value = int(visible)
107        xpath = ('//input[@value="%d" and @name="wlan0_ssid_broadcast"]' %
108                  int_value)
109        self.click_button_by_xpath(xpath, alert_handler=self._alert_handler)
110
111
112    def is_update_interval_supported(self):
113        """
114        Returns True if setting the PSK refresh interval is supported.
115
116        @return True is supported; False otherwise
117        """
118        return False
119
120
121    def _set_security_wep(self, key_value, authentication):
122        self.wait_for_object_by_xpath('//select[@name="wep_type"]')
123        self.select_item_from_popup_by_id(' WEP ','wep_type')
124        # This router doesn not support ASCII passphrase for
125        # generating hex key.Converting ASCII to hex before setting
126        self.set_content_of_text_field_by_id(key_value.encode("hex"),
127                                             'key1_64_hex')
128
129
130    def _set_security_wpapsk(self, security, shared_key, update_interval=1800):
131        self.wait_for_object_by_xpath('//select[@name="wep_type"]')
132        if security == ap_spec.SECURITY_TYPE_WPAPSK:
133            self.select_item_from_popup_by_id(' WPA ','wep_type')
134        else:
135            self.select_item_from_popup_by_id(' WPA2 ','wep_type')
136        self.set_content_of_text_field_by_id(shared_key,
137                                             'wlan0_psk_pass_phrase')
138        self.set_content_of_text_field_by_id(shared_key, 'wpapsk2')
139
140
141    def _alert_handler(self, alert):
142        """Checks for any modal dialogs which popup to alert the user and
143        either raises a RuntimeError or ignores the alert.
144
145        Args:
146          alert: The modal dialog's contents.
147        """
148        text = alert.text
149        if 'To disable SSID Broadcast will cause WPS not work' in text:
150            alert.accept()
151        else:
152            raise RuntimeError('We have an unhandled alert: %s' % text)
153
154