Skip to content

Commit 7ae98a4

Browse files
authored
Merge pull request #142 from okyeron/CustomSerialNumber
Set custom serial number
2 parents 7e75112 + d5b88fd commit 7ae98a4

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

src/arduino/Adafruit_USBD_Device.cpp

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ void Adafruit_USBD_Device::setProductDescriptor(const char *s) {
122122
_desc_str_arr[STRID_PRODUCT] = s;
123123
}
124124

125+
void Adafruit_USBD_Device::setSerialDescriptor(const char *s) {
126+
_desc_str_arr[STRID_SERIAL] = s;
127+
}
128+
125129
void Adafruit_USBD_Device::task(void) { tud_task(); }
126130

127131
bool Adafruit_USBD_Device::mounted(void) { return tud_mounted(); }
@@ -206,6 +210,7 @@ void Adafruit_USBD_Device::clearConfiguration(void) {
206210
_desc_str_arr[STRID_LANGUAGE] = (const char *)((uint32_t)USB_LANGUAGE);
207211
_desc_str_arr[STRID_MANUFACTURER] = USB_MANUFACTURER;
208212
_desc_str_arr[STRID_PRODUCT] = USB_PRODUCT;
213+
_desc_str_arr[STRID_SERIAL] = nullptr;
209214
// STRID_SERIAL is platform dependent
210215

211216
_desc_str_count = 4;
@@ -285,24 +290,30 @@ bool Adafruit_USBD_Device::begin(uint8_t rhport) {
285290
return true;
286291
}
287292

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

292-
for (uint8_t i = 0; i < serial_len; i++) {
293-
for (uint8_t j = 0; j < 2; j++) {
294-
const char nibble_to_hex[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
295-
'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'};
296305

297-
uint8_t nibble = (serial_id[i] >> (j * 4)) & 0xf;
298-
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+
}
299309
}
300-
}
301310

302-
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+
}
303315
}
304316

305-
static int strcpy_utf16(const char *s, uint16_t *buf, int bufsize);
306317
uint16_t const *Adafruit_USBD_Device::descriptor_string_cb(uint8_t index,
307318
uint16_t langid) {
308319
(void)langid;

src/arduino/Adafruit_USBD_Device.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class Adafruit_USBD_Device {
8585
void setLanguageDescriptor(uint16_t language_id);
8686
void setManufacturerDescriptor(const char *s);
8787
void setProductDescriptor(const char *s);
88+
void setSerialDescriptor(const char *s);
8889
uint8_t getSerialDescriptor(uint16_t *serial_utf16);
8990

9091
//------------- Control -------------//

0 commit comments

Comments
 (0)