]> git.donarmstrong.com Git - qmk_firmware.git/blob - quantum/serial_link/protocol/byte_stuffer.c
Merge branch 'master' of https://github.com/jackhumbert/qmk_firmware
[qmk_firmware.git] / quantum / 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 "serial_link/protocol/byte_stuffer.h"
26 #include "serial_link/protocol/frame_validator.h"
27 #include "serial_link/protocol/physical.h"
28 #include <stdbool.h>
29
30 // This implements the "Consistent overhead byte stuffing protocol"
31 // https://en.wikipedia.org/wiki/Consistent_Overhead_Byte_Stuffing
32 // http://www.stuartcheshire.org/papers/COBSforToN.pdf
33
34 typedef struct byte_stuffer_state {
35     uint16_t next_zero;
36     uint16_t data_pos;
37     bool long_frame;
38     uint8_t data[MAX_FRAME_SIZE];
39 }byte_stuffer_state_t;
40
41 static byte_stuffer_state_t states[NUM_LINKS];
42
43 void init_byte_stuffer_state(byte_stuffer_state_t* state) {
44     state->next_zero = 0;
45     state->data_pos = 0;
46     state->long_frame = false;
47 }
48
49 void init_byte_stuffer(void) {
50     int i;
51     for (i=0;i<NUM_LINKS;i++) {
52         init_byte_stuffer_state(&states[i]);
53     }
54 }
55
56 void byte_stuffer_recv_byte(uint8_t link, uint8_t data) {
57     byte_stuffer_state_t* state = &states[link];
58     // Start of a new frame
59     if (state->next_zero == 0) {
60         state->next_zero = data;
61         state->long_frame = data == 0xFF;
62         state->data_pos = 0;
63         return;
64     }
65
66     state->next_zero--;
67     if (data == 0) {
68         if (state->next_zero == 0) {
69             // The frame is completed
70             if (state->data_pos > 0) {
71                 validator_recv_frame(link, state->data, state->data_pos);
72             }
73         }
74         else {
75             // The frame is invalid, so reset
76             init_byte_stuffer_state(state);
77         }
78     }
79     else {
80         if (state->data_pos == MAX_FRAME_SIZE) {
81             // We exceeded our maximum frame size
82             // therefore there's nothing else to do than reset to a new frame
83             state->next_zero = data;
84             state->long_frame = data == 0xFF;
85             state->data_pos = 0;
86         }
87         else if (state->next_zero == 0) {
88             if (state->long_frame) {
89                 // This is part of a long frame, so continue
90                 state->next_zero = data;
91                 state->long_frame = data == 0xFF;
92             }
93             else {
94                 // Special case for zeroes
95                 state->next_zero = data;
96                 state->data[state->data_pos++] = 0;
97             }
98         }
99         else {
100             state->data[state->data_pos++] = data;
101         }
102     }
103 }
104
105 static void send_block(uint8_t link, uint8_t* start, uint8_t* end, uint8_t num_non_zero) {
106     send_data(link, &num_non_zero, 1);
107     if (end > start) {
108         send_data(link, start, end-start);
109     }
110 }
111
112 void byte_stuffer_send_frame(uint8_t link, uint8_t* data, uint16_t size) {
113     const uint8_t zero = 0;
114     if (size > 0) {
115         uint16_t num_non_zero = 1;
116         uint8_t* end = data + size;
117         uint8_t* start = data;
118         while (data < end) {
119             if (num_non_zero == 0xFF) {
120                 // There's more data after big non-zero block
121                 // So send it, and start a new block
122                 send_block(link, start, data, num_non_zero);
123                 start = data;
124                 num_non_zero = 1;
125             }
126             else {
127                 if (*data == 0) {
128                     // A zero encountered, so send the block
129                     send_block(link, start, data, num_non_zero);
130                     start = data + 1;
131                     num_non_zero = 1;
132                 }
133                 else {
134                     num_non_zero++;
135                 }
136                 ++data;
137             }
138         }
139         send_block(link, start, data, num_non_zero);
140         send_data(link, &zero, 1);
141     }
142 }