Skip to content

Commit 76579d2

Browse files
jfischer-nokartben
authored andcommitted
usb: device_next: allow to limit number or digits in serial number
Add Kconfig option to limit the length requested from HWINFO to a meaningful number of digits. Also, check the length returned by the HWINFO driver and rename the variables to a more suitable name. Signed-off-by: Johann Fischer <johann.fischer@nordicsemi.no>
1 parent 452a53b commit 76579d2

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

subsys/usb/device_next/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ config USBD_MSG_WORK_DELAY
9696
Message work may need to be delayed because the device stack is not
9797
yet ready to publish the message. The delay unit is milliseconds.
9898

99+
config USBD_HWINFO_DEVID_LENGTH
100+
int "The length of the device ID requested from HWINFO in bytes"
101+
depends on HWINFO
102+
range 8 128
103+
default 16
104+
help
105+
Each byte represents two digits in the serial number string
106+
descriptor. This option can be used to limit the length requested
107+
from HWINFO to a meaningful number of digits.
108+
99109
rsource "class/Kconfig"
100110
rsource "app/Kconfig.cdc_acm_serial"
101111

subsys/usb/device_next/usbd_ch9.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -533,13 +533,13 @@ static int sreq_get_desc_cfg(struct usbd_context *const uds_ctx,
533533
return 0;
534534
}
535535

536-
#define USBD_HWID_SN_MAX 32U
536+
#define USBD_SN_ASCII7_LENGTH (CONFIG_USBD_HWINFO_DEVID_LENGTH * 2)
537537

538538
/* Generate valid USB device serial number from hwid */
539-
static ssize_t get_sn_from_hwid(uint8_t sn[static USBD_HWID_SN_MAX])
539+
static ssize_t get_sn_from_hwid(uint8_t sn[static USBD_SN_ASCII7_LENGTH])
540540
{
541541
static const char hex[] = "0123456789ABCDEF";
542-
uint8_t hwid[USBD_HWID_SN_MAX / 2U];
542+
uint8_t hwid[USBD_SN_ASCII7_LENGTH / 2U];
543543
ssize_t hwid_len = -ENOSYS;
544544

545545
if (IS_ENABLED(CONFIG_HWINFO)) {
@@ -554,36 +554,37 @@ static ssize_t get_sn_from_hwid(uint8_t sn[static USBD_HWID_SN_MAX])
554554
return hwid_len;
555555
}
556556

557-
for (ssize_t i = 0; i < hwid_len; i++) {
557+
for (ssize_t i = 0; i < MIN(hwid_len, sizeof(hwid)); i++) {
558558
sn[i * 2] = hex[hwid[i] >> 4];
559559
sn[i * 2 + 1] = hex[hwid[i] & 0xF];
560560
}
561561

562-
return hwid_len * 2;
562+
return MIN(hwid_len, sizeof(hwid)) * 2;
563563
}
564564

565565
/* Copy and convert ASCII-7 string descriptor to UTF16-LE */
566566
static void string_ascii7_to_utf16le(struct usbd_desc_node *const dn,
567567
struct net_buf *const buf, const uint16_t wLength)
568568
{
569-
uint8_t hwid_sn[USBD_HWID_SN_MAX];
569+
uint8_t sn_ascii7_str[USBD_SN_ASCII7_LENGTH];
570570
struct usb_desc_header head = {
571571
.bDescriptorType = dn->bDescriptorType,
572572
};
573-
uint8_t *ascii7_str;
573+
const uint8_t *ascii7_str;
574574
size_t len;
575575
size_t i;
576576

577-
if (dn->str.utype == USBD_DUT_STRING_SERIAL_NUMBER && dn->str.use_hwinfo) {
578-
ssize_t hwid_len = get_sn_from_hwid(hwid_sn);
577+
if (IS_ENABLED(CONFIG_HWINFO) &&
578+
dn->str.utype == USBD_DUT_STRING_SERIAL_NUMBER && dn->str.use_hwinfo) {
579+
ssize_t sn_ascii7_str_len = get_sn_from_hwid(sn_ascii7_str);
579580

580-
if (hwid_len < 0) {
581+
if (sn_ascii7_str_len < 0) {
581582
errno = -ENOTSUP;
582583
return;
583584
}
584585

585-
head.bLength = sizeof(head) + hwid_len * 2;
586-
ascii7_str = hwid_sn;
586+
head.bLength = sizeof(head) + sn_ascii7_str_len * 2;
587+
ascii7_str = sn_ascii7_str;
587588
} else {
588589
head.bLength = dn->bLength;
589590
ascii7_str = (uint8_t *)dn->ptr;

0 commit comments

Comments
 (0)