1# Hwsim wrapper
2# Copyright (c) 2016, Tieto Corporation
3#
4# This software may be distributed under the terms of the BSD license.
5# See README for more details.
6
7import remotehost
8from wpasupplicant import WpaSupplicant
9import hostapd
10import config
11import rutils
12import monitor
13import traceback
14import wlantest
15
16import logging
17logger = logging.getLogger()
18
19def run_hwsim_test(devices, setup_params, refs, duts, monitors, hwsim_test):
20    try:
21        ref_hosts = []
22        dut_hosts = []
23        dev = []
24        apdev = []
25
26        # get hosts
27        for ref in refs:
28            ref_host = rutils.get_host(devices, ref)
29            ref_hosts.append(ref_host)
30        for dut in duts:
31            dut_host = rutils.get_host(devices, dut)
32            dut_hosts.append(dut_host)
33
34        # setup log dir
35        local_log_dir = setup_params['local_log_dir']
36
37        # setup hw before test
38        rutils.setup_hw(ref_hosts, setup_params)
39        rutils.setup_hw(dut_hosts, setup_params)
40
41        # run monitors if requested/possible
42        for ref_host in ref_hosts:
43            monitor.add(ref_host, monitors)
44            monitor.run(ref_host, setup_params)
45        for dut_host in dut_hosts:
46            monitor.add(dut_host, monitors)
47            monitor.run(dut_host, setup_params)
48
49        monitor_hosts = monitor.create(devices, setup_params, refs, duts,
50                                       monitors)
51        mon = None
52        if len(monitor_hosts) > 0:
53            mon = monitor_hosts[0]
54            wlantest.Wlantest.reset_remote_wlantest()
55            wlantest.Wlantest.register_remote_wlantest(mon, setup_params,
56                                                       monitor)
57
58        # run hostapd/wpa_supplicant
59        for ref_host in ref_hosts:
60            rutils.run_wpasupplicant(ref_host, setup_params)
61            wpas = WpaSupplicant(hostname=ref_host.host, global_iface="udp",
62                                 global_port=ref_host.port)
63            wpas.interface_add(ref_host.ifname)
64            dev.append(wpas)
65        for dut_host in dut_hosts:
66            rutils.run_hostapd(dut_host, setup_params)
67            dut_host.dev['bssid'] = rutils.get_mac_addr(dut_host)
68            apdev.append(dut_host.dev)
69
70        if hwsim_test.__code__.co_argcount == 1:
71            hwsim_test(dev)
72        elif hwsim_test.__code__.co_argcount == 2:
73            hwsim_test(dev, apdev)
74        else:
75            params = {}
76            params['long'] = 1
77            params['logdir'] = local_log_dir
78            hwsim_test(dev, apdev, params)
79
80       # hostapd/wpa_supplicant cleanup
81        for wpas in dev:
82            wpas.interface_remove(wpas.host.ifname)
83            wpas.terminate()
84        dev = []
85
86        # remove monitors
87        for ref_host in ref_hosts:
88            monitor.remove(ref_host)
89        for dut_host in dut_hosts:
90            monitor.remove(dut_host)
91
92        for ref_host in ref_hosts:
93            rutils.kill_wpasupplicant(ref_host, setup_params)
94            ref_host.get_logs(local_log_dir)
95        for dut_host in dut_hosts:
96            rutils.kill_hostapd(dut_host, setup_params)
97            dut_host.get_logs(local_log_dir)
98        if mon is not None:
99            wlantest.Wlantest.reset_remote_wlantest()
100            mon.get_logs(local_log_dir)
101
102        return ""
103    except:
104        logger.info(traceback.format_exc())
105        for wpas in dev:
106            try:
107                wpas.interface_remove(wpas.host.ifname)
108                wpas.terminate()
109            except:
110                pass
111
112        for ref_host in ref_hosts:
113            monitor.remove(ref_host)
114        for dut_host in dut_hosts:
115            monitor.remove(dut_host)
116
117        for ref_host in ref_hosts:
118            rutils.kill_wpasupplicant(ref_host, setup_params)
119            ref_host.get_logs(local_log_dir)
120        for dut_host in dut_hosts:
121            rutils.kill_hostapd(dut_host, setup_params)
122            dut_host.get_logs(local_log_dir)
123        if mon is not None:
124            wlantest.Wlantest.reset_remote_wlantest()
125            mon.get_logs(local_log_dir)
126        raise
127