From 5a1600e24f2ca0cf8e9d75e23691bc1971657934 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 20 Feb 2016 12:06:23 +0200 Subject: [PATCH] Add link parameter --- serial_link/protocol/byte_stuffer.c | 77 ++++---- serial_link/protocol/byte_stuffer.h | 4 +- serial_link/protocol/frame_router.h | 2 +- serial_link/protocol/frame_validator.c | 8 +- serial_link/protocol/frame_validator.h | 4 +- serial_link/protocol/physical.h | 2 +- serial_link/tests/byte_stuffer_tests.c | 212 +++++++++++----------- serial_link/tests/frame_validator_tests.c | 24 +-- 8 files changed, 171 insertions(+), 162 deletions(-) diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index e5a26c2c2e..05b7aa6f80 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -32,6 +32,7 @@ SOFTWARE. // http://www.stuartcheshire.org/papers/COBSforToN.pdf #define MAX_FRAME_SIZE 1024 +#define NUM_LINKS 2 typedef struct byte_stuffer_state { uint16_t next_zero; @@ -40,70 +41,78 @@ typedef struct byte_stuffer_state { uint8_t data[MAX_FRAME_SIZE]; }byte_stuffer_state_t; -static byte_stuffer_state_t state; +static byte_stuffer_state_t states[NUM_LINKS]; -void init_byte_stuffer(void) { - state.next_zero = 0; - state.data_pos = 0; - state.long_frame = false; +void init_byte_stuffer_state(byte_stuffer_state_t* state) { + state->next_zero = 0; + state->data_pos = 0; + state->long_frame = false; } -void recv_byte(uint8_t data) { +void init_byte_stuffer(void) { + int i; + for (i=0;inext_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(link, state->data, state->data_pos); } } else { // The frame is invalid, so reset - init_byte_stuffer(); + init_byte_stuffer_state(state); } } 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; } } } -static void send_block(uint8_t* start, uint8_t* end, uint8_t num_non_zero) { - send_data(&num_non_zero, 1); +static void send_block(uint8_t link, uint8_t* start, uint8_t* end, uint8_t num_non_zero) { + send_data(link, &num_non_zero, 1); if (end > start) { - send_data(start, end-start); + send_data(link, start, end-start); } } -void send_frame(uint8_t* data, uint16_t size) { +void send_frame(uint8_t link, uint8_t* data, uint16_t size) { const uint8_t zero = 0; if (size > 0) { uint16_t num_non_zero = 1; @@ -113,14 +122,14 @@ void send_frame(uint8_t* data, uint16_t size) { if (num_non_zero == 0xFF) { // There's more data after big non-zero block // So send it, and start a new block - send_block(start, data, num_non_zero); + send_block(link, start, data, num_non_zero); start = data; num_non_zero = 1; } else { if (*data == 0) { // A zero encountered, so send the block - send_block(start, data, num_non_zero); + send_block(link, start, data, num_non_zero); start = data + 1; num_non_zero = 1; } @@ -130,7 +139,7 @@ void send_frame(uint8_t* data, uint16_t size) { ++data; } } - send_block(start, data, num_non_zero); - send_data(&zero, 1); + send_block(link, start, data, num_non_zero); + send_data(link, &zero, 1); } } diff --git a/serial_link/protocol/byte_stuffer.h b/serial_link/protocol/byte_stuffer.h index 4dcdd24c11..cd94b45828 100644 --- a/serial_link/protocol/byte_stuffer.h +++ b/serial_link/protocol/byte_stuffer.h @@ -23,5 +23,5 @@ SOFTWARE. */ void init_byte_stuffer(void); -void recv_byte(uint8_t data); -void send_frame(uint8_t* data, uint16_t size); +void recv_byte(uint8_t link, uint8_t data); +void send_frame(uint8_t link, uint8_t* data, uint16_t size); diff --git a/serial_link/protocol/frame_router.h b/serial_link/protocol/frame_router.h index dd1bfb1230..3906b06c71 100644 --- a/serial_link/protocol/frame_router.h +++ b/serial_link/protocol/frame_router.h @@ -22,4 +22,4 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -void route_incoming_frame(uint8_t* data, uint16_t size); +void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size); diff --git a/serial_link/protocol/frame_validator.c b/serial_link/protocol/frame_validator.c index f326097ffa..80e662b2e8 100644 --- a/serial_link/protocol/frame_validator.c +++ b/serial_link/protocol/frame_validator.c @@ -102,19 +102,19 @@ static uint32_t crc32_byte(uint8_t *p, uint32_t bytelength) return (crc ^ 0xffffffff); } -void validator_recv_frame(uint8_t* data, uint16_t size) { +void validator_recv_frame(uint8_t link, uint8_t* data, uint16_t size) { if (size > 4) { uint32_t frame_crc; memcpy(&frame_crc, data + size -4, 4); uint32_t expected_crc = crc32_byte(data, size - 4); if (frame_crc == expected_crc) { - route_incoming_frame(data, size-4); + route_incoming_frame(link, data, size-4); } } } -void validator_send_frame(uint8_t* data, uint16_t size) { +void validator_send_frame(uint8_t link, uint8_t* data, uint16_t size) { uint32_t crc = crc32_byte(data, size); memcpy(data + size, &crc, 4); - send_frame(data, size + 4); + send_frame(link, data, size + 4); } diff --git a/serial_link/protocol/frame_validator.h b/serial_link/protocol/frame_validator.h index 2e518894fb..c35fc27260 100644 --- a/serial_link/protocol/frame_validator.h +++ b/serial_link/protocol/frame_validator.h @@ -22,6 +22,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -void validator_recv_frame(uint8_t* data, uint16_t size); +void validator_recv_frame(uint8_t link, uint8_t* data, uint16_t size); // The buffer pointed to by the data needs 4 additional bytes -void validator_send_frame(uint8_t* data, uint16_t size); +void validator_send_frame(uint8_t link, uint8_t* data, uint16_t size); diff --git a/serial_link/protocol/physical.h b/serial_link/protocol/physical.h index 73a7855dfb..ee5883d36b 100644 --- a/serial_link/protocol/physical.h +++ b/serial_link/protocol/physical.h @@ -22,4 +22,4 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -void send_data(const uint8_t* data, uint16_t size); +void send_data(uint8_t link, const uint8_t* data, uint16_t size); diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index f29cc12993..0f58479cb9 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -39,34 +39,34 @@ BeforeEach(ByteStuffer) { } AfterEach(ByteStuffer) {} -void validator_recv_frame(uint8_t* data, uint16_t size) { +void validator_recv_frame(uint8_t link, uint8_t* data, uint16_t size) { mock(data, size); } -void send_data(const uint8_t* data, uint16_t size) { +void send_data(uint8_t link, const uint8_t* data, uint16_t size) { memcpy(sent_data + sent_data_size, data, size); sent_data_size += size; } Ensure(ByteStuffer, receives_no_frame_for_a_single_zero_byte) { never_expect(validator_recv_frame); - recv_byte(0); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_no_frame_for_a_single_FF_byte) { never_expect(validator_recv_frame); - recv_byte(0xFF); + recv_byte(0, 0xFF); } Ensure(ByteStuffer, receives_no_frame_for_a_single_random_byte) { never_expect(validator_recv_frame); - recv_byte(0x4A); + recv_byte(0, 0x4A); } Ensure(ByteStuffer, receives_no_frame_for_a_zero_length_frame) { never_expect(validator_recv_frame); - recv_byte(1); - recv_byte(0); + recv_byte(0, 1); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_single_byte_valid_frame) { @@ -75,9 +75,9 @@ Ensure(ByteStuffer, receives_single_byte_valid_frame) { when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected, 1)) ); - recv_byte(2); - recv_byte(0x37); - recv_byte(0); + recv_byte(0, 2); + recv_byte(0, 0x37); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_three_bytes_valid_frame) { @@ -86,11 +86,11 @@ Ensure(ByteStuffer, receives_three_bytes_valid_frame) { when(size, is_equal_to(3)), when(data, is_equal_to_contents_of(expected, 3)) ); - recv_byte(4); - recv_byte(0x37); - recv_byte(0x99); - recv_byte(0xFF); - recv_byte(0); + recv_byte(0, 4); + recv_byte(0, 0x37); + recv_byte(0, 0x99); + recv_byte(0, 0xFF); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_single_zero_valid_frame) { @@ -99,9 +99,9 @@ Ensure(ByteStuffer, receives_single_zero_valid_frame) { when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected, 1)) ); - recv_byte(1); - recv_byte(1); - recv_byte(0); + recv_byte(0, 1); + recv_byte(0, 1); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_valid_frame_with_zeroes) { @@ -110,12 +110,12 @@ Ensure(ByteStuffer, receives_valid_frame_with_zeroes) { when(size, is_equal_to(4)), when(data, is_equal_to_contents_of(expected, 4)) ); - recv_byte(2); - recv_byte(5); - recv_byte(2); - recv_byte(3); - recv_byte(1); - recv_byte(0); + recv_byte(0, 2); + recv_byte(0, 5); + recv_byte(0, 2); + recv_byte(0, 3); + recv_byte(0, 1); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_two_valid_frames) { @@ -129,13 +129,13 @@ Ensure(ByteStuffer, receives_two_valid_frames) { when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(expected2, 1)) ); - recv_byte(2); - recv_byte(5); - recv_byte(1); - recv_byte(0); - recv_byte(2); - recv_byte(3); - recv_byte(0); + recv_byte(1, 2); + recv_byte(1, 5); + recv_byte(1, 1); + recv_byte(1, 0); + recv_byte(1, 2); + recv_byte(1, 3); + recv_byte(1, 0); } Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) { @@ -144,13 +144,13 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_zero) { when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected, 2)) ); - recv_byte(3); - recv_byte(1); - recv_byte(0); - recv_byte(3); - recv_byte(5); - recv_byte(7); - recv_byte(0); + recv_byte(1, 3); + recv_byte(1, 1); + recv_byte(1, 0); + recv_byte(1, 3); + recv_byte(1, 5); + recv_byte(1, 7); + recv_byte(1, 0); } Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) { @@ -159,14 +159,14 @@ Ensure(ByteStuffer, receives_valid_frame_after_unexpected_non_zero) { when(size, is_equal_to(2)), when(data, is_equal_to_contents_of(expected, 2)) ); - recv_byte(2); - recv_byte(9); - recv_byte(4); // This should have been zero - recv_byte(0); - recv_byte(3); - recv_byte(5); - recv_byte(7); - recv_byte(0); + recv_byte(0, 2); + recv_byte(0, 9); + recv_byte(0, 4); // This should have been zero + recv_byte(0, 0); + recv_byte(0, 3); + recv_byte(0, 5); + recv_byte(0, 7); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_of_frame) { @@ -179,11 +179,11 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_and_then_end_ when(size, is_equal_to(254)), when(data, is_equal_to_contents_of(expected, 254)) ); - recv_byte(0xFF); + recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(i+1); + recv_byte(0, i+1); } - recv_byte(0); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_non_zero) { @@ -197,13 +197,13 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_ when(size, is_equal_to(255)), when(data, is_equal_to_contents_of(expected, 255)) ); - recv_byte(0xFF); + recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(i+1); + recv_byte(0, i+1); } - recv_byte(2); - recv_byte(7); - recv_byte(0); + recv_byte(0, 2); + recv_byte(0, 7); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_zero) { @@ -217,13 +217,13 @@ Ensure(ByteStuffer, receives_a_valid_frame_with_over254_non_zeroes_next_byte_is_ when(size, is_equal_to(255)), when(data, is_equal_to_contents_of(expected, 255)) ); - recv_byte(0xFF); + recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(i+1); + recv_byte(0, i+1); } - recv_byte(1); - recv_byte(1); - recv_byte(0); + recv_byte(0, 1); + recv_byte(0, 1); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { @@ -242,23 +242,23 @@ Ensure(ByteStuffer, receives_two_long_frames_and_some_more) { when(size, is_equal_to(515)), when(data, is_equal_to_contents_of(expected, 510)) ); - recv_byte(0xFF); + recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(i+1); + recv_byte(0, i+1); } - recv_byte(0xFF); + recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(i+1); + recv_byte(0, i+1); } - recv_byte(8); - recv_byte(1); - recv_byte(2); - recv_byte(3); - recv_byte(4); - recv_byte(5); - recv_byte(6); - recv_byte(7); - recv_byte(0); + recv_byte(0, 8); + recv_byte(0, 1); + recv_byte(0, 2); + recv_byte(0, 3); + recv_byte(0, 4); + recv_byte(0, 5); + recv_byte(0, 6); + recv_byte(0, 7); + recv_byte(0, 0); } Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) { @@ -268,23 +268,23 @@ Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) { when(data, is_equal_to_contents_of(expected, MAX_FRAME_SIZE)) ); int i; - recv_byte(1); + recv_byte(0, 1); for(i=0;i #include "protocol/frame_validator.c" -void route_incoming_frame(uint8_t* data, uint16_t size) { +void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size) { mock(data, size); } -void send_frame(uint8_t* data, uint16_t size) { +void send_frame(uint8_t link, uint8_t* data, uint16_t size) { mock(data, size); } @@ -41,10 +41,10 @@ AfterEach(FrameValidator) {} Ensure(FrameValidator, doesnt_validate_frames_under_5_bytes) { never_expect(route_incoming_frame); uint8_t data[] = {1, 2}; - validator_recv_frame(0, 1); - validator_recv_frame(data, 2); - validator_recv_frame(data, 3); - validator_recv_frame(data, 4); + validator_recv_frame(0, 0, 1); + validator_recv_frame(0, data, 2); + validator_recv_frame(0, data, 3); + validator_recv_frame(0, data, 4); } Ensure(FrameValidator, validates_one_byte_frame_with_correct_crc) { @@ -53,13 +53,13 @@ Ensure(FrameValidator, validates_one_byte_frame_with_correct_crc) { when(size, is_equal_to(1)), when(data, is_equal_to_contents_of(data, 1)) ); - validator_recv_frame(data, 5); + validator_recv_frame(0, data, 5); } Ensure(FrameValidator, does_not_validate_one_byte_frame_with_incorrect_crc) { uint8_t data[] = {0x44, 0, 0, 0, 0}; never_expect(route_incoming_frame); - validator_recv_frame(data, 5); + validator_recv_frame(1, data, 5); } Ensure(FrameValidator, validates_four_byte_frame_with_correct_crc) { @@ -68,7 +68,7 @@ Ensure(FrameValidator, validates_four_byte_frame_with_correct_crc) { when(size, is_equal_to(4)), when(data, is_equal_to_contents_of(data, 4)) ); - validator_recv_frame(data, 8); + validator_recv_frame(1, data, 8); } Ensure(FrameValidator, validates_five_byte_frame_with_correct_crc) { @@ -77,7 +77,7 @@ Ensure(FrameValidator, validates_five_byte_frame_with_correct_crc) { when(size, is_equal_to(5)), when(data, is_equal_to_contents_of(data, 5)) ); - validator_recv_frame(data, 9); + validator_recv_frame(0, data, 9); } Ensure(FrameValidator, sends_one_byte_with_correct_crc) { @@ -87,7 +87,7 @@ Ensure(FrameValidator, sends_one_byte_with_correct_crc) { when(size, is_equal_to(sizeof(expected))), when(data, is_equal_to_contents_of(expected, sizeof(expected))) ); - validator_send_frame(original, 1); + validator_send_frame(0, original, 1); } Ensure(FrameValidator, sends_five_bytes_with_correct_crc) { @@ -97,5 +97,5 @@ Ensure(FrameValidator, sends_five_bytes_with_correct_crc) { when(size, is_equal_to(sizeof(expected))), when(data, is_equal_to_contents_of(expected, sizeof(expected))) ); - validator_send_frame(original, 5); + validator_send_frame(0, original, 5); }