1 /* This test is designed to test the ux_utility_timer_....  */
2 
3 #include <stdio.h>
4 #include "tx_api.h"
5 #include "ux_api.h"
6 #include "ux_system.h"
7 #include "ux_utility.h"
8 
9 #include "ux_host_stack.h"
10 #include "ux_device_stack.h"
11 
12 #include "ux_device_class_cdc_acm.h"
13 #include "ux_host_class_cdc_acm.h"
14 
15 #include "ux_host_class_dpump.h"
16 #include "ux_device_class_dpump.h"
17 
18 #include "ux_host_class_hid.h"
19 #include "ux_device_class_hid.h"
20 
21 #include "ux_host_class_storage.h"
22 #include "ux_device_class_storage.h"
23 
24 #include "ux_test_dcd_sim_slave.h"
25 #include "ux_test_hcd_sim_host.h"
26 #include "ux_test_utility_sim.h"
27 
28 
29 /* Define USBX test constants.  */
30 
31 #define UX_TEST_STACK_SIZE      4096
32 #define UX_TEST_BUFFER_SIZE     2048
33 #define UX_TEST_RUN             1
34 #define UX_TEST_MEMORY_SIZE     (64*1024)
35 
36 #define     LSB(x) ( (x) & 0x00ff)
37 #define     MSB(x) (((x) & 0xff00) >> 8)
38 
39 /* Configuration descriptor 9 bytes */
40 #define CFG_DESC(wTotalLength, bNumInterfaces, bConfigurationValue)\
41     /* Configuration 1 descriptor 9 bytes */\
42     0x09, 0x02, LSB(wTotalLength), MSB(wTotalLength),\
43     (bNumInterfaces), (bConfigurationValue), 0x00,\
44     0x40, 0x00,
45 #define CFG_DESC_LEN 9
46 
47 /* DPUMP interface descriptors. */
48 #define DPUMP_IFC_DESC(ifc, alt, nb_ep) \
49     /* Interface descriptor */\
50     0x09, 0x04, (ifc), (alt), (nb_ep), 0x99, 0x99, 0x99, 0x00,
51 
52 #define DPUMP_IFC_EP_DESC(epaddr, eptype, epsize) \
53     /* Endpoint descriptor */\
54     0x07, 0x05, (epaddr), (eptype), LSB(epsize), MSB(epsize), 0x01,
55 
56 #define DPUMP_IFC_DESC_ALL_LEN(nb_ep) (9 + (nb_ep) * 7)
57 
58 #define CFG_DESC_ALL_LEN (CFG_DESC_LEN + DPUMP_IFC_DESC_ALL_LEN(4))
59 
60 #define CFG_DESC_ALL \
61     CFG_DESC(CFG_DESC_ALL_LEN, 1, 1)\
62     DPUMP_IFC_DESC(0, 0, 4)\
63     DPUMP_IFC_EP_DESC(0x81, 2, 64)\
64     DPUMP_IFC_EP_DESC(0x02, 2, 64)\
65     DPUMP_IFC_EP_DESC(0x83, 1, 64)\
66     DPUMP_IFC_EP_DESC(0x84, 3, 64)\
67 
68 /* Define the counters used in the test application...  */
69 
70 static ULONG                           thread_0_counter;
71 static ULONG                           thread_1_counter;
72 static ULONG                           error_counter;
73 
74 static UCHAR                           error_callback_ignore = UX_FALSE;
75 static ULONG                           error_callback_counter;
76 
77 static UCHAR                           buffer[UX_TEST_BUFFER_SIZE];
78 
79 static TX_TIMER                        test_timer;
80 
81 /* Define USBX test global variables.  */
82 
83 static UX_HOST_CLASS                   *class_driver;
84 static UX_HOST_CLASS_DPUMP             *dpump;
85 static UX_SLAVE_CLASS_DPUMP            *dpump_slave = UX_NULL;
86 
87 static UCHAR device_framework_full_speed[] = {
88 
89     /* Device descriptor 18 bytes */
90     0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
91     0xec, 0x08, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
92     0x00, 0x01,
93 
94     CFG_DESC_ALL
95 };
96 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED sizeof(device_framework_full_speed)
97 
98 static UCHAR device_framework_high_speed[] = {
99 
100     /* Device descriptor */
101     0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
102     0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
103     0x03, 0x01,
104 
105     /* Device qualifier descriptor */
106     0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
107     0x01, 0x00,
108 
109     CFG_DESC_ALL
110 };
111 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED sizeof(device_framework_high_speed)
112 
113 /* String Device Framework :
114     Byte 0 and 1 : Word containing the language ID : 0x0904 for US
115     Byte 2       : Byte containing the index of the descriptor
116     Byte 3       : Byte containing the length of the descriptor string
117 */
118 
119 static UCHAR string_framework[] = {
120 
121     /* Manufacturer string descriptor : Index 1 */
122     0x09, 0x04, 0x01, 0x0c,
123     0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
124     0x6f, 0x67, 0x69, 0x63,
125 
126     /* Product string descriptor : Index 2 */
127     0x09, 0x04, 0x02, 0x0c,
128     0x44, 0x61, 0x74, 0x61, 0x50, 0x75, 0x6d, 0x70,
129     0x44, 0x65, 0x6d, 0x6f,
130 
131     /* Serial Number string descriptor : Index 3 */
132     0x09, 0x04, 0x03, 0x04,
133     0x30, 0x30, 0x30, 0x31
134 };
135 #define STRING_FRAMEWORK_LENGTH sizeof(string_framework)
136 
137     /* Multiple languages are supported on the device, to add
138        a language besides English, the unicode language code must
139        be appended to the language_id_framework array and the length
140        adjusted accordingly. */
141 static UCHAR language_id_framework[] = {
142 
143 /* English. */
144     0x09, 0x04
145 };
146 #define LANGUAGE_ID_FRAMEWORK_LENGTH sizeof(language_id_framework)
147 
148 /* Define prototypes for external Host Controller's (HCDs), classes and clients.  */
149 
150 static VOID                ux_test_instance_activate(VOID  *dpump_instance);
151 static VOID                ux_test_instance_deactivate(VOID *dpump_instance);
152 
153 UINT                       _ux_host_class_dpump_entry(UX_HOST_CLASS_COMMAND *command);
154 UINT                       ux_hcd_sim_initialize(UX_HCD *hcd);
155 UINT                       _ux_host_class_dpump_write(UX_HOST_CLASS_DPUMP *dpump, UCHAR * data_pointer,
156                                     ULONG requested_length, ULONG *actual_length);
157 UINT                       _ux_host_class_dpump_read (UX_HOST_CLASS_DPUMP *dpump, UCHAR *data_pointer,
158                                     ULONG requested_length, ULONG *actual_length);
159 
160 static TX_THREAD           ux_test_thread_simulation_0;
161 static TX_THREAD           ux_test_thread_simulation_1;
162 static void                ux_test_thread_simulation_0_entry(ULONG);
163 static void                ux_test_thread_simulation_1_entry(ULONG);
164 
165 
166 /* Define the ISR dispatch.  */
167 
168 extern VOID    (*test_isr_dispatch)(void);
169 
170 
171 /* Prototype for test control return.  */
172 
173 void  test_control_return(UINT status);
174 
175 /* Simulator actions. */
176 
177 static UX_TEST_HCD_SIM_ACTION endpoint0x83_create_del_skip[] = {
178 /* function, request to match,
179    port action, port status,
180    request action, request EP, request data, request actual length, request status,
181    status, additional callback,
182    no_return */
183 {   UX_HCD_CREATE_ENDPOINT, NULL,
184         UX_FALSE, 0,
185         UX_TEST_MATCH_EP, 0x83, UX_NULL, 0, 0,
186         UX_SUCCESS},
187 {   UX_HCD_CREATE_ENDPOINT, NULL,
188         UX_FALSE, 0,
189         UX_TEST_MATCH_EP, 0x83, UX_NULL, 0, 0,
190         UX_SUCCESS},
191 {   0   }
192 };
193 
194 /* Define the ISR dispatch routine.  */
195 
test_isr(void)196 static void    test_isr(void)
197 {
198 
199     /* For further expansion of interrupt-level testing.  */
200 }
201 
202 
error_callback(UINT system_level,UINT system_context,UINT error_code)203 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
204 {
205 
206     error_callback_counter ++;
207 
208     if (!error_callback_ignore)
209     {
210         {
211             /* Failed test.  */
212             printf("Error #%d, system_level: %d, system_context: %d, error_code: 0x%x\n", __LINE__, system_level, system_context, error_code);
213             // test_control_return(1);
214         }
215     }
216 }
217 
break_on_dpump_ready(VOID)218 static UINT break_on_dpump_ready(VOID)
219 {
220 
221 UINT             status;
222 UX_HOST_CLASS   *class;
223 
224     /* Find the main data pump container.  */
225     status =  ux_host_stack_class_get(_ux_system_host_class_dpump_name, &class);
226     if (status != UX_SUCCESS)
227         /* Do not break. */
228         return UX_SUCCESS;
229 
230     /* Find the instance. */
231     status =  ux_host_stack_class_instance_get(class, 0, (VOID **) &dpump);
232     if (status != UX_SUCCESS)
233         /* Do not break. */
234         return UX_SUCCESS;
235 
236     if (dpump -> ux_host_class_dpump_state != UX_HOST_CLASS_INSTANCE_LIVE)
237         /* Do not break. */
238         return UX_SUCCESS;
239 
240     return 1;
241 }
242 
break_on_removal(VOID)243 static UINT break_on_removal(VOID)
244 {
245 
246 UINT                     status;
247 UX_DEVICE               *device;
248 
249     status = ux_host_stack_device_get(0, &device);
250     if (status == UX_SUCCESS)
251         /* Do not break. */
252         return UX_SUCCESS;
253 
254     return 1;
255 }
256 
257 
test_ux_device_class_dpump_entry(UX_SLAVE_CLASS_COMMAND * command)258 static UINT test_ux_device_class_dpump_entry(UX_SLAVE_CLASS_COMMAND *command)
259 {
260     switch(command->ux_slave_class_command_request)
261     {
262         case UX_SLAVE_CLASS_COMMAND_INITIALIZE:
263         case UX_SLAVE_CLASS_COMMAND_QUERY:
264         case UX_SLAVE_CLASS_COMMAND_CHANGE:
265             return UX_SUCCESS;
266 
267         default:
268             return UX_NO_CLASS_MATCH;
269     }
270 }
271 
test_ux_host_class_dpump_entry(UX_HOST_CLASS_COMMAND * command)272 static UINT test_ux_host_class_dpump_entry(UX_HOST_CLASS_COMMAND *command)
273 {
274     switch (command -> ux_host_class_command_request)
275     {
276         case UX_HOST_CLASS_COMMAND_QUERY:
277         default:
278             return _ux_host_class_dpump_entry(command);
279     }
280 }
281 
282 /* Define what the initial system looks like.  */
283 
284 #ifdef CTEST
test_application_define(void * first_unused_memory)285 void test_application_define(void *first_unused_memory)
286 #else
287 void    usbx_ux_utility_timer_test_application_define(void *first_unused_memory)
288 #endif
289 {
290 
291 UINT                             status;
292 CHAR                            *stack_pointer;
293 CHAR                            *memory_pointer;
294 CHAR                            *rpool_start;
295 CHAR                            *cpool_start;
296 const CHAR                       flags[] = {
297     UX_REGULAR_MEMORY, UX_CACHE_SAFE_MEMORY, 0xFF
298 };
299 const CHAR                       expect_error[] = {
300     UX_FALSE, UX_FALSE, UX_TRUE
301 };
302 
303     /* Inform user.  */
304     printf("Running ux_utility_timer_... Test................................... ");
305 
306     /* Initialize the free memory pointer.  */
307     stack_pointer = (CHAR *) first_unused_memory;
308     memory_pointer = stack_pointer + (UX_TEST_STACK_SIZE * 2);
309 
310     rpool_start = memory_pointer;
311     cpool_start = memory_pointer + UX_TEST_MEMORY_SIZE;
312 
313     /* Initialize USBX Memory.  */
314     status =  ux_system_initialize(rpool_start, UX_TEST_MEMORY_SIZE, cpool_start, UX_TEST_MEMORY_SIZE);
315 
316     /* Check for error.  */
317     if (status != UX_SUCCESS)
318     {
319 
320         printf("ERROR #%d\n", __LINE__);
321         test_control_return(1);
322     }
323 
324     /* Register the error callback. */
325     _ux_utility_error_callback_register(error_callback);
326 
327     /* Create the simulation thread.  */
328     status =  tx_thread_create(&ux_test_thread_simulation_0, "test simulation", ux_test_thread_simulation_0_entry, 0,
329             stack_pointer, UX_TEST_STACK_SIZE,
330             20, 20, 1, TX_AUTO_START);
331 
332     /* Check for error.  */
333     if (status != TX_SUCCESS)
334     {
335 
336         printf("ERROR #%d\n", __LINE__);
337         test_control_return(1);
338     }
339 }
340 
ux_test_thread_simulation_0_entry(ULONG arg)341 static void  ux_test_thread_simulation_0_entry(ULONG arg)
342 {
343 UINT status;
344 
345     /* Ignore errors. */
346     error_callback_ignore = UX_TRUE;
347 
348     /* Timer create. */
349     status = ux_utility_timer_create(&test_timer, "test_timer", UX_NULL, 0, 100, 100, TX_NO_ACTIVATE);
350     if (status != UX_SUCCESS)
351     {
352 
353         printf("ERROR #%d: timer create fail\n", __LINE__);
354         test_control_return(1);
355     }
356 
357     /* Timer create again, expect error. */
358     status = ux_utility_timer_create(&test_timer, "test_timer", UX_NULL, 0, 100, 100, TX_NO_ACTIVATE);
359     if (status == UX_SUCCESS)
360     {
361 
362         printf("ERROR #%d: Expect timer create fail\n", __LINE__);
363         error_counter ++;
364     }
365 
366     /* Sleep for a tick to make sure everything is complete.  */
367     tx_thread_sleep(1);
368 
369     /* Check for errors from other threads.  */
370     if (error_counter)
371     {
372 
373         /* Test error.  */
374         printf("ERROR #%d: total %ld errors\n", __LINE__, error_counter);
375         test_control_return(1);
376     }
377     else
378     {
379 
380         /* Successful test.  */
381         printf("SUCCESS!\n");
382         test_control_return(0);
383     }
384 }