1 /* 2 * Copyright (C) 2014 BlueKitchen GmbH 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the copyright holders nor the names of 14 * contributors may be used to endorse or promote products derived 15 * from this software without specific prior written permission. 16 * 4. Any redistribution, use, or modification is done solely for 17 * personal benefit and not for any commercial purpose or for 18 * monetary gain. 19 * 20 * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24 * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * Please inquire about commercial licensing options at 34 * [email protected] 35 * 36 */ 37 38 /* 39 * btstack_slip.c 40 * SLIP encoder/decoder 41 */ 42 43 #include "btstack_slip.h" 44 #include "btstack_debug.h" 45 46 typedef enum { 47 SLIP_ENCODER_DEFAULT, 48 SLIP_ENCODER_SEND_DC, 49 SLIP_ENCODER_SEND_DD 50 } btstack_slip_encoder_state_t; 51 52 // h5 slip state machine 53 typedef enum { 54 SLIP_DECODER_UNKNOWN = 1, 55 SLIP_DECODER_ACTIVE, 56 SLIP_DECODER_X_C0, 57 SLIP_DECODER_X_DB, 58 SLIP_DECODER_COMPLETE 59 } btstack_slip_decoder_state_t; 60 61 62 // encoder 63 static btstack_slip_encoder_state_t encoder_state; 64 static const uint8_t * encoder_data; 65 static uint16_t encoder_len; 66 67 // decoder 68 static btstack_slip_decoder_state_t decoder_state; 69 static uint8_t * decoder_buffer; 70 static uint16_t decoder_max_size; 71 static uint16_t decoder_pos; 72 73 74 // ENCODER 75 76 /** 77 * @brief Initialise SLIP encoder with data 78 * @param data 79 * @param len 80 */ 81 void btstack_slip_encoder_start(const uint8_t * data, uint16_t len){ 82 encoder_state = SLIP_ENCODER_DEFAULT; 83 encoder_data = data; 84 encoder_len = len; 85 } 86 87 /** 88 * @brief Check if encoder has data ready 89 * @return True if data ready 90 */ 91 int btstack_slip_encoder_has_data(void){ 92 if (encoder_state != SLIP_ENCODER_DEFAULT) return 1; 93 return encoder_len > 0; 94 } 95 96 /** 97 * @brief Get next byte from encoder 98 * @return Next bytes from encoder 99 */ 100 uint8_t btstack_slip_encoder_get_byte(void){ 101 uint8_t next_byte; 102 switch (encoder_state){ 103 case SLIP_ENCODER_DEFAULT: 104 next_byte = *encoder_data++; 105 encoder_len--; 106 switch (next_byte){ 107 case BTSTACK_SLIP_SOF: 108 encoder_state = SLIP_ENCODER_SEND_DC; 109 return 0xdb; 110 case 0xdb: 111 encoder_state = SLIP_ENCODER_SEND_DD; 112 return 0xdb; 113 default: 114 return next_byte; 115 } 116 break; 117 case SLIP_ENCODER_SEND_DC: 118 encoder_state = SLIP_ENCODER_DEFAULT; 119 return 0x0dc; 120 case SLIP_ENCODER_SEND_DD: 121 encoder_state = SLIP_ENCODER_DEFAULT; 122 return 0x0dd; 123 } 124 } 125 126 // Decoder 127 128 static void btstack_slip_decoder_reset(void){ 129 decoder_state = SLIP_DECODER_UNKNOWN; 130 decoder_pos = 0; 131 } 132 133 static void btstack_slip_decoder_store_byte(uint8_t input){ 134 if (decoder_pos >= decoder_max_size){ 135 log_error("btstack_slip_decoder_store_byte: packet to long"); 136 btstack_slip_decoder_reset(); 137 } 138 decoder_buffer[decoder_pos++] = input; 139 } 140 141 /** 142 * @brief Initialise SLIP decoder with buffer 143 * @param buffer to store received data 144 * @param max_size of buffer 145 */ 146 void btstack_slip_decoder_init(uint8_t * buffer, uint16_t max_size){ 147 decoder_buffer = buffer; 148 decoder_max_size = max_size; 149 btstack_slip_decoder_reset(); 150 } 151 152 /** 153 * @brief Process received byte 154 * @param data 155 */ 156 157 void btstack_slip_decoder_process(uint8_t input){ 158 switch(decoder_state){ 159 case SLIP_DECODER_UNKNOWN: 160 if (input != BTSTACK_SLIP_SOF) break; 161 btstack_slip_decoder_reset(); 162 decoder_state = SLIP_DECODER_X_C0; 163 break; 164 case SLIP_DECODER_COMPLETE: 165 log_error("btstack_slip_decoder_process called in state COMPLETE"); 166 btstack_slip_decoder_reset(); 167 break; 168 case SLIP_DECODER_X_C0: 169 switch(input){ 170 case BTSTACK_SLIP_SOF: 171 break; 172 case 0xdb: 173 decoder_state = SLIP_DECODER_X_DB; 174 break; 175 default: 176 btstack_slip_decoder_store_byte(input); 177 decoder_state = SLIP_DECODER_ACTIVE; 178 break; 179 } 180 break; 181 case SLIP_DECODER_X_DB: 182 switch(input){ 183 case 0xdc: 184 btstack_slip_decoder_store_byte(BTSTACK_SLIP_SOF); 185 decoder_state = SLIP_DECODER_ACTIVE; 186 break; 187 case 0xdd: 188 btstack_slip_decoder_store_byte(0xdb); 189 decoder_state = SLIP_DECODER_ACTIVE; 190 break; 191 default: 192 btstack_slip_decoder_reset(); 193 break; 194 } 195 break; 196 case SLIP_DECODER_ACTIVE: 197 switch(input){ 198 case BTSTACK_SLIP_SOF: 199 if (decoder_pos){ 200 decoder_state = SLIP_DECODER_COMPLETE; 201 } else { 202 btstack_slip_decoder_reset(); 203 } 204 break; 205 case 0xdb: 206 decoder_state = SLIP_DECODER_X_DB; 207 break; 208 default: 209 btstack_slip_decoder_store_byte(input); 210 break; 211 } 212 break; 213 } 214 } 215 216 /** 217 * @brief Get size of decoded frame 218 * @return size of frame. Size = 0 => frame not complete 219 */ 220 221 uint16_t btstack_slip_decoder_frame_size(void){ 222 switch (decoder_state){ 223 case SLIP_DECODER_COMPLETE: 224 return decoder_pos; 225 default: 226 return 0; 227 } 228 } 229