123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- #include "sdk_common.h"
- #if NRF_MODULE_ENABLED(SLIP)
- #include "slip.h"
- #include <string.h>
- #define SLIP_BYTE_END 0300
- #define SLIP_BYTE_ESC 0333
- #define SLIP_BYTE_ESC_END 0334
- #define SLIP_BYTE_ESC_ESC 0335
- ret_code_t slip_encode(uint8_t * p_output, uint8_t * p_input, uint32_t input_length, uint32_t * p_output_buffer_length)
- {
- if (p_output == NULL || p_input == NULL || p_output_buffer_length == NULL)
- {
- return NRF_ERROR_NULL;
- }
- *p_output_buffer_length = 0;
- uint32_t input_index;
- for (input_index = 0; input_index < input_length; input_index++)
- {
- switch (p_input[input_index])
- {
- case SLIP_BYTE_END:
- p_output[(*p_output_buffer_length)++] = SLIP_BYTE_ESC;
- p_output[(*p_output_buffer_length)++] = SLIP_BYTE_ESC_END;
- break;
- case SLIP_BYTE_ESC:
- p_output[(*p_output_buffer_length)++] = SLIP_BYTE_ESC;
- p_output[(*p_output_buffer_length)++] = SLIP_BYTE_ESC_ESC;
- break;
- default:
- p_output[(*p_output_buffer_length)++] = p_input[input_index];
- }
- }
- p_output[(*p_output_buffer_length)++] = SLIP_BYTE_END;
- return NRF_SUCCESS;
- }
- ret_code_t slip_decode_add_byte(slip_t * p_slip, uint8_t c)
- {
- if (p_slip == NULL)
- {
- return NRF_ERROR_NULL;
- }
- if (p_slip->current_index == p_slip->buffer_len)
- {
- return NRF_ERROR_NO_MEM;
- }
- switch (p_slip->state)
- {
- case SLIP_STATE_DECODING:
- switch (c)
- {
- case SLIP_BYTE_END:
-
- return NRF_SUCCESS;
- case SLIP_BYTE_ESC:
-
- p_slip->state = SLIP_STATE_ESC_RECEIVED;
- break;
- default:
-
- p_slip->p_buffer[p_slip->current_index++] = c;
- break;
- }
- break;
- case SLIP_STATE_ESC_RECEIVED:
- switch (c)
- {
- case SLIP_BYTE_ESC_END:
- p_slip->p_buffer[p_slip->current_index++] = SLIP_BYTE_END;
- p_slip->state = SLIP_STATE_DECODING;
- break;
- case SLIP_BYTE_ESC_ESC:
- p_slip->p_buffer[p_slip->current_index++] = SLIP_BYTE_ESC;
- p_slip->state = SLIP_STATE_DECODING;
- break;
- default:
-
- p_slip->state = SLIP_STATE_CLEARING_INVALID_PACKET;
- return NRF_ERROR_INVALID_DATA;
- }
- break;
- case SLIP_STATE_CLEARING_INVALID_PACKET:
- if (c == SLIP_BYTE_END)
- {
- p_slip->state = SLIP_STATE_DECODING;
- p_slip->current_index = 0;
- }
- break;
- }
- return NRF_ERROR_BUSY;
- }
- #endif
|