示例#1
0
def test_cmd_response_class():
    response = parse_cmd_response(b"\x01\x00\x00\x01\x00\x00\x00\x00")
    assert isinstance(response, CmdResponse)
    assert response.header == CmdHeader(CommandTag.FLASH_ERASE_ALL, 0, 0, 1)
    assert response.raw_data == b"\x00\x00\x00\x00"
    assert response != parse_cmd_response(
        b"\xA0\x00\x00\x02\x00\x00\x00\x00\x01\x00\x00\x00")
    assert response.info()
    assert str(response)
    assert repr(response)
示例#2
0
def test_get_property_response_class():
    response = parse_cmd_response(
        b"\xA7\x00\x00\x02\x00\x00\x00\x00\x01\x00\x00\x00")
    assert isinstance(response, GetPropertyResponse)
    assert response.header == CmdHeader(ResponseTag.GET_PROPERTY, 0, 0, 2)
    assert response.status == 0
    assert response.values == [1]
    assert response.info()
示例#3
0
def test_read_memory_response_class():
    response = parse_cmd_response(
        b"\xA3\x00\x00\x02\x00\x00\x00\x00\x01\x00\x00\x00")
    assert isinstance(response, ReadMemoryResponse)
    assert response.header == CmdHeader(ResponseTag.READ_MEMORY, 0, 0, 2)
    assert response.status == 0
    assert response.length == 1
    assert response.info()
示例#4
0
def test_generic_response_class():
    response = parse_cmd_response(
        b"\xA0\x00\x00\x02\x00\x00\x00\x00\x01\x00\x00\x00")
    assert isinstance(response, GenericResponse)
    assert response.header == CmdHeader(ResponseTag.GENERIC, 0, 0, 2)
    assert response.status == 0
    assert response.cmd_tag == CommandTag.FLASH_ERASE_ALL
    assert response.info()
示例#5
0
def test_flash_read_once_response_class():
    response = parse_cmd_response(
        b"\xAF\x00\x00\x03\x00\x00\x00\x00\x04\x00\x00\x00\x01\x00\x00\x00")
    assert isinstance(response, FlashReadOnceResponse)
    assert response.header == CmdHeader(ResponseTag.FLASH_READ_ONCE, 0, 0, 3)
    assert response.status == 0
    assert response.length == 4
    assert response.data == b"\x01\x00\x00\x00"
    assert response.info()
示例#6
0
def test_flash_read_resource_response_class():
    response = parse_cmd_response(
        b"\xB0\x00\x00\x02\x00\x00\x00\x00\x04\x00\x00\x00")
    assert isinstance(response, FlashReadResourceResponse)
    assert response.header == CmdHeader(ResponseTag.FLASH_READ_RESOURCE, 0, 0,
                                        2)
    assert response.status == 0
    assert response.length == 4
    assert response.info()
示例#7
0
def test_tp_hsm_gen_key_response_class():
    response = parse_cmd_response(
        b"\xb6\x00\x00\x03\x00\x00\x00\x00\x30\x00\x00\x00\x40\x00\x00\x00")
    assert isinstance(response, TrustProvisioningResponse)
    assert response.header == CmdHeader(tag=0xB6,
                                        flags=0x00,
                                        reserved=0,
                                        params_count=3)
    assert response.status == 0
    assert response.values == [48, 64]
    assert response.info()
示例#8
0
 def read(self, timeout=1000):
     if self._dev_conf.valid_cmd(self._cmd_tag):
         cmd, raw_data = self.CMD[self._cmd_tag](*self._cmd_params,
                                                 index=self._response_index,
                                                 config=self._dev_conf,
                                                 fail_step=self.fail_step)
         self._response_index += 1
     else:
         cmd, raw_data = pack_response(ResponseTag.GENERIC,
                                       StatusCode.UNKNOWN_COMMAND,
                                       self._cmd_tag)
     logging.debug(f"RAW-IN [{len(raw_data)}]: " +
                   ', '.join(f"{b:02X}" for b in raw_data))
     return parse_cmd_response(raw_data) if cmd else raw_data
示例#9
0
    def read(self) -> Union[CmdResponse, bytes]:
        """Read data from device.

        :return: read data
        :rtype: Union[spsdk.mboot.commands.CmdResponse, bytes]
        """
        _, frame_type = self._read_frame_header()
        length = to_int(self._read(2))
        crc = to_int(self._read(2))
        data = self._read(length)
        self._send_ack()
        calculated_crc = self._calc_frame_crc(data, frame_type)
        assert crc == calculated_crc, "Received invalid CRC"
        if frame_type == FPType.CMD:
            return parse_cmd_response(data)
        return data
示例#10
0
    def read(self, timeout: int = 1000) -> Union[CmdResponse, bytes]:
        """Read data from device.

        :param timeout: read timeout in milliseconds, defaults to 1000
        :type timeout: int, optional
        :return: read data
        :rtype: Union[spsdk.mboot.commands.CmdResponse, bytes]
        """
        _, frame_type = self._read_frame_header()
        length = to_int(self._read(2))
        crc = to_int(self._read(2))
        data = self._read(length)
        self._send_ack()
        calculated_crc = self._calc_frame_crc(data, frame_type)
        assert crc == calculated_crc, "Received invalid CRC"
        if frame_type == FPType.CMD:
            return parse_cmd_response(data)
        return data
示例#11
0
    def read(self) -> Union[CmdResponse, bytes]:
        """Read data from device.

        :return: read data
        :rtype: Union[spsdk.mboot.commands.CmdResponse, bytes]
        :raises McuBootDataAbortError: Indicates data transmission abort
        """
        _, frame_type = self._read_frame_header()
        length = to_int(self._read(2))
        crc = to_int(self._read(2))
        if not length:
            self._send_ack()
            raise McuBootDataAbortError()
        data = self._read(length)
        self._send_ack()
        calculated_crc = self._calc_frame_crc(data, frame_type)
        assert crc == calculated_crc, "Received invalid CRC"
        if frame_type == FPType.CMD:
            return parse_cmd_response(data)
        return data
示例#12
0
    def read(self) -> Union[CmdResponse, bytes]:
        """Read data from device.

        :return: read data
        :raises McuBootDataAbortError: Indicates data transmission abort
        :raises McuBootConnectionError: When received invalid CRC
        """
        _, frame_type = self._read_frame_header()
        length = to_int(self._read(2))
        crc = to_int(self._read(2))
        if not length:
            self._send_ack()
            raise McuBootDataAbortError()
        data = self._read(length)
        self._send_ack()
        calculated_crc = self._calc_frame_crc(data, frame_type)
        if crc != calculated_crc:
            raise McuBootConnectionError("Received invalid CRC")
        if frame_type == FPType.CMD:
            return parse_cmd_response(data)
        return data