Skip to content

Commit 3f634b6

Browse files
committed
pbio/drv/bluetooth: Raise on sending app data when disconnected.
We wouldn't want to wait forever if there is no connection. With stdout, the output is just ignored, but here it makes sense to raise.
1 parent 4b6dbb4 commit 3f634b6

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

lib/pbio/drv/bluetooth/bluetooth.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,18 @@ bool pbdrv_bluetooth_tx_is_idle(void) {
107107
}
108108

109109
/**
110-
* Buffer for generic notification, but not buffered or schedules.
110+
* Buffer for generic awaitable notification.
111111
*/
112112
static uint8_t user_notification_send_buf[PBDRV_BLUETOOTH_MAX_CHAR_SIZE];
113113
static size_t user_notification_size;
114114

115115
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) {
116116
PBIO_OS_ASYNC_BEGIN(state);
117117

118+
if (!pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PYBRICKS)) {
119+
return PBIO_ERROR_INVALID_OP;
120+
}
121+
118122
if (size + 1 > PBIO_ARRAY_SIZE(user_notification_send_buf)) {
119123
return PBIO_ERROR_INVALID_ARG;
120124
}
@@ -130,7 +134,8 @@ pbio_error_t pbdrv_bluetooth_send_event_notification(pbio_os_state_t *state, pbi
130134
memcpy(&user_notification_send_buf[1], data, size);
131135
pbio_os_request_poll();
132136

133-
// Await until main process has finished sending user data.
137+
// Await until main process has finished sending user data. If it
138+
// disconnected while sending, this completes as well.
134139
PBIO_OS_AWAIT_WHILE(state, user_notification_size);
135140

136141
PBIO_OS_ASYNC_END(PBIO_SUCCESS);

lib/pbio/include/pbdrv/bluetooth.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ bool pbdrv_bluetooth_tx_is_idle(void);
328328
* @param [in] data Data to send.
329329
* @param [in] size Data size, not counting event type byte.
330330
* @return ::PBIO_SUCCESS on completion.
331+
* ::PBIO_ERROR_INVALID_OP if there is no connection.
331332
* ::PBIO_ERROR_AGAIN while awaiting.
332333
* ::PBIO_ERROR_BUSY if this operation is already ongoing.
333334
* ::PBIO_ERROR_INVALID_ARG if @p size is too large.

lib/pbio/sys/host.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ bool pbsys_host_tx_is_idle(void) {
218218
* @param size [in] The size of the data to transmit.
219219
* contains the number of bytes actually processed.
220220
* @return ::PBIO_ERROR_AGAIN while the operation is in progress.
221-
* ::PBIO_ERROR_NODEV if there is no connection to send it to.
221+
* ::PBIO_ERROR_INVALID_OP if there is no connection to send it to.
222222
* ::PBIO_ERROR_BUSY if another transfer is already queued or in progress.
223223
* ::PBIO_SUCCESS on completion.
224224
*/

0 commit comments

Comments
 (0)