def test_019_test_device_id_byte_conversion_03(self): """ Test converting a device_id value to its byte array value, then back to the device_id value """ device_id = 0x01c4 did_bytes = device_id_to_bytes(device_id) self.assertEqual(did_bytes, [0x01, 0xc4]) new_device_id = device_id_join_bytes(did_bytes) self.assertEqual(new_device_id, device_id)
def test_014_device_id_join_bytes(self): """ Test converting a list of device_id bytes into its original value. """ device_id_bytes = [0x00, 0x00] device_id = utils.device_id_join_bytes(device_id_bytes) self.assertEquals(device_id, 0x0000) device_id_bytes = [0xff, 0xff] device_id = utils.device_id_join_bytes(device_id_bytes) self.assertEquals(device_id, 0xffff) device_id_bytes = [0x43, 0x21] device_id = utils.device_id_join_bytes(device_id_bytes) self.assertEquals(device_id, 0x4321) device_id_bytes = [0x00, 0x01] device_id = utils.device_id_join_bytes(device_id_bytes) self.assertEquals(device_id, 0x1) device_id_bytes = [0xa7, 0x2b] device_id = utils.device_id_join_bytes(device_id_bytes) self.assertEquals(device_id, 0xa72b) device_id_bytes = [0xef, 0x00] device_id = utils.device_id_join_bytes(device_id_bytes) self.assertEquals(device_id, 0xef00)
def deserialize(self, packet_bytes): """ Populate the fields of a DeviceBusPacket instance. :param packet_bytes: - They bytes to use to populate the packet instance. Raises: BusDataException: if the packet is smaller than the minimum packet size, if the packet has an invalid header byte, if the packet has an invalid trailer byte, or if the length byte in the packet does not match the actual length of the packet. ChecksumException: if checksum validation fails on the packet being deserialized. """ # check length to make sure we have at minimum the min packet length if len(packet_bytes) < PKT_MIN_LENGTH: raise BusDataException('Invalid packet byte stream length of ' + str(len(packet_bytes))) # check header byte - if invalid, toss if packet_bytes[0] != PKT_VALID_HEADER: raise BusDataException('No header byte found in incoming packet.') # check length - if packet_bytes len doesn't match, toss if packet_bytes[1] != len(packet_bytes) - PKT_META_BYTES: raise BusDataException('Invalid length from incoming packet ({}).'.format(packet_bytes[1])) # get sequence num self.sequence = packet_bytes[2] # get device type self.device_type = packet_bytes[3] # get board id self.board_id = board_id_join_bytes(packet_bytes[4:8]) # get device id self.device_id = device_id_join_bytes(packet_bytes[8:10]) # get data (up to 32 bytes) - todo multi-packet transmissions self.data = [x for x in packet_bytes[10:len(packet_bytes) - 2]] # get the checksum and verify it - toss if no good check = self.generate_checksum(self.sequence, self.device_type, self.board_id, self.device_id, self.data) if check != packet_bytes[len(packet_bytes) - 2]: raise ChecksumException('Invalid checksum in incoming packet.') # get the trailer byte - toss if no good if packet_bytes[len(packet_bytes) - 1] != PKT_VALID_TRAILER: raise BusDataException('Invalid trailer byte found in incoming packet.')
def test_015_device_id_join_bytes(self): """ Test converting a list of device_id bytes into its original value. """ device_id_bytes = [] with self.assertRaises(ValueError): utils.device_id_join_bytes(device_id_bytes) device_id_bytes = [0x12] with self.assertRaises(ValueError): utils.device_id_join_bytes(device_id_bytes) device_id_bytes = [0x12, 0x34, 0x56] with self.assertRaises(ValueError): utils.device_id_join_bytes(device_id_bytes) device_id_bytes = 0x1234 with self.assertRaises(ValueError): utils.device_id_join_bytes(device_id_bytes)