예제 #1
0
    def test_class_with_known_answers(self):
        for crcfun_name, v in self.known_answers:
            for i, msg in enumerate(self.test_messages_for_known_answers):
                crc1 = PredefinedCrc(crcfun_name)
                crc1.update(msg)
                self.assertEqual(
                    crc1.crcValue, v[i],
                    "Wrong answer for crc1 %s, input '%s'" %
                    (crcfun_name, msg))

                crc2 = crc1.new()
                # Check that crc1 maintains its same value, after .new() call.
                self.assertEqual(
                    crc1.crcValue, v[i],
                    "Wrong state for crc1 %s, input '%s'" % (crcfun_name, msg))
                # Check that the new class instance created by .new() contains the initialisation value.
                # This depends on the first string in self.test_messages_for_known_answers being
                # the empty string.
                self.assertEqual(
                    crc2.crcValue, v[0],
                    "Wrong state for crc2 %s, input '%s'" % (crcfun_name, msg))

                crc2.update(msg)
                # Check that crc1 maintains its same value, after crc2 has called .update()
                self.assertEqual(
                    crc1.crcValue, v[i],
                    "Wrong state for crc1 %s, input '%s'" % (crcfun_name, msg))
                # Check that crc2 contains the right value after calling .update()
                self.assertEqual(
                    crc2.crcValue, v[i],
                    "Wrong state for crc2 %s, input '%s'" % (crcfun_name, msg))
예제 #2
0
 def test_class_predefined_table(self):
     for table_entry in _predefined_crc_definitions:
         # Check predefined class
         crc1 = PredefinedCrc(table_entry['name'])
         crc1.update("123456789")
         self.assertEqual(crc1.crcValue, table_entry['check'],
                          "Wrong answer for CRC '%s'" % table_entry['name'])
예제 #3
0
    def __init__(self, data):
        """
        Create an LDP packet

        Returns a new LDP packet
        Args:
          data (bytes) : packet contents

        Throws ValueError if packet is malformed
        """
        if len(data) < self.__header_len + self.__checksum_len:
            raise ValueError('Malformed LDP packet: too short')
        header = struct.unpack('>L', data[:self.__header_len])[0]
        self.type = header >> 24
        self.length = header & 0xffffff
        if self.length > len(data):
            raise ValueError('Malformed LDP packet: invalid length')

        crc = PredefinedCrc('crc-32-mpeg')
        crc.update(data[:self.length])
        if crc.crcValue != 0:
            raise ValueError('Malformed LDP packet: invalid checksum')

        self.payload = data[self.__header_len:self.length -
                            self.__checksum_len]
예제 #4
0
 def __init__(self, port):
     if port:
         self._com = Serial(port, baudrate=115200, timeout=1.0)
         if self._com is None:
             raise Exception("environment sensor is not found")
     else:
         raise Exception("no such environment sensor COM port")
     self._crc16func = mkPredefinedCrcFun('crc-16')
     self._crc16obj  = PredefinedCrc('crc-16')
예제 #5
0
파일: test.py 프로젝트: Alattack/catapult
    def test_class_with_known_answers(self):
        for crcfun_name, v in self.known_answers:
            for i, msg in enumerate(self.test_messages_for_known_answers):
                crc1 = PredefinedCrc(crcfun_name)
                crc1.update(msg)
                self.assertEqual(crc1.crcValue, v[i], "Wrong answer for crc1 %s, input '%s'" % (crcfun_name,msg))

                crc2 = crc1.new()
                # Check that crc1 maintains its same value, after .new() call.
                self.assertEqual(crc1.crcValue, v[i], "Wrong state for crc1 %s, input '%s'" % (crcfun_name,msg))
                # Check that the new class instance created by .new() contains the initialisation value.
                # This depends on the first string in self.test_messages_for_known_answers being
                # the empty string.
                self.assertEqual(crc2.crcValue, v[0], "Wrong state for crc2 %s, input '%s'" % (crcfun_name,msg))

                crc2.update(msg)
                # Check that crc1 maintains its same value, after crc2 has called .update()
                self.assertEqual(crc1.crcValue, v[i], "Wrong state for crc1 %s, input '%s'" % (crcfun_name,msg))
                # Check that crc2 contains the right value after calling .update()
                self.assertEqual(crc2.crcValue, v[i], "Wrong state for crc2 %s, input '%s'" % (crcfun_name,msg))
예제 #6
0
파일: main.py 프로젝트: flarn2006/BHSTools
	def __bytes__(self):
		data = self.gen_data()
		crc = PredefinedCrc('crc-16-mcrf4xx')
		crc.update(data)
		digest = crc.digest()
		return data + bytes([digest[1], digest[0]])
예제 #7
0
class EnvSensor:
    def __init__(self, port):
        if port:
            self._com = Serial(port, baudrate=115200, timeout=1.0)
            if self._com is None:
                raise Exception("environment sensor is not found")
        else:
            raise Exception("no such environment sensor COM port")
        self._crc16func = mkPredefinedCrcFun('crc-16')
        self._crc16obj  = PredefinedCrc('crc-16')

    def read_datas(self):
        datas = None
        self._send_command_for_read(0x5012)
        resp = self._recv_response()
        if resp:
            datas = EnvSensor._parse_latest_sensing_data(resp)
        self._send_command_for_read(0x5013)
        resp = self._recv_response()
        if resp:
            calc_datas = EnvSensor._parse_latest_calculation_data(resp)
            if datas:
                datas.update(calc_datas)
            else:
                datas = calc_datas

        return datas

    def _send_command_for_read(self, addr):
        command = bytearray([0x52, 0x42, 0x05, 0x00, 0x01, (addr & 0xFF), (addr >> 8)])
        crc = self._crc16func(command, crc=0xFFFF)
        command.append(crc & 0xFF)
        command.append(crc >> 8)
        self._com.write(command)

    def _recv_response(self):
        resp_header = self._com.read(5)
        if (resp_header[0] != 0x52) or (resp_header[1] != 0x42):
            return None
        resp_len = resp_header[2] + (resp_header[3] << 8)
        if (resp_header[4] & 0x80):
            ignore_bytes = self._com.read(resp_len - 1)
            return None  # error response
        payload_len = resp_len - 2
        resp_body = self._com.read(payload_len - 1)
        self._crc16obj.crcValue = 0xFFFF
        self._crc16obj.update(resp_header)
        self._crc16obj.update(resp_body)
        resp_crc = (self._com.read())[0] + ((self._com.read())[0] << 8)
        if (resp_crc != self._crc16obj.crcValue):
            print("unexpected CRC value on response: ", resp_crc)
            return None

        return resp_body

    @staticmethod
    def _parse_latest_sensing_data(resp):
        datas = {}

        # skip addr and sequence number
        pos = 2   # skip addr
        pos += 1  # sequence number

        # read rest fields
        datas["temperature"]  = read16bit_value(resp, pos, 0.01)
        pos += 2
        datas["humidity"]     = read16bit_value(resp, pos, 0.01)
        pos += 2
        datas["illuminance"]  = read16bit_value(resp, pos, 1)
        pos += 2
        datas["air_pressure"] = read32bit_value(resp, pos, 0.001)
        pos += 4
        datas["sound_noise"]  = read16bit_value(resp, pos, 0.01)
        pos += 2
        datas["e_tvoc"] = read16bit_value(resp, pos, 1)
        pos += 2
        datas["e_co2"]  = read16bit_value(resp, pos, 1)

        return datas

    @staticmethod
    def _parse_latest_calculation_data(resp):
        datas = {}

        # skip addr and sequence number
        pos = 2
        pos += 1

        # read rest fields
        datas["discomfort_index"]      = read16bit_value(resp, pos, 0.01)
        pos += 2
        datas["heat_stroke_alarmness"] = read16bit_value(resp, pos, 0.01)
        pos += 2
        datas["vibr_info"] = resp[pos]

        return datas
예제 #8
0
파일: test.py 프로젝트: Alattack/catapult
 def test_class_predefined_table(self):
     for table_entry in _predefined_crc_definitions:
         # Check predefined class
         crc1 = PredefinedCrc(table_entry['name'])
         crc1.update("123456789")
         self.assertEqual(crc1.crcValue, table_entry['check'], "Wrong answer for CRC '%s'" % table_entry['name'])
예제 #9
0
 def crc(self):
     crc = PredefinedCrc('crc-16-buypass')
     crc.update(self.value[:-2])
     return crc.crcValue