1 #include "usbx_test_common_hid.h"
2 #include "ux_host_class_hid_keyboard.h"
3 
4 
5 #define DUMMY_USBX_MEMORY_SIZE (64*1024)
6 static UCHAR dummy_usbx_memory[DUMMY_USBX_MEMORY_SIZE];
7 
8 static UCHAR hid_report_descriptor[] = {
9 
10     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
11     0x09, 0x06,                    // USAGE (Keyboard)
12     0xa1, 0x01,                    // COLLECTION (Application)
13     0x05, 0x07,                    //   USAGE_PAGE (Keyboard)
14     0x19, 0xe0,                    //   USAGE_MINIMUM (Keyboard LeftControl)
15     0x29, 0xe7,                    //   USAGE_MAXIMUM (Keyboard Right GUI)
16     0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
17     0x25, 0x01,                    //   LOGICAL_MAXIMUM (1)
18     0x75, 0x01,                    //   REPORT_SIZE (1)
19     0x95, 0x08,                    //   REPORT_COUNT (8)
20     0x81, 0x02,                    //   INPUT (Data,Var,Abs)
21     0x95, 0x01,                    //   REPORT_COUNT (1)
22     0x75, 0x08,                    //   REPORT_SIZE (8)
23     0x81, 0x03,                    //   INPUT (Cnst,Var,Abs)
24     0x95, 0x05,                    //   REPORT_COUNT (5)
25     0x75, 0x01,                    //   REPORT_SIZE (1)
26     0x05, 0x08,                    //   USAGE_PAGE (LEDs)
27     0x19, 0x01,                    //   USAGE_MINIMUM (Num Lock)
28     0x29, 0x05,                    //   USAGE_MAXIMUM (Kana)
29     0x91, 0x02,                    //   OUTPUT (Data,Var,Abs)
30     0x95, 0x01,                    //   REPORT_COUNT (1)
31     0x75, 0x03,                    //   REPORT_SIZE (3)
32     0x91, 0x03,                    //   OUTPUT (Cnst,Var,Abs)
33     0x95, 0x06,                    //   REPORT_COUNT (6)
34     0x75, 0x08,                    //   REPORT_SIZE (8)
35     0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
36     0x25, 0x65,                    //   LOGICAL_MAXIMUM (101)
37     0x05, 0x07,                    //   USAGE_PAGE (Keyboard)
38     0x19, 0x00,                    //   USAGE_MINIMUM (Reserved (no event indicated))
39     0x29, 0x65,                    //   USAGE_MAXIMUM (Keyboard Application)
40     0x81, 0x00,                    //   INPUT (Data,Ary,Abs)
41     0xc0                           // END_COLLECTION
42 };
43 #define HID_REPORT_LENGTH sizeof(hid_report_descriptor)/sizeof(hid_report_descriptor[0])
44 
45 
46 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 52
47 static UCHAR device_framework_full_speed[DEVICE_FRAMEWORK_LENGTH_FULL_SPEED] = {
48 
49     /* Device descriptor */
50         0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
51         0x81, 0x0A, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
52         0x00, 0x01,
53 
54     /* Configuration descriptor */
55         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
56         0x32,
57 
58     /* Interface descriptor */
59         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
60         0x00,
61 
62     /* HID descriptor */
63         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
64         MSB(HID_REPORT_LENGTH),
65 
66     /* Endpoint descriptor */
67         0x07, 0x05,
68         0x02, /* Set direction to OUT, which is wrong. */
69         0x03, 0x08, 0x00, 0x08
70 
71     };
72 
73 
74 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED 62
75 static UCHAR device_framework_high_speed[DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED] = {
76 
77     /* Device descriptor */
78         0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
79         0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
80         0x03, 0x01,
81 
82     /* Device qualifier descriptor */
83         0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
84         0x01, 0x00,
85 
86     /* Configuration descriptor */
87         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
88         0x32,
89 
90     /* Interface descriptor */
91         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
92         0x00,
93 
94     /* HID descriptor */
95         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
96         MSB(HID_REPORT_LENGTH),
97 
98     /* Endpoint descriptor */
99         0x07, 0x05,
100         0x02, /* Set direction to OUT, which is wrong. */
101         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 
155 /* Define what the initial system looks like.  */
156 
157 #ifdef CTEST
test_application_define(void * first_unused_memory)158 void test_application_define(void *first_unused_memory)
159 #else
160 void    usbx_ux_device_class_hid_activate_test2_application_define(void *first_unused_memory)
161 #endif
162 {
163 
164 UINT                            status;
165 CHAR *                          stack_pointer;
166 CHAR *                          memory_pointer;
167 
168 
169     /* Inform user.  */
170     printf("Running ux_device_class_hid_activate Test 2....................... ");
171 
172     /* Initialize the free memory pointer */
173     stack_pointer = (CHAR *) usbx_memory;
174     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
175 
176     /* Initialize USBX. Memory */
177     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
178 
179     /* Check for error.  */
180     if (status != UX_SUCCESS)
181     {
182 
183         printf("Error on line %d, error code: %d\n", __LINE__, status);
184         test_control_return(1);
185     }
186 
187     /* Register the error callback. */
188     _ux_utility_error_callback_register(error_callback);
189 
190     /* The code below is required for installing the host portion of USBX */
191     status =  ux_host_stack_initialize(ux_system_host_change_function);
192     if (status != UX_SUCCESS)
193     {
194 
195         printf("Error on line %d, error code: %d\n", __LINE__, status);
196         test_control_return(1);
197     }
198 
199     status =  ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
200     if (status != UX_SUCCESS)
201     {
202 
203         printf("Error on line %d, error code: %d\n", __LINE__, status);
204         test_control_return(1);
205     }
206 
207     /* Register the HID client(s).  */
208     status =  ux_host_class_hid_client_register(_ux_system_host_class_hid_client_keyboard_name, ux_host_class_hid_keyboard_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     /* The code below is required for installing the device portion of USBX. No call back for
217        device status change in this example. */
218     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
219                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
220                                        string_framework, STRING_FRAMEWORK_LENGTH,
221                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
222     if(status!=UX_SUCCESS)
223     {
224 
225         printf("Error on line %d, error code: %d\n", __LINE__, status);
226         test_control_return(1);
227     }
228 
229     /* Initialize the hid class parameters.  */
230     hid_parameter.ux_device_class_hid_parameter_report_address = hid_report_descriptor;
231     hid_parameter.ux_device_class_hid_parameter_report_length  = HID_REPORT_LENGTH;
232     hid_parameter.ux_device_class_hid_parameter_callback       = demo_thread_hid_callback;
233 
234     /* Initilize the device hid class. The class is connected with interface 2 */
235     status =  ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
236                                                 1,2, (VOID *)&hid_parameter);
237     if(status!=UX_SUCCESS)
238     {
239 
240         printf("Error on line %d, error code: %d\n", __LINE__, status);
241         test_control_return(1);
242     }
243 
244     /* Initialize the simulated device controller.  */
245     status =  _ux_dcd_sim_slave_initialize();
246 
247     /* Check for error.  */
248     if (status != UX_SUCCESS)
249     {
250 
251         printf("Error on line %d, error code: %d\n", __LINE__, status);
252         test_control_return(1);
253     }
254 
255     /* Register all the USB host controllers available in this system */
256     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
257 
258     /* Check for error.  */
259     if (status != UX_SUCCESS)
260     {
261 
262         printf("Error on line %d, error code: %d\n", __LINE__, status);
263         test_control_return(1);
264     }
265 
266     /* Create the main host simulation thread.  */
267     status =  tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
268             stack_pointer, UX_DEMO_STACK_SIZE,
269             20, 20, 1, TX_AUTO_START);
270 
271     /* Check for error.  */
272     if (status != TX_SUCCESS)
273     {
274 
275         printf("Error on line %d, error code: %d\n", __LINE__, status);
276         test_control_return(1);
277     }
278 }
279 
tx_demo_thread_host_simulation_entry(ULONG arg)280 static void  tx_demo_thread_host_simulation_entry(ULONG arg)
281 {
282 
283 UINT    status;
284 
285 
286     /* Find the HID class */
287     status = demo_class_hid_get();
288     if (status == UX_SUCCESS)
289     {
290 
291         printf("Error on line %d, error code: %d\n", __LINE__, status);
292         test_control_return(1);
293     }
294 
295     /* Now disconnect the device.  */
296     _ux_device_stack_disconnect();
297 
298     /* And deinitialize the class.  */
299     status =  ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
300 
301     /* Deinitialize the device side of usbx.  */
302     _ux_device_stack_uninitialize();
303 
304     /* And finally the usbx system resources.  */
305     _ux_system_uninitialize();
306 
307     /* Successful test.  */
308     printf("SUCCESS!\n");
309     test_control_return(0);
310 }
311 
demo_thread_hid_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)312 static UINT    demo_thread_hid_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
313 {
314     return(UX_SUCCESS);
315 }
316