1 /* This test concentrates on the report decompression api when the report contains an array item. */
2
3 #include "usbx_test_common_hid.h"
4 #include "ux_host_class_hid_keyboard.h"
5
6
7 static UCHAR report_buffer_compressed[1024];
8 static SLONG report_buffer_decompressed[1024];
9
10 static UCHAR hid_report_descriptor[] = {
11
12 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
13 0x09, 0x06, // USAGE (Keyboard)
14 0xa1, 0x01, // COLLECTION (Application)
15 0x15, 0x00, // LOGICAL_MINIMUM (0)
16 0x25, 0x65, // LOGICAL_MAXIMUM (101)
17 0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
18 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
19 0x75, 0x08, // REPORT_SIZE (8)
20 0x95, 0x06, // REPORT_COUNT (6)
21 0x81, 0x00, // INPUT (Data,Ary,Abs)
22
23 /* USBX expects keyboards to have at least one output report, otherwise it's an error. */
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
31 0xc0, // END_COLLECTION
32 };
33 #define HID_REPORT_LENGTH sizeof(hid_report_descriptor)/sizeof(hid_report_descriptor[0])
34
35
36 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 52
37 static UCHAR device_framework_full_speed[DEVICE_FRAMEWORK_LENGTH_FULL_SPEED] = {
38
39 /* Device descriptor */
40 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
41 0x81, 0x0A, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
42 0x00, 0x01,
43
44 /* Configuration descriptor */
45 0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
46 0x32,
47
48 /* Interface descriptor */
49 0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
50 0x00,
51
52 /* HID descriptor */
53 0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
54 MSB(HID_REPORT_LENGTH),
55
56 /* Endpoint descriptor (Interrupt) */
57 0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
58
59 };
60
61
62 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED 62
63 static UCHAR device_framework_high_speed[DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED] = {
64
65 /* Device descriptor */
66 0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
67 0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
68 0x03, 0x01,
69
70 /* Device qualifier descriptor */
71 0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
72 0x01, 0x00,
73
74 /* Configuration descriptor */
75 0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
76 0x32,
77
78 /* Interface descriptor */
79 0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
80 0x00,
81
82 /* HID descriptor */
83 0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
84 MSB(HID_REPORT_LENGTH),
85
86 /* Endpoint descriptor (Interrupt) */
87 0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
88
89 };
90
91
92 /* String Device Framework :
93 Byte 0 and 1 : Word containing the language ID : 0x0904 for US
94 Byte 2 : Byte containing the index of the descriptor
95 Byte 3 : Byte containing the length of the descriptor string
96 */
97
98 #define STRING_FRAMEWORK_LENGTH 40
99 static UCHAR string_framework[] = {
100
101 /* Manufacturer string descriptor : Index 1 */
102 0x09, 0x04, 0x01, 0x0c,
103 0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
104 0x6f, 0x67, 0x69, 0x63,
105
106 /* Product string descriptor : Index 2 */
107 0x09, 0x04, 0x02, 0x0c,
108 0x55, 0x53, 0x42, 0x20, 0x4b, 0x65, 0x79, 0x62,
109 0x6f, 0x61, 0x72, 0x64,
110
111 /* Serial Number string descriptor : Index 3 */
112 0x09, 0x04, 0x03, 0x04,
113 0x30, 0x30, 0x30, 0x31
114 };
115
116
117 /* Multiple languages are supported on the device, to add
118 a language besides english, the unicode language code must
119 be appended to the language_id_framework array and the length
120 adjusted accordingly. */
121 #define LANGUAGE_ID_FRAMEWORK_LENGTH 2
122 static UCHAR language_id_framework[] = {
123
124 /* English. */
125 0x09, 0x04
126 };
127
128
129
error_callback(UINT system_level,UINT system_context,UINT error_code)130 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
131 {
132
133 /* Failed test. */
134 printf("Error on line %d, system_level: %d, system_context: %d, error code: %d\n", __LINE__, system_level, system_context, error_code);
135 test_control_return(1);
136 }
137
138 /* Define what the initial system looks like. */
139
140 #ifdef CTEST
test_application_define(void * first_unused_memory)141 void test_application_define(void *first_unused_memory)
142 #else
143 void usbx_hid_report_descriptor_decompress_array_test_application_define(void *first_unused_memory)
144 #endif
145 {
146
147 UINT status;
148 CHAR * stack_pointer;
149 CHAR * memory_pointer;
150 UINT descriptor_size = HID_REPORT_LENGTH;
151
152 /* Inform user. */
153 printf("Running HID Report Descriptor Decompress Array Test................. ");
154
155 /* Initialize the free memory pointer */
156 stack_pointer = (CHAR *) usbx_memory;
157 memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
158
159 /* Initialize USBX. Memory */
160 status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
161
162 /* Check for error. */
163 if (status != UX_SUCCESS)
164 {
165
166 printf("Error on line %d\n", __LINE__);
167 test_control_return(1);
168 }
169
170 /* Register the error callback. */
171 _ux_utility_error_callback_register(error_callback);
172
173 /* The code below is required for installing the host portion of USBX */
174 status = ux_host_stack_initialize(UX_NULL);
175 if (status != UX_SUCCESS)
176 {
177
178 printf("Error on line %d\n", __LINE__);
179 test_control_return(1);
180 }
181
182 status = ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
183 if (status != UX_SUCCESS)
184 {
185
186 printf("Error on line %d\n", __LINE__);
187 test_control_return(1);
188 }
189
190 /* Register the HID client(s). */
191 status = ux_host_class_hid_client_register(_ux_system_host_class_hid_client_keyboard_name, ux_host_class_hid_keyboard_entry);
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 /* The code below is required for installing the device portion of USBX. No call back for
200 device status change in this example. */
201 status = ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
202 device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
203 string_framework, STRING_FRAMEWORK_LENGTH,
204 language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
205 if(status!=UX_SUCCESS)
206 {
207
208 printf("Error on line %d\n", __LINE__);
209 test_control_return(1);
210 }
211
212 /* Initialize the hid class parameters. */
213 hid_parameter.ux_device_class_hid_parameter_report_address = hid_report_descriptor;
214 hid_parameter.ux_device_class_hid_parameter_report_length = HID_REPORT_LENGTH;
215 hid_parameter.ux_device_class_hid_parameter_callback = demo_thread_hid_callback;
216
217 /* Initilize the device hid class. The class is connected with interface 2 */
218 status = ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
219 1,2, (VOID *)&hid_parameter);
220 if(status!=UX_SUCCESS)
221 {
222
223 printf("Error on line %d\n", __LINE__);
224 test_control_return(1);
225 }
226
227
228 /* Initialize the simulated device controller. */
229 status = _ux_dcd_sim_slave_initialize();
230
231 /* Check for error. */
232 if (status != UX_SUCCESS)
233 {
234
235 printf("Error on line %d\n", __LINE__);
236 test_control_return(1);
237 }
238
239 /* Register all the USB host controllers available in this system */
240 status = ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
241
242 /* Check for error. */
243 if (status != UX_SUCCESS)
244 {
245
246 printf("Error on line %d\n", __LINE__);
247 test_control_return(1);
248 }
249
250 /* Create the main host simulation thread. */
251 status = tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
252 stack_pointer, UX_DEMO_STACK_SIZE,
253 20, 20, 1, TX_AUTO_START);
254
255 /* Check for error. */
256 if (status != TX_SUCCESS)
257 {
258
259 printf("Error on line %d\n", __LINE__);
260 test_control_return(1);
261 }
262 }
263
tx_demo_thread_host_simulation_entry(ULONG arg)264 static void tx_demo_thread_host_simulation_entry(ULONG arg)
265 {
266
267 UINT status;
268 UX_HOST_CLASS_HID_REPORT_GET_ID report_id;
269 UX_HOST_CLASS_HID_CLIENT_REPORT input_report_request;
270 UX_HOST_CLASS_HID_REPORT *input_report_descriptor;
271
272
273 /* Find the HID class */
274 status = demo_class_hid_get();
275 if (status != UX_SUCCESS)
276 {
277
278 printf("Error on line %d\n", __LINE__);
279 test_control_return(1);
280 }
281
282 /* Get the first input report descriptor. */
283 report_id.ux_host_class_hid_report_get_report = UX_NULL;
284 report_id.ux_host_class_hid_report_get_type = UX_HOST_CLASS_HID_REPORT_TYPE_INPUT;
285 status = ux_host_class_hid_report_id_get(hid, &report_id);
286 if (status != UX_SUCCESS)
287 {
288
289 printf("Error on line %d\n", __LINE__);
290 test_control_return(1);
291 }
292
293 input_report_descriptor = report_id.ux_host_class_hid_report_get_report;
294 if (input_report_descriptor -> ux_host_class_hid_report_id != 0)
295 {
296
297 printf("Error on line %d\n", __LINE__);
298 test_control_return(1);
299 }
300
301 /* Fill out the request for the decompress api. */
302 input_report_request.ux_host_class_hid_client_report = input_report_descriptor;
303 input_report_request.ux_host_class_hid_client_report_buffer = report_buffer_decompressed;
304 input_report_request.ux_host_class_hid_client_report_length = input_report_descriptor -> ux_host_class_hid_report_byte_length;
305 input_report_request.ux_host_class_hid_client_report_actual_length = 0;
306 input_report_request.ux_host_class_hid_client_report_flags = UX_HOST_CLASS_HID_REPORT_DECOMPRESSED;
307
308 /* Fill out report. */
309 report_buffer_compressed[0] = 0x00;
310 report_buffer_compressed[1] = 0x10;
311 report_buffer_compressed[2] = 0x23;
312 report_buffer_compressed[3] = 0x37;
313 report_buffer_compressed[4] = 0x43;
314 report_buffer_compressed[5] = 0x65;
315
316 /* Request decompression. */
317 status = _ux_host_class_hid_report_decompress(hid, &input_report_request, report_buffer_compressed, 6);
318
319 if (status != UX_SUCCESS)
320 {
321
322 printf("Error on line %d\n", __LINE__);
323 test_control_return(1);
324 }
325
326 /* Check output of decompression of report. */
327 if (
328 report_buffer_decompressed[0] != 0x00010000 ||
329 report_buffer_decompressed[1] != 0x00 ||
330 report_buffer_decompressed[2] != 0x00010010 ||
331 report_buffer_decompressed[3] != 0x10 ||
332 report_buffer_decompressed[4] != 0x00010023 ||
333 report_buffer_decompressed[5] != 0x23 ||
334 report_buffer_decompressed[6] != 0x00010037 ||
335 report_buffer_decompressed[7] != 0x37 ||
336 report_buffer_decompressed[8] != 0x00010043 ||
337 report_buffer_decompressed[9] != 0x43 ||
338 report_buffer_decompressed[10] != 0x00010065 ||
339 report_buffer_decompressed[11] != 0x65
340 )
341 {
342
343 printf("Error on line %d\n", __LINE__);
344 test_control_return(1);
345 }
346
347 /* Now disconnect the device. */
348 _ux_device_stack_disconnect();
349
350 /* And deinitialize the class. */
351 status = ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
352
353 /* Deinitialize the device side of usbx. */
354 _ux_device_stack_uninitialize();
355
356 /* And finally the usbx system resources. */
357 _ux_system_uninitialize();
358
359 /* Successful test. */
360 printf("SUCCESS!\n");
361 test_control_return(0);
362 }
363
demo_thread_hid_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)364 static UINT demo_thread_hid_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
365 {
366 return(UX_SUCCESS);
367 }
368