1 /* This file tests the ux_host_class_hid_remote_control_entry API. */
2 
3 #include "usbx_test_common_hid.h"
4 
5 #include "ux_host_class_hid_keyboard.h"
6 #include "ux_host_class_hid_remote_control.h"
7 
8 
9 #define DUMMY_USBX_MEMORY_SIZE (64*1024)
10 static UCHAR dummy_usbx_memory[DUMMY_USBX_MEMORY_SIZE];
11 
12 static UCHAR hid_report_descriptor[] = {
13 
14     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
15     0x09, 0x06,                    // USAGE (Keyboard)
16     0xa1, 0x01,                    // COLLECTION (Application)
17     0x05, 0x07,                    //   USAGE_PAGE (Keyboard)
18     0x19, 0xe0,                    //   USAGE_MINIMUM (Keyboard LeftControl)
19     0x29, 0xe7,                    //   USAGE_MAXIMUM (Keyboard Right GUI)
20     0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
21     0x25, 0x01,                    //   LOGICAL_MAXIMUM (1)
22     0x75, 0x01,                    //   REPORT_SIZE (1)
23     0x95, 0x08,                    //   REPORT_COUNT (8)
24     0x81, 0x02,                    //   INPUT (Data,Var,Abs)
25     0x95, 0x01,                    //   REPORT_COUNT (1)
26     0x75, 0x08,                    //   REPORT_SIZE (8)
27     0x81, 0x03,                    //   INPUT (Cnst,Var,Abs)
28     0x95, 0x05,                    //   REPORT_COUNT (5)
29     0x75, 0x01,                    //   REPORT_SIZE (1)
30     0x05, 0x08,                    //   USAGE_PAGE (LEDs)
31     0x19, 0x01,                    //   USAGE_MINIMUM (Num Lock)
32     0x29, 0x05,                    //   USAGE_MAXIMUM (Kana)
33     0x91, 0x02,                    //   OUTPUT (Data,Var,Abs)
34     0x95, 0x01,                    //   REPORT_COUNT (1)
35     0x75, 0x03,                    //   REPORT_SIZE (3)
36     0x91, 0x03,                    //   OUTPUT (Cnst,Var,Abs)
37     0x95, 0x06,                    //   REPORT_COUNT (6)
38     0x75, 0x08,                    //   REPORT_SIZE (8)
39     0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
40     0x25, 0x65,                    //   LOGICAL_MAXIMUM (101)
41     0x05, 0x07,                    //   USAGE_PAGE (Keyboard)
42     0x19, 0x00,                    //   USAGE_MINIMUM (Reserved (no event indicated))
43     0x29, 0x65,                    //   USAGE_MAXIMUM (Keyboard Application)
44     0x81, 0x00,                    //   INPUT (Data,Ary,Abs)
45     0xc0                           // END_COLLECTION
46 };
47 #define HID_REPORT_LENGTH sizeof(hid_report_descriptor)/sizeof(hid_report_descriptor[0])
48 
49 
50 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 52
51 static UCHAR device_framework_full_speed[DEVICE_FRAMEWORK_LENGTH_FULL_SPEED] = {
52 
53     /* Device descriptor */
54         0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
55         0x81, 0x0A, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
56         0x00, 0x01,
57 
58     /* Configuration descriptor */
59         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
60         0x32,
61 
62     /* Interface descriptor */
63         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
64         0x00,
65 
66     /* HID descriptor */
67         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
68         MSB(HID_REPORT_LENGTH),
69 
70     /* Endpoint descriptor (Interrupt) */
71         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
72 
73     };
74 
75 
76 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED 62
77 static UCHAR device_framework_high_speed[DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED] = {
78 
79     /* Device descriptor */
80         0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
81         0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
82         0x03, 0x01,
83 
84     /* Device qualifier descriptor */
85         0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
86         0x01, 0x00,
87 
88     /* Configuration descriptor */
89         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
90         0x32,
91 
92     /* Interface descriptor */
93         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
94         0x00,
95 
96     /* HID descriptor */
97         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
98         MSB(HID_REPORT_LENGTH),
99 
100     /* Endpoint descriptor (Interrupt) */
101         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
102 
103     };
104 
105 
106     /* String Device Framework :
107      Byte 0 and 1 : Word containing the language ID : 0x0904 for US
108      Byte 2       : Byte containing the index of the descriptor
109      Byte 3       : Byte containing the length of the descriptor string
110     */
111 
112 #define STRING_FRAMEWORK_LENGTH 40
113 static UCHAR string_framework[] = {
114 
115     /* Manufacturer string descriptor : Index 1 */
116         0x09, 0x04, 0x01, 0x0c,
117         0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
118         0x6f, 0x67, 0x69, 0x63,
119 
120     /* Product string descriptor : Index 2 */
121         0x09, 0x04, 0x02, 0x0c,
122         0x55, 0x53, 0x42, 0x20, 0x4b, 0x65, 0x79, 0x62,
123         0x6f, 0x61, 0x72, 0x64,
124 
125     /* Serial Number string descriptor : Index 3 */
126         0x09, 0x04, 0x03, 0x04,
127         0x30, 0x30, 0x30, 0x31
128     };
129 
130 
131     /* Multiple languages are supported on the device, to add
132        a language besides english, the unicode language code must
133        be appended to the language_id_framework array and the length
134        adjusted accordingly. */
135 #define LANGUAGE_ID_FRAMEWORK_LENGTH 2
136 static UCHAR language_id_framework[] = {
137 
138     /* English. */
139         0x09, 0x04
140     };
141 
142 
143 UINT  _ux_hcd_sim_host_entry(UX_HCD *hcd, UINT function, VOID *parameter);
144 
145 
ux_system_host_change_function(ULONG a,UX_HOST_CLASS * b,VOID * c)146 static UINT ux_system_host_change_function(ULONG a, UX_HOST_CLASS *b, VOID *c)
147 {
148     return 0;
149 }
150 
error_callback(UINT system_level,UINT system_context,UINT error_code)151 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
152 {
153 
154     //if (error_code != )
155     printf("hello");
156     {
157 
158         /* Something went wrong.  */
159         printf("Error on line %d\n", __LINE__);
160         test_control_return(1);
161     }
162 }
163 
164 /* Define what the initial system looks like.  */
165 
166 #ifdef CTEST
test_application_define(void * first_unused_memory)167 void test_application_define(void *first_unused_memory)
168 #else
169 void    usbx_ux_host_class_hid_remote_control_entry_test_application_define(void *first_unused_memory)
170 #endif
171 {
172 
173 UINT status;
174 CHAR *                          stack_pointer;
175 CHAR *                          memory_pointer;
176 
177 
178     /* Inform user.  */
179     printf("Running ux_host_class_hid_remote_control_entry Test................. ");
180 
181     /* Initialize the free memory pointer */
182     stack_pointer = (CHAR *) usbx_memory;
183     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
184 
185     /* Initialize USBX. Memory */
186     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
187 
188     /* Check for error.  */
189     if (status != UX_SUCCESS)
190     {
191 
192         printf("Error on line %d, error code: %d\n", __LINE__, status);
193         test_control_return(1);
194     }
195 
196     /* Register the error callback. */
197     _ux_utility_error_callback_register(error_callback);
198 
199     /* The code below is required for installing the host portion of USBX */
200     status =  ux_host_stack_initialize(ux_system_host_change_function);
201     if (status != UX_SUCCESS)
202     {
203 
204         printf("Error on line %d, error code: %d\n", __LINE__, status);
205         test_control_return(1);
206     }
207 
208     status =  ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
209     if (status != UX_SUCCESS)
210     {
211 
212         printf("Error on line %d, error code: %d\n", __LINE__, status);
213         test_control_return(1);
214     }
215 
216     /* Register the HID client(s).  */
217     status =  ux_host_class_hid_client_register(_ux_system_host_class_hid_client_keyboard_name, ux_host_class_hid_keyboard_entry);
218     if (status != UX_SUCCESS)
219     {
220 
221         printf("Error on line %d, error code: %d\n", __LINE__, status);
222         test_control_return(1);
223     }
224 
225     /* The code below is required for installing the device portion of USBX. No call back for
226        device status change in this example. */
227     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
228                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
229                                        string_framework, STRING_FRAMEWORK_LENGTH,
230                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
231     if(status!=UX_SUCCESS)
232     {
233 
234         printf("Error on line %d, error code: %d\n", __LINE__, status);
235         test_control_return(1);
236     }
237 
238     /* Initialize the hid class parameters.  */
239     hid_parameter.ux_device_class_hid_parameter_report_address = hid_report_descriptor;
240     hid_parameter.ux_device_class_hid_parameter_report_length  = HID_REPORT_LENGTH;
241     hid_parameter.ux_device_class_hid_parameter_callback       = demo_thread_hid_callback;
242 
243     /* Initilize the device hid class. The class is connected with interface 2 */
244     status =  ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
245                                                 1,2, (VOID *)&hid_parameter);
246     if(status!=UX_SUCCESS)
247     {
248 
249         printf("Error on line %d, error code: %d\n", __LINE__, status);
250         test_control_return(1);
251     }
252 
253     /* Initialize the simulated device controller.  */
254     status =  _ux_dcd_sim_slave_initialize();
255 
256     /* Check for error.  */
257     if (status != UX_SUCCESS)
258     {
259 
260         printf("Error on line %d, error code: %d\n", __LINE__, status);
261         test_control_return(1);
262     }
263 
264     /* Register all the USB host controllers available in this system */
265     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
266 
267     /* Check for error.  */
268     if (status != UX_SUCCESS)
269     {
270 
271         printf("Error on line %d, error code: %d\n", __LINE__, status);
272         test_control_return(1);
273     }
274 
275     /* Create the main host simulation thread.  */
276     status =  tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
277             stack_pointer, UX_DEMO_STACK_SIZE,
278             20, 20, 1, TX_AUTO_START);
279 
280     /* Check for error.  */
281     if (status != TX_SUCCESS)
282     {
283 
284         printf("Error on line %d, error code: %d\n", __LINE__, status);
285         test_control_return(1);
286     }
287 }
288 
tx_demo_thread_host_simulation_entry(ULONG arg)289 static void  tx_demo_thread_host_simulation_entry(ULONG arg)
290 {
291 
292 UINT status;
293 UX_HOST_CLASS_HID_CLIENT_COMMAND    client_command;
294 
295     /* Find the HID class */
296     status = demo_class_hid_get();
297     if (status != UX_SUCCESS)
298     {
299 
300         printf("Error on line %d, error code: %d\n", __LINE__, status);
301         test_control_return(1);
302     }
303 
304     /**************************************************/
305     /** Test case: no match in switch (command -> ux_host_class_hid_client_command_request). **/
306     /**************************************************/
307 
308     /* Set to unknown command. */
309     client_command.ux_host_class_hid_client_command_request = 0xdeadbeef;
310 
311     status = _ux_host_class_hid_remote_control_entry(&client_command);
312     if (status != UX_ERROR)
313     {
314 
315         printf("Error on line %d, error code: %d\n", __LINE__, status);
316         test_control_return(1);
317     }
318 
319     /* Now disconnect the device.  */
320     _ux_device_stack_disconnect();
321 
322     /* And deinitialize the class.  */
323     status =  ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
324 
325     /* Deinitialize the device side of usbx.  */
326     _ux_device_stack_uninitialize();
327 
328     /* And finally the usbx system resources.  */
329     _ux_system_uninitialize();
330 
331     /* Successful test.  */
332     printf("SUCCESS!\n");
333     test_control_return(0);
334 }
335 
demo_thread_hid_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)336 static UINT    demo_thread_hid_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
337 {
338     return(UX_SUCCESS);
339 }
340