1#!/usr/bin/env python3.4
2#
3#   Copyright 2017 - 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
18import socket
19import sys
20
21from acts import base_test
22from acts.controllers.ap_lib import hostapd_ap_preset
23from acts.controllers.ap_lib import hostapd_bss_settings
24from acts.controllers.ap_lib import hostapd_constants
25from acts.controllers.ap_lib import hostapd_config
26from acts.controllers.ap_lib import hostapd_security
27
28
29class SetupWifiNetworkTest(base_test.BaseTestClass):
30    def wait_for_test_completion(self):
31        port = int(self.user_params["socket_port"])
32        timeout = float(self.user_params["socket_timeout_secs"])
33
34        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
35        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
36
37        server_address = ('localhost', port)
38        logging.info("Starting server socket on localhost port %s", port)
39        sock.bind(('localhost', port))
40        sock.settimeout(timeout)
41        sock.listen(1)
42        logging.info("Waiting for client socket connection")
43        try:
44            connection, client_address = sock.accept()
45        except socket.timeout:
46            logging.error("Did not receive signal. Shutting down AP")
47        except socket.error:
48            logging.error("Socket connection errored out. Shutting down AP")
49        finally:
50            connection.close()
51            sock.shutdown(socket.SHUT_RDWR)
52            sock.close()
53
54    def setup_ap(self):
55        bss_settings = []
56        self.access_point = self.access_points[0]
57        network_type = self.user_params["network_type"]
58        if (network_type == "2G"):
59            self.channel = hostapd_constants.AP_DEFAULT_CHANNEL_2G
60        else:
61            self.channel = hostapd_constants.AP_DEFAULT_CHANNEL_5G
62
63        self.ssid = self.user_params["ssid"]
64        self.security = self.user_params["security"]
65        if self.security == "none":
66            self.config = hostapd_ap_preset.create_ap_preset(
67                channel=self.channel,
68                ssid=self.ssid,
69                bss_settings=bss_settings,
70                profile_name='whirlwind')
71        else:
72            self.passphrase = self.user_params["passphrase"]
73            self.hostapd_security = hostapd_security.Security(
74                security_mode=self.security, password=self.passphrase)
75            self.config = hostapd_ap_preset.create_ap_preset(
76                channel=self.channel,
77                ssid=self.ssid,
78                security=self.hostapd_security,
79                bss_settings=bss_settings,
80                profile_name='whirlwind')
81        self.access_point.start_ap(self.config)
82
83    def test_set_up_single_ap(self):
84        req_params = [
85            "AccessPoint", "network_type", "ssid", "passphrase", "security",
86            "socket_port", "socket_timeout_secs"
87        ]
88        opt_params = []
89        self.unpack_userparams(
90            req_param_names=req_params, opt_param_names=opt_params)
91        # Setup the AP environment
92        self.setup_ap()
93        # AP enviroment created. Wait for client to teardown the environment
94        self.wait_for_test_completion()
95
96    def test_set_up_open_ap(self):
97        req_params = [
98            "AccessPoint", "network_type", "ssid", "security", "socket_port",
99            "socket_timeout_secs"
100        ]
101        opt_params = []
102        self.unpack_userparams(
103            req_param_names=req_params, opt_param_names=opt_params)
104        # Setup the AP environment
105        self.setup_ap()
106        # AP enviroment created. Wait for client to teardown the environment
107        self.wait_for_test_completion()
108