def test_memloc_from_bytes(self): memloc = MemoryLocation.from_bytes(address_bytes=b'\x12\x34', memorysize_bytes=b'\xFF') self.assertEqual(memloc.address, 0x1234) self.assertEqual(memloc.memorysize, 0xFF) self.assertEqual(memloc.address_format, 16) self.assertEqual(memloc.memorysize_format, 8) memloc = MemoryLocation.from_bytes(address_bytes=b'\x12\x34\x56', memorysize_bytes=b'\x66\x77\x88') self.assertEqual(memloc.address, 0x123456) self.assertEqual(memloc.memorysize, 0x667788) self.assertEqual(memloc.address_format, 24) self.assertEqual(memloc.memorysize_format, 24)
def test_memloc_max_size(self): MemoryLocation.from_bytes(address_bytes='\x12\x34\x56\x78\x9a', memorysize_bytes='\xFF') with self.assertRaises(ValueError): MemoryLocation.from_bytes(address_bytes='\x12\x34\x56\x78\x9a\xbc', memorysize_bytes='\xFF') MemoryLocation.from_bytes(address_bytes='\x12\x34', memorysize_bytes='\x12\x34\x56\x78') with self.assertRaises(ValueError): MemoryLocation.from_bytes(address_bytes='\x12\x34', memorysize_bytes='\x12\x34\x56\x78\x9a')
def interpret_response(cls, response, memory_location): """ Populates the response ``service_data`` property with an instance of :class:`WriteMemoryByAddress.ResponseData<udsoncan.services.WriteMemoryByAddress.ResponseData>` :param response: The received response to interpret :type response: :ref:`Response<Response>` :param memory_location: The memory location used for the request. The bytes position varies depending on the ``memory_location`` format :type memory_location: :ref:`MemoryLocation <MemoryLocation>` :raises InvalidResponseException: If length of ``response.data`` is too short """ from udsoncan import MemoryLocation if not isinstance(memory_location, MemoryLocation): raise ValueError( 'Given memory location must be an instance of MemoryLocation') address_bytes = memory_location.get_address_bytes() memorysize_bytes = memory_location.get_memorysize_bytes() expected_response_size = 1 + len(address_bytes) + len(memorysize_bytes) if len(response.data) < expected_response_size: raise InvalidResponseException( response, 'Repsonse should be at least %d bytes' % (expected_response_size)) response.service_data = cls.ResponseData() response.service_data.alfid_echo = response.data[0] offset = 1 length = len(memory_location.get_address_bytes()) address_echo = response.data[1:1 + length] offset += length length = len(memory_location.get_memorysize_bytes()) memorysize_echo = response.data[offset:offset + length] response.service_data.memory_location_echo = MemoryLocation.from_bytes( address_bytes=address_echo, memorysize_bytes=memorysize_echo)