xref: /btstack/src/btstack_slip.c (revision a8d51f092f1b660d0f6921369ad2bc3f9368296c)
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 #define BTSTACK_FILE__ "btstack_slip.c"
39 
40 /*
41  *  btstack_slip.c
42  *  SLIP encoder/decoder
43  */
44 
45 #include "btstack_slip.h"
46 #include "btstack_debug.h"
47 
48 typedef enum {
49 	SLIP_ENCODER_DEFAULT,
50 	SLIP_ENCODER_SEND_DC,
51 	SLIP_ENCODER_SEND_DD
52 } btstack_slip_encoder_state_t;
53 
54 // h5 slip state machine
55 typedef enum {
56     SLIP_DECODER_UNKNOWN = 1,
57     SLIP_DECODER_ACTIVE,
58     SLIP_DECODER_X_C0,
59     SLIP_DECODER_X_DB,
60     SLIP_DECODER_COMPLETE
61 } btstack_slip_decoder_state_t;
62 
63 
64 // encoder
65 static btstack_slip_encoder_state_t encoder_state;
66 static const uint8_t * encoder_data;
67 static uint16_t  encoder_len;
68 
69 // decoder
70 static btstack_slip_decoder_state_t decoder_state;
71 static uint8_t * decoder_buffer;
72 static uint16_t  decoder_max_size;
73 static uint16_t  decoder_pos;
74 
75 
76 // ENCODER
77 
78 /**
79  * @brief Initialise SLIP encoder with data
80  * @param data
81  * @param len
82  */
83 void btstack_slip_encoder_start(const uint8_t * data, uint16_t len){
84 	encoder_state = SLIP_ENCODER_DEFAULT;
85 	encoder_data  = data;
86 	encoder_len   = len;
87 }
88 
89 /**
90  * @brief Check if encoder has data ready
91  * @return True if data ready
92  */
93 int  btstack_slip_encoder_has_data(void){
94 	if (encoder_state != SLIP_ENCODER_DEFAULT) return 1;
95 	return encoder_len > 0u;
96 }
97 
98 /**
99  * @brief Get next byte from encoder
100  * @return Next bytes from encoder
101  */
102 uint8_t btstack_slip_encoder_get_byte(void){
103 	uint8_t next_byte;
104 	switch (encoder_state){
105 		case SLIP_ENCODER_DEFAULT:
106 			next_byte = *encoder_data++;
107 			encoder_len--;
108 			switch (next_byte){
109 				case BTSTACK_SLIP_SOF:
110 					encoder_state = SLIP_ENCODER_SEND_DC;
111 					return 0xdb;
112 				case 0xdb:
113 					encoder_state = SLIP_ENCODER_SEND_DD;
114 					return 0xdb;
115 				default:
116                     break;
117 			}
118 			return next_byte;
119 		case SLIP_ENCODER_SEND_DC:
120 			encoder_state = SLIP_ENCODER_DEFAULT;
121 			return 0x0dc;
122 		case SLIP_ENCODER_SEND_DD:
123 			encoder_state = SLIP_ENCODER_DEFAULT;
124 			return 0x0dd;
125         default:
126             log_error("btstack_slip_encoder_get_byte invalid state %x", encoder_state);
127             return 0x00;
128 	}
129 }
130 
131 // Decoder
132 
133 static void btstack_slip_decoder_reset(void){
134 	decoder_state = SLIP_DECODER_UNKNOWN;
135 	decoder_pos = 0;
136 }
137 
138 static void btstack_slip_decoder_store_byte(uint8_t input){
139 	if (decoder_pos >= decoder_max_size){
140 	    log_error("btstack_slip_decoder_store_byte: packet to long");
141 	    btstack_slip_decoder_reset();
142 	}
143 	decoder_buffer[decoder_pos++] = input;
144 }
145 
146 /**
147  * @brief Initialise SLIP decoder with buffer
148  * @param buffer to store received data
149  * @param max_size of buffer
150  */
151 void btstack_slip_decoder_init(uint8_t * buffer, uint16_t max_size){
152 	decoder_buffer = buffer;
153 	decoder_max_size = max_size;
154 	btstack_slip_decoder_reset();
155 }
156 
157 /**
158  * @brief Process received byte
159  * @param data
160  */
161 
162 void btstack_slip_decoder_process(uint8_t input){
163 	switch(decoder_state){
164         case SLIP_DECODER_UNKNOWN:
165             if (input != BTSTACK_SLIP_SOF) break;
166             btstack_slip_decoder_reset();
167             decoder_state = SLIP_DECODER_X_C0;
168             break;
169         case SLIP_DECODER_COMPLETE:
170         	log_error("btstack_slip_decoder_process called in state COMPLETE");
171             btstack_slip_decoder_reset();
172         	break;
173         case SLIP_DECODER_X_C0:
174             switch(input){
175                 case BTSTACK_SLIP_SOF:
176                     break;
177                 case 0xdb:
178                     decoder_state = SLIP_DECODER_X_DB;
179                     break;
180                 default:
181                     btstack_slip_decoder_store_byte(input);
182                     decoder_state = SLIP_DECODER_ACTIVE;
183                     break;
184             }
185             break;
186         case SLIP_DECODER_X_DB:
187             switch(input){
188                 case 0xdc:
189                     btstack_slip_decoder_store_byte(BTSTACK_SLIP_SOF);
190                     decoder_state = SLIP_DECODER_ACTIVE;
191                     break;
192                 case 0xdd:
193                     btstack_slip_decoder_store_byte(0xdb);
194                     decoder_state = SLIP_DECODER_ACTIVE;
195                     break;
196                 default:
197                     btstack_slip_decoder_reset();
198                     break;
199             }
200             break;
201         case SLIP_DECODER_ACTIVE:
202             switch(input){
203                 case BTSTACK_SLIP_SOF:
204                     if (decoder_pos){
205                     	decoder_state = SLIP_DECODER_COMPLETE;
206                     } else {
207 	                    btstack_slip_decoder_reset();
208                     }
209                     break;
210                 case 0xdb:
211                     decoder_state = SLIP_DECODER_X_DB;
212                     break;
213                 default:
214                     btstack_slip_decoder_store_byte(input);
215                     break;
216             }
217             break;
218 		default:
219 			btstack_assert(false);
220 			break;
221     }
222 }
223 
224 /**
225  * @brief Get size of decoded frame
226  * @return size of frame. Size = 0 => frame not complete
227  */
228 
229 uint16_t btstack_slip_decoder_frame_size(void){
230 	switch (decoder_state){
231 		case SLIP_DECODER_COMPLETE:
232 			return decoder_pos;
233 		default:
234 			return 0;
235 	}
236 }
237