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