2 # Copyright (c) 2016, Tieto Corporation
4 # This software may be distributed under the terms of the BSD license.
5 # See README for more details.
8 from remotehost import Host
13 logger = logging.getLogger()
16 # standalone monitor with multi iface support
17 def create(devices, setup_params, refs, duts, monitors):
22 # choose only standalone monitors
23 for monitor in monitors:
24 if monitor not in hosts and monitor != "all":
28 dev = config.get_device(devices, mon)
32 host = Host(host = dev['hostname'],
33 ifname = dev['ifname'],
38 host.execute(["iw", "reg", "set", setup_params['country']])
39 setup_hw = setup_params['setup_hw']
41 host.execute(setup_hw + " -I " + iface + " -R 1")
48 def destroy(devices, hosts):
51 for monitor in host.monitors:
52 host.execute(["ifconfig", monitor, "down"])
54 def setup(host, monitor_params):
58 ifaces = re.split('; | |, ', host.ifname)
60 for param in monitor_params:
64 logger.debug(traceback.format_exc())
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']])
73 logger.debug("Could not setup monitor interface: " + buf)
75 host.monitors.append(iface)
78 def run(host, setup_params):
83 if len(host.monitors) == 0:
86 log_dir = setup_params['log_dir']
87 tc_name = setup_params['tc_name']
92 for monitor in host.monitors:
93 host.execute(["ifconfig", monitor, "up"])
94 tshark = tshark + " -i " + monitor
95 log_monitor = log_monitor + "_" + monitor
97 log = log_dir + tc_name + "_" + host.name + log_monitor + ".pcap"
99 thread = host.execute_run([tshark, "-w", log], monitor_res)
106 if len(host.monitors) == 0:
108 if host.thread is None:
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")
117 # Add monitor to existing interface
118 def add(host, monitors):
122 for monitor in monitors:
123 if monitor != "all" and monitor != host.name:
125 mon = "mon_" + host.ifname
126 status, buf = host.execute(["iw", host.ifname, "interface", "add", mon,
129 host.monitors.append(mon)
130 host.execute(["ifconfig", mon, "up"])
132 logger.debug("Could not add monitor for " + host.name)
136 for monitor in host.monitors:
137 host.execute(["iw", monitor, "del"])
138 host.monitors.remove(monitor)
141 # get monitor params from hostapd
142 def get_monitor_params(hapd):
143 freq = hapd.get_status_field("freq")
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":
155 center_freq1 = str(int(freq) + 10)
156 elif secondary_channel == "-1":
157 center_freq1 = str(int(freq) - 10)
160 elif vht_oper_chwidth == "1":
162 center_freq1 = str(int(vht_oper_centr_freq_seg0_idx) * 5 + 5000)
163 elif vht_oper_chwidth == "2":
165 center_freq1 = str(int(vht_oper_centr_freq_seg0_idx) * 5 + 5000)
166 elif vht_oper_chwidth == "3":
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)
173 monitor_params = { "freq" : freq,
175 "center_freq1" : center_freq1,
176 "center_freq2" : center_freq2 }
178 return monitor_params