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.
This commit is contained in:
hubiscode 2023-05-29 11:10:19 -04:00 committed by GitHub
parent c7211db2b3
commit b0a6f244f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -139,8 +139,14 @@ const uint8_t *tud_descriptor_configuration_cb(__unused uint8_t index) {
}
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 @@ const uint16_t *tud_descriptor_string_cb(uint8_t index, __unused uint16_t langid
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];
}
}