arduino / arduino/ArduinoCore-samd
USBDeviceClass::sendStringDescriptor is not compliant with MSC specs
- Dominant language
- C
- Stars
- 502
- Forks
- 740
- PR merge metrics
- No merged PRs in 30d
Description
Hi,
I've created Mass Storage Class Device that is pluggable (based on PluggableUSBModule).
However couple of adjustments were required to be made in USBDeviceClass.
One I've already reported ( https://github.com/arduino/ArduinoCore-samd/issues/302 ), USBDeviceClass::send() needs to write by EPX_SIZE bytes exactly (or less), not by EPX_SIZE-1 how it is now, when data length is >= EPX_SIZE .
Another issue is that MSC protocol adds some additional requirements to Serial Number requests.
It may request string descriptor payload of only one byte. The device should send one byte with size of its S/N descriptor payload without stall.
USB2.0 command verification program (USB20CV R1.5.4.1/2) may request payload of more than 256 bytes. The device should send its S/N descriptor payload with its actual size without stall.
I've added these handling into USBDeviceClass::sendStringDescriptor()
```
bool USBDeviceClass::sendStringDescriptor(const uint8_t *string, uint16_t maxlen)
{
if (maxlen == 0) return false; // USB2CV test TD.1.2 check 5
if (maxlen > 256) maxlen = 256; // USB2CV test TD.1.2 check 12
size_t slen = strlen((const char*)string) * 2 + 2;
if (slen > 256) slen = 256; // sanity check
if (maxlen > slen+2) maxlen = slen+2;
uint8_t buffer[maxlen];
buffer[0] = slen;
// USB2CV test TD.1.2 check 3
if (maxlen == 1) return USBDevice.sendControl(buffer, 1);
buffer[1] = 0x03; // USB2CV test TD.1.2 check 5
uint8_t i;
for (i = 2; i < maxlen && *string; i++) {
buffer[i++] = *string++;
if (i == maxlen) break;
buffer[i] = 0;
}
return USBDevice.sendControl(buffer, i);
}
```
I am not sure should I make a pull request or providing with this code is enough. Please advise.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start at USBDeviceClass::sendStringDescriptor() and inspect the related USBDeviceClass::send() behavior described in the issue. Compare the handling with the stated MSC and USB2CV requirements for maxlen 0, 1, and values above 256; done means descriptor requests return the correct payload without stalling.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c
- Domain
- embedded-iot
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100