1#!/usr/bin/env python2
2#
3# Show/check devices
4# Copyright (c) 2016, Tieto Corporation
5#
6# This software may be distributed under the terms of the BSD license.
7# See README for more details.
8
9import time
10import traceback
11import config
12import os
13import sys
14import getopt
15import re
16
17import logging
18logger = logging.getLogger()
19
20import rutils
21from remotehost import Host
22from wpasupplicant import WpaSupplicant
23import hostapd
24
25def show_devices(devices, setup_params):
26    """Show/check available devices"""
27    print("Devices:")
28    for device in devices:
29        host = rutils.get_host(devices, device['name'])
30        # simple check if authorized_keys works correctly
31        status, buf = host.execute(["id"])
32        if status != 0:
33            print("[" + host.name + "] - ssh communication:  FAILED")
34            continue
35        else:
36            print("[" + host.name + "] - ssh communication: OK")
37        # check setup_hw works correctly
38        rutils.setup_hw_host(host, setup_params)
39
40        # show uname
41        status, buf = host.execute(["uname", "-s", "-n", "-r", "-m", "-o"])
42        print("\t" + buf)
43        # show ifconfig
44        ifaces = re.split('; | |, ', host.ifname)
45        for iface in ifaces:
46            status, buf = host.execute(["ifconfig", iface])
47            if status != 0:
48                print("\t" + iface + " failed\n")
49                continue
50            lines = buf.splitlines()
51            for line in lines:
52                print("\t" + line)
53        # check hostapd, wpa_supplicant, iperf exist
54        status, buf = host.execute([setup_params['wpa_supplicant'], "-v"])
55        if status != 0:
56            print("\t" + setup_params['wpa_supplicant'] + " not find\n")
57            continue
58        lines = buf.splitlines()
59        for line in lines:
60            print("\t" + line)
61        print("")
62        status, buf = host.execute([setup_params['hostapd'], "-v"])
63        if status != 1:
64            print("\t" + setup_params['hostapd'] + " not find\n")
65            continue
66        lines = buf.splitlines()
67        for line in lines:
68            print("\t" + line)
69        print("")
70        status, buf = host.execute([setup_params['iperf'], "-v"])
71        if status != 0 and status != 1:
72            print("\t" + setup_params['iperf'] + " not find\n")
73            continue
74        lines = buf.splitlines()
75        for line in lines:
76            print("\t" + line)
77        print("")
78
79def check_device(devices, setup_params, dev_name, monitor=False):
80    host = rutils.get_host(devices, dev_name)
81    # simple check if authorized_keys works correctly
82    status, buf = host.execute(["id"])
83    if status != 0:
84        raise Exception(dev_name + " - ssh communication FAILED: " + buf)
85
86    rutils.setup_hw_host(host, setup_params)
87
88    ifaces = re.split('; | |, ', host.ifname)
89    # check interfaces (multi for monitor)
90    for iface in ifaces:
91        status, buf = host.execute(["ifconfig", iface])
92        if status != 0:
93            raise Exception(dev_name + " ifconfig " + iface + " failed: " + buf)
94
95    # monitor doesn't need wpa_supplicant/hostapd ...
96    if monitor == True:
97        return
98
99    status, buf = host.execute(["ls", "-l", setup_params['wpa_supplicant']])
100    if status != 0:
101        raise Exception(dev_name + " - wpa_supplicant: " + buf)
102
103    status, buf = host.execute(["ls", "-l", setup_params['hostapd']])
104    if status != 0:
105        raise Exception(dev_name + " - hostapd: " + buf)
106
107    status, buf = host.execute(["which", setup_params['iperf']])
108    if status != 0:
109        raise Exception(dev_name + " - iperf: " + buf)
110
111    status, buf = host.execute(["which", "tshark"])
112    if status != 0:
113        logger.debug(dev_name + " - tshark: " + buf)
114
115def check_devices(devices, setup_params, refs, duts, monitors):
116    """Check duts/refs/monitors devices"""
117    for dut in duts:
118        check_device(devices, setup_params, dut)
119    for ref in refs:
120        check_device(devices, setup_params, ref)
121    for monitor in monitors:
122        if monitor == "all":
123            continue
124        check_device(devices, setup_params, monitor, monitor=True)
125