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
14 logger = logging.getLogger()
17 # standalone monitor with multi iface support
18 def create(devices, setup_params, refs, duts, monitors):
23 # choose only standalone monitors
24 for monitor in monitors:
25 if monitor not in hosts and monitor != "all":
29 dev = config.get_device(devices, mon)
33 host = Host(host = dev['hostname'],
34 ifname = dev['ifname'],
39 host.execute(["iw", "reg", "set", setup_params['country']])
40 rutils.setup_hw_host(host, setup_params, True)
47 def destroy(devices, hosts):
50 for monitor in host.monitors:
51 host.execute(["ifconfig", monitor, "down"])
53 def setup(host, monitor_params):
57 ifaces = re.split('; | |, ', host.ifname)
59 for param in monitor_params:
63 logger.debug(traceback.format_exc())
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']])
72 logger.debug("Could not setup monitor interface: " + buf)
74 host.monitors.append(iface)
77 def run(host, setup_params):
82 if len(host.monitors) == 0:
85 log_dir = setup_params['log_dir']
86 tc_name = setup_params['tc_name']
91 for monitor in host.monitors:
92 host.execute(["ifconfig", monitor, "up"])
93 tshark = tshark + " -i " + monitor
94 log_monitor = log_monitor + "_" + monitor
96 log = log_dir + tc_name + "_" + host.name + log_monitor + ".pcap"
98 thread = host.execute_run([tshark, "-w", log], monitor_res)
105 if len(host.monitors) == 0:
107 if host.thread is None:
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")
116 # Add monitor to existing interface
117 def add(host, monitors):
121 for monitor in monitors:
122 if monitor != "all" and monitor != host.name:
124 mon = "mon_" + host.ifname
125 status, buf = host.execute(["iw", host.ifname, "interface", "add", mon,
128 host.monitors.append(mon)
129 host.execute(["ifconfig", mon, "up"])
131 logger.debug("Could not add monitor for " + host.name)
135 for monitor in host.monitors:
136 host.execute(["iw", monitor, "del"])
137 host.monitors.remove(monitor)
140 # get monitor params from hostapd/wpa_supplicant
141 def get_monitor_params(wpa, is_p2p=False):
143 get_status_field_f = wpa.get_group_status_field
145 get_status_field_f = wpa.get_status_field
146 freq = get_status_field_f("freq")
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":
158 center_freq1 = str(int(freq) + 10)
159 elif secondary_channel == "-1":
160 center_freq1 = str(int(freq) - 10)
163 elif vht_oper_chwidth == "1":
165 center_freq1 = str(int(vht_oper_centr_freq_seg0_idx) * 5 + 5000)
166 elif vht_oper_chwidth == "2":
168 center_freq1 = str(int(vht_oper_centr_freq_seg0_idx) * 5 + 5000)
169 elif vht_oper_chwidth == "3":
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)
176 monitor_params = { "freq" : freq,
178 "center_freq1" : center_freq1,
179 "center_freq2" : center_freq2 }
181 return monitor_params