byte_stuffer.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2016 Fred Sundvik
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. #include "serial_link/protocol/byte_stuffer.h"
  21. #include "serial_link/protocol/frame_validator.h"
  22. #include "serial_link/protocol/physical.h"
  23. #include <stdbool.h>
  24. // This implements the "Consistent overhead byte stuffing protocol"
  25. // https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing
  26. // http://www.stuartcheshire.org/papers/COBSforToN.pdf
  27. typedef struct byte_stuffer_state {
  28. uint16_t next_zero;
  29. uint16_t data_pos;
  30. bool long_frame;
  31. uint8_t data[MAX_FRAME_SIZE];
  32. } byte_stuffer_state_t;
  33. static byte_stuffer_state_t states[NUM_LINKS];
  34. void init_byte_stuffer_state(byte_stuffer_state_t* state) {
  35. state->next_zero = 0;
  36. state->data_pos = 0;
  37. state->long_frame = false;
  38. }
  39. void init_byte_stuffer(void) {
  40. int i;
  41. for (i = 0; i < NUM_LINKS; i++) {
  42. init_byte_stuffer_state(&states[i]);
  43. }
  44. }
  45. void byte_stuffer_recv_byte(uint8_t link, uint8_t data) {
  46. byte_stuffer_state_t* state = &states[link];
  47. // Start of a new frame
  48. if (state->next_zero == 0) {
  49. state->next_zero = data;
  50. state->long_frame = data == 0xFF;
  51. state->data_pos = 0;
  52. return;
  53. }
  54. state->next_zero--;
  55. if (data == 0) {
  56. if (state->next_zero == 0) {
  57. // The frame is completed
  58. if (state->data_pos > 0) {
  59. validator_recv_frame(link, state->data, state->data_pos);
  60. }
  61. } else {
  62. // The frame is invalid, so reset
  63. init_byte_stuffer_state(state);
  64. }
  65. } else {
  66. if (state->data_pos == MAX_FRAME_SIZE) {
  67. // We exceeded our maximum frame size
  68. // therefore there's nothing else to do than reset to a new frame
  69. state->next_zero = data;
  70. state->long_frame = data == 0xFF;
  71. state->data_pos = 0;
  72. } else if (state->next_zero == 0) {
  73. if (state->long_frame) {
  74. // This is part of a long frame, so continue
  75. state->next_zero = data;
  76. state->long_frame = data == 0xFF;
  77. } else {
  78. // Special case for zeroes
  79. state->next_zero = data;
  80. state->data[state->data_pos++] = 0;
  81. }
  82. } else {
  83. state->data[state->data_pos++] = data;
  84. }
  85. }
  86. }
  87. static void send_block(uint8_t link, uint8_t* start, uint8_t* end, uint8_t num_non_zero) {
  88. send_data(link, &num_non_zero, 1);
  89. if (end > start) {
  90. send_data(link, start, end - start);
  91. }
  92. }
  93. void byte_stuffer_send_frame(uint8_t link, uint8_t* data, uint16_t size) {
  94. const uint8_t zero = 0;
  95. if (size > 0) {
  96. uint16_t num_non_zero = 1;
  97. uint8_t* end = data + size;
  98. uint8_t* start = data;
  99. while (data < end) {
  100. if (num_non_zero == 0xFF) {
  101. // There's more data after big non-zero block
  102. // So send it, and start a new block
  103. send_block(link, start, data, num_non_zero);
  104. start = data;
  105. num_non_zero = 1;
  106. } else {
  107. if (*data == 0) {
  108. // A zero encountered, so send the block
  109. send_block(link, start, data, num_non_zero);
  110. start = data + 1;
  111. num_non_zero = 1;
  112. } else {
  113. num_non_zero++;
  114. }
  115. ++data;
  116. }
  117. }
  118. send_block(link, start, data, num_non_zero);
  119. send_data(link, &zero, 1);
  120. }
  121. }