1 /*
2 * Copyright (C) 2021 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 BLUEKITCHEN
24 * GMBH 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 #define BTSTACK_FILE__ "btstack_uart_slip_wrapper.c"
39
40 /*
41 * btstack_uart_slip_wrapper.c
42 *
43 *
44 */
45
46 #include "btstack_uart_slip_wrapper.h"
47
48 #include "btstack_run_loop.h"
49 #include "btstack_debug.h"
50 #include "btstack_defines.h"
51 #include "btstack_slip.h"
52 #include "btstack_util.h"
53
54 // max size of outgoing SLIP chunks
55 #define SLIP_TX_CHUNK_LEN 128
56
57 #define SLIP_RECEIVE_BUFFER_SIZE 128
58
59 // wrapped driver
60 static const btstack_uart_t * original_uart;
61
62 // callbacks
63 static void (*frame_sent)(void);
64 static void (*frame_received)(uint16_t frame_size);
65
66 // SLIP encoding
67 static uint8_t btstack_uart_slip_outgoing_buffer[SLIP_TX_CHUNK_LEN+1];
68
69 // SLIP decoding
70 static uint8_t btstack_uart_slip_wrapper_read_byte;
71 static uint32_t btstack_uart_slip_wrapper_receive_start;
72 static uint32_t btstack_uart_slip_wrapper_baudrate;
73
74
75 // SLIP ENCODING
76
btstack_uart_slip_posix_encode_chunk_and_send(void)77 static void btstack_uart_slip_posix_encode_chunk_and_send(void){
78 uint16_t pos = 0;
79 while (btstack_slip_encoder_has_data() & (pos < SLIP_TX_CHUNK_LEN)) {
80 btstack_uart_slip_outgoing_buffer[pos++] = btstack_slip_encoder_get_byte();
81 }
82
83 // setup async write and start sending
84 original_uart->send_block(btstack_uart_slip_outgoing_buffer, pos);
85 }
86
btstack_uart_slip_wrapper_block_sent(void)87 static void btstack_uart_slip_wrapper_block_sent(void){
88 // check if more data to send
89 if (btstack_slip_encoder_has_data()){
90 btstack_uart_slip_posix_encode_chunk_and_send();
91 return;
92 }
93
94 // notify done
95 if (frame_sent){
96 frame_sent();
97 }
98 }
99
btstack_uart_slip_wrapper_send_frame(const uint8_t * frame,uint16_t frame_size)100 static void btstack_uart_slip_wrapper_send_frame(const uint8_t * frame, uint16_t frame_size){
101
102 // Prepare encoding of Header + Packet (+ DIC)
103 btstack_slip_encoder_start(frame, frame_size);
104
105 // Fill rest of chunk from packet and send
106 btstack_uart_slip_posix_encode_chunk_and_send();
107 }
108
btstack_uart_slip_wrapper_set_frame_sent(void (* frame_handler)(void))109 static void btstack_uart_slip_wrapper_set_frame_sent( void (*frame_handler)(void)){
110 frame_sent = frame_handler;
111 original_uart->set_block_sent(&btstack_uart_slip_wrapper_block_sent);
112 }
113
114 // SLIP DECODING
115
btstack_uart_slip_wrapper_read_next_byte(void)116 static void btstack_uart_slip_wrapper_read_next_byte(void){
117 original_uart->receive_block(&btstack_uart_slip_wrapper_read_byte, 1);
118 }
119
120 // track time receiving SLIP frame
btstack_uart_slip_wrapper_block_received(void)121 static void btstack_uart_slip_wrapper_block_received(void){
122
123 // track start time when receiving first byte // a bit hackish
124 if ((btstack_uart_slip_wrapper_receive_start == 0u) && (btstack_uart_slip_wrapper_read_byte != BTSTACK_SLIP_SOF)){
125 btstack_uart_slip_wrapper_receive_start = btstack_run_loop_get_time_ms();
126 }
127 btstack_slip_decoder_process(btstack_uart_slip_wrapper_read_byte);
128 uint16_t frame_size = btstack_slip_decoder_frame_size();
129 if (frame_size) {
130 // track time
131 uint32_t packet_receive_time = btstack_run_loop_get_time_ms() - btstack_uart_slip_wrapper_receive_start;
132 uint32_t nominal_time = (frame_size + 6u) * 10u * 1000u / btstack_uart_slip_wrapper_baudrate;
133 UNUSED(nominal_time);
134 UNUSED(packet_receive_time);
135 if (packet_receive_time > btstack_max(2 * nominal_time, 5)){
136 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 }
138 // reset state
139 btstack_uart_slip_wrapper_receive_start = 0;
140 // deliver frame
141 frame_received(frame_size);
142 } else {
143 btstack_uart_slip_wrapper_read_next_byte();
144 }
145 }
146
btstack_uart_slip_wrapper_set_frame_received(void (* frame_handler)(uint16_t frame_size))147 static void btstack_uart_slip_wrapper_set_frame_received( void (*frame_handler)(uint16_t frame_size)){
148 frame_received = frame_handler;
149 original_uart->set_block_received(&btstack_uart_slip_wrapper_block_received);
150 }
151
btstack_uart_slip_wrapper_receive_frame(uint8_t * buffer,uint16_t len)152 static void btstack_uart_slip_wrapper_receive_frame(uint8_t *buffer, uint16_t len){
153 btstack_slip_decoder_init(buffer, len);
154 btstack_uart_slip_wrapper_read_next_byte();
155 }
156
157 // SLIP End
158
btstack_uart_slip_wrapper_init(const btstack_uart_config_t * config)159 static int btstack_uart_slip_wrapper_init(const btstack_uart_config_t * config){
160 btstack_uart_slip_wrapper_baudrate = config->baudrate;
161 return original_uart->init(config);
162 }
163
btstack_uart_slip_wrapper_set_baudrate(uint32_t baudrate)164 static int btstack_uart_slip_wrapper_set_baudrate(uint32_t baudrate){
165 if (original_uart->set_baudrate != NULL){
166 btstack_uart_slip_wrapper_baudrate = baudrate;
167 return original_uart->set_baudrate(baudrate);
168 }
169 return 0;
170 }
171
btstack_uart_slip_wrapper_set_parity(int parity)172 static int btstack_uart_slip_wrapper_set_parity(int parity){
173 if (original_uart->set_baudrate != NULL) {
174 return original_uart->set_parity(parity);
175 }
176 return 0;
177 }
178
btstack_uart_slip_wrapper_set_flowcontrol(int flowcontrol)179 static int btstack_uart_slip_wrapper_set_flowcontrol(int flowcontrol){
180 if (original_uart->set_baudrate != NULL) {
181 return original_uart->set_flowcontrol(flowcontrol);
182 }
183 return 0;
184 }
185
btstack_uart_slip_wrapper_open(void)186 static int btstack_uart_slip_wrapper_open(void){
187 return original_uart->open();
188 }
189
btstack_uart_slip_wrapper_close(void)190 static int btstack_uart_slip_wrapper_close(void){
191 return original_uart->close();
192 }
193
btstack_uart_slip_wrapper_set_block_received(void (* block_handler)(void))194 static void btstack_uart_slip_wrapper_set_block_received( void (*block_handler)(void)){
195 UNUSED(block_handler);
196 btstack_assert(false);
197 }
198
btstack_uart_slip_wrapper_set_block_sent(void (* block_handler)(void))199 static void btstack_uart_slip_wrapper_set_block_sent( void (*block_handler)(void)){
200 UNUSED(block_handler);
201 btstack_assert(false);
202 }
203
204 // SLEEP support
205
btstack_uart_slip_wrapper_get_supported_sleep_modes(void)206 static int btstack_uart_slip_wrapper_get_supported_sleep_modes(void){
207 if (original_uart->get_supported_sleep_modes != NULL){
208 return original_uart->get_supported_sleep_modes();
209 }
210 return 0;
211 }
212
btstack_uart_slip_wrapper_set_sleep(btstack_uart_sleep_mode_t sleep_mode)213 static void btstack_uart_slip_wrapper_set_sleep(btstack_uart_sleep_mode_t sleep_mode){
214 if (original_uart->set_sleep != NULL){
215 original_uart->set_sleep(sleep_mode);
216 }
217 }
218
btstack_uart_slip_wrapper_set_wakeup_handler(void (* handler)(void))219 static void btstack_uart_slip_wrapper_set_wakeup_handler(void (*handler)(void)){
220 if (original_uart->set_wakeup_handler != NULL){
221 original_uart->set_wakeup_handler(handler);
222 }
223 }
224
btstack_uart_slip_wrapper_instance(const btstack_uart_t * uart_without_slip)225 const btstack_uart_t * btstack_uart_slip_wrapper_instance(const btstack_uart_t * uart_without_slip){
226 static const btstack_uart_t btstack_uart_slip_wrapper = {
227 /* int (*init)(hci_transport_config_uart_t * config); */ &btstack_uart_slip_wrapper_init,
228 /* int (*open)(void); */ &btstack_uart_slip_wrapper_open,
229 /* int (*close)(void); */ &btstack_uart_slip_wrapper_close,
230 /* void (*set_block_received)(void (*handler)(void)); */ &btstack_uart_slip_wrapper_set_block_received,
231 /* void (*set_block_sent)(void (*handler)(void)); */ &btstack_uart_slip_wrapper_set_block_sent,
232 /* int (*set_baudrate)(uint32_t baudrate); */ &btstack_uart_slip_wrapper_set_baudrate,
233 /* int (*set_parity)(int parity); */ &btstack_uart_slip_wrapper_set_parity,
234 /* int (*set_flowcontrol)(int flowcontrol); */ &btstack_uart_slip_wrapper_set_flowcontrol,
235
236 /* void (*receive_block)(uint8_t *buffer, uint16_t len); */ NULL,
237 /* void (*send_block)(const uint8_t *buffer, uint16_t length); */ NULL,
238
239 /* int (*get_supported_sleep_modes); */ &btstack_uart_slip_wrapper_get_supported_sleep_modes,
240 /* void (*set_sleep)(btstack_uart_sleep_mode_t sleep_mode); */ &btstack_uart_slip_wrapper_set_sleep,
241 /* void (*set_wakeup_handler)(void (*handler)(void)); */ &btstack_uart_slip_wrapper_set_wakeup_handler,
242
243 /* void (*set_frame_received)(void (*cb)(uint16_t frame_size) */ &btstack_uart_slip_wrapper_set_frame_received,
244 /* void (*set_frame_sent)(void (*block_handler)(void)); */ &btstack_uart_slip_wrapper_set_frame_sent,
245 /* void (*receive_frame)(uint8_t *buffer, uint16_t len); */ &btstack_uart_slip_wrapper_receive_frame,
246 /* void (*send_frame)(const uint8_t *buffer, uint16_t length); */ &btstack_uart_slip_wrapper_send_frame
247 };
248 original_uart = uart_without_slip;
249 return &btstack_uart_slip_wrapper;
250 }
251