Skip to content

Commit bdf60f7

Browse files
committed
pbio/sys/host: Test if connection is established.
Needed in other system code to test for shutdown when idle for some time while no connection is active.
1 parent e0b0fdc commit bdf60f7

File tree

6 files changed

+36
-4
lines changed

6 files changed

+36
-4
lines changed

lib/pbio/drv/usb/usb_ev3.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,10 @@ void pbdrv_usb_tx_flush(void) {
534534
USBBufferFlush((tUSBBuffer *)&g_sTxBuffer);
535535
}
536536

537+
bool pbdrv_usb_connection_is_active(void) {
538+
return false;
539+
}
540+
537541
PROCESS_THREAD(pbdrv_usb_process, ev, data) {
538542

539543
PROCESS_BEGIN();

lib/pbio/drv/usb/usb_stm32.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,10 @@ static void pbdrv_usb_stm32_reset_tx_state(void) {
214214
pbdrv_usb_stm32_is_events_subscribed = false;
215215
}
216216

217+
bool pbdrv_usb_connection_is_active(void) {
218+
return pbdrv_usb_stm32_is_events_subscribed;
219+
}
220+
217221
/**
218222
* @brief Pybricks_Itf_Init
219223
* Initializes the Pybricks media low layer

lib/pbio/include/pbdrv/usb.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ uint32_t pbdrv_usb_stdout_tx_available(void);
6262
*/
6363
bool pbdrv_usb_stdout_tx_is_idle(void);
6464

65+
/**
66+
* Indicates if a Pybricks app is connected and configured.
67+
*
68+
* @retval true if active, so the host has subscribed to events.
69+
*/
70+
bool pbdrv_usb_connection_is_active(void);
71+
6572
#else // PBDRV_CONFIG_USB
6673

6774
static inline pbdrv_usb_bcd_t pbdrv_usb_get_bcd(void) {
@@ -80,6 +87,10 @@ static inline bool pbdrv_usb_stdout_tx_is_idle(void) {
8087
return true;
8188
}
8289

90+
static inline bool pbdrv_usb_connection_is_active(void) {
91+
return false;
92+
}
93+
8394
#endif // PBDRV_CONFIG_USB
8495

8596
#endif // _PBDRV_USB_H_

lib/pbio/include/pbsys/host.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ typedef bool (*pbsys_host_stdin_event_callback_t)(uint8_t c);
2626
#if PBSYS_CONFIG_HOST
2727

2828
void pbsys_host_init(void);
29+
bool pbsys_host_is_connected(void);
2930
uint32_t pbsys_host_stdin_get_free(void);
3031
void pbsys_host_stdin_write(const uint8_t *data, uint32_t size);
3132
void pbsys_host_stdin_set_callback(pbsys_host_stdin_event_callback_t callback);
@@ -38,6 +39,7 @@ bool pbsys_host_tx_is_idle(void);
3839
#else // PBSYS_CONFIG_HOST
3940

4041
#define pbsys_host_init()
42+
#define pbsys_host_is_connected() false
4143
#define pbsys_host_stdin_get_free() 0
4244
#define pbsys_host_stdin_write(data, size) { (void)(data); (void)(size); }
4345
#define pbsys_host_stdin_set_callback(callback) { (void)(callback); }

lib/pbio/sys/hmi.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
#include <stddef.h>
1111
#include <stdint.h>
1212

13-
#include <contiki.h>
14-
1513
#include <pbdrv/bluetooth.h>
1614
#include <pbdrv/clock.h>
1715
#include <pbdrv/core.h>
@@ -22,6 +20,7 @@
2220
#include <pbio/light.h>
2321
#include <pbio/os.h>
2422
#include <pbsys/config.h>
23+
#include <pbsys/host.h>
2524
#include <pbsys/main.h>
2625
#include <pbsys/status.h>
2726
#include <pbsys/storage_settings.h>
@@ -265,8 +264,7 @@ pbio_error_t pbsys_hmi_await_program_selection(void) {
265264
}
266265

267266
// Don't time out while connected to host.
268-
if (pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_LE)) {
269-
// REVISIT: This should ask sys/host for "is connected" so it covers all connection types.
267+
if (pbsys_host_is_connected()) {
270268
time_start = pbdrv_clock_get_ms();
271269
}
272270

lib/pbio/sys/host.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ void pbsys_host_init(void) {
2222
pbsys_bluetooth_init();
2323
}
2424

25+
/**
26+
* Tests if the hub is connected to the host with BLE or USB.
27+
*
28+
* Connected implies an active connection to a Pybricks app, not just
29+
* physically plugged in.
30+
*
31+
* @return @c true if connection is active, else @c false.
32+
*/
33+
bool pbsys_host_is_connected(void) {
34+
return pbdrv_bluetooth_is_connected(PBDRV_BLUETOOTH_CONNECTION_PYBRICKS) ||
35+
pbdrv_usb_connection_is_active();
36+
}
37+
2538
// Publisher APIs. Pybricks Profile connections call these to push data to
2639
// a common stdin buffer.
2740

0 commit comments

Comments
 (0)