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
6import ap_spec
7import trendnet692gr_ap_configurator
8import time
9
10
11class Trendnet812druAPConfigurator(trendnet692gr_ap_configurator.
12                                  Trendnet692grAPConfigurator):
13    """Derived class to control the Trendnet TEW-812DRU."""
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 'WPS in Open security' in text:
25            alert.accept()
26        else:
27            raise RuntimeError('An unexpected alert was thrown: %s' % text)
28
29
30    def get_supported_modes(self):
31        return [{'band': ap_spec.BAND_2GHZ, 'modes': [ap_spec.MODE_N]},
32                {'band': ap_spec.BAND_5GHZ, 'modes': [ap_spec.MODE_N]}]
33
34
35    def is_security_mode_supported(self, security_mode):
36        """
37        Returns if a given security_type is supported.
38
39        @param security_mode: one security modes defined in the APSpec
40
41        @return True if the security mode is supported; False otherwise.
42
43        """
44        return security_mode in (ap_spec.SECURITY_TYPE_DISABLED,
45                                 ap_spec.SECURITY_TYPE_WEP,
46                                 ap_spec.SECURITY_TYPE_WPAPSK,
47                                 ap_spec.SECURITY_TYPE_WPA2PSK)
48
49
50    def navigate_to_page(self, page_number):
51        """Navigates to the given page.
52
53        @param page_number: the page to navigate to.
54        """
55        # All settings are on the same page, so we always open the config page
56        if self.current_band == ap_spec.BAND_2GHZ:
57            if page_number == 1:
58                page_url = os.path.join(self.admin_interface_url ,
59                                        'wireless/basic.asp?wl_unit=0')
60            elif page_number == 2:
61                page_url = os.path.join(self.admin_interface_url ,
62                                        'wireless/security.asp?wl_unit=0')
63            else:
64                raise RuntimeError('Invalid page number passed. Number of pages'
65                                   '%d, page value sent was %d' %
66                                   (self.get_number_of_pages(), page_number))
67        elif self.current_band == ap_spec.BAND_5GHZ:
68            if page_number == 1:
69                page_url = os.path.join(self.admin_interface_url ,
70                                        'wireless/basic.asp?wl_unit=1')
71            elif page_number == 2:
72                page_url = os.path.join(self.admin_interface_url ,
73                                        'wireless/security.asp?wl_unit=1')
74            else:
75                raise RuntimeError('Invalid page number passed. Number of pages'
76                                   '%d, page value sent was %d' %
77                                    (self.get_number_of_pages(), page_number))
78        else:
79            raise RuntimeError('Incorrect band band = %s' % self.current_band)
80        self.get_url(page_url, page_title='TEW-812DRU')
81
82
83    def _set_ssid(self, ssid):
84        xpath = '//input[@maxlength="32" and @name="wl_ssid"]'
85        self.set_content_of_text_field_by_xpath(ssid, xpath, abort_check=True)
86        self._ssid = ssid
87
88
89    def _set_mode(self, mode, band=None):
90        # Different bands are not supported so we ignore.
91        # Create the mode to popup item mapping
92        mode_mapping = {ap_spec.MODE_N: 'Auto'}
93        mode_name = ''
94        if mode in mode_mapping.keys():
95            mode_name = mode_mapping[mode]
96        else:
97            raise RuntimeError('The mode selected %s is not supported by router'
98                               ' %s.', ap_spec.mode_string_for_mode(mode),
99                               self.name)
100        xpath = '//select[@name="wl_nmode"]'
101        while self.number_of_items_in_popup_by_xpath(xpath) < 2:
102            time.sleep(0.25)
103        self.select_item_from_popup_by_xpath(mode_name, xpath)
104
105
106    def set_radio(self, enabled=True):
107        self.add_item_to_command_list(self._set_radio, (enabled, ), 1, 200)
108
109
110    def _set_radio(self, enabled=True):
111        xpath = '//select[@name="wl_bss_enabled"]'
112        if enabled:
113            self.select_item_from_popup_by_xpath('On', xpath)
114        else:
115            self.select_item_from_popup_by_xpath('Off', xpath)
116
117
118    def _set_visibility(self, visible=True):
119        xpath = '//select[@name="wl_closed"]'
120        if visible:
121            self.select_item_from_popup_by_xpath('Enabled', xpath)
122        else:
123            self.select_item_from_popup_by_xpath('Disabled', xpath)
124
125
126    def _set_channel(self, channel):
127        position = self._get_channel_popup_position(channel)
128        xpath = '//select[@name="wl_chanspec"]'
129        channel_choices_2GHZ = ['Auto', '1', '2', '3', '4', '5', '6', '7', '8',
130                                '9', '10', '11']
131        channel_choices_5GHZ = ['Auto', '36', '40', '44', '48', '149', '153',
132                                '157', '161']
133        if self.current_band == ap_spec.BAND_2GHZ:
134            self.select_item_from_popup_by_xpath(channel_choices_2GHZ[position],
135                                                 xpath)
136        else:
137            self.select_item_from_popup_by_xpath(channel_choices_5GHZ[position],
138                                                 xpath)
139
140
141    def _set_security_wpapsk(self, security, shared_key, update_interval=1800):
142        self.wait_for_object_by_id('security_mode')
143        if security == ap_spec.SECURITY_TYPE_WPAPSK:
144            wpa_item = 'WPA-PSK'
145        else:
146            wpa_item = 'WPA2-PSK'
147        self.select_item_from_popup_by_id(wpa_item, 'security_mode',
148                                          wait_for_xpath='id("wpaPassphrase")')
149        self.set_content_of_text_field_by_id(shared_key, 'wpaPassphrase')
150        self.set_content_of_text_field_by_id(update_interval,
151                                             'rotationInterval')
152