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