|
43 | 43 | ****************************************************************************** |
44 | 44 | */ |
45 | 45 | /* Includes ------------------------------------------------------------------*/ |
| 46 | +#include <string.h> |
| 47 | + |
46 | 48 | #include "usbd_core.h" |
47 | 49 | #include "usbd_conf.h" |
48 | 50 | #include "usbd_pybricks.h" |
49 | 51 |
|
| 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 | + |
50 | 59 | /* Private typedef -----------------------------------------------------------*/ |
51 | 60 | /* Private define ------------------------------------------------------------*/ |
52 | 61 | #define USBD_VID 0x0483 |
|
57 | 66 | #define USBD_CONFIGURATION_FS_STRING "Pybricks Config" |
58 | 67 | #define USBD_INTERFACE_FS_STRING "Pybricks Interface" |
59 | 68 |
|
| 69 | +static const char firmware_version[] = PBIO_VERSION_STR; |
| 70 | +static const char software_version[] = PBIO_PROTOCOL_VERSION_STR; |
| 71 | + |
60 | 72 | #define DEVICE_ID1 (0x1FFF7A10) |
61 | 73 | #define DEVICE_ID2 (0x1FFF7A14) |
62 | 74 | #define DEVICE_ID3 (0x1FFF7A18) |
63 | 75 |
|
64 | 76 | #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) |
66 | 84 |
|
67 | 85 | /* USB Standard Device Descriptor */ |
68 | 86 | __ALIGN_BEGIN uint8_t USBD_DeviceDesc[USB_LEN_DEV_DESC] __ALIGN_END = { |
@@ -378,6 +396,57 @@ static uint8_t *USBD_Pybricks_BOSDescriptor(USBD_SpeedTypeDef speed, uint16_t *l |
378 | 396 | /* Prevent unused argument(s) compilation warning */ |
379 | 397 | UNUSED(speed); |
380 | 398 |
|
| 399 | + static uint8_t created = 0; |
| 400 | + uint8_t *ptr; |
| 401 | + |
| 402 | + /* Generate BOS Descriptor on first attempt */ |
| 403 | + if (!created) { |
| 404 | + created = 1; |
| 405 | + ptr = &USBD_BOSDesc[USB_SIZ_BOS_DESC_CONST]; |
| 406 | + |
| 407 | + /* Add firmware version */ |
| 408 | + *ptr++ = USB_SIZ_PLATFORM_HDR + sizeof(firmware_version); |
| 409 | + *ptr++ = USB_DEVICE_CAPABITY_TYPE; |
| 410 | + *ptr++ = 0x05; |
| 411 | + *ptr++ = 0x00; |
| 412 | + |
| 413 | + pbio_uuid128_le_copy(ptr, pbio_gatt_firmware_version_char_uuid_128); |
| 414 | + ptr += USB_SIZ_UUID; |
| 415 | + |
| 416 | + memcpy(ptr, firmware_version, sizeof(firmware_version)); |
| 417 | + ptr += sizeof(firmware_version); |
| 418 | + |
| 419 | + /* Add software (protocol) version */ |
| 420 | + *ptr++ = USB_SIZ_PLATFORM_HDR + sizeof(software_version); |
| 421 | + *ptr++ = USB_DEVICE_CAPABITY_TYPE; |
| 422 | + *ptr++ = 0x05; |
| 423 | + *ptr++ = 0x00; |
| 424 | + |
| 425 | + pbio_uuid128_le_copy(ptr, pbio_gatt_software_version_char_uuid_128); |
| 426 | + ptr += USB_SIZ_UUID; |
| 427 | + |
| 428 | + memcpy(ptr, software_version, sizeof(software_version)); |
| 429 | + ptr += sizeof(software_version); |
| 430 | + |
| 431 | + /* Add hub capabilities */ |
| 432 | + *ptr++ = USB_SIZ_PLATFORM_HDR + PBIO_PYBRICKS_HUB_CAPABILITIES_VALUE_SIZE; |
| 433 | + *ptr++ = USB_DEVICE_CAPABITY_TYPE; |
| 434 | + *ptr++ = 0x05; |
| 435 | + *ptr++ = 0x00; |
| 436 | + |
| 437 | + pbio_uuid128_le_copy(ptr, pbio_pybricks_hub_capabilities_char_uuid); |
| 438 | + ptr += USB_SIZ_UUID; |
| 439 | + |
| 440 | + pbio_pybricks_hub_capabilities(ptr, |
| 441 | + USBD_PYBRICKS_MAX_PACKET_SIZE - USB_SIZ_UUID, |
| 442 | + PBSYS_APP_HUB_FEATURE_FLAGS, |
| 443 | + PBSYS_PROGRAM_LOAD_MAX_PROGRAM_SIZE); |
| 444 | + ptr += PBIO_PYBRICKS_HUB_CAPABILITIES_VALUE_SIZE; |
| 445 | + |
| 446 | + /* Update bNumDeviceCaps field in BOS Descriptor */ |
| 447 | + USBD_BOSDesc[4] += 3; |
| 448 | + } |
| 449 | + |
381 | 450 | *length = sizeof(USBD_BOSDesc); |
382 | 451 | return (uint8_t *)USBD_BOSDesc; |
383 | 452 | } |
|
0 commit comments