SHARE
TWEET

Untitled

a guest Feb 17th, 2017 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. """
  4. Analyze serial sniffing log files created with jpnevulator.
  5.  
  6. This tool is able to analyze log files created similar to this:
  7.  
  8.     stty -F /dev/ttyUSB1 9600
  9.     stty -F /dev/ttyUSB2 9600
  10.     stty -F /dev/ttyUSB1
  11.     stty -F /dev/ttyUSB2
  12.     jpnevulator --timing-print \\
  13.         --tty /dev/ttyUSB1:SENDING \\
  14.         --tty /dev/ttyUSB2:RECEIVING \\
  15.         --read \\
  16.         --timing-delta=80000 \\
  17.         --ascii | tee huber-comm-log_XX_$(date -u +%Y-%m-%dT%H:%M:%S%z).txt
  18.  
  19. Their content looks like this:
  20.  
  21.     2015-08-10 14:39:50.025182: SENDING
  22.     9E 66 1E 06 98 E0 98 80 66 18 1E 98 86 98 86 98 .f......f.......
  23.     86 98 86 98 86 98 86 98 86 98 86 98 86 98 86 98 ................
  24.     86 98 86 98 86 60 E6 78 E6 80                   .....`.x..
  25.     2015-08-10 14:39:50.087181: RECEIVING
  26.     5B 53 30 31 43 31 39 30 30 30 30 30 30 30 30 30 [S01C19000000000
  27.     30 30 30 30 30 30 30 31 30 31 44 0D             0000000101D.
  28.  
  29. """
  30.  
  31. import argparse
  32. import pdb
  33. from datetime import datetime as dt
  34.  
  35. def hex_formatter(raw, bytes_per_line=None):
  36.     if bytes_per_line:
  37.         def chunks(l, n):
  38.             """Yield successive n-sized chunks from l."""
  39.             for i in range(0, len(l), n):
  40.                 yield l[i:i+n]
  41.         hex_bytes = list('{:02X}'.format(byte) for byte in raw)
  42.         return '\n'.join(' '.join(el) for el in chunks(hex_bytes, bytes_per_line))
  43.     else:
  44.         return ' '.join('{:02X}'.format(byte) for byte in raw)
  45.  
  46. def main():
  47.     parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
  48.     parser.add_argument('jpnevulator_log_file', help='')
  49.     args = parser.parse_args()
  50.  
  51.     sending, receiving = 'SENDING', 'RECEIVING'
  52.     bytes_per_line = 16
  53.     pause_time = 1.
  54.  
  55.     hex_chars_per_line = bytes_per_line * 3 - 1
  56.     num_incoming, num_outgoing = 0, 0
  57.     packets = []
  58.     current_direction = None
  59.     current_dt = None
  60.     current_buffer = b""
  61.     with open(args.jpnevulator_log_file) as fp:
  62.         for line in fp:
  63.             line = line.strip()
  64.             if not line: continue
  65.             kind = None
  66.             if len(line) >= 26 and line[2] != ' ':
  67.                 if current_buffer:
  68.                     packets.append({
  69.                       'direction': current_direction,
  70.                       'dt':        current_dt,
  71.                       'message':   current_buffer,
  72.                     })
  73.                     current_direction = None
  74.                     current_dt = None
  75.                     current_buffer = b""
  76.                 try:
  77.                     current_dt = dt.strptime(line[0:26], '%Y-%m-%d %H:%M:%S.%f')
  78.                 except ValueError:
  79.                     print("failing to analyze this line:")
  80.                     continue
  81.                 direction = line[28:]
  82.                 if direction == sending:
  83.                     current_direction = sending
  84.                     num_outgoing += 1
  85.                 elif direction == receiving:
  86.                     current_direction = receiving
  87.                     num_incoming += 1
  88.                 else:
  89.                     print("failing to analyze this line:")
  90.                     print(line)
  91.                 continue
  92.             if direction is None:
  93.                 print("discarding this line (no data direction detected so far):")
  94.                 print(line)
  95.                 continue
  96.             hex_data = line[0:hex_chars_per_line]
  97.             current_buffer += bytes(int(byte, 16) for byte in hex_data.split())
  98.  
  99.     print('Packets:')
  100.     last_dt, diff_dt = None, None
  101.     for packet in packets:
  102.         if last_dt:
  103.             diff_dt = (packet['dt'] - last_dt).total_seconds()
  104.         if diff_dt and diff_dt > pause_time: print('-----------------')
  105.         print("{dt} {diff_dt} {direction} {message}".format(diff_dt=diff_dt, **packet))
  106.         last_dt = packet['dt']
  107.  
  108.     print('Number of incoming frames: {}'.format(num_incoming))
  109.     print('Number of outgoing frames: {}'.format(num_outgoing))
  110.  
  111.  
  112. if __name__ == '__main__':
  113.     main()
RAW Paste Data
Top