1
0
Fork 0
forked from forks/qmk_firmware

Add byte stuffer recv handling of too long frames

This commit is contained in:
Fred Sundvik 2016-02-14 15:57:44 +02:00
parent e8cb6d8023
commit 26537474ae
3 changed files with 61 additions and 5 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
*.stackdump

View file

@ -45,9 +45,6 @@ void init_byte_stuffer_state(byte_stuffer_state_t* state) {
state->long_frame = false; state->long_frame = false;
} }
static void start_frame(byte_stuffer_state_t* state, uint8_t data) {
}
void recv_byte(byte_stuffer_state_t* state, uint8_t data) { void recv_byte(byte_stuffer_state_t* state, uint8_t data) {
// Start of a new frame // Start of a new frame
if (state->next_zero == 0) { if (state->next_zero == 0) {
@ -61,7 +58,9 @@ void recv_byte(byte_stuffer_state_t* state, uint8_t data) {
if (data == 0) { if (data == 0) {
if (state->next_zero == 0) { if (state->next_zero == 0) {
// The frame is completed // The frame is completed
recv_frame(state->data, state->data_pos); if (state->data_pos > 0) {
recv_frame(state->data, state->data_pos);
}
} }
else { else {
// The frame is invalid, so reset // The frame is invalid, so reset
@ -69,8 +68,16 @@ void recv_byte(byte_stuffer_state_t* state, uint8_t data) {
} }
} }
else { else {
if (state->next_zero == 0) { 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;
}
else if (state->next_zero == 0) {
if (state->long_frame) { if (state->long_frame) {
// This is part of a long frame, so continue
state->next_zero = data; state->next_zero = data;
state->long_frame = data == 0xFF; state->long_frame = data == 0xFF;
} }

View file

@ -55,6 +55,12 @@ Ensure(ByteStuffer, receives_no_frame_for_a_single_random_byte) {
recv_byte(&state, 0x4A); recv_byte(&state, 0x4A);
} }
Ensure(ByteStuffer, receives_no_frame_for_a_zero_length_frame) {
never_expect(recv_frame);
recv_byte(&state, 1);
recv_byte(&state, 0);
}
Ensure(ByteStuffer, receives_single_byte_valid_frame) { Ensure(ByteStuffer, receives_single_byte_valid_frame) {
uint8_t expected[] = {0x37}; uint8_t expected[] = {0x37};
expect(recv_frame, expect(recv_frame,
@ -246,3 +252,45 @@ Ensure(ByteStuffer, receives_two_long_frames_and_some_more) {
recv_byte(&state, 7); recv_byte(&state, 7);
recv_byte(&state, 0); recv_byte(&state, 0);
} }
Ensure(ByteStuffer, receives_an_all_zeros_frame_that_is_maximum_size) {
uint8_t expected[MAX_FRAME_SIZE] = {};
expect(recv_frame,
when(size, is_equal_to(MAX_FRAME_SIZE)),
when(data, is_equal_to_contents_of(expected, MAX_FRAME_SIZE))
);
int i;
recv_byte(&state, 1);
for(i=0;i<MAX_FRAME_SIZE;i++) {
recv_byte(&state, 1);
}
recv_byte(&state, 0);
}
Ensure(ByteStuffer, doesnt_recv_a_frame_thats_too_long_all_zeroes) {
uint8_t expected[1] = {0};
never_expect(recv_frame);
int i;
recv_byte(&state, 1);
for(i=0;i<MAX_FRAME_SIZE;i++) {
recv_byte(&state, 1);
}
recv_byte(&state, 1);
recv_byte(&state, 0);
}
Ensure(ByteStuffer, received_frame_is_aborted_when_its_too_long) {
uint8_t expected[1] = {1};
expect(recv_frame,
when(size, is_equal_to(1)),
when(data, is_equal_to_contents_of(expected, 1))
);
int i;
recv_byte(&state, 1);
for(i=0;i<MAX_FRAME_SIZE;i++) {
recv_byte(&state, 1);
}
recv_byte(&state, 2);
recv_byte(&state, 1);
recv_byte(&state, 0);
}