Skip to content

Commit 3c1bdb0

Browse files
committed
Merge pull request arduino#4490 from sandeepmistry/sam-usb-send-string-descriptor
[SAM - USB Device] USB_SendStringDescriptor fixes
2 parents cd9a6ec + aeec344 commit 3c1bdb0

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

hardware/arduino/sam/cores/arduino/USB/USBCore.cpp

+14-8
Original file line numberDiff line numberDiff line change
@@ -258,15 +258,21 @@ int USBD_SendControl(uint8_t flags __attribute__ ((unused)), const void* d, uint
258258
// plain ASCII string but is sent out as UTF-16 with the
259259
// correct 2-byte prefix
260260
static bool USB_SendStringDescriptor(const uint8_t *string, int wLength) {
261-
uint16_t buff[64];
262-
int l = 1;
263-
wLength-=2;
264-
while (*string && wLength>0) {
265-
buff[l++] = (uint8_t)(*string++);
266-
wLength-=2;
261+
if (wLength < 2)
262+
return false;
263+
264+
uint8_t buffer[wLength];
265+
buffer[0] = strlen((const char*)string) * 2 + 2;
266+
buffer[1] = 0x03;
267+
268+
uint8_t i;
269+
for (i = 2; i < wLength && *string; i++) {
270+
buffer[i++] = *string++;
271+
if (i == wLength) break;
272+
buffer[i] = 0;
267273
}
268-
buff[0] = (3<<8) | (l*2);
269-
return USBD_SendControl(0, (uint8_t*)buff, l*2);
274+
275+
return USBD_SendControl(0, (uint8_t*)buffer, i);
270276
}
271277

272278
// Does not timeout or cross fifo boundaries

0 commit comments

Comments
 (0)