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
5
6import os
7
8import ap_spec
9import trendnet_ap_configurator
10
11
12class Trendnet432brpAPConfigurator(trendnet_ap_configurator.
13                                   TrendnetAPConfigurator):
14    """Derived class to control the Trendnet TEW-432BRP."""
15
16
17    def _alert_handler(self, alert):
18        """
19        Checks for any modal dialogs which popup to alert the user and
20        either raises a RuntimeError or ignores the alert.
21
22        @param alert: The modal dialog's contents.
23        """
24        text = alert.text
25        if 'The length of Key1 must be 10 characters' in text:
26            alert.accept()
27        elif 'Are you sure you want to continue' in text:
28            alert.accept()
29        else:
30            raise RuntimeError(text)
31
32
33    def get_number_of_pages(self):
34        return 2
35
36
37    def get_supported_bands(self):
38        return [{'band': ap_spec.BAND_2GHZ, 'channels': range(1, 13)}]
39
40
41    def get_supported_modes(self):
42        return [{'band': ap_spec.BAND_2GHZ, 'modes': [ap_spec.MODE_G]}]
43
44
45    def is_security_mode_supported(self, security_mode):
46        """Returns in the given security mode is supported.
47
48        @param security_mode: a security mode that is define in APSpec.
49        """
50        return security_mode in (ap_spec.SECURITY_TYPE_DISABLED,
51                                 ap_spec.SECURITY_TYPE_WEP,
52                                 ap_spec.SECURITY_TYPE_WPAPSK,
53                                 ap_spec.SECURITY_TYPE_WPA2PSK)
54
55
56    def navigate_to_page(self, page_number):
57        """Navigates to the given pages.
58
59        @param page_number: the page to navigate to.
60        """
61        if page_number == 1:
62            page_url = os.path.join(self.admin_interface_url,'wlan_basic.asp')
63            self.get_url(page_url, page_title='TEW-432BRP')
64        elif page_number == 2:
65            page_url = os.path.join(self.admin_interface_url,
66                                    'wlan_security.asp')
67            self.get_url(page_url, page_title='TEW-432BRP')
68        else:
69            raise RuntimeError('Invalid page number passed.  Number of pages '
70                               '%d, page value sent was %d' %
71                               (self.get_number_of_pages(), page_number))
72
73
74    def save_page(self, page_number):
75        """Saves the given page.
76
77        @param page_number: the page to save.
78        """
79        xpath = ('//input[@name="apply" and @value="Apply"]')
80        self.click_button_by_xpath(xpath, alert_handler=self._alert_handler)
81
82
83    def set_radio(self, enabled=True):
84        self.add_item_to_command_list(self._set_radio, (enabled, ), 1, 1000)
85
86
87    def _set_radio(self, enabled=True):
88        # value=0 is ON; value=1 is OFF
89        int_value = not(int(enabled))
90        xpath = ('//input[@value="%d" and @name="enable"]' % int_value)
91        self.click_button_by_xpath(xpath, alert_handler=self._alert_handler)
92
93
94    def _set_ssid(self, ssid):
95        self.set_content_of_text_field_by_id(ssid, 'ssid')
96        self._ssid = ssid
97
98
99    def _set_channel(self, channel):
100        position = self._get_channel_popup_position(channel)
101        channel_choices = ['1', '2', '3', '4', '5', '6', '7'
102                           '8', '9', '10', '11', '12', '13']
103        self.select_item_from_popup_by_id(channel_choices[position], 'channel')
104
105
106    def set_band(self, band):
107        return None
108
109
110    def set_mode(self, mode, band=None):
111        return None
112
113
114    def set_security_disabled(self):
115        self.add_item_to_command_list(self._set_security_disabled, (), 2, 1000)
116
117
118    def _set_security_disabled(self):
119        self.wait_for_object_by_id('wep_type')
120        self.select_item_from_popup_by_id(' Disable ', 'wep_type')
121
122
123    def set_security_wep(self, key_value, authentication):
124        self.add_item_to_command_list(self._set_security_wep,
125                                      (key_value, authentication), 2, 900)
126
127
128    def _set_security_wep(self, key_value, authentication):
129        self.wait_for_object_by_id('wep_type')
130        self.select_item_from_popup_by_id(' WEP ', 'wep_type')
131        self.select_item_from_popup_by_id('ASCII', 'wep_key_type')
132        self.select_item_from_popup_by_id(' 64-bit', 'wep_key_len')
133        self.set_content_of_text_field_by_id(key_value, 'key1',
134                                             abort_check=True)
135
136
137    def _set_security_wpapsk(self, security, shared_key, update_interval=None):
138        self.wait_for_object_by_id('wep_type')
139        if security == ap_spec.SECURITY_TYPE_WPAPSK:
140            self.select_item_from_popup_by_id(' WPA ', 'wep_type')
141        else:
142            self.select_item_from_popup_by_id(' WPA2 ', 'wep_type')
143        self.set_content_of_text_field_by_id(shared_key, 'wpapsk1')
144        self.set_content_of_text_field_by_id(shared_key, 'wpapsk2')
145
146
147    def _set_visibility(self, visible=True):
148        # value=0 is visible; value=1 is invisible
149        int_value = not(int(visible))
150        xpath = ('//input[@value="%d" and @name="ssid_broadcast"]' % int_value)
151        self.click_button_by_xpath(xpath)
152