def __init__(self, actions=[], generate_tag_request_action=True, tag_id=None, send_tag_response_when_completed=True): self.actions = [] self.interface_status = None self.generate_tag_request_action = generate_tag_request_action self.tag_id = tag_id self.send_tag_response_when_completed = send_tag_response_when_completed for action in actions: if type( action ) == StatusAction and action.status_operand_extension == StatusActionOperandExtensions.INTERFACE_STATUS: if self.interface_status != None: raise ParseError( "An ALP command can contain one and only one Interface Status action" ) self.interface_status = action if type(action) == TagRequestAction: if self.tag_id != None: raise ParseError( "An ALP command can contain one and only one Tag Request Action" ) self.tag_id = action.operand.tag_id self.send_tag_response_when_completed = action.respond_when_completed # we don't add this to self.actions but prepend it on serializing if type(action) == RegularAction: self.actions.append(action) if self.generate_tag_request_action and self.tag_id == None: self.tag_id = random.randint(0, 255) super(Command, self).__init__()
def parse_serial_interface_header(self, s): b = s.read("uint:8") if b != 0xC0: raise ParseError("expected 0xC0, found {0}".format(b)) version = s.read("uint:8") if version != 0: raise ParseError("Expected version 0, found {0}".format(version)) counter = s.read("uint:8") message_type = s.read("uint:8") #TODO different handler? cmd_len = s.read("uint:8") crc1 = s.read("uint:8") crc2 = s.read("uint:8") if len(self.buffer) - s.bytepos < cmd_len: raise ReadError("ALP command not complete yet, expected {0} bytes, got {1}".format(cmd_len, s.len - s.bytepos)) payload = s.peeklist('bytes:b', b=cmd_len)[0] crc = calculate_crc(bytes(payload)) self.down_counter = self.down_counter + 1 if self.down_counter > 255: self.down_counter = 0 if counter != self.down_counter: pprint("counters not equal") #TODO consequence? self.down_counter = counter #reset counter if crc[0] != crc1 or crc[1] != crc2: raise ParseError("CRC is incorrect found {} {} and expected {} {}".format(crc1, crc2, crc[0], crc[1])) return cmd_len, message_type
def parse_serial_interface_header(self, s): b = s.read("uint:8") if b != 0xC0 : raise ParseError("expected 0xC0, found {0}".format(b)) version = s.read("uint:8") if version != 0: raise ParseError("Expected version 0, found {0}".format(version)) cmd_len = s.read("uint:8") if len(self.buffer) - s.bytepos < cmd_len: raise ReadError("ALP command not complete yet, expected {0} bytes, got {1}".format(cmd_len, s.len - s.bytepos)) return cmd_len
def parse_tag_request_action(self, b7, b6, s): if b6: raise ParseError("bit 6 is RFU") tag_id = s.read("uint:8") return TagRequestAction( respond_when_completed=b7, operation=TagRequest(operand=TagId(tag_id=tag_id)))
def parse_alp_return_status_action(self, b7, b6, s): if b7: raise ParseError("Status Operand extension 2 and 3 is RFU") if b6: # interface status interface_id = s.read("uint:8") try: interface_status_operation = { 0x00 : self.parse_alp_interface_status_host, 0xd7 : self.parse_alp_interface_status_d7asp, }[interface_id](s) return StatusAction(operation=interface_status_operation, status_operand_extension=StatusActionOperandExtensions.INTERFACE_STATUS) except KeyError: raise ParseError("Received ALP Interface status for interface " + str(interface_id) + " which is not implemented") else: # action status pass # TODO
def parse_forward_action(self, b7, b6, s): if b7: raise ParseError("bit 7 is RFU") interface_id = InterfaceType(int(s.read("uint:8"))) assert(interface_id == InterfaceType.D7ASP) interface_config = Configuration.parse(s) return ForwardAction(resp=b6, operation=Forward(operand=InterfaceConfiguration(interface_id=interface_id, interface_configuration=interface_config)))
def parse_alp_action(self, s): # meaning of first 2 bits depend on action opcode b7 = s.read("bool") b6 = s.read("bool") op = s.read("uint:6") try: return{ 1 : self.parse_alp_read_file_data_action, 32 : self.parse_alp_return_file_data_action, 34 : self.parse_alp_return_status_action, 52 : self.parse_tag_request_action }[op](b7, b6, s) except KeyError: raise ParseError("alp_action " + str(op) + " is not implemented")
def parse_forward_action(self, b7, b6, s): if b7: raise ParseError("bit 7 is RFU") interface_id = InterfaceType(int(s.read("uint:8"))) interface_config = None if (interface_id == InterfaceType.D7ASP): interface_config = Configuration.parse(s) elif (interface_id == InterfaceType.SERIAL): pass # no interface config else: assert (False) return ForwardAction(resp=b6, operation=Forward(operand=InterfaceConfiguration( interface_id=interface_id, interface_configuration=interface_config)))
def parse_alp_action(self, s): # meaning of first 2 bits depend on action opcode b7 = s.read("bool") b6 = s.read("bool") op = s.read("uint:6") try: return { 1: self.parse_alp_read_file_data_action, 4: self.parse_alp_write_file_data_action, 9: self.parse_break_query_action, 32: self.parse_alp_return_file_data_action, 33: self.parse_alp_return_file_header_action, 34: self.parse_alp_return_status_action, 35: self.parse_tag_response_action, 50: self.parse_forward_action, 51: self.parse_indirect_forward_action, 52: self.parse_tag_request_action }[op](b7, b6, s) except KeyError: raise ParseError("alp_action " + str(op) + " is not implemented")