xref: /btstack/src/btstack_slip.c (revision ab67bfbb5af443d3b854a8f4f0d060783f448aff)
1*ab67bfbbSMatthias Ringwald /*
2*ab67bfbbSMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
3*ab67bfbbSMatthias Ringwald  *
4*ab67bfbbSMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
5*ab67bfbbSMatthias Ringwald  * modification, are permitted provided that the following conditions
6*ab67bfbbSMatthias Ringwald  * are met:
7*ab67bfbbSMatthias Ringwald  *
8*ab67bfbbSMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
9*ab67bfbbSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
10*ab67bfbbSMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
11*ab67bfbbSMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
12*ab67bfbbSMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
13*ab67bfbbSMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
14*ab67bfbbSMatthias Ringwald  *    contributors may be used to endorse or promote products derived
15*ab67bfbbSMatthias Ringwald  *    from this software without specific prior written permission.
16*ab67bfbbSMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
17*ab67bfbbSMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
18*ab67bfbbSMatthias Ringwald  *    monetary gain.
19*ab67bfbbSMatthias Ringwald  *
20*ab67bfbbSMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21*ab67bfbbSMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*ab67bfbbSMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*ab67bfbbSMatthias Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24*ab67bfbbSMatthias Ringwald  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*ab67bfbbSMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*ab67bfbbSMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27*ab67bfbbSMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*ab67bfbbSMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*ab67bfbbSMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30*ab67bfbbSMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*ab67bfbbSMatthias Ringwald  * SUCH DAMAGE.
32*ab67bfbbSMatthias Ringwald  *
33*ab67bfbbSMatthias Ringwald  * Please inquire about commercial licensing options at
34*ab67bfbbSMatthias Ringwald  * [email protected]
35*ab67bfbbSMatthias Ringwald  *
36*ab67bfbbSMatthias Ringwald  */
37*ab67bfbbSMatthias Ringwald 
38*ab67bfbbSMatthias Ringwald /*
39*ab67bfbbSMatthias Ringwald  *  btstack_slip.c
40*ab67bfbbSMatthias Ringwald  *  SLIP encoder/decoder
41*ab67bfbbSMatthias Ringwald  */
42*ab67bfbbSMatthias Ringwald 
43*ab67bfbbSMatthias Ringwald #include "btstack_slip.h"
44*ab67bfbbSMatthias Ringwald 
45*ab67bfbbSMatthias Ringwald typedef enum {
46*ab67bfbbSMatthias Ringwald 	SLIP_ENCODER_DEFAULT,
47*ab67bfbbSMatthias Ringwald 	SLIP_ENCODER_SEND_0xDC,
48*ab67bfbbSMatthias Ringwald 	SLIP_ENCODER_SEND_0xDD
49*ab67bfbbSMatthias Ringwald } btstack_slip_encoder_state_t;
50*ab67bfbbSMatthias Ringwald 
51*ab67bfbbSMatthias Ringwald static btstack_slip_encoder_state_t encoder_state;
52*ab67bfbbSMatthias Ringwald static const uint8_t * encoder_data;
53*ab67bfbbSMatthias Ringwald static uint16_t  encoder_len;
54*ab67bfbbSMatthias Ringwald 
55*ab67bfbbSMatthias Ringwald // ENCODER
56*ab67bfbbSMatthias Ringwald 
57*ab67bfbbSMatthias Ringwald /**
58*ab67bfbbSMatthias Ringwald  * @brief Initialise SLIP encoder with data
59*ab67bfbbSMatthias Ringwald  * @param data
60*ab67bfbbSMatthias Ringwald  * @param len
61*ab67bfbbSMatthias Ringwald  */
62*ab67bfbbSMatthias Ringwald void btstack_slip_encoder_start(const uint8_t * data, uint16_t len){
63*ab67bfbbSMatthias Ringwald 	encoder_state = SLIP_ENCODER_DEFAULT;
64*ab67bfbbSMatthias Ringwald 	encoder_data  = data;
65*ab67bfbbSMatthias Ringwald 	encoder_len   = len;
66*ab67bfbbSMatthias Ringwald }
67*ab67bfbbSMatthias Ringwald 
68*ab67bfbbSMatthias Ringwald /**
69*ab67bfbbSMatthias Ringwald  * @brief Check if encoder has data ready
70*ab67bfbbSMatthias Ringwald  * @return True if data ready
71*ab67bfbbSMatthias Ringwald  */
72*ab67bfbbSMatthias Ringwald int  btstack_slip_encoder_has_data(void){
73*ab67bfbbSMatthias Ringwald 	if (encoder_state != SLIP_ENCODER_DEFAULT) return 1;
74*ab67bfbbSMatthias Ringwald 	return encoder_len > 0;
75*ab67bfbbSMatthias Ringwald }
76*ab67bfbbSMatthias Ringwald 
77*ab67bfbbSMatthias Ringwald /**
78*ab67bfbbSMatthias Ringwald  * @brief Get next byte from encoder
79*ab67bfbbSMatthias Ringwald  * @return Next bytes from encoder
80*ab67bfbbSMatthias Ringwald  */
81*ab67bfbbSMatthias Ringwald uint8_t btstack_slip_encoder_get_byte(void){
82*ab67bfbbSMatthias Ringwald 	uint8_t next_byte;
83*ab67bfbbSMatthias Ringwald 	switch (encoder_state){
84*ab67bfbbSMatthias Ringwald 		case SLIP_ENCODER_DEFAULT:
85*ab67bfbbSMatthias Ringwald 			next_byte = *encoder_data++;
86*ab67bfbbSMatthias Ringwald 			encoder_len++;
87*ab67bfbbSMatthias Ringwald 			switch (next_byte){
88*ab67bfbbSMatthias Ringwald 				case BTSTACK_SLIP_SOF:
89*ab67bfbbSMatthias Ringwald 					encoder_state = SLIP_ENCODER_SEND_0xDC;
90*ab67bfbbSMatthias Ringwald 					return 0xdb;
91*ab67bfbbSMatthias Ringwald 				case 0xdb:
92*ab67bfbbSMatthias Ringwald 					encoder_state = SLIP_ENCODER_SEND_0xDD;
93*ab67bfbbSMatthias Ringwald 					return 0xdb;
94*ab67bfbbSMatthias Ringwald 				default:
95*ab67bfbbSMatthias Ringwald 					return next_byte;
96*ab67bfbbSMatthias Ringwald 			}
97*ab67bfbbSMatthias Ringwald 			break;
98*ab67bfbbSMatthias Ringwald 		case SLIP_ENCODER_SEND_0xDC:
99*ab67bfbbSMatthias Ringwald 			return 0x0dc;
100*ab67bfbbSMatthias Ringwald 		case SLIP_ENCODER_SEND_0xDD:
101*ab67bfbbSMatthias Ringwald 			return 0x0dd;
102*ab67bfbbSMatthias Ringwald 	}
103*ab67bfbbSMatthias Ringwald }
104*ab67bfbbSMatthias Ringwald 
105*ab67bfbbSMatthias Ringwald #if 0
106*ab67bfbbSMatthias Ringwald 
107*ab67bfbbSMatthias Ringwald // format: 0xc0 HEADER PACKER 0xc0
108*ab67bfbbSMatthias Ringwald // @param uint8_t header[4]
109*ab67bfbbSMatthias Ringwald static void hci_transport_slip_send_frame(const uint8_t * header, const uint8_t * packet, uint16_t packet_size){
110*ab67bfbbSMatthias Ringwald 
111*ab67bfbbSMatthias Ringwald     // Start of Frame
112*ab67bfbbSMatthias Ringwald     hci_transport_slip_send_eof();
113*ab67bfbbSMatthias Ringwald 
114*ab67bfbbSMatthias Ringwald     // Header
115*ab67bfbbSMatthias Ringwald     hci_transport_slip_send_block(header, 4);
116*ab67bfbbSMatthias Ringwald 
117*ab67bfbbSMatthias Ringwald     // Packet
118*ab67bfbbSMatthias Ringwald     hci_transport_slip_send_block(packet, packet_size);
119*ab67bfbbSMatthias Ringwald 
120*ab67bfbbSMatthias Ringwald     // Endo of frame
121*ab67bfbbSMatthias Ringwald     hci_transport_slip_send_eof();
122*ab67bfbbSMatthias Ringwald }
123*ab67bfbbSMatthias Ringwald #endif
124