1*dd90474bSMatthias Ringwald /* 2*dd90474bSMatthias Ringwald * Copyright (C) 2021 BlueKitchen GmbH 3*dd90474bSMatthias Ringwald * 4*dd90474bSMatthias Ringwald * Redistribution and use in source and binary forms, with or without 5*dd90474bSMatthias Ringwald * modification, are permitted provided that the following conditions 6*dd90474bSMatthias Ringwald * are met: 7*dd90474bSMatthias Ringwald * 8*dd90474bSMatthias Ringwald * 1. Redistributions of source code must retain the above copyright 9*dd90474bSMatthias Ringwald * notice, this list of conditions and the following disclaimer. 10*dd90474bSMatthias Ringwald * 2. Redistributions in binary form must reproduce the above copyright 11*dd90474bSMatthias Ringwald * notice, this list of conditions and the following disclaimer in the 12*dd90474bSMatthias Ringwald * documentation and/or other materials provided with the distribution. 13*dd90474bSMatthias Ringwald * 3. Neither the name of the copyright holders nor the names of 14*dd90474bSMatthias Ringwald * contributors may be used to endorse or promote products derived 15*dd90474bSMatthias Ringwald * from this software without specific prior written permission. 16*dd90474bSMatthias Ringwald * 4. Any redistribution, use, or modification is done solely for 17*dd90474bSMatthias Ringwald * personal benefit and not for any commercial purpose or for 18*dd90474bSMatthias Ringwald * monetary gain. 19*dd90474bSMatthias Ringwald * 20*dd90474bSMatthias Ringwald * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS 21*dd90474bSMatthias Ringwald * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22*dd90474bSMatthias Ringwald * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23*dd90474bSMatthias Ringwald * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS 24*dd90474bSMatthias Ringwald * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25*dd90474bSMatthias Ringwald * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 26*dd90474bSMatthias Ringwald * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 27*dd90474bSMatthias Ringwald * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28*dd90474bSMatthias Ringwald * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29*dd90474bSMatthias Ringwald * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 30*dd90474bSMatthias Ringwald * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31*dd90474bSMatthias Ringwald * SUCH DAMAGE. 32*dd90474bSMatthias Ringwald * 33*dd90474bSMatthias Ringwald * Please inquire about commercial licensing options at 34*dd90474bSMatthias Ringwald * [email protected] 35*dd90474bSMatthias Ringwald * 36*dd90474bSMatthias Ringwald */ 37*dd90474bSMatthias Ringwald 38*dd90474bSMatthias Ringwald #define BTSTACK_FILE__ "btstack_uart_slip_wrapper.c" 39*dd90474bSMatthias Ringwald 40*dd90474bSMatthias Ringwald /* 41*dd90474bSMatthias Ringwald * btstack_uart_slip_wrapper.c 42*dd90474bSMatthias Ringwald * 43*dd90474bSMatthias Ringwald * 44*dd90474bSMatthias Ringwald */ 45*dd90474bSMatthias Ringwald 46*dd90474bSMatthias Ringwald #include "btstack_uart_slip_wrapper.h" 47*dd90474bSMatthias Ringwald 48*dd90474bSMatthias Ringwald #include "btstack_run_loop.h" 49*dd90474bSMatthias Ringwald #include "btstack_debug.h" 50*dd90474bSMatthias Ringwald #include "btstack_defines.h" 51*dd90474bSMatthias Ringwald #include "btstack_slip.h" 52*dd90474bSMatthias Ringwald #include "btstack_util.h" 53*dd90474bSMatthias Ringwald 54*dd90474bSMatthias Ringwald // max size of outgoing SLIP chunks 55*dd90474bSMatthias Ringwald #define SLIP_TX_CHUNK_LEN 128 56*dd90474bSMatthias Ringwald 57*dd90474bSMatthias Ringwald #define SLIP_RECEIVE_BUFFER_SIZE 128 58*dd90474bSMatthias Ringwald 59*dd90474bSMatthias Ringwald // wrapped driver 60*dd90474bSMatthias Ringwald static const btstack_uart_t * original_uart; 61*dd90474bSMatthias Ringwald 62*dd90474bSMatthias Ringwald // callbacks 63*dd90474bSMatthias Ringwald static void (*frame_sent)(void); 64*dd90474bSMatthias Ringwald static void (*frame_received)(uint16_t frame_size); 65*dd90474bSMatthias Ringwald 66*dd90474bSMatthias Ringwald // SLIP encoding 67*dd90474bSMatthias Ringwald static uint8_t btstack_uart_slip_outgoing_buffer[SLIP_TX_CHUNK_LEN+1]; 68*dd90474bSMatthias Ringwald 69*dd90474bSMatthias Ringwald // SLIP decoding 70*dd90474bSMatthias Ringwald static uint8_t btstack_uart_slip_wrapper_read_byte; 71*dd90474bSMatthias Ringwald static uint32_t btstack_uart_slip_wrapper_receive_start; 72*dd90474bSMatthias Ringwald static uint32_t btstack_uart_slip_wrapper_baudrate; 73*dd90474bSMatthias Ringwald 74*dd90474bSMatthias Ringwald 75*dd90474bSMatthias Ringwald // SLIP ENCODING 76*dd90474bSMatthias Ringwald 77*dd90474bSMatthias Ringwald static void btstack_uart_slip_posix_encode_chunk_and_send(void){ 78*dd90474bSMatthias Ringwald uint16_t pos = 0; 79*dd90474bSMatthias Ringwald while (btstack_slip_encoder_has_data() & (pos < SLIP_TX_CHUNK_LEN)) { 80*dd90474bSMatthias Ringwald btstack_uart_slip_outgoing_buffer[pos++] = btstack_slip_encoder_get_byte(); 81*dd90474bSMatthias Ringwald } 82*dd90474bSMatthias Ringwald 83*dd90474bSMatthias Ringwald // setup async write and start sending 84*dd90474bSMatthias Ringwald original_uart->send_block(btstack_uart_slip_outgoing_buffer, pos); 85*dd90474bSMatthias Ringwald } 86*dd90474bSMatthias Ringwald 87*dd90474bSMatthias Ringwald static void btstack_uart_slip_wrapper_block_sent(void){ 88*dd90474bSMatthias Ringwald // check if more data to send 89*dd90474bSMatthias Ringwald if (btstack_slip_encoder_has_data()){ 90*dd90474bSMatthias Ringwald btstack_uart_slip_posix_encode_chunk_and_send(); 91*dd90474bSMatthias Ringwald return; 92*dd90474bSMatthias Ringwald } 93*dd90474bSMatthias Ringwald 94*dd90474bSMatthias Ringwald // notify done 95*dd90474bSMatthias Ringwald if (frame_sent){ 96*dd90474bSMatthias Ringwald frame_sent(); 97*dd90474bSMatthias Ringwald } 98*dd90474bSMatthias Ringwald } 99*dd90474bSMatthias Ringwald 100*dd90474bSMatthias Ringwald static void btstack_uart_slip_wrapper_send_frame(const uint8_t * frame, uint16_t frame_size){ 101*dd90474bSMatthias Ringwald 102*dd90474bSMatthias Ringwald // Prepare encoding of Header + Packet (+ DIC) 103*dd90474bSMatthias Ringwald btstack_slip_encoder_start(frame, frame_size); 104*dd90474bSMatthias Ringwald 105*dd90474bSMatthias Ringwald // Fill rest of chunk from packet and send 106*dd90474bSMatthias Ringwald btstack_uart_slip_posix_encode_chunk_and_send(); 107*dd90474bSMatthias Ringwald } 108*dd90474bSMatthias Ringwald 109*dd90474bSMatthias Ringwald static void btstack_uart_slip_wrapper_set_frame_sent( void (*frame_handler)(void)){ 110*dd90474bSMatthias Ringwald frame_sent = frame_handler; 111*dd90474bSMatthias Ringwald original_uart->set_block_sent(&btstack_uart_slip_wrapper_block_sent); 112*dd90474bSMatthias Ringwald } 113*dd90474bSMatthias Ringwald 114*dd90474bSMatthias Ringwald // SLIP DECODING 115*dd90474bSMatthias Ringwald 116*dd90474bSMatthias Ringwald static void btstack_uart_slip_wrapper_read_next_byte(void){ 117*dd90474bSMatthias Ringwald original_uart->receive_block(&btstack_uart_slip_wrapper_read_byte, 1); 118*dd90474bSMatthias Ringwald } 119*dd90474bSMatthias Ringwald 120*dd90474bSMatthias Ringwald // track time receiving SLIP frame 121*dd90474bSMatthias Ringwald static void btstack_uart_slip_wrapper_block_received(void){ 122*dd90474bSMatthias Ringwald 123*dd90474bSMatthias Ringwald // track start time when receiving first byte // a bit hackish 124*dd90474bSMatthias Ringwald if ((btstack_uart_slip_wrapper_receive_start == 0u) && (btstack_uart_slip_wrapper_read_byte != BTSTACK_SLIP_SOF)){ 125*dd90474bSMatthias Ringwald btstack_uart_slip_wrapper_receive_start = btstack_run_loop_get_time_ms(); 126*dd90474bSMatthias Ringwald } 127*dd90474bSMatthias Ringwald btstack_slip_decoder_process(btstack_uart_slip_wrapper_read_byte); 128*dd90474bSMatthias Ringwald uint16_t frame_size = btstack_slip_decoder_frame_size(); 129*dd90474bSMatthias Ringwald if (frame_size) { 130*dd90474bSMatthias Ringwald // track time 131*dd90474bSMatthias Ringwald uint32_t packet_receive_time = btstack_run_loop_get_time_ms() - btstack_uart_slip_wrapper_receive_start; 132*dd90474bSMatthias Ringwald uint32_t nominal_time = (frame_size + 6u) * 10u * 1000u / btstack_uart_slip_wrapper_baudrate; 133*dd90474bSMatthias Ringwald UNUSED(nominal_time); 134*dd90474bSMatthias Ringwald UNUSED(packet_receive_time); 135*dd90474bSMatthias Ringwald if (packet_receive_time > btstack_max(2 * nominal_time, 5)){ 136*dd90474bSMatthias Ringwald log_info("slip frame time %u ms for %u decoded bytes. nominal time %u ms", (int) packet_receive_time, frame_size, (int) nominal_time); 137*dd90474bSMatthias Ringwald } 138*dd90474bSMatthias Ringwald // reset state 139*dd90474bSMatthias Ringwald btstack_uart_slip_wrapper_receive_start = 0; 140*dd90474bSMatthias Ringwald // deliver frame 141*dd90474bSMatthias Ringwald frame_received(frame_size); 142*dd90474bSMatthias Ringwald } else { 143*dd90474bSMatthias Ringwald btstack_uart_slip_wrapper_read_next_byte(); 144*dd90474bSMatthias Ringwald } 145*dd90474bSMatthias Ringwald } 146*dd90474bSMatthias Ringwald 147*dd90474bSMatthias Ringwald static void btstack_uart_slip_wrapper_set_frame_received( void (*frame_handler)(uint16_t frame_size)){ 148*dd90474bSMatthias Ringwald frame_received = frame_handler; 149*dd90474bSMatthias Ringwald original_uart->set_block_received(&btstack_uart_slip_wrapper_block_received); 150*dd90474bSMatthias Ringwald } 151*dd90474bSMatthias Ringwald 152*dd90474bSMatthias Ringwald static void btstack_uart_slip_wrapper_receive_frame(uint8_t *buffer, uint16_t len){ 153*dd90474bSMatthias Ringwald btstack_slip_decoder_init(buffer, len); 154*dd90474bSMatthias Ringwald btstack_uart_slip_wrapper_read_next_byte(); 155*dd90474bSMatthias Ringwald } 156*dd90474bSMatthias Ringwald 157*dd90474bSMatthias Ringwald // SLIP End 158*dd90474bSMatthias Ringwald 159*dd90474bSMatthias Ringwald static int btstack_uart_slip_wrapper_init(const btstack_uart_config_t * config){ 160*dd90474bSMatthias Ringwald btstack_uart_slip_wrapper_baudrate = config->baudrate; 161*dd90474bSMatthias Ringwald return original_uart->init(config); 162*dd90474bSMatthias Ringwald } 163*dd90474bSMatthias Ringwald 164*dd90474bSMatthias Ringwald static int btstack_uart_slip_wrapper_set_baudrate(uint32_t baudrate){ 165*dd90474bSMatthias Ringwald if (original_uart->set_baudrate != NULL){ 166*dd90474bSMatthias Ringwald btstack_uart_slip_wrapper_baudrate = baudrate; 167*dd90474bSMatthias Ringwald return original_uart->set_baudrate(baudrate); 168*dd90474bSMatthias Ringwald } 169*dd90474bSMatthias Ringwald return 0; 170*dd90474bSMatthias Ringwald } 171*dd90474bSMatthias Ringwald 172*dd90474bSMatthias Ringwald static int btstack_uart_slip_wrapper_set_parity(int parity){ 173*dd90474bSMatthias Ringwald if (original_uart->set_baudrate != NULL) { 174*dd90474bSMatthias Ringwald return original_uart->set_parity(parity); 175*dd90474bSMatthias Ringwald } 176*dd90474bSMatthias Ringwald return 0; 177*dd90474bSMatthias Ringwald } 178*dd90474bSMatthias Ringwald 179*dd90474bSMatthias Ringwald static int btstack_uart_slip_wrapper_set_flowcontrol(int flowcontrol){ 180*dd90474bSMatthias Ringwald if (original_uart->set_baudrate != NULL) { 181*dd90474bSMatthias Ringwald return original_uart->set_flowcontrol(flowcontrol); 182*dd90474bSMatthias Ringwald } 183*dd90474bSMatthias Ringwald return 0; 184*dd90474bSMatthias Ringwald } 185*dd90474bSMatthias Ringwald 186*dd90474bSMatthias Ringwald static int btstack_uart_slip_wrapper_open(void){ 187*dd90474bSMatthias Ringwald return original_uart->open(); 188*dd90474bSMatthias Ringwald } 189*dd90474bSMatthias Ringwald 190*dd90474bSMatthias Ringwald static int btstack_uart_slip_wrapper_close(void){ 191*dd90474bSMatthias Ringwald return original_uart->close(); 192*dd90474bSMatthias Ringwald } 193*dd90474bSMatthias Ringwald 194*dd90474bSMatthias Ringwald static void btstack_uart_slip_wrapper_set_block_received( void (*block_handler)(void)){ 195*dd90474bSMatthias Ringwald UNUSED(block_handler); 196*dd90474bSMatthias Ringwald btstack_assert(false); 197*dd90474bSMatthias Ringwald } 198*dd90474bSMatthias Ringwald 199*dd90474bSMatthias Ringwald static void btstack_uart_slip_wrapper_set_block_sent( void (*block_handler)(void)){ 200*dd90474bSMatthias Ringwald UNUSED(block_handler); 201*dd90474bSMatthias Ringwald btstack_assert(false); 202*dd90474bSMatthias Ringwald } 203*dd90474bSMatthias Ringwald 204*dd90474bSMatthias Ringwald // SLEEP support 205*dd90474bSMatthias Ringwald 206*dd90474bSMatthias Ringwald static int btstack_uart_slip_wrapper_get_supported_sleep_modes(void){ 207*dd90474bSMatthias Ringwald if (original_uart->get_supported_sleep_modes != NULL){ 208*dd90474bSMatthias Ringwald return original_uart->get_supported_sleep_modes(); 209*dd90474bSMatthias Ringwald } 210*dd90474bSMatthias Ringwald return 0; 211*dd90474bSMatthias Ringwald } 212*dd90474bSMatthias Ringwald 213*dd90474bSMatthias Ringwald static void btstack_uart_slip_wrapper_set_sleep(btstack_uart_sleep_mode_t sleep_mode){ 214*dd90474bSMatthias Ringwald if (original_uart->set_sleep != NULL){ 215*dd90474bSMatthias Ringwald original_uart->set_sleep(sleep_mode); 216*dd90474bSMatthias Ringwald } 217*dd90474bSMatthias Ringwald } 218*dd90474bSMatthias Ringwald 219*dd90474bSMatthias Ringwald static void btstack_uart_slip_wrapper_set_wakeup_handler(void (*handler)(void)){ 220*dd90474bSMatthias Ringwald if (original_uart->set_wakeup_handler != NULL){ 221*dd90474bSMatthias Ringwald original_uart->set_wakeup_handler(handler); 222*dd90474bSMatthias Ringwald } 223*dd90474bSMatthias Ringwald } 224*dd90474bSMatthias Ringwald 225*dd90474bSMatthias Ringwald const btstack_uart_t * btstack_uart_slip_wrapper_instance(const btstack_uart_t * uart_without_slip){ 226*dd90474bSMatthias Ringwald static const btstack_uart_t btstack_uart_slip_wrapper = { 227*dd90474bSMatthias Ringwald /* int (*init)(hci_transport_config_uart_t * config); */ &btstack_uart_slip_wrapper_init, 228*dd90474bSMatthias Ringwald /* int (*open)(void); */ &btstack_uart_slip_wrapper_open, 229*dd90474bSMatthias Ringwald /* int (*close)(void); */ &btstack_uart_slip_wrapper_close, 230*dd90474bSMatthias Ringwald /* void (*set_block_received)(void (*handler)(void)); */ &btstack_uart_slip_wrapper_set_block_received, 231*dd90474bSMatthias Ringwald /* void (*set_block_sent)(void (*handler)(void)); */ &btstack_uart_slip_wrapper_set_block_sent, 232*dd90474bSMatthias Ringwald /* int (*set_baudrate)(uint32_t baudrate); */ &btstack_uart_slip_wrapper_set_baudrate, 233*dd90474bSMatthias Ringwald /* int (*set_parity)(int parity); */ &btstack_uart_slip_wrapper_set_parity, 234*dd90474bSMatthias Ringwald /* int (*set_flowcontrol)(int flowcontrol); */ &btstack_uart_slip_wrapper_set_flowcontrol, 235*dd90474bSMatthias Ringwald 236*dd90474bSMatthias Ringwald /* void (*receive_block)(uint8_t *buffer, uint16_t len); */ NULL, 237*dd90474bSMatthias Ringwald /* void (*send_block)(const uint8_t *buffer, uint16_t length); */ NULL, 238*dd90474bSMatthias Ringwald 239*dd90474bSMatthias Ringwald /* int (*get_supported_sleep_modes); */ &btstack_uart_slip_wrapper_get_supported_sleep_modes, 240*dd90474bSMatthias Ringwald /* void (*set_sleep)(btstack_uart_sleep_mode_t sleep_mode); */ &btstack_uart_slip_wrapper_set_sleep, 241*dd90474bSMatthias Ringwald /* void (*set_wakeup_handler)(void (*handler)(void)); */ &btstack_uart_slip_wrapper_set_wakeup_handler, 242*dd90474bSMatthias Ringwald 243*dd90474bSMatthias Ringwald /* void (*set_frame_received)(void (*cb)(uint16_t frame_size) */ &btstack_uart_slip_wrapper_set_frame_received, 244*dd90474bSMatthias Ringwald /* void (*set_frame_sent)(void (*block_handler)(void)); */ &btstack_uart_slip_wrapper_set_frame_sent, 245*dd90474bSMatthias Ringwald /* void (*receive_frame)(uint8_t *buffer, uint16_t len); */ &btstack_uart_slip_wrapper_receive_frame, 246*dd90474bSMatthias Ringwald /* void (*send_frame)(const uint8_t *buffer, uint16_t length); */ &btstack_uart_slip_wrapper_send_frame 247*dd90474bSMatthias Ringwald }; 248*dd90474bSMatthias Ringwald original_uart = uart_without_slip; 249*dd90474bSMatthias Ringwald return &btstack_uart_slip_wrapper; 250*dd90474bSMatthias Ringwald } 251