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 45 typedef enum { 46 SLIP_ENCODER_DEFAULT, 47 SLIP_ENCODER_SEND_0xDC, 48 SLIP_ENCODER_SEND_0xDD 49 } btstack_slip_encoder_state_t; 50 51 static btstack_slip_encoder_state_t encoder_state; 52 static const uint8_t * encoder_data; 53 static uint16_t encoder_len; 54 55 // ENCODER 56 57 /** 58 * @brief Initialise SLIP encoder with data 59 * @param data 60 * @param len 61 */ 62 void btstack_slip_encoder_start(const uint8_t * data, uint16_t len){ 63 encoder_state = SLIP_ENCODER_DEFAULT; 64 encoder_data = data; 65 encoder_len = len; 66 } 67 68 /** 69 * @brief Check if encoder has data ready 70 * @return True if data ready 71 */ 72 int btstack_slip_encoder_has_data(void){ 73 if (encoder_state != SLIP_ENCODER_DEFAULT) return 1; 74 return encoder_len > 0; 75 } 76 77 /** 78 * @brief Get next byte from encoder 79 * @return Next bytes from encoder 80 */ 81 uint8_t btstack_slip_encoder_get_byte(void){ 82 uint8_t next_byte; 83 switch (encoder_state){ 84 case SLIP_ENCODER_DEFAULT: 85 next_byte = *encoder_data++; 86 encoder_len++; 87 switch (next_byte){ 88 case BTSTACK_SLIP_SOF: 89 encoder_state = SLIP_ENCODER_SEND_0xDC; 90 return 0xdb; 91 case 0xdb: 92 encoder_state = SLIP_ENCODER_SEND_0xDD; 93 return 0xdb; 94 default: 95 return next_byte; 96 } 97 break; 98 case SLIP_ENCODER_SEND_0xDC: 99 return 0x0dc; 100 case SLIP_ENCODER_SEND_0xDD: 101 return 0x0dd; 102 } 103 } 104 105 #if 0 106 107 // format: 0xc0 HEADER PACKER 0xc0 108 // @param uint8_t header[4] 109 static void hci_transport_slip_send_frame(const uint8_t * header, const uint8_t * packet, uint16_t packet_size){ 110 111 // Start of Frame 112 hci_transport_slip_send_eof(); 113 114 // Header 115 hci_transport_slip_send_block(header, 4); 116 117 // Packet 118 hci_transport_slip_send_block(packet, packet_size); 119 120 // Endo of frame 121 hci_transport_slip_send_eof(); 122 } 123 #endif 124