1#!/usr/bin/env python3
2#
3#   Copyright 2019 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17import logging
18
19from acts import asserts
20from acts.controllers.ap_lib import hostapd_ap_preset
21
22
23def validate_setup_ap_and_associate(*args, **kwargs):
24    """Validates if setup_ap_and_associate was a success or not
25
26       Args: Args match setup_ap_and_associate
27    """
28    asserts.assert_true(setup_ap_and_associate(*args, **kwargs),
29                        'Failed to associate.')
30    asserts.explicit_pass('Successfully associated.')
31
32
33def setup_ap_and_associate(access_point,
34                           client,
35                           profile_name,
36                           channel,
37                           ssid,
38                           mode=None,
39                           preamble=None,
40                           beacon_interval=None,
41                           dtim_period=None,
42                           frag_threshold=None,
43                           rts_threshold=None,
44                           force_wmm=None,
45                           hidden=False,
46                           security=None,
47                           additional_ap_parameters=None,
48                           password=None,
49                           check_connectivity=False,
50                           n_capabilities=None,
51                           ac_capabilities=None,
52                           vht_bandwidth=None,
53                           setup_bridge=False):
54    """Sets up the AP and associates a client.
55
56    Args:
57        access_point: An ACTS access_point controller
58        client: A WlanDevice.
59        profile_name: The profile name of one of the hostapd ap presets.
60        channel: What channel to set the AP to.
61        preamble: Whether to set short or long preamble (True or False)
62        beacon_interval: The beacon interval (int)
63        dtim_period: Length of dtim period (int)
64        frag_threshold: Fragmentation threshold (int)
65        rts_threshold: RTS threshold (int)
66        force_wmm: Enable WMM or not (True or False)
67        hidden: Advertise the SSID or not (True or False)
68        security: What security to enable.
69        additional_ap_parameters: Additional parameters to send the AP.
70        password: Password to connect to WLAN if necessary.
71        check_connectivity: Whether to check for internet connectivity.
72    """
73    setup_ap(access_point, profile_name, channel, ssid, mode, preamble,
74             beacon_interval, dtim_period, frag_threshold, rts_threshold,
75             force_wmm, hidden, security, additional_ap_parameters, password,
76             check_connectivity, n_capabilities, ac_capabilities,
77             vht_bandwidth, setup_bridge)
78
79    if security and security.wpa3:
80        return associate(client,
81                         ssid,
82                         password,
83                         key_mgmt='SAE',
84                         check_connectivity=check_connectivity,
85                         hidden=hidden)
86    else:
87        return associate(client,
88                         ssid,
89                         password=password,
90                         check_connectivity=check_connectivity,
91                         hidden=hidden)
92
93
94def setup_ap(access_point,
95             profile_name,
96             channel,
97             ssid,
98             mode=None,
99             preamble=None,
100             beacon_interval=None,
101             dtim_period=None,
102             frag_threshold=None,
103             rts_threshold=None,
104             force_wmm=None,
105             hidden=False,
106             security=None,
107             additional_ap_parameters=None,
108             password=None,
109             check_connectivity=False,
110             n_capabilities=None,
111             ac_capabilities=None,
112             vht_bandwidth=None,
113             setup_bridge=False):
114    """Sets up the AP.
115
116    Args:
117        access_point: An ACTS access_point controller
118        profile_name: The profile name of one of the hostapd ap presets.
119        channel: What channel to set the AP to.
120        preamble: Whether to set short or long preamble (True or False)
121        beacon_interval: The beacon interval (int)
122        dtim_period: Length of dtim period (int)
123        frag_threshold: Fragmentation threshold (int)
124        rts_threshold: RTS threshold (int)
125        force_wmm: Enable WMM or not (True or False)
126        hidden: Advertise the SSID or not (True or False)
127        security: What security to enable.
128        additional_ap_parameters: Additional parameters to send the AP.
129        password: Password to connect to WLAN if necessary.
130        check_connectivity: Whether to check for internet connectivity.
131    """
132    ap = hostapd_ap_preset.create_ap_preset(profile_name=profile_name,
133                                            iface_wlan_2g=access_point.wlan_2g,
134                                            iface_wlan_5g=access_point.wlan_5g,
135                                            channel=channel,
136                                            ssid=ssid,
137                                            mode=mode,
138                                            short_preamble=preamble,
139                                            beacon_interval=beacon_interval,
140                                            dtim_period=dtim_period,
141                                            frag_threshold=frag_threshold,
142                                            rts_threshold=rts_threshold,
143                                            force_wmm=force_wmm,
144                                            hidden=hidden,
145                                            bss_settings=[],
146                                            security=security,
147                                            n_capabilities=n_capabilities,
148                                            ac_capabilities=ac_capabilities,
149                                            vht_bandwidth=vht_bandwidth)
150    access_point.start_ap(hostapd_config=ap,
151                          setup_bridge=setup_bridge,
152                          additional_parameters=additional_ap_parameters)
153
154
155def associate(client,
156              ssid,
157              password=None,
158              key_mgmt=None,
159              check_connectivity=True,
160              hidden=False):
161    """Associates a client to a WLAN network.
162
163    Args:
164        client: A WlanDevice
165        ssid: SSID of the ap we are looking for.
166        password: The password for the WLAN, if applicable.
167        key_mgmt: The hostapd wpa_key_mgmt value.
168        check_connectivity: Whether to check internet connectivity.
169        hidden: If the WLAN is hidden or not.
170    """
171    return client.associate(ssid,
172                            password,
173                            key_mgmt=key_mgmt,
174                            check_connectivity=check_connectivity,
175                            hidden=hidden)
176
177
178def status(client):
179    """Requests the state of WLAN network.
180
181    Args:
182        None
183    """
184    status = ''
185    status_response = client.status()
186
187    if status_response.get('error') is None:
188        # No error, so get the result
189        status = status_response['result']
190
191    logging.debug('status: %s' % status)
192    return status
193
194
195def disconnect(client):
196    """Disconnect client from its WLAN network.
197
198    Args:
199        client: A WlanDevice
200    """
201    client.disconnect()
202