コード例 #1
0
    def __init__(self, packetString=None, header=None, data=None, checkCRC=True):
        if (packetString != None):
            # Sanity check
            packetStringLength = len(packetString)
            if (packetStringLength < 4):
                raise InvalidPacketFormatError( \
                    'Impossible packet, must have a packet of at least 4 bytes but got {0}' \
                        .format(packetStringLength))

            # The string can either be bytes or an actual string
            # Force it to a string if that is the case.
            if (type(packetString) == str):
                packetString = packetString.encode('iso-8859-1')

            # Extract the header information
            self.headerLength = 4
            headerString = packetString[:self.headerLength]
            ctrlByte, packetLength, crc, command \
                = struct.unpack(Formatting.CommandData.Header, headerString)

            # Extract the value from the subsystem byte
            subSystem = ctrlByte & BitMask.SubSystem

            # Check if the response byte is an acknowledge
            packetTypeCode = ctrlByte & BitMask.PacketType
            packetType = packetTypeCode >> BitPosition.PacketType

            # See if the packet is a response or a command packet
            if (packetType == PacketType.Command):
                raise InvalidPacketFormatError('Cannot create a response packet with the string of a command packet.')

            self.header = NebHeader(subSystem, packetType, command, crc, packetLength)

            # Extract the data substring
            dataString = packetString[self.headerLength:self.headerLength + packetLength]

            # Perform CRC of data bytes
            if (checkCRC):
                calculatedCRC = nebUtilities.genNebCRC8(bytearray(packetString))
                if calculatedCRC != self.header.crc:
                    raise CRCError(calculatedCRC, self.header.crc)

            if packetType == PacketType.Ack:
                self.data = AckData()
            else:
                # Build the data object based on the subsystem and command.
                self.data = ResponsePacketDataConstructors[subSystem][self.header.command](dataString)

        elif (header != None and data != None):
            self.header = header
            self.data = data
コード例 #2
0
 def createResponsePacket(self, subSystem, commands, data, dataString):
     crc = nebUtilities.crc8(bytearray(dataString))
     header = NebHeader(subSystem, False, commands, crc, len(dataString))
     responsePacket = NebResponsePacket(packetString=None, header=header, data=data, checkCRC=False)
     responsePacket.header.crc = nebUtilities.genNebCRC8(bytearray(responsePacket.stringEncode()))
     return responsePacket