#!/usr/bin/python
#
# Copyright (C) 2005  Alexandre Boeglin <alex@boeglin.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

import sys

f = open(sys.argv[1])
line_list = f.readlines()

packet_min = 64
packet_max = 370

processing = 0
number = 0
value = 0
data = 0
data_len = 0

for line in line_list :
  if line.startswith('[') :
    if not processing and 'going down' in line :
      processing = 1
      number += 1
    elif processing :
      if number >= packet_min and number <= packet_max :
        key = data_len + value + (value >> 8)
        for i in range(data_len) :
          key = key + ((data >> (i << 3)) & 0xFF)
        key = ((key ^ 0xFF) + 1) & 0xFF
        print ':%02X%04X00%s%02X' % (
            data_len,
            value,
            ('%X' % data).zfill(data_len << 1),
            key)
      processing = 0
      data = 0
    continue
  if processing :
    param = line.split()[0]
    if param == 'Value' :
      value = int(line.split()[2], 16)
    elif param == 'TransferBufferLength' :
      data_len = int(line.split()[2], 16)
    elif param.startswith('0') :
      weight = int(param[:-1], 16)
      data += int(''.join(line.split()[1:]), 16) << (weight << 3)

print ':00000001FF'
