Go to the documentation of this file.00001 import struct
00002
00003 HEADER_START = 0xfe
00004 CONFIG_HEADER = 0xfc
00005 LOAD_HEADER = 0xfb
00006 DONE_HEADER = 0xfa
00007
00008 TAKEOVER_HEADER_SIZE = 3
00009
00010
00011 class TakeoverHeader(object):
00012 def __init__(self, takeover_id, checksum=0):
00013 super(TakeoverHeader, self).__init__()
00014 self._takeover_id = takeover_id
00015 self._checksum = checksum
00016
00017 def to_bytes(self):
00018 return struct.pack('<B', HEADER_START) \
00019 + struct.pack('<B', self._takeover_id) \
00020 + struct.pack('<H', self._checksum)
00021
00022 def cal_checksum(self):
00023 data = self.to_bytes()[1:]
00024 checksum = 0
00025 for byt in data:
00026 checksum += ord(byt)
00027
00028 return checksum
00029
00030 def get_checksum(self):
00031 return self._checksum
00032
00033 def set_checksum(self, value):
00034 self._checksum = value
00035
00036 def get_id(self):
00037 return self._takeover_id
00038
00039 def get_length(self):
00040 return TAKEOVER_HEADER_SIZE
00041
00042 def pkg_ok(self):
00043 prev_checksum = self.get_checksum()
00044 self.set_checksum(0)
00045 self.set_checksum(self.cal_checksum())
00046 return prev_checksum == self.get_checksum()
00047
00048 def convert_to_pkg(self, raw_data):
00049 self._takeover_id = struct.unpack('<B', bytearray(raw_data[0]))[0]
00050 self._checksum = struct.unpack('<H', bytearray(raw_data[1:3]))[0]