def test_register_data_request(self): from popcop.transport import ReceivedFrame from popcop.standard.register import DataRequestMessage, ValueType from popcop import STANDARD_FRAME_TYPE_CODE self.assertEqual(DataRequestMessage()._encode(), bytes([0, 0])) self.assertEqual(DataRequestMessage(name='Hello')._encode(), b'\x05Hello\x00') self.assertEqual(DataRequestMessage(value=123, type_id=ValueType.I8). _encode(), bytes([0, 7, 123])) msg = popcop.standard.decode(ReceivedFrame(STANDARD_FRAME_TYPE_CODE, bytes([1, 0, 0, 0]), 0)) self.assertIsInstance(msg, DataRequestMessage) self.assertEqual(msg.name, '') self.assertEqual(msg.type_id, ValueType.EMPTY) self.assertIsNone(msg.value) msg = popcop.standard.decode(ReceivedFrame(STANDARD_FRAME_TYPE_CODE, bytes([1, 0, 3, 48, 49, 50, 1, 52, 53, 54]), 0)) self.assertIsInstance(msg, DataRequestMessage) self.assertEqual(msg.name, '012') self.assertEqual(msg.type_id, ValueType.STRING) self.assertEqual(msg.value, '456') print(msg)
def test_register_discovery_response(self): from popcop.transport import ReceivedFrame from popcop.standard import encode, decode from popcop.standard.register import DiscoveryResponseMessage from popcop import STANDARD_FRAME_TYPE_CODE # Encoding a full frame then stripping the delimiters, type code, and CRC. self.assertEqual( encode(DiscoveryResponseMessage())[1:-6], bytes([4, 0, 0, 0, 0])) self.assertEqual( encode(DiscoveryResponseMessage(12345, 'Hello'))[1:-6], b'\x04\x00\x39\x30\x05Hello') msg = decode( ReceivedFrame(STANDARD_FRAME_TYPE_CODE, bytes([4, 0, 0, 0, 0]), 0)) self.assertIsInstance(msg, DiscoveryResponseMessage) self.assertEqual(msg.index, 0) self.assertEqual(msg.name, '') msg = decode( ReceivedFrame(STANDARD_FRAME_TYPE_CODE, bytes([4, 0, 0x39, 0x30, 5]) + b'Hello', 0)) self.assertIsInstance(msg, DiscoveryResponseMessage) self.assertEqual(msg.index, 12345) self.assertEqual(msg.name, 'Hello') print(msg)
def test_bootloader_status_response(self): from popcop.transport import ReceivedFrame from popcop.standard import encode, decode from popcop.standard.bootloader import State, StatusResponseMessage from popcop import STANDARD_FRAME_TYPE_CODE # Encoding a full frame then stripping the delimiters, type code, and CRC. self.assertEqual( encode( StatusResponseMessage(Decimal('0.000123456'), 0xBADC0FFEE, State.BOOT_CANCELLED))[1:-6], bytes([ 11, 0, 0x40, 0xe2, 1, 0, 0, 0, 0, 0, 0xEE, 0xFF, 0xC0, 0xAD, 0x0B, 0, 0, 0, 2 ])) msg = decode( ReceivedFrame( STANDARD_FRAME_TYPE_CODE, bytes([ 11, 0, 0x40, 0xe2, 1, 0, 0, 0, 0, 0, 0xEE, 0xFF, 0xC0, 0xAD, 0x0B, 0, 0, 0, 2 ]), 0)) self.assertIsInstance(msg, StatusResponseMessage) self.assertEqual(msg.uptime, Decimal('0.000123456')) self.assertEqual(msg.flags, 0xBADC0FFEE) self.assertEqual(msg.state, State.BOOT_CANCELLED)
def test_register_discovery_request(self): from popcop.transport import ReceivedFrame from popcop.standard import encode, decode from popcop.standard.register import DiscoveryRequestMessage from popcop import STANDARD_FRAME_TYPE_CODE # Encoding a full frame then stripping the delimiters, type code, and CRC. self.assertEqual(encode(DiscoveryRequestMessage())[1:-6], bytes([3, 0, 0, 0])) self.assertEqual(encode(DiscoveryRequestMessage(12345))[1:-6], bytes([3, 0, 0x39, 0x30])) msg = decode(ReceivedFrame(STANDARD_FRAME_TYPE_CODE, bytes([3, 0, 0, 0]), 0)) self.assertIsInstance(msg, DiscoveryRequestMessage) self.assertEqual(msg.index, 0) msg = decode(ReceivedFrame(STANDARD_FRAME_TYPE_CODE, bytes([3, 0, 0x39, 0x30]), 0)) self.assertIsInstance(msg, DiscoveryRequestMessage) self.assertEqual(msg.index, 12345) print(msg)
def test_bootloader_status_request(self): from popcop.transport import ReceivedFrame from popcop.standard import encode, decode from popcop.standard.bootloader import State, StatusRequestMessage from popcop import STANDARD_FRAME_TYPE_CODE # Encoding a full frame then stripping the delimiters, type code, and CRC. self.assertEqual(encode(StatusRequestMessage(State.APP_UPGRADE_IN_PROGRESS))[1:-6], bytes([10, 0, 3])) msg = decode(ReceivedFrame(STANDARD_FRAME_TYPE_CODE, bytes([10, 0, 4]), 0)) self.assertIsInstance(msg, StatusRequestMessage) self.assertEqual(msg.desired_state, State.READY_TO_BOOT)
def test_device_management_command_request(self): from popcop.transport import ReceivedFrame from popcop.standard import encode, decode from popcop.standard.device_management import CommandRequestMessage, Command from popcop import STANDARD_FRAME_TYPE_CODE # Encoding a full frame then stripping the delimiters, type code, and CRC. self.assertEqual(encode(CommandRequestMessage(Command.LAUNCH_BOOTLOADER))[1:-6], bytes([8, 0, 2, 0])) self.assertEqual(encode(CommandRequestMessage(Command.PRINT_DIAGNOSTICS_VERBOSE))[1:-6], bytes([8, 0, 5, 0])) msg = decode(ReceivedFrame(STANDARD_FRAME_TYPE_CODE, bytes([8, 0, 1, 0]), 0)) self.assertIsInstance(msg, CommandRequestMessage) self.assertEqual(msg.command, Command.POWER_OFF)
def test_bootloader_image_data_response(self): from popcop.transport import ReceivedFrame from popcop.standard import encode, decode from popcop.standard.bootloader import ImageType, ImageDataResponseMessage from popcop import STANDARD_FRAME_TYPE_CODE # Encoding a full frame then stripping the delimiters, type code, and CRC. self.assertEqual( encode( ImageDataResponseMessage(123456, ImageType.CERTIFICATE_OF_AUTHENTICITY, b'Hello world!'))[1:-6], bytes([ 13, 0, 0x40, 0xe2, 1, 0, 0, 0, 0, 0, # 123456 1 ]) + b'Hello world!') msg = decode( ReceivedFrame( STANDARD_FRAME_TYPE_CODE, bytes([ 13, 0, 0x40, 0xe2, 1, 0, 0, 0, 0, 0, # 123456 1 ]) + b'Hello world!', 0)) self.assertIsInstance(msg, ImageDataResponseMessage) self.assertEqual(msg.image_offset, 123456) self.assertEqual(msg.image_type, ImageType.CERTIFICATE_OF_AUTHENTICITY) self.assertEqual(msg.image_data, b'Hello world!')
def test_device_management_command_response(self): from popcop.transport import ReceivedFrame from popcop.standard import encode, decode from popcop.standard.device_management import CommandResponseMessage, Command, CommandExecutionStatus from popcop import STANDARD_FRAME_TYPE_CODE # Encoding a full frame then stripping the delimiters, type code, and CRC. self.assertEqual(encode(CommandResponseMessage(Command.LAUNCH_BOOTLOADER, CommandExecutionStatus.MAYBE_LATER))[1:-6], bytes([9, 0, 2, 0, 2])) self.assertEqual(encode(CommandResponseMessage(Command.PRINT_DIAGNOSTICS_VERBOSE, CommandExecutionStatus.OK))[1:-6], bytes([9, 0, 5, 0, 0])) msg = decode(ReceivedFrame(STANDARD_FRAME_TYPE_CODE, bytes([9, 0, 1, 0, 1]), 0)) self.assertIsInstance(msg, CommandResponseMessage) self.assertEqual(msg.command, Command.POWER_OFF) self.assertEqual(msg.status, CommandExecutionStatus.BAD_COMMAND)
def test_register_data_response(self): from popcop.transport import ReceivedFrame from popcop.standard.register import DataResponseMessage, ValueType from popcop import STANDARD_FRAME_TYPE_CODE self.assertEqual(DataResponseMessage()._encode(), bytes([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])) self.assertEqual(DataResponseMessage(name='Hello', flags=1)._encode(), b'\0\0\0\0\0\0\0\0\1\x05Hello\x00') # >>> '%016x' % (int(Decimal('2.123') * _NANOSECONDS_PER_SECOND)) # '000000007e8a68c0' # Don't forget to flip byte order because little-endian self.assertEqual(DataResponseMessage(timestamp=Decimal('2.123'), flags=2, name='Hello', value=123, type_id=ValueType.I8). _encode(), b'\xc0\x68\x8a\x7e\x00\x00\x00\x00' # Timestamp 2.123 seconds - see computation above b'\x02\x05Hello\x07\x7b') msg = popcop.standard.decode(ReceivedFrame(STANDARD_FRAME_TYPE_CODE, bytes([2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), 0)) self.assertIsInstance(msg, DataResponseMessage) self.assertEqual(msg.timestamp, Decimal(0)) self.assertEqual(msg.flags.mutable, False) self.assertEqual(msg.flags.persistent, False) self.assertEqual(msg.name, '') self.assertEqual(msg.type_id, ValueType.EMPTY) self.assertIsNone(msg.value) msg = popcop.standard.decode(ReceivedFrame(STANDARD_FRAME_TYPE_CODE, bytes([2, 0, 0xc0, 0x68, 0x8a, 0x7e, 0x0, 0x0, 0x0, 0x0, # TS as above 3, 3, 48, 49, 50, 1, 52, 53, 54]), 0)) self.assertIsInstance(msg, DataResponseMessage) self.assertEqual(msg.timestamp, Decimal('2.123')) self.assertEqual(msg.flags.mutable, True) self.assertEqual(msg.flags.persistent, True) self.assertEqual(msg.name, '012') self.assertEqual(msg.type_id, ValueType.STRING) self.assertEqual(msg.value, '456') print(msg) msg = popcop.standard.decode(ReceivedFrame(STANDARD_FRAME_TYPE_CODE, bytes([2, 0, 0xc0, 0x68, 0x8a, 0x7e, 0x0, 0x0, 0x0, 0x0, # TS as above 0, 16, 99, 116, 108, 46, 110, 117, 109, 95, 97, 116, 116, 101, 109, 112, 116, 115, 9, 100, 0, 0, 0]), 0)) self.assertIsInstance(msg, DataResponseMessage) self.assertEqual(msg.timestamp, Decimal('2.123')) self.assertEqual(msg.flags.mutable, False) self.assertEqual(msg.flags.persistent, False) self.assertEqual(msg.name, 'ctl.num_attempts') self.assertEqual(msg.type_id, ValueType.U32) self.assertEqual(msg.value, [100]) print(msg)