def setPayloadId(self, id): '''Set payload id''' if isinstance(id, list): id = protocols.from8bit(id) self._payload_id = id
def setPayloadLengthParsed(self, length): '''Save parsed payload length''' if isinstance(length, list): length = protocols.from8bit(length) self._payload_parsed_length = length
def processIncomingPacket(self, packet): '''Takes a raw packet, checks it and returns the correct packet class''' # Quick checks to make sure this is a legitimate packet if not isinstance(packet, list): raise TypeError, 'Expected a list' if packet[0] != START_BYTE or packet[-1] != END_BYTE: raise Exception, 'Start and/or end byte missing' packet = self._unEscape(packet) contents = {} contents['flags'] = None contents['payload_id'] = None contents['payload'] = [] contents['checksum'] = None index = 1 # Grab flags contents['flags'] = flags = packet[index] index += 1 # Grab payload id contents['payload_id'] = protocols.from8bit(packet[index:index+2]) index += 2 # Grab payload contents['payload'] = payload = packet[index:(len(packet) - 2)] index += len(payload) # Grab checksum contents['checksum'] = checksum = packet[index] index += 1 # Check checksum gen_checksum = getChecksum(packet[1:index-1]) if checksum != gen_checksum: raise Exception, 'Checksum is incorrect! Provided: %d, generated: %d' % (checksum, gen_checksum) # Just double check we only have one byte left if index != (len(packet) - 1): raise Exception, 'Packet incorrectly processed, %d bytes left' % (len(packet) - 1 - index) # Create response packet object if contents['flags'] & HEADER_IS_PROTO: packet_types = self._response_protocol_packets else: packet_types = self._response_firmware_packets try: type = packet_types[contents['payload_id']] except KeyError: type = 'responseGeneric' if not hasattr(self, type): type = 'responseGeneric' response = getattr(self, type)() # Populate data response.parseHeaderFlags(contents['flags']) response.setPayloadId(contents['payload_id']) response.parsePayload(contents['payload']) response.validate() return response