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