1 /* Include necessary system files.  */
2 
3 #include "usbx_ux_test_cdc_ecm.h"
4 
5 static ULONG                error_callback_counter;
6 static UCHAR                buffer[64];
7 static ULONG                error_header_sim;
8 
count_error_callback(struct UX_TEST_ACTION_STRUCT * action,VOID * params)9 static void count_error_callback(struct UX_TEST_ACTION_STRUCT *action, VOID *params)
10 {
11 UX_TEST_ERROR_CALLBACK_PARAMS *error = (UX_TEST_ERROR_CALLBACK_PARAMS *)params;
12 
13     // printf("error trap #%d: 0x%x, 0x%x, 0x%x\n", __LINE__, error->system_level, error->system_context, error->error_code);
14     error_callback_counter ++;
15 }
16 
17 static UX_TEST_HCD_SIM_ACTION count_on_error_trap[] = {
18 {   .usbx_function = UX_TEST_OVERRIDE_ERROR_CALLBACK,
19     .action_func = count_error_callback,
20 },
21 {   0   }
22 };
23 
simulate_ip_header_error(struct UX_TEST_ACTION_STRUCT * action,VOID * params)24 static void simulate_ip_header_error(struct UX_TEST_ACTION_STRUCT *action, VOID *params)
25 {
26 
27 UX_TEST_OVERRIDE_UX_HCD_SIM_HOST_ENTRY_PARAMS *p = (UX_TEST_OVERRIDE_UX_HCD_SIM_HOST_ENTRY_PARAMS *)params;
28 UX_TRANSFER                                   *transfer = (UX_TRANSFER *)p->parameter;
29 UCHAR                                         *pretend_ptr = transfer->ux_transfer_request_data_pointer;
30 
31 
32     if (error_header_sim & 1)
33     {
34         *(pretend_ptr + 12) = 0x08;
35         *(pretend_ptr + 13) = 0x01;
36     }
37     else
38     {
39         *(pretend_ptr + 12) = 0x00;
40         *(pretend_ptr + 13) = 0x00;
41     }
42 
43     error_header_sim ++;
44 }
45 
46 static UX_TEST_ACTION bulk_transfer_replace[] =
47 {
48     {
49         .usbx_function = UX_TEST_OVERRIDE_UX_HCD_SIM_HOST_ENTRY,
50         .function = UX_HCD_TRANSFER_REQUEST,
51         .req_setup = UX_NULL,
52         .req_action = UX_TEST_MATCH_EP,
53         .req_ep_address = 0x02,
54         .req_actual_len = 64,
55         .req_status = UX_SUCCESS,
56         .action_func = simulate_ip_header_error,
57         .status = UX_SUCCESS,
58         .do_after = UX_FALSE,
59         .no_return = UX_TRUE,
60     },
61 { 0 },
62 };
63 
64 
65 /* Define what the initial system looks like.  */
66 #ifdef CTEST
test_application_define(void * first_unused_memory)67 void test_application_define(void *first_unused_memory)
68 #else
69 void usbx_ux_device_class_cdc_ecm_bulkout_thread_test_application_define(void *first_unused_memory)
70 #endif
71 {
72 
73     /* Inform user.  */
74     printf("Running ux_device_class_cdc_ecm_bulkout_thread Test................. ");
75 
76     stepinfo("\n");
77 
78     /* Override error trap. */
79     ux_test_link_hooks_from_array(count_on_error_trap);
80 
81     ux_test_cdc_ecm_initialize(first_unused_memory);
82 }
83 
post_init_host()84 static void post_init_host()
85 {
86 
87 UINT                        i;
88 UX_SLAVE_ENDPOINT           *endpoint;
89 USB_NETWORK_DEVICE_TYPE     *ux_nx_device;
90 NX_IP                       *ip;
91 
92 
93     stepinfo(">>>>>>>>>>>>>>>>>>> Test activate check\n");
94     UX_TEST_ASSERT(cdc_ecm_device != UX_NULL);
95 
96     /* Disable the other threads.  */
97     _ux_utility_thread_suspend(&cdc_ecm_device->ux_slave_class_cdc_ecm_bulkin_thread);
98     // _ux_utility_thread_suspend(&cdc_ecm_device->ux_slave_class_cdc_ecm_bulkout_thread);
99     _ux_utility_thread_suspend(&cdc_ecm_device->ux_slave_class_cdc_ecm_interrupt_thread);
100 
101     stepinfo(">>>>>>>>>>>>>>>>>>> Test UX allocate error\n");
102     error_callback_counter = 0;
103     for (i = 0; i < 2000; i ++)
104     {
105         write_packet_udp(&udp_socket_host, &packet_pool_host, DEVICE_IP_ADDRESS, DEVICE_SOCKET_PORT_UDP, global_basic_test_num_writes_host++, "host");
106         _ux_utility_thread_sleep(1);
107         if (error_callback_counter)
108             break;
109     }
110     UX_TEST_ASSERT(error_callback_counter);
111     for(;i ; i --)
112     {
113         read_packet_udp(&udp_socket_device, global_basic_test_num_reads_device++, "device");
114         _ux_utility_thread_sleep(1);
115     }
116 
117     stepinfo(">>>>>>>>>>>>>>>>>>> Test packet header error\n");
118     for (i = 0; i < 4; i ++)
119     {
120         ux_test_hcd_sim_host_set_actions(bulk_transfer_replace);
121         write_packet_udp(&udp_socket_host, &packet_pool_host, DEVICE_IP_ADDRESS, DEVICE_SOCKET_PORT_UDP, global_basic_test_num_writes_host++, "host");
122         _ux_utility_thread_sleep(1);
123     }
124 
125     // stepinfo(">>>>>>>>>>>>>>>>>>> Test connect to avoid post post operation\n");
126     // /* Connect. */
127     // ux_test_dcd_sim_slave_connect(UX_FULL_SPEED_DEVICE);
128     // ux_test_hcd_sim_host_connect(UX_FULL_SPEED_DEVICE);
129     // class_cdc_ecm_get_host();
130 
131     stepinfo(">>>>>>>>>>>>>>>>>>> Test IP instance attach\n");
132     ux_nx_device = (USB_NETWORK_DEVICE_TYPE *)cdc_ecm_device->ux_slave_class_cdc_ecm_network_handle;
133     ux_nx_device -> ux_network_device_link_status = UX_FALSE;
134     ip = ux_nx_device -> ux_network_device_ip_instance;
135     ux_nx_device -> ux_network_device_ip_instance = UX_NULL;
136     cdc_ecm_device -> ux_slave_class_cdc_ecm_packet_pool = UX_NULL;
137     write_packet_udp(&udp_socket_host, &packet_pool_host, DEVICE_IP_ADDRESS, DEVICE_SOCKET_PORT_UDP, global_basic_test_num_writes_host++, "host");
138     _ux_utility_delay_ms(1);
139     ux_nx_device -> ux_network_device_ip_instance = ip;
140     ux_nx_device -> ux_network_device_link_status = UX_TRUE;
141     _ux_utility_delay_ms(UX_DEVICE_CLASS_CDC_ECM_PACKET_POOL_INST_WAIT + 10);
142     UX_TEST_ASSERT(cdc_ecm_device -> ux_slave_class_cdc_ecm_packet_pool == ip -> nx_ip_default_packet_pool);
143 
144     stepinfo(">>>>>>>>>>>>>>>>>>> Test endpoint not ready error\n");
145     endpoint = cdc_ecm_device -> ux_slave_class_cdc_ecm_bulkout_endpoint;
146     cdc_ecm_device -> ux_slave_class_cdc_ecm_bulkout_endpoint = UX_NULL;
147     write_packet_udp(&udp_socket_host, &packet_pool_host, DEVICE_IP_ADDRESS, DEVICE_SOCKET_PORT_UDP, global_basic_test_num_writes_host++, "host");
148     _ux_utility_delay_ms(1);
149     cdc_ecm_device -> ux_slave_class_cdc_ecm_bulkout_endpoint = endpoint;
150     _ux_utility_delay_ms(UX_DEVICE_CLASS_CDC_ECM_LINK_CHECK_WAIT + 10);
151 
152     stepinfo(">>>>>>>>>>>>>>>>>>> post_init_host done\n");
153 }
154 
post_init_device()155 static void post_init_device()
156 {
157     stepinfo(">>>>>>>>>>>>>>>>>>> post_init_device empty\n");
158 }