Skip to content

Commit 0e28b6d

Browse files
committed
move to getSerialDescriptor
1 parent 270088f commit 0e28b6d

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed

src/arduino/Adafruit_USBD_Device.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -290,24 +290,30 @@ bool Adafruit_USBD_Device::begin(uint8_t rhport) {
290290
return true;
291291
}
292292

293+
static int strcpy_utf16(const char *s, uint16_t *buf, int bufsize);
294+
293295
uint8_t Adafruit_USBD_Device::getSerialDescriptor(uint16_t *serial_utf16) {
294-
uint8_t serial_id[16] __attribute__((aligned(4)));
295-
uint8_t const serial_len = TinyUSB_Port_GetSerialNumber(serial_id);
296296

297-
for (uint8_t i = 0; i < serial_len; i++) {
298-
for (uint8_t j = 0; j < 2; j++) {
299-
const char nibble_to_hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
300-
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
297+
if (!_desc_str_arr[STRID_SERIAL]) {
298+
uint8_t serial_id[16] __attribute__((aligned(4)));
299+
uint8_t const serial_len = TinyUSB_Port_GetSerialNumber(serial_id);
300+
301+
for (uint8_t i = 0; i < serial_len; i++) {
302+
for (uint8_t j = 0; j < 2; j++) {
303+
const char nibble_to_hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
304+
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
301305

302-
uint8_t nibble = (serial_id[i] >> (j * 4)) & 0xf;
303-
serial_utf16[1 + i * 2 + (1 - j)] = nibble_to_hex[nibble]; // UTF-16-LE
306+
uint8_t nibble = (serial_id[i] >> (j * 4)) & 0xf;
307+
serial_utf16[1 + i * 2 + (1 - j)] = nibble_to_hex[nibble]; // UTF-16-LE
308+
}
304309
}
305-
}
306310

307-
return 2 * serial_len;
311+
return 2 * serial_len;
312+
}else{
313+
return strcpy_utf16(_desc_str_arr[STRID_SERIAL], serial_utf16 + 1, 32);
314+
}
308315
}
309316

310-
static int strcpy_utf16(const char *s, uint16_t *buf, int bufsize);
311317
uint16_t const *Adafruit_USBD_Device::descriptor_string_cb(uint8_t index,
312318
uint16_t langid) {
313319
(void)langid;
@@ -321,11 +327,8 @@ uint16_t const *Adafruit_USBD_Device::descriptor_string_cb(uint8_t index,
321327
break;
322328

323329
case STRID_SERIAL:
324-
if (!_desc_str_arr[STRID_SERIAL]) {
325-
chr_count = getSerialDescriptor(_desc_str);
326-
break;
327-
}
328-
// else we have a serial string, treat as all others, fall through
330+
chr_count = getSerialDescriptor(_desc_str);
331+
break;
329332

330333
default:
331334
// Invalid index

0 commit comments

Comments
 (0)