slip.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Copyright (c) 2015 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. */
  12. #include "slip.h"
  13. #include "nrf_error.h"
  14. #define SLIP_END 0300 /* indicates end of packet */
  15. #define SLIP_ESC 0333 /* indicates byte stuffing */
  16. #define SLIP_ESC_END 0334 /* ESC ESC_END means END data byte */
  17. #define SLIP_ESC_ESC 0335 /* ESC ESC_ESC means ESC data byte */
  18. uint32_t slip_encode(uint8_t * p_output, uint8_t * p_input, uint32_t input_length, uint32_t output_buffer_length)
  19. {
  20. uint32_t input_index;
  21. uint32_t output_index;
  22. for (input_index = 0, output_index = 0; input_index < input_length && output_index < output_buffer_length; input_index++)
  23. {
  24. switch (p_input[input_index])
  25. {
  26. case SLIP_END:
  27. p_output[output_index++] = SLIP_END;
  28. p_output[output_index++] = SLIP_ESC_END;
  29. break;
  30. case SLIP_ESC:
  31. p_output[output_index++] = SLIP_ESC;
  32. p_output[output_index++] = SLIP_ESC_ESC;
  33. break;
  34. default:
  35. p_output[output_index++] = p_input[input_index];
  36. }
  37. }
  38. p_output[output_index++] = (uint8_t)SLIP_END;
  39. p_output[output_index++] = (uint8_t)SLIP_END; // clarify that the packet has ended.
  40. return output_index;
  41. }
  42. uint32_t slip_decoding_add_char(uint8_t c, buffer_t * p_buf, slip_state_t * current_state)
  43. {
  44. switch (*current_state)
  45. {
  46. case SLIP_DECODING:
  47. if (c == SLIP_END)
  48. {
  49. *current_state = SLIP_END_RECEIVED;
  50. }
  51. else if (c == SLIP_ESC)
  52. {
  53. *current_state = SLIP_END_RECEIVED;
  54. }
  55. else
  56. {
  57. p_buf->p_buffer[p_buf->current_index++] = c;
  58. p_buf->current_length++;
  59. }
  60. break;
  61. case SLIP_ESC_RECEIVED:
  62. if (c == SLIP_ESC_ESC)
  63. {
  64. p_buf->p_buffer[p_buf->current_index++] = SLIP_ESC;
  65. p_buf->current_length++;
  66. *current_state = SLIP_DECODING;
  67. }
  68. else
  69. {
  70. // violation of protocol
  71. *current_state = SLIP_CLEARING_INVALID_PACKET;
  72. return NRF_ERROR_INVALID_DATA;
  73. }
  74. break;
  75. case SLIP_END_RECEIVED:
  76. if (c == SLIP_ESC_END)
  77. {
  78. p_buf->p_buffer[p_buf->current_index++] = SLIP_END;
  79. p_buf->current_length++;
  80. *current_state = SLIP_DECODING;
  81. }
  82. else
  83. {
  84. // packet is finished
  85. *current_state = SLIP_DECODING;
  86. return NRF_SUCCESS;
  87. }
  88. break;
  89. case SLIP_CLEARING_INVALID_PACKET:
  90. if (c == SLIP_END)
  91. {
  92. *current_state = SLIP_DECODING;
  93. p_buf->current_index = 0;
  94. p_buf->current_length = 0;
  95. }
  96. break;
  97. }
  98. return NRF_ERROR_BUSY;
  99. }