|
|
@ -24,26 +24,35 @@ SOFTWARE.
|
|
|
|
|
|
|
|
|
|
|
|
#include "protocol/byte_stuffer.h"
|
|
|
|
#include "protocol/byte_stuffer.h"
|
|
|
|
#include "protocol/frame_validator.h"
|
|
|
|
#include "protocol/frame_validator.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
|
|
// This implements the "Consistent overhead byte stuffing protocol"
|
|
|
|
// This implements the "Consistent overhead byte stuffing protocol"
|
|
|
|
// https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing
|
|
|
|
// https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing
|
|
|
|
// http://www.stuartcheshire.org/papers/COBSforToN.pdf
|
|
|
|
// http://www.stuartcheshire.org/papers/COBSforToN.pdf
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define MAX_FRAME_SIZE 1024
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct byte_stuffer_state {
|
|
|
|
typedef struct byte_stuffer_state {
|
|
|
|
uint16_t next_zero;
|
|
|
|
uint16_t next_zero;
|
|
|
|
uint16_t data_pos;
|
|
|
|
uint16_t data_pos;
|
|
|
|
uint8_t data[256];
|
|
|
|
bool long_frame;
|
|
|
|
|
|
|
|
uint8_t data[MAX_FRAME_SIZE];
|
|
|
|
}byte_stuffer_state_t;
|
|
|
|
}byte_stuffer_state_t;
|
|
|
|
|
|
|
|
|
|
|
|
void init_byte_stuffer_state(byte_stuffer_state_t* state) {
|
|
|
|
void init_byte_stuffer_state(byte_stuffer_state_t* state) {
|
|
|
|
state->next_zero = 0;
|
|
|
|
state->next_zero = 0;
|
|
|
|
state->data_pos = 0;
|
|
|
|
state->data_pos = 0;
|
|
|
|
|
|
|
|
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) {
|
|
|
|
state->next_zero = data;
|
|
|
|
state->next_zero = data;
|
|
|
|
|
|
|
|
state->long_frame = data == 0xFF;
|
|
|
|
state->data_pos = 0;
|
|
|
|
state->data_pos = 0;
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -56,16 +65,21 @@ void recv_byte(byte_stuffer_state_t* state, uint8_t data) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
// The frame is invalid, so reset
|
|
|
|
// The frame is invalid, so reset
|
|
|
|
state->next_zero = 0;
|
|
|
|
init_byte_stuffer_state(state);
|
|
|
|
state->data_pos = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
if (state->next_zero == 0) {
|
|
|
|
if (state->next_zero == 0) {
|
|
|
|
|
|
|
|
if (state->long_frame) {
|
|
|
|
|
|
|
|
state->next_zero = data;
|
|
|
|
|
|
|
|
state->long_frame = data == 0xFF;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
// Special case for zeroes
|
|
|
|
// Special case for zeroes
|
|
|
|
state->next_zero = data;
|
|
|
|
state->next_zero = data;
|
|
|
|
state->data[state->data_pos++] = 0;
|
|
|
|
state->data[state->data_pos++] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
state->data[state->data_pos++] = data;
|
|
|
|
state->data[state->data_pos++] = data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|