import struct


class APCI:
    START = 0x68

    def __init__(self, length=0, send_seq=0, recv_seq=0):
        self.length = length
        self.send_seq = send_seq
        self.recv_seq = recv_seq

    def pack(self):
        return struct.pack('!BBHH', self.START, self.length, self.send_seq << 1, self.recv_seq << 1)

    @classmethod
    def unpack(cls, data):
        if len(data) < 6 or data[0] != cls.START:
            return None
        length = data[1]
        send_seq = (struct.unpack('!H', data[2:4])[0]) >> 1
        recv_seq = (struct.unpack('!H', data[4:6])[0]) >> 1
        return cls(length, send_seq, recv_seq)
