]> git.donarmstrong.com Git - qmk_firmware.git/commitdiff
Add crc32 validation of received frames
authorFred Sundvik <fsundvik@gmail.com>
Sun, 14 Feb 2016 19:13:16 +0000 (21:13 +0200)
committerFred Sundvik <fsundvik@gmail.com>
Sun, 14 Feb 2016 19:13:16 +0000 (21:13 +0200)
serial_link/protocol/frame_validator.c
serial_link/tests/frame_validator_tests.c

index 1ffd3aad855b5f1f3e847e806fa18656ef123aa3..d3337f6e3287072f88b43b5c05c3ec47ed24687a 100644 (file)
@@ -23,6 +23,7 @@ SOFTWARE.
 */
 
 #include "protocol/frame_validator.h"
+#include "protocol/frame_router.h"
 
 const uint32_t poly8_lookup[256] =
 {
@@ -101,5 +102,11 @@ static uint32_t crc32_byte(uint8_t *p, uint32_t bytelength)
 }
 
 void recv_frame(uint8_t* data, uint16_t size) {
-
+    if (size > 4) {
+        uint32_t frame_crc = *(uint32_t*)(data + size - 4);
+        uint32_t expected_crc = crc32_byte(data, size - 4);
+        if (frame_crc == expected_crc) {
+            route_frame(data, size-4);
+        }
+    }
 }
index f6a4fcd84285f003565defae9044beaf9ef498fb..1aca9f95fce6b810e8a69cce549c64bda567c7b7 100644 (file)
@@ -42,3 +42,38 @@ Ensure(FrameValidator, doesnt_validate_frames_under_5_bytes) {
     recv_frame(data, 3);
     recv_frame(data, 4);
 }
+
+Ensure(FrameValidator, validates_one_byte_frame_with_correct_crc) {
+    uint8_t data[] = {0x44, 0x04, 0x6A, 0xB3, 0xA3};
+    expect(route_frame,
+        when(size, is_equal_to(1)),
+        when(data, is_equal_to_contents_of(data, 1))
+    );
+    recv_frame(data, 5);
+}
+
+Ensure(FrameValidator, does_not_validate_one_byte_frame_with_incorrect_crc) {
+    uint8_t data[] = {0x44, 0, 0, 0, 0};
+    never_expect(route_frame);
+    recv_frame(data, 5);
+}
+
+Ensure(FrameValidator, validates_four_byte_frame_with_correct_crc) {
+    uint8_t data[] = {0x44, 0x10, 0xFF, 0x00, 0x74, 0x4E, 0x30, 0xBA};
+    expect(route_frame,
+        when(size, is_equal_to(4)),
+        when(data, is_equal_to_contents_of(data, 4))
+    );
+    recv_frame(data, 8);
+}
+
+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)),
+        when(data, is_equal_to_contents_of(data, 5))
+    );
+    recv_frame(data, 9);
+}