xref: /btstack/src/btstack_slip.c (revision 1ba1a8829ab2f3eafb4cdaf67f9b0730403f9e91)
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         default:
124             log_error("btstack_slip_encoder_get_byte invalid state %x", encoder_state);
125             return 0x00;
126 	}
127 }
128 
129 // Decoder
130 
131 static void btstack_slip_decoder_reset(void){
132 	decoder_state = SLIP_DECODER_UNKNOWN;
133 	decoder_pos = 0;
134 }
135 
136 static void btstack_slip_decoder_store_byte(uint8_t input){
137 	if (decoder_pos >= decoder_max_size){
138 	    log_error("btstack_slip_decoder_store_byte: packet to long");
139 	    btstack_slip_decoder_reset();
140 	}
141 	decoder_buffer[decoder_pos++] = input;
142 }
143 
144 /**
145  * @brief Initialise SLIP decoder with buffer
146  * @param buffer to store received data
147  * @param max_size of buffer
148  */
149 void btstack_slip_decoder_init(uint8_t * buffer, uint16_t max_size){
150 	decoder_buffer = buffer;
151 	decoder_max_size = max_size;
152 	btstack_slip_decoder_reset();
153 }
154 
155 /**
156  * @brief Process received byte
157  * @param data
158  */
159 
160 void btstack_slip_decoder_process(uint8_t input){
161 	switch(decoder_state){
162         case SLIP_DECODER_UNKNOWN:
163             if (input != BTSTACK_SLIP_SOF) break;
164             btstack_slip_decoder_reset();
165             decoder_state = SLIP_DECODER_X_C0;
166             break;
167         case SLIP_DECODER_COMPLETE:
168         	log_error("btstack_slip_decoder_process called in state COMPLETE");
169             btstack_slip_decoder_reset();
170         	break;
171         case SLIP_DECODER_X_C0:
172             switch(input){
173                 case BTSTACK_SLIP_SOF:
174                     break;
175                 case 0xdb:
176                     decoder_state = SLIP_DECODER_X_DB;
177                     break;
178                 default:
179                     btstack_slip_decoder_store_byte(input);
180                     decoder_state = SLIP_DECODER_ACTIVE;
181                     break;
182             }
183             break;
184         case SLIP_DECODER_X_DB:
185             switch(input){
186                 case 0xdc:
187                     btstack_slip_decoder_store_byte(BTSTACK_SLIP_SOF);
188                     decoder_state = SLIP_DECODER_ACTIVE;
189                     break;
190                 case 0xdd:
191                     btstack_slip_decoder_store_byte(0xdb);
192                     decoder_state = SLIP_DECODER_ACTIVE;
193                     break;
194                 default:
195                     btstack_slip_decoder_reset();
196                     break;
197             }
198             break;
199         case SLIP_DECODER_ACTIVE:
200             switch(input){
201                 case BTSTACK_SLIP_SOF:
202                     if (decoder_pos){
203                     	decoder_state = SLIP_DECODER_COMPLETE;
204                     } else {
205 	                    btstack_slip_decoder_reset();
206                     }
207                     break;
208                 case 0xdb:
209                     decoder_state = SLIP_DECODER_X_DB;
210                     break;
211                 default:
212                     btstack_slip_decoder_store_byte(input);
213                     break;
214             }
215             break;
216     }
217 }
218 
219 /**
220  * @brief Get size of decoded frame
221  * @return size of frame. Size = 0 => frame not complete
222  */
223 
224 uint16_t btstack_slip_decoder_frame_size(void){
225 	switch (decoder_state){
226 		case SLIP_DECODER_COMPLETE:
227 			return decoder_pos;
228 		default:
229 			return 0;
230 	}
231 }
232