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 os
6
7import ap_spec
8import trendnet_ap_configurator
9
10
11class Trendnet654trAPConfigurator(trendnet_ap_configurator.
12                                  TrendnetAPConfigurator):
13    """Derived class to control the Trendnet TEW-654TR."""
14
15
16    def _alert_handler(self, alert):
17        """
18        Checks for any modal dialogs which popup to alert the user and
19        either raises a RuntimeError or ignores the alert.
20
21        @param alert: The modal dialog's contents.
22        """
23        text = alert.text
24        if 'The length of Key1 must be 5 characters' in text:
25            alert.accept()
26        else:
27            raise RuntimeError('An unexpected alert was thrown: %s' % text)
28
29
30    def get_number_of_pages(self):
31        return 1
32
33
34    def navigate_to_page(self, page_number):
35        """
36        Navigates to the page corresponding to the given page number.
37
38        This method performs the translation between a page number and a url to
39        load. This is used internally by apply_settings.
40
41        @param page_number: page number of the page to load
42
43        """
44        self.navigate_to_login_page()
45        if page_number == 1:
46            page_url = os.path.join(self.admin_interface_url,'wireless.htm')
47            self.get_url(page_url, page_title='TEW-654TR')
48        else:
49            raise RuntimeError('Invalid page number passed.  Number of pages '
50                               '%d, page value sent was %d' %
51                               (self.get_number_of_pages(), page_number))
52
53
54    def navigate_to_login_page(self):
55        """Navigates through the login page.
56
57        If we are logged out during and time this method walks through the login
58        process so the appropriate page is loaded to update the settings.
59
60        """
61        # We need to login first in order to configure settings.
62        self.get_url(self.admin_interface_url, page_title='TEW-654TR')
63        self.wait_for_object_by_id('user_name')
64        self.set_content_of_text_field_by_id('admin', 'user_name',
65                                             abort_check=True)
66        self.set_content_of_text_field_by_id('password', 'user_pwd',
67                                             abort_check=True)
68        self.click_button_by_id('login')
69
70
71    def save_page(self, page_number):
72        """
73        Saves the given page.
74
75        @param page_number: Page number of the page to save.
76
77        """
78        xpath = ('//a[contains(@href,"send_request")]//img')
79        self.click_button_by_xpath(xpath, alert_handler=self._alert_handler)
80        self.wait_for_object_by_id('back_btn')
81        self.click_button_by_id('back_btn')
82
83
84    def set_radio(self, enabled=True):
85        return None
86
87
88    def _set_ssid(self, ssid):
89        self.set_content_of_text_field_by_id(ssid, 'ssid')
90        self._ssid = ssid
91
92
93    def _set_channel(self, channel):
94        position = self._get_channel_popup_position(channel)
95        channel_choices = ['1', '2', '3', '4', '5', '6', '7'
96                           '8', '9', '10', '11']
97        self.select_item_from_popup_by_id(channel_choices[position], 'channel')
98
99
100    def set_band(self, band):
101        return None
102
103
104    def _set_mode(self, mode, band=None):
105        # Different bands are not supported so we ignore.
106        # Create the mode to popup item mapping
107        mode_mapping = {ap_spec.MODE_B| ap_spec.MODE_G | ap_spec.MODE_N:
108                        '2.4Ghz 802.11b/g/n mixed mode',
109                        ap_spec.MODE_N: '2.4Ghz 802.11n only mode',
110                        ap_spec.MODE_B | ap_spec.MODE_G:
111                        '2.4Ghz 802.11b/g mixed mode'}
112        mode_name = ''
113        if mode in mode_mapping.keys():
114            mode_name = mode_mapping[mode]
115        else:
116            raise RuntimeError('The mode selected %s is not supported by router'
117                               ' %s.', ap_spec.mode_string_for_mode(mode),
118                               self.name)
119        self.select_item_from_popup_by_id(mode_name, 'dot11_mode')
120
121
122    def set_security_disabled(self):
123        self.add_item_to_command_list(self._set_security_disabled, (), 1, 100)
124
125
126    def _set_security_disabled(self):
127        self.wait_for_object_by_id('security_type')
128        item = 'Disable Wireless Security ( not recommended )'
129        self.select_item_from_popup_by_id(item, 'security_type')
130
131
132    def set_security_wep(self, key_value, authentication):
133        self.add_item_to_command_list(self._set_security_wep,
134                                      (key_value, authentication), 1, 100)
135
136
137    def _set_security_wep(self, key_value, authentication):
138        self.wait_for_object_by_id('security_type')
139        item = 'Enable WEP Wireless Security ( basic )'
140        self.select_item_from_popup_by_id(item, 'security_type')
141        self.set_content_of_text_field_by_id(key_value, 'wep_key1',
142                                             abort_check=True)
143
144
145    def set_security_wpapsk(self, security, shared_key, update_interval=1800):
146        self.add_item_to_command_list(self._set_security_wpapsk,
147                                      (security, shared_key, update_interval),
148                                       1, 100)
149
150
151    def _set_security_wpapsk(self, security, shared_key, update_interval=None):
152        self.wait_for_object_by_id('security_type')
153        if security == ap_spec.SECURITY_TYPE_WPAPSK:
154            wpa_item = 'Enable WPA Wireless Security ( enhanced )'
155        else:
156            wpa_item = 'Enable WPA2 Wireless Security ( enhanced )'
157        self.select_item_from_popup_by_id(wpa_item, 'security_type')
158        self.set_content_of_text_field_by_id(shared_key, 'passphrase')
159        self.set_content_of_text_field_by_id(shared_key, 'confirm_passphrase')
160
161
162    def _set_visibility(self, visible=True):
163        # value=0 is visible; value=1 is invisible
164        int_value = not(int(visible))
165        xpath = ('//input[@value="%d" and @name="ssid_broadcast"]' % int_value)
166        self.click_button_by_xpath(xpath)
167