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