aboutsummaryrefslogtreecommitdiffstats
path: root/tests/remote/monitor.py
blob: e5bcaad7a4c2e533d9dedcbfef690461ca4eddaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# Monitor support
# Copyright (c) 2016, Tieto Corporation
#
# This software may be distributed under the terms of the BSD license.
# See README for more details.

import time
from remotehost import Host
import config
import re
import traceback
import logging
logger = logging.getLogger()
import hostapd

# standalone monitor with multi iface support
def create(devices, setup_params, refs, duts, monitors):
    mons= []
    mhosts = []
    hosts = duts + refs

    # choose only standalone monitors
    for monitor in monitors:
        if monitor not in hosts and monitor != "all":
            mons.append(monitor)

    for mon in mons:
        dev = config.get_device(devices, mon)
        if dev is None:
            continue

        host = Host(host = dev['hostname'],
                    ifname = dev['ifname'],
                    port = dev['port'],
                    name = dev['name'])

        try:
            host.execute(["iw", "reg", "set", setup_params['country']])
            setup_hw = setup_params['setup_hw']
            for iface in ifaces:
                host.execute(setup_hw + " -I " + iface + " -R 1")
        except:
            pass
        mhosts.append(host)

    return mhosts

def destroy(devices, hosts):
    for host in hosts:
        stop(host)
        for monitor in host.monitors:
            host.execute(["ifconfig", monitor, "down"])

def setup(host, monitor_params):
    if host is None:
        return

    ifaces = re.split('; | |, ', host.ifname)
    count = 0
    for param in monitor_params:
        try:
            iface = ifaces[count]
        except:
            logger.debug(traceback.format_exc())
            break
        host.execute(["ifconfig", iface, " down"])
        host.execute(["iw", iface, "set type monitor"])
        host.execute(["ifconfig", iface, "up"])
        status, buf = host.execute(["iw", iface, "set", "freq", param['freq'],
                                    param['bw'], param['center_freq1'],
                                    param['center_freq2']])
        if status != 0:
            logger.debug("Could not setup monitor interface: " + buf)
            continue
        host.monitors.append(iface)
        count = count + 1

def run(host, setup_params):
    monitor_res = []
    log_monitor = ""
    if host is None:
        return None
    if len(host.monitors) == 0:
        return None
    try:
        log_dir = setup_params['log_dir']
        tc_name = setup_params['tc_name']
    except:
        return None

    tshark = "tshark"
    for monitor in host.monitors:
        host.execute(["ifconfig", monitor, "up"])
        tshark = tshark + " -i " + monitor
        log_monitor = log_monitor + "_" + monitor

    log = log_dir + tc_name + "_" + host.name + log_monitor + ".pcap"
    host.add_log(log)
    thread = host.execute_run([tshark, "-w", log], monitor_res)
    host.thread = thread


def stop(host):
    if host is None:
        return
    if len(host.monitors) == 0:
        return
    if host.thread is None:
        return

    host.execute(["killall", "-s", "INT", "tshark"])
    host.wait_execute_complete(host.thread, 5)
    if host.thread.isAlive():
       raise Exception("tshark still alive")
    host.thread = None

# Add monitor to existing interface
def add(host, monitors):
    if host is None:
        return

    for monitor in monitors:
        if monitor != "all" and monitor != host.name:
            continue
        mon = "mon_" + host.ifname
        status, buf = host.execute(["iw", host.ifname, "interface", "add", mon,
                                    "type", "monitor"])
        if status == 0:
            host.monitors.append(mon)
            host.execute(["ifconfig", mon, "up"])
        else:
            logger.debug("Could not add monitor for " + host.name)

def remove(host):
    stop(host)
    for monitor in host.monitors:
        host.execute(["iw", monitor, "del"])
        host.monitors.remove(monitor)


# get monitor params from hostapd/wpa_supplicant
def get_monitor_params(wpa, is_p2p=False):
    if is_p2p:
        get_status_field_f = wpa.get_group_status_field
    else:
        get_status_field_f = wpa.get_status_field
    freq = get_status_field_f("freq")
    bw = "20"
    center_freq1=""
    center_freq2=""

    vht_oper_chwidth = get_status_field_f("vht_oper_chwidth")
    secondary_channel = get_status_field_f("secondary_channel")
    vht_oper_centr_freq_seg0_idx = get_status_field_f("vht_oper_centr_freq_seg0_idx")
    vht_oper_centr_freq_seg1_idx = get_status_field_f("vht_oper_centr_freq_seg1_idx")
    if vht_oper_chwidth == "0" or vht_oper_chwidth is None:
        if secondary_channel == "1":
            bw = "40"
            center_freq1 = str(int(freq) + 10)
        elif secondary_channel == "-1":
            center_freq1 = str(int(freq) - 10)
        else:
            pass
    elif vht_oper_chwidth == "1":
        bw = "80"
        center_freq1 = str(int(vht_oper_centr_freq_seg0_idx) * 5 + 5000)
    elif vht_oper_chwidth == "2":
        bw = "160"
        center_freq1 = str(int(vht_oper_centr_freq_seg0_idx) * 5 + 5000)
    elif vht_oper_chwidth == "3":
        bw = "80+80"
        center_freq1 = str(int(vht_oper_centr_freq_seg0_idx) * 5 + 5000)
        center_freq2 = str(int(vht_oper_centr_freq_seg1_idx) * 5 + 5000)
    else:
        pass

    monitor_params = { "freq" : freq,
                       "bw" : bw,
                       "center_freq1" : center_freq1,
                       "center_freq2" : center_freq2 }

    return monitor_params