dotbot.hdlc module#

Module implementing HDLC protocol primitives.

exception dotbot.hdlc.HDLCDecodeException[source]#

Bases: Exception

Exception raised when decoding wrong HDLC frames.

class dotbot.hdlc.HDLCHandler[source]#

Bases: object

Handles the reception of an HDLC frame byte by byte.

handle_byte(byte)[source]#

Handle new byte received.

property payload#

Returns the payload contained in a frame.

class dotbot.hdlc.HDLCState(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]#

Bases: Enum

State of the HDLC handler.

IDLE = 0#
READY = 2#
RECEIVING = 1#
dotbot.hdlc.hdlc_decode(frame)[source]#

Decodes an HDLC frame and return the payload it contains.

>>> hdlc_decode(b"~test\x88\x07~")
bytearray(b'test')
>>> hdlc_decode(b"~\x00\x00\xf6\xf6\xf6\xf6\xb2+~")
bytearray(b'\x00\x00\xf6\xf6\xf6\xf6')
>>> hdlc_decode(b"~\x00\x01\n\n\n\x9c\xf2~")
bytearray(b'\x00\x01\n\n\n')
>>> hdlc_decode(b"~}^test}^\x9d\xa6~")
bytearray(b'~test~')
>>> hdlc_decode(b"~}^test}]\x06\x94~")
bytearray(b'~test}')
>>> hdlc_decode(b"~\xe7\x94:\xa6\x83}^~")
bytearray(b'\xe7\x94:\xa6')
>>> hdlc_decode(b"~\'$W\x82\x13}]~")
bytearray(b"\'$W\x82")
>>> hdlc_decode(b"~\x00\x00~")
bytearray(b'')
>>> hdlc_decode(b"~test\x42\x42~")
Traceback (most recent call last):
dotbot.hdlc.HDLCDecodeException: Invalid FCS
>>> hdlc_decode(b"~\x00~")
Traceback (most recent call last):
dotbot.hdlc.HDLCDecodeException: Invalid payload
Parameters:

frame (bytes) –

Return type:

bytes

dotbot.hdlc.hdlc_encode(payload)[source]#

Encodes a payload in an HDLC frame. >>> hdlc_encode(b”test”) bytearray(b’~testx88x07~’) >>> hdlc_encode(b””) bytearray(b’~x00x00~’) >>> hdlc_encode(b”x00x00xf6xf6xf6xf6”) bytearray(b’~x00x00xf6xf6xf6xf6xb2+~’) >>> hdlc_encode(b”x00x01nnn”) bytearray(b’~x00x01nnnx9cxf2~’) >>> hdlc_encode(b”~test~”) bytearray(b’~}^test}^x9dxa6~’) >>> hdlc_encode(b”~test}”) bytearray(b’~}^test}]x06x94~’) >>> hdlc_encode(b”xe7x94:xa6”) bytearray(b’~xe7x94:xa6x83}^~’) >>> hdlc_encode(b”’$Wx82”) bytearray(b”~'$Wx82x13}]~”)

Parameters:

payload (bytes) –

Return type:

bytes