Skip to content

Commit 24680b8

Browse files
committed
Add versions and capabilities to BOS descriptor
Adds versions and capabilities to the BOS Descriptor by appending dynamically-generated platform descriptors that use the same UUIDs that are used with BLE and contain the following values: * Firmware version * Software (protocol) version * Hub capabilities Signed-off-by: Nate Karstens <[email protected]>
1 parent f486635 commit 24680b8

File tree

3 files changed

+80
-1
lines changed

3 files changed

+80
-1
lines changed

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

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,19 @@
4343
******************************************************************************
4444
*/
4545
/* Includes ------------------------------------------------------------------*/
46+
#include <string.h>
47+
4648
#include "usbd_core.h"
4749
#include "usbd_conf.h"
4850
#include "usbd_pybricks.h"
4951

52+
#include "pbio/protocol.h"
53+
#include "pbio/version.h"
54+
#include "pbsys/app.h"
55+
#include "pbsys/program_load.h"
56+
#include "pbdrvconfig.h"
57+
#include "sys/config.h"
58+
5059
/* Private typedef -----------------------------------------------------------*/
5160
/* Private define ------------------------------------------------------------*/
5261
#define USBD_VID 0x0483
@@ -57,12 +66,21 @@
5766
#define USBD_CONFIGURATION_FS_STRING "Pybricks Config"
5867
#define USBD_INTERFACE_FS_STRING "Pybricks Interface"
5968

69+
static const char firmware_version[] = PBIO_VERSION_STR;
70+
static const char software_version[] = PBIO_PROTOCOL_VERSION_STR;
71+
6072
#define DEVICE_ID1 (0x1FFF7A10)
6173
#define DEVICE_ID2 (0x1FFF7A14)
6274
#define DEVICE_ID3 (0x1FFF7A18)
6375

6476
#define USB_SIZ_STRING_SERIAL 0x1A
65-
#define USB_SIZ_BOS_DESC 33
77+
#define USB_SIZ_BOS_DESC_CONST (5 + 28)
78+
#define USB_SIZ_UUID (128 / 8)
79+
#define USB_SIZ_PLATFORM_HDR (4 + USB_SIZ_UUID)
80+
#define USB_SIZ_BOS_DESC (USB_SIZ_BOS_DESC_CONST + \
81+
USB_SIZ_PLATFORM_HDR + sizeof(firmware_version) + \
82+
USB_SIZ_PLATFORM_HDR + sizeof(software_version) + \
83+
USB_SIZ_PLATFORM_HDR + PBIO_PYBRICKS_HUB_CAPABILITIES_VALUE_SIZE)
6684

6785
/* USB Standard Device Descriptor */
6886
__ALIGN_BEGIN static uint8_t USBD_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = {
@@ -362,6 +380,57 @@ static uint8_t *USBD_Pybricks_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *l
362380
/* Prevent unused argument(s) compilation warning */
363381
UNUSED(speed);
364382

383+
static uint8_t created = 0;
384+
uint8_t *ptr;
385+
386+
/* Generate BOS Descriptor on first attempt */
387+
if (!created) {
388+
created = 1;
389+
ptr = &USBD_BOSDesc[USB_SIZ_BOS_DESC_CONST];
390+
391+
/* Add firmware version */
392+
*ptr++ = USB_SIZ_PLATFORM_HDR + sizeof(firmware_version);
393+
*ptr++ = USB_DEVICE_CAPABITY_TYPE;
394+
*ptr++ = 0x05;
395+
*ptr++ = 0x00;
396+
397+
pbio_uuid128_le_copy(ptr, pbio_gatt_firmware_version_char_uuid_128);
398+
ptr += USB_SIZ_UUID;
399+
400+
memcpy(ptr, firmware_version, sizeof(firmware_version));
401+
ptr += sizeof(firmware_version);
402+
403+
/* Add software (protocol) version */
404+
*ptr++ = USB_SIZ_PLATFORM_HDR + sizeof(software_version);
405+
*ptr++ = USB_DEVICE_CAPABITY_TYPE;
406+
*ptr++ = 0x05;
407+
*ptr++ = 0x00;
408+
409+
pbio_uuid128_le_copy(ptr, pbio_gatt_software_version_char_uuid_128);
410+
ptr += USB_SIZ_UUID;
411+
412+
memcpy(ptr, software_version, sizeof(software_version));
413+
ptr += sizeof(software_version);
414+
415+
/* Add hub capabilities */
416+
*ptr++ = USB_SIZ_PLATFORM_HDR + PBIO_PYBRICKS_HUB_CAPABILITIES_VALUE_SIZE;
417+
*ptr++ = USB_DEVICE_CAPABITY_TYPE;
418+
*ptr++ = 0x05;
419+
*ptr++ = 0x00;
420+
421+
pbio_uuid128_le_copy(ptr, pbio_pybricks_hub_capabilities_char_uuid);
422+
ptr += USB_SIZ_UUID;
423+
424+
pbio_pybricks_hub_capabilities(ptr,
425+
USBD_PYBRICKS_MAX_PACKET_SIZE - USB_SIZ_UUID,
426+
PBSYS_APP_HUB_FEATURE_FLAGS,
427+
PBSYS_PROGRAM_LOAD_MAX_PROGRAM_SIZE);
428+
ptr += PBIO_PYBRICKS_HUB_CAPABILITIES_VALUE_SIZE;
429+
430+
/* Update bNumDeviceCaps field in BOS Descriptor */
431+
USBD_BOSDesc[4] += 3;
432+
}
433+
365434
*length = sizeof(USBD_BOSDesc);
366435
return (uint8_t *)USBD_BOSDesc;
367436
}

lib/pbio/include/pbio/protocol.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,9 @@ extern const uint8_t pbio_pybricks_hub_capabilities_char_uuid[];
301301

302302
extern const uint16_t pbio_gatt_device_info_service_uuid;
303303
extern const uint16_t pbio_gatt_firmware_version_char_uuid;
304+
extern const uint8_t pbio_gatt_firmware_version_char_uuid_128[];
304305
extern const uint16_t pbio_gatt_software_version_char_uuid;
306+
extern const uint8_t pbio_gatt_software_version_char_uuid_128[];
305307
extern const uint16_t pbio_gatt_pnp_id_char_uuid;
306308

307309
extern const uint8_t pbio_lwp3_hub_service_uuid[];

lib/pbio/src/protocol/pybricks.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,17 @@ const uint16_t pbio_gatt_device_info_service_uuid = 0x180A;
8787

8888
/** Bluetooth Firmware Version Characteristic UUID. */
8989
const uint16_t pbio_gatt_firmware_version_char_uuid = 0x2A26;
90+
const uint8_t pbio_gatt_firmware_version_char_uuid_128[] = {
91+
0x00, 0x00, 0x2A, 0x26, 0x00, 0x00, 0x10, 0x00,
92+
0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB,
93+
};
9094

9195
/** Bluetooth Software Version Characteristic UUID (Pybricks protocol version). */
9296
const uint16_t pbio_gatt_software_version_char_uuid = 0x2A28;
97+
const uint8_t pbio_gatt_software_version_char_uuid_128[] = {
98+
0x00, 0x00, 0x2A, 0x28, 0x00, 0x00, 0x10, 0x00,
99+
0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB,
100+
};
93101

94102
/** Bluetooth PnP ID Characteristic UUID. */
95103
const uint16_t pbio_gatt_pnp_id_char_uuid = 0x2A50;

0 commit comments

Comments
 (0)