nRF5 SDK v16.0.0
SLIP library

The Serial Line Internet Protocol (SLIP) library encodes and decodes SLIP packets as defined in RFC1055. The module uses buffer memory provided by the user.

See SLIP encoding and decoding for the API documentation.

SLIP basics

SLIP is used to encode data streams into packets. This process requires four special bytes:

Name Octal HEX Description
END 300 0xC0 Defines the end of a packet.
ESC 333 0xDB Indicates that the following byte should not be interpreted as END or ESC byte.
ESC_END 334 0xDC Indicates that the data byte has the same value as the END byte, but is not to be interpreted as END byte.
ESC_ESC 335 0xDD Indicates that the data byte has the same value as the ESC byte, but is not to be interpreted as ESC byte.

The ESC byte is used to indicate that one of the data bytes has the same value as either the END or the ESC byte, but it should not be interpreted as this special byte. For example, to send a data byte with value END, ESC + ESC_END is sent instead. To send ESC, ESC + ESC_ESC is sent. These sequences are then interpreted as data bytes with the value 0xC0 and 0xDB, respectively. The bytes ESC_END and ESC_ESC do not need to be encoded.

Note that a sequence of n bytes might entirely consist of END and ESC. Therefore, the size of the output packet might be as long as 2*n+1 bytes.

Encoding a SLIP packet

To encode a buffer using SLIP, call the function slip_encode. Remember to allocate a sufficiently large output buffer. The following code segment shows how to encode a buffer into a SLIP packet:

uint8_t input_buffer[3] = {0x01, 0xC0, 0xDB};
uint8_t output_buffer[sizeof(input_buffer)*2];
uint32_t output_length;
slip_encode(output_buffer, input_buffer, sizeof(input_buffer), &output_length);

For the sample input, the resulting output buffer is {0x01, 0xDB, 0xDC, 0xDB, 0xDD, 0xC0}.

Decoding a SLIP packet

To decode a SLIP packet, call the function slip_decode_add_byte for each byte that is received. The following code segment shows how to receive and decode SLIP data from UART:

static uint8_t slip_buffer[128];
static slip_t slip = {
.p_buffer = slip_buffer,
.current_index = 0,
.buffer_len = sizeof(slip_buffer)}
static void uart_event_handler(nrf_drv_uart_event_t * p_event, void * p_context)
{
ret_code_t ret_code;
uint8_t c = p_event->data.rxtx.p_data[0];
switch (p_event->type)
{
ret_code = slip_decode_add_byte(&slip, c);
switch (ret_code)
{
on_packet_received(slip.p_buffer, slip.current_index);
// fall through
slip.current_index = 0;
break;
default:
// no implementation
break;
}
break;
case NRF_DRV_UART_EVT_TX_DONE: // fall through
break;
}
}

After the full packet was received, the decoded data is available in slip_t::p_buffer.


Documentation feedback | Developer Zone | Subscribe | Updated