示例#1
0
    def test_output_wiht_crc(self):
        """ Test crc and is_crc functions. """
        field = Field.crc()

        self.assertEqual('crc', field.name)
        self.assertTrue(Field.is_crc(field))

        field = Field.padding(1)
        self.assertFalse(Field.is_crc(field))
    def __check_crc(cmd, result, extended_crc=False):
        # type: (MasterCommandSpec, Result, bool) -> bool
        """ Calculate the CRC of the data for a certain master command.

        :param cmd: instance of MasterCommandSpec.
        :param result: A dict containing the result of the master command.
        :param extended_crc: Indicates whether the action should be included in the crc
        :returns: boolean
        """
        crc = 0
        if extended_crc:
            crc += cmd.action[0]
            crc += cmd.action[1]
        for field in cmd.output_fields:
            if Field.is_crc(field):
                break
            else:
                for byte in field.encode(result[field.name]):
                    crc += byte
        return result['crc'] == bytearray([67, (crc // 256), (crc % 256)])