diff options
| author | skullY <skullydazed@gmail.com> | 2019-08-30 11:19:03 -0700 |
|---|---|---|
| committer | skullydazed <skullydazed@users.noreply.github.com> | 2019-08-30 15:01:52 -0700 |
| commit | b624f32f944acdc59dcb130674c09090c5c404cb (patch) | |
| tree | bc13adbba137d122d9a2c2fb2fafcbb08ac10e25 /quantum/serial_link/protocol/byte_stuffer.c | |
| parent | 61af76a10d00aba185b8338604171de490a13e3b (diff) | |
| download | qmk_firmware-b624f32f944acdc59dcb130674c09090c5c404cb.tar.gz qmk_firmware-b624f32f944acdc59dcb130674c09090c5c404cb.zip | |
clang-format changes
Diffstat (limited to 'quantum/serial_link/protocol/byte_stuffer.c')
| -rw-r--r-- | quantum/serial_link/protocol/byte_stuffer.c | 55 |
1 files changed, 24 insertions, 31 deletions
diff --git a/quantum/serial_link/protocol/byte_stuffer.c b/quantum/serial_link/protocol/byte_stuffer.c index 2c87d64c2..d3a91d828 100644 --- a/quantum/serial_link/protocol/byte_stuffer.c +++ b/quantum/serial_link/protocol/byte_stuffer.c | |||
| @@ -34,21 +34,21 @@ SOFTWARE. | |||
| 34 | typedef struct byte_stuffer_state { | 34 | typedef struct byte_stuffer_state { |
| 35 | uint16_t next_zero; | 35 | uint16_t next_zero; |
| 36 | uint16_t data_pos; | 36 | uint16_t data_pos; |
| 37 | bool long_frame; | 37 | bool long_frame; |
| 38 | uint8_t data[MAX_FRAME_SIZE]; | 38 | uint8_t data[MAX_FRAME_SIZE]; |
| 39 | }byte_stuffer_state_t; | 39 | } byte_stuffer_state_t; |
| 40 | 40 | ||
| 41 | static byte_stuffer_state_t states[NUM_LINKS]; | 41 | static byte_stuffer_state_t states[NUM_LINKS]; |
| 42 | 42 | ||
| 43 | void init_byte_stuffer_state(byte_stuffer_state_t* state) { | 43 | void init_byte_stuffer_state(byte_stuffer_state_t* state) { |
| 44 | state->next_zero = 0; | 44 | state->next_zero = 0; |
| 45 | state->data_pos = 0; | 45 | state->data_pos = 0; |
| 46 | state->long_frame = false; | 46 | state->long_frame = false; |
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | void init_byte_stuffer(void) { | 49 | void init_byte_stuffer(void) { |
| 50 | int i; | 50 | int i; |
| 51 | for (i=0;i<NUM_LINKS;i++) { | 51 | for (i = 0; i < NUM_LINKS; i++) { |
| 52 | init_byte_stuffer_state(&states[i]); | 52 | init_byte_stuffer_state(&states[i]); |
| 53 | } | 53 | } |
| 54 | } | 54 | } |
| @@ -57,9 +57,9 @@ void byte_stuffer_recv_byte(uint8_t link, uint8_t data) { | |||
| 57 | byte_stuffer_state_t* state = &states[link]; | 57 | byte_stuffer_state_t* state = &states[link]; |
| 58 | // Start of a new frame | 58 | // Start of a new frame |
| 59 | if (state->next_zero == 0) { | 59 | if (state->next_zero == 0) { |
| 60 | state->next_zero = data; | 60 | state->next_zero = data; |
| 61 | state->long_frame = data == 0xFF; | 61 | state->long_frame = data == 0xFF; |
| 62 | state->data_pos = 0; | 62 | state->data_pos = 0; |
| 63 | return; | 63 | return; |
| 64 | } | 64 | } |
| 65 | 65 | ||
| @@ -70,33 +70,28 @@ void byte_stuffer_recv_byte(uint8_t link, uint8_t data) { | |||
| 70 | if (state->data_pos > 0) { | 70 | if (state->data_pos > 0) { |
| 71 | validator_recv_frame(link, state->data, state->data_pos); | 71 | validator_recv_frame(link, state->data, state->data_pos); |
| 72 | } | 72 | } |
| 73 | } | 73 | } else { |
| 74 | else { | ||
| 75 | // The frame is invalid, so reset | 74 | // The frame is invalid, so reset |
| 76 | init_byte_stuffer_state(state); | 75 | init_byte_stuffer_state(state); |
| 77 | } | 76 | } |
| 78 | } | 77 | } else { |
| 79 | else { | ||
| 80 | if (state->data_pos == MAX_FRAME_SIZE) { | 78 | if (state->data_pos == MAX_FRAME_SIZE) { |
| 81 | // We exceeded our maximum frame size | 79 | // We exceeded our maximum frame size |
| 82 | // therefore there's nothing else to do than reset to a new frame | 80 | // therefore there's nothing else to do than reset to a new frame |
| 83 | state->next_zero = data; | 81 | state->next_zero = data; |
| 84 | state->long_frame = data == 0xFF; | 82 | state->long_frame = data == 0xFF; |
| 85 | state->data_pos = 0; | 83 | state->data_pos = 0; |
| 86 | } | 84 | } else if (state->next_zero == 0) { |
| 87 | else if (state->next_zero == 0) { | ||
| 88 | if (state->long_frame) { | 85 | if (state->long_frame) { |
| 89 | // This is part of a long frame, so continue | 86 | // This is part of a long frame, so continue |
| 90 | state->next_zero = data; | 87 | state->next_zero = data; |
| 91 | state->long_frame = data == 0xFF; | 88 | state->long_frame = data == 0xFF; |
| 92 | } | 89 | } else { |
| 93 | else { | ||
| 94 | // Special case for zeroes | 90 | // Special case for zeroes |
| 95 | state->next_zero = data; | 91 | state->next_zero = data; |
| 96 | state->data[state->data_pos++] = 0; | 92 | state->data[state->data_pos++] = 0; |
| 97 | } | 93 | } |
| 98 | } | 94 | } else { |
| 99 | else { | ||
| 100 | state->data[state->data_pos++] = data; | 95 | state->data[state->data_pos++] = data; |
| 101 | } | 96 | } |
| 102 | } | 97 | } |
| @@ -105,7 +100,7 @@ void byte_stuffer_recv_byte(uint8_t link, uint8_t data) { | |||
| 105 | static void send_block(uint8_t link, uint8_t* start, uint8_t* end, uint8_t num_non_zero) { | 100 | 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); | 101 | send_data(link, &num_non_zero, 1); |
| 107 | if (end > start) { | 102 | if (end > start) { |
| 108 | send_data(link, start, end-start); | 103 | send_data(link, start, end - start); |
| 109 | } | 104 | } |
| 110 | } | 105 | } |
| 111 | 106 | ||
| @@ -113,24 +108,22 @@ void byte_stuffer_send_frame(uint8_t link, uint8_t* data, uint16_t size) { | |||
| 113 | const uint8_t zero = 0; | 108 | const uint8_t zero = 0; |
| 114 | if (size > 0) { | 109 | if (size > 0) { |
| 115 | uint16_t num_non_zero = 1; | 110 | uint16_t num_non_zero = 1; |
| 116 | uint8_t* end = data + size; | 111 | uint8_t* end = data + size; |
| 117 | uint8_t* start = data; | 112 | uint8_t* start = data; |
| 118 | while (data < end) { | 113 | while (data < end) { |
| 119 | if (num_non_zero == 0xFF) { | 114 | if (num_non_zero == 0xFF) { |
| 120 | // There's more data after big non-zero block | 115 | // There's more data after big non-zero block |
| 121 | // So send it, and start a new block | 116 | // So send it, and start a new block |
| 122 | send_block(link, start, data, num_non_zero); | 117 | send_block(link, start, data, num_non_zero); |
| 123 | start = data; | 118 | start = data; |
| 124 | num_non_zero = 1; | 119 | num_non_zero = 1; |
| 125 | } | 120 | } else { |
| 126 | else { | ||
| 127 | if (*data == 0) { | 121 | if (*data == 0) { |
| 128 | // A zero encountered, so send the block | 122 | // A zero encountered, so send the block |
| 129 | send_block(link, start, data, num_non_zero); | 123 | send_block(link, start, data, num_non_zero); |
| 130 | start = data + 1; | 124 | start = data + 1; |
| 131 | num_non_zero = 1; | 125 | num_non_zero = 1; |
| 132 | } | 126 | } else { |
| 133 | else { | ||
| 134 | num_non_zero++; | 127 | num_non_zero++; |
| 135 | } | 128 | } |
| 136 | ++data; | 129 | ++data; |
