From 2a6696bd3d0837c2e655a99000bcbcc759b40075 Mon Sep 17 00:00:00 2001 From: Fred Sundvik Date: Sun, 14 Feb 2016 21:31:01 +0200 Subject: Add validator send frame --- serial_link/protocol/frame_validator.c | 7 +++++++ serial_link/tests/frame_validator_tests.c | 26 ++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) (limited to 'serial_link') diff --git a/serial_link/protocol/frame_validator.c b/serial_link/protocol/frame_validator.c index d3337f6e32..fdb3ef51c3 100644 --- a/serial_link/protocol/frame_validator.c +++ b/serial_link/protocol/frame_validator.c @@ -24,6 +24,7 @@ SOFTWARE. #include "protocol/frame_validator.h" #include "protocol/frame_router.h" +#include "protocol/byte_stuffer.h" const uint32_t poly8_lookup[256] = { @@ -110,3 +111,9 @@ void recv_frame(uint8_t* data, uint16_t size) { } } } + +void validator_send_frame(uint8_t* data, uint16_t size) { + uint32_t* crc = (uint32_t*)(data + size); + *crc = crc32_byte(data, size); + send_frame(data, size + 4); +} diff --git a/serial_link/tests/frame_validator_tests.c b/serial_link/tests/frame_validator_tests.c index 1aca9f95fc..816256f6b7 100644 --- a/serial_link/tests/frame_validator_tests.c +++ b/serial_link/tests/frame_validator_tests.c @@ -30,6 +30,10 @@ void route_frame(uint8_t* data, uint16_t size) { mock(data, size); } +void send_frame(uint8_t* data, uint16_t size) { + mock(data, size); +} + Describe(FrameValidator); BeforeEach(FrameValidator) {} AfterEach(FrameValidator) {} @@ -68,8 +72,6 @@ Ensure(FrameValidator, validates_four_byte_frame_with_correct_crc) { } Ensure(FrameValidator, validates_five_byte_frame_with_correct_crc) { - //0xBA304E74 - //0x470B99F4 uint8_t data[] = {1, 2, 3, 4, 5, 0xF4, 0x99, 0x0B, 0x47}; expect(route_frame, when(size, is_equal_to(5)), @@ -77,3 +79,23 @@ Ensure(FrameValidator, validates_five_byte_frame_with_correct_crc) { ); recv_frame(data, 9); } + +Ensure(FrameValidator, sends_one_byte_with_correct_crc) { + uint8_t original[] = {0x44, 0, 0, 0, 0}; + uint8_t expected[] = {0x44, 0x04, 0x6A, 0xB3, 0xA3}; + expect(send_frame, + when(size, is_equal_to(sizeof(expected))), + when(data, is_equal_to_contents_of(expected, sizeof(expected))) + ); + validator_send_frame(original, 1); +} + +Ensure(FrameValidator, sends_five_bytes_with_correct_crc) { + uint8_t original[] = {1, 2, 3, 4, 5, 0, 0, 0, 0}; + uint8_t expected[] = {1, 2, 3, 4, 5, 0xF4, 0x99, 0x0B, 0x47}; + expect(send_frame, + when(size, is_equal_to(sizeof(expected))), + when(data, is_equal_to_contents_of(expected, sizeof(expected))) + ); + validator_send_frame(original, 5); +} -- cgit v1.2.3