]> git.ipfire.org Git - thirdparty/hostap.git/blame - tests/hwsim/tshark.py
HE: MCS size is always a minimum of 4 bytes
[thirdparty/hostap.git] / tests / hwsim / tshark.py
CommitLineData
2e1d7386
JB
1#
2# tshark module - refactored from test_scan.py
3#
4# Copyright (c) 2014, Qualcomm Atheros, Inc.
5# Copyright (c) 2015, Intel Mobile Communications GmbH
6#
7# This software may be distributed under the terms of the BSD license.
8# See README for more details.
9
10import time
11import subprocess
12import logging
13logger = logging.getLogger()
14
7d1ebdec
JB
15class UnknownFieldsException(Exception):
16 def __init__(self, fields):
17 Exception.__init__(self, "unknown tshark fields %s" % ','.join(fields))
18 self.fields = fields
2e1d7386
JB
19
20_tshark_filter_arg = '-Y'
21
7d1ebdec 22def _run_tshark(filename, filter, display=None, wait=True):
2076846c
JM
23 global _tshark_filter_arg
24
efd0a6fb
JM
25 if wait:
26 # wait a bit to make it more likely for wlantest sniffer to have
27 # captured and written the results into a file that we can process here
d2e7cfc0 28 time.sleep(0.1)
2e1d7386
JB
29
30 try:
fab49f61
JM
31 arg = ["tshark", "-r", filename,
32 _tshark_filter_arg, filter]
2e1d7386
JB
33 if display:
34 arg.append('-Tfields')
35 for d in display:
36 arg.append('-e')
37 arg.append(d)
38 else:
39 arg.append('-V')
40 cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
2db07d16 41 stderr=subprocess.PIPE)
bab493b9 42 except Exception as e:
2e1d7386
JB
43 logger.info("Could run run tshark check: " + str(e))
44 cmd = None
45 return None
46
2db07d16 47 output = cmd.communicate()
2a79a8ce
JM
48 out = output[0].decode(errors='ignore')
49 out1 = output[1].decode()
2e1d7386
JB
50 res = cmd.wait()
51 if res == 1:
7d1ebdec 52 errmsg = "Some fields aren't valid"
2a79a8ce
JM
53 if errmsg in out1:
54 errors = out1.split('\n')
7d1ebdec
JB
55 fields = []
56 collect = False
57 for f in errors:
58 if collect:
59 f = f.strip()
60 if f:
61 fields.append(f)
62 continue
63 if errmsg in f:
64 collect = True
65 continue
66 raise UnknownFieldsException(fields)
2e1d7386
JB
67 # remember this for efficiency
68 _tshark_filter_arg = '-R'
69 arg[3] = '-R'
70 cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
71 stderr=open('/dev/null', 'w'))
2a79a8ce 72 out = cmd.communicate()[0].decode()
2e1d7386 73 cmd.wait()
e384b156 74 if res == 2:
2a79a8ce
JM
75 if "tshark: Neither" in out1 and "are field or protocol names" in out1:
76 errors = out1.split('\n')
e384b156
JM
77 fields = []
78 for f in errors:
79 if f.startswith("tshark: Neither "):
80 f = f.split(' ')[2].strip('"')
81 if f:
82 fields.append(f)
83 continue
84 raise UnknownFieldsException(fields)
2e1d7386
JB
85
86 return out
7d1ebdec
JB
87
88def run_tshark(filename, filter, display=None, wait=True):
89 if display is None: display = []
90 try:
91 return _run_tshark(filename, filter, display, wait)
bab493b9 92 except UnknownFieldsException as e:
7d1ebdec
JB
93 all_wlan_mgt = True
94 for f in e.fields:
95 if not f.startswith('wlan_mgt.'):
96 all_wlan_mgt = False
97 break
98 if not all_wlan_mgt:
99 raise
100 return _run_tshark(filename, filter.replace('wlan_mgt', 'wlan'),
101 [x.replace('wlan_mgt', 'wlan') for x in display],
102 wait)
2cbaf0de
JM
103
104def run_tshark_json(filename, filter):
fab49f61
JM
105 arg = ["tshark", "-r", filename,
106 _tshark_filter_arg, filter]
2cbaf0de
JM
107 arg.append('-Tjson')
108 arg.append('-x')
109 try:
110 cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
111 stderr=subprocess.PIPE)
bab493b9 112 except Exception as e:
2cbaf0de
JM
113 logger.info("Could run run tshark: " + str(e))
114 return None
115 output = cmd.communicate()
2a79a8ce 116 out = output[0].decode()
2cbaf0de
JM
117 res = cmd.wait()
118 return out