1 /*
2  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdio.h>
8 #include "freertos/FreeRTOS.h"
9 #include "freertos/semphr.h"
10 #include "unity.h"
11 #include "test_utils.h"
12 #include "test_usb_mock_classes.h"
13 #include "test_hcd_common.h"
14 
15 // --------------------------------------------------- Test Cases ------------------------------------------------------
16 
17 /*
18 Test HCD interrupt pipe URBs
19 Purpose:
20     - Test that an interrupt pipe can be created
21     - URBs can be created and enqueued to the interrupt pipe
22     - Interrupt pipe returns HCD_PIPE_EVENT_URB_DONE
23     - Test that URBs can be aborted when enqueued
24 
25 Procedure:
26     - Setup HCD and wait for connection
27     - Allocate default pipe and enumerate the device
28     - Setup interrupt pipe and allocate URBs
29     - Enqueue URBs, expect HCD_PIPE_EVENT_URB_DONE, and requeue
30     - Stop after fixed number of iterations
31     - Deallocate URBs
32     - Teardown
33 
34 Note: Some mice will NAK until it is moved, so try moving the mouse around if this test case gets stuck.
35 */
36 
37 #define TEST_HID_DEV_SPEED                  USB_SPEED_LOW
38 #define NUM_URBS                            3
39 #define URB_DATA_BUFF_SIZE                  4       //MPS is 4
40 #define NUM_URB_ITERS                       (NUM_URBS * 100)
41 
42 TEST_CASE("Test HCD interrupt pipe URBs", "[hcd][ignore]")
43 {
44     hcd_port_handle_t port_hdl = test_hcd_setup();  //Setup the HCD and port
45     usb_speed_t port_speed = test_hcd_wait_for_conn(port_hdl);  //Trigger a connection
46     TEST_ASSERT_EQUAL(TEST_HID_DEV_SPEED, TEST_HID_DEV_SPEED);
47     vTaskDelay(pdMS_TO_TICKS(100)); //Short delay send of SOF (for FS) or EOPs (for LS)
48 
49     hcd_pipe_handle_t default_pipe = test_hcd_pipe_alloc(port_hdl, NULL, 0, port_speed); //Create a default pipe (using a NULL EP descriptor)
50     uint8_t dev_addr = test_hcd_enum_device(default_pipe);
51 
52     //Allocate interrupt pipe and URBS
53     hcd_pipe_handle_t intr_pipe = test_hcd_pipe_alloc(port_hdl, &mock_hid_mouse_in_ep_desc, dev_addr, port_speed);
54     urb_t *urb_list[NUM_URBS];
55     for (int i = 0; i < NUM_URBS; i++) {
56         urb_list[i] = test_hcd_alloc_urb(0, URB_DATA_BUFF_SIZE);
57         urb_list[i]->transfer.num_bytes = URB_DATA_BUFF_SIZE;
58         urb_list[i]->transfer.context = URB_CONTEXT_VAL;
59     }
60 
61     //Enqueue URBs
62     for (int i = 0; i < NUM_URBS; i++) {
63         TEST_ASSERT_EQUAL(ESP_OK, hcd_urb_enqueue(intr_pipe, urb_list[i]));
64     }
65     int iter_count = NUM_URB_ITERS;
66     for (iter_count = NUM_URB_ITERS; iter_count > 0; iter_count--) {
67         //Wait for an URB to be done
68         test_hcd_expect_pipe_event(intr_pipe, HCD_PIPE_EVENT_URB_DONE);
69         //Dequeue the URB and check results
70         urb_t *urb = hcd_urb_dequeue(intr_pipe);
71         TEST_ASSERT_EQUAL(USB_TRANSFER_STATUS_COMPLETED, urb->transfer.status);
72         TEST_ASSERT_EQUAL(URB_CONTEXT_VAL, urb->transfer.context);
73         mock_hid_process_report((mock_hid_mouse_report_t *)urb->transfer.data_buffer, iter_count);
74         //Requeue URB
75         if (iter_count > NUM_URBS) {
76             TEST_ASSERT_EQUAL(ESP_OK, hcd_urb_enqueue(intr_pipe, urb));
77         }
78     }
79 
80     //Free URB list and pipe
81     for (int i = 0; i < NUM_URBS; i++) {
82         test_hcd_free_urb(urb_list[i]);
83     }
84     test_hcd_pipe_free(intr_pipe);
85     test_hcd_pipe_free(default_pipe);
86     //Clearnup
87     test_hcd_wait_for_disconn(port_hdl, false);
88     test_hcd_teardown(port_hdl);
89 }
90