USB descriptor string length. (#1341)
Implement a mechanism to set the maximum string length used in
tud_descriptor_string_cb() by defining USBD_DESC_STR_MAX. If
USBD_DESC_STR_MAX is not defined, the behavior remains unchanged and the
previous default value of 20 is used. A compile time error is produced
if USBD_DESC_STR_MAX is higher than 127 since the length of the string
plus header is returned in a single byte as (2 * len + 2). Similarly, a
compile time error is generated if the length is defined as less than 17
in order to ensure that there is enough room for the 16-character serial
number plus header.
diff --git a/src/rp2_common/pico_stdio_usb/stdio_usb_descriptors.c b/src/rp2_common/pico_stdio_usb/stdio_usb_descriptors.c
index c3ce49b..2fb8616 100644
--- a/src/rp2_common/pico_stdio_usb/stdio_usb_descriptors.c
+++ b/src/rp2_common/pico_stdio_usb/stdio_usb_descriptors.c
@@ -139,8 +139,14 @@
}
const uint16_t *tud_descriptor_string_cb(uint8_t index, __unused uint16_t langid) {
- #define DESC_STR_MAX (20)
- static uint16_t desc_str[DESC_STR_MAX];
+#ifndef USBD_DESC_STR_MAX
+#define USBD_DESC_STR_MAX (20)
+#elif USBD_DESC_STR_MAX > 127
+#error USBD_DESC_STR_MAX too high (max is 127).
+#elif USBD_DESC_STR_MAX < 17
+#error USBD_DESC_STR_MAX too low (min is 17).
+#endif
+ static uint16_t desc_str[USBD_DESC_STR_MAX];
// Assign the SN using the unique flash id
if (!usbd_serial_str[0]) {
@@ -156,7 +162,7 @@
return NULL;
}
const char *str = usbd_desc_str[index];
- for (len = 0; len < DESC_STR_MAX - 1 && str[len]; ++len) {
+ for (len = 0; len < USBD_DESC_STR_MAX - 1 && str[len]; ++len) {
desc_str[1 + len] = str[len];
}
}