Add sending of small frames with no zeroes

This commit is contained in:
Fred Sundvik 2016-02-14 17:45:25 +02:00
parent 26537474ae
commit a089eaa868
4 changed files with 82 additions and 16 deletions

View file

@ -24,7 +24,7 @@ SOFTWARE.
#include "protocol/byte_stuffer.h"
#include "protocol/frame_validator.h"
#include <stdio.h>
#include "protocol/physical.h"
// This implements the "Consistent overhead byte stuffing protocol"
// https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing
@ -92,3 +92,13 @@ void recv_byte(byte_stuffer_state_t* state, uint8_t data) {
}
}
}
void send_frame(uint8_t* data, uint16_t size) {
if (size > 0) {
uint8_t numZeroes = size + 1;
const uint8_t zero = 0;
send_data(&numZeroes, 1);
send_data(data, size);
send_data(&zero, 1);
}
}

View file

@ -25,3 +25,4 @@ SOFTWARE.
typedef struct byte_stuffer_state byte_stuffer_state_t;
void init_byte_stuffer_state(byte_stuffer_state_t* state);
void recv_byte(byte_stuffer_state_t* state, uint8_t data);
void send_frame(uint8_t* data, uint16_t size);

View file

@ -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 send_data(const uint8_t* data, uint16_t size);

View file

@ -27,12 +27,16 @@ SOFTWARE.
#include "protocol/byte_stuffer.h"
#include "protocol/byte_stuffer.c"
#include "protocol/frame_validator.h"
#include "protocol/physical.h"
byte_stuffer_state_t state;
static byte_stuffer_state_t state;
static uint8_t sent_data[MAX_FRAME_SIZE*2];
static uint16_t sent_data_size;
Describe(ByteStuffer);
BeforeEach(ByteStuffer) {
init_byte_stuffer_state(&state);
sent_data_size = 0;
}
AfterEach(ByteStuffer) {}
@ -40,6 +44,11 @@ void recv_frame(uint8_t* data, uint16_t size) {
mock(data, size);
}
void send_data(const uint8_t* data, uint16_t size) {
memcpy(sent_data + sent_data_size, data, size);
sent_data_size += size;
}
Ensure(ByteStuffer, receives_no_frame_for_a_single_zero_byte) {
never_expect(recv_frame);
recv_byte(&state, 0);
@ -294,3 +303,24 @@ Ensure(ByteStuffer, received_frame_is_aborted_when_its_too_long) {
recv_byte(&state, 1);
recv_byte(&state, 0);
}
Ensure(ByteStuffer, send_zero_size_frame_does_nothing) {
assert_that(sent_data_size, is_equal_to(0));
send_frame(NULL, 0);
}
Ensure(ByteStuffer, send_one_byte_frame) {
uint8_t data[] = {5};
send_frame(data, 1);
assert_that(sent_data_size, is_equal_to(3));
uint8_t expected[] = {2, 5, 0};
assert_that(sent_data, is_equal_to_contents_of(expected, 3));
}
Ensure(ByteStuffer, send_two_byte_frame) {
uint8_t data[] = {5, 0x77};
send_frame(data, 2);
assert_that(sent_data_size, is_equal_to(4));
uint8_t expected[] = {3, 5, 0x77, 0};
assert_that(sent_data, is_equal_to_contents_of(expected, 4));
}