Skip to content

Commit 9542739

Browse files
committed
pbio/protocol: add slots info to hub capabilities and status
In order to support multiple program slots, connected apps will need to know how many slots are available on the hub and what the currently selected slot is. These are added to the hub capabilities and status report respectively and the Pybricks Profile version is bumped to 1.5.0.
1 parent eb3e2e6 commit 9542739

File tree

8 files changed

+45
-14
lines changed

8 files changed

+45
-14
lines changed

lib/pbio/drv/bluetooth/pybricks_service_server.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ static uint16_t pybricks_service_read_callback(hci_con_handle_t con_handle, uint
8989
if (attribute_handle == pybricks_hub_capabilities_value_handle) {
9090
if (buffer && buffer_size >= PBIO_PYBRICKS_HUB_CAPABILITIES_VALUE_SIZE) {
9191
pbio_pybricks_hub_capabilities(buffer, pbio_int_math_min(att_server_get_mtu(con_handle) - 3, 512),
92-
PBSYS_CONFIG_APP_FEATURE_FLAGS, pbsys_storage_get_maximum_program_size());
92+
PBSYS_CONFIG_APP_FEATURE_FLAGS, pbsys_storage_get_maximum_program_size(), PBSYS_CONFIG_HMI_NUM_SLOTS);
9393
}
9494
return PBIO_PYBRICKS_HUB_CAPABILITIES_VALUE_SIZE;
9595
}

lib/pbio/drv/usb/stm32_usbd/usbd_desc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,8 @@ void USBD_Pybricks_Desc_Init(void) {
536536
pbio_pybricks_hub_capabilities(ptr,
537537
USBD_PYBRICKS_MAX_PACKET_SIZE - 1,
538538
PBSYS_CONFIG_APP_FEATURE_FLAGS,
539-
pbsys_storage_get_maximum_program_size());
539+
pbsys_storage_get_maximum_program_size(),
540+
PBSYS_CONFIG_HMI_NUM_SLOTS);
540541
ptr += PBIO_PYBRICKS_HUB_CAPABILITIES_VALUE_SIZE;
541542

542543
/* Update wTotalLength field in BOS Descriptor */

lib/pbio/include/pbio/protocol.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#define PBIO_PROTOCOL_VERSION_MAJOR 1
2525

2626
/** The minor version number for the protocol. */
27-
#define PBIO_PROTOCOL_VERSION_MINOR 4
27+
#define PBIO_PROTOCOL_VERSION_MINOR 5
2828

2929
/** The patch version number for the protocol. */
3030
#define PBIO_PROTOCOL_VERSION_PATCH 0
@@ -342,9 +342,9 @@ typedef enum {
342342
#define PBIO_PYBRICKS_STATUS_FLAG(status) (1 << status)
343343

344344
/** Size of status report event message in bytes. */
345-
#define PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE 6
345+
#define PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE 7
346346

347-
uint32_t pbio_pybricks_event_status_report(uint8_t *buf, uint32_t flags, pbio_pybricks_user_program_id_t program_id);
347+
uint32_t pbio_pybricks_event_status_report(uint8_t *buf, uint32_t flags, pbio_pybricks_user_program_id_t program_id, uint8_t slot);
348348

349349
/**
350350
* Application-specific feature flag supported by a hub.
@@ -391,12 +391,13 @@ typedef enum {
391391
void pbio_pybricks_hub_capabilities(uint8_t *buf,
392392
uint16_t max_char_size,
393393
pbio_pybricks_feature_flags_t feature_flags,
394-
uint32_t max_user_prog_size);
394+
uint32_t max_user_prog_size,
395+
uint8_t num_slots);
395396

396397
/**
397398
* Number of bytes in the Pybricks hub capabilities characteristic value.
398399
*/
399-
#define PBIO_PYBRICKS_HUB_CAPABILITIES_VALUE_SIZE 10
400+
#define PBIO_PYBRICKS_HUB_CAPABILITIES_VALUE_SIZE 11
400401

401402
extern const uint8_t pbio_pybricks_service_uuid[];
402403
extern const uint8_t pbio_pybricks_command_event_char_uuid[];

lib/pbio/include/pbsys/status.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
#include <pbio/protocol.h>
1616

17-
#define PBSYS_STATUS_REPORT_SIZE 6
17+
#define PBSYS_STATUS_REPORT_SIZE 7
1818

1919
/**
2020
* Status flag change.

lib/pbio/platform/prime_hub/pbsysconfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#define PBSYS_CONFIG_BLUETOOTH (1)
1111
#define PBSYS_CONFIG_BLUETOOTH_TOGGLE (1)
1212
#define PBSYS_CONFIG_BLUETOOTH_TOGGLE_BUTTON (512) // PBIO_BUTTON_RIGHT_UP, but enum value cannot be used here.
13-
#define PBSYS_CONFIG_HMI_NUM_SLOTS (0)
13+
#define PBSYS_CONFIG_HMI_NUM_SLOTS (5)
1414
#define PBSYS_CONFIG_HUB_LIGHT_MATRIX (1)
1515
#define PBSYS_CONFIG_HOST (1)
1616
#define PBSYS_CONFIG_MAIN (1)

lib/pbio/src/protocol/pybricks.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,22 @@ _Static_assert(NUM_PBIO_PYBRICKS_STATUS <= sizeof(uint32_t) * 8,
1717
*
1818
* The buffer must be at least ::PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE bytes.
1919
*
20+
* @since Pybricks Profile v1.0.0
21+
*
22+
* Program ID parameter was added in Pybricks Profile v1.4.0.
23+
* Slot parameter was added in Pybricks Profile v1.5.0.
24+
*
2025
* @param [in] buf The buffer to hold the binary data.
2126
* @param [in] flags The status flags.
22-
* @param [in] program_id Program identifier.
27+
* @param [in] program_id Program identifier of currently running program.
28+
* @param [in] slot The currently selected program slot.
2329
* @return The number of bytes written to @p buf.
2430
*/
25-
uint32_t pbio_pybricks_event_status_report(uint8_t *buf, uint32_t flags, pbio_pybricks_user_program_id_t program_id) {
31+
uint32_t pbio_pybricks_event_status_report(uint8_t *buf, uint32_t flags, pbio_pybricks_user_program_id_t program_id, uint8_t slot) {
2632
buf[0] = PBIO_PYBRICKS_EVENT_STATUS_REPORT;
2733
pbio_set_uint32_le(&buf[1], flags);
2834
buf[5] = program_id;
35+
buf[6] = slot;
2936
return PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE;
3037
}
3138

@@ -37,15 +44,22 @@ uint32_t pbio_pybricks_event_status_report(uint8_t *buf, uint32_t flags, pbio_py
3744
* @param [in] max_char_size The maximum characteristic value size (negotiated MTU - 3).
3845
* @param [in] feature_flags The feature flags.
3946
* @param [in] max_user_prog_size The maximum allowable size for the user program.
47+
* @param [in] num_slots The number of program slots available on the hub.
48+
*
49+
* @since Pybricks Profile v1.2.0
50+
*
51+
* num_slots was added in Pybricks Profile v1.5.0.
4052
*/
4153
void pbio_pybricks_hub_capabilities(uint8_t *buf,
4254
uint16_t max_char_size,
4355
pbio_pybricks_feature_flags_t feature_flags,
44-
uint32_t max_user_prog_size) {
56+
uint32_t max_user_prog_size,
57+
uint8_t num_slots) {
4558

4659
pbio_set_uint16_le(&buf[0], max_char_size);
4760
pbio_set_uint32_le(&buf[2], feature_flags);
4861
pbio_set_uint32_le(&buf[6], max_user_prog_size);
62+
buf[10] = num_slots;
4963
}
5064

5165
/**

lib/pbio/sys/bluetooth.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <pbsys/status.h>
2323
#include <pbsys/storage.h>
2424

25+
#include "hmi.h"
2526
#include "storage.h"
2627

2728
// REVISIT: this can be the negotiated MTU - 3 to allow for better throughput
@@ -254,6 +255,9 @@ static PT_THREAD(pbsys_bluetooth_monitor_status(struct pt *pt)) {
254255
static struct etimer timer;
255256
static uint32_t old_status_flags;
256257
static send_msg_t msg;
258+
#if PBSYS_CONFIG_HMI_NUM_SLOTS
259+
static uint8_t old_program_slot;
260+
#endif
257261

258262
PT_BEGIN(pt);
259263

@@ -267,7 +271,11 @@ static PT_THREAD(pbsys_bluetooth_monitor_status(struct pt *pt)) {
267271

268272
for (;;) {
269273
// wait for status to change or timeout
270-
PT_WAIT_UNTIL(pt, pbsys_status_get_flags() != old_status_flags || etimer_expired(&timer));
274+
PT_WAIT_UNTIL(pt, pbsys_status_get_flags() != old_status_flags ||
275+
#if PBSYS_CONFIG_HMI_NUM_SLOTS
276+
pbsys_hmi_get_selected_program_slot() != old_program_slot ||
277+
#endif
278+
etimer_expired(&timer));
271279

272280
etimer_restart(&timer);
273281

lib/pbio/sys/status.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,16 @@ static void pbsys_status_update_flag(pbio_pybricks_status_t status, bool set) {
5959
* @return The number of bytes written to @p buf.
6060
*/
6161
uint32_t pbsys_status_get_status_report(uint8_t *buf) {
62+
#if PBSYS_CONFIG_HMI_NUM_SLOTS
63+
uint8_t slot = pbsys_hmi_get_selected_program_slot();
64+
#else
65+
uint8_t slot = 0;
66+
#endif
67+
6268
_Static_assert(PBSYS_STATUS_REPORT_SIZE == PBIO_PYBRICKS_EVENT_STATUS_REPORT_SIZE,
6369
"size of status report does not match size of event");
64-
return pbio_pybricks_event_status_report(buf, pbsys_status.flags, pbsys_status.program_id);
70+
71+
return pbio_pybricks_event_status_report(buf, pbsys_status.flags, pbsys_status.program_id, slot);
6572
}
6673

6774
/**

0 commit comments

Comments
 (0)