def test_end_marker(self): """ Check that the packets are delimited by the end marker. """ data = bytes((0x42, 0x34)) + slip.END data = io.BytesIO(data) self.assertEqual(next(slip.decode(data)), bytes((0x42, 0x34)))
def rx_thread(conn, tun_fd): """ Thread reading from the serial connection and forwarding it on the TUN device. """ decoder = decode(conn) for packet in decoder: os.write(tun_fd, packet)
def test_unescape_unknown(self): """" Checks that we raise a ValueError in case of an unknown escape sequence. """ data = slip.ESC + bytes((0x42, )) + slip.END data = io.BytesIO(data) with self.assertRaises(ValueError): next(slip.decode(data))
def test_multiple_packet(self): """ Checks that we can have several packets that will be parsed at once. """ data = bytes((0x42, 0x34)) + slip.END + bytes((0xde, 0xad)) + slip.END data = io.BytesIO(data) decoder = slip.decode(data) self.assertEqual(next(decoder), bytes((0x42, 0x34))) self.assertEqual(next(decoder), bytes((0xde, 0xad)))
def test_escape_escape(self): """ Checks that ESC is correctly un-escaped """ data = slip.ESC + slip.ESC_ESC + slip.END data = io.BytesIO(data) expected = slip.ESC decoder = slip.decode(data) self.assertEqual(next(decoder), expected)
def test_escape_end(self): """ Checks that we can the END is correctly un-escaped. """ data = slip.ESC + slip.ESC_END + slip.END data = io.BytesIO(data) expected = slip.END decoder = slip.decode(data) self.assertEqual(next(decoder), expected)
def recv_slip_packet(self): while self.alive and self.serial.is_open: try: # read all that is there or wait for one byte (blocking) data = self.serial.read(self.serial.in_waiting or 1) except serial.SerialException as e: # probably some I/O problem such as disconnected USB serial self.logger.error(f"serial error: {e}") return b"" else: if data: self.buffer.extend(data) while slip.SLIP_END_CHAR in self.buffer: packet, self.buffer = self.buffer.split( slip.SLIP_END_CHAR, 1) try: return slip.decode(packet) except slip.SLIPEncodingError as error: self.logger.debug(error) #hexdump.hexdump(packet) #self.logger.exception(error) return b""
def recv_slip_packet(self): while self.alive and self.serial.is_open: try: # read all that is there or wait for one byte (blocking) data = self.serial.read(self.serial.in_waiting or 1) except serial.SerialException as e: raise IOError(f"serial error: {e}") else: if data: self.buffer.extend(data) #print(len(self.buffer)) if len(self.buffer)>2000: self.warn_overload() while slip.SLIP_END_CHAR in self.buffer: packet, self.buffer = self.buffer.split(slip.SLIP_END_CHAR, 1) try: return slip.decode(packet) except slip.SLIPEncodingError as error: self.logger.debug(error); #hexdump.hexdump(packet) #self.logger.exception(error) return b""