SLIP Encoding and Decoding

As the serial hardware is very simple and does not impose any kind of "packet structure" on data, it is the responsibility of the transmitting program to let the receiver know where a chunk of data begins and where it ends. The simplest way would be to place two "marker" bytes at the begining and end. For this purpose SLIP protocol is used. This encoding scheme is explained in RFC 1055.

Figure 2. SLIP Encapsulation

The SLIP protocol defines two special characters: END and ESC. END is octal 300 (decimal 192) and ESC is octal 333 (decimal 219) not to be confused with the ASCII ESCape character; for the purposes of this discussion, ESC will indicate the SLIP ESC character. To send a packet, a SLIP host simply starts sending the data in the packet. If a data byte is the same code as END character, a two byte sequence of ESC and ESC_END octal 334 (decimal 220) is sent instead. If it the same as an ESC character, an two byte sequence of ESC and ESC_ESC octal 335 (decimal 221) is sent instead. When the last byte in the packet has been sent, an END character is then transmitted.

The function send_packet() is used to encode the data using SLIP protocol. The function prototype is void send_packet(unsigned char *p, int len); This function reads character by character from the buffer p and perform the encoding and transmit the data packet over the serial line.

The function recv_packet() is used for SLIP decoding. The function prototype is given below. void recv_packet(void); This function is coded in a finite state machine model. The decoded characters are stored into slip_buffer[] array.