# USB: Enumeration Driver - Local Test with emulated device based on TinyUSB
## Introduction
To verify the process how Enumeration Driver handles different scenarios the artificial approach of emulation USB device with TinyUSB is used.
This test helps to verify all possible scenarios of detaching device under the enumeration process.
## Key Terms
**Mocked Device** - A esp32-cpu based board, which represents the USB device, build on TinyUSB component (**tud_teardown()** feature is required, thus the version of the component should be higher than v0.17.0). The Mocked Device is used to emulate different scenarios with ability of controlling the USB Enumeration flow and detach the Mocked Device when necessary.
**Host** - A esp32-cpu based board with USB Host functionality. Used to verify the part of the USB Host Library - Enumeration Driver.
## Test Hardware requirements
For testing purpose two esp32-cpu based hardware with USB-OTG support are required (One for Host, one for Device (mocked dev)):
```
+----------------+
| USB Host Board |
| +------------+ | USB
| | USB-to-UART|========++
| +------------+ | ||
| +------------+ | \/
| | USB OTG | | \/
| +------------+ | || +-----------------+
+-------||-------+ ++==| |
/\ | PC Host |
/\ USB | (Test runner) |
+-------||-------+ ++==| |
| || | || +-----------------+
| +------------+ | /\
| | USB OTG | | /\
| +------------+ | ||
| +------------+ | ||
| | USB-to-UART|========++
| +------------+ | USB
| USB Dev Board |
| (mocked dev) |
+----------------+
```
## Mocked Device
Implementation could be found in [mock_dev.c](main/mock_dev.c) and [mock_dev.h](main/mock_dev.h).
The Mocked Device has a Final State Machine (FSM), which could be configured to stop at any stage and run the teardown process after. This flow represents the emulation of device detachment.
FSM is described in the **mock_dev_stage_t** structure and could be found in [mock_dev.h](main/mock_dev.h).
Available stages:
- Request Short Device Descriptor
- Request Full Device Descriptor
- Request Short Configuration Descriptor
- Request Full Configuration Descriptor
- Request Short LANGID Table
- Request Full LANDID Table
- Request Short Manufacturer String Descriptor
- Request Full Manufacturer String Descriptor
- Request Short Product String Descriptor
- Request Full Product String Descriptor
- Request Short Serial String Descriptor
- Request Full Serial String Descriptor
- Set configuration
Not covered stages:
- Device Reset (First and Second)
- Set Address
These stages are handled internally in TinyUSB and there is no user callbacks available. That means, that without specific changes in the TinyUSB component code, it is impossible to achieve emulation of device detaching during these stages.
### Mocked Device - Stages
To specify the descriptor, which should be used on every stage, there is a possibility to configure the pointer at every stage.
Example of fully-defined non-detachable Mocked Device configuration:
To verify the USB Host Library - Enumeration Driver, **usb_host_lib** example is used.
## Run Test
When all the hardware have been configured and prepared, the Test can be run via following commands. As the Test requires both Host and Device parts, the following description will be for Host and Device.
The description is provided, assuming that the test is run under Linux and Host board has a /dev/ttyACM0 path, Mocked Device has a /dev/ttyUSB0 path, and we are in the esp-idf root folder.
I (91000) CLASS: Getting Manufacturer string descriptor
Espressif
I (91010) CLASS: Getting Product string descriptor
Enum Testing
I (91020) CLASS: Getting Serial Number string descriptor
123456
```
Which means that the Mocked Device is connected to Host and the enumeration process was completed.
Different scenarios are being tested during the test run, so there could be errors in the log
That means, that the device emulates the specific behavior, to cause this error.
Important things are:
- During the test there must be no KERNEL PANIC, CPU reset etc. (not to miss the reset, it is better to configure the Panic handler behaviour to: Print registers and halt)
- The Host application should be responsive after the test. (In terms of usb_host_lib example, that means handle new device connection and show the descriptors from the connected device).
"Normal" test flow error are:
```
E (116330) USBH: Dev 28 EP 0 STALL
```
Or
```
E (96200) ENUM: Short dev desc has wrong bDescriptorType
E (96200) ENUM: [0:0] CHECK_SHORT_DEV_DESC FAILED
E (96820) ENUM: Full dev desc has wrong bDescriptorType
E (96820) ENUM: [0:0] CHECK_FULL_DEV_DESC FAILED
W (97470) ENUM: Device has more than 1 configuration
E (97470) USBH: Dev 16 EP 0 STALL
E (97470) ENUM: Bad transfer status 4: CHECK_SHORT_CONFIG_DESC
E (97470) ENUM: [0:0] CHECK_SHORT_CONFIG_DESC FAILED