diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | serial_link/protocol/byte_stuffer.c | 17 | ||||
-rw-r--r-- | serial_link/tests/byte_stuffer_tests.c | 48 |
3 files changed, 61 insertions, 5 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..2d68e206e8 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.stackdump diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index cc7afe97ae..f0071b1f77 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -45,9 +45,6 @@ void init_byte_stuffer_state(byte_stuffer_state_t* state) { state->long_frame = false; } -static void start_frame(byte_stuffer_state_t* state, uint8_t data) { -} - void recv_byte(byte_stuffer_state_t* state, uint8_t data) { // Start of a new frame if (state->next_zero == 0) { @@ -61,7 +58,9 @@ void recv_byte(byte_stuffer_state_t* state, uint8_t data) { if (data == 0) { if (state->next_zero == 0) { // The frame is completed - recv_frame(state->data, state->data_pos); + if (state->data_pos > 0) { + recv_frame(state->data, state->data_pos); + } } else { // The frame is invalid, so reset @@ -69,8 +68,16 @@ void recv_byte(byte_stuffer_state_t* state, uint8_t data) { } } else { - if (state->next_zero == 0) { + 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; + } + 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; } diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index a28c361938..bfa0193861 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -55,6 +55,12 @@ Ensure(ByteStuffer, receives_no_frame_for_a_single_random_byte) { recv_byte(&state, 0x4A); } +Ensure(ByteStuffer, receives_no_frame_for_a_zero_length_frame) { + never_expect(recv_frame); + recv_byte(&state, 1); + recv_byte(&state, 0); +} + Ensure(ByteStuffer, receives_single_byte_valid_frame) { uint8_t expected[] = {0x37}; expect(recv_frame, @@ -246,3 +252,45 @@ Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { recv_byte(&state, 7); recv_byte(&state, 0); } + +Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) { + uint8_t expected[MAX_FRAME_SIZE] = {}; + expect(recv_frame, + when(size, is_equal_to(MAX_FRAME_SIZE)), + when(data, is_equal_to_contents_of(expected, MAX_FRAME_SIZE)) + ); + int i; + recv_byte(&state, 1); + for(i=0;i<MAX_FRAME_SIZE;i++) { + recv_byte(&state, 1); + } + recv_byte(&state, 0); +} + +Ensure(ByteStuffer, doesnt_recv_a_frame_thats_too_long_all_zeroes) { + uint8_t expected[1] = {0}; + never_expect(recv_frame); + int i; + recv_byte(&state, 1); + for(i=0;i<MAX_FRAME_SIZE;i++) { + recv_byte(&state, 1); + } + recv_byte(&state, 1); + recv_byte(&state, 0); +} + +Ensure(ByteStuffer, received_frame_is_aborted_when_its_too_long) { + uint8_t expected[1] = {1}; + expect(recv_frame, + when(size, is_equal_to(1)), + when(data, is_equal_to_contents_of(expected, 1)) + ); + int i; + recv_byte(&state, 1); + for(i=0;i<MAX_FRAME_SIZE;i++) { + recv_byte(&state, 1); + } + recv_byte(&state, 2); + recv_byte(&state, 1); + recv_byte(&state, 0); +} |