Skip to content

Commit b0a6f24

Browse files
authored
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.
1 parent c7211db commit b0a6f24

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/rp2_common/pico_stdio_usb/stdio_usb_descriptors.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,14 @@ const uint8_t *tud_descriptor_configuration_cb(__unused uint8_t index) {
139139
}
140140

141141
const uint16_t *tud_descriptor_string_cb(uint8_t index, __unused uint16_t langid) {
142-
#define DESC_STR_MAX (20)
143-
static uint16_t desc_str[DESC_STR_MAX];
142+
#ifndef USBD_DESC_STR_MAX
143+
#define USBD_DESC_STR_MAX (20)
144+
#elif USBD_DESC_STR_MAX > 127
145+
#error USBD_DESC_STR_MAX too high (max is 127).
146+
#elif USBD_DESC_STR_MAX < 17
147+
#error USBD_DESC_STR_MAX too low (min is 17).
148+
#endif
149+
static uint16_t desc_str[USBD_DESC_STR_MAX];
144150

145151
// Assign the SN using the unique flash id
146152
if (!usbd_serial_str[0]) {
@@ -156,7 +162,7 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, __unused uint16_t langid
156162
return NULL;
157163
}
158164
const char *str = usbd_desc_str[index];
159-
for (len = 0; len < DESC_STR_MAX - 1 && str[len]; ++len) {
165+
for (len = 0; len < USBD_DESC_STR_MAX - 1 && str[len]; ++len) {
160166
desc_str[1 + len] = str[len];
161167
}
162168
}

0 commit comments

Comments
 (0)