Simvik

Parse a device descriptor

Read vendor, product, class and packet size from the 18-byte device descriptor. An easy embedded C problem (USB), run and graded in the browser.

easy · USB · usb · descriptors · endianness

During enumeration the host asks every USB device for its 18-byte device descriptor. Multi-byte fields are little-endian.

OffsetFieldSize
0bLength (must be 18)1
1bDescriptorType (must be 1)1
2bcdUSB2
4bDeviceClass1
7bMaxPacketSize01
8idVendor2
10idProduct2
17bNumConfigurations1

Write parse_device(d, len, info): return 0 and fill info if the descriptor is valid (at least 18 bytes received, length 18, type 1, packet size 8, 16, 32 or 64), or -1 otherwise.

Starter code

int parse_device(const uint8_t *d, int len, usb_dev_t *info) {
  (void)len;
  info->usb = (uint16_t)(d[2] << 8 | d[3]);
  info->cls = d[4];
  info->max_packet = d[7];
  info->vid = (uint16_t)(d[8] << 8 | d[9]);
  info->pid = (uint16_t)(d[10] << 8 | d[11]);
  info->configs = d[17];
  return 0;
}

Example tests

A USB serial adapter

uint8_t d[18] = { 18, 1, 0x00, 0x02, 0x02, 0, 0, 64, 0x83, 0x04, 0x40, 0x57, 0x00, 0x02, 1, 2, 3, 1 };
show(d, 18);

Expected output:

USB 2.00, VID 0483 PID 5740, class 2, EP0 64 bytes, 1 configuration(s)

Learn it first

  • USB · Enumeration, descriptors, endpoints, transfer types and device classes.