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