Add sending of small frames with no zeroes
[jackhill/qmk/firmware.git] / serial_link / protocol / byte_stuffer.c
1 /*
2 The MIT License (MIT)
3
4 Copyright (c) 2016 Fred Sundvik
5
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24
25 #include "protocol/byte_stuffer.h"
26 #include "protocol/frame_validator.h"
27 #include "protocol/physical.h"
28
29 // This implements the "Consistent overhead byte stuffing protocol"
30 // https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing
31 // http://www.stuartcheshire.org/papers/COBSforToN.pdf
32
33 #define MAX_FRAME_SIZE 1024
34
35 typedef struct byte_stuffer_state {
36 uint16_t next_zero;
37 uint16_t data_pos;
38 bool long_frame;
39 uint8_t data[MAX_FRAME_SIZE];
40 }byte_stuffer_state_t;
41
42 void init_byte_stuffer_state(byte_stuffer_state_t* state) {
43 state->next_zero = 0;
44 state->data_pos = 0;
45 state->long_frame = false;
46 }
47
48 void recv_byte(byte_stuffer_state_t* state, uint8_t data) {
49 // Start of a new frame
50 if (state->next_zero == 0) {
51 state->next_zero = data;
52 state->long_frame = data == 0xFF;
53 state->data_pos = 0;
54 return;
55 }
56
57 state->next_zero--;
58 if (data == 0) {
59 if (state->next_zero == 0) {
60 // The frame is completed
61 if (state->data_pos > 0) {
62 recv_frame(state->data, state->data_pos);
63 }
64 }
65 else {
66 // The frame is invalid, so reset
67 init_byte_stuffer_state(state);
68 }
69 }
70 else {
71 if (state->data_pos == MAX_FRAME_SIZE) {
72 // We exceeded our maximum frame size
73 // therefore there's nothing else to do than reset to a new frame
74 state->next_zero = data;
75 state->long_frame = data == 0xFF;
76 state->data_pos = 0;
77 }
78 else if (state->next_zero == 0) {
79 if (state->long_frame) {
80 // This is part of a long frame, so continue
81 state->next_zero = data;
82 state->long_frame = data == 0xFF;
83 }
84 else {
85 // Special case for zeroes
86 state->next_zero = data;
87 state->data[state->data_pos++] = 0;
88 }
89 }
90 else {
91 state->data[state->data_pos++] = data;
92 }
93 }
94 }
95
96 void send_frame(uint8_t* data, uint16_t size) {
97 if (size > 0) {
98 uint8_t numZeroes = size + 1;
99 const uint8_t zero = 0;
100 send_data(&numZeroes, 1);
101 send_data(data, size);
102 send_data(&zero, 1);
103 }
104 }