[6320] | 1 | import sys |
---|
| 2 | import os |
---|
| 3 | import datetime |
---|
| 4 | import numpy as np |
---|
| 5 | import wlan_exp.util as wlan_exp_util |
---|
| 6 | import wlan_exp.log.util as log_util |
---|
| 7 | import wlan_exp.log.util_hdf as hdf_util |
---|
| 8 | import wlan_exp.log.util_sample_data as sample_data_util |
---|
| 9 | |
---|
| 10 | DEFAULT_LOGFILE = 'ap_one_node_capture.hdf5' |
---|
| 11 | logfile_error = False |
---|
| 12 | |
---|
| 13 | # Use log file given as command line argument, if present |
---|
| 14 | if(len(sys.argv) > 1): |
---|
| 15 | LOGFILE = str(sys.argv[1]) |
---|
| 16 | |
---|
| 17 | # Check if the string argument matchs a local file |
---|
| 18 | if not os.path.isfile(LOGFILE): |
---|
| 19 | # User specified non-existant file - give up and exit |
---|
| 20 | logfile_error = True |
---|
| 21 | |
---|
| 22 | else: |
---|
| 23 | # No command line argument - check if default file name exists locally |
---|
| 24 | LOGFILE = DEFAULT_LOGFILE |
---|
| 25 | |
---|
| 26 | if not os.path.isfile(LOGFILE): |
---|
| 27 | # No local file specified or found - check for matching sample data file |
---|
| 28 | try: |
---|
| 29 | LOGFILE = sample_data_util.get_sample_data_file(DEFAULT_LOGFILE) |
---|
| 30 | print("Local log file not found - Using sample data file!") |
---|
| 31 | except IOError as e: |
---|
| 32 | logfile_error = True |
---|
| 33 | |
---|
| 34 | if logfile_error: |
---|
| 35 | print("ERROR: Logfile {0} not found".format(LOGFILE)) |
---|
| 36 | sys.exit() |
---|
| 37 | else: |
---|
| 38 | print("Reading log file '{0}' ({1:5.1f} MB)\n".format(LOGFILE, (os.path.getsize(LOGFILE)/2**20))) |
---|
| 39 | |
---|
| 40 | if len(sys.argv) == 3: |
---|
| 41 | HDF5_FILE_OUT = str(sys.argv[2]) |
---|
| 42 | else: |
---|
| 43 | HDF5_FILE_OUT = 'np_rx_ofdm_entries.hdf5' |
---|
| 44 | |
---|
| 45 | print("WLAN Exp Log Example: OFDM Rx Entry Exporter") |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | # Extract the raw log data and log index from the HDF5 file |
---|
| 49 | log_data = hdf_util.hdf5_to_log_data(filename=LOGFILE) |
---|
| 50 | raw_log_index = hdf_util.hdf5_to_log_index(filename=LOGFILE) |
---|
| 51 | |
---|
| 52 | # Generate indexes with only Rx_OFDM events |
---|
| 53 | log_index_rx = log_util.filter_log_index(raw_log_index, include_only=['RX_OFDM'], |
---|
| 54 | merge={'RX_OFDM': ['RX_OFDM', 'RX_OFDM_LTG']}) |
---|
| 55 | |
---|
| 56 | # Generate numpy array of all OFDM Rx entries |
---|
| 57 | log_np = log_util.log_data_to_np_arrays(log_data, log_index_rx) |
---|
| 58 | log_rx_ofdm = log_np['RX_OFDM'] |
---|
| 59 | |
---|
| 60 | ################################################################# |
---|
| 61 | # Filter the OFDM Rx Entries |
---|
| 62 | # Find the source address for the most receptions |
---|
| 63 | |
---|
| 64 | # Extract unique values for address 2 (transmitting address in received MAC headers) |
---|
| 65 | uniq_addrs = np.unique(log_rx_ofdm['addr2']) |
---|
| 66 | |
---|
| 67 | # Count the number of receptions per source address |
---|
| 68 | num_rx = list() |
---|
| 69 | for ii,ua in enumerate(uniq_addrs): |
---|
| 70 | num_rx.append(np.sum(log_rx_ofdm['addr2'] == ua)) |
---|
| 71 | |
---|
| 72 | # Find the source address responsible for the most receptions |
---|
| 73 | most_rx = max(num_rx) |
---|
| 74 | most_common_addr = uniq_addrs[num_rx.index(most_rx)] |
---|
| 75 | |
---|
| 76 | print("Found {0} receptions from {1}".format(most_rx, wlan_exp_util.mac_addr_to_str(most_common_addr))) |
---|
| 77 | |
---|
| 78 | # Create new numpy array of all receptions from most-common source |
---|
| 79 | arr_rx_one_src = np.empty(most_rx, dtype=log_rx_ofdm.dtype) |
---|
| 80 | arr_rx_one_src[:] = log_rx_ofdm[(log_rx_ofdm['addr2'] == most_common_addr)] |
---|
| 81 | |
---|
| 82 | # Create some strings to use as attributes in the HDF5 file |
---|
| 83 | # - attributes are only for convenience - they aren't required for writing or reading HDF5 files |
---|
| 84 | root_desc = 'Source Log file: {0}\n'.format(LOGFILE) |
---|
| 85 | root_desc += 'HDF5 file written at {0}\n'.format(datetime.datetime.now()) |
---|
| 86 | |
---|
| 87 | ds_desc = 'All Rx OFDM entries from addr {0}\n'.format(wlan_exp_util.mac_addr_to_str(most_common_addr)) |
---|
| 88 | |
---|
| 89 | # Construct dictionaries to feed the HDF5 exporter |
---|
| 90 | # - The dict key names are used as HDF5 dataset names |
---|
| 91 | log_dict = {'RX_OFDM': arr_rx_one_src} |
---|
| 92 | attr_dict = {'/': root_desc, 'RX_OFDM': ds_desc} |
---|
| 93 | |
---|
| 94 | print('Generating HDF5 file {0}'.format(HDF5_FILE_OUT)) |
---|
| 95 | hdf_util.np_arrays_to_hdf5(HDF5_FILE_OUT, np_log_dict=log_dict, attr_dict=attr_dict) |
---|