diff options
Diffstat (limited to 'serial_link/protocol')
-rw-r--r-- | serial_link/protocol/byte_stuffer.c | 52 | ||||
-rw-r--r-- | serial_link/protocol/byte_stuffer.h | 5 |
2 files changed, 29 insertions, 28 deletions
diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index f9aa02a3f5..e5a26c2c2e 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -40,56 +40,58 @@ typedef struct byte_stuffer_state { uint8_t data[MAX_FRAME_SIZE]; }byte_stuffer_state_t; -void init_byte_stuffer_state(byte_stuffer_state_t* state) { - state->next_zero = 0; - state->data_pos = 0; - state->long_frame = false; +static byte_stuffer_state_t state; + +void init_byte_stuffer(void) { + state.next_zero = 0; + state.data_pos = 0; + state.long_frame = false; } -void recv_byte(byte_stuffer_state_t* state, uint8_t data) { +void recv_byte(uint8_t data) { // Start of a new frame - if (state->next_zero == 0) { - state->next_zero = data; - state->long_frame = data == 0xFF; - state->data_pos = 0; + if (state.next_zero == 0) { + state.next_zero = data; + state.long_frame = data == 0xFF; + state.data_pos = 0; return; } - state->next_zero--; + state.next_zero--; if (data == 0) { - if (state->next_zero == 0) { + if (state.next_zero == 0) { // The frame is completed - if (state->data_pos > 0) { - validator_recv_frame(state->data, state->data_pos); + if (state.data_pos > 0) { + validator_recv_frame(state.data, state.data_pos); } } else { // The frame is invalid, so reset - init_byte_stuffer_state(state); + init_byte_stuffer(); } } else { - if (state->data_pos == MAX_FRAME_SIZE) { + if (state.data_pos == MAX_FRAME_SIZE) { // We exceeded our maximum frame size // therefore there's nothing else to do than reset to a new frame - state->next_zero = data; - state->long_frame = data == 0xFF; - state->data_pos = 0; + state.next_zero = data; + state.long_frame = data == 0xFF; + state.data_pos = 0; } - else if (state->next_zero == 0) { - if (state->long_frame) { + else if (state.next_zero == 0) { + if (state.long_frame) { // This is part of a long frame, so continue - state->next_zero = data; - state->long_frame = data == 0xFF; + state.next_zero = data; + state.long_frame = data == 0xFF; } else { // Special case for zeroes - state->next_zero = data; - state->data[state->data_pos++] = 0; + state.next_zero = data; + state.data[state.data_pos++] = 0; } } else { - state->data[state->data_pos++] = data; + state.data[state.data_pos++] = data; } } } diff --git a/serial_link/protocol/byte_stuffer.h b/serial_link/protocol/byte_stuffer.h index ea6b8451d4..4dcdd24c11 100644 --- a/serial_link/protocol/byte_stuffer.h +++ b/serial_link/protocol/byte_stuffer.h @@ -22,7 +22,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -typedef struct byte_stuffer_state byte_stuffer_state_t; -void init_byte_stuffer_state(byte_stuffer_state_t* state); -void recv_byte(byte_stuffer_state_t* state, uint8_t data); +void init_byte_stuffer(void); +void recv_byte(uint8_t data); void send_frame(uint8_t* data, uint16_t size); |