Exemple #1
0
    def test_decode(self):
        self.assertRaises(MessageError, Message.decode, b'\xA5\x03\x42\x00\x00\x00\xE5')
        self.assertRaises(MessageError, Message.decode,
                          b'\xA4\x14\x42' + (b'\x00' * 20) + b'\xE5')
        self.assertRaises(MessageError, Message.decode, b'\xA4\x03\x42\x01\x02\xF3\xE5')
        msg = Message.decode(b'\xA4\x03\x42\x00\x00\x00\xE5')
        self.assertEqual(len(msg), 7)
        self.assertEqual(msg.type, MESSAGE_CHANNEL_ASSIGN)
        self.assertEqual(msg.payload, b'\x00' * 3)
        self.assertEqual(msg.checksum, 0xE5)

        msg = Message.decode(b'\xA4\x03\x42\x00\x00\x00\xE5')
        self.assertTrue(isinstance(msg, MSG.ChannelAssignMessage))
        self.assertRaises(MessageError, Message.decode, b'\xA4\x03\xFF\x00\x00\x00\xE5')
        self.assertRaises(MessageError, Message.decode, b'\xA4\x03\x42')
        self.assertRaises(MessageError, Message.decode, b'\xA4\x05\x42\x00\x00\x00\x00')
Exemple #2
0
def EventPump(evm):
    buffer_ = b''
    while True:
        with evm.runningLock:
            if not evm.running:
                break

        try:
            buffer_ += evm.driver.read(20)
        except USBError as e:
            if e.errno in (60, 110):  # timeout
                continue
            else:
                raise

        messages = []
        while buffer_:
            try:
                msg = Message.decode(buffer_)
                messages.append(msg)
                buffer_ = buffer_[len(msg):]
            except MessageError as err:
                if err.internal is not Message.INCOMPLETE:
                    i, length = 1, len(buffer_)
                    # move to the next SYNC byte
                    while i < length and buffer_[i] != MESSAGE_TX_SYNC:
                        i += 1
                    buffer_ = buffer_[i:]
                else:
                    break

        with evm.evmCallbackLock:
            for message in messages:
                for callback in evm.callbacks:
                    try:
                        callback.process(message)
                    except Exception as err:  # pylint: disable=broad-except
                        print(err)