1#   Copyright 2017 - The Android Open Source Project
2#
3#   Licensed under the Apache License, Version 2.0 (the "License");
4#   you may not use this file except in compliance with the License.
5#   You may obtain a copy of the License at
6#
7#       http://www.apache.org/licenses/LICENSE-2.0
8#
9#   Unless required by applicable law or agreed to in writing, software
10#   distributed under the License is distributed on an "AS IS" BASIS,
11#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12#   See the License for the specific language governing permissions and
13#   limitations under the License.
14
15from acts.controllers.ap_lib import hostapd_config
16from acts.controllers.ap_lib import hostapd_constants
17
18
19def create_ap_preset(profile_name,
20                     channel=None,
21                     dtim=2,
22                     frequency=None,
23                     security=None,
24                     ssid=None,
25                     vht_bandwidth=80,
26                     bss_settings=[],
27                     iface_wlan_2g=hostapd_constants.WLAN0_STRING,
28                     iface_wlan_5g=hostapd_constants.WLAN1_STRING):
29    """AP preset config generator.  This a wrapper for hostapd_config but
30       but supplies the default settings for the preset that is selected.
31
32        You may specify channel or frequency, but not both.  Both options
33        are checked for validity (i.e. you can't specify an invalid channel
34        or a frequency that will not be accepted).
35
36    Args:
37        profile_name: The name of the device want the preset for.
38                      Options: whirlwind
39        channel: int, channel number.
40        dtim: int, DTIM value of the AP, default is 2.
41        frequency: int, frequency of channel.
42        security: Security, the secuirty settings to use.
43        ssid: string, The name of the ssid to brodcast.
44        vht_bandwidth: VHT bandwidth for 11ac operation.
45        bss_settings: The settings for all bss.
46        iface_wlan_2g: the wlan 2g interface name of the AP.
47        iface_wlan_5g: the wlan 5g interface name of the AP.
48
49    Returns: A hostapd_config object that can be used by the hostapd object.
50    """
51
52    force_wmm = None
53    force_wmm = None
54    beacon_interval = None
55    dtim_period = None
56    short_preamble = None
57    interface = None
58    mode = None
59    n_capabilities = None
60    ac_capabilities = None
61    if channel:
62        frequency = hostapd_config.get_frequency_for_channel(channel)
63    elif frequency:
64        channel = hostapd_config.get_channel_for_frequency(frequency)
65    else:
66        raise ValueError('Specify either frequency or channel.')
67
68    if (profile_name == 'whirlwind'):
69        force_wmm = True
70        beacon_interval = 100
71        short_preamble = True
72        if frequency < 5000:
73            interface = iface_wlan_2g
74            mode = hostapd_constants.MODE_11N_MIXED
75            n_capabilities = [
76                hostapd_constants.N_CAPABILITY_LDPC,
77                hostapd_constants.N_CAPABILITY_SGI20,
78                hostapd_constants.N_CAPABILITY_SGI40,
79                hostapd_constants.N_CAPABILITY_TX_STBC,
80                hostapd_constants.N_CAPABILITY_RX_STBC1,
81                hostapd_constants.N_CAPABILITY_DSSS_CCK_40
82            ]
83            config = hostapd_config.HostapdConfig(
84                ssid=ssid,
85                security=security,
86                interface=interface,
87                mode=mode,
88                force_wmm=force_wmm,
89                beacon_interval=beacon_interval,
90                dtim_period=dtim_period,
91                short_preamble=short_preamble,
92                frequency=frequency,
93                n_capabilities=n_capabilities,
94                bss_settings=bss_settings)
95        else:
96            interface = iface_wlan_5g
97            mode = hostapd_constants.MODE_11AC_MIXED
98            if hostapd_config.ht40_plus_allowed(channel):
99                extended_channel = hostapd_constants.N_CAPABILITY_HT40_PLUS
100            elif hostapd_config.ht40_minus_allowed(channel):
101                extended_channel = hostapd_constants.N_CAPABILITY_HT40_MINUS
102            # Define the n capability vector for 20 MHz and higher bandwidth
103            if vht_bandwidth >= 40:
104                n_capabilities = [
105                    hostapd_constants.N_CAPABILITY_LDPC, extended_channel,
106                    hostapd_constants.N_CAPABILITY_SGI20,
107                    hostapd_constants.N_CAPABILITY_SGI40,
108                    hostapd_constants.N_CAPABILITY_TX_STBC,
109                    hostapd_constants.N_CAPABILITY_RX_STBC1
110                ]
111            else:
112                n_capabilities = [
113                    hostapd_constants.N_CAPABILITY_LDPC,
114                    hostapd_constants.N_CAPABILITY_SGI20,
115                    hostapd_constants.N_CAPABILITY_SGI40,
116                    hostapd_constants.N_CAPABILITY_TX_STBC,
117                    hostapd_constants.N_CAPABILITY_RX_STBC1,
118                    hostapd_constants.N_CAPABILITY_HT20
119                ]
120            ac_capabilities = [
121                hostapd_constants.AC_CAPABILITY_MAX_MPDU_11454,
122                hostapd_constants.AC_CAPABILITY_RXLDPC,
123                hostapd_constants.AC_CAPABILITY_SHORT_GI_80,
124                hostapd_constants.AC_CAPABILITY_TX_STBC_2BY1,
125                hostapd_constants.AC_CAPABILITY_RX_STBC_1,
126                hostapd_constants.AC_CAPABILITY_MAX_A_MPDU_LEN_EXP7,
127                hostapd_constants.AC_CAPABILITY_RX_ANTENNA_PATTERN,
128                hostapd_constants.AC_CAPABILITY_TX_ANTENNA_PATTERN
129            ]
130            config = hostapd_config.HostapdConfig(
131                ssid=ssid,
132                security=security,
133                interface=interface,
134                mode=mode,
135                force_wmm=force_wmm,
136                vht_channel_width=vht_bandwidth,
137                beacon_interval=beacon_interval,
138                dtim_period=dtim_period,
139                short_preamble=short_preamble,
140                frequency=frequency,
141                n_capabilities=n_capabilities,
142                ac_capabilities=ac_capabilities,
143                bss_settings=bss_settings)
144    else:
145        raise ValueError('Invalid ap model specified (%s)' % profile_name)
146    return config
147