@@ -63,6 +63,8 @@ typedef struct {
63
63
64
64
const int RFCOMM_SERVER_CHANNEL = 1 ;
65
65
66
+ static const io_stream_t * claim_stream (uint32_t baud_rate );
67
+
66
68
static bool is_up = false;
67
69
static bluetooth_settings_t bluetooth ;
68
70
static stream_rx_buffer_t rxbuffer = {0 };
@@ -71,6 +73,16 @@ static nvs_address_t nvs_address;
71
73
static enqueue_realtime_command_ptr enqueue_realtime_command = protocol_enqueue_realtime_command ;
72
74
static bt_session_t session ;
73
75
static uint8_t spp_service_buffer [150 ];
76
+ static io_stream_properties_t bt_stream = {
77
+ .type = StreamType_Bluetooth ,
78
+ .instance = 20 ,
79
+ .flags .claimable = On ,
80
+ .flags .claimed = Off ,
81
+ .flags .connected = Off ,
82
+ .flags .can_set_baud = On ,
83
+ .flags .modbus_ready = Off ,
84
+ .claim = claim_stream
85
+ };
74
86
static on_report_options_ptr on_report_options ;
75
87
static on_execute_realtime_ptr on_execute_realtime ;
76
88
@@ -235,6 +247,8 @@ static void msg_bt_open_failed (sys_state_t state)
235
247
hal .stream .write_all ("]" ASCII_EOL );
236
248
}
237
249
250
+ #if PICO_CYW43_ARCH_POLL && !WIFI_ENABLE
251
+
238
252
static void bt_poll (sys_state_t state )
239
253
{
240
254
static uint32_t last_ms0 ;
@@ -249,11 +263,18 @@ static void bt_poll (sys_state_t state)
249
263
on_execute_realtime (state );
250
264
}
251
265
252
- static void packetHandler (uint8_t type , uint16_t channel , uint8_t * packet , uint16_t size )
266
+ #endif
267
+
268
+ static bool is_connected (void )
253
269
{
254
- static const io_stream_t bluetooth_stream = {
270
+ return bt_stream .flags .connected ;
271
+ }
272
+
273
+ static const io_stream_t * claim_stream (uint32_t baud_rate )
274
+ {
275
+ static const io_stream_t stream = {
255
276
.type = StreamType_Bluetooth ,
256
- .state . connected = true ,
277
+ .is_connected = is_connected ,
257
278
.read = BTStreamGetC ,
258
279
.write = BTStreamWriteS ,
259
280
.write_char = BTStreamPutC ,
@@ -263,8 +284,20 @@ static void packetHandler (uint8_t type, uint16_t channel, uint8_t *packet, uint
263
284
.set_enqueue_rt_handler = BTSetRtHandler
264
285
};
265
286
287
+ if (bt_stream .flags .claimed || bt_stream .flags .connected )
288
+ return NULL ;
289
+
290
+ if (baud_rate != 0 )
291
+ bt_stream .flags .claimed = On ;
292
+
293
+ return & stream ;
294
+ }
295
+
296
+ static void packetHandler (uint8_t type , uint16_t channel , uint8_t * packet , uint16_t size )
297
+ {
266
298
static uint16_t mtu ;
267
299
static uint8_t tx_buf [TX_BUFFER_SIZE ];
300
+ static const io_stream_t * stream = NULL ;
268
301
269
302
UNUSED (channel );
270
303
@@ -292,15 +325,22 @@ static void packetHandler (uint8_t type, uint16_t channel, uint8_t *packet, uint
292
325
if ((session .status = rfcomm_event_channel_opened_get_status (packet )))
293
326
protocol_enqueue_rt_command (msg_bt_open_failed );
294
327
else {
295
-
296
- mtu = rfcomm_event_channel_opened_get_max_frame_size (packet );
297
- session .channel = rfcomm_event_channel_opened_get_rfcomm_cid (packet );
298
- session .connected = true;
299
-
300
- rxbuffer .tail = rxbuffer .head ; // Flush rx & tx
301
- txbuffer .tail = txbuffer .head ; // buffers.
328
+ if (!bt_stream .flags .connected ) {
329
+
330
+ rxbuffer .tail = rxbuffer .head ; // Flush rx & tx
331
+ txbuffer .tail = txbuffer .head ; // buffers.
302
332
303
- stream_connect (& bluetooth_stream );
333
+ if (bt_stream .flags .claimed )
334
+ bt_stream .flags .connected = On ;
335
+ else if (!bt_stream .flags .connected && (stream = claim_stream (0 )))
336
+ bt_stream .flags .connected = stream_connect (stream );
337
+
338
+ mtu = rfcomm_event_channel_opened_get_max_frame_size (packet );
339
+ session .channel = rfcomm_event_channel_opened_get_rfcomm_cid (packet );
340
+ session .connected = bt_stream .flags .connected ;
341
+ } // else deny connection...
342
+
343
+ // if(!bt_stream.flags.connected) deny connection...
304
344
}
305
345
break ;
306
346
@@ -327,7 +367,11 @@ static void packetHandler (uint8_t type, uint16_t channel, uint8_t *packet, uint
327
367
session .channel = 0 ;
328
368
session .connected = false;
329
369
* session .client_mac = '\0' ;
330
- stream_disconnect (& bluetooth_stream );
370
+ if (stream ) {
371
+ stream_disconnect (stream );
372
+ stream = NULL ;
373
+ }
374
+ bt_stream .flags .connected = Off ;
331
375
break ;
332
376
333
377
default :
@@ -367,6 +411,10 @@ bool bluetooth_start_local (void)
367
411
{
368
412
static char device_name [52 ];
369
413
static btstack_packet_callback_registration_t hci_event_callback ;
414
+ static io_stream_details_t streams = {
415
+ .n_streams = 1 ,
416
+ .streams = & bt_stream ,
417
+ };
370
418
371
419
if (* bluetooth .device_name == '\0' )
372
420
return false;
@@ -400,6 +448,8 @@ bool bluetooth_start_local (void)
400
448
401
449
hci_power_control (HCI_POWER_ON );
402
450
451
+ stream_register_streams (& streams );
452
+
403
453
is_up = true;
404
454
405
455
#if PICO_CYW43_ARCH_POLL && !WIFI_ENABLE
@@ -417,7 +467,6 @@ bool bluetooth_disable (void)
417
467
return true;
418
468
}
419
469
420
-
421
470
static const setting_group_detail_t bluetooth_groups [] = {
422
471
{ Group_Root , Group_Bluetooth , "Bluetooth" },
423
472
};
0 commit comments