|
| 1 | +/* Direct SIL regression test for samples/demo_device_hid_mouse_rtos.c */ |
| 2 | +#include <stdio.h> |
| 3 | +#include "ux_api.h" |
| 4 | +#include "ux_host_class_hid.h" |
| 5 | +#include "ux_host_class_hid_mouse.h" |
| 6 | + |
| 7 | +void test_control_return(UINT status); |
| 8 | +extern UINT ux_demo_init(VOID); |
| 9 | + |
| 10 | +UX_HOST_CLASS_HID_MOUSE *mouse = UX_NULL; |
| 11 | +UX_HOST_CLASS_HID *hid_instance = UX_NULL; |
| 12 | +static UINT ux_host_event_callback(ULONG event, UX_HOST_CLASS *current_class, VOID *current_instance); |
| 13 | + |
| 14 | +/* Ensure device-only demo compiles within the test. */ |
| 15 | +#define UX_DEMO_STACK_SIZE 1024 |
| 16 | +static UX_THREAD ux_demo_thread_host_simulation; |
| 17 | +static ULONG ux_simulation_thread_stack[UX_DEMO_STACK_SIZE / sizeof(ULONG)]; |
| 18 | +static void tx_demo_thread_host_simulation_entry(ULONG arg); |
| 19 | + |
| 20 | +void usbx_hid_mouse_demo_device_rtos_test_application_define(void *first_unused_memory) |
| 21 | +{ |
| 22 | + UX_PARAMETER_NOT_USED(first_unused_memory); |
| 23 | + |
| 24 | + UINT status; |
| 25 | + |
| 26 | + printf("Running HID Mouse Demo Test............................ "); |
| 27 | + |
| 28 | + |
| 29 | + /* Initialize device side using the demo's API (creates its HID thread). */ |
| 30 | + status = ux_demo_init(); |
| 31 | + |
| 32 | + if (status != UX_SUCCESS) |
| 33 | + { |
| 34 | + printf("Demo device init fail\n"); |
| 35 | + test_control_return(1); |
| 36 | + } |
| 37 | + |
| 38 | + /* Initialize host side. */ |
| 39 | + status = ux_host_stack_initialize(ux_host_event_callback); |
| 40 | + |
| 41 | + if (status != UX_SUCCESS) |
| 42 | + { |
| 43 | + printf("Host init fail\n"); |
| 44 | + test_control_return(1); |
| 45 | + } |
| 46 | + |
| 47 | + status = ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry); |
| 48 | + |
| 49 | + if (status != UX_SUCCESS) |
| 50 | + { |
| 51 | + printf("Host HID reg fail\n"); |
| 52 | + test_control_return(1); |
| 53 | + } |
| 54 | + |
| 55 | + status = ux_host_class_hid_client_register(_ux_system_host_class_hid_client_mouse_name, ux_host_class_hid_mouse_entry); |
| 56 | + |
| 57 | + if (status != UX_SUCCESS) |
| 58 | + { |
| 59 | + printf("Host mouse client reg fail\n"); |
| 60 | + test_control_return(1); |
| 61 | + } |
| 62 | + |
| 63 | + /* Register all the USB host controllers available in this system */ |
| 64 | + status = ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0); |
| 65 | + |
| 66 | + /* Check for error. */ |
| 67 | + if (status != UX_SUCCESS) |
| 68 | + { |
| 69 | + |
| 70 | + printf("Error on line %d, error code: %d\n", __LINE__, status); |
| 71 | + test_control_return(1); |
| 72 | + } |
| 73 | + |
| 74 | + /* Create the main host simulation thread. */ |
| 75 | + status = ux_utility_thread_create(&ux_demo_thread_host_simulation, "tx demo host simulation", |
| 76 | + tx_demo_thread_host_simulation_entry, 0, ux_simulation_thread_stack, |
| 77 | + UX_DEMO_STACK_SIZE, 20, 20, 1, UX_AUTO_START); |
| 78 | + |
| 79 | + /* Check for error. */ |
| 80 | + if (status != UX_SUCCESS) |
| 81 | + { |
| 82 | + |
| 83 | + printf("Error on line %d, error code: %d\n", __LINE__, status); |
| 84 | + test_control_return(1); |
| 85 | + } |
| 86 | + |
| 87 | +} |
| 88 | + |
| 89 | +static void tx_demo_thread_host_simulation_entry(ULONG arg) |
| 90 | +{ |
| 91 | + |
| 92 | +UINT status; |
| 93 | + |
| 94 | +ULONG cur_mouse_buttons = 0; |
| 95 | +SLONG cur_mouse_x_position = 0; |
| 96 | +SLONG cur_mouse_y_position = 0; |
| 97 | +SLONG cur_mouse_wheel_movement = 0; |
| 98 | +ULONG timeout_count = 0; |
| 99 | + |
| 100 | + |
| 101 | + while (1) |
| 102 | + { |
| 103 | + /* Start if the hid client is a mouse and connected */ |
| 104 | + if ((mouse != NULL) && |
| 105 | + (mouse -> ux_host_class_hid_mouse_state == (ULONG) UX_HOST_CLASS_INSTANCE_LIVE)) |
| 106 | + { |
| 107 | + |
| 108 | + status = ux_host_class_hid_mouse_buttons_get(mouse, &cur_mouse_buttons); |
| 109 | + if (status != UX_SUCCESS) |
| 110 | + { |
| 111 | + |
| 112 | + printf("Error on line %d\n", __LINE__); |
| 113 | + test_control_return(1); |
| 114 | + } |
| 115 | + |
| 116 | + status = ux_host_class_hid_mouse_position_get(mouse, &cur_mouse_x_position, &cur_mouse_y_position); |
| 117 | + if (status != UX_SUCCESS) |
| 118 | + { |
| 119 | + |
| 120 | + printf("Error on line %d\n", __LINE__); |
| 121 | + test_control_return(1); |
| 122 | + } |
| 123 | + |
| 124 | + status = ux_host_class_hid_mouse_wheel_get(mouse, &cur_mouse_wheel_movement); |
| 125 | + if (status != UX_SUCCESS) |
| 126 | + { |
| 127 | + |
| 128 | + printf("Error on line %d\n", __LINE__); |
| 129 | + test_control_return(1); |
| 130 | + } |
| 131 | + |
| 132 | + status = ux_host_class_hid_mouse_position_get(mouse, &cur_mouse_x_position, &cur_mouse_y_position); |
| 133 | + if (status != UX_SUCCESS) |
| 134 | + { |
| 135 | + |
| 136 | + printf("Error on line %d\n", __LINE__); |
| 137 | + test_control_return(1); |
| 138 | + } |
| 139 | + |
| 140 | + if ((cur_mouse_x_position != 0) || (cur_mouse_y_position != 0)) |
| 141 | + { |
| 142 | + |
| 143 | + printf("SUCCESS!\n"); |
| 144 | + test_control_return(0); |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + } |
| 149 | + else |
| 150 | + { |
| 151 | + timeout_count ++; |
| 152 | + if (timeout_count > 500) |
| 153 | + { |
| 154 | + |
| 155 | + printf("Timeout waiting for HID mouse traffic\n"); |
| 156 | + test_control_return(1); |
| 157 | + return; |
| 158 | + } |
| 159 | + ux_utility_delay_ms(MS_TO_TICK(10)); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | +} |
| 165 | + |
| 166 | +static UINT ux_host_event_callback(ULONG event, UX_HOST_CLASS *current_class, VOID *current_instance) |
| 167 | +{ |
| 168 | + UINT status = UX_SUCCESS; |
| 169 | + |
| 170 | + /* Get current Hid Client */ |
| 171 | + UX_HOST_CLASS_HID_CLIENT *client = (UX_HOST_CLASS_HID_CLIENT *)current_instance; |
| 172 | + |
| 173 | + switch (event) |
| 174 | + { |
| 175 | + case UX_DEVICE_INSERTION: |
| 176 | + |
| 177 | + /* Get current Hid Class */ |
| 178 | + if (current_class -> ux_host_class_entry_function == ux_host_class_hid_entry) |
| 179 | + { |
| 180 | + if (hid_instance == UX_NULL) |
| 181 | + { |
| 182 | + /* Get current Hid Instance */ |
| 183 | + hid_instance = (UX_HOST_CLASS_HID *)current_instance; |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + break; |
| 188 | + |
| 189 | + case UX_DEVICE_REMOVAL: |
| 190 | + |
| 191 | + /* Free HID Instance */ |
| 192 | + if ((VOID*)hid_instance == current_instance) |
| 193 | + { |
| 194 | + hid_instance = UX_NULL; |
| 195 | + } |
| 196 | + |
| 197 | + break; |
| 198 | + |
| 199 | + case UX_HID_CLIENT_INSERTION: |
| 200 | + |
| 201 | + /* Check the HID_client if this is a HID mouse device */ |
| 202 | + if (client -> ux_host_class_hid_client_handler == ux_host_class_hid_mouse_entry) |
| 203 | + { |
| 204 | + /* Get current Hid Client */ |
| 205 | + if (mouse == UX_NULL) |
| 206 | + { |
| 207 | + mouse = client -> ux_host_class_hid_client_local_instance; |
| 208 | + } |
| 209 | + } |
| 210 | + |
| 211 | + |
| 212 | + break; |
| 213 | + |
| 214 | + case UX_HID_CLIENT_REMOVAL: |
| 215 | + |
| 216 | + |
| 217 | + if ((VOID*)mouse == client -> ux_host_class_hid_client_local_instance) |
| 218 | + { |
| 219 | + /* Clear hid mouse instance */ |
| 220 | + mouse = UX_NULL; |
| 221 | + } |
| 222 | + |
| 223 | + break; |
| 224 | + |
| 225 | + case UX_DEVICE_CONNECTION: |
| 226 | + break; |
| 227 | + |
| 228 | + case UX_DEVICE_DISCONNECTION: |
| 229 | + break; |
| 230 | + |
| 231 | + default: |
| 232 | + break; |
| 233 | + } |
| 234 | + |
| 235 | + return status; |
| 236 | +} |
| 237 | + |
0 commit comments