Skip to content

Commit 398b627

Browse files
committed
pbio/drv/bluetooth: Split out connection checks.
We had already split off the peripheral since it has more than one instance. The same will now apply to the host. The check for LE connections without Pybricks was never used, and NUS is not supported, so this function can go.
1 parent 5ea0795 commit 398b627

8 files changed

Lines changed: 58 additions & 108 deletions

File tree

lib/pbio/drv/bluetooth/bluetooth.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ void pbdrv_bluetooth_host_connection_changed(void) {
9494
pbio_error_t pbdrv_bluetooth_tx(const uint8_t *data, uint32_t *size) {
9595

9696
// make sure we have a Bluetooth connection
97-
if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PYBRICKS)) {
97+
if (!pbdrv_bluetooth_host_is_connected()) {
9898
return PBIO_ERROR_INVALID_OP;
9999
}
100100

@@ -112,15 +112,15 @@ pbio_error_t pbdrv_bluetooth_tx(const uint8_t *data, uint32_t *size) {
112112
}
113113

114114
uint32_t pbdrv_bluetooth_tx_available(void) {
115-
if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PYBRICKS)) {
115+
if (!pbdrv_bluetooth_host_is_connected()) {
116116
return UINT32_MAX;
117117
}
118118

119119
return lwrb_get_free(&stdout_ring_buf);
120120
}
121121

122122
bool pbdrv_bluetooth_tx_is_idle(void) {
123-
if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PYBRICKS)) {
123+
if (!pbdrv_bluetooth_host_is_connected()) {
124124
return true;
125125
}
126126

@@ -130,7 +130,7 @@ bool pbdrv_bluetooth_tx_is_idle(void) {
130130
pbio_error_t pbdrv_bluetooth_send_event_notification(pbio_os_state_t *state, pbio_pybricks_event_t event_type, const uint8_t *data, size_t size) {
131131
PBIO_OS_ASYNC_BEGIN(state);
132132

133-
if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PYBRICKS)) {
133+
if (!pbdrv_bluetooth_host_is_connected()) {
134134
return PBIO_ERROR_INVALID_OP;
135135
}
136136

@@ -194,7 +194,7 @@ const char *pbdrv_bluetooth_peripheral_get_name(pbdrv_bluetooth_peripheral_t *pe
194194

195195
pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect(pbdrv_bluetooth_peripheral_t *peri, pbdrv_bluetooth_peripheral_connect_config_t *config) {
196196

197-
if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_HCI)) {
197+
if (!pbdrv_bluetooth_hci_is_enabled()) {
198198
return PBIO_ERROR_INVALID_OP;
199199
}
200200

@@ -332,7 +332,7 @@ pbdrv_bluetooth_advertising_state_t pbdrv_bluetooth_advertising_state;
332332

333333
pbio_error_t pbdrv_bluetooth_start_advertising(bool start) {
334334

335-
if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_HCI)) {
335+
if (!pbdrv_bluetooth_hci_is_enabled()) {
336336
return PBIO_ERROR_INVALID_OP;
337337
}
338338

@@ -367,7 +367,7 @@ uint8_t pbdrv_bluetooth_broadcast_data_size;
367367

368368
pbio_error_t pbdrv_bluetooth_start_broadcasting(const uint8_t *data, size_t size) {
369369

370-
if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_HCI)) {
370+
if (!pbdrv_bluetooth_hci_is_enabled()) {
371371
return PBIO_ERROR_INVALID_OP;
372372
}
373373

@@ -417,7 +417,7 @@ pbdrv_bluetooth_start_observing_callback_t pbdrv_bluetooth_observe_callback;
417417

418418
pbio_error_t pbdrv_bluetooth_start_observing(pbdrv_bluetooth_start_observing_callback_t callback) {
419419

420-
if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_HCI)) {
420+
if (!pbdrv_bluetooth_hci_is_enabled()) {
421421
return PBIO_ERROR_INVALID_OP;
422422
}
423423

@@ -514,7 +514,7 @@ static pbdrv_bluetooth_classic_task_context_t pbdrv_bluetooth_classic_task_conte
514514

515515
pbio_error_t pbdrv_bluetooth_start_inquiry_scan(pbdrv_bluetooth_inquiry_result_t *results, uint32_t *results_count, uint32_t *results_count_max, uint32_t duration_ms) {
516516

517-
if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_HCI)) {
517+
if (!pbdrv_bluetooth_hci_is_enabled()) {
518518
return PBIO_ERROR_INVALID_OP;
519519
}
520520

@@ -582,7 +582,7 @@ pbio_error_t pbdrv_bluetooth_process_thread(pbio_os_state_t *state, void *contex
582582
pbio_error_t err;
583583

584584
// Shorthand notation accessible throughout.
585-
bool can_send = pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PYBRICKS);
585+
bool can_send = pbdrv_bluetooth_host_is_connected();
586586

587587
// For looping over peripherals.
588588
static uint8_t peri_index;
@@ -724,7 +724,7 @@ pbio_error_t pbdrv_bluetooth_close_user_tasks(pbio_os_state_t *state, pbio_os_ti
724724
void pbdrv_bluetooth_deinit(void) {
725725

726726
// If Bluetooth is not even initialized, nothing to do.
727-
if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_HCI)) {
727+
if (!pbdrv_bluetooth_hci_is_enabled()) {
728728
return;
729729
}
730730

lib/pbio/drv/bluetooth/bluetooth_btstack.c

Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,6 @@ static void pbdrv_bluetooth_peripheral_disconnect_now(pbdrv_bluetooth_peripheral
185185
gap_disconnect(peri->con_handle);
186186
}
187187

188-
/**
189-
* Checks if the given peripheral is connected.
190-
*/
191-
bool pbdrv_bluetooth_peripheral_is_connected(pbdrv_bluetooth_peripheral_t *peri) {
192-
return peri->con_handle != HCI_CON_HANDLE_INVALID;
193-
}
194188

195189
static pbio_os_state_t bluetooth_thread_state;
196190
static pbio_os_state_t bluetooth_thread_err;
@@ -214,30 +208,16 @@ static void propagate_event(uint8_t *packet) {
214208
event_packet = NULL;
215209
}
216210

217-
bool pbdrv_bluetooth_is_connected(pbdrv_bluetooth_connection_t connection) {
218-
219-
// Nothing connected if HCI is not running.
220-
if (bluetooth_thread_err != PBIO_ERROR_AGAIN) {
221-
return false;
222-
}
223-
224-
if (connection == PBDRV_BLUETOOTH_CONNECTION_HCI) {
225-
return true;
226-
}
227-
228-
if (connection == PBDRV_BLUETOOTH_CONNECTION_LE && le_con_handle != HCI_CON_HANDLE_INVALID) {
229-
return true;
230-
}
231-
232-
if (connection == PBDRV_BLUETOOTH_CONNECTION_PYBRICKS && pybricks_con_handle != HCI_CON_HANDLE_INVALID) {
233-
return true;
234-
}
211+
bool pbdrv_bluetooth_peripheral_is_connected(pbdrv_bluetooth_peripheral_t *peri) {
212+
return peri->con_handle != HCI_CON_HANDLE_INVALID;
213+
}
235214

236-
if (connection == PBDRV_BLUETOOTH_CONNECTION_UART && uart_con_handle != HCI_CON_HANDLE_INVALID) {
237-
return true;
238-
}
215+
bool pbdrv_bluetooth_host_is_connected(void) {
216+
return pybricks_con_handle != HCI_CON_HANDLE_INVALID;
217+
}
239218

240-
return false;
219+
bool pbdrv_bluetooth_hci_is_enabled(void) {
220+
return bluetooth_thread_err == PBIO_ERROR_AGAIN;
241221
}
242222

243223
static void nordic_spp_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size) {
@@ -1203,7 +1183,7 @@ static pbio_error_t bluetooth_btstack_handle_power_control(pbio_os_state_t *stat
12031183

12041184
pbio_error_t pbdrv_bluetooth_controller_reset(pbio_os_state_t *state, pbio_os_timer_t *timer) {
12051185

1206-
if (pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_HCI)) {
1186+
if (pbdrv_bluetooth_hci_is_enabled()) {
12071187
return PBIO_ERROR_INVALID_OP;
12081188
}
12091189

lib/pbio/drv/bluetooth/bluetooth_stm32_bluenrg.c

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,14 @@ bool pbdrv_bluetooth_peripheral_is_connected(pbdrv_bluetooth_peripheral_t *peri)
137137
return peri == &peripheral_singleton && peri->con_handle != 0;
138138
}
139139

140+
bool pbdrv_bluetooth_host_is_connected(void) {
141+
return pybricks_notify_en;
142+
}
143+
144+
bool pbdrv_bluetooth_hci_is_enabled(void) {
145+
return true;
146+
}
147+
140148
/**
141149
* Converts a BlueNRG-MS error code to a PBIO error code.
142150
* @param [in] status The BlueNRG-MS error code.
@@ -301,28 +309,6 @@ pbio_error_t pbdrv_bluetooth_stop_advertising_func(pbio_os_state_t *state, void
301309
PBIO_OS_ASYNC_END(PBIO_SUCCESS);
302310
}
303311

304-
305-
bool pbdrv_bluetooth_is_connected(pbdrv_bluetooth_connection_t connection) {
306-
307-
if (connection == PBDRV_BLUETOOTH_CONNECTION_HCI) {
308-
return true;
309-
}
310-
311-
if (connection == PBDRV_BLUETOOTH_CONNECTION_LE && conn_handle) {
312-
return true;
313-
}
314-
315-
if (connection == PBDRV_BLUETOOTH_CONNECTION_PYBRICKS && pybricks_notify_en) {
316-
return true;
317-
}
318-
319-
if (connection == PBDRV_BLUETOOTH_CONNECTION_UART && uart_tx_notify_en) {
320-
return true;
321-
}
322-
323-
return false;
324-
}
325-
326312
pbio_error_t pbdrv_bluetooth_send_pybricks_value_notification(pbio_os_state_t *state, const uint8_t *data, uint16_t size) {
327313

328314
PBIO_OS_ASYNC_BEGIN(state);

lib/pbio/drv/bluetooth/bluetooth_stm32_cc2640.c

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,14 @@ bool pbdrv_bluetooth_peripheral_is_connected(pbdrv_bluetooth_peripheral_t *peri)
156156
return peri == &peripheral_singleton && peri->con_handle != NO_CONNECTION;
157157
}
158158

159+
bool pbdrv_bluetooth_host_is_connected(void) {
160+
return pybricks_notify_en && !busy_disconnecting;
161+
}
162+
163+
bool pbdrv_bluetooth_hci_is_enabled(void) {
164+
return true;
165+
}
166+
159167
/**
160168
* Converts a ble error code to the most appropriate pbio error code.
161169
* @param [in] status The ble error code.
@@ -343,27 +351,6 @@ pbio_error_t pbdrv_bluetooth_stop_advertising_func(pbio_os_state_t *state, void
343351
PBIO_OS_ASYNC_END(PBIO_SUCCESS);
344352
}
345353

346-
bool pbdrv_bluetooth_is_connected(pbdrv_bluetooth_connection_t connection) {
347-
348-
if (connection == PBDRV_BLUETOOTH_CONNECTION_HCI) {
349-
return true;
350-
}
351-
352-
if (connection == PBDRV_BLUETOOTH_CONNECTION_LE && conn_handle != NO_CONNECTION && !busy_disconnecting) {
353-
return true;
354-
}
355-
356-
if (connection == PBDRV_BLUETOOTH_CONNECTION_PYBRICKS && pybricks_notify_en) {
357-
return true;
358-
}
359-
360-
if (connection == PBDRV_BLUETOOTH_CONNECTION_UART && uart_tx_notify_en) {
361-
return true;
362-
}
363-
364-
return false;
365-
}
366-
367354
pbio_error_t pbdrv_bluetooth_send_pybricks_value_notification(pbio_os_state_t *state, const uint8_t *data, uint16_t size) {
368355

369356
static attHandleValueNoti_t notification;
@@ -403,8 +390,8 @@ pbio_error_t pbdrv_bluetooth_peripheral_scan_and_connect_func(pbio_os_state_t *s
403390
if (conn_handle != NO_CONNECTION &&
404391
(peri->config->options & PBDRV_BLUETOOTH_PERIPHERAL_OPTIONS_DISCONNECT_HOST)) {
405392
DEBUG_PRINT("Disconnect from Pybricks code (%d).\n", conn_handle);
406-
// Guard used in pbdrv_bluetooth_is_connected so higher level processes
407-
// won't try to send anything while we are disconnecting.
393+
// Guard used in pbdrv_bluetooth_host_is_connected so higher level
394+
// processes won't try to send anything while we are disconnecting.
408395
busy_disconnecting = true;
409396
PBIO_OS_AWAIT_WHILE(state, write_xfer_size);
410397
GAP_TerminateLinkReq(conn_handle, 0x13);

lib/pbio/include/pbdrv/bluetooth.h

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,6 @@
2222
#define PBDRV_BLUETOOTH_MAX_CHAR_SIZE 20
2323
#define PBDRV_BLUETOOTH_MAX_ADV_SIZE 31
2424

25-
/**
26-
* BLE characteristic connection identifiers.
27-
*/
28-
typedef enum {
29-
/* Whether Bluetooth is present and enabled on this platform. */
30-
PBDRV_BLUETOOTH_CONNECTION_HCI,
31-
/* A low energy device connection. */
32-
PBDRV_BLUETOOTH_CONNECTION_LE,
33-
/** The Pybricks service. */
34-
PBDRV_BLUETOOTH_CONNECTION_PYBRICKS,
35-
/** The Nordic UART service. */
36-
PBDRV_BLUETOOTH_CONNECTION_UART,
37-
} pbdrv_bluetooth_connection_t;
38-
3925
/** Data structure that holds context needed for sending BLE notifications. */
4026
typedef struct _pbdrv_bluetooth_send_context_t pbdrv_bluetooth_send_context_t;
4127

@@ -85,8 +71,6 @@ struct _pbdrv_bluetooth_send_context_t {
8571
const uint8_t *data;
8672
/** The size of @p data. */
8773
uint8_t size;
88-
/** The connection to use. Only characteristics with notify capability are allowed. */
89-
pbdrv_bluetooth_connection_t connection;
9074
};
9175

9276
/**
@@ -301,12 +285,21 @@ const char *pbdrv_bluetooth_get_hub_name(void);
301285
const char *pbdrv_bluetooth_get_fw_version(void);
302286

303287
/**
304-
* Tests if a central is connected to the Bluetooth chip.
288+
* Tests if a central is connected to the Bluetooth chip and subscribed to
289+
* Pybricks events.
290+
*
305291
* @param [in] connection The type of connection of interest.
306-
* @return True if the requested connection type is present,
292+
* @return True if Pybricks host connected, otherwise false.
293+
*/
294+
bool pbdrv_bluetooth_host_is_connected(void);
295+
296+
/**
297+
* Tests if the Bluetooth controller is up and running.
298+
*
299+
* @return True if running,
307300
* otherwise false.
308301
*/
309-
bool pbdrv_bluetooth_is_connected(pbdrv_bluetooth_connection_t connection);
302+
bool pbdrv_bluetooth_hci_is_enabled(void);
310303

311304
/**
312305
* Sets a callback to be called when a Bluetooth host is connected or disconnected.
@@ -638,7 +631,11 @@ static inline const char *pbdrv_bluetooth_get_fw_version(void) {
638631
return "";
639632
}
640633

641-
static inline bool pbdrv_bluetooth_is_connected(pbdrv_bluetooth_connection_t connection) {
634+
static inline bool pbdrv_bluetooth_host_is_connected(void) {
635+
return false;
636+
}
637+
638+
static inline bool pbdrv_bluetooth_hci_is_enabled(void) {
642639
return false;
643640
}
644641

lib/pbio/sys/hmi_pup.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ static void pbsys_hmi_host_update_indications(void) {
143143
}
144144

145145
// Update BLE light indication.
146-
if (pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PYBRICKS)) {
146+
if (pbdrv_bluetooth_host_is_connected()) {
147147
pbsys_status_set(PBIO_PYBRICKS_STATUS_BLE_HOST_CONNECTED);
148148
} else {
149149
pbsys_status_clear(PBIO_PYBRICKS_STATUS_BLE_HOST_CONNECTED);

lib/pbio/sys/host.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void pbsys_host_schedule_status_update(const uint8_t *buf) {
4747
* @return @c true if connection is active, else @c false.
4848
*/
4949
bool pbsys_host_is_connected(void) {
50-
return pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PYBRICKS) ||
50+
return pbdrv_bluetooth_host_is_connected() ||
5151
pbdrv_usb_connection_is_active();
5252
}
5353

pybricks/iodevices/pb_type_iodevices_xbox_controller.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ static mp_obj_t pb_type_xbox_make_new(const mp_obj_type_t *type, size_t n_args,
453453
// By default, disconnect Technic Hub from host, as this is required for
454454
// most hosts. Stay connected only if the user explicitly requests it.
455455
#if PYBRICKS_HUB_TECHNICHUB
456-
if (!mp_obj_is_true(stay_connected_in) && pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PYBRICKS)) {
456+
if (!mp_obj_is_true(stay_connected_in) && pbdrv_bluetooth_host_is_connected()) {
457457
scan_config.options |= PBDRV_BLUETOOTH_PERIPHERAL_OPTIONS_DISCONNECT_HOST;
458458
mp_printf(&mp_plat_print, "The hub may disconnect from the computer for better connectivity with the controller.\n");
459459
mp_hal_delay_ms(500);

0 commit comments

Comments
 (0)