1 /* This file tests the ux_device_class_hid_deactivate API. */
2 
3 #include "usbx_test_common_hid.h"
4 
5 #include "ux_host_class_hid_keyboard.h"
6 
7 
8 #define DUMMY_USBX_MEMORY_SIZE (64*1024)
9 static UCHAR dummy_usbx_memory[DUMMY_USBX_MEMORY_SIZE];
10 
11 static UINT was_instance_deactivate_callback_called;
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 
instance_deactivate_callback(VOID * parameter)152 static VOID instance_deactivate_callback(VOID *parameter)
153 {
154 
155     was_instance_deactivate_callback_called = 1;
156 }
157 
error_callback(UINT system_level,UINT system_context,UINT error_code)158 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
159 {
160 
161     //if (error_code != )
162     printf("hello");
163     {
164 
165         /* Something went wrong.  */
166         printf("Error on line %d\n", __LINE__);
167         test_control_return(1);
168     }
169 }
170 
171 /* Define what the initial system looks like.  */
172 
173 #ifdef CTEST
test_application_define(void * first_unused_memory)174 void test_application_define(void *first_unused_memory)
175 #else
176 void    usbx_ux_device_class_hid_deactivate_test_application_define(void *first_unused_memory)
177 #endif
178 {
179 
180 UINT status;
181 CHAR *                          stack_pointer;
182 CHAR *                          memory_pointer;
183 
184 
185     /* Inform user.  */
186     printf("Running ux_device_class_hid_deactivate test ........................ ");
187 
188     /* Initialize the free memory pointer */
189     stack_pointer = (CHAR *) usbx_memory;
190     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
191 
192     /* Initialize USBX. Memory */
193     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
194 
195     /* Check for error.  */
196     if (status != UX_SUCCESS)
197     {
198 
199         printf("Error on line %d, error code: %d\n", __LINE__, status);
200         test_control_return(1);
201     }
202 
203     /* Register the error callback. */
204     _ux_utility_error_callback_register(error_callback);
205 
206     /* The code below is required for installing the host portion of USBX */
207     status =  ux_host_stack_initialize(ux_system_host_change_function);
208     if (status != UX_SUCCESS)
209     {
210 
211         printf("Error on line %d, error code: %d\n", __LINE__, status);
212         test_control_return(1);
213     }
214 
215     status =  ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
216     if (status != UX_SUCCESS)
217     {
218 
219         printf("Error on line %d, error code: %d\n", __LINE__, status);
220         test_control_return(1);
221     }
222 
223     /* Register the HID client(s).  */
224     status =  ux_host_class_hid_client_register(_ux_system_host_class_hid_client_keyboard_name, ux_host_class_hid_keyboard_entry);
225     if (status != UX_SUCCESS)
226     {
227 
228         printf("Error on line %d, error code: %d\n", __LINE__, status);
229         test_control_return(1);
230     }
231 
232     /* The code below is required for installing the device portion of USBX. No call back for
233        device status change in this example. */
234     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
235                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
236                                        string_framework, STRING_FRAMEWORK_LENGTH,
237                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
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 hid class parameters.  */
246     hid_parameter.ux_device_class_hid_parameter_report_address = hid_report_descriptor;
247     hid_parameter.ux_device_class_hid_parameter_report_length  = HID_REPORT_LENGTH;
248     hid_parameter.ux_device_class_hid_parameter_callback       = demo_thread_hid_callback;
249     hid_parameter.ux_slave_class_hid_instance_deactivate       = instance_deactivate_callback;
250 
251     /* Initilize the device hid class. The class is connected with interface 2 */
252     status =  ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
253                                                 1,2, (VOID *)&hid_parameter);
254     if(status!=UX_SUCCESS)
255     {
256 
257         printf("Error on line %d, error code: %d\n", __LINE__, status);
258         test_control_return(1);
259     }
260 
261     /* Initialize the simulated device controller.  */
262     status =  _ux_dcd_sim_slave_initialize();
263 
264     /* Check for error.  */
265     if (status != UX_SUCCESS)
266     {
267 
268         printf("Error on line %d, error code: %d\n", __LINE__, status);
269         test_control_return(1);
270     }
271 
272     /* Register all the USB host controllers available in this system */
273     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
274 
275     /* Check for error.  */
276     if (status != UX_SUCCESS)
277     {
278 
279         printf("Error on line %d, error code: %d\n", __LINE__, status);
280         test_control_return(1);
281     }
282 
283     /* Create the main host simulation thread.  */
284     status =  tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
285             stack_pointer, UX_DEMO_STACK_SIZE,
286             20, 20, 1, TX_AUTO_START);
287 
288     /* Check for error.  */
289     if (status != TX_SUCCESS)
290     {
291 
292         printf("Error on line %d, error code: %d\n", __LINE__, status);
293         test_control_return(1);
294     }
295 }
296 
tx_demo_thread_host_simulation_entry(ULONG arg)297 static void  tx_demo_thread_host_simulation_entry(ULONG arg)
298 {
299 
300 UINT status;
301     /* Find the HID class */
302     status = demo_class_hid_get();
303     if (status != UX_SUCCESS)
304     {
305 
306         printf("Error on line %d, error code: %d\n", __LINE__, status);
307         test_control_return(1);
308     }
309 
310     /* Now disconnect the device.  */
311     _ux_device_stack_disconnect();
312 
313     /* And deinitialize the class.  */
314     status =  ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
315 
316     /**************************************************/
317     /** Test case: callback was invoked. **/
318     /**************************************************/
319 
320     if (was_instance_deactivate_callback_called != 1) {
321 
322         printf("Error on line %d\n", __LINE__);
323         test_control_return(1);
324     }
325 
326     /**************************************************/
327     /** Test case end. **/
328     /**************************************************/
329 
330     /* Deinitialize the device side of usbx.  */
331     _ux_device_stack_uninitialize();
332 
333     /* And finally the usbx system resources.  */
334     _ux_system_uninitialize();
335 
336     /* Successful test.  */
337     printf("SUCCESS!\n");
338     test_control_return(0);
339 }
340 
demo_thread_hid_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)341 static UINT    demo_thread_hid_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
342 {
343     return(UX_SUCCESS);
344 }
345