1 /* TODO: some common stuff from storage we might want to pull out:
2     -memory check - pretty good
3     -connect and disconnect
4     -getting class/instance
5 */
6 
7 #include "ux_api.h"
8 #include "ux_utility.h"
9 #include "ux_host_class_hid.h"
10 #include "ux_device_class_hid.h"
11 #include "ux_host_class_hid_remote_control.h"
12 
13 #include "ux_test.h"
14 #include "ux_test_actions.h"
15 #include "ux_test_dcd_sim_slave.h"
16 #include "ux_test_hcd_sim_host.h"
17 
18 
19 #define     LSB(x)              (x & 0xff)
20 #define     MSB(x)              ((x & 0xff00) >> 8)
21 
22 /* Define constants.  */
23 #define                             UX_DEMO_STACK_SIZE  	1024
24 #define                             UX_DEMO_MEMORY_SIZE     (64*1024)
25 
26 /* Define local/extern function prototypes.  */
27 static void                         test_main_thread_entry(ULONG);
28 
29 /* Define global data structures.  */
30 static UCHAR                                usbx_memory[UX_DEMO_MEMORY_SIZE + (UX_DEMO_STACK_SIZE * 2)];
31 static TX_THREAD                            test_main_thread;
32 static TX_THREAD                            test_slave_thread;
33 static UCHAR                                test_slave_thread_stack[4096];
34 static UX_HOST_CLASS                        *global_host_hid_class;
35 static UX_HOST_CLASS_HID                    *global_host_hid;
36 static UX_SLAVE_CLASS_HID                   *global_slave_hid;
37 static UX_SLAVE_CLASS_HID                   *global_slave_hid_persistent;
38 static UX_HOST_CLASS_HID_CLIENT             *global_host_hid_client;
39 static UX_HOST_CLASS_HID_REMOTE_CONTROL     *global_host_remote_control;
40 static UX_SLAVE_CLASS_HID_PARAMETER         global_slave_hid_parameter;
41 static UX_HCD                               *global_hcd;
42 
43 
44 static UCHAR hid_report_descriptor[] = {
45 
46     0x05, 0x0c,                    // USAGE_PAGE (Consumer Devices)
47     0x09, 0x01,                    // USAGE (Consumer Control)
48     0xa1, 0x01,                    // COLLECTION (Application)
49     0x09, 0x02,                    //   USAGE (Numeric Key Pad)
50     0xa1, 0x02,                    //   COLLECTION (Logical)
51     0x05, 0x09,                    //     USAGE_PAGE (Button)
52     0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)
53     0x29, 0x0a,                    //     USAGE_MAXIMUM (Button 10)
54     0x15, 0x01,                    //     LOGICAL_MINIMUM (1)
55     0x25, 0x0a,                    //     LOGICAL_MAXIMUM (10)
56     0x75, 0x04,                    //     REPORT_SIZE (4)
57     0x95, 0x01,                    //     REPORT_COUNT (1)
58     0x81, 0x00,                    //     INPUT (Data,Ary,Abs)
59     0xc0,                          //   END_COLLECTION
60     0x05, 0x0c,                    //   USAGE_PAGE (Consumer Devices)
61     0x09, 0x86,                    //   USAGE (Channel)
62     0x09, 0xe0,                    //   USAGE (Volume)
63     0x15, 0xff,                    //   LOGICAL_MINIMUM (-1)
64     0x25, 0x01,                    //   LOGICAL_MAXIMUM (1)
65     0x75, 0x02,                    //   REPORT_SIZE (2)
66     0x95, 0x02,                    //   REPORT_COUNT (2)
67     0x81, 0x46,                    //   INPUT (Data,Var,Rel,Null)
68     0xc0                           // END_COLLECTION
69 };
70 
71 static UCHAR device_framework_full_speed[] = {
72 
73     /* Device descriptor */
74         0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
75         0x81, 0x0A, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
76         0x00, 0x01,
77 
78     /* Configuration descriptor */
79         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
80         0x32,
81 
82     /* Interface descriptor */
83         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
84         0x00,
85 
86     /* HID descriptor */
87         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(sizeof(hid_report_descriptor)),
88         MSB(sizeof(hid_report_descriptor)),
89 
90     /* Endpoint descriptor (Interrupt) */
91         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
92 
93     };
94 
95 #define FULL_SPEED_REPORT_DESCRIPTOR_LENGTH_LSB_POS  (0x12 + 0x09 + 0x09 + 0x7)
96 #define FULL_SPEED_REPORT_DESCRIPTOR_LENGTH_MSB_POS  (FULL_SPEED_REPORT_DESCRIPTOR_LENGTH_LSB_POS + 1)
97 
98 
99 static UCHAR device_framework_high_speed[] = {
100 
101     /* Device descriptor */
102         0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
103         0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
104         0x03, 0x01,
105 
106     /* Device qualifier descriptor */
107         0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
108         0x01, 0x00,
109 
110     /* Configuration descriptor */
111         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
112         0x32,
113 
114     /* Interface descriptor */
115         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
116         0x00,
117 
118     /* HID descriptor */
119         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(sizeof(hid_report_descriptor)),
120         MSB(sizeof(hid_report_descriptor)),
121 
122     /* Endpoint descriptor (Interrupt) */
123         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
124 
125     };
126 
127 #define HIGH_SPEED_REPORT_DESCRIPTOR_LENGTH_LSB_POS  (0x12 + 0x0a + 0x09 + 0x09 + 0x7)
128 #define HIGH_SPEED_REPORT_DESCRIPTOR_LENGTH_MSB_POS  (HIGH_SPEED_REPORT_DESCRIPTOR_LENGTH_LSB_POS + 1)
129 
130 
131     /* String Device Framework :
132      Byte 0 and 1 : Word containing the language ID : 0x0904 for US
133      Byte 2       : Byte containing the index of the descriptor
134      Byte 3       : Byte containing the length of the descriptor string
135     */
136 
137 #define STRING_FRAMEWORK_LENGTH 40
138 static UCHAR string_framework[] = {
139 
140     /* Manufacturer string descriptor : Index 1 */
141         0x09, 0x04, 0x01, 0x0c,
142         0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
143         0x6f, 0x67, 0x69, 0x63,
144 
145     /* Product string descriptor : Index 2 */
146         0x09, 0x04, 0x02, 0x0c,
147         0x55, 0x53, 0x42, 0x20, 0x4b, 0x65, 0x79, 0x62,
148         0x6f, 0x61, 0x72, 0x64,
149 
150     /* Serial Number string descriptor : Index 3 */
151         0x09, 0x04, 0x03, 0x04,
152         0x30, 0x30, 0x30, 0x31
153     };
154 
155 
156     /* Multiple languages are supported on the device, to add
157        a language besides english, the unicode language code must
158        be appended to the language_id_framework array and the length
159        adjusted accordingly. */
160 #define LANGUAGE_ID_FRAMEWORK_LENGTH 2
161 static UCHAR language_id_framework[] = {
162 
163     /* English. */
164         0x09, 0x04
165     };
166 
167 /* Functions from storage basic test. */
168 
get_global_hid_values()169 static VOID get_global_hid_values()
170 {
171 
172     UX_TEST_CHECK_SUCCESS(ux_host_stack_class_get(_ux_system_host_class_hid_name, &global_host_hid_class));
173     UX_TEST_CHECK_SUCCESS(ux_host_stack_class_instance_get(global_host_hid_class, 0, (void **) &global_host_hid));
174     UX_TEST_ASSERT(global_host_hid -> ux_host_class_hid_state == UX_HOST_CLASS_INSTANCE_LIVE);
175     global_host_hid_client = global_host_hid -> ux_host_class_hid_client;
176     UX_TEST_ASSERT(global_host_hid_client -> ux_host_class_hid_client_local_instance != UX_NULL);
177     global_host_remote_control = (UX_HOST_CLASS_HID_REMOTE_CONTROL *)global_host_hid_client -> ux_host_class_hid_client_local_instance;
178 }
179 
wait_for_enum_completion_and_get_global_hid_values()180 static VOID wait_for_enum_completion_and_get_global_hid_values()
181 {
182 
183     ux_test_wait_for_enum_thread_completion();
184     get_global_hid_values();
185 }
186 
187 /* Returns whether or not the enumeration succeeded. */
connect_host_and_slave()188 static VOID connect_host_and_slave()
189 {
190 
191     ux_test_connect_slave_and_host_wait_for_enum_completion();
192     get_global_hid_values();
193 }
194 
195 /* General HID utilities. */
196 
set_report_descriptor(UCHAR * report_descriptor,ULONG report_descriptor_length)197 void set_report_descriptor(UCHAR *report_descriptor, ULONG report_descriptor_length)
198 {
199 
200     /* Should only be called if the host and slave is disconnected. */
201     UX_TEST_ASSERT(global_hcd->ux_hcd_nb_devices == 0);
202     UX_TEST_ASSERT(_ux_system_slave->ux_system_slave_device.ux_slave_device_state == UX_DEVICE_RESET);
203 
204     global_slave_hid_persistent->ux_device_class_hid_report_address = report_descriptor;
205     global_slave_hid_persistent->ux_device_class_hid_report_length = report_descriptor_length;
206 
207     device_framework_full_speed[FULL_SPEED_REPORT_DESCRIPTOR_LENGTH_LSB_POS] = LSB(report_descriptor_length);
208     device_framework_full_speed[FULL_SPEED_REPORT_DESCRIPTOR_LENGTH_MSB_POS] = MSB(report_descriptor_length);
209 
210     device_framework_high_speed[HIGH_SPEED_REPORT_DESCRIPTOR_LENGTH_LSB_POS] = LSB(report_descriptor_length);
211     device_framework_high_speed[HIGH_SPEED_REPORT_DESCRIPTOR_LENGTH_MSB_POS] = MSB(report_descriptor_length);
212 }
213 
slave_hid_callback(UX_SLAVE_CLASS_HID * hid,UX_SLAVE_CLASS_HID_EVENT * event)214 UINT slave_hid_callback(UX_SLAVE_CLASS_HID *hid, UX_SLAVE_CLASS_HID_EVENT *event)
215 {
216     return 0;
217 }
218 
slave_class_hid_instance_activate(VOID * instance)219 VOID slave_class_hid_instance_activate(VOID *instance)
220 {
221 
222     if (global_slave_hid_persistent)
223         UX_TEST_ASSERT(global_slave_hid_persistent == instance);
224     global_slave_hid_persistent = instance;
225 
226     global_slave_hid = instance;
227 }
228 
slave_class_hid_instance_deactivate(VOID * instance)229 VOID slave_class_hid_instance_deactivate(VOID *instance)
230 {
231 
232     global_slave_hid = UX_NULL;
233 }
234 
235 /* Define what the initial system looks like.  */
236 
237 #ifdef CTEST
test_application_define(void * first_unused_memory)238 void test_application_define(void *first_unused_memory)
239 #else
240 void    usbx_hid_remote_control_tests_application_define(void *first_unused_memory)
241 #endif
242 {
243 
244 UINT    status;
245 CHAR    *stack_pointer;
246 CHAR    *memory_pointer;
247 
248 
249     /* Inform user.  */
250     printf("Running HID Remote Control Tests.................................... ");
251 
252     stepinfo("\n");
253 
254     /* Initialize the free memory pointer */
255     stack_pointer = (CHAR *) usbx_memory;
256     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
257 
258     /* Initialize USBX. Memory */
259     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
260 
261     /* Check for error.  */
262     if (status != UX_SUCCESS)
263     {
264 
265         printf("Error on line %d\n", __LINE__);
266         test_control_return(1);
267     }
268 
269     /* Register the error callback. */
270     _ux_utility_error_callback_register(ux_test_error_callback);
271 
272     /* The code below is required for installing the host portion of USBX */
273     status =  ux_host_stack_initialize(UX_NULL);
274     if (status != UX_SUCCESS)
275     {
276 
277         printf("Error on line %d\n", __LINE__);
278         test_control_return(1);
279     }
280 
281     status =  ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
282     if (status != UX_SUCCESS)
283     {
284 
285         printf("Error on line %d\n", __LINE__);
286         test_control_return(1);
287     }
288 
289     /* Register the HID client(s).  */
290     status =  ux_host_class_hid_client_register(_ux_system_host_class_hid_client_remote_control_name, ux_host_class_hid_remote_control_entry);
291     if (status != UX_SUCCESS)
292     {
293 
294         printf("Error on line %d\n", __LINE__);
295         test_control_return(1);
296     }
297 
298     /* The code below is required for installing the device portion of USBX. No call back for
299        device status change in this example. */
300     status =  ux_device_stack_initialize(device_framework_high_speed, sizeof(device_framework_high_speed),
301                                          device_framework_full_speed, sizeof(device_framework_full_speed),
302                                          string_framework, STRING_FRAMEWORK_LENGTH,
303                                          language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH, UX_NULL);
304     if(status!=UX_SUCCESS)
305     {
306 
307         printf("Error on line %d\n", __LINE__);
308         test_control_return(1);
309     }
310 
311     /* Initialize the hid class parameters for a mouse.  */
312     global_slave_hid_parameter.ux_slave_class_hid_instance_activate =         slave_class_hid_instance_activate;
313     global_slave_hid_parameter.ux_slave_class_hid_instance_deactivate =       slave_class_hid_instance_deactivate;
314     global_slave_hid_parameter.ux_device_class_hid_parameter_report_address = hid_report_descriptor;
315     global_slave_hid_parameter.ux_device_class_hid_parameter_report_length  = sizeof(hid_report_descriptor);
316     global_slave_hid_parameter.ux_device_class_hid_parameter_callback       = slave_hid_callback;
317 
318     /* Initilize the device hid class. The class is connected with interface 2. */
319     status =  ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
320                                              1, 2, (VOID *)&global_slave_hid_parameter);
321     if(status!=UX_SUCCESS)
322     {
323 
324         printf("Error on line %d\n", __LINE__);
325         test_control_return(1);
326     }
327 
328 
329     /* Initialize the simulated device controller.  */
330     status =  _ux_dcd_sim_slave_initialize();
331 
332     /* Check for error.  */
333     if (status != UX_SUCCESS)
334     {
335 
336         printf("Error on line %d\n", __LINE__);
337         test_control_return(1);
338     }
339 
340     /* Register all the USB host controllers available in this system */
341     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
342 
343     /* Check for error.  */
344     if (status != UX_SUCCESS)
345     {
346 
347         printf("Error on line %d\n", __LINE__);
348         test_control_return(1);
349     }
350 
351     global_hcd = &_ux_system_host->ux_system_host_hcd_array[0];
352 
353     /* Create the main host simulation thread.  */
354     status =  tx_thread_create(&test_main_thread, "test_main_thread", test_main_thread_entry, 0,
355             stack_pointer, UX_DEMO_STACK_SIZE,
356             20, 20, 1, TX_AUTO_START);
357 
358     /* Check for error.  */
359     if (status != TX_SUCCESS)
360     {
361 
362         printf("Error on line %d\n", __LINE__);
363         test_control_return(1);
364     }
365 }
366 
367 /* basic_test resources */
368 
basic_test_get_next_channel_volume_value(ULONG value)369 static UINT basic_test_get_next_channel_volume_value(ULONG value)
370 {
371 
372     if (value == 0x03)
373         return 0x00;
374     else if (value == 0x00)
375         return 0x01;
376     else if (value == 0x01)
377         return 0x03;
378 
379     return 0xff;
380 }
381 
basic_test_slave_thread_entry(ULONG arg)382 static void basic_test_slave_thread_entry(ULONG arg)
383 {
384 
385 UX_SLAVE_CLASS_HID_EVENT    hid_event;
386 ULONG                       value;
387 UINT                        max_num_loops;
388 
389 
390     /* reset the HID event structure.  */
391     ux_utility_memory_set(&hid_event, 0, sizeof(UX_SLAVE_CLASS_HID_EVENT));
392 
393     /* Set length of event.  */
394     hid_event.ux_device_class_hid_event_length = 1;
395 
396     /* Set initial keypad value. */
397     hid_event.ux_device_class_hid_event_buffer[0] = 0x01;
398 
399     /* Set initial channel value. */
400     hid_event.ux_device_class_hid_event_buffer[0] |= (0x03 << 4);
401 
402     /* Set initial volume value. */
403     hid_event.ux_device_class_hid_event_buffer[0] |= (0x01 << 6);
404 
405     max_num_loops = 2*UX_HOST_CLASS_HID_REMOTE_CONTROL_USAGE_ARRAY_LENGTH;
406     while (max_num_loops--)
407     {
408 
409         stepinfo("    slave - max_num_loops: %d\n", max_num_loops);
410 
411         /* Wait for host to receive. */
412         ux_utility_thread_sleep(2);
413 
414         /* Set the mouse event.  */
415         UX_TEST_CHECK_SUCCESS(ux_device_class_hid_event_set(global_slave_hid, &hid_event));
416 
417         /* Change keypad value. */
418         value = hid_event.ux_device_class_hid_event_buffer[0] & 0x0f;
419         if (value >= 0x0a)
420             value = 0x01;
421         else
422             value++;
423 
424         hid_event.ux_device_class_hid_event_buffer[0] &= 0xf0;
425         hid_event.ux_device_class_hid_event_buffer[0] |= value;
426 
427         /* Change channel value. */
428         value = ((hid_event.ux_device_class_hid_event_buffer[0] & 0x30) >> 4);
429         hid_event.ux_device_class_hid_event_buffer[0] &= ~0x30;
430         hid_event.ux_device_class_hid_event_buffer[0] |= (basic_test_get_next_channel_volume_value(value) << 4);
431 
432         /* Change volume value. */
433         value = ((hid_event.ux_device_class_hid_event_buffer[0] & 0xc0) >> 6);
434         hid_event.ux_device_class_hid_event_buffer[0] &= ~0xc0;
435         hid_event.ux_device_class_hid_event_buffer[0] |= (basic_test_get_next_channel_volume_value(value) << 6);
436     }
437 }
438 
basic_test()439 static void basic_test()
440 {
441 
442 UINT    max_num_loops;
443 ULONG   usage;
444 ULONG   value;
445 ULONG   expected_keypad_value;
446 ULONG   expected_channel_value;
447 ULONG   expected_volume_value;
448 
449 
450     stepinfo("basic_test\n");
451 
452     UX_TEST_CHECK_SUCCESS(tx_thread_create(&test_slave_thread, "test_slave_thread", basic_test_slave_thread_entry, 0,
453                                            test_slave_thread_stack, UX_DEMO_STACK_SIZE,
454                                            20, 20, 1, TX_AUTO_START));
455 
456     /* Initialize expected values. */
457     expected_keypad_value = 0x01;
458     expected_channel_value = 0x03;
459     expected_volume_value = 0x01;
460 
461     /* Set number of successful loops to execute. */
462     max_num_loops = 2*UX_HOST_CLASS_HID_REMOTE_CONTROL_USAGE_ARRAY_LENGTH;
463     while (max_num_loops--)
464     {
465 
466         stepinfo("    host - max_num_loops:  %d\n", max_num_loops);
467 
468         /* Wait for an event from the device. Each event should have 3 usages. The first is the keypad. */
469         while (ux_host_class_hid_remote_control_usage_get(global_host_remote_control, &usage, &value) != UX_SUCCESS)
470             tx_thread_sleep(1);
471 
472         if (usage != (0x00090000 | expected_keypad_value) || value != expected_keypad_value)
473         {
474 
475             printf("Error on line %d. usage: 0x%lx, expected usage: 0x%lx, value: 0x%lx, expected_keypad_value: 0x%lx\n",
476                    __LINE__, usage, 0x00090000 | expected_keypad_value, value, expected_keypad_value);
477             test_control_return(1);
478         }
479 
480         if (++expected_keypad_value > 0x0a)
481             expected_keypad_value = 1;
482 
483         /* Get the channel value. */
484         ux_host_class_hid_remote_control_usage_get(global_host_remote_control, &usage, &value);
485         if (usage != 0x000c0086 || value != expected_channel_value)
486         {
487 
488             printf("Error on line %d\n", __LINE__);
489             test_control_return(1);
490         }
491 
492         expected_channel_value = basic_test_get_next_channel_volume_value(value);
493 
494         /* Get the volume value. */
495         ux_host_class_hid_remote_control_usage_get(global_host_remote_control, &usage, &value);
496         if (usage != 0x000c00e0 || value != expected_volume_value)
497         {
498 
499             printf("Error on line %d\n", __LINE__);
500             test_control_return(1);
501         }
502 
503         expected_volume_value = basic_test_get_next_channel_volume_value(value);
504     }
505 
506     UX_TEST_CHECK_SUCCESS(tx_thread_terminate(&test_slave_thread));
507     UX_TEST_CHECK_SUCCESS(tx_thread_delete(&test_slave_thread));
508 }
509 
510 /* event_overflow_test resources */
511 
512 #define EBT_MAX_EVENTS ((UX_HOST_CLASS_HID_REMOTE_CONTROL_USAGE_ARRAY_LENGTH/2) - 1)
513 #define EBT_NUM_OVERFLOW_EVENTS 100
514 
515 static TX_SEMAPHORE ebt_slave_wakes_host_semaphore;
516 static TX_SEMAPHORE ebt_host_wakes_slave_semaphore;
517 
518 static UCHAR host_event_buffer_test_hid_report_descriptor[] = {
519 
520     0x05, 0x0c,                    // USAGE_PAGE (Consumer Devices)
521     0x09, 0x01,                    // USAGE (Consumer Control)
522     0xa1, 0x01,                    // COLLECTION (Application)
523     0x05, 0x0c,                    //   USAGE_PAGE (Consumer Devices)
524     0x09, 0xe0,                    //   USAGE (Volume)
525     0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
526     0x25, 0xff,                    //   LOGICAL_MAXIMUM (255)
527     0x75, 0x08,                    //   REPORT_SIZE (8)
528     0x95, 0x01,                    //   REPORT_COUNT (1)
529     0x81, 0x46,                    //   INPUT (Data,Var,Rel,Null)
530     0xc0                           // END_COLLECTION
531 };
532 
event_buffer_test_slave_thread_entry(ULONG arg)533 static void event_buffer_test_slave_thread_entry(ULONG arg)
534 {
535 
536 UX_SLAVE_CLASS_HID_EVENT    hid_event = { 0 };
537 UINT                        i;
538 
539 
540     /* Add the exact amount. Remember, the host's usage array consists of pairs,
541        hence the divide by two. */
542     for (i = 0; i < EBT_MAX_EVENTS; i++)
543     {
544 
545         /* Setup and send event. */
546         hid_event.ux_device_class_hid_event_length = 1;
547         hid_event.ux_device_class_hid_event_buffer[0] = i;
548         UX_TEST_CHECK_SUCCESS(ux_device_class_hid_event_set(global_slave_hid, &hid_event));
549 
550         /* Wait for host to receive it. */
551         tx_thread_sleep(2);
552     }
553 
554     /* Wake up host test thread. */
555     tx_semaphore_put(&ebt_slave_wakes_host_semaphore);
556 
557     /* Wait for second part of test. */
558     tx_semaphore_get(&ebt_host_wakes_slave_semaphore, TX_WAIT_FOREVER);
559 
560     /* We expect to receive some errors. */
561     ux_test_add_action_to_main_list_multiple(create_error_match_action(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_BUFFER_OVERFLOW), EBT_NUM_OVERFLOW_EVENTS);
562 
563     for (i = 0; i < EBT_MAX_EVENTS + EBT_NUM_OVERFLOW_EVENTS; i++)
564     {
565 
566         /* Setup and send event. */
567         hid_event.ux_device_class_hid_event_length = 1;
568         hid_event.ux_device_class_hid_event_buffer[0] = i;
569         UX_TEST_CHECK_SUCCESS(ux_device_class_hid_event_set(global_slave_hid, &hid_event));
570 
571         /* Wait for host to receive it. */
572         tx_thread_sleep(2);
573     }
574 
575     /* Ensure all of our actions are gone. */
576     UX_TEST_ASSERT_MESSAGE(ux_test_check_actions_empty(), "Number of actions remaining: %d\n", ux_test_get_num_actions_left());
577 
578     /* Wake up host test thread. */
579     tx_semaphore_put(&ebt_slave_wakes_host_semaphore);
580 }
581 
host_event_buffer_test()582 static void host_event_buffer_test()
583 {
584 
585 ULONG   usage;
586 ULONG   value;
587 UINT    i;
588 
589 
590     stepinfo("event_buffer_overflow_test\n");
591 
592     ux_test_disconnect_slave_and_host_wait_for_enum_completion(global_hcd);
593     set_report_descriptor(host_event_buffer_test_hid_report_descriptor, sizeof(host_event_buffer_test_hid_report_descriptor));
594     connect_host_and_slave();
595 
596     UX_TEST_CHECK_SUCCESS(tx_semaphore_create(&ebt_slave_wakes_host_semaphore, "ebt_slave_wakes_host_semaphore", 0));
597     UX_TEST_CHECK_SUCCESS(tx_semaphore_create(&ebt_host_wakes_slave_semaphore, "ebt_host_wakes_slave_semaphore", 0));
598 
599     UX_TEST_CHECK_SUCCESS(tx_thread_create(&test_slave_thread, "test_slave_thread", event_buffer_test_slave_thread_entry, 0,
600                                            test_slave_thread_stack, UX_DEMO_STACK_SIZE,
601                                            20, 20, 1, TX_AUTO_START));
602 
603     stepinfo("    exact amount\n");
604 
605     /* Wait for slave to send exact amount. */
606     tx_semaphore_get(&ebt_slave_wakes_host_semaphore, TX_WAIT_FOREVER);
607 
608     /* Ensure exact amount was sent. */
609     for (i = 0; i < EBT_MAX_EVENTS; i++)
610     {
611 
612         UX_TEST_CHECK_SUCCESS(ux_host_class_hid_remote_control_usage_get(global_host_remote_control, &usage, &value));
613         UX_TEST_ASSERT(usage == 0x000c00e0);
614         UX_TEST_ASSERT(value == i);
615     }
616 
617     /* Should be no more. */
618     UX_TEST_CHECK_NOT_SUCCESS(ux_host_class_hid_remote_control_usage_get(global_host_remote_control, &usage, &value));
619 
620     stepinfo("    overflow\n");
621 
622     /* Wake up slave. */
623     tx_semaphore_put(&ebt_host_wakes_slave_semaphore);
624 
625     /* Wait for slave to overflow. */
626     tx_semaphore_get(&ebt_slave_wakes_host_semaphore, TX_WAIT_FOREVER);
627 
628     /* Ensure exact amount was sent. */
629     for (i = 0; i < EBT_MAX_EVENTS; i++)
630     {
631 
632         UX_TEST_CHECK_SUCCESS(ux_host_class_hid_remote_control_usage_get(global_host_remote_control, &usage, &value));
633         UX_TEST_ASSERT(usage == 0x000c00e0);
634         UX_TEST_ASSERT(value == i);
635     }
636 
637     /* Should be no more. */
638     UX_TEST_CHECK_NOT_SUCCESS(ux_host_class_hid_remote_control_usage_get(global_host_remote_control, &usage, &value));
639 
640     UX_TEST_CHECK_SUCCESS(tx_thread_terminate(&test_slave_thread));
641     UX_TEST_CHECK_SUCCESS(tx_thread_delete(&test_slave_thread));
642 }
643 
test_main_thread_entry(ULONG arg)644 static void test_main_thread_entry(ULONG arg)
645 {
646 
647 UINT    status;
648 UINT    i;
649 void    (*tests[])() =
650 {
651     basic_test,
652     host_event_buffer_test,
653 };
654 
655 
656     ux_test_wait_for_enum_thread_completion();
657     get_global_hid_values();
658     ux_test_memory_test_initialize();
659     get_global_hid_values();
660     /* Run tests. */
661     for (i = 0; i < ARRAY_COUNT(tests); i++)
662     {
663         tests[i]();
664 
665         ux_test_disconnect_slave_and_host_wait_for_enum_completion(global_hcd);
666         set_report_descriptor(hid_report_descriptor, sizeof(hid_report_descriptor));
667         connect_host_and_slave();
668 
669         UX_TEST_ASSERT(ux_test_check_actions_empty());
670     }
671 
672     /* Now disconnect the device.  */
673     _ux_device_stack_disconnect();
674 
675     /* And deinitialize the class.  */
676     status =  ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
677 
678     /* Deinitialize the device side of usbx.  */
679     _ux_device_stack_uninitialize();
680 
681     /* And finally the usbx system resources.  */
682     _ux_system_uninitialize();
683 
684     /* Successful test.  */
685     printf("SUCCESS!\n");
686     test_control_return(0);
687 }
688 
demo_thread_hid_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)689 static UINT demo_thread_hid_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
690 {
691     return(UX_SUCCESS);
692 }