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