tests/remote: Use a function to add a log file to a remote host
[mech_eap.git] / tests / remote / monitor.py
1 # Monitor support
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
7 import time
8 from remotehost import Host
9 import config
10 import re
11 import traceback
12 import logging
13 logger = logging.getLogger()
14 import hostapd
15
16 # standalone monitor with multi iface support
17 def create(devices, setup_params, refs, duts, monitors):
18     mons= []
19     mhosts = []
20     hosts = duts + refs
21
22     # choose only standalone monitors
23     for monitor in monitors:
24         if monitor not in hosts and monitor != "all":
25             mons.append(monitor)
26
27     for mon in mons:
28         dev = config.get_device(devices, mon)
29         if dev is None:
30             continue
31
32         host = Host(host = dev['hostname'],
33                     ifname = dev['ifname'],
34                     port = dev['port'],
35                     name = dev['name'])
36
37         try:
38             host.execute(["iw", "reg", "set", setup_params['country']])
39             setup_hw = setup_params['setup_hw']
40             for iface in ifaces:
41                 host.execute(setup_hw + " -I " + iface + " -R 1")
42         except:
43             pass
44         mhosts.append(host)
45
46     return mhosts
47
48 def destroy(devices, hosts):
49     for host in hosts:
50         stop(host)
51         for monitor in host.monitors:
52             host.execute(["ifconfig", monitor, "down"])
53
54 def setup(host, monitor_params):
55     if host is None:
56         return
57
58     ifaces = re.split('; | |, ', host.ifname)
59     count = 0
60     for param in monitor_params:
61         try:
62             iface = ifaces[count]
63         except:
64             logger.debug(traceback.format_exc())
65             break
66         host.execute(["ifconfig", iface, " down"])
67         host.execute(["iw", iface, "set type monitor"])
68         host.execute(["ifconfig", iface, "up"])
69         status, buf = host.execute(["iw", iface, "set", "freq", param['freq'],
70                                     param['bw'], param['center_freq1'],
71                                     param['center_freq2']])
72         if status != 0:
73             logger.debug("Could not setup monitor interface: " + buf)
74             continue
75         host.monitors.append(iface)
76         count = count + 1
77
78 def run(host, setup_params):
79     monitor_res = []
80     log_monitor = ""
81     if host is None:
82         return None
83     if len(host.monitors) == 0:
84         return None
85     try:
86         log_dir = setup_params['log_dir']
87         tc_name = setup_params['tc_name']
88     except:
89         return None
90
91     tshark = "tshark"
92     for monitor in host.monitors:
93         host.execute(["ifconfig", monitor, "up"])
94         tshark = tshark + " -i " + monitor
95         log_monitor = log_monitor + "_" + monitor
96
97     log = log_dir + tc_name + "_" + host.name + log_monitor + ".pcap"
98     host.add_log(log)
99     thread = host.execute_run([tshark, "-w", log], monitor_res)
100     host.thread = thread
101
102
103 def stop(host):
104     if host is None:
105         return
106     if len(host.monitors) == 0:
107         return
108     if host.thread is None:
109         return
110
111     host.execute(["killall", "-s", "INT", "tshark"])
112     host.wait_execute_complete(host.thread, 5)
113     if host.thread.isAlive():
114        raise Exception("tshark still alive")
115     host.thread = None
116
117 # Add monitor to existing interface
118 def add(host, monitors):
119     if host is None:
120         return
121
122     for monitor in monitors:
123         if monitor != "all" and monitor != host.name:
124             continue
125         mon = "mon_" + host.ifname
126         status, buf = host.execute(["iw", host.ifname, "interface", "add", mon,
127                                     "type", "monitor"])
128         if status == 0:
129             host.monitors.append(mon)
130             host.execute(["ifconfig", mon, "up"])
131         else:
132             logger.debug("Could not add monitor for " + host.name)
133
134 def remove(host):
135     stop(host)
136     for monitor in host.monitors:
137         host.execute(["iw", monitor, "del"])
138         host.monitors.remove(monitor)
139
140
141 # get monitor params from hostapd
142 def get_monitor_params(hapd):
143     freq = hapd.get_status_field("freq")
144     bw = "20"
145     center_freq1=""
146     center_freq2=""
147
148     vht_oper_chwidth = hapd.get_status_field("vht_oper_chwidth")
149     secondary_channel = hapd.get_status_field("secondary_channel")
150     vht_oper_centr_freq_seg0_idx = hapd.get_status_field("vht_oper_centr_freq_seg0_idx")
151     vht_oper_centr_freq_seg1_idx = hapd.get_status_field("vht_oper_centr_freq_seg1_idx")
152     if vht_oper_chwidth == "0" or vht_oper_chwidth is None:
153         if secondary_channel == "1":
154             bw = "40"
155             center_freq1 = str(int(freq) + 10)
156         elif secondary_channel == "-1":
157             center_freq1 = str(int(freq) - 10)
158         else:
159             pass
160     elif vht_oper_chwidth == "1":
161         bw = "80"
162         center_freq1 = str(int(vht_oper_centr_freq_seg0_idx) * 5 + 5000)
163     elif vht_oper_chwidth == "2":
164         bw = "160"
165         center_freq1 = str(int(vht_oper_centr_freq_seg0_idx) * 5 + 5000)
166     elif vht_oper_chwidth == "3":
167         bw = "80+80"
168         center_freq1 = str(int(vht_oper_centr_freq_seg0_idx) * 5 + 5000)
169         center_freq2 = str(int(vht_oper_centr_freq_seg1_idx) * 5 + 5000)
170     else:
171         pass
172
173     monitor_params = { "freq" : freq,
174                        "bw" : bw,
175                        "center_freq1" : center_freq1,
176                        "center_freq2" : center_freq2 }
177
178     return monitor_params