summaryrefslogtreecommitdiffstats
path: root/serial_link
diff options
context:
space:
mode:
authorFred Sundvik <fsundvik@gmail.com>2016-02-20 14:32:26 +0200
committerFred Sundvik <fsundvik@gmail.com>2016-02-20 14:32:26 +0200
commitcee5406ed76f16670a54720b8d3c6d8f8c193c49 (patch)
treea0bfb14af2f2d707ca0e87ee60f4b8b7e219ee39 /serial_link
parent4b9fccc75633b4ea297de373d5d7f01288d2fb11 (diff)
Add complete master broadcast test
Diffstat (limited to 'serial_link')
-rw-r--r--serial_link/protocol/frame_router.c8
-rw-r--r--serial_link/protocol/frame_router.h1
-rw-r--r--serial_link/protocol/transport.h25
-rw-r--r--serial_link/tests/frame_router_tests.c45
4 files changed, 73 insertions, 6 deletions
diff --git a/serial_link/protocol/frame_router.c b/serial_link/protocol/frame_router.c
index db6223a8d7..0675cea284 100644
--- a/serial_link/protocol/frame_router.c
+++ b/serial_link/protocol/frame_router.c
@@ -23,8 +23,16 @@ SOFTWARE.
*/
#include "protocol/frame_router.h"
+#include "protocol/transport.h"
+
+static bool is_master;
+
+void router_set_master(bool master) {
+ is_master = master;
+}
void route_incoming_frame(uint8_t link, uint8_t* data, uint16_t size){
+ transport_recv_frame(0, data, 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 a0238079e8..67db3122fa 100644
--- a/serial_link/protocol/frame_router.h
+++ b/serial_link/protocol/frame_router.h
@@ -25,5 +25,6 @@ SOFTWARE.
#define UP_LINK 0
#define DOWN_LINK 1
+void router_set_master(bool master);
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/transport.h b/serial_link/protocol/transport.h
new file mode 100644
index 0000000000..a40d64d414
--- /dev/null
+++ b/serial_link/protocol/transport.h
@@ -0,0 +1,25 @@
+/*
+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.
+*/
+
+void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size);
diff --git a/serial_link/tests/frame_router_tests.c b/serial_link/tests/frame_router_tests.c
index 5ce6c9c0f5..eb0c710ea1 100644
--- a/serial_link/tests/frame_router_tests.c
+++ b/serial_link/tests/frame_router_tests.c
@@ -27,12 +27,13 @@ SOFTWARE.
#include "protocol/byte_stuffer.c"
#include "protocol/frame_validator.c"
#include "protocol/frame_router.c"
+#include "protocol/transport.h"
static uint8_t received_data[256];
static uint16_t received_data_size;
typedef struct {
- uint8_t received_data[256];
+ uint8_t sent_data[256];
uint16_t sent_data_size;
} receive_buffer_t;
@@ -61,7 +62,7 @@ typedef struct {
void send_data(uint8_t link, const uint8_t* data, uint16_t size) {
receive_buffer_t* buffer = &current_router_buffer->send_buffers[link];
- memcpy(buffer->received_data + buffer->sent_data_size, data, size);
+ memcpy(buffer->sent_data + buffer->sent_data_size, data, size);
buffer->sent_data_size += size;
}
@@ -72,18 +73,50 @@ static void receive_data(uint8_t link, uint8_t* data, uint16_t size) {
}
}
+static void activate_router(uint8_t num) {
+ current_router_buffer = router_buffers + num;
+ router_set_master(num==0);
+}
+
+static void simulate_transport(uint8_t from, uint8_t to) {
+ activate_router(to);
+ if (from > to) {
+ }
+ else if(to > from) {
+ receive_data(UP_LINK,
+ router_buffers[from].send_buffers[DOWN_LINK].sent_data,
+ router_buffers[from].send_buffers[DOWN_LINK].sent_data_size);
+ }
+}
+
+void transport_recv_frame(uint8_t from, uint8_t* data, uint16_t size) {
+ mock(from, data, size);
+}
+
Ensure(ByteStuffer, master_broadcast_is_received_by_everyone) {
frame_buffer_t data;
data.data = 0xAB7055BB;
- current_router_buffer = router_buffers + 0;
+ activate_router(0);
router_send_frame(0xFF, (uint8_t*)&data, 4);
assert_that(router_buffers[0].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0));
assert_that(router_buffers[0].send_buffers[UP_LINK].sent_data_size, is_equal_to(0));
- current_router_buffer = router_buffers + 1;
- receive_data(UP_LINK, router_buffers[0].send_buffers[DOWN_LINK].received_data,
- router_buffers[0].send_buffers[DOWN_LINK].sent_data_size);
+ expect(transport_recv_frame,
+ when(from, is_equal_to(0)),
+ when(size, is_equal_to(4)),
+ when(data, is_equal_to_contents_of(&data.data, 4))
+ );
+ simulate_transport(0, 1);
assert_that(router_buffers[1].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0));
assert_that(router_buffers[1].send_buffers[UP_LINK].sent_data_size, is_equal_to(0));
+
+ expect(transport_recv_frame,
+ when(from, is_equal_to(0)),
+ when(size, is_equal_to(4)),
+ when(data, is_equal_to_contents_of(&data.data, 4))
+ );
+ simulate_transport(1, 2);
+ assert_that(router_buffers[2].send_buffers[DOWN_LINK].sent_data_size, is_greater_than(0));
+ assert_that(router_buffers[2].send_buffers[UP_LINK].sent_data_size, is_equal_to(0));
}