From 4b9fccc75633b4ea297de373d5d7f01288d2fb11 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sat, 20 Feb 2016 13:59:48 +0200 Subject: [PATCH] Start implement router --- serial_link/protocol/byte_stuffer.c | 4 +- serial_link/protocol/byte_stuffer.h | 4 +- serial_link/protocol/frame_router.c | 33 ++++ serial_link/protocol/frame_router.h | 4 + serial_link/protocol/frame_validator.c | 2 +- serial_link/tests/byte_stuffer_tests.c | 208 +++++++++++----------- serial_link/tests/frame_router_tests.c | 89 +++++++++ serial_link/tests/frame_validator_tests.c | 6 +- 8 files changed, 238 insertions(+), 112 deletions(-) create mode 100644 serial_link/protocol/frame_router.c create mode 100644 serial_link/tests/frame_router_tests.c diff --git a/serial_link/protocol/byte_stuffer.c b/serial_link/protocol/byte_stuffer.c index 05b7aa6f..8b529667 100644 --- a/serial_link/protocol/byte_stuffer.c +++ b/serial_link/protocol/byte_stuffer.c @@ -56,7 +56,7 @@ void init_byte_stuffer(void) { } } -void recv_byte(uint8_t link, uint8_t data) { +void byte_stuffer_recv_byte(uint8_t link, uint8_t data) { byte_stuffer_state_t* state = &states[link]; // Start of a new frame if (state->next_zero == 0) { @@ -112,7 +112,7 @@ static void send_block(uint8_t link, uint8_t* start, uint8_t* end, uint8_t num_n } } -void send_frame(uint8_t link, uint8_t* data, uint16_t size) { +void byte_stuffer_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; diff --git a/serial_link/protocol/byte_stuffer.h b/serial_link/protocol/byte_stuffer.h index cd94b458..839a876f 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 link, uint8_t data); -void send_frame(uint8_t link, uint8_t* data, uint16_t size); +void byte_stuffer_recv_byte(uint8_t link, uint8_t data); +void byte_stuffer_send_frame(uint8_t link, uint8_t* data, uint16_t size); diff --git a/serial_link/protocol/frame_router.c b/serial_link/protocol/frame_router.c new file mode 100644 index 00000000..db6223a8 --- /dev/null +++ b/serial_link/protocol/frame_router.c @@ -0,0 +1,33 @@ +/* +The MIT License (MIT) + +Copyright (c) 2016 Fred Sundvik + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +*/ + +#include "protocol/frame_router.h" + +void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size){ + validator_send_frame(DOWN_LINK, data, size); +} + +void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size) { + validator_send_frame(DOWN_LINK, data, size); +} diff --git a/serial_link/protocol/frame_router.h b/serial_link/protocol/frame_router.h index 3906b06c..a0238079 100644 --- a/serial_link/protocol/frame_router.h +++ b/serial_link/protocol/frame_router.h @@ -22,4 +22,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#define UP_LINK 0 +#define DOWN_LINK 1 + void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size); +void router_send_frame(uint8_t destination, uint8_t* data, uint16_t size); diff --git a/serial_link/protocol/frame_validator.c b/serial_link/protocol/frame_validator.c index 80e662b2..a3face65 100644 --- a/serial_link/protocol/frame_validator.c +++ b/serial_link/protocol/frame_validator.c @@ -116,5 +116,5 @@ void validator_recv_frame(uint8_t link, 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(link, data, size + 4); + byte_stuffer_send_frame(link, data, size + 4); } diff --git a/serial_link/tests/byte_stuffer_tests.c b/serial_link/tests/byte_stuffer_tests.c index 0f58479c..912c4d32 100644 --- a/serial_link/tests/byte_stuffer_tests.c +++ b/serial_link/tests/byte_stuffer_tests.c @@ -50,23 +50,23 @@ void send_data(uint8_t link, const uint8_t* data, uint16_t size) { Ensure(ByteStuffer, receives_no_frame_for_a_single_zero_byte) { never_expect(validator_recv_frame); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 0); } Ensure(ByteStuffer, receives_no_frame_for_a_single_FF_byte) { never_expect(validator_recv_frame); - recv_byte(0, 0xFF); + byte_stuffer_recv_byte(0, 0xFF); } Ensure(ByteStuffer, receives_no_frame_for_a_single_random_byte) { never_expect(validator_recv_frame); - recv_byte(0, 0x4A); + byte_stuffer_recv_byte(0, 0x4A); } Ensure(ByteStuffer, receives_no_frame_for_a_zero_length_frame) { never_expect(validator_recv_frame); - recv_byte(0, 1); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_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(0, 2); - recv_byte(0, 0x37); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 2); + byte_stuffer_recv_byte(0, 0x37); + byte_stuffer_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(0, 4); - recv_byte(0, 0x37); - recv_byte(0, 0x99); - recv_byte(0, 0xFF); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 4); + byte_stuffer_recv_byte(0, 0x37); + byte_stuffer_recv_byte(0, 0x99); + byte_stuffer_recv_byte(0, 0xFF); + byte_stuffer_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(0, 1); - recv_byte(0, 1); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_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(0, 2); - recv_byte(0, 5); - recv_byte(0, 2); - recv_byte(0, 3); - recv_byte(0, 1); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 2); + byte_stuffer_recv_byte(0, 5); + byte_stuffer_recv_byte(0, 2); + byte_stuffer_recv_byte(0, 3); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_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(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); + byte_stuffer_recv_byte(1, 2); + byte_stuffer_recv_byte(1, 5); + byte_stuffer_recv_byte(1, 1); + byte_stuffer_recv_byte(1, 0); + byte_stuffer_recv_byte(1, 2); + byte_stuffer_recv_byte(1, 3); + byte_stuffer_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(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); + byte_stuffer_recv_byte(1, 3); + byte_stuffer_recv_byte(1, 1); + byte_stuffer_recv_byte(1, 0); + byte_stuffer_recv_byte(1, 3); + byte_stuffer_recv_byte(1, 5); + byte_stuffer_recv_byte(1, 7); + byte_stuffer_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(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); + byte_stuffer_recv_byte(0, 2); + byte_stuffer_recv_byte(0, 9); + byte_stuffer_recv_byte(0, 4); // This should have been zero + byte_stuffer_recv_byte(0, 0); + byte_stuffer_recv_byte(0, 3); + byte_stuffer_recv_byte(0, 5); + byte_stuffer_recv_byte(0, 7); + byte_stuffer_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(0, 0xFF); + byte_stuffer_recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(0, i+1); + byte_stuffer_recv_byte(0, i+1); } - recv_byte(0, 0); + byte_stuffer_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(0, 0xFF); + byte_stuffer_recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(0, i+1); + byte_stuffer_recv_byte(0, i+1); } - recv_byte(0, 2); - recv_byte(0, 7); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 2); + byte_stuffer_recv_byte(0, 7); + byte_stuffer_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(0, 0xFF); + byte_stuffer_recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(0, i+1); + byte_stuffer_recv_byte(0, i+1); } - recv_byte(0, 1); - recv_byte(0, 1); - recv_byte(0, 0); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_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(0, 0xFF); + byte_stuffer_recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(0, i+1); + byte_stuffer_recv_byte(0, i+1); } - recv_byte(0, 0xFF); + byte_stuffer_recv_byte(0, 0xFF); for (i=0;i<254;i++) { - recv_byte(0, i+1); + byte_stuffer_recv_byte(0, i+1); } - 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); + byte_stuffer_recv_byte(0, 8); + byte_stuffer_recv_byte(0, 1); + byte_stuffer_recv_byte(0, 2); + byte_stuffer_recv_byte(0, 3); + byte_stuffer_recv_byte(0, 4); + byte_stuffer_recv_byte(0, 5); + byte_stuffer_recv_byte(0, 6); + byte_stuffer_recv_byte(0, 7); + byte_stuffer_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(0, 1); + byte_stuffer_recv_byte(0, 1); for(i=0;i +#include +#include "protocol/byte_stuffer.c" +#include "protocol/frame_validator.c" +#include "protocol/frame_router.c" + +static uint8_t received_data[256]; +static uint16_t received_data_size; + +typedef struct { + uint8_t received_data[256]; + uint16_t sent_data_size; +} receive_buffer_t; + +typedef struct { + receive_buffer_t send_buffers[2]; +} router_buffer_t; + +router_buffer_t router_buffers[8]; + +router_buffer_t* current_router_buffer; + + +Describe(ByteStuffer); +BeforeEach(ByteStuffer) { + init_byte_stuffer(); + memset(router_buffers, 0, sizeof(router_buffers)); + current_router_buffer = 0; +} +AfterEach(ByteStuffer) {} + +typedef struct { + uint32_t data; + uint8_t extra[16]; +} frame_buffer_t; + + +void send_data(uint8_t link, const uint8_t* data, uint16_t size) { + receive_buffer_t* buffer = ¤t_router_buffer->send_buffers[link]; + memcpy(buffer->received_data + buffer->sent_data_size, data, size); + buffer->sent_data_size += size; +} + +static void receive_data(uint8_t link, uint8_t* data, uint16_t size) { + int i; + for(i=0;i