Updated through tag hostap_2_5 from git://w1.fi/hostap.git
[mech_eap.git] / libeap / tests / hwsim / tshark.py
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
10 import time
11 import subprocess
12 import logging
13 logger = logging.getLogger()
14
15
16 _tshark_filter_arg = '-Y'
17
18 def run_tshark(filename, filter, display=None, wait=True):
19     global _tshark_filter_arg
20
21     if wait:
22         # wait a bit to make it more likely for wlantest sniffer to have
23         # captured and written the results into a file that we can process here
24         time.sleep(1)
25
26     try:
27         arg = [ "tshark", "-r", filename,
28                 _tshark_filter_arg, filter ]
29         if display:
30             arg.append('-Tfields')
31             for d in display:
32                 arg.append('-e')
33                 arg.append(d)
34         else:
35             arg.append('-V')
36         cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
37                                stderr=open('/dev/null', 'w'))
38     except Exception, e:
39         logger.info("Could run run tshark check: " + str(e))
40         cmd = None
41         return None
42
43     out = cmd.communicate()[0]
44     res = cmd.wait()
45     if res == 1:
46         # remember this for efficiency
47         _tshark_filter_arg = '-R'
48         arg[3] = '-R'
49         cmd = subprocess.Popen(arg, stdout=subprocess.PIPE,
50                                stderr=open('/dev/null', 'w'))
51         out = cmd.communicate()[0]
52         cmd.wait()
53
54     return out