From 551aabf4074f79915e53b01ba1d028cabe0a414c Mon Sep 17 00:00:00 2001 From: MAY Date: Thu, 30 Apr 2026 22:57:54 +0200 Subject: [PATCH] add usbx device hid samples --- .../regression_samples_rtos_test.yml | 46 + samples/demo_device_hid_keyboard_rtos.c | 721 +++++++ samples/demo_device_hid_mouse_rtos.c | 967 ++++----- samples/demo_device_hid_mouse_standalone.c | 718 ------- scripts/build_samples_rtos.sh | 2 + scripts/test_samples_rtos.sh | 2 + test/cmake/usbx_samples_rtos/CMakeLists.txt | 117 ++ test/cmake/usbx_samples_rtos/run.sh | 15 + .../usbx_hid_keyboard_demo_device_rtos_test.c | 258 +++ .../usbx_hid_mouse_demo_device_rtos_test.c | 247 +++ .../regression_samples_rtos/usbxtestcontrol.c | 160 ++ test/regression_samples_rtos/ux_test.c | 1834 +++++++++++++++++ test/regression_samples_rtos/ux_test.h | 540 +++++ .../regression_samples_rtos/ux_test_actions.h | 21 + 14 files changed, 4460 insertions(+), 1188 deletions(-) create mode 100644 .github/workflows/regression_samples_rtos_test.yml create mode 100644 samples/demo_device_hid_keyboard_rtos.c delete mode 100644 samples/demo_device_hid_mouse_standalone.c create mode 100644 scripts/build_samples_rtos.sh create mode 100644 scripts/test_samples_rtos.sh create mode 100644 test/cmake/usbx_samples_rtos/CMakeLists.txt create mode 100644 test/cmake/usbx_samples_rtos/run.sh create mode 100644 test/regression_samples_rtos/usbx_hid_keyboard_demo_device_rtos_test.c create mode 100644 test/regression_samples_rtos/usbx_hid_mouse_demo_device_rtos_test.c create mode 100644 test/regression_samples_rtos/usbxtestcontrol.c create mode 100644 test/regression_samples_rtos/ux_test.c create mode 100644 test/regression_samples_rtos/ux_test.h create mode 100644 test/regression_samples_rtos/ux_test_actions.h diff --git a/.github/workflows/regression_samples_rtos_test.yml b/.github/workflows/regression_samples_rtos_test.yml new file mode 100644 index 000000000..fd8a78e7e --- /dev/null +++ b/.github/workflows/regression_samples_rtos_test.yml @@ -0,0 +1,46 @@ +name: USBX Regression Samples RTOS Test + +on: + workflow_dispatch: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + + manual_tests: + if: github.event_name == 'workflow_dispatch' + permissions: + contents: read + issues: read + checks: write + pull-requests: write + pages: write + id-token: write + + uses: eclipse-threadx/threadx/.github/workflows/regression_template.yml@master + with: + cmake_path: ./test/cmake/usbx_samples_rtos + build_script: bash ./scripts/build_samples_rtos.sh msrc_rtos_build + test_script: bash ./scripts/test_samples_rtos.sh msrc_rtos_build + coverage_name: msrc_rtos_build + skip_coverage: true + + auto_tests: + if: github.event_name != 'workflow_dispatch' + permissions: + contents: read + issues: read + checks: write + pull-requests: write + pages: write + id-token: write + + uses: eclipse-threadx/threadx/.github/workflows/regression_template.yml@master + with: + cmake_path: ./test/cmake/usbx_samples_rtos + build_script: bash ./scripts/build_samples_rtos.sh msrc_rtos_build + test_script: bash ./scripts/test_samples_rtos.sh msrc_rtos_build + coverage_name: msrc_rtos_build + skip_coverage: true diff --git a/samples/demo_device_hid_keyboard_rtos.c b/samples/demo_device_hid_keyboard_rtos.c new file mode 100644 index 000000000..7ac619bc1 --- /dev/null +++ b/samples/demo_device_hid_keyboard_rtos.c @@ -0,0 +1,721 @@ +/*************************************************************************** + * Copyright (c) 2025-present Eclipse ThreadX Contributors + * + * This program and the accompanying materials are made available under the + * terms of the MIT License which is available at + * https://opensource.org/licenses/MIT. + * + * + * SPDX-License-Identifier: MIT + **************************************************************************/ + +/**************************************************************************/ +/**************************************************************************/ +/** */ +/** Overview */ +/** */ +/** This sample configures USBX device mode to enumerate as a USB HID */ +/** keyboard on the host. It runs with ThreadX, creates dedicated */ +/** threads for device-controller setup and HID report generation, and */ +/** periodically sends keyboard input reports after the device has been */ +/** configured by the host. */ +/** */ +/** Note */ +/** */ +/** This demonstration is not optimized, to optimize application user */ +/** should configure related class flag in ux_user.h and adjust */ +/** UX_DEVICE_MEMORY_STACK_SIZE */ +/** */ +/** */ +/** AUTHOR */ +/** */ +/** Mohamed AYED */ +/** */ +/**************************************************************************/ +/**************************************************************************/ + +#include "ux_api.h" +#include "ux_device_class_hid.h" + +#ifndef UX_DEVICE_SIDE_ONLY +#error UX_DEVICE_SIDE_ONLY must be defined +#endif + +#if (UX_DEVICE_CLASS_HID_EVENT_BUFFER_LENGTH < 8) +#error HID keyboard event buffer length must be more then 8. +#endif + +/**************************************************/ +/** Define constants */ +/**************************************************/ +#define UX_DEVICE_MEMORY_STACK_SIZE (7*1024) +#define UX_DEMO_THREAD_STACK_SIZE (512) + +#define UX_HID_NUM_LOCK_MASK 0x01 +#define UX_HID_CAPS_LOCK_MASK 0x02 + +/************************************************************/ +/** Demo device class demo callbacks function prototypes */ +/************************************************************/ +VOID ux_demo_device_hid_instance_activate(VOID *hid_instance); +VOID ux_demo_device_hid_instance_deactivate(VOID *hid_instance); +UINT ux_demo_device_hid_callback(UX_SLAVE_CLASS_HID *hid_instance, UX_SLAVE_CLASS_HID_EVENT *hid_event); +UINT ux_demo_device_hid_get_callback(UX_SLAVE_CLASS_HID *hid_instance, UX_SLAVE_CLASS_HID_EVENT *hid_event); + +#ifndef DEMO_TEST +/************************************************************/ +/** usbx application initialization with RTOS */ +/************************************************************/ +VOID tx_application_define(VOID *first_unused_memory); +#endif /* DEMO_TEST */ + +/************************************************************/ +/** usbx device hid keyboard instance */ +/************************************************************/ +UX_SLAVE_CLASS_HID *hid_keyboard; + +/************************************************************/ +/** Thread object */ +/************************************************************/ +static UX_THREAD ux_demo_thread; +static ULONG ux_demo_thread_stack[UX_DEMO_THREAD_STACK_SIZE / sizeof(ULONG)]; +static ULONG ux_demo_thread_size = UX_DEMO_THREAD_STACK_SIZE; +static VOID ux_demo_thread_entry(ULONG thread_input); + +static UX_THREAD ux_device_hid_thread; +static ULONG ux_device_hid_thread_stack[UX_DEMO_THREAD_STACK_SIZE / sizeof(ULONG)]; +static ULONG ux_device_hid_thread_size = UX_DEMO_THREAD_STACK_SIZE; +static VOID ux_device_hid_thread_entry(ULONG thread_input); + +/************************************************************/ +/** usbx demo callback prototype */ +/************************************************************/ +static VOID ux_demo_error_callback(UINT system_level, UINT system_context, UINT error_code); + +/************************************************************/ +/** Demo function prototypes */ +/************************************************************/ +UINT usbx_demo_init(VOID); +UINT usbx_demo_uninit(VOID); +UINT ux_device_hid_init(VOID); +UINT ux_device_hid_uninit(VOID); +UINT ux_device_hid_keyboard_send_character(UX_SLAVE_CLASS_HID *device_hid); + +/************************************************************/ +/** Demo variables */ +/************************************************************/ +static CHAR ux_system_memory_pool[UX_DEVICE_MEMORY_STACK_SIZE]; +ULONG num_lock_flag = UX_FALSE; +ULONG caps_lock_flag = UX_FALSE; + +/************************************************************/ +/** usbx demo extern function prototypes */ +/************************************************************/ +#ifndef EXTERNAL_MAIN +extern int board_setup(void); +#endif /* EXTERNAL_MAIN */ + +#ifdef DEMO_TEST +extern int usb_device_dcd_initialize(void *param); +#endif /* DEMO_TEST */ + +/************************************************************/ +/** USB descriptors */ +/************************************************************/ + +/* USB HID Report descriptor Length */ +#define UX_HID_REPORT_DESCRIPTOR_LENGTH 63 + +/* USB HID Report descriptor */ +unsigned char hid_report_descriptor[] = { + 0x05, 0x01, // USAGE_PAGE (Generic Desktop) + 0x09, 0x06, // USAGE (Keyboard) + 0xA1, 0x01, // COLLECTION (Application) + 0x05, 0x07, // USAGE_PAGE (Keyboard/Keypad) + 0x19, 0xE0, // USAGE_MINIMUM (Left Control) + 0x29, 0xE7, // USAGE_MAXIMUM (Right GUI) + 0x15, 0x00, // LOGICAL_MINIMUM (Logical Min (0)) + 0x25, 0x01, // LOGICAL_MAXIMUM (Logical Max (1)) + 0x75, 0x01, // REPORT_SIZE (1 bit) + 0x95, 0x08, // REPORT_COUNT (8 modifiers) + 0x81, 0x02, // INPUT (Data, Variable, Absolute) + 0x75, 0x08, // REPORT_SIZE (8 bits) + 0x95, 0x01, // REPORT_COUNT (1 byte) + 0x81, 0x01, // INPUT (Constant (Reserved)) + 0x05, 0x08, // USAGE_PAGE (LED) + 0x19, 0x01, // USAGE_MINIMUM (Num Lock) + 0x29, 0x05, // USAGE_MAXIMUM (Kana) + 0x75, 0x01, // REPORT_SIZE (1 bit) + 0x95, 0x05, // REPORT_COUNT (5 LEDs) + 0x91, 0x02, // OUTPUT (Data, Variable, Absolute) + 0x75, 0x03, // REPORT_SIZE (3 bits) + 0x95, 0x01, // REPORT_COUNT (1 report) + 0x91, 0x01, // OUTPUT (Constant (Padding)) + 0x05, 0x07, // USAGE_PAGE (Keyboard/Keypad) + 0x19, 0x00, // USAGE_MINIMUM (No Event) + 0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application) + 0x15, 0x00, // LOGICAL_MINIMUM (Logical Min (0)) + 0x25, 0x65, // LOGICAL_MAXIMUM (Logical Max (101)) + 0x75, 0x08, // REPORT_SIZE (8 bits) + 0x95, 0x06, // REPORT_COUNT (6 keycodes) + 0x81, 0x00, // INPUT (Data, Array) + 0xC0 // END_COLLECTION +}; + + +/* USB High Speed Device Descriptor Length */ +#define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED 62 + +/* USB High Speed Device Descriptor */ +unsigned char device_framework_high_speed[] = { + + /* Device descriptor */ + 0x12, /* bLength */ + 0x01, /* bDescriptorType */ + 0x00, 0x02, /* bcdUSB */ + 0x00, /* bDeviceClass */ + 0x00, /* bDeviceSubClass */ + 0x00, /* bDeviceProtocol */ + 0x40, /* bMaxPacketSize0 */ + 0x0A, 0x1A, /* idVendor */ + 0x40, 0x57, /* idProduct */ + 0x00, 0x01, /* bcdDevice */ + 0x01, /* iManufacturer */ + 0x02, /* iProduct */ + 0x03, /* iSerialNumber */ + 0x01, /* bNumConfigurations */ + + /* Device Qualifier descriptor */ + 0x0A, /* bLength */ + 0x06, /* bDescriptorType */ + 0x00, 0x02, /* bcdUSB */ + 0x00, /* bDeviceClass */ + 0x00, /* bDeviceSubClass */ + 0x00, /* bDeviceProtocol */ + 0x40, /* bMaxPacketSize0 */ + 0x01, /* bNumConfigurations */ + 0x00, /* bReserved */ + + /* Configuration descriptor */ + 0x09, /* bLength */ + 0x02, /* bDescriptorType */ + 0x22, 0x00, /* wTotalLength */ + 0x01, /* bNumInterfaces */ + 0x01, /* bConfigurationValue */ + 0x05, /* iConfiguration */ + 0xC0, /* bmAttributes */ + 0x32, /* bMaxPower */ + + /* HID Interface descriptor */ + 0x09, /* bLength */ + 0x04, /* bDescriptorType */ + 0x00, /* bInterfaceNumber */ + 0x00, /* bAlternateSetting */ + 0x01, /* bNumEndpoints */ + 0x03, /* bInterfaceClass */ + 0x01, /* bInterfaceSubClass */ + 0x01, /* bInterfaceProtocol */ + 0x06, /* iInterface */ + + /* HID Descriptor */ + 0x09, /* bLength */ + 0x21, /* bDescriptorType */ + 0x10, 0x01, /* bcdHID */ + 0x21, /* bCountryCode */ + 0x01, /* bNumDescriptors */ + 0x22, /* bReportDescriptorType */ + 0x3F, 0x00, /* wReportDescriptorLength */ + + /* HID Endpoint IN Descriptor */ + 0x07, /* bLength */ + 0x05, /* bDescriptorType */ + 0x81, /* bEndpointAddress */ + 0x03, /* bmAttributes */ + 0x08, 0x00, /* wMaxPacketSize */ + 0x08 /* bInterval */ +}; + +/* USB Full Speed Device Descriptor Length */ +#define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 52 + +/* USB Full Speed Device Descriptor */ +unsigned char device_framework_full_speed[] = { + + /* Device descriptor */ + 0x12, /* bLength */ + 0x01, /* bDescriptorType */ + 0x00, 0x02, /* bcdUSB */ + 0x00, /* bDeviceClass */ + 0x00, /* bDeviceSubClass */ + 0x00, /* bDeviceProtocol */ + 0x08, /* bMaxPacketSize0 */ + 0x0A, 0x1A, /* idVendor */ + 0x40, 0x57, /* idProduct */ + 0x00, 0x01, /* bcdDevice */ + 0x01, /* iManufacturer */ + 0x02, /* iProduct */ + 0x03, /* iSerialNumber */ + 0x01, /* bNumConfigurations */ + + /* Configuration descriptor */ + 0x09, /* bLength */ + 0x02, /* bDescriptorType */ + 0x22, 0x00, /* wTotalLength */ + 0x01, /* bNumInterfaces */ + 0x01, /* bConfigurationValue */ + 0x04, /* iConfiguration */ + 0xC0, /* bmAttributes */ + 0x32, /* bMaxPower */ + + /* HID Interface descriptor */ + 0x09, /* bLength */ + 0x04, /* bDescriptorType */ + 0x00, /* bInterfaceNumber */ + 0x00, /* bAlternateSetting */ + 0x01, /* bNumEndpoints */ + 0x03, /* bInterfaceClass */ + 0x01, /* bInterfaceSubClass */ + 0x01, /* bInterfaceProtocol */ + 0x06, /* iInterface */ + + /* HID Descriptor */ + 0x09, /* bLength */ + 0x21, /* bDescriptorType */ + 0x10, 0x01, /* bcdHID */ + 0x21, /* bCountryCode */ + 0x01, /* bNumDescriptors */ + 0x22, /* bReportDescriptorType */ + 0x3F, 0x00, /* wReportDescriptorLength */ + + /* HID Endpoint IN Descriptor */ + 0x07, /* bLength */ + 0x05, /* bDescriptorType */ + 0x81, /* bEndpointAddress */ + 0x03, /* bmAttributes */ + 0x08, 0x00, /* wMaxPacketSize */ + 0x08 /* bInterval */ +}; + +/* USB String Device Framework Length */ +#define DEVICE_FRAMEWORK_LENGTH_STRING 84 + +/* USB String Device Framework */ +unsigned char device_framework_string[] = { + /* iManufacturer string descriptor */ + 0x09, 0x04, /* Language ID */ + 0x01, /* String Index */ + 0x0F, /* String Length */ + 'E', 'c', 'l', 'i', 'p', 's', 'e', ' ', 'T', 'h', 'r', 'e', 'a', 'd', 'x', + + /* iProduct string descriptor */ + 0x09, 0x04, /* Language ID */ + 0x02, /* String Index */ + 0x04, /* String Length */ + 'U', 'S', 'B', 'X', + + /* iSerialNumber string descriptor */ + 0x09, 0x04, /* Language ID */ + 0x03, /* String Index */ + 0x0E, /* String Length */ + 'U', 'S', 'B', 'D', 'E', 'V', 'I', 'C', 'E', '0', '0', '0', '0', '1', + + /* iConfiguration string descriptor */ + 0x09, 0x04, /* Language ID */ + 0x04, /* String Index */ + 0x0A, /* String Length */ + 'F', 'U', 'L', 'L', ' ', 'S', 'P', 'E', 'E', 'D', + + /* iConfiguration string descriptor */ + 0x09, 0x04, /* Language ID */ + 0x05, /* String Index */ + 0x0A, /* String Length */ + 'H', 'I', 'G', 'H', ' ', 'S', 'P', 'E', 'E', 'D', + + /* iInterface HID string descriptor */ + 0x09, 0x04, /* Language ID */ + 0x06, /* String Index */ + 0x07, /* String Length */ + 'K', 'e', 'y', 'b', 'o', 'r', 'd' +}; + +/* USB Language ID Framework Length */ +#define DEVICE_FRAMEWORK_LENGTH_LANGUAGE_ID 2 + +/* USB Language ID Framework */ +unsigned char device_framework_language_id[] = { + 0x09, 0x04 /* Language ID (0x0409 = English (US)) */ +}; + + +#ifndef EXTERNAL_MAIN +/********************************************************************/ +/** main */ +/** */ +/** Perform board-level setup and then transfer control to the */ +/** ThreadX kernel so the RTOS sample can start its threads. */ +/********************************************************************/ +int main(void) +{ + /* Initialize the board. */ + board_setup(); + + /* Enter the ThreadX kernel. */ + tx_kernel_enter(); +} +#endif /* EXTERNAL_MAIN */ + +#ifndef DEMO_TEST +/********************************************************************/ +/** tx_application_define */ +/** */ +/** ThreadX application entry used to start the USBX demo once */ +/** the kernel has been initialized. */ +/********************************************************************/ +VOID tx_application_define(VOID *first_unused_memory) +{ + UX_PARAMETER_NOT_USED(first_unused_memory); + + usbx_demo_init(); +} +#endif /* DEMO_TEST */ + +/********************************************************************/ +/** usbx_demo_init */ +/** */ +/** Create the demo threads and initialize the USBX HID device */ +/** stack used by this sample. */ +/********************************************************************/ +UINT usbx_demo_init(VOID) +{ + +UINT status; + + /* Create the main demo thread. */ + status = ux_utility_thread_create(&ux_demo_thread, "usbx_demo_app_thread_entry", + ux_demo_thread_entry, 0, ux_demo_thread_stack, + ux_demo_thread_size, 20, 20, 1, UX_AUTO_START); + + if(status != UX_SUCCESS) + return status; + + /* Create the hid demo thread. */ + status = ux_utility_thread_create(&ux_device_hid_thread, "usbx_hid_app_thread_entry", + ux_device_hid_thread_entry, 0, ux_device_hid_thread_stack, + ux_device_hid_thread_size, 20, 20, 1, UX_AUTO_START); + + if(status != UX_SUCCESS) + return status; + + status = ux_device_hid_init(); + + if(status != UX_SUCCESS) + return status; + + return status; +} + +/********************************************************************/ +/** ux_device_hid_init */ +/** */ +/** Initialize USBX device resources, install the device stack, */ +/** and register the HID keyboard class instance with its */ +/** callbacks. */ +/********************************************************************/ +UINT ux_device_hid_init(VOID) +{ + +UINT status; +UX_SLAVE_CLASS_HID_PARAMETER hid_keyboard_parameter = {UX_NULL}; + +#ifndef DEMO_TEST + /* Initialize USBX Memory. */ + status = ux_system_initialize(ux_system_memory_pool, UX_DEVICE_MEMORY_STACK_SIZE, UX_NULL, 0); + + if(status != UX_SUCCESS) + return status; +#endif /* DEMO_TEST */ + + /* Install the device portion of USBX. */ + status = ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED, + device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED, + device_framework_string, DEVICE_FRAMEWORK_LENGTH_STRING, + device_framework_language_id, DEVICE_FRAMEWORK_LENGTH_LANGUAGE_ID, + UX_NULL); + + if(status != UX_SUCCESS) + return status; + + /* Initialize the hid keyboard class parameters for the device. */ + hid_keyboard_parameter.ux_slave_class_hid_instance_activate = ux_demo_device_hid_instance_activate; + hid_keyboard_parameter.ux_slave_class_hid_instance_deactivate = ux_demo_device_hid_instance_deactivate; + hid_keyboard_parameter.ux_device_class_hid_parameter_report_address = hid_report_descriptor; + hid_keyboard_parameter.ux_device_class_hid_parameter_report_length = UX_HID_REPORT_DESCRIPTOR_LENGTH; + hid_keyboard_parameter.ux_device_class_hid_parameter_report_id = UX_FALSE; + hid_keyboard_parameter.ux_device_class_hid_parameter_callback = ux_demo_device_hid_callback; + hid_keyboard_parameter.ux_device_class_hid_parameter_get_callback = ux_demo_device_hid_get_callback; + + /* Initialize the device hid class. The class is connected with interface 0 on configuration 1. */ + status = ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry, + 1, 0, (VOID *)&hid_keyboard_parameter); + + if(status != UX_SUCCESS) + return status; + + /* Register error callback. */ + ux_utility_error_callback_register(ux_demo_error_callback); + + return status; +} + +/********************************************************************/ +/** usbx_demo_uninit */ +/** */ +/** Stop the demo worker threads created by usbx_demo_init so the */ +/** RTOS sample can be shut down cleanly. */ +/********************************************************************/ +UINT usbx_demo_uninit(VOID) +{ + +UINT status; + + /* Delete the main demo thread. */ + status = ux_utility_thread_delete(&ux_demo_thread); + + /* Delete the hid demo thread. */ + status = ux_utility_thread_delete(&ux_device_hid_thread); + + if(status != UX_SUCCESS) + return status; + + return status; +} + +/********************************************************************/ +/** ux_device_hid_uninit */ +/** */ +/** Tear down the HID device stack and delete the threads created */ +/** for this RTOS sample. */ +/********************************************************************/ +UINT ux_device_hid_uninit(VOID) +{ + +UINT status; + + /* Uninitialize USBX Memory. */ + status = ux_device_stack_uninitialize(); + + if(status != UX_SUCCESS) + return status; + + /* Uninitialize the device hid class. */ + status = ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry); + + if(status != UX_SUCCESS) + return status; + + return status; +} + +/********************************************************************/ +/** ux_demo_device_hid_instance_activate */ +/** */ +/** Store the HID class instance pointer when the device becomes */ +/** active so the worker thread can start sending reports. */ +/********************************************************************/ +VOID ux_demo_device_hid_instance_activate(VOID *hid_instance) +{ + if (hid_keyboard == UX_NULL) + hid_keyboard = (UX_SLAVE_CLASS_HID*) hid_instance; +} + +/********************************************************************/ +/** ux_demo_device_hid_instance_deactivate */ +/** */ +/** Clear the cached HID class instance pointer when the device */ +/** is deactivated or disconnected. */ +/********************************************************************/ +VOID ux_demo_device_hid_instance_deactivate(VOID *hid_instance) +{ + if (hid_instance == (VOID *)hid_keyboard) + hid_keyboard = UX_NULL; +} + +/********************************************************************/ +/** ux_demo_device_hid_callback */ +/** */ +/** Process keyboard output reports from the host and mirror the */ +/** Num Lock and Caps Lock LED state into local flags. */ +/********************************************************************/ +UINT ux_demo_device_hid_callback(UX_SLAVE_CLASS_HID *hid_instance, UX_SLAVE_CLASS_HID_EVENT *hid_event) +{ + UX_PARAMETER_NOT_USED(hid_instance); + + /* There was an event. Analyze it. Is it NUM LOCK ? */ + if ((hid_event -> ux_device_class_hid_event_buffer[0] & UX_HID_NUM_LOCK_MASK) && + (hid_event -> ux_device_class_hid_event_report_type == UX_DEVICE_CLASS_HID_REPORT_TYPE_OUTPUT)) + + /* Set the Num lock flag. */ + num_lock_flag = UX_TRUE; + else + /* Reset the Num lock flag. */ + num_lock_flag = UX_FALSE; + + /* There was an event. Analyze it. Is it CAPS LOCK ? */ + if ((hid_event -> ux_device_class_hid_event_buffer[0] & UX_HID_CAPS_LOCK_MASK) && + (hid_event -> ux_device_class_hid_event_report_type == UX_DEVICE_CLASS_HID_REPORT_TYPE_OUTPUT)) + /* Set the Caps lock flag. */ + caps_lock_flag = UX_TRUE; + else + /* Reset the Caps lock flag. */ + caps_lock_flag = UX_FALSE; + + return UX_SUCCESS; +} + +/********************************************************************/ +/** ux_demo_device_hid_get_callback */ +/** */ +/** Respond to HID GET-style requests that require application */ +/** data. The keyboard demo has no dynamic data to return, so the */ +/** request completes successfully without modifying the event. */ +/********************************************************************/ +UINT ux_demo_device_hid_get_callback(UX_SLAVE_CLASS_HID *hid_instance, UX_SLAVE_CLASS_HID_EVENT *hid_event) +{ + UX_PARAMETER_NOT_USED(hid_instance); + UX_PARAMETER_NOT_USED(hid_event); + + return UX_SUCCESS; +} + +/********************************************************************/ +/** ux_demo_thread_entry */ +/** */ +/** Register the device controller used by the sample, either the */ +/** simulator DCD for tests or the board-specific DCD provided by */ +/** the platform. */ +/********************************************************************/ +static VOID ux_demo_thread_entry(ULONG thread_input) +{ + + UX_PARAMETER_NOT_USED(thread_input); + +#ifdef DEMO_TEST + + /* Register the USB device simulator controllers for testing */ + ux_dcd_sim_slave_initialize(); +#else /* DEMO_TEST */ + + /* Register the USB device controllers available in this system */ + usb_device_dcd_initialize(UX_NULL); +#endif /* DEMO_TEST */ + +} + +/********************************************************************/ +/** ux_device_hid_thread_entry */ +/** */ +/** Poll the device state and submit HID keyboard reports while */ +/** the device is configured. When disconnected, the thread */ +/** sleeps until the host enumerates the device again. */ +/********************************************************************/ +static VOID ux_device_hid_thread_entry(ULONG thread_input) +{ + + UX_PARAMETER_NOT_USED(thread_input); + + + while (1) + { + /* Check if the device state already configured. */ + if ((UX_SLAVE_DEVICE_CHECK_STATE(UX_DEVICE_CONFIGURED)) && (hid_keyboard != UX_NULL)) + { + ux_device_hid_keyboard_send_character(hid_keyboard); + } + else + { + /* Sleep thread for 10ms. */ + ux_utility_delay_ms(MS_TO_TICK(10)); + } + } +} + +/********************************************************************/ +/** ux_device_hid_keyboard_send_character */ +/** */ +/** Build and send one HID keyboard report sequence that presses */ +/** and releases the next key in the demo stream, then advances */ +/** through the alphabet until all characters have been sent. */ +/********************************************************************/ +UINT ux_device_hid_keyboard_send_character(UX_SLAVE_CLASS_HID *device_hid) +{ + +UINT status = UX_SUCCESS; +UX_SLAVE_CLASS_HID_EVENT device_hid_event; +static UCHAR key = 4; + + /* Reset the HID event structure. */ + ux_utility_memory_set(&device_hid_event, 0, sizeof(UX_SLAVE_CLASS_HID_EVENT)); + + if (key != 0) + { + /* Sleep thread for 20ms. */ + ux_utility_delay_ms(MS_TO_TICK(20)); + + /* Then insert a key into the keyboard event. Length is fixed to 8. */ + device_hid_event.ux_device_class_hid_event_report_id = 0; + device_hid_event.ux_device_class_hid_event_report_type = UX_DEVICE_CLASS_HID_REPORT_TYPE_INPUT; + device_hid_event.ux_device_class_hid_event_length = 8; + device_hid_event.ux_device_class_hid_event_buffer[0] = 0; /* 0x02: Left Shift modifier */ + device_hid_event.ux_device_class_hid_event_buffer[1] = 0; + device_hid_event.ux_device_class_hid_event_buffer[2] = key; /* key */ + device_hid_event.ux_device_class_hid_event_buffer[3] = 0; + device_hid_event.ux_device_class_hid_event_buffer[4] = 0; + device_hid_event.ux_device_class_hid_event_buffer[5] = 0; + device_hid_event.ux_device_class_hid_event_buffer[6] = 0; + device_hid_event.ux_device_class_hid_event_buffer[7] = 0; + + /* Set the keyboard event. */ + status = ux_device_class_hid_event_set(device_hid, &device_hid_event); + + if (status != UX_SUCCESS) + return status; + + /* Next event has the key depressed. */ + device_hid_event.ux_device_class_hid_event_buffer[2] = 0; + + /* Set the keyboard event. */ + status = ux_device_class_hid_event_set(device_hid, &device_hid_event); + + if (status != UX_SUCCESS) + return status; + + /* Are we at the end of alphabet ? */ + if (key != (0x04 + 25)) + /* Next key. */ + key++; + else + key = 0; + } + else + { + /* Sleep thread for 10ms. */ + ux_utility_delay_ms(MS_TO_TICK(10)); + } + + return status; +} + +/********************************************************************/ +/** ux_demo_error_callback */ +/** */ +/** Print USBX error details to aid debugging when the stack */ +/** reports runtime failures. */ +/********************************************************************/ +static VOID ux_demo_error_callback(UINT system_level, UINT system_context, UINT error_code) +{ + /* + * Refer to ux_api.h. For example, + * UX_SYSTEM_LEVEL_INTERRUPT, UX_SYSTEM_CONTEXT_DCD, UX_DEVICE_HANDLE_UNKNOWN + */ + printf("USBX error: system level(%d), context(%d), error code(0x%x)\r\n", system_level, system_context, error_code); +} diff --git a/samples/demo_device_hid_mouse_rtos.c b/samples/demo_device_hid_mouse_rtos.c index d9a33ecb9..d5ec6289c 100644 --- a/samples/demo_device_hid_mouse_rtos.c +++ b/samples/demo_device_hid_mouse_rtos.c @@ -14,15 +14,17 @@ /** */ /** Overview */ /** */ -/** This example works as a USB HID device. It will appear as a USB */ -/** mouse device on PC. This application demo is running in rtos */ -/** mode. */ +/** This sample configures USBX device mode to enumerate as a USB HID */ +/** mouse on the host. It runs with RTOS, creates dedicated threads */ +/** for device-controller setup and HID report generation, and sends */ +/** periodic input reports that move the host cursor in a rectangular */ +/** pattern after the device has been configured by the host. */ /** */ /** Note */ /** */ /** This demonstration is not optimized, to optimize application user */ /** should configure related class flag in ux_user.h and adjust */ -/** DEMO_STACK_SIZE and UX_DEVICE_MEMORY_STACK_SIZE */ +/** UX_DEVICE_MEMORY_STACK_SIZE */ /** */ /** */ /** AUTHOR */ @@ -32,357 +34,327 @@ /**************************************************************************/ /**************************************************************************/ -#include "tx_api.h" #include "ux_api.h" #include "ux_device_class_hid.h" -/* if defined mouse with absolute positioning is a type of USB HID mouse that reports its position using - absolute coordinates rather than relative movement deltas. This is common in touch devices, graphics tablets, - and some remote control devices, where the report indicates an (X, Y) position within a defined logical range, - not just movement increments.*/ -/* #define UX_DEMO_MOUSE_ABSOLUTE */ - -/* Defined the mouse will act as boot device. */ -#define DEMO_HID_BOOT_DEVICE - -/**************************************************/ -/** Define constants */ -/**************************************************/ -#define DEMO_STACK_SIZE 4*1024 -#define UX_DEVICE_MEMORY_STACK_SIZE 7*1024 - -#define UX_DEMO_HID_DEVICE_VID 0x090A -#define UX_DEMO_HID_DEVICE_PID 0x4036 - -#define UX_DEMO_MAX_EP0_SIZE 0x40U -#define UX_DEMO_HID_CONFIG_DESC_SIZE 0x22U -#define UX_DEMO_BCD_USB 0x0200 -#define UX_DEMO_BCD_HID 0x0110 - -#define UX_DEMO_HID_ENDPOINT_SIZE 0x08 -#define UX_DEMO_HID_ENDPOINT_ADDRESS 0x81 -#define UX_DEMO_HID_ENDPOINT_BINTERVAL 0x08 - -#ifdef DEMO_HID_BOOT_DEVICE -#define UX_DEMO_HID_SUBCLASS 0x01 -#else /* DEMO_HID_BOOT_DEVICE */ -#define UX_DEMO_HID_SUBCLASS 0x00 +#ifndef UX_DEVICE_SIDE_ONLY +#error UX_DEVICE_SIDE_ONLY must be defined #endif -#ifdef UX_DEMO_MOUSE_ABSOLUTE -#define UX_DEMO_HID_MOUSE_CURSOR_MOVE 350 -#else /* UX_DEMO_MOUSE_ABSOLUTE */ -#define UX_DEMO_HID_MOUSE_CURSOR_MOVE 3 -#define UX_DEMO_HID_MOUSE_CURSOR_MOVE_N 100 -#endif /* UX_DEMO_MOUSE_ABSOLUTE */ - -#define UX_MOUSE_CURSOR_MOVE_RIGHT 0x00 -#define UX_MOUSE_CURSOR_MOVE_DOWN 0x01 -#define UX_MOUSE_CURSOR_MOVE_LEFT 0x02 -#define UX_MOUSE_CURSOR_MOVE_UP 0x03 - -/**************************************************/ -/** usbx device hid demo callbacks */ -/**************************************************/ +#if (UX_DEVICE_CLASS_HID_EVENT_BUFFER_LENGTH < 4) +#error HID mouse event buffer length must be more then 4. +#endif + +/************************************************************/ +/** Define constants */ +/************************************************************/ +#define UX_DEVICE_MEMORY_STACK_SIZE (7*1024) +#define UX_DEMO_THREAD_STACK_SIZE (512) + +#define UX_DEMO_HID_MOUSE_CURSOR_MOVE 3 +#define UX_DEMO_HID_MOUSE_CURSOR_MOVE_N 100 + +#define UX_MOUSE_CURSOR_MOVE_RIGHT 0x01 +#define UX_MOUSE_CURSOR_MOVE_DOWN 0x02 +#define UX_MOUSE_CURSOR_MOVE_LEFT 0x03 +#define UX_MOUSE_CURSOR_MOVE_UP 0x04 + +/************************************************************/ +/** Demo device class demo callbacks function prototypes */ +/************************************************************/ VOID ux_demo_device_hid_instance_activate(VOID *hid_instance); VOID ux_demo_device_hid_instance_deactivate(VOID *hid_instance); UINT ux_demo_device_hid_callback(UX_SLAVE_CLASS_HID *hid_instance, UX_SLAVE_CLASS_HID_EVENT *hid_event); UINT ux_demo_device_hid_get_callback(UX_SLAVE_CLASS_HID *hid_instance, UX_SLAVE_CLASS_HID_EVENT *hid_event); -/**************************************************/ -/** usbx application initialization with RTOS */ -/**************************************************/ +/************************************************************/ +/** usbx application initialization with RTOS */ +/************************************************************/ +#ifndef DEMO_TEST VOID tx_application_define(VOID *first_unused_memory); +#endif /* DEMO_TEST */ -/**************************************************/ -/** usbx device hid demo thread */ -/**************************************************/ -VOID ux_demo_device_hid_thread_entry(ULONG thread_input); - -/**************************************************/ -/** usbx device hid demo mouse */ -/**************************************************/ -UINT ux_demo_hid_mouse_buttons(UX_SLAVE_CLASS_HID *device_hid); -UINT ux_demo_hid_mouse_scroll_wheel(UX_SLAVE_CLASS_HID *device_hid); -#ifndef UX_DEMO_MOUSE_ABSOLUTE -UINT ux_demo_hid_mouse_cursor_move(UX_SLAVE_CLASS_HID *device_hid); -#else -UINT ux_demo_hid_mouse_absolute_cursor_move(UX_SLAVE_CLASS_HID *device_hid); -#endif /* UX_DEMO_MOUSE_ABSOLUTE */ - -/**************************************************/ -/** usbx callback error */ -/**************************************************/ -VOID ux_demo_error_callback(UINT system_level, UINT system_context, UINT error_code); +/************************************************************/ +/** usbx device hid mouse instance */ +/************************************************************/ +UX_SLAVE_CLASS_HID *hid_mouse; +/************************************************************/ +/** Thread object */ +/************************************************************/ +static UX_THREAD ux_demo_thread; +static ULONG ux_demo_thread_stack[UX_DEMO_THREAD_STACK_SIZE / sizeof(ULONG)]; +static ULONG ux_demo_thread_size = UX_DEMO_THREAD_STACK_SIZE; +static VOID ux_demo_thread_entry(ULONG thread_input); + +static UX_THREAD ux_device_hid_thread; +static ULONG ux_device_hid_thread_stack[UX_DEMO_THREAD_STACK_SIZE / sizeof(ULONG)]; +static ULONG ux_device_hid_thread_size = UX_DEMO_THREAD_STACK_SIZE; +static VOID ux_device_hid_thread_entry(ULONG thread_input); + +/************************************************************/ +/** usbx demo callback prototype */ +/************************************************************/ +static VOID ux_demo_error_callback(UINT system_level, UINT system_context, UINT error_code); + +/************************************************************/ +/** usbx device hid demo mouse */ +/************************************************************/ +UINT usbx_demo_init(VOID); +UINT usbx_demo_uninit(VOID); +UINT ux_device_hid_init(VOID); +UINT ux_device_hid_uninit(VOID); +UINT ux_device_hid_mouse_cursor_move(UX_SLAVE_CLASS_HID *device_hid); + +/************************************************************/ +/** Demo variables */ +/************************************************************/ +static CHAR ux_system_memory_pool[UX_DEVICE_MEMORY_STACK_SIZE]; + +/************************************************************/ +/** usbx demo extern function prototypes */ +/************************************************************/ #ifndef EXTERNAL_MAIN extern int board_setup(void); #endif /* EXTERNAL_MAIN */ -extern int usb_device_dcd_initialize(void *param); -/**************************************************/ -/** usbx device hid mouse instance */ -/**************************************************/ -UX_SLAVE_CLASS_HID *hid_mouse; - -/**************************************************/ -/** thread object */ -/**************************************************/ -static TX_THREAD ux_hid_thread; - -/**************************************************/ -/** HID Report descriptor */ -/**************************************************/ -UCHAR hid_mouse_report[] = { - 0x05, 0x01, // USAGE_PAGE (Generic Desktop) - 0x09, 0x02, // USAGE (Mouse) - 0xa1, 0x01, // COLLECTION (Application) - - /* Pointer and Physical are required by Apple Recovery */ - 0x09, 0x01, // USAGE (Pointer) - 0xa1, 0x00, // COLLECTION (Physical) - - /* 3 Buttons */ - 0x05, 0x09, // USAGE_PAGE (Button) - 0x19, 0x01, // USAGE_MINIMUM (Button 1) - 0x29, 0x03, // USAGE_MAXIMUM (Button 3) - 0x15, 0x00, // LOGICAL_MINIMUM (0) - 0x25, 0x01, // LOGICAL_MAXIMUM (1) - 0x75, 0x01, // REPORT_SIZE (1) - 0x95, 0x03, // REPORT_COUNT (3) -> 3 buttons - 0x81, 0x02, // INPUT (Data, Variable, Absolute) -> Buttons - - 0x75, 0x05, // REPORT_SIZE (5) - 0x95, 0x01, // REPORT_COUNT (1) - 0x81, 0x03, // INPUT (Constant, Variable, Absolute) -> Padding bits - - /* X, Y */ - 0x05, 0x01, // USAGE_PAGE (Generic Desktop) - 0x09, 0x30, // USAGE (X) - 0x09, 0x31, // USAGE (Y) -#ifdef UX_DEMO_MOUSE_ABSOLUTE - 0x16, 0x00, 0x00, // LOGICAL_MINIMUM (0) - 0x26, 0xFF, 0x7F, // LOGICAL_MAXIMUM (32767) (0x7FFF) - 0x75, 0x10, // REPORT_SIZE (16) (2 bytes per axis) - 0x95, 0x02, // REPORT_COUNT (2) -> X, Y position - 0x81, 0x02, // INPUT (Data, Variable, Absolute) -> Absolute X, Y position -#else /* UX_DEMO_MOUSE_ABSOLUTE */ - 0x15, 0x81, // LOGICAL_MINIMUM (-127) - 0x25, 0x7F, // LOGICAL_MAXIMUM (127) - 0x75, 0x08, // REPORT_SIZE (8) (1 bytes per axis) - 0x95, 0x02, // REPORT_COUNT (2) -> X, Y movement - 0x81, 0x06, // INPUT (Data, Variable, Relative) -> X, Y are relative -#endif /* UX_DEMO_MOUSE_ABSOLUTE */ - - /* Wheel */ - 0x09, 0x38, // USAGE (Mouse Wheel) - 0x15, 0x81, // LOGICAL_MINIMUM (-127) - 0x25, 0x7F, // LOGICAL_MAXIMUM (127) - 0x75, 0x08, // REPORT_SIZE (8) - 0x95, 0x01, // REPORT_COUNT (1) -> Wheel movement - 0x81, 0x06, // INPUT (Data, Variable, Relative) -> Wheel - - /* End */ - 0xC0, // END_COLLECTION - 0xC0 // END_COLLECTION +#ifndef DEMO_TEST +extern int usb_device_dcd_initialize(void *param); +#endif /* DEMO_TEST */ + +/************************************************************/ +/** USB descriptors */ +/************************************************************/ + +/* USB HID Report descriptor Length */ +#define UX_HID_REPORT_DESCRIPTOR_LENGTH 62 + +/* USB HID Report descriptor */ +unsigned char hid_report_descriptor[] = { + 0x05, 0x01, // USAGE_PAGE (Generic Desktop) + 0x09, 0x02, // USAGE (Mouse) + 0xA1, 0x01, // COLLECTION (Application) + 0x09, 0x01, // USAGE (Pointer) + 0xA1, 0x00, // COLLECTION (Physical) + 0x05, 0x09, // USAGE_PAGE (Button) + 0x19, 0x01, // USAGE_MINIMUM (Button 1) + 0x29, 0x03, // USAGE_MAXIMUM (Button 3) + 0x15, 0x00, // LOGICAL_MINIMUM (Logical Min (0)) + 0x25, 0x01, // LOGICAL_MAXIMUM (Logical Max (1)) + 0x75, 0x01, // REPORT_SIZE (1 bit) + 0x95, 0x03, // REPORT_COUNT (3 buttons) + 0x81, 0x02, // INPUT (Data, Variable, Absolute) + 0x75, 0x05, // REPORT_SIZE (5 bits) + 0x95, 0x01, // REPORT_COUNT (1 report) + 0x81, 0x03, // INPUT (Constant, Variable, Absolute (Padding)) + 0x05, 0x01, // USAGE_PAGE (Generic Desktop) + 0x09, 0x30, // USAGE (X) + 0x09, 0x31, // USAGE (Y) + 0x15, 0x81, // LOGICAL_MINIMUM (Logical Min (-127)) + 0x25, 0x7F, // LOGICAL_MAXIMUM (Logical Max (127)) + 0x75, 0x08, // REPORT_SIZE (8 bits) + 0x95, 0x02, // REPORT_COUNT (2 axes (X, Y)) + 0x81, 0x06, // INPUT (Data, Variable, Relative) + 0x09, 0x38, // USAGE (Wheel) + 0x15, 0x81, // LOGICAL_MINIMUM (Logical Min (-127)) + 0x25, 0x7F, // LOGICAL_MAXIMUM (Logical Max (127)) + 0x75, 0x08, // REPORT_SIZE (8 bits) + 0x95, 0x01, // REPORT_COUNT (1 wheel) + 0x81, 0x06, // INPUT (Data, Variable, Relative) + 0xC0, // END_COLLECTION + 0xC0 // END_COLLECTION }; -#define UX_HID_MOUSE_REPORT_LENGTH (sizeof(hid_mouse_report)/sizeof(hid_mouse_report[0])) +/* USB High Speed Device Descriptor Length */ +#define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED 62 -#define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED sizeof(ux_demo_device_framework_full_speed) +/* USB High Speed Device Descriptor */ +unsigned char device_framework_high_speed[] = { -UCHAR ux_demo_device_framework_full_speed[] = { /* Device descriptor */ - 0x12, /* bLength */ - 0x01, /* bDescriptorType */ - UX_W0(UX_DEMO_BCD_USB), UX_W1(UX_DEMO_BCD_USB), /* bcdUSB */ - 0x00, /* bDeviceClass : 0x00 : Interface-defined */ - 0x00, /* bDeviceSubClass : 0x00 : Reset */ - 0x00, /* bDeviceProtocol : 0x00 : Reset */ - UX_DEMO_MAX_EP0_SIZE, /* bMaxPacketSize0 */ - UX_W0(UX_DEMO_HID_DEVICE_VID), UX_W1(UX_DEMO_HID_DEVICE_VID), /* idVendor : ... */ - UX_W0(UX_DEMO_HID_DEVICE_PID), UX_W1(UX_DEMO_HID_DEVICE_PID), /* idProduct */ - 0x00, 0x00, /* bcdDevice */ - 0x01, /* iManufacturer */ - 0x02, /* iProduct */ - 0x03, /* iSerialNumber */ - 0x01, /* bNumConfigurations */ - - /* Configuration Descriptor, total 34 */ - 0x09, /* bLength */ - 0x02, /* bDescriptorType */ - UX_W0(UX_DEMO_HID_CONFIG_DESC_SIZE), /* wTotalLength */ - UX_W1(UX_DEMO_HID_CONFIG_DESC_SIZE), - 0x01, /* bNumInterfaces */ - 0x01, /* bConfigurationValue */ - 0x04, /* iConfiguration */ - 0xC0, /* bmAttributes */ - /* D6 : 0x1 : Self-powered */ - /* D5, Remote Wakeup : 0x0 : Not supported */ - 0x32, /* bMaxPower : 50 : 100mA */ - - /* Interface descriptor */ - 0x09, /* bLength */ - 0x04, /* bDescriptorType */ - 0x00, /* bInterfaceNumber */ - 0x00, /* bAlternateSetting */ - 0x01, /* bNumEndpoints */ - 0x03, /* bInterfaceClass : 0x03 : HID */ - UX_DEMO_HID_SUBCLASS, /* bInterfaceSubClass : ... : Boot/non-boot Subclass */ - 0x02, /* bInterfaceProtocol : 0x00 : Undefined */ - 0x06, /* iInterface */ - - /* HID Descriptor */ - 0x09, /* bLength : 9 */ - 0x21, /* bDescriptorType : 0x21 : HID descriptor */ - 0x10, 0x01, /* bcdHID : 0x0110 */ - 0x21, /* bCountryCode : 33 : US */ - 0x01, /* bNumDescriptors */ - 0x22, /* bReportDescriptorType1 : 0x22 : Report descriptor */ - UX_W0(UX_HID_MOUSE_REPORT_LENGTH), /* wDescriptorLength1 */ - UX_W1(UX_HID_MOUSE_REPORT_LENGTH), - - /* Endpoint Descriptor */ - 0x07, /* bLength */ - 0x05, /* bDescriptorType */ - UX_DEMO_HID_ENDPOINT_ADDRESS, /* bEndpointAddress */ - /* D7, Direction : 0x01 */ - /* D3..0, Endpoint number : 2 */ - 0x03, /* bmAttributes */ - /* D1..0, Transfer Type : 0x3 : Interrupt */ - /* D3..2, Synchronization Type : 0x0 : No Synchronization */ - /* D5..4, Usage Type : 0x0 : Data endpoint */ - UX_W0(UX_DEMO_HID_ENDPOINT_SIZE), /* wMaxPacketSize */ - UX_W1(UX_DEMO_HID_ENDPOINT_SIZE), /* D10..0, Max Packet Size */ - /* D12..11, Additional transactions : 0x00 */ - UX_DEMO_HID_ENDPOINT_BINTERVAL, /* bInterval : 8 : 8ms / x128 (FS 128ms/HS 16ms) */ -}; - -#define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED sizeof(ux_demo_device_framework_high_speed) -UCHAR ux_demo_device_framework_high_speed[] = { - /* Device descriptor */ - 0x12, /* bLength */ - 0x01, /* bDescriptorType */ - UX_W0(UX_DEMO_BCD_USB), UX_W1(UX_DEMO_BCD_USB), /* bcdUSB */ - 0x00, /* bDeviceClass : 0x00 : Interface-defined */ - 0x00, /* bDeviceSubClass : 0x00 : Reset */ - 0x00, /* bDeviceProtocol : 0x00 : Reset */ - UX_DEMO_MAX_EP0_SIZE, /* bMaxPacketSize0 */ - UX_W0(UX_DEMO_HID_DEVICE_VID), UX_W1(UX_DEMO_HID_DEVICE_VID), /* idVendor : ... */ - UX_W0(UX_DEMO_HID_DEVICE_PID), UX_W1(UX_DEMO_HID_DEVICE_PID), /* idProduct */ - 0x01, 0x00, /* bcdDevice */ - 0x01, /* iManufacturer */ - 0x02, /* iProduct */ - 0x03, /* iSerialNumber */ - 0x01, /* bNumConfigurations */ - - /* Device qualifier descriptor */ - 0x0A, /* bLength */ - 0x06, /* bDescriptorType */ - UX_W0(UX_DEMO_BCD_USB), UX_W1(UX_DEMO_BCD_USB), /* bcdUSB */ - 0x00, /* bDeviceClass : 0x00 : Interface-defined */ - 0x00, /* bDeviceSubClass : 0x00 : Reset */ - 0x00, /* bDeviceProtocol : 0x00 : Reset */ - UX_DEMO_MAX_EP0_SIZE, /* bMaxPacketSize0 */ - 0x01, /* bNumConfigurations */ - 0x00, /* bReserved */ + 0x12, /* bLength */ + 0x01, /* bDescriptorType */ + 0x00, 0x02, /* bcdUSB */ + 0x00, /* bDeviceClass */ + 0x00, /* bDeviceSubClass */ + 0x00, /* bDeviceProtocol */ + 0x40, /* bMaxPacketSize0 */ + 0x0A, 0x1A, /* idVendor */ + 0x40, 0x57, /* idProduct */ + 0x01, 0x00, /* bcdDevice */ + 0x01, /* iManufacturer */ + 0x02, /* iProduct */ + 0x03, /* iSerialNumber */ + 0x01, /* bNumConfigurations */ + + /* Device Qualifier descriptor */ + 0x0A, /* bLength */ + 0x06, /* bDescriptorType */ + 0x00, 0x02, /* bcdUSB */ + 0x00, /* bDeviceClass */ + 0x00, /* bDeviceSubClass */ + 0x00, /* bDeviceProtocol */ + 0x40, /* bMaxPacketSize0 */ + 0x01, /* bNumConfigurations */ + 0x00, /* bReserved */ /* Configuration descriptor */ - 0x09, /* bLength */ - 0x02, /* bDescriptorType */ - UX_W0(UX_DEMO_HID_CONFIG_DESC_SIZE), /* wTotalLength */ - UX_W1(UX_DEMO_HID_CONFIG_DESC_SIZE), - 0x01, /* bNumInterfaces */ - 0x01, /* bConfigurationValue */ - 0x05, /* iConfiguration */ - 0xC0, /* bmAttributes */ - /* D6 : 0x1 : Self-powered */ - /* D5, Remote Wakeup : 0x0 : Not supported */ - 0x19, /* bMaxPower : 50 : 100mA */ - - /* Interface descriptor */ - 0x09, /* bLength */ - 0x04, /* bDescriptorType */ - 0x00, /* bInterfaceNumber */ - 0x00, /* bAlternateSetting */ - 0x01, /* bNumEndpoints */ - 0x03, /* bInterfaceClass : 0x03 : HID */ - UX_DEMO_HID_SUBCLASS, /* bInterfaceSubClass : ... : Boot/non-boot Subclass */ - 0x02, /* bInterfaceProtocol : 0x00 : Undefined */ - 0x06, /* iInterface */ + 0x09, /* bLength */ + 0x02, /* bDescriptorType */ + 0x22, 0x00, /* wTotalLength */ + 0x01, /* bNumInterfaces */ + 0x01, /* bConfigurationValue */ + 0x05, /* iConfiguration */ + 0xC0, /* bmAttributes */ + 0x32, /* bMaxPower */ + + /* HID Interface descriptor */ + 0x09, /* bLength */ + 0x04, /* bDescriptorType */ + 0x00, /* bInterfaceNumber */ + 0x00, /* bAlternateSetting */ + 0x01, /* bNumEndpoints */ + 0x03, /* bInterfaceClass */ + 0x01, /* bInterfaceSubClass */ + 0x02, /* bInterfaceProtocol */ + 0x06, /* iInterface */ /* HID Descriptor */ - 0x09, /* bLength : 9 */ - 0x21, /* bDescriptorType : 0x21 : HID descriptor */ - UX_W0(UX_DEMO_BCD_HID), UX_W1(UX_DEMO_BCD_HID), /* bcdHID : 0x0110 */ - 0x21, /* bCountryCode : 33 : US */ - 0x01, /* bNumDescriptors */ - 0x22, /* bReportDescriptorType1 : 0x22 : Report descriptor */ - UX_W0(UX_HID_MOUSE_REPORT_LENGTH), /* wDescriptorLength1 */ - UX_W1(UX_HID_MOUSE_REPORT_LENGTH), - - /* Endpoint Descriptor (Interrupt In) */ - 0x07, /* bLength */ - 0x05, /* bDescriptorType */ - UX_DEMO_HID_ENDPOINT_ADDRESS, /* bEndpointAddress */ - /* D7, Direction : 0x01 */ - /* D3..0, Endpoint number : 2 */ - 0x03, /* bmAttributes */ - /* D1..0, Transfer Type : 0x3 : Interrupt */ - /* D3..2, Synchronization Type : 0x0 : No Synchronization */ - /* D5..4, Usage Type : 0x0 : Data endpoint */ - UX_W0(UX_DEMO_HID_ENDPOINT_SIZE), /* wMaxPacketSize */ - UX_W1(UX_DEMO_HID_ENDPOINT_SIZE), /* D10..0, Max Packet Size */ - /* D12..11, Additional transactions : 0x00 */ - UX_DEMO_HID_ENDPOINT_BINTERVAL, /* bInterval : 8 : 8ms / x128 (FS 128ms/HS 16ms) */ + 0x09, /* bLength */ + 0x21, /* bDescriptorType */ + 0x10, 0x01, /* bcdHID */ + 0x21, /* bCountryCode */ + 0x01, /* bNumDescriptors */ + 0x22, /* bReportDescriptorType */ + 0x3E, 0x00, /* wReportDescriptorLength */ + + /* HID Endpoint IN Descriptor */ + 0x07, /* bLength */ + 0x05, /* bDescriptorType */ + 0x81, /* bEndpointAddress */ + 0x03, /* bmAttributes */ + 0x08, 0x00, /* wMaxPacketSize */ + 0x08 /* bInterval */ }; +/* USB Full Speed Device Descriptor Length */ +#define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 52 -/* String Device Framework : - Byte 0 and 1 : Word containing the language ID : 0x0904 for US - Byte 2 : Byte containing the index of the descriptor - Byte 3 : Byte containing the length of the descriptor string -*/ -#define STRING_FRAMEWORK_LENGTH sizeof(ux_demo_string_framework) -UCHAR ux_demo_string_framework[] = { +/* USB Full Speed Device Descriptor */ +unsigned char device_framework_full_speed[] = { - /* iManufacturer string descriptor : Index 1 */ - 0x09, 0x04, 0x01, 12, - 'U', 'S', 'B', 'X', ' ', 'e', 'c', 'l', 'i', 'p', 's', 'e', + /* Device descriptor */ + 0x12, /* bLength */ + 0x01, /* bDescriptorType */ + 0x00, 0x02, /* bcdUSB */ + 0x00, /* bDeviceClass */ + 0x00, /* bDeviceSubClass */ + 0x00, /* bDeviceProtocol */ + 0x40, /* bMaxPacketSize0 */ + 0x0A, 0x1A, /* idVendor */ + 0x40, 0x57, /* idProduct */ + 0x01, 0x00, /* bcdDevice */ + 0x01, /* iManufacturer */ + 0x02, /* iProduct */ + 0x03, /* iSerialNumber */ + 0x01, /* bNumConfigurations */ - /* iProduct string descriptor : Index 2 */ - 0x09, 0x04, 0x02, 14, - 'H', 'I', 'D', ' ', 'M', 'o', 'u', 's', 'e', ' ', 'D', 'e', 'm', 'o', + /* Configuration descriptor */ + 0x09, /* bLength */ + 0x02, /* bDescriptorType */ + 0x22, 0x00, /* wTotalLength */ + 0x01, /* bNumInterfaces */ + 0x01, /* bConfigurationValue */ + 0x04, /* iConfiguration */ + 0xC0, /* bmAttributes */ + 0x32, /* bMaxPower */ + + /* HID Interface descriptor */ + 0x09, /* bLength */ + 0x04, /* bDescriptorType */ + 0x00, /* bInterfaceNumber */ + 0x00, /* bAlternateSetting */ + 0x01, /* bNumEndpoints */ + 0x03, /* bInterfaceClass */ + 0x01, /* bInterfaceSubClass */ + 0x02, /* bInterfaceProtocol */ + 0x06, /* iInterface */ - /* iSerialNumber Number string descriptor : Index 3 */ - 0x09, 0x04, 0x03, 13, - '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', + /* HID Descriptor */ + 0x09, /* bLength */ + 0x21, /* bDescriptorType */ + 0x10, 0x01, /* bcdHID */ + 0x21, /* bCountryCode */ + 0x01, /* bNumDescriptors */ + 0x22, /* bReportDescriptorType */ + 0x3E, 0x00, /* wReportDescriptorLength */ + + /* HID Endpoint IN Descriptor */ + 0x07, /* bLength */ + 0x05, /* bDescriptorType */ + 0x81, /* bEndpointAddress */ + 0x03, /* bmAttributes */ + 0x08, 0x00, /* wMaxPacketSize */ + 0x08 /* bInterval */ +}; - /* iConfiguration string descriptor : Index 4 */ - 0x09, 0x04, 0x04, 10, +/* USB String Device Framework Length */ +#define DEVICE_FRAMEWORK_LENGTH_STRING 82 + +/* USB String Device Framework */ +unsigned char device_framework_string[] = { + /* iManufacturer string descriptor */ + 0x09, 0x04, /* Language ID */ + 0x01, /* String Index */ + 0x0F, /* String Length */ + 'E', 'c', 'l', 'i', 'p', 's', 'e', ' ', 'T', 'h', 'r', 'e', 'a', 'd', 'x', + + /* iProduct string descriptor */ + 0x09, 0x04, /* Language ID */ + 0x02, /* String Index */ + 0x04, /* String Length */ + 'U', 'S', 'B', 'X', + + /* iSerialNumber string descriptor */ + 0x09, 0x04, /* Language ID */ + 0x03, /* String Index */ + 0x0E, /* String Length */ + 'U', 'S', 'B', 'D', 'E', 'V', 'I', 'C', 'E', '0', '0', '0', '0', '1', + + /* iConfiguration string descriptor */ + 0x09, 0x04, /* Language ID */ + 0x04, /* String Index */ + 0x0A, /* String Length */ 'F', 'U', 'L', 'L', ' ', 'S', 'P', 'E', 'E', 'D', - /* iConfiguration string descriptor : Index 5 */ - 0x09, 0x04, 0x05, 10, + /* iConfiguration string descriptor */ + 0x09, 0x04, /* Language ID */ + 0x05, /* String Index */ + 0x0A, /* String Length */ 'H', 'I', 'G', 'H', ' ', 'S', 'P', 'E', 'E', 'D', - /* iInterface string descriptor : Index 6 */ - 0x09, 0x04, 0x06, 5, + /* iInterface HID string descriptor */ + 0x09, 0x04, /* Language ID */ + 0x06, /* String Index */ + 0x05, /* String Length */ 'M', 'o', 'u', 's', 'e' }; +/* USB Language ID Framework Length */ +#define DEVICE_FRAMEWORK_LENGTH_LANGUAGE_ID 2 -/* Multiple languages are supported on the device, to add a language besides english, - the unicode language code must be appended to the ux_demo_language_id_framework array and the length - adjusted accordingly. -*/ -#define LANGUAGE_ID_FRAMEWORK_LENGTH sizeof(ux_demo_language_id_framework) -UCHAR ux_demo_language_id_framework[] = { - /* English. */ - 0x09, 0x04 +/* USB Language ID Framework */ +unsigned char device_framework_language_id[] = { + 0x09, 0x04 /* Language ID (0x0409 = English (US)) */ }; #ifndef EXTERNAL_MAIN +/********************************************************************/ +/** main */ +/** */ +/** Perform board-level setup and then transfer control to the */ +/** ThreadX kernel so the RTOS sample can start its threads. */ +/********************************************************************/ int main(void) { /* Initialize the board. */ @@ -393,65 +365,161 @@ int main(void) } #endif /* EXTERNAL_MAIN */ +#ifndef DEMO_TEST +/********************************************************************/ +/** tx_application_define */ +/** */ +/** ThreadX application entry used to start the USBX demo once */ +/** the kernel has been initialized. */ +/********************************************************************/ VOID tx_application_define(VOID *first_unused_memory) { -CHAR *stack_pointer; -CHAR *memory_pointer; -UINT status; -UX_SLAVE_CLASS_HID_PARAMETER hid_mouse_parameter; + UX_PARAMETER_NOT_USED(first_unused_memory); + + usbx_demo_init(); +} +#endif /* DEMO_TEST */ + +/********************************************************************/ +/** usbx_demo_init */ +/** */ +/** Create the demo threads and initialize the USBX HID device */ +/** stack used by this sample. */ +/********************************************************************/ +UINT usbx_demo_init(VOID) +{ +UINT status; + + /* Create the main demo thread. */ + status = ux_utility_thread_create(&ux_demo_thread, "usbx_demo_app_thread_entry", + ux_demo_thread_entry, 0, ux_demo_thread_stack, + ux_demo_thread_size, 20, 20, 1, UX_AUTO_START); + + if(status != UX_SUCCESS) + return status; + + /* Create the hid demo thread. */ + status = ux_utility_thread_create(&ux_device_hid_thread, "usbx_hid_app_thread_entry", + ux_device_hid_thread_entry, 0, ux_device_hid_thread_stack, + ux_device_hid_thread_size, 20, 20, 1, UX_AUTO_START); + + if(status != UX_SUCCESS) + return status; + + status = ux_device_hid_init(); + + if(status != UX_SUCCESS) + return status; + + return status; +} - /* Initialize the free memory pointer. */ - stack_pointer = (CHAR *) first_unused_memory; +/********************************************************************/ +/** ux_device_hid_init */ +/** */ +/** Initialize USBX device resources, install the device stack, */ +/** and register the HID mouse class instance with its callbacks. */ +/********************************************************************/ +UINT ux_device_hid_init(VOID) +{ - /* Initialize the RAM disk memory. */ - memory_pointer = stack_pointer + DEMO_STACK_SIZE; +UINT status; +UX_SLAVE_CLASS_HID_PARAMETER hid_mouse_parameter = {UX_NULL}; - /* Initialize USBX Memory */ - status = ux_system_initialize(memory_pointer, UX_DEVICE_MEMORY_STACK_SIZE, UX_NULL, 0); +#ifndef DEMO_TEST + /* Initialize USBX Memory. */ + status = ux_system_initialize(ux_system_memory_pool, UX_DEVICE_MEMORY_STACK_SIZE, UX_NULL, 0); if(status != UX_SUCCESS) - return; + return status; +#endif /* DEMO_TEST */ /* Install the device portion of USBX. */ - status = ux_device_stack_initialize(ux_demo_device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED, - ux_demo_device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED, - ux_demo_string_framework, STRING_FRAMEWORK_LENGTH, - ux_demo_language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH, + status = ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED, + device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED, + device_framework_string, DEVICE_FRAMEWORK_LENGTH_STRING, + device_framework_language_id, DEVICE_FRAMEWORK_LENGTH_LANGUAGE_ID, UX_NULL); if(status != UX_SUCCESS) - return; + return status; - /* Initialize the hid mouse class parameters for the device */ + /* Initialize the hid mouse class parameters for the device. */ hid_mouse_parameter.ux_slave_class_hid_instance_activate = ux_demo_device_hid_instance_activate; hid_mouse_parameter.ux_slave_class_hid_instance_deactivate = ux_demo_device_hid_instance_deactivate; - hid_mouse_parameter.ux_device_class_hid_parameter_report_address = hid_mouse_report; - hid_mouse_parameter.ux_device_class_hid_parameter_report_length = UX_HID_MOUSE_REPORT_LENGTH; + hid_mouse_parameter.ux_device_class_hid_parameter_report_address = hid_report_descriptor; + hid_mouse_parameter.ux_device_class_hid_parameter_report_length = UX_HID_REPORT_DESCRIPTOR_LENGTH; hid_mouse_parameter.ux_device_class_hid_parameter_report_id = UX_FALSE; hid_mouse_parameter.ux_device_class_hid_parameter_callback = ux_demo_device_hid_callback; hid_mouse_parameter.ux_device_class_hid_parameter_get_callback = ux_demo_device_hid_get_callback; - /* Initialize the device hid class. The class is connected with interface 0 on configuration 1. */ + /* Initialize the device hid class. The class is connected with interface 0 on configuration 1. */ status = ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry, 1, 0, (VOID *)&hid_mouse_parameter); if(status != UX_SUCCESS) - return; + return status; - /* Create the main demo thread. */ - status = ux_utility_thread_create(&ux_hid_thread, "hid_usbx_app_thread_entry", - ux_demo_device_hid_thread_entry, 0, stack_pointer, - 512, 20, 20, 1, TX_AUTO_START); + /* Register error callback. */ + ux_utility_error_callback_register(ux_demo_error_callback); + + return status; +} + +/********************************************************************/ +/** usbx_demo_uninit */ +/** */ +/** Stop the demo worker threads created by usbx_demo_init so the */ +/** RTOS sample can be shut down cleanly. */ +/********************************************************************/ +UINT usbx_demo_uninit(VOID) +{ + +UINT status; + + /* Delete the main demo thread. */ + status = ux_utility_thread_delete(&ux_demo_thread); + + /* Delete the hid demo thread. */ + status = ux_utility_thread_delete(&ux_device_hid_thread); if(status != UX_SUCCESS) - return; + return status; - /* Register error callback. */ - ux_utility_error_callback_register(ux_demo_error_callback); + return status; +} + +/********************************************************************/ +/** ux_device_hid_uninit */ +/** */ +/** Tear down the HID device stack and delete the threads created */ +/** for this RTOS sample. */ +/********************************************************************/ +UINT ux_device_hid_uninit(VOID) +{ + +UINT status; + + /* Uninitialize USBX Memory. */ + status = ux_device_stack_uninitialize(); + + if(status != UX_SUCCESS) + return status; + + /* Uninitialize the device hid class. */ + status = ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry); + + if(status != UX_SUCCESS) + return status; + + return status; } /********************************************************************/ /** ux_demo_device_hid_instance_activate */ +/** */ +/** Store the HID class instance pointer when the device becomes */ +/** active so the worker thread can start sending reports. */ /********************************************************************/ VOID ux_demo_device_hid_instance_activate(VOID *hid_instance) { @@ -461,6 +529,9 @@ VOID ux_demo_device_hid_instance_activate(VOID *hid_instance) /********************************************************************/ /** ux_demo_device_hid_instance_deactivate */ +/** */ +/** Clear the cached HID class instance pointer when the device */ +/** is deactivated or disconnected. */ /********************************************************************/ VOID ux_demo_device_hid_instance_deactivate(VOID *hid_instance) { @@ -470,6 +541,10 @@ VOID ux_demo_device_hid_instance_deactivate(VOID *hid_instance) /********************************************************************/ /** ux_demo_device_hid_callback */ +/** */ +/** Handle HID class notifications from the stack. This demo does */ +/** not need runtime event processing, so the callback only */ +/** acknowledges the request. */ /********************************************************************/ UINT ux_demo_device_hid_callback(UX_SLAVE_CLASS_HID *hid_instance, UX_SLAVE_CLASS_HID_EVENT *hid_event) { @@ -481,6 +556,10 @@ UINT ux_demo_device_hid_callback(UX_SLAVE_CLASS_HID *hid_instance, UX_SLAVE_CLAS /********************************************************************/ /** ux_demo_device_hid_get_callback */ +/** */ +/** Respond to HID GET-style requests that require application */ +/** data. The mouse demo has no dynamic data to return, so the */ +/** request completes successfully without modifying the event. */ /********************************************************************/ UINT ux_demo_device_hid_get_callback(UX_SLAVE_CLASS_HID *hid_instance, UX_SLAVE_CLASS_HID_EVENT *hid_event) { @@ -491,47 +570,75 @@ UINT ux_demo_device_hid_get_callback(UX_SLAVE_CLASS_HID *hid_instance, UX_SLAVE_ } /********************************************************************/ -/** ux_demo_device_hid_thread_entry: hid demo thread */ +/** ux_demo_thread_entry */ +/** */ +/** Register the device controller used by the sample, either the */ +/** simulator DCD for tests or the board-specific DCD provided by */ +/** the platform. */ /********************************************************************/ -VOID ux_demo_device_hid_thread_entry(ULONG thread_input) +VOID ux_demo_thread_entry(ULONG thread_input) { UX_PARAMETER_NOT_USED(thread_input); +#ifndef DEMO_TEST + + /* Register the USB device simulator controllers for testing */ + ux_dcd_sim_slave_initialize(); +#else /* DEMO_TEST */ + /* Register the USB device controllers available in this system */ usb_device_dcd_initialize(UX_NULL); +#endif /* DEMO_TEST */ + +} + +/********************************************************************/ +/** ux_device_hid_thread_entry */ +/** */ +/** Poll the device state and submit HID mouse reports while the */ +/** device is configured. When disconnected, the thread sleeps */ +/** until the host enumerates the device again. */ +/********************************************************************/ +VOID ux_device_hid_thread_entry(ULONG thread_input) +{ + + UX_PARAMETER_NOT_USED(thread_input); while (1) { - /* Check if the device state already configured. */ - if ((UX_SLAVE_DEVICE_CHECK_STATE(UX_DEVICE_CONFIGURED)) && (hid_mouse != UX_NULL)) - { -#ifdef UX_DEMO_MOUSE_ABSOLUTE - ux_demo_hid_mouse_absolute_cursor_move(hid_mouse); -#else /* UX_DEMO_MOUSE_ABSOLUTE */ - ux_demo_hid_mouse_cursor_move(hid_mouse); -#endif /* UX_DEMO_MOUSE_ABSOLUTE */ - } - else - { - /* Sleep thread for 10ms. */ - ux_utility_delay_ms(MS_TO_TICK(10)); - } + /* Check if the device state already configured. */ + if ((UX_SLAVE_DEVICE_CHECK_STATE(UX_DEVICE_CONFIGURED)) && (hid_mouse != UX_NULL)) + { + ux_device_hid_mouse_cursor_move(hid_mouse); + } + else + { + /* Sleep thread for 10ms. */ + ux_utility_delay_ms(MS_TO_TICK(10)); + } } } -#ifndef UX_DEMO_MOUSE_ABSOLUTE /********************************************************************/ -/** ux_demo_hid_mouse_cursor_move: show how to move mouse cursor */ +/** ux_device_hid_mouse_cursor_move */ +/** */ +/** Build and send one HID input report that moves the mouse */ +/** cursor along a rectangular path by updating the relative X/Y */ +/** fields and advancing the current direction state. */ /********************************************************************/ -UINT ux_demo_hid_mouse_cursor_move(UX_SLAVE_CLASS_HID *device_hid) +UINT ux_device_hid_mouse_cursor_move(UX_SLAVE_CLASS_HID *device_hid) { + UINT status; UX_SLAVE_CLASS_HID_EVENT device_hid_event; -static UCHAR mouse_x; -static UCHAR mouse_y; -static UCHAR mouse_move_dir; +static CHAR mouse_x; +static CHAR mouse_y; static UCHAR mouse_move_count; +static UCHAR mouse_move_dir = UX_MOUSE_CURSOR_MOVE_RIGHT; + + /* Reset the HID event structure. */ + ux_utility_memory_set(&device_hid_event, 0, sizeof(UX_SLAVE_CLASS_HID_EVENT)); /* Sleep thread for 10ms. */ ux_utility_delay_ms(MS_TO_TICK(10)); @@ -550,7 +657,7 @@ static UCHAR mouse_move_count; { case UX_MOUSE_CURSOR_MOVE_RIGHT: /* +x. */ - mouse_x = UX_DEMO_HID_MOUSE_CURSOR_MOVE; + mouse_x = (CHAR)UX_DEMO_HID_MOUSE_CURSOR_MOVE; mouse_y = 0; mouse_move_count ++; @@ -560,12 +667,17 @@ static UCHAR mouse_move_count; mouse_move_dir = UX_MOUSE_CURSOR_MOVE_DOWN; } + status = ux_device_class_hid_event_set(device_hid, &device_hid_event); + + if(status != UX_SUCCESS) + return UX_ERROR; + break; case UX_MOUSE_CURSOR_MOVE_DOWN: /* +y. */ mouse_x = 0; - mouse_y = UX_DEMO_HID_MOUSE_CURSOR_MOVE; + mouse_y = (CHAR)UX_DEMO_HID_MOUSE_CURSOR_MOVE; mouse_move_count ++; if (mouse_move_count >= UX_DEMO_HID_MOUSE_CURSOR_MOVE_N) @@ -574,6 +686,11 @@ static UCHAR mouse_move_count; mouse_move_dir = UX_MOUSE_CURSOR_MOVE_LEFT; } + status = ux_device_class_hid_event_set(device_hid, &device_hid_event); + + if(status != UX_SUCCESS) + return UX_ERROR; + break; case UX_MOUSE_CURSOR_MOVE_LEFT: /* -x. */ @@ -588,118 +705,29 @@ static UCHAR mouse_move_count; mouse_move_dir = UX_MOUSE_CURSOR_MOVE_UP; } + status = ux_device_class_hid_event_set(device_hid, &device_hid_event); + + if(status != UX_SUCCESS) + return UX_ERROR; + break; case UX_MOUSE_CURSOR_MOVE_UP: /* -y. */ mouse_x = 0; - mouse_y = (UCHAR)(-UX_DEMO_HID_MOUSE_CURSOR_MOVE); + mouse_y = (CHAR)(-UX_DEMO_HID_MOUSE_CURSOR_MOVE); mouse_move_count ++; if (mouse_move_count >= UX_DEMO_HID_MOUSE_CURSOR_MOVE_N) { mouse_move_count = 0; - mouse_move_dir = UX_MOUSE_CURSOR_MOVE_RIGHT; + mouse_move_dir = 0; } - break; - - default: + status = ux_device_class_hid_event_set(device_hid, &device_hid_event); - mouse_x = 0; - mouse_y = 0; - - ux_utility_memory_set(&device_hid_event, 0, sizeof(UX_SLAVE_CLASS_HID_EVENT)); - - break; - } - - status = ux_device_class_hid_event_set(device_hid, &device_hid_event); - - if(status != UX_SUCCESS) - return UX_ERROR; - - return mouse_move_dir; -} -#else /* UX_DEMO_MOUSE_ABSOLUTE */ -/***************************************************************************************/ -/** ux_demo_hid_mouse_absolute_cursor_move: */ -/** show how to daw a rectangle with width 10000, height 10000, step size 500 */ -/***************************************************************************************/ -UINT ux_demo_hid_mouse_absolute_cursor_move(UX_SLAVE_CLASS_HID *device_hid) -{ -UINT status; -UX_SLAVE_CLASS_HID_EVENT device_hid_event; -ULONG start_mouse_x = 8000; -ULONG start_mouse_y = 8000; -ULONG width = 10000; -ULONG height = 10000; -static ULONG mouse_x; -static ULONG mouse_y; -static UCHAR mouse_move_dir; - - /* Sleep thread for 100ms. */ - ux_utility_delay_ms(MS_TO_TICK(100)); - - /* Initialize mouse event. */ - device_hid_event.ux_device_class_hid_event_report_id = 0; - device_hid_event.ux_device_class_hid_event_report_type = UX_DEVICE_CLASS_HID_REPORT_TYPE_INPUT; - device_hid_event.ux_device_class_hid_event_length = 6; - device_hid_event.ux_device_class_hid_event_buffer[0] = 0; /* ...M|R|L */ - device_hid_event.ux_device_class_hid_event_buffer[1] = UX_W0(mouse_x); /* X */ - device_hid_event.ux_device_class_hid_event_buffer[2] = UX_W1(mouse_x); /* X */ - device_hid_event.ux_device_class_hid_event_buffer[3] = UX_W0(mouse_y); /* Y */ - device_hid_event.ux_device_class_hid_event_buffer[4] = UX_W1(mouse_y); /* Y */ - device_hid_event.ux_device_class_hid_event_buffer[5] = 0; /* Wheel */ - - - switch (mouse_move_dir) - { - case UX_MOUSE_CURSOR_MOVE_RIGHT: /* +x. */ - - mouse_x += UX_DEMO_HID_MOUSE_CURSOR_MOVE; - - if (mouse_x >= start_mouse_x + width) - { - mouse_x = start_mouse_x + width; - mouse_move_dir = UX_MOUSE_CURSOR_MOVE_DOWN; - } - - break; - - case UX_MOUSE_CURSOR_MOVE_DOWN: /* +y. */ - - mouse_y += UX_DEMO_HID_MOUSE_CURSOR_MOVE; - - if (mouse_y >= start_mouse_y + height) - { - mouse_y = start_mouse_y + height; - mouse_move_dir = UX_MOUSE_CURSOR_MOVE_LEFT; - } - - break; - - case UX_MOUSE_CURSOR_MOVE_LEFT: /* -y. */ - - mouse_x -= UX_DEMO_HID_MOUSE_CURSOR_MOVE; - - if (mouse_x <= start_mouse_x) - { - mouse_x = start_mouse_x; - mouse_move_dir = UX_MOUSE_CURSOR_MOVE_UP; - } - - break; - - case UX_MOUSE_CURSOR_MOVE_UP: /* -y. */ - - mouse_y -= UX_DEMO_HID_MOUSE_CURSOR_MOVE; - - if (mouse_y <= start_mouse_y) - { - mouse_y = start_mouse_y; - mouse_move_dir = UX_MOUSE_CURSOR_MOVE_RIGHT; - } + if(status != UX_SUCCESS) + return UX_ERROR; break; @@ -713,17 +741,16 @@ static UCHAR mouse_move_dir; break; } - /* Set the mouse event. */ - status = ux_device_class_hid_event_set(device_hid, &device_hid_event); - - if(status != UX_SUCCESS) - return UX_ERROR; - return mouse_move_dir; } -#endif /* UX_DEMO_MOUSE_ABSOLUTE */ -VOID ux_demo_error_callback(UINT system_level, UINT system_context, UINT error_code) +/********************************************************************/ +/** ux_demo_error_callback */ +/** */ +/** Print USBX error details to aid debugging when the stack */ +/** reports runtime failures. */ +/********************************************************************/ +static VOID ux_demo_error_callback(UINT system_level, UINT system_context, UINT error_code) { /* * Refer to ux_api.h. For example, diff --git a/samples/demo_device_hid_mouse_standalone.c b/samples/demo_device_hid_mouse_standalone.c deleted file mode 100644 index 4a7a1222d..000000000 --- a/samples/demo_device_hid_mouse_standalone.c +++ /dev/null @@ -1,718 +0,0 @@ -/*************************************************************************** - * Copyright (c) 2025-present Eclipse ThreadX Contributors - * - * This program and the accompanying materials are made available under the - * terms of the MIT License which is available at - * https://opensource.org/licenses/MIT. - * - * - * SPDX-License-Identifier: MIT - **************************************************************************/ - -/**************************************************************************/ -/**************************************************************************/ -/** */ -/** Overview */ -/** */ -/** This example works as a USB HID device. It will appear as a USB */ -/** mouse device on PC. This application demo is running in standalone */ -/** mode. */ -/** */ -/** Note */ -/** */ -/** This demonstration is not optimized, to optimize application user */ -/** should configure related class flag in ux_user.h and adjust */ -/** DEMO_STACK_SIZE and UX_DEVICE_MEMORY_STACK_SIZE */ -/** */ -/** */ -/** AUTHOR */ -/** */ -/** Mohamed AYED */ -/** */ -/**************************************************************************/ -/**************************************************************************/ - -#include "ux_api.h" -#include "ux_device_class_hid.h" - -/* if defined mouse with absolute positioning is a type of USB HID mouse that reports its position using - absolute coordinates rather than relative movement deltas. This is common in touch devices, graphics tablets, - and some remote control devices, where the report indicates an (X, Y) position within a defined logical range, - not just movement increments.*/ -/* #define UX_DEMO_MOUSE_ABSOLUTE */ - -/* Defined the mouse will act as boot device. */ -#define DEMO_HID_BOOT_DEVICE - -/**************************************************/ -/** Define constants */ -/**************************************************/ -#define UX_DEVICE_MEMORY_STACK_SIZE 7*1024 - -#define UX_DEMO_HID_DEVICE_VID 0x090A -#define UX_DEMO_HID_DEVICE_PID 0x4036 - -#define UX_DEMO_MAX_EP0_SIZE 0x40U -#define UX_DEMO_HID_CONFIG_DESC_SIZE 0x22U -#define UX_DEMO_BCD_USB 0x0200 -#define UX_DEMO_BCD_HID 0x0110 - -#define UX_DEMO_HID_ENDPOINT_SIZE 0x08 -#define UX_DEMO_HID_ENDPOINT_ADDRESS 0x81 -#define UX_DEMO_HID_ENDPOINT_BINTERVAL 0x08 - -#ifdef DEMO_HID_BOOT_DEVICE -#define UX_DEMO_HID_SUBCLASS 0x01 -#else /* DEMO_HID_BOOT_DEVICE */ -#define UX_DEMO_HID_SUBCLASS 0x00 -#endif - -#ifdef UX_DEMO_MOUSE_ABSOLUTE -#define UX_DEMO_HID_MOUSE_CURSOR_MOVE 350 -#else /* UX_DEMO_MOUSE_ABSOLUTE */ -#define UX_DEMO_HID_MOUSE_CURSOR_MOVE 3 -#define UX_DEMO_HID_MOUSE_CURSOR_MOVE_N 100 -#endif /* UX_DEMO_MOUSE_ABSOLUTE */ - -#define UX_MOUSE_CURSOR_MOVE_RIGHT 0x00 -#define UX_MOUSE_CURSOR_MOVE_DOWN 0x01 -#define UX_MOUSE_CURSOR_MOVE_LEFT 0x02 -#define UX_MOUSE_CURSOR_MOVE_UP 0x03 - -/**************************************************/ -/** usbx device hid demo callbacks */ -/**************************************************/ -VOID ux_demo_device_hid_instance_activate(VOID *hid_instance); -VOID ux_demo_device_hid_instance_deactivate(VOID *hid_instance); -UINT ux_demo_device_hid_callback(UX_SLAVE_CLASS_HID *hid_instance, UX_SLAVE_CLASS_HID_EVENT *hid_event); -UINT ux_demo_device_hid_get_callback(UX_SLAVE_CLASS_HID *hid_instance, UX_SLAVE_CLASS_HID_EVENT *hid_event); - -/**************************************************/ -/** usbx application initialization with RTOS */ -/**************************************************/ -VOID tx_application_define(VOID *first_unused_memory); - -/**************************************************/ -/** usbx device hid demo thread */ -/**************************************************/ -VOID ux_demo_device_hid_thread_entry(ULONG thread_input); - -/**************************************************/ -/** usbx device hid demo mouse */ -/**************************************************/ -UINT ux_demo_hid_mouse_buttons(UX_SLAVE_CLASS_HID *device_hid); -UINT ux_demo_hid_mouse_scroll_wheel(UX_SLAVE_CLASS_HID *device_hid); -#ifndef UX_DEMO_MOUSE_ABSOLUTE -UINT ux_demo_hid_mouse_cursor_move(UX_SLAVE_CLASS_HID *device_hid); -#else -UINT ux_demo_hid_mouse_absolute_cursor_move(UX_SLAVE_CLASS_HID *device_hid); -#endif /* UX_DEMO_MOUSE_ABSOLUTE */ - -/**************************************************/ -/** usbx callback error */ -/**************************************************/ -VOID ux_demo_error_callback(UINT system_level, UINT system_context, UINT error_code); - -#ifndef EXTERNAL_MAIN -extern int board_setup(void); -#endif /* EXTERNAL_MAIN */ -extern int usb_device_dcd_initialize(void *param); - -/**************************************************/ -/** usbx device hid mouse instance */ -/**************************************************/ -UX_SLAVE_CLASS_HID *hid_mouse; - -VOID ux_application_define(VOID); -VOID ux_demo_device_hid_task(VOID); -static CHAR ux_system_memory_pool[UX_DEVICE_MEMORY_STACK_SIZE]; - -/**************************************************/ -/** HID Report descriptor */ -/**************************************************/ -UCHAR hid_mouse_report[] = { - 0x05, 0x01, // USAGE_PAGE (Generic Desktop) - 0x09, 0x02, // USAGE (Mouse) - 0xa1, 0x01, // COLLECTION (Application) - - /* Pointer and Physical are required by Apple Recovery */ - 0x09, 0x01, // USAGE (Pointer) - 0xa1, 0x00, // COLLECTION (Physical) - - /* 3 Buttons */ - 0x05, 0x09, // USAGE_PAGE (Button) - 0x19, 0x01, // USAGE_MINIMUM (Button 1) - 0x29, 0x03, // USAGE_MAXIMUM (Button 3) - 0x15, 0x00, // LOGICAL_MINIMUM (0) - 0x25, 0x01, // LOGICAL_MAXIMUM (1) - 0x75, 0x01, // REPORT_SIZE (1) - 0x95, 0x03, // REPORT_COUNT (3) -> 3 buttons - 0x81, 0x02, // INPUT (Data, Variable, Absolute) -> Buttons - - 0x75, 0x05, // REPORT_SIZE (5) - 0x95, 0x01, // REPORT_COUNT (1) - 0x81, 0x03, // INPUT (Constant, Variable, Absolute) -> Padding bits - - /* X, Y */ - 0x05, 0x01, // USAGE_PAGE (Generic Desktop) - 0x09, 0x30, // USAGE (X) - 0x09, 0x31, // USAGE (Y) -#ifdef UX_DEMO_MOUSE_ABSOLUTE - 0x16, 0x00, 0x00, // LOGICAL_MINIMUM (0) - 0x26, 0xFF, 0x7F, // LOGICAL_MAXIMUM (32767) (0x7FFF) - 0x75, 0x10, // REPORT_SIZE (16) (2 bytes per axis) - 0x95, 0x02, // REPORT_COUNT (2) -> X, Y position - 0x81, 0x02, // INPUT (Data, Variable, Absolute) -> Absolute X, Y position -#else /* UX_DEMO_MOUSE_ABSOLUTE */ - 0x15, 0x81, // LOGICAL_MINIMUM (-127) - 0x25, 0x7F, // LOGICAL_MAXIMUM (127) - 0x75, 0x08, // REPORT_SIZE (8) (1 bytes per axis) - 0x95, 0x02, // REPORT_COUNT (2) -> X, Y movement - 0x81, 0x06, // INPUT (Data, Variable, Relative) -> X, Y are relative -#endif /* UX_DEMO_MOUSE_ABSOLUTE */ - - /* Wheel */ - 0x09, 0x38, // USAGE (Mouse Wheel) - 0x15, 0x81, // LOGICAL_MINIMUM (-127) - 0x25, 0x7F, // LOGICAL_MAXIMUM (127) - 0x75, 0x08, // REPORT_SIZE (8) - 0x95, 0x01, // REPORT_COUNT (1) -> Wheel movement - 0x81, 0x06, // INPUT (Data, Variable, Relative) -> Wheel - - /* End */ - 0xC0, // END_COLLECTION - 0xC0 // END_COLLECTION -}; - -#define UX_HID_MOUSE_REPORT_LENGTH (sizeof(hid_mouse_report)/sizeof(hid_mouse_report[0])) - -#define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED sizeof(ux_demo_device_framework_full_speed) - -UCHAR ux_demo_device_framework_full_speed[] = { - /* Device descriptor */ - 0x12, /* bLength */ - 0x01, /* bDescriptorType */ - UX_W0(UX_DEMO_BCD_USB), UX_W1(UX_DEMO_BCD_USB), /* bcdUSB */ - 0x00, /* bDeviceClass : 0x00 : Interface-defined */ - 0x00, /* bDeviceSubClass : 0x00 : Reset */ - 0x00, /* bDeviceProtocol : 0x00 : Reset */ - UX_DEMO_MAX_EP0_SIZE, /* bMaxPacketSize0 */ - UX_W0(UX_DEMO_HID_DEVICE_VID), UX_W1(UX_DEMO_HID_DEVICE_VID), /* idVendor : ... */ - UX_W0(UX_DEMO_HID_DEVICE_PID), UX_W1(UX_DEMO_HID_DEVICE_PID), /* idProduct */ - 0x00, 0x00, /* bcdDevice */ - 0x01, /* iManufacturer */ - 0x02, /* iProduct */ - 0x03, /* iSerialNumber */ - 0x01, /* bNumConfigurations */ - - /* Configuration Descriptor, total 34 */ - 0x09, /* bLength */ - 0x02, /* bDescriptorType */ - UX_W0(UX_DEMO_HID_CONFIG_DESC_SIZE), /* wTotalLength */ - UX_W1(UX_DEMO_HID_CONFIG_DESC_SIZE), - 0x01, /* bNumInterfaces */ - 0x01, /* bConfigurationValue */ - 0x04, /* iConfiguration */ - 0xC0, /* bmAttributes */ - /* D6 : 0x1 : Self-powered */ - /* D5, Remote Wakeup : 0x0 : Not supported */ - 0x32, /* bMaxPower : 50 : 100mA */ - - /* Interface descriptor */ - 0x09, /* bLength */ - 0x04, /* bDescriptorType */ - 0x00, /* bInterfaceNumber */ - 0x00, /* bAlternateSetting */ - 0x01, /* bNumEndpoints */ - 0x03, /* bInterfaceClass : 0x03 : HID */ - UX_DEMO_HID_SUBCLASS, /* bInterfaceSubClass : ... : Boot/non-boot Subclass */ - 0x02, /* bInterfaceProtocol : 0x00 : Undefined */ - 0x06, /* iInterface */ - - /* HID Descriptor */ - 0x09, /* bLength : 9 */ - 0x21, /* bDescriptorType : 0x21 : HID descriptor */ - 0x10, 0x01, /* bcdHID : 0x0110 */ - 0x21, /* bCountryCode : 33 : US */ - 0x01, /* bNumDescriptors */ - 0x22, /* bReportDescriptorType1 : 0x22 : Report descriptor */ - UX_W0(UX_HID_MOUSE_REPORT_LENGTH), /* wDescriptorLength1 */ - UX_W1(UX_HID_MOUSE_REPORT_LENGTH), - - /* Endpoint Descriptor */ - 0x07, /* bLength */ - 0x05, /* bDescriptorType */ - UX_DEMO_HID_ENDPOINT_ADDRESS, /* bEndpointAddress */ - /* D7, Direction : 0x01 */ - /* D3..0, Endpoint number : 2 */ - 0x03, /* bmAttributes */ - /* D1..0, Transfer Type : 0x3 : Interrupt */ - /* D3..2, Synchronization Type : 0x0 : No Synchronization */ - /* D5..4, Usage Type : 0x0 : Data endpoint */ - UX_W0(UX_DEMO_HID_ENDPOINT_SIZE), /* wMaxPacketSize */ - UX_W1(UX_DEMO_HID_ENDPOINT_SIZE), /* D10..0, Max Packet Size */ - /* D12..11, Additional transactions : 0x00 */ - UX_DEMO_HID_ENDPOINT_BINTERVAL, /* bInterval : 8 : 8ms / x128 (FS 128ms/HS 16ms) */ -}; - -#define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED sizeof(ux_demo_device_framework_high_speed) -UCHAR ux_demo_device_framework_high_speed[] = { - /* Device descriptor */ - 0x12, /* bLength */ - 0x01, /* bDescriptorType */ - UX_W0(UX_DEMO_BCD_USB), UX_W1(UX_DEMO_BCD_USB), /* bcdUSB */ - 0x00, /* bDeviceClass : 0x00 : Interface-defined */ - 0x00, /* bDeviceSubClass : 0x00 : Reset */ - 0x00, /* bDeviceProtocol : 0x00 : Reset */ - UX_DEMO_MAX_EP0_SIZE, /* bMaxPacketSize0 */ - UX_W0(UX_DEMO_HID_DEVICE_VID), UX_W1(UX_DEMO_HID_DEVICE_VID), /* idVendor : ... */ - UX_W0(UX_DEMO_HID_DEVICE_PID), UX_W1(UX_DEMO_HID_DEVICE_PID), /* idProduct */ - 0x01, 0x00, /* bcdDevice */ - 0x01, /* iManufacturer */ - 0x02, /* iProduct */ - 0x03, /* iSerialNumber */ - 0x01, /* bNumConfigurations */ - - /* Device qualifier descriptor */ - 0x0A, /* bLength */ - 0x06, /* bDescriptorType */ - UX_W0(UX_DEMO_BCD_USB), UX_W1(UX_DEMO_BCD_USB), /* bcdUSB */ - 0x00, /* bDeviceClass : 0x00 : Interface-defined */ - 0x00, /* bDeviceSubClass : 0x00 : Reset */ - 0x00, /* bDeviceProtocol : 0x00 : Reset */ - UX_DEMO_MAX_EP0_SIZE, /* bMaxPacketSize0 */ - 0x01, /* bNumConfigurations */ - 0x00, /* bReserved */ - - /* Configuration descriptor */ - 0x09, /* bLength */ - 0x02, /* bDescriptorType */ - UX_W0(UX_DEMO_HID_CONFIG_DESC_SIZE), /* wTotalLength */ - UX_W1(UX_DEMO_HID_CONFIG_DESC_SIZE), - 0x01, /* bNumInterfaces */ - 0x01, /* bConfigurationValue */ - 0x05, /* iConfiguration */ - 0xC0, /* bmAttributes */ - /* D6 : 0x1 : Self-powered */ - /* D5, Remote Wakeup : 0x0 : Not supported */ - 0x19, /* bMaxPower : 50 : 100mA */ - - /* Interface descriptor */ - 0x09, /* bLength */ - 0x04, /* bDescriptorType */ - 0x00, /* bInterfaceNumber */ - 0x00, /* bAlternateSetting */ - 0x01, /* bNumEndpoints */ - 0x03, /* bInterfaceClass : 0x03 : HID */ - UX_DEMO_HID_SUBCLASS, /* bInterfaceSubClass : ... : Boot/non-boot Subclass */ - 0x02, /* bInterfaceProtocol : 0x00 : Undefined */ - 0x06, /* iInterface */ - - /* HID Descriptor */ - 0x09, /* bLength : 9 */ - 0x21, /* bDescriptorType : 0x21 : HID descriptor */ - UX_W0(UX_DEMO_BCD_HID), UX_W1(UX_DEMO_BCD_HID), /* bcdHID : 0x0110 */ - 0x21, /* bCountryCode : 33 : US */ - 0x01, /* bNumDescriptors */ - 0x22, /* bReportDescriptorType1 : 0x22 : Report descriptor */ - UX_W0(UX_HID_MOUSE_REPORT_LENGTH), /* wDescriptorLength1 */ - UX_W1(UX_HID_MOUSE_REPORT_LENGTH), - - /* Endpoint Descriptor (Interrupt In) */ - 0x07, /* bLength */ - 0x05, /* bDescriptorType */ - UX_DEMO_HID_ENDPOINT_ADDRESS, /* bEndpointAddress */ - /* D7, Direction : 0x01 */ - /* D3..0, Endpoint number : 2 */ - 0x03, /* bmAttributes */ - /* D1..0, Transfer Type : 0x3 : Interrupt */ - /* D3..2, Synchronization Type : 0x0 : No Synchronization */ - /* D5..4, Usage Type : 0x0 : Data endpoint */ - UX_W0(UX_DEMO_HID_ENDPOINT_SIZE), /* wMaxPacketSize */ - UX_W1(UX_DEMO_HID_ENDPOINT_SIZE), /* D10..0, Max Packet Size */ - /* D12..11, Additional transactions : 0x00 */ - UX_DEMO_HID_ENDPOINT_BINTERVAL, /* bInterval : 8 : 8ms / x128 (FS 128ms/HS 16ms) */ -}; - - -/* String Device Framework : - Byte 0 and 1 : Word containing the language ID : 0x0904 for US - Byte 2 : Byte containing the index of the descriptor - Byte 3 : Byte containing the length of the descriptor string -*/ -#define STRING_FRAMEWORK_LENGTH sizeof(ux_demo_string_framework) -UCHAR ux_demo_string_framework[] = { - - /* iManufacturer string descriptor : Index 1 */ - 0x09, 0x04, 0x01, 12, - 'U', 'S', 'B', 'X', ' ', 'e', 'c', 'l', 'i', 'p', 's', 'e', - - /* iProduct string descriptor : Index 2 */ - 0x09, 0x04, 0x02, 14, - 'H', 'I', 'D', ' ', 'M', 'o', 'u', 's', 'e', ' ', 'D', 'e', 'm', 'o', - - /* iSerialNumber Number string descriptor : Index 3 */ - 0x09, 0x04, 0x03, 13, - '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', - - /* iConfiguration string descriptor : Index 4 */ - 0x09, 0x04, 0x04, 10, - 'F', 'U', 'L', 'L', ' ', 'S', 'P', 'E', 'E', 'D', - - /* iConfiguration string descriptor : Index 5 */ - 0x09, 0x04, 0x05, 10, - 'H', 'I', 'G', 'H', ' ', 'S', 'P', 'E', 'E', 'D', - - /* iInterface string descriptor : Index 6 */ - 0x09, 0x04, 0x06, 5, - 'M', 'o', 'u', 's', 'e' -}; - - -/* Multiple languages are supported on the device, to add a language besides english, - the unicode language code must be appended to the ux_demo_language_id_framework array and the length - adjusted accordingly. -*/ -#define LANGUAGE_ID_FRAMEWORK_LENGTH sizeof(ux_demo_language_id_framework) -UCHAR ux_demo_language_id_framework[] = { - /* English. */ - 0x09, 0x04 -}; - -#ifndef EXTERNAL_MAIN -int main(void) -{ - /* Initialize the board. */ - board_setup(); - - - ux_application_define(); - - while (1) - { - ux_system_tasks_run(); - ux_demo_device_hid_task(); - } - -} -#endif /* EXTERNAL_MAIN */ - - -VOID ux_application_define(VOID) -{ -CHAR *memory_pointer; -UINT status; -UX_SLAVE_CLASS_HID_PARAMETER hid_mouse_parameter; - - - /* Use static memory block. */ - memory_pointer = ux_system_memory_pool; - - /* Initialize USBX Memory */ - status = ux_system_initialize(memory_pointer, UX_DEVICE_MEMORY_STACK_SIZE, UX_NULL, 0); - - if(status != UX_SUCCESS) - return; - - /* Install the device portion of USBX. */ - status = ux_device_stack_initialize(ux_demo_device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED, - ux_demo_device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED, - ux_demo_string_framework, STRING_FRAMEWORK_LENGTH, - ux_demo_language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH, - UX_NULL); - - if(status != UX_SUCCESS) - return; - - /* Initialize the hid mouse class parameters for the device */ - hid_mouse_parameter.ux_slave_class_hid_instance_activate = ux_demo_device_hid_instance_activate; - hid_mouse_parameter.ux_slave_class_hid_instance_deactivate = ux_demo_device_hid_instance_deactivate; - hid_mouse_parameter.ux_device_class_hid_parameter_report_address = hid_mouse_report; - hid_mouse_parameter.ux_device_class_hid_parameter_report_length = UX_HID_MOUSE_REPORT_LENGTH; - hid_mouse_parameter.ux_device_class_hid_parameter_report_id = UX_FALSE; - hid_mouse_parameter.ux_device_class_hid_parameter_callback = ux_demo_device_hid_callback; - hid_mouse_parameter.ux_device_class_hid_parameter_get_callback = ux_demo_device_hid_get_callback; - - /* Initialize the device hid class. The class is connected with interface 0 on configuration 1. */ - status = ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry, - 1, 0, (VOID *)&hid_mouse_parameter); - - if(status != UX_SUCCESS) - return; - - /* Register error callback. */ - ux_utility_error_callback_register(ux_demo_error_callback); - - - /* Register the USB device controllers available in this system. */ - usb_device_dcd_initialize(UX_NULL); -} - -/********************************************************************/ -/** ux_demo_device_hid_instance_activate */ -/********************************************************************/ -VOID ux_demo_device_hid_instance_activate(VOID *hid_instance) -{ - if (hid_mouse == UX_NULL) - hid_mouse = (UX_SLAVE_CLASS_HID*) hid_instance; -} - -/********************************************************************/ -/** ux_demo_device_hid_instance_deactivate */ -/********************************************************************/ -VOID ux_demo_device_hid_instance_deactivate(VOID *hid_instance) -{ - if (hid_instance == (VOID *)hid_mouse) - hid_mouse = UX_NULL; -} - -/********************************************************************/ -/** ux_demo_device_hid_callback */ -/********************************************************************/ -UINT ux_demo_device_hid_callback(UX_SLAVE_CLASS_HID *hid_instance, UX_SLAVE_CLASS_HID_EVENT *hid_event) -{ - UX_PARAMETER_NOT_USED(hid_instance); - UX_PARAMETER_NOT_USED(hid_event); - - return UX_SUCCESS; -} - -/********************************************************************/ -/** ux_demo_device_hid_get_callback */ -/********************************************************************/ -UINT ux_demo_device_hid_get_callback(UX_SLAVE_CLASS_HID *hid_instance, UX_SLAVE_CLASS_HID_EVENT *hid_event) -{ - UX_PARAMETER_NOT_USED(hid_instance); - UX_PARAMETER_NOT_USED(hid_event); - - return UX_SUCCESS; -} - -/********************************************************************/ -/** ux_demo_device_hid_task: hid demo task */ -/********************************************************************/ -VOID ux_demo_device_hid_task(VOID) -{ - - /* Check if the device state already configured. */ - if ((UX_SLAVE_DEVICE_CHECK_STATE(UX_DEVICE_CONFIGURED)) && (hid_mouse != UX_NULL)) - { -#ifdef UX_DEMO_MOUSE_ABSOLUTE - ux_demo_hid_mouse_absolute_cursor_move(hid_mouse); -#else /* UX_DEMO_MOUSE_ABSOLUTE */ - ux_demo_hid_mouse_cursor_move(hid_mouse); -#endif /* UX_DEMO_MOUSE_ABSOLUTE */ - } -} - -#ifndef UX_DEMO_MOUSE_ABSOLUTE -/********************************************************************/ -/** ux_demo_hid_mouse_cursor_move: show how to move mouse cursor */ -/********************************************************************/ -UINT ux_demo_hid_mouse_cursor_move(UX_SLAVE_CLASS_HID *device_hid) -{ -UINT status; -UX_SLAVE_CLASS_HID_EVENT device_hid_event; -static UCHAR mouse_x; -static UCHAR mouse_y; -static UCHAR mouse_move_dir; -static UCHAR mouse_move_count; - - /* Sleep thread for 10ms. */ - ux_utility_delay_ms(MS_TO_TICK(10)); - - /* Initialize mouse event. */ - device_hid_event.ux_device_class_hid_event_report_id = 0; - device_hid_event.ux_device_class_hid_event_report_type = UX_DEVICE_CLASS_HID_REPORT_TYPE_INPUT; - device_hid_event.ux_device_class_hid_event_length = 4; - device_hid_event.ux_device_class_hid_event_buffer[0] = 0; /* ...R|M|L */ - device_hid_event.ux_device_class_hid_event_buffer[1] = mouse_x; /* X */ - device_hid_event.ux_device_class_hid_event_buffer[2] = mouse_y; /* Y */ - device_hid_event.ux_device_class_hid_event_buffer[3] = 0; /* Wheel */ - - /* Move cursor. */ - switch(mouse_move_dir) - { - case UX_MOUSE_CURSOR_MOVE_RIGHT: /* +x. */ - - mouse_x = UX_DEMO_HID_MOUSE_CURSOR_MOVE; - mouse_y = 0; - mouse_move_count ++; - - if (mouse_move_count >= UX_DEMO_HID_MOUSE_CURSOR_MOVE_N) - { - mouse_move_count = 0; - mouse_move_dir = UX_MOUSE_CURSOR_MOVE_DOWN; - } - - break; - - case UX_MOUSE_CURSOR_MOVE_DOWN: /* +y. */ - - mouse_x = 0; - mouse_y = UX_DEMO_HID_MOUSE_CURSOR_MOVE; - mouse_move_count ++; - - if (mouse_move_count >= UX_DEMO_HID_MOUSE_CURSOR_MOVE_N) - { - mouse_move_count = 0; - mouse_move_dir = UX_MOUSE_CURSOR_MOVE_LEFT; - } - - break; - - case UX_MOUSE_CURSOR_MOVE_LEFT: /* -x. */ - - mouse_x = (CHAR)(-UX_DEMO_HID_MOUSE_CURSOR_MOVE); - mouse_y = 0; - mouse_move_count ++; - - if (mouse_move_count >= UX_DEMO_HID_MOUSE_CURSOR_MOVE_N) - { - mouse_move_count = 0; - mouse_move_dir = UX_MOUSE_CURSOR_MOVE_UP; - } - - break; - - case UX_MOUSE_CURSOR_MOVE_UP: /* -y. */ - - mouse_x = 0; - mouse_y = (UCHAR)(-UX_DEMO_HID_MOUSE_CURSOR_MOVE); - mouse_move_count ++; - - if (mouse_move_count >= UX_DEMO_HID_MOUSE_CURSOR_MOVE_N) - { - mouse_move_count = 0; - mouse_move_dir = UX_MOUSE_CURSOR_MOVE_RIGHT; - } - - break; - - default: - - mouse_x = 0; - mouse_y = 0; - - ux_utility_memory_set(&device_hid_event, 0, sizeof(UX_SLAVE_CLASS_HID_EVENT)); - - break; - } - - status = ux_device_class_hid_event_set(device_hid, &device_hid_event); - - if(status != UX_SUCCESS) - return UX_ERROR; - - return mouse_move_dir; -} -#else /* UX_DEMO_MOUSE_ABSOLUTE */ -/***************************************************************************************/ -/** ux_demo_hid_mouse_absolute_cursor_move: */ -/** show how to daw a rectangle with width 10000, height 10000, step size 500 */ -/***************************************************************************************/ -UINT ux_demo_hid_mouse_absolute_cursor_move(UX_SLAVE_CLASS_HID *device_hid) -{ -UINT status; -UX_SLAVE_CLASS_HID_EVENT device_hid_event; -ULONG start_mouse_x = 8000; -ULONG start_mouse_y = 8000; -ULONG width = 10000; -ULONG height = 10000; -static ULONG mouse_x; -static ULONG mouse_y; -static UCHAR mouse_move_dir; - - /* Sleep thread for 100ms. */ - ux_utility_delay_ms(MS_TO_TICK(100)); - - /* Initialize mouse event. */ - device_hid_event.ux_device_class_hid_event_report_id = 0; - device_hid_event.ux_device_class_hid_event_report_type = UX_DEVICE_CLASS_HID_REPORT_TYPE_INPUT; - device_hid_event.ux_device_class_hid_event_length = 6; - device_hid_event.ux_device_class_hid_event_buffer[0] = 0; /* ...M|R|L */ - device_hid_event.ux_device_class_hid_event_buffer[1] = UX_W0(mouse_x); /* X */ - device_hid_event.ux_device_class_hid_event_buffer[2] = UX_W1(mouse_x); /* X */ - device_hid_event.ux_device_class_hid_event_buffer[3] = UX_W0(mouse_y); /* Y */ - device_hid_event.ux_device_class_hid_event_buffer[4] = UX_W1(mouse_y); /* Y */ - device_hid_event.ux_device_class_hid_event_buffer[5] = 0; /* Wheel */ - - - switch (mouse_move_dir) - { - case UX_MOUSE_CURSOR_MOVE_RIGHT: /* +x. */ - - mouse_x += UX_DEMO_HID_MOUSE_CURSOR_MOVE; - - if (mouse_x >= start_mouse_x + width) - { - mouse_x = start_mouse_x + width; - mouse_move_dir = UX_MOUSE_CURSOR_MOVE_DOWN; - } - - break; - - case UX_MOUSE_CURSOR_MOVE_DOWN: /* +y. */ - - mouse_y += UX_DEMO_HID_MOUSE_CURSOR_MOVE; - - if (mouse_y >= start_mouse_y + height) - { - mouse_y = start_mouse_y + height; - mouse_move_dir = UX_MOUSE_CURSOR_MOVE_LEFT; - } - - break; - - case UX_MOUSE_CURSOR_MOVE_LEFT: /* -y. */ - - mouse_x -= UX_DEMO_HID_MOUSE_CURSOR_MOVE; - - if (mouse_x <= start_mouse_x) - { - mouse_x = start_mouse_x; - mouse_move_dir = UX_MOUSE_CURSOR_MOVE_UP; - } - - break; - - case UX_MOUSE_CURSOR_MOVE_UP: /* -y. */ - - mouse_y -= UX_DEMO_HID_MOUSE_CURSOR_MOVE; - - if (mouse_y <= start_mouse_y) - { - mouse_y = start_mouse_y; - mouse_move_dir = UX_MOUSE_CURSOR_MOVE_RIGHT; - } - - break; - - default: - - mouse_x = 0; - mouse_y = 0; - - ux_utility_memory_set(&device_hid_event, 0, sizeof(UX_SLAVE_CLASS_HID_EVENT)); - - break; - } - - /* Set the mouse event. */ - status = ux_device_class_hid_event_set(device_hid, &device_hid_event); - - if(status != UX_SUCCESS) - return UX_ERROR; - - return mouse_move_dir; -} -#endif /* UX_DEMO_MOUSE_ABSOLUTE */ - -VOID ux_demo_error_callback(UINT system_level, UINT system_context, UINT error_code) -{ - /* - * Refer to ux_api.h. For example, - * UX_SYSTEM_LEVEL_INTERRUPT, UX_SYSTEM_CONTEXT_DCD, UX_DEVICE_HANDLE_UNKNOWN - */ - printf("USBX error: system level(%d), context(%d), error code(0x%x)\r\n", system_level, system_context, error_code); -} diff --git a/scripts/build_samples_rtos.sh b/scripts/build_samples_rtos.sh new file mode 100644 index 000000000..e76be2dfb --- /dev/null +++ b/scripts/build_samples_rtos.sh @@ -0,0 +1,2 @@ +#!/bin/bash +bash "$(dirname "$(realpath "$0")")/../test/cmake/usbx_samples_rtos/run.sh" build "$@" diff --git a/scripts/test_samples_rtos.sh b/scripts/test_samples_rtos.sh new file mode 100644 index 000000000..96dc9dbbc --- /dev/null +++ b/scripts/test_samples_rtos.sh @@ -0,0 +1,2 @@ +#!/bin/bash +CTEST_PARALLEL_LEVEL=4 bash "$(dirname "$(realpath "$0")")/../test/cmake/usbx_samples_rtos/run.sh" test "$@" diff --git a/test/cmake/usbx_samples_rtos/CMakeLists.txt b/test/cmake/usbx_samples_rtos/CMakeLists.txt new file mode 100644 index 000000000..c3f1c3048 --- /dev/null +++ b/test/cmake/usbx_samples_rtos/CMakeLists.txt @@ -0,0 +1,117 @@ +cmake_minimum_required(VERSION 3.13 FATAL_ERROR) +cmake_policy(SET CMP0054 NEW) +cmake_policy(SET CMP0057 NEW) +cmake_policy(SET CMP0077 NEW) + +project(usbx_samples_rtos_test LANGUAGES C) + +# Use customized ux_user.h +set(UX_USER_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../usbx/ux_user.h) + +set(BUILD_CONFIGURATIONS + msrc_rtos_build +) + +set(CMAKE_CONFIGURATION_TYPES + ${BUILD_CONFIGURATIONS} + CACHE STRING "list of supported configuration types" FORCE) +set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS + ${CMAKE_CONFIGURATION_TYPES}) + +if((NOT CMAKE_BUILD_TYPE) OR (NOT ("${CMAKE_BUILD_TYPE}" IN_LIST CMAKE_CONFIGURATION_TYPES))) + set(CMAKE_BUILD_TYPE + "msrc_rtos_build" + CACHE STRING "Build Type of the project" FORCE) +endif() + +message(STATUS "Build for usbx regression_samples_rtos") +message(STATUS "Build type: ${CMAKE_BUILD_TYPE}") +message(STATUS "Using toolchain file: ${CMAKE_TOOLCHAIN_FILE}.") + +get_filename_component(EXTERNALS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../externals ABSOLUTE) + +set(msrc_rtos_build + -DNX_PHYSICAL_HEADER=20 + -DUX_DISABLE_ASSERT + -DUX_HOST_STACK_CONFIGURATION_INSTANCE_CREATE_CONTROL=0 + -DUX_DEVICE_ENABLE_GET_STRING_WITH_ZERO_LANGUAGE_ID + -DUX_ENABLE_ASSERT + -DUX_ENABLE_ERROR_CHECKING + -DUX_PIMA_WITH_MTP_SUPPORT + -DUX_SLAVE_REQUEST_CONTROL_MAX_LENGTH=512 + -DUX_HOST_CLASS_AUDIO_2_SUPPORT + -DUX_HOST_CLASS_AUDIO_INTERRUPT_SUPPORT +) + +if($ENV{USBX_STATIC}) + message(STATUS "Building STATIC usbx") + set(BUILD_SHARED_LIBS OFF) +else() + message(STATUS "Building usbx BUILD_SHARED_LIBS: ${BUILD_SHARED_LIBS}") +endif() + +if($ENV{ENABLE_64}) + message(STATUS "Building for 64bit") +else() + add_compile_options(-m32) + add_link_options(-m32) + message(STATUS "Building for 32bit") +endif() + +add_compile_options( + -std=c99 + -ggdb + -g3 + -gdwarf-2 + -fdiagnostics-color + -DUX_USE_IO_INSTRUCTIONS + ${${CMAKE_BUILD_TYPE}}) + +enable_testing() + +# Build ThreadX only. NetX Duo/FileX are not required by these HID samples. +add_subdirectory(${EXTERNALS_DIR}/threadx threadx) +target_compile_options(threadx PRIVATE -DTX_ENABLE_EVENT_TRACE) +if(NOT DEFINED ENV{ENABLE_IDLE}) + target_compile_options(threadx PRIVATE -DTX_LINUX_NO_IDLE_ENABLE) +endif() + +add_library(netxduo INTERFACE IMPORTED GLOBAL) +add_library("azrtos::netxduo" ALIAS netxduo) + +add_library(filex INTERFACE IMPORTED GLOBAL) +add_library("azrtos::filex" ALIAS filex) + +add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../../../ usbx) + +# Keep only USBX sources used by HID sample RTOS tests. +get_target_property(USBX_SOURCES usbx SOURCES) +list(FILTER USBX_SOURCES EXCLUDE REGEX ".*ux_(network_driver|pictbridge|device_class_audio|host_class_audio|device_class_cdc|host_class_cdc|device_class_storage|host_class_storage|host_class_asix|host_class_printer|host_class_prolific|host_class_gser|host_class_swar|host_class_video|host_class_pima|device_class_pima|device_class_printer|device_class_rndis|device_class_dfu|device_class_ccid|device_class_video|host_class_hub).*\\.c$") +set_target_properties(usbx PROPERTIES SOURCES "${USBX_SOURCES}") + +set(SAMPLES_DIR ${CMAKE_CURRENT_LIST_DIR}/../../../samples) +set(TEST_SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/../../regression_samples_rtos) + +add_library(samples_rtos_test_utility + ${TEST_SOURCE_DIR}/usbxtestcontrol.c) +target_link_libraries(samples_rtos_test_utility + PUBLIC + azrtos::usbx + azrtos::threadx + azrtos::netxduo + azrtos::filex) +target_compile_definitions(samples_rtos_test_utility PUBLIC CTEST) + +add_executable(usbx_hid_keyboard_demo_device_rtos_test + ${TEST_SOURCE_DIR}/usbx_hid_keyboard_demo_device_rtos_test.c + ${SAMPLES_DIR}/demo_device_hid_keyboard_rtos.c) +target_link_libraries(usbx_hid_keyboard_demo_device_rtos_test PRIVATE samples_rtos_test_utility) +target_compile_definitions(usbx_hid_keyboard_demo_device_rtos_test PRIVATE DEMO_TEST EXTERNAL_MAIN UX_DEVICE_SIDE_ONLY) +add_test(${CMAKE_BUILD_TYPE}::usbx_hid_keyboard_demo_device_rtos_test usbx_hid_keyboard_demo_device_rtos_test) + +add_executable(usbx_hid_mouse_demo_device_rtos_test + ${TEST_SOURCE_DIR}/usbx_hid_mouse_demo_device_rtos_test.c + ${SAMPLES_DIR}/demo_device_hid_mouse_rtos.c) +target_link_libraries(usbx_hid_mouse_demo_device_rtos_test PRIVATE samples_rtos_test_utility) +target_compile_definitions(usbx_hid_mouse_demo_device_rtos_test PRIVATE DEMO_TEST EXTERNAL_MAIN UX_DEVICE_SIDE_ONLY) +add_test(${CMAKE_BUILD_TYPE}::usbx_hid_mouse_demo_device_rtos_test usbx_hid_mouse_demo_device_rtos_test) diff --git a/test/cmake/usbx_samples_rtos/run.sh b/test/cmake/usbx_samples_rtos/run.sh new file mode 100644 index 000000000..7a48a2e5b --- /dev/null +++ b/test/cmake/usbx_samples_rtos/run.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +cd $(dirname $0) + +# Checkout externals (ThreadX only for RTOS samples) +[ -d externals ] || mkdir ../../externals +git clone https://github.com/eclipse-threadx/threadx.git ../../externals/threadx + +# Add junit output for ctest generation +if ! grep -q "\-\-output\-junit \$1.xml" ../../externals/threadx/scripts/cmake_bootstrap.sh; then + sed -i 's/ctest $parallel --timeout 1000 -O $1.txt/& --output-junit $1.xml/g' ../../externals/threadx/scripts/cmake_bootstrap.sh +fi + +[ -f .run.sh ] || ln -sf ../../externals/threadx/scripts/cmake_bootstrap.sh .run.sh +./.run.sh $* diff --git a/test/regression_samples_rtos/usbx_hid_keyboard_demo_device_rtos_test.c b/test/regression_samples_rtos/usbx_hid_keyboard_demo_device_rtos_test.c new file mode 100644 index 000000000..e653df2dd --- /dev/null +++ b/test/regression_samples_rtos/usbx_hid_keyboard_demo_device_rtos_test.c @@ -0,0 +1,258 @@ +/* Regression test for samples/demo_device_hid_keyboard_rtos.c + * + * This test initialises the HID keyboard device demo (device side only) and + * exercises it through the host-side HID stack using the USB simulator pair. + * It verifies that: + * 1. The device enumerates correctly on the host. + * 2. The host HID keyboard client instance becomes live. + * 3. Key events can be read from the device. + */ + +#include +#include "tx_api.h" +#include "ux_api.h" +#include "ux_host_class_hid.h" +#include "ux_host_class_hid_keyboard.h" + +/* Provided by the demo source when compiled with -DDEMO_TEST. + * Calls ux_device_hid_init() (device stack, no ux_system_initialize), + * registers the DCD, and starts the device HID worker thread. */ +extern UINT usbx_demo_init(VOID); + +/* Declared in usbxtestcontrol.c */ +void test_control_return(UINT status); + +/* --------------------------------------------------------------------------- + * Test-local state + * --------------------------------------------------------------------------*/ + +#define TEST_STACK_SIZE 2048 +#define TEST_MAX_WAIT_MS 5000 /* maximum milliseconds to wait for events */ +#define TEST_TICK_MS 10 /* poll interval */ +#define TEST_MEMORY_SIZE (64 * 1024) + +static UX_HOST_CLASS_HID *hid_instance; +static UX_HOST_CLASS_HID_KEYBOARD *hid_keyboard; + +static TX_THREAD host_sim_thread; +static ULONG host_sim_stack[TEST_STACK_SIZE / sizeof(ULONG)]; + +static UCHAR test_memory[TEST_MEMORY_SIZE]; + +static UINT host_event_callback(ULONG event, + UX_HOST_CLASS *current_class, + VOID *current_instance); +static void host_sim_thread_entry(ULONG arg); + +/* --------------------------------------------------------------------------- + * test_application_define (dispatched by the test harness under CTEST) + * --------------------------------------------------------------------------*/ + +#ifdef CTEST +void test_application_define(void *first_unused_memory) +#else +void usbx_hid_keyboard_demo_device_rtos_test_application_define(void *first_unused_memory) +#endif +{ + UINT status; + + (void)first_unused_memory; + + printf("Running HID Keyboard Demo Device RTOS Test.............. "); + + /* ------------------------------------------------------------------- + * 1. Initialise the combined USBX system (host + device memory). + * The sample's ux_device_hid_init() skips this under DEMO_TEST. + * ----------------------------------------------------------------- */ + status = ux_system_initialize(test_memory, TEST_MEMORY_SIZE, UX_NULL, 0); + if (status != UX_SUCCESS) + { + printf("FAILED (ux_system_initialize, line %d, status 0x%02x)\n", __LINE__, status); + test_control_return(1); + return; + } + + /* ------------------------------------------------------------------- + * 2. Initialise the host stack and register the HID class + keyboard + * client before the device is connected. + * ----------------------------------------------------------------- */ + status = ux_host_stack_initialize(host_event_callback); + if (status != UX_SUCCESS) + { + printf("FAILED (host stack init, line %d, status 0x%02x)\n", __LINE__, status); + test_control_return(1); + return; + } + + status = ux_host_stack_class_register(_ux_system_host_class_hid_name, + ux_host_class_hid_entry); + if (status != UX_SUCCESS) + { + printf("FAILED (HID class register, line %d, status 0x%02x)\n", __LINE__, status); + test_control_return(1); + return; + } + + status = ux_host_class_hid_client_register( + _ux_system_host_class_hid_client_keyboard_name, + ux_host_class_hid_keyboard_entry); + if (status != UX_SUCCESS) + { + printf("FAILED (keyboard client register, line %d, status 0x%02x)\n", __LINE__, status); + test_control_return(1); + return; + } + + /* ------------------------------------------------------------------- + * 3. Initialise the device side via the demo's test-mode entry: + * - ux_device_stack_initialize() (uses sample's USB descriptors) + * - register the HID keyboard class (uses sample's callbacks) + * - ux_dcd_sim_slave_initialize() (DCD must be registered before HCD) + * - start the device HID worker thread + * ----------------------------------------------------------------- */ + status = usbx_demo_init(); + if (status != UX_SUCCESS) + { + printf("FAILED (device init, line %d, status 0x%02x)\n", __LINE__, status); + test_control_return(1); + return; + } + + /* ------------------------------------------------------------------- + * 4. Register the HCD simulator. Because the DCD was already + * registered in step 3, this immediately establishes the USB link. + * ----------------------------------------------------------------- */ + status = ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, + ux_hcd_sim_host_initialize, 0, 0); + if (status != UX_SUCCESS) + { + printf("FAILED (HCD register, line %d, status 0x%02x)\n", __LINE__, status); + test_control_return(1); + return; + } + + /* ------------------------------------------------------------------- + * 5. Create the host simulation thread that verifies device behaviour. + * ----------------------------------------------------------------- */ + status = tx_thread_create(&host_sim_thread, + "host sim thread", + host_sim_thread_entry, 0, + host_sim_stack, sizeof(host_sim_stack), + 20, 20, TX_NO_TIME_SLICE, TX_AUTO_START); + if (status != TX_SUCCESS) + { + printf("FAILED (host thread create, line %d, status 0x%02x)\n", __LINE__, status); + test_control_return(1); + } +} + +/* --------------------------------------------------------------------------- + * host_sim_thread_entry — waits for enumeration, reads key events, reports. + * --------------------------------------------------------------------------*/ + +static void host_sim_thread_entry(ULONG arg) +{ + UINT wait_ticks = 0; + UINT max_ticks = TEST_MAX_WAIT_MS / TEST_TICK_MS; + ULONG key = 0; + ULONG modifier = 0; + UINT status; + + (void)arg; + + /* --- Wait for the keyboard instance to become live. --- */ + while (wait_ticks < max_ticks) + { + if ((hid_keyboard != UX_NULL) && + (hid_keyboard->ux_host_class_hid_keyboard_state == + (ULONG)UX_HOST_CLASS_INSTANCE_LIVE)) + { + break; + } + tx_thread_sleep(MS_TO_TICK(TEST_TICK_MS)); + wait_ticks++; + } + + if ((hid_keyboard == UX_NULL) || + (hid_keyboard->ux_host_class_hid_keyboard_state != + (ULONG)UX_HOST_CLASS_INSTANCE_LIVE)) + { + printf("FAILED (keyboard not live after %u ms)\n", TEST_MAX_WAIT_MS); + test_control_return(1); + return; + } + + /* --- Wait for at least one key event from the device. --- */ + wait_ticks = 0; + while (wait_ticks < max_ticks) + { + status = ux_host_class_hid_keyboard_key_get(hid_keyboard, &key, &modifier); + if (status == UX_SUCCESS) + break; + + tx_thread_sleep(MS_TO_TICK(TEST_TICK_MS)); + wait_ticks++; + } + + if (status != UX_SUCCESS) + { + printf("FAILED (no key event received after %u ms)\n", TEST_MAX_WAIT_MS); + test_control_return(1); + return; + } + + printf("SUCCESS!\n"); + test_control_return(0); +} + +/* --------------------------------------------------------------------------- + * host_event_callback — tracks HID class and keyboard client insertions. + * --------------------------------------------------------------------------*/ + +static UINT host_event_callback(ULONG event, + UX_HOST_CLASS *current_class, + VOID *current_instance) +{ + UX_HOST_CLASS_HID_CLIENT *client = + (UX_HOST_CLASS_HID_CLIENT *)current_instance; + + switch (event) + { + case UX_DEVICE_INSERTION: + if (current_class->ux_host_class_entry_function == + ux_host_class_hid_entry) + { + if (hid_instance == UX_NULL) + hid_instance = (UX_HOST_CLASS_HID *)current_instance; + } + break; + + case UX_DEVICE_REMOVAL: + if ((VOID *)hid_instance == current_instance) + hid_instance = UX_NULL; + break; + + case UX_HID_CLIENT_INSERTION: + if (client->ux_host_class_hid_client_handler == + ux_host_class_hid_keyboard_entry) + { + if (hid_keyboard == UX_NULL) + hid_keyboard = (UX_HOST_CLASS_HID_KEYBOARD *) + client->ux_host_class_hid_client_local_instance; + } + break; + + case UX_HID_CLIENT_REMOVAL: + if ((VOID *)hid_keyboard == + client->ux_host_class_hid_client_local_instance) + { + hid_keyboard = UX_NULL; + } + break; + + default: + break; + } + + return UX_SUCCESS; +} diff --git a/test/regression_samples_rtos/usbx_hid_mouse_demo_device_rtos_test.c b/test/regression_samples_rtos/usbx_hid_mouse_demo_device_rtos_test.c new file mode 100644 index 000000000..ea44e0c14 --- /dev/null +++ b/test/regression_samples_rtos/usbx_hid_mouse_demo_device_rtos_test.c @@ -0,0 +1,247 @@ +/* Regression test for samples/demo_device_hid_mouse_rtos.c + * + * This test initialises the HID mouse device demo (device side only) and + * exercises it through the host-side HID stack using the USB simulator pair. + * It verifies that: + * 1. The device enumerates correctly on the host. + * 2. The host HID mouse client instance becomes live. + * 3. Mouse position data can be read from the device. + */ + +#include +#include "tx_api.h" +#include "ux_api.h" +#include "ux_host_class_hid.h" +#include "ux_host_class_hid_mouse.h" + +/* Provided by the demo source when compiled with -DDEMO_TEST. + * Calls ux_device_hid_init() (device stack, no ux_system_initialize), + * registers the DCD, and starts the device HID worker thread. */ +extern UINT usbx_demo_init(VOID); + +/* Declared in usbxtestcontrol.c */ +void test_control_return(UINT status); + +/* --------------------------------------------------------------------------- + * Test-local state + * --------------------------------------------------------------------------*/ + +#define TEST_STACK_SIZE 2048 +#define TEST_MAX_WAIT_MS 5000 /* maximum milliseconds to wait for events */ +#define TEST_TICK_MS 10 /* poll interval */ +#define TEST_MEMORY_SIZE (64 * 1024) + +static UX_HOST_CLASS_HID *hid_instance; +static UX_HOST_CLASS_HID_MOUSE *hid_mouse; + +static TX_THREAD host_sim_thread; +static ULONG host_sim_stack[TEST_STACK_SIZE / sizeof(ULONG)]; + +static UCHAR test_memory[TEST_MEMORY_SIZE]; + +static UINT host_event_callback(ULONG event, + UX_HOST_CLASS *current_class, + VOID *current_instance); +static void host_sim_thread_entry(ULONG arg); + +/* --------------------------------------------------------------------------- + * test_application_define (dispatched by the test harness under CTEST) + * --------------------------------------------------------------------------*/ + +#ifdef CTEST +void test_application_define(void *first_unused_memory) +#else +void usbx_hid_mouse_demo_device_rtos_test_application_define(void *first_unused_memory) +#endif +{ + UINT status; + + (void)first_unused_memory; + + printf("Running HID Mouse Demo Device RTOS Test................. "); + + /* ------------------------------------------------------------------- + * 1. Initialise the combined USBX system (host + device memory). + * The sample's ux_device_hid_init() skips this under DEMO_TEST, + * so we must do it here before any stack is initialised. + * ----------------------------------------------------------------- */ + status = ux_system_initialize(test_memory, TEST_MEMORY_SIZE, UX_NULL, 0); + if (status != UX_SUCCESS) + { + printf("FAILED (ux_system_initialize, line %d, status 0x%02x)\n", __LINE__, status); + test_control_return(1); + return; + } + + /* ------------------------------------------------------------------- + * 2. Initialise the host stack and register the HID class + mouse + * client before the device is connected. + * ----------------------------------------------------------------- */ + status = ux_host_stack_initialize(host_event_callback); + if (status != UX_SUCCESS) + { + printf("FAILED (host stack init, line %d, status 0x%02x)\n", __LINE__, status); + test_control_return(1); + return; + } + + status = ux_host_stack_class_register(_ux_system_host_class_hid_name, + ux_host_class_hid_entry); + if (status != UX_SUCCESS) + { + printf("FAILED (HID class register, line %d, status 0x%02x)\n", __LINE__, status); + test_control_return(1); + return; + } + + status = ux_host_class_hid_client_register( + _ux_system_host_class_hid_client_mouse_name, + ux_host_class_hid_mouse_entry); + if (status != UX_SUCCESS) + { + printf("FAILED (mouse client register, line %d, status 0x%02x)\n", __LINE__, status); + test_control_return(1); + return; + } + + /* ------------------------------------------------------------------- + * 3. Initialise the device side via the demo's test-mode entry: + * - ux_device_stack_initialize() (uses sample's USB descriptors) + * - register the HID mouse class (uses sample's callbacks) + * - ux_dcd_sim_slave_initialize() (DCD must be registered before HCD) + * - start the device HID worker thread + * ----------------------------------------------------------------- */ + status = usbx_demo_init(); + if (status != UX_SUCCESS) + { + printf("FAILED (device init, line %d, status 0x%02x)\n", __LINE__, status); + test_control_return(1); + return; + } + + /* ------------------------------------------------------------------- + * 4. Register the HCD simulator. Because the DCD was already + * registered in step 3, this immediately establishes the USB link + * and starts device enumeration. + * ----------------------------------------------------------------- */ + status = ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, + ux_hcd_sim_host_initialize, 0, 0); + if (status != UX_SUCCESS) + { + printf("FAILED (HCD register, line %d, status 0x%02x)\n", __LINE__, status); + test_control_return(1); + return; + } + + /* ------------------------------------------------------------------- + * 5. Create the host simulation thread that verifies device behaviour. + * ----------------------------------------------------------------- */ + status = tx_thread_create(&host_sim_thread, + "host sim thread", + host_sim_thread_entry, 0, + host_sim_stack, sizeof(host_sim_stack), + 20, 20, TX_NO_TIME_SLICE, TX_AUTO_START); + if (status != TX_SUCCESS) + { + printf("FAILED (host thread create, line %d, status 0x%02x)\n", __LINE__, status); + test_control_return(1); + } +} + +/* --------------------------------------------------------------------------- + * host_sim_thread_entry — waits for enumeration, reads mouse data, reports. + * --------------------------------------------------------------------------*/ + +static void host_sim_thread_entry(ULONG arg) +{ + UINT wait_ticks = 0; + UINT max_ticks = TEST_MAX_WAIT_MS / TEST_TICK_MS; + SLONG x = 0, y = 0; + + (void)arg; + + /* --- Wait for the mouse instance to become live. --- */ + while (wait_ticks < max_ticks) + { + if ((hid_mouse != UX_NULL) && + (hid_mouse->ux_host_class_hid_mouse_state == + (ULONG)UX_HOST_CLASS_INSTANCE_LIVE)) + { + break; + } + tx_thread_sleep(MS_TO_TICK(TEST_TICK_MS)); + wait_ticks++; + } + + if ((hid_mouse == UX_NULL) || + (hid_mouse->ux_host_class_hid_mouse_state != + (ULONG)UX_HOST_CLASS_INSTANCE_LIVE)) + { + printf("FAILED (mouse not live after %u ms)\n", TEST_MAX_WAIT_MS); + test_control_return(1); + return; + } + + /* --- Read mouse position at least once successfully. --- */ + if (ux_host_class_hid_mouse_position_get(hid_mouse, &x, &y) != UX_SUCCESS) + { + printf("FAILED (position_get failed)\n"); + test_control_return(1); + return; + } + + printf("SUCCESS!\n"); + test_control_return(0); +} + +/* --------------------------------------------------------------------------- + * host_event_callback — tracks HID class and mouse client insertions. + * --------------------------------------------------------------------------*/ + +static UINT host_event_callback(ULONG event, + UX_HOST_CLASS *current_class, + VOID *current_instance) +{ + UX_HOST_CLASS_HID_CLIENT *client = + (UX_HOST_CLASS_HID_CLIENT *)current_instance; + + switch (event) + { + case UX_DEVICE_INSERTION: + if (current_class->ux_host_class_entry_function == + ux_host_class_hid_entry) + { + if (hid_instance == UX_NULL) + hid_instance = (UX_HOST_CLASS_HID *)current_instance; + } + break; + + case UX_DEVICE_REMOVAL: + if ((VOID *)hid_instance == current_instance) + hid_instance = UX_NULL; + break; + + case UX_HID_CLIENT_INSERTION: + if (client->ux_host_class_hid_client_handler == + ux_host_class_hid_mouse_entry) + { + if (hid_mouse == UX_NULL) + hid_mouse = (UX_HOST_CLASS_HID_MOUSE *) + client->ux_host_class_hid_client_local_instance; + } + break; + + case UX_HID_CLIENT_REMOVAL: + if ((VOID *)hid_mouse == + client->ux_host_class_hid_client_local_instance) + { + hid_mouse = UX_NULL; + } + break; + + default: + break; + } + + return UX_SUCCESS; +} diff --git a/test/regression_samples_rtos/usbxtestcontrol.c b/test/regression_samples_rtos/usbxtestcontrol.c new file mode 100644 index 000000000..48824f0d9 --- /dev/null +++ b/test/regression_samples_rtos/usbxtestcontrol.c @@ -0,0 +1,160 @@ +/* This is the test control routine for the USBX sample regression tests. + Each sample is compiled as its own test binary (one test per executable). + Under CTEST, the test array contains a single entry: test_application_define, + which each test file defines under #ifdef CTEST. */ + +#include "tx_api.h" +#include "ux_api.h" +#include +#include + +#define TEST_STACK_SIZE 6144 + +/* Define the test control thread and shared state. */ + +TX_THREAD test_control_thread; +TX_THREAD test_thread; + +ULONG test_control_return_status; +ULONG test_control_successful_tests; +ULONG test_control_failed_tests; +ULONG test_control_system_errors; + +UCHAR *test_free_memory_ptr; + +VOID (*test_isr_dispatch)(void); + +UCHAR test_control_thread_stack[TEST_STACK_SIZE]; +UCHAR tests_memory[64*1024*1024]; + +/* Define the external preempt/system-state references used by test_control_return. */ + +extern volatile UINT _tx_thread_preempt_disable; +extern volatile ULONG _tx_thread_system_state; + +/* Test entry type. */ + +typedef struct TEST_ENTRY_STRUCT +{ + VOID (*test_entry)(void *); +} TEST_ENTRY; + +/* Each binary defines test_application_define in its test source file. */ + +void test_application_define(void *first_unused_memory); + +/* Test array: exactly one entry per binary when compiled with -DCTEST. */ + +TEST_ENTRY test_control_tests[] = +{ +#ifdef CTEST + test_application_define, +#endif + { UX_NULL }, +}; + +/* Thread and return prototypes. */ + +void test_control_thread_entry(ULONG thread_input); +void test_control_return(UINT status); + +extern ULONG _tx_thread_created_count; + +/* main — entry point provided by the test harness (EXTERNAL_MAIN must be defined + in sample source files so they do not provide their own main). */ + +void main(void) +{ + tx_kernel_enter(); +} + +/* tx_application_define — called by the ThreadX kernel during startup. */ + +void tx_application_define(void *first_unused_memory) +{ + test_control_successful_tests = 0; + test_control_failed_tests = 0; + test_control_system_errors = 0; + + tx_thread_create(&test_control_thread, "test control thread", + test_control_thread_entry, 0, + test_control_thread_stack, TEST_STACK_SIZE, + 17, 15, TX_NO_TIME_SLICE, TX_AUTO_START); + + test_free_memory_ptr = &tests_memory[0]; +} + +/* test_control_thread_entry — dispatches each test in test_control_tests[]. */ + +void test_control_thread_entry(ULONG thread_input) +{ + ULONG previous_failed; + ULONG previous_thread_count; + UINT i; + + (void)thread_input; + + tx_thread_priority_change(&test_control_thread, 0, &i); + + printf("**** USBX Samples Regression Test Suite ****\n\n"); + printf("Version: %s Data width: x%i\n\n", + _ux_version_id, (int)(sizeof(void *) * 8)); + + i = 0; + while (test_control_tests[i].test_entry != UX_NULL) + { + previous_failed = test_control_failed_tests; + previous_thread_count = _tx_thread_created_count; + + (test_control_tests[i++].test_entry)(test_free_memory_ptr); + + /* If the test created threads, suspend until test_control_return wakes us. */ + if (test_control_failed_tests == previous_failed && + _tx_thread_created_count != previous_thread_count) + { + tx_thread_suspend(&test_control_thread); + } + } + + printf("\n**** Summary: Passed: %lu Failed: %lu System errors: %lu ****\n\n", + test_control_successful_tests, + test_control_failed_tests, + test_control_system_errors); + + exit((int)test_control_failed_tests); +} + +/* test_control_return — called by each test when it is done. */ + +void test_control_return(UINT status) +{ + UINT old_posture = TX_INT_ENABLE; + + test_control_return_status = status; + + old_posture = tx_interrupt_control(TX_INT_ENABLE); + + if (status) + test_control_failed_tests++; + else + test_control_successful_tests++; + + /* Basic sanity checks. */ + if (_tx_thread_preempt_disable) + { + printf(" ***** SYSTEM ERROR ***** _tx_thread_preempt_disable non-zero!\n"); + test_control_system_errors++; + } + if (_tx_thread_system_state) + { + printf(" ***** SYSTEM ERROR ***** _tx_thread_system_state non-zero!\n"); + test_control_system_errors++; + } + if (old_posture == TX_INT_DISABLE) + { + printf(" ***** SYSTEM ERROR ***** test returned with interrupts disabled!\n"); + test_control_system_errors++; + } + + tx_thread_resume(&test_control_thread); +} diff --git a/test/regression_samples_rtos/ux_test.c b/test/regression_samples_rtos/ux_test.c new file mode 100644 index 000000000..b2eeb8e6a --- /dev/null +++ b/test/regression_samples_rtos/ux_test.c @@ -0,0 +1,1834 @@ +#include "ux_test.h" +#include "ux_test_hcd_sim_host.h" +#include "ux_test_dcd_sim_slave.h" + +#define UX_TEST_TIMEOUT_MS 3000 + +static UX_TEST_ACTION ux_test_action_handler_check(UX_TEST_ACTION *action, UX_TEST_FUNCTION usbx_function, void *params, UCHAR advance); + +UINT _ux_hcd_sim_host_entry(UX_HCD *hcd, UINT function, VOID *parameter); +UINT _ux_dcd_sim_slave_function(UX_SLAVE_DCD *dcd, UINT function, VOID *parameter); +void test_control_return(UINT); +extern ULONG ux_test_port_status; + +static UX_TEST_ACTION *ux_test_hook_action_list; /* Link list, with .next NULL as end. */ +static UX_TEST_ACTION *ux_test_main_action_list; /* Link list, with .next NULL as end. */ +static UX_TEST_ACTION *ux_test_user_list_actions;/* Link list, with .created_list_next NULL as end. */ +static UCHAR ux_test_expedient = 1; +static UCHAR ignore_all_errors = 0; +static UCHAR exit_on_errors = 0; +static ULONG ux_test_memory_test_no_device_memory_free_amount; + +static UCHAR ux_test_assert_hit_hint_off = 0; +static UCHAR ux_test_assert_hit_exit_off = 0; +static ULONG ux_test_assert_hit_count = 0; +static UINT ux_test_assert_hit_exit_code = 1; + +VOID _ux_test_main_action_list_thread_update(TX_THREAD *old, TX_THREAD *new) +{ + UX_TEST_ACTION *list = ux_test_main_action_list; + if (old != new) + { + while(list) + { + if (list->thread_ptr == old) + list->thread_ptr == new; + list = list->next; + } + } +} + +VOID _ux_test_main_action_list_semaphore_update(TX_SEMAPHORE *old, TX_SEMAPHORE *new) +{ + UX_TEST_ACTION *list = ux_test_main_action_list; + if (old != new) + { + while(list) + { + if (list->semaphore_ptr == old) + list->semaphore_ptr == new; + list = list->next; + } + } +} + +VOID _ux_test_main_action_list_mutex_update(TX_MUTEX *old, TX_MUTEX *new) +{ + UX_TEST_ACTION *list = ux_test_main_action_list; + if (old != new) + { + while(list) + { + if (list->mutex_ptr == old) + list->mutex_ptr == new; + list = list->next; + } + } +} + + +UCHAR _ux_test_check_action_function(UX_TEST_ACTION *new_actions) +{ + +UCHAR result; + + +#ifndef UX_TEST_RACE_CONDITION_TESTS_ON + result = (new_actions->usbx_function > 0 && new_actions->usbx_function < (ULONG)UX_TEST_OVERRIDE_RACE_CONDITION_OVERRIDES); +#else + result = (new_actions->_usbx_function > 0 && new_actions->_usbx_function < (ULONG)UX_TEST_NUMBER_OVERRIDES); +#endif + + return result; +} + +VOID _ux_test_append_action(UX_TEST_ACTION *list, UX_TEST_ACTION *new_action) +{ + +UX_TEST_ACTION *tail; + + + tail = list; + while (tail->next != UX_NULL) + tail = tail->next; + tail->next = new_action; +} + +UINT ux_test_list_action_compare(UX_TEST_ACTION *list_item, UX_TEST_ACTION *action) +{ +UX_TEST_ACTION temp; +UINT status; + + /* It's created one, check its contents. */ + if (list_item->created_list_next == list_item) + { + /* Copy action contents to test. */ + temp = *action; + + /* Set action next, created to match. */ + temp.next = list_item->next; + temp.created_list_next = list_item->created_list_next; + + /* Compare. */ + status = ux_utility_memory_compare(list_item, &temp, sizeof(temp)); + + /* Return success if equal. */ + if (status == UX_SUCCESS) + return status; + } + else + { + + /* It's static one, check address. */ + if (list_item == action) + return UX_SUCCESS; + } + + /* No, they do not match. */ + return UX_NO_CLASS_MATCH; +} + +VOID ux_test_remove_hook(UX_TEST_ACTION *action) +{ +UX_TEST_ACTION *item; +UX_TEST_ACTION *previous; + + item = ux_test_hook_action_list; + while(item) + { + if (ux_test_list_action_compare(item, action) != UX_SUCCESS) + { + previous = item; + item = item->next; + continue; + } + + /* Remove action from head. */ + if (item == ux_test_hook_action_list) + { + ux_test_hook_action_list = item->next; + } + + /* Remove action from list. */ + else + { + previous->next = action->next; + } + + /* Free if it's created. */ + if (action->created_list_next == action) + free(action); + + /* Remove is done. */ + return; + } +} + +UINT ux_test_link_hook(UX_TEST_ACTION *action) +{ +UX_TEST_ACTION *tail; + + if (ux_test_hook_action_list == UX_NULL) + { + ux_test_hook_action_list = action; + } + else + { + tail = ux_test_hook_action_list; + while(tail) + { + /* Check existing. */ + if (ux_test_list_action_compare(tail, action) == UX_SUCCESS) + return UX_ERROR; + + /* Check next. */ + if (tail->next == UX_NULL) + break; + + tail = tail->next; + } + tail->next = action; + } + return UX_SUCCESS; +} + +UINT ux_test_add_hook(UX_TEST_ACTION action) +{ +UX_TEST_ACTION *created_action; +UINT status; + + created_action = (UX_TEST_ACTION *)malloc(sizeof(UX_TEST_ACTION)); + UX_TEST_ASSERT(created_action); + + *created_action = action; + + created_action->next = UX_NULL; + + /* We use the memory pointer to indicate it's added by memory allocation. + * On clean up or free we should free allocated memory. + */ + created_action->created_list_next = created_action; + + status = ux_test_link_hook(created_action); + if (status != UX_SUCCESS) + { + free(created_action); + } + return status; +} + +VOID ux_test_remove_hooks_from_array(UX_TEST_ACTION *actions) +{ +UX_TEST_ACTION *action; + + action = actions; + while(action->usbx_function) + { + //printf("rm %p\n", action); + UX_TEST_ASSERT(_ux_test_check_action_function(action)); + ux_test_remove_hook(action); + action ++; + } +} + +VOID ux_test_remove_hooks_from_list(UX_TEST_ACTION *actions) +{ +UX_TEST_ACTION *action; +UX_TEST_ACTION *next; + + action = actions; + while(action) + { + UX_TEST_ASSERT(_ux_test_check_action_function(action)); + next = action->next; + ux_test_remove_hook(action); + action = next; + } +} + +VOID ux_test_link_hooks_from_array(UX_TEST_ACTION *actions) +{ +UX_TEST_ACTION *action; + + action = actions; + while(action->usbx_function) + { + UX_TEST_ASSERT(_ux_test_check_action_function(action)); + ux_test_link_hook(action); + action ++; + } +} + +VOID ux_test_link_hooks_from_list(UX_TEST_ACTION *actions) +{ +UX_TEST_ACTION *action; + + action = actions; + while(action) + { + UX_TEST_ASSERT(_ux_test_check_action_function(action)); + ux_test_link_hook(action); + action = action->next; + } +} + +VOID ux_test_add_hooks_from_array(UX_TEST_ACTION *actions) +{ +UX_TEST_ACTION *action; + + action = actions; + while(action->usbx_function) + { + UX_TEST_ASSERT(_ux_test_check_action_function(action)); + ux_test_add_hook(*action); + action ++; + } +} + +VOID ux_test_add_hooks_from_list(UX_TEST_ACTION *actions) +{ +UX_TEST_ACTION *action; + + action = actions; + while(action) + { + UX_TEST_ASSERT(_ux_test_check_action_function(action)); + ux_test_add_hook(*action); + action = action->next; + } +} + +ULONG ux_test_do_hooks_before(UX_TEST_FUNCTION usbx_function, VOID *params) +{ +UX_TEST_ACTION *list; +UX_TEST_ACTION action; +ULONG action_count = 0; +ULONG action_return = 0; + + list = ux_test_hook_action_list; + while(list) + { + action = ux_test_action_handler_check(list, usbx_function, params, UX_FALSE); + if (action.matched && !action.do_after) + { + action_count ++; + if (!action.no_return) + action_return = 0x80000000u; + } + ux_test_do_action_before(&action, params); + list = list->next; + } + return (action_count | action_return); +} + +VOID ux_test_do_hooks_after(UX_TEST_FUNCTION usbx_function, VOID *params) +{ +UX_TEST_ACTION *list; +UX_TEST_ACTION action; +ULONG action_count = 0; + + list = ux_test_hook_action_list; + while(list) + { + action = ux_test_action_handler_check(list, usbx_function, params, UX_FALSE); + if (action.matched) + action_count ++; + ux_test_do_action_after(&action, params); + list = list->next; + } +} + +VOID ux_test_free_hook_actions() +{ +UX_TEST_ACTION *action; + + while(ux_test_hook_action_list) + { + action = ux_test_hook_action_list; + ux_test_hook_action_list = ux_test_hook_action_list->next; + + /* If this action is added/created, free allocated memory. */ + if (action->created_list_next == action) + { + free(action); + } + } +} + +VOID _ux_test_add_action_to_list(UX_TEST_ACTION *list, UX_TEST_ACTION action) +{ + +UX_TEST_ACTION *new_action = UX_NULL; +UX_TEST_ACTION *tail; + + + /* Do some idiot-proof checks. */ + { + /* Check the function. */ +#ifndef UX_TEST_RACE_CONDITION_TESTS_ON + UX_TEST_ASSERT((action.usbx_function > 0 && action.usbx_function < (ULONG)UX_TEST_OVERRIDE_RACE_CONDITION_OVERRIDES)); +#else + UX_TEST_ASSERT((action._usbx_function > 0 && action._usbx_function < (ULONG)UX_TEST_NUMBER_OVERRIDES)); +#endif + + /* Make sure expedient is on when it should be. */ + if (action.no_return == 0 && + /* For user callbacks, it's okay. */ + action.usbx_function < UX_TEST_OVERRIDE_USER_CALLBACKS) + { + /* Allowed if flow stopped to avoid low level operations. */ + #if 0 + UX_TEST_ASSERT(ux_test_is_expedient_on()); + #endif + } + } + + if (/* Is this the main list? */ + list == ux_test_main_action_list || + /* Is the head of user-list already an action? */ + list->usbx_function) + { + new_action = (UX_TEST_ACTION *)malloc(sizeof(UX_TEST_ACTION)); + *new_action = action; + new_action->next = UX_NULL; + } + + if (list == ux_test_main_action_list) + { + + if (!ux_test_main_action_list) + ux_test_main_action_list = new_action; + else + _ux_test_append_action(list, new_action); + } + else + { + + if (!list->usbx_function) + *list = action; + else + { + + /* Append to user created list. */ + if (!ux_test_user_list_actions) + ux_test_user_list_actions = new_action; + else + { + + tail = ux_test_user_list_actions; + while (tail->created_list_next) + tail = tail->created_list_next; + tail->created_list_next = new_action; + } + + _ux_test_append_action(list, new_action); + } + } +} + +VOID ux_test_add_action_to_main_list(UX_TEST_ACTION new_action) +{ + + _ux_test_add_action_to_list(ux_test_main_action_list, new_action); +} + +VOID ux_test_add_action_to_main_list_multiple(UX_TEST_ACTION new_action, UINT num) +{ + + while (num--) + _ux_test_add_action_to_list(ux_test_main_action_list, new_action); +} + +VOID ux_test_add_action_to_user_list(UX_TEST_ACTION *list, UX_TEST_ACTION new_action) +{ + + _ux_test_add_action_to_list(list, new_action); +} + +VOID ux_test_set_main_action_list_from_list(UX_TEST_ACTION *new_actions) +{ + + UX_TEST_ASSERT(!ux_test_main_action_list); + while (new_actions) + { + + ux_test_add_action_to_main_list(*new_actions); + new_actions = new_actions->next; + } +} + +VOID ux_test_set_main_action_list_from_array(UX_TEST_ACTION *new_actions) +{ + + UX_TEST_ASSERT(!ux_test_main_action_list); + while (new_actions->usbx_function) + { + + ux_test_add_action_to_main_list(*new_actions); + new_actions++; + } +} + +VOID ux_test_free_user_list_actions() +{ + +UX_TEST_ACTION *action; + + + while (ux_test_user_list_actions) + { + + action = ux_test_user_list_actions; + ux_test_user_list_actions = ux_test_user_list_actions->created_list_next; + free(action); + } +} + +static VOID _ux_test_advance_actions() +{ + +UX_TEST_ACTION *action; + + + UX_TEST_ASSERT(ux_test_main_action_list != UX_NULL); + action = ux_test_main_action_list; + ux_test_main_action_list = ux_test_main_action_list->next; + free(action); +} + +VOID ux_test_clear_main_list_actions() +{ + + while (ux_test_main_action_list) + _ux_test_advance_actions(); +} + +VOID _ux_test_set_actions_and_set_function(UX_TEST_ACTION *new_actions, UX_TEST_FUNCTION function) +{ + + UX_TEST_ASSERT(new_actions != UX_NULL); + + while (new_actions->function || new_actions->usbx_function) + { + + if (new_actions->function && !new_actions->usbx_function) + new_actions->usbx_function = function; + ux_test_add_action_to_main_list(*new_actions); + new_actions++; + } +} + +VOID ux_test_hcd_sim_host_set_actions(UX_TEST_ACTION *new_actions) +{ + + ux_test_clear_main_list_actions(); + + if (new_actions != UX_NULL) + _ux_test_set_actions_and_set_function(new_actions, UX_TEST_OVERRIDE_UX_HCD_SIM_HOST_ENTRY); +} + +VOID ux_test_dcd_sim_slave_set_actions(UX_TEST_ACTION *new_actions) +{ + + ux_test_clear_main_list_actions(); + + if (new_actions != UX_NULL) + _ux_test_set_actions_and_set_function(new_actions, UX_TEST_OVERRIDE_UX_DCD_SIM_SLAVE_FUNCTION); +} + +/* Returns whether there are any actions in the list. */ +UCHAR ux_test_check_actions_empty() +{ + return(ux_test_main_action_list == UX_NULL ? UX_TRUE : UX_FALSE); +} + +UINT ux_test_wait_for_empty_actions() +{ + return ux_test_wait_for_null((VOID **)&ux_test_main_action_list); +} + +UINT ux_test_wait_for_empty_actions_wait_time(UINT wait_time_ms) +{ + return ux_test_wait_for_null_wait_time((VOID **)&ux_test_main_action_list, wait_time_ms); +} + +UINT ux_test_get_num_actions_left() +{ + +UINT num_actions_remaining = 0; +UX_TEST_ACTION *action = ux_test_main_action_list; + + + while (action) + { + num_actions_remaining++; + action = action->next; + } + + return(num_actions_remaining); +} + +VOID _ux_host_class_storage_driver_read_write_notify(VOID *func); + +VOID ux_test_cleanup_everything(VOID) +{ + + /* Free main list actions. */ + ux_test_clear_main_list_actions(); + + /* Free allocated user list actions. */ + ux_test_free_user_list_actions(); + + /* Free added hook actions. */ + ux_test_free_hook_actions(); + + /* Reset expedient value. */ + ux_test_expedient = UX_TRUE; + + /* Reset free memory test amount. */ + ux_test_memory_test_no_device_memory_free_amount = 0; + + /* Assert related. */ + ux_test_assert_hit_count = 0; + ux_test_assert_hit_hint_off = 0; + ux_test_assert_hit_exit_off = 0; + ux_test_assert_hit_exit_code = 1; + +#if defined(UX_HOST_CLASS_STORAGE_NO_FILEX) + _ux_host_class_storage_driver_read_write_notify(UX_NULL); +#endif +} + +VOID ux_test_do_action_before(UX_TEST_ACTION *action, VOID *params) +{ + + if (action->matched && !action->do_after) + { + if (action->action_func) + { + action->action_func(action, params); + } + } +} + +VOID ux_test_do_action_after(UX_TEST_ACTION *action, VOID *params) +{ + + if (action->matched && action->do_after) + { + if (action->action_func) + { + action->action_func(action, params); + } + } +} + +VOID ux_test_turn_on_expedient(UCHAR *original_expedient) +{ + + if (original_expedient) + { + *original_expedient = ux_test_expedient; + } + ux_test_expedient = 1; +} + +VOID ux_test_turn_off_expedient(UCHAR *original_expedient) +{ + + if (original_expedient) + { + *original_expedient = ux_test_expedient; + } + ux_test_expedient = 0; +} + +VOID ux_test_set_expedient(UCHAR value) +{ + + ux_test_expedient = value; +} + +UCHAR ux_test_is_expedient_on() +{ + + return(ux_test_expedient); +} + +UX_TEST_ACTION ux_test_action_handler(UX_TEST_FUNCTION usbx_function, void *params) +{ + return ux_test_action_handler_check(ux_test_main_action_list, usbx_function, params, UX_TRUE); +} + +static UX_TEST_ACTION ux_test_action_handler_check(UX_TEST_ACTION *list, UX_TEST_FUNCTION usbx_function, void *params, UCHAR advance) +{ + +UINT i; +UINT min; +UX_TEST_ACTION *this = list; +UX_TEST_ACTION result = { 0 }; +UCHAR act = 0; +UX_TRANSFER *host_req; +UX_SLAVE_TRANSFER *slave_req; +ULONG *slave_req_code = UX_NULL; +UINT *req_code = UX_NULL; +ULONG *req_actual_len = UX_NULL; +UX_ENDPOINT *ep; +TX_THREAD *this_thread; +UX_TEST_SETUP req_setup; +UX_TEST_SIM_ENTRY_ACTION generic_transfer_parameter = { 0 }; +UX_TEST_GENERIC_CD *generic_cd_params; +UX_TEST_GENERIC_CD _generic_cd_params; +UINT str_len0; +UINT str_len1; +UCHAR ignore_param_checks = 0; +UX_TEST_OVERRIDE_TX_SEMAPHORE_GET_PARAMS *semaphore_get_params; +UX_TEST_OVERRIDE_TX_SEMAPHORE_CREATE_PARAMS *semaphore_create_params; +UX_TEST_OVERRIDE_TX_THREAD_CREATE_PARAMS *thread_create_params; +UX_TEST_ERROR_CALLBACK_PARAMS *error_callback_params; +UX_TEST_OVERRIDE_UX_DEVICE_MEDIA_READ_WRITE_FLUSH_PARAMS*device_media_read_write_flush_params; +UX_TEST_OVERRIDE_UX_DEVICE_MEDIA_STATUS_PARAMS *device_media_status_params; +UX_TEST_OVERRIDE_UX_HOST_STACK_TRANSFER_REQUEST_PARAMS *host_stack_transfer_request_params; +UX_TEST_OVERRIDE_TX_THREAD_PREEMPTION_CHANGE_PARAMS *thread_preemption_change_params; +UX_TEST_OVERRIDE_UX_HOST_STACK_INTERFACE_SET_PARAMS *host_stack_interface_set_params; +UX_TEST_OVERRIDE_TX_MUTEX_GET_PARAMS *mutex_get_params; +UX_TEST_OVERRIDE_TX_MUTEX_PUT_PARAMS *mutex_put_params; +UX_TEST_OVERRIDE_TX_MUTEX_CREATE_PARAMS *mutex_create_params; +UX_TEST_OVERRIDE_NX_PACKET_POOL_CREATE_PARAMS *packet_pool_create_params; +UX_TEST_OVERRIDE_NX_PACKET_ALLOCATE_PARAMS *packet_allocate_params; + + + if (this) + { + + if (this->usbx_function == usbx_function) + { + + /* If appropriate, setup generic controller driver (CD) parameter. */ + if (this->usbx_function == UX_TEST_OVERRIDE_UX_HCD_SIM_HOST_ENTRY || + this->usbx_function == UX_TEST_OVERRIDE_UX_DCD_SIM_SLAVE_FUNCTION || + this->usbx_function == UX_TEST_OVERRIDE_UX_HOST_STACK_TRANSFER_REQUEST) + { + /* Treat a transfer request action like an HCD action. */ + if (usbx_function == UX_TEST_OVERRIDE_UX_HOST_STACK_TRANSFER_REQUEST) + { + host_stack_transfer_request_params = params; + generic_cd_params = &_generic_cd_params; + generic_cd_params->parameter = host_stack_transfer_request_params->transfer_request; + generic_cd_params->function = UX_HCD_TRANSFER_REQUEST; + } + else + { + generic_cd_params = params; + } + } + + /* Should we ignore the param checking? */ + if (this->ignore_params) + { + if (this->usbx_function == UX_TEST_OVERRIDE_UX_HCD_SIM_HOST_ENTRY || + this->usbx_function == UX_TEST_OVERRIDE_UX_DCD_SIM_SLAVE_FUNCTION) + { + /* Check the sub-function. */ + if (this->function == generic_cd_params->function) + { + ignore_param_checks = 1; + } + } + else + { + /* There is no sub-function. */ + ignore_param_checks = 1; + } + } + + act = 1; + if (!ignore_param_checks) + { + switch (usbx_function) + { + + case UX_TEST_OVERRIDE_UX_HCD_SIM_HOST_ENTRY: + case UX_TEST_OVERRIDE_UX_DCD_SIM_SLAVE_FUNCTION: + case UX_TEST_OVERRIDE_UX_HOST_STACK_TRANSFER_REQUEST: + + act = 0; + if (this->function == generic_cd_params->function) + { + + act = 1; + switch (generic_cd_params->function) + { + + /* We have action on endpoint entries */ + case UX_HCD_CREATE_ENDPOINT: + //case UX_DCD_CREATE_ENDPOINT: + case UX_HCD_DESTROY_ENDPOINT: + //case UX_DCD_DESTROY_ENDPOINT: + case UX_HCD_RESET_ENDPOINT: + //case UX_DCD_RESET_ENDPOINT: + case UX_DCD_ENDPOINT_STATUS: + case UX_DCD_STALL_ENDPOINT: + + ep = generic_cd_params->parameter; + + if ((this->req_action & UX_TEST_MATCH_EP) && + this->req_ep_address != ep->ux_endpoint_descriptor.bEndpointAddress) + act = 0; + + break; + + /* We have action on transfer request abort */ + case UX_HCD_TRANSFER_ABORT: + //case UX_DCD_TRANSFER_ABORT: + + if (UX_TEST_OVERRIDE_UX_DCD_SIM_SLAVE_FUNCTION != usbx_function) + { + + host_req = (UX_TRANSFER *)generic_cd_params->parameter; + if ((this->req_action & UX_TEST_MATCH_EP) && + this->req_ep_address != host_req->ux_transfer_request_endpoint->ux_endpoint_descriptor.bEndpointAddress) + act = 0; + } + else + { + + slave_req = (UX_SLAVE_TRANSFER *)generic_cd_params->parameter; + if ((this->req_action & UX_TEST_MATCH_EP) && + this->req_ep_address != slave_req->ux_slave_transfer_request_endpoint->ux_slave_endpoint_descriptor.bEndpointAddress) + act = 0; + } + break; + + /* We have action on transfer request */ + case UX_HCD_TRANSFER_REQUEST: + //case UX_DCD_TRANSFER_REQUEST: + + generic_transfer_parameter.req_setup = &req_setup; + + if (usbx_function == UX_TEST_OVERRIDE_UX_HCD_SIM_HOST_ENTRY || + usbx_function == UX_TEST_OVERRIDE_UX_HOST_STACK_TRANSFER_REQUEST) + { + host_req = generic_cd_params->parameter; + + generic_transfer_parameter.req_actual_len = host_req->ux_transfer_request_actual_length; + generic_transfer_parameter.req_data = host_req->ux_transfer_request_data_pointer; + generic_transfer_parameter.req_ep_address = host_req->ux_transfer_request_endpoint->ux_endpoint_descriptor.bEndpointAddress; + generic_transfer_parameter.req_requested_len = host_req->ux_transfer_request_requested_length; + generic_transfer_parameter.req_status = host_req->ux_transfer_request_status; + generic_transfer_parameter.req_setup->ux_test_setup_index = host_req->ux_transfer_request_index; + generic_transfer_parameter.req_setup->ux_test_setup_request = host_req->ux_transfer_request_function; + generic_transfer_parameter.req_setup->ux_test_setup_type = host_req->ux_transfer_request_type; + generic_transfer_parameter.req_setup->ux_test_setup_value = host_req->ux_transfer_request_value; + + req_actual_len = &host_req->ux_transfer_request_actual_length; + req_code = &host_req->ux_transfer_request_completion_code; + } + else + { + slave_req = generic_cd_params->parameter; + + generic_transfer_parameter.req_actual_len = slave_req->ux_slave_transfer_request_actual_length; + generic_transfer_parameter.req_data = slave_req->ux_slave_transfer_request_data_pointer; + generic_transfer_parameter.req_ep_address = slave_req->ux_slave_transfer_request_endpoint->ux_slave_endpoint_descriptor.bEndpointAddress; + generic_transfer_parameter.req_requested_len = slave_req->ux_slave_transfer_request_requested_length; + generic_transfer_parameter.req_status = slave_req->ux_slave_transfer_request_status; + generic_transfer_parameter.req_setup->ux_test_setup_type = (UCHAR)slave_req->ux_slave_transfer_request_type; + + req_actual_len = &slave_req->ux_slave_transfer_request_actual_length; + slave_req_code = &slave_req->ux_slave_transfer_request_completion_code; + } + + if (this->req_action & UX_TEST_MATCH_EP && + this->req_ep_address != generic_transfer_parameter.req_ep_address) + act = 0; + + /* We must confirm request setup is matching */ + if (this->req_setup) + { + + if ((this->req_action & UX_TEST_SETUP_MATCH_REQUEST) && + (generic_transfer_parameter.req_setup->ux_test_setup_type != this->req_setup->ux_test_setup_type)) + act = 0; + + if ((this->req_action & UX_TEST_SETUP_MATCH_REQUEST) && + (generic_transfer_parameter.req_setup->ux_test_setup_request != this->req_setup->ux_test_setup_request)) + act = 0; + + if ((this->req_action & UX_TEST_SETUP_MATCH_VALUE) && + (generic_transfer_parameter.req_setup->ux_test_setup_value != this->req_setup->ux_test_setup_value)) + act = 0; + + if ((this->req_action & UX_TEST_SETUP_MATCH_INDEX) && + (generic_transfer_parameter.req_setup->ux_test_setup_index != this->req_setup->ux_test_setup_index)) + act = 0; + } + + /* Compare data. We only do the comparison if this action is not meant for substituting data. */ + if (!(this->req_action & UX_TEST_SIM_REQ_ANSWER) && this->req_data) + { + if (generic_transfer_parameter.req_data == UX_NULL || + /* Make sure we don't go off the end during the compare. */ + generic_transfer_parameter.req_requested_len < this->req_actual_len) + act = 0; + else + { + + /* Is there no mask? */ + if (!this->req_data_match_mask) + { + if (ux_utility_memory_compare(generic_transfer_parameter.req_data, this->req_data, this->req_actual_len) == UX_ERROR) + { + act = 0; + } + } + else + { + /* Compare with mask. */ + for (i = 0; i < this->req_actual_len; i++) + { + if (this->req_data_match_mask[i] && + this->req_data[i] != generic_transfer_parameter.req_data[i]) + { + act = 0; + break; + } + } + } + } + } + + /* Compare requested lengths. */ + if ((this->req_action & UX_TEST_MATCH_REQ_LEN) && + (generic_transfer_parameter.req_requested_len != this->req_requested_len)) + act = 0; + + /* Do additional check. */ + if (this->check_func && !this->check_func()) + act = 0; + + break; + + case UX_DCD_CHANGE_STATE: + case UX_HCD_RESET_PORT: + case UX_HCD_ENABLE_PORT: + case UX_HCD_GET_PORT_STATUS: + + /* AFAIK, there's nothing to check here. */ + + break; + + /* The other entries are not handled now */ + default: + UX_TEST_ASSERT(0); + break; + } + } + break; + + #if 0 + case UX_TEST_OVERRIDE_UX_UTILITY_MEMORY_ALLOCATE: + + memory_allocate_params = params; + if (memory_allocate_params->memory_alignment != this->memory_alignment || + memory_allocate_params->memory_cache_flag != this->memory_cache_flag || + memory_allocate_params->memory_size_requested != this->memory_size_requested) + { + act = 0; + } + break; + #endif + + case UX_TEST_OVERRIDE_TX_SEMAPHORE_GET: + + semaphore_get_params = params; + + if (this->semaphore_ptr != UX_NULL && semaphore_get_params->semaphore_ptr != this->semaphore_ptr || + semaphore_get_params->wait_option != this->wait_option) + { + act = 0; + } + break; + + case UX_TEST_OVERRIDE_ERROR_CALLBACK: + + error_callback_params = params; + if ((this->error_code && error_callback_params->error_code != this->error_code) || + (this->system_context && error_callback_params->system_context != this->system_context) || + (this->system_level && error_callback_params->system_level != this->system_level)) + { + act = 0; + } + break; + + case UX_TEST_OVERRIDE_UX_DEVICE_MEDIA_READ: + + device_media_read_write_flush_params = params; + if ((device_media_read_write_flush_params->lba != this->lba || + device_media_read_write_flush_params->lun != this->lun || + device_media_read_write_flush_params->number_blocks != this->number_blocks)) + { + act = 0; + } + break; + + case UX_TEST_OVERRIDE_UX_DEVICE_MEDIA_WRITE: + + device_media_read_write_flush_params = params; + if ((device_media_read_write_flush_params->lba != this->lba || + device_media_read_write_flush_params->lun != this->lun || + device_media_read_write_flush_params->number_blocks != this->number_blocks)) + { + act = 0; + } + break; + + case UX_TEST_OVERRIDE_UX_DEVICE_MEDIA_FLUSH: + + device_media_read_write_flush_params = params; + if ((device_media_read_write_flush_params->lba != this->lba || + device_media_read_write_flush_params->lun != this->lun || + device_media_read_write_flush_params->number_blocks != this->number_blocks)) + { + act = 0; + } + break; + + case UX_TEST_OVERRIDE_UX_DEVICE_MEDIA_STATUS: + + device_media_status_params = params; + if ((device_media_status_params->lun != this->lun || + device_media_status_params->media_id != this->media_id)) + { + act = 0; + } + break; + + case UX_TEST_OVERRIDE_NX_PACKET_POOL_CREATE: + + packet_pool_create_params = params; + + /* We just compare names. */ + + UX_TEST_ASSERT(this->name_ptr != UX_NULL); + + if (strcmp(packet_pool_create_params->name_ptr, this->name_ptr)) + { + act = 0; + } + + break; + + case UX_TEST_OVERRIDE_NX_PACKET_ALLOCATE: + + packet_allocate_params = params; + + /* We just compare names. */ + + UX_TEST_ASSERT(this->name_ptr != UX_NULL); + + if (strcmp(packet_allocate_params->pool_ptr->nx_packet_pool_name, this->name_ptr)) + { + act = 0; + } + + break; + + case UX_TEST_OVERRIDE_TX_THREAD_CREATE: + + thread_create_params = params; + + /* We just compare names. */ + + UX_TEST_ASSERT(this->name_ptr != UX_NULL); + + if (strcmp(thread_create_params->name_ptr, this->name_ptr)) + { + act = 0; + } + + break; + + case UX_TEST_OVERRIDE_TX_SEMAPHORE_CREATE: + + semaphore_create_params = params; + + /* We just compare names. */ + + UX_TEST_ASSERT(this->semaphore_name != UX_NULL); + + if (strcmp(semaphore_create_params->semaphore_name, this->semaphore_name)) + { + act = 0; + } + + break; + + case UX_TEST_OVERRIDE_TX_THREAD_PREEMPTION_CHANGE: + + thread_preemption_change_params = params; + if (this->thread_ptr != thread_preemption_change_params->thread_ptr || + this->new_threshold != thread_preemption_change_params->new_threshold) + { + act = 0; + } + break; + + case UX_TEST_OVERRIDE_UX_HOST_STACK_INTERFACE_SET: + + host_stack_interface_set_params = params; + if (this->interface->ux_interface_descriptor.bInterfaceNumber != host_stack_interface_set_params->interface->ux_interface_descriptor.bInterfaceNumber || + this->interface->ux_interface_descriptor.bAlternateSetting != host_stack_interface_set_params->interface->ux_interface_descriptor.bAlternateSetting) + { + act = 0; + } + break; + + case UX_TEST_OVERRIDE_TX_MUTEX_GET: + + mutex_get_params = params; + if (this->mutex_ptr && + this->mutex_ptr != mutex_get_params->mutex_ptr) + { + act = 0; + } + if (mutex_get_params->wait_option != this->wait_option) + { + act = 0; + } + break; + + case UX_TEST_OVERRIDE_TX_MUTEX_CREATE: + + mutex_create_params = params; + if (this->mutex_ptr && + this->mutex_ptr != mutex_create_params->mutex_ptr) + { + act = 0; + } + if (mutex_create_params->inherit != this->inherit) + { + act = 0; + } + if (this->name_ptr != UX_NULL) + { + str_len0 = 0; + _ux_utility_string_length_check(this->name_ptr, &str_len0, 2048); + if (mutex_create_params->name_ptr != UX_NULL) + { + str_len1 = 0; + _ux_utility_string_length_check(mutex_create_params->name_ptr, &str_len1, 2048); + } + else + { + str_len1 = 0; + } + if (str_len0 != str_len1) + { + act = 0; + } + if (str_len0 == str_len1 && + ux_utility_memory_compare(this->name_ptr, mutex_create_params->name_ptr, str_len0) != UX_SUCCESS) + { + act = 0; + } + } + break; + + case UX_TEST_OVERRIDE_TX_MUTEX_PUT: + + mutex_put_params = params; + if (this->mutex_ptr != UX_NULL && + this->mutex_ptr != mutex_put_params->mutex_ptr) + { + act = 0; + } + break; + } + } + + /* Non-param checks - these checks happen regardless of the "ignore_params" + value. */ + + if (this->thread_to_match) + { + this_thread = tx_thread_identify(); + if (this_thread != this->thread_to_match) + act = 0; + } + + if (this->check_func && this->check_func(params) != UX_TRUE) + { + act = 0; + } + } + } + + if (act) + { + + //stepinfo(" action matched; usbx_function: %d\n", usbx_function); + + if (usbx_function == UX_TEST_OVERRIDE_UX_HCD_SIM_HOST_ENTRY || + usbx_function == UX_TEST_OVERRIDE_UX_DCD_SIM_SLAVE_FUNCTION || + usbx_function == UX_TEST_OVERRIDE_UX_HOST_STACK_TRANSFER_REQUEST) + { + + /* Apply to port status */ + if (this -> port_action) + { + + UX_TEST_ASSERT(!this->no_return); + + ux_test_port_status = this -> port_status; + // printf("Port status -> %lx\n", port_status); + /* Signal change on the port (HCD0.RH.PORT0). */ + _ux_system_host -> ux_system_host_hcd_array -> ux_hcd_root_hub_signal[0] = 1; + /* Signal detach to host enum thread. */ + _ux_host_semaphore_put(&_ux_system_host -> ux_system_host_enum_semaphore); + } + + /* Apply to transfer */ + if (this -> req_action & UX_TEST_SIM_REQ_ANSWER) + { + + /* We should always return, otherwise the data we copy will just + get overridden by the actual transfer request. */ + UX_TEST_ASSERT(!this->no_return); + + /* Is there data to copy? */ + if (this->req_data) + { + /* Make sure we don't overflow. */ + min = generic_transfer_parameter.req_requested_len < this->req_actual_len ? generic_transfer_parameter.req_requested_len : this->req_actual_len; + + /* Copy the data. */ + _ux_utility_memory_copy(generic_transfer_parameter.req_data, this->req_data, min); + } + + /* Set actual length */ + if (this->req_actual_len == (~0)) + *req_actual_len = generic_transfer_parameter.req_requested_len; + else + *req_actual_len = this->req_actual_len; + + /* Set status code */ + if (usbx_function == UX_TEST_OVERRIDE_UX_HCD_SIM_HOST_ENTRY || + usbx_function == UX_TEST_OVERRIDE_UX_HOST_STACK_TRANSFER_REQUEST) + *req_code = this->req_status; + else + *slave_req_code = this->req_status; + } + } + + result = *this; + result.matched = 1; + + if (advance) + _ux_test_advance_actions(); + } + + return result; +} + +#define SLEEP_STEP 1 +UINT ux_test_breakable_sleep(ULONG tick, UINT (*sleep_break_check_callback)(VOID)) +{ + +UINT status; +ULONG t; + + while(tick) { + + /* Sleep for a while. */ + t = (tick > SLEEP_STEP) ? SLEEP_STEP : tick; +#if defined(UX_HOST_STANDALONE) || defined(UX_DEVICE_STANDALONE) + ux_system_tasks_run(); +#endif + tx_thread_sleep(t); + + /* Check if we want to break. */ + if (sleep_break_check_callback) + { + + status = sleep_break_check_callback(); + if (status != UX_SUCCESS) + { + + /* We break sleep loop! + Status is returned for use to check. */ + return status; + } + } + + /* Update remaining ticks. */ + tick -= t; + } + + /* Normal end. */ + return UX_SUCCESS; +} +UINT ux_test_sleep_break_if(ULONG tick, UINT (*check)(VOID), + UINT break_on_match_or_not, + UINT rc_to_check) +{ +ULONG t0, t1; +UINT status; + + t0 = tx_time_get(); + while(1) + { +#if defined(UX_HOST_STANDALONE) + ux_system_tasks_run(); + tx_thread_relinquish(); +#else + tx_thread_sleep(1); +#endif + if (check) + { + status = check(); + if (break_on_match_or_not) + { + /* If RC match expected, break. */ + if (status == rc_to_check) + break; + } + else + { + /* If RC not match expected, break. */ + if (status != rc_to_check) + break; + } + } + t1 = tx_time_get(); + if (_ux_utility_time_elapsed(t0, t1) >= tick) + { + return(UX_TIMEOUT); + } + } + return(UX_SUCCESS); +} + +/* Error callback */ + +UX_TEST_ERROR_CALLBACK_ERROR ux_error_hcd_transfer_stalled = { UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_HCD, UX_TRANSFER_STALLED }; + +VOID ux_test_ignore_all_errors() +{ + ignore_all_errors = UX_TRUE; +} + +VOID ux_test_unignore_all_errors() +{ + ignore_all_errors = UX_FALSE; +} + +void test_control_return(UINT status); +VOID ux_test_error_callback(UINT system_level, UINT system_context, UINT error_code) +{ + +UX_TEST_ERROR_CALLBACK_PARAMS params = { system_level, system_context, error_code }; +UX_TEST_ACTION action; + + + if (ignore_all_errors == UX_TRUE) + { + return; + } + + if (ux_test_do_hooks_before(UX_TEST_OVERRIDE_ERROR_CALLBACK, ¶ms)) + return; + + action = ux_test_action_handler(UX_TEST_OVERRIDE_ERROR_CALLBACK, ¶ms); + if (action.matched) + { + return; + } + + /* Failed test. Windows has some stupid printf bug where it stalls + everything if other threads are printing as well. Doesn't matter anyways, + since I use breakpoints for debugging this error. */ +#ifndef WIN32 + printf("%s:%d Unexpected error: 0x%x, 0x%x, 0x%x\n", __FILE__, __LINE__, system_level, system_context, error_code); +#endif + + if (exit_on_errors) + test_control_return(1); +} + +/* Utility methods. */ + +static ULONG ux_test_memory_test_no_device_memory_free_amount; + +void ux_test_memory_test_initialize() +{ + +UCHAR connected_when_started = 0; + + /* Are we connected? */ + if (_ux_system_host->ux_system_host_hcd_array[0].ux_hcd_nb_devices != 0) + { + /* Yes. */ + connected_when_started = 1; + + /* Then disconnect. */ + ux_test_disconnect_slave_and_host_wait_for_enum_completion(); + } + + /* Get normal amount of free memory when disconnected. This is to detect + memory leaks. */ + ux_test_memory_test_no_device_memory_free_amount = _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available; + ux_test_connect_slave_and_host_wait_for_enum_completion(); + + /* With basic free memory amount, do basic memory test. */ + ux_test_disconnect_slave_and_host_wait_for_enum_completion(); + ux_test_connect_slave_and_host_wait_for_enum_completion(); + + /* (Let's be nice to the user.) Hello user! :) */ + if (!connected_when_started) + { + ux_test_disconnect_slave_and_host_wait_for_enum_completion(); + } +} + +void ux_test_memory_test_check() +{ + + /* Has the memory check value not been initialized yet? */ + if (ux_test_memory_test_no_device_memory_free_amount == 0) + return; + UX_TEST_ASSERT(_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available == ux_test_memory_test_no_device_memory_free_amount); +} + +#if defined(UX_HOST_STANDALONE) +static UINT _host_enum_is_pending(void) +{ +UX_DEVICE *enum_device = _ux_system_host -> ux_system_host_enum_device; + /* Case 1 : there is nothing in enum list, nothing pending. */ + if (enum_device == UX_NULL) + return(UX_ERROR); + /* Case 2 : enum list is not NULL, check enum flags each device. */ + while(enum_device) + { + if (enum_device -> ux_device_flags & UX_DEVICE_FLAG_ENUM) + return(UX_SUCCESS); + enum_device = enum_device -> ux_device_enum_next; + } + return(UX_ERROR); +} +static UINT _host_rh_removal_is_pending(void) +{ +UX_HCD *hcd = &_ux_system_host -> ux_system_host_hcd_array[0]; + /* If device connected, pending. */ + if (hcd -> ux_hcd_rh_device_connection) + return(UX_SUCCESS); + return(UX_ERROR); +} +#endif + +/* Wait for the enum thread to finish whatever it's currently doing. Note that + this implies it's actually _doing_ something when this is called. + + Specifics: We do this by simply waiting for the the enum thread's semaphore's + suspended count to be non-zero. */ +VOID ux_test_wait_for_enum_thread_completion() +{ +#if !defined(UX_HOST_STANDALONE) + /* Is it actually running? */ + if (_ux_system_host -> ux_system_host_enum_semaphore.tx_semaphore_suspended_count == 0) + { + + /* Wait for enum thread to complete. */ + UX_TEST_CHECK_SUCCESS(ux_test_wait_for_value_uint(&_ux_system_host -> ux_system_host_enum_semaphore.tx_semaphore_suspended_count, 1)); + } +#else + { + UINT status; + if (ux_test_port_status & UX_PS_CCS) + { + /* Loop a while to confirm enumeration start. */ + status = ux_test_sleep_break_on_success(10, _host_enum_is_pending); + if (status == UX_SUCCESS) + { + /* Loop a while to confirm enumeration end. */ + status = ux_test_sleep_break_on_error(100, _host_enum_is_pending); + UX_ASSERT(status == UX_SUCCESS); + } + } + else + { + /* Loop a while to confirm device removal. */ + status = ux_test_sleep_break_on_error(100, _host_rh_removal_is_pending); + UX_ASSERT(status == UX_SUCCESS); + } + } +#endif +} + +VOID ux_test_disconnect_host_no_wait() +{ + + ux_test_hcd_sim_host_disconnect_no_wait(); +} + +VOID ux_test_disconnect_host_wait_for_enum_completion() +{ + + /* No wait because ux_test_wait_for_enum_completion() expects the enum thread + to be running when it's called. */ + ux_test_hcd_sim_host_disconnect_no_wait(); + ux_test_wait_for_enum_thread_completion(); + ux_test_memory_test_check(); +} + +/* This function should not be called from inside USBX because we wait for the + deactivation to finish. If we have a semaphore the deactivation routine requires, + then we have deadlock. */ +void ux_test_disconnect_slave_and_host_wait_for_enum_completion() +{ + +UX_HCD *hcd = &_ux_system_host->ux_system_host_hcd_array[0]; + + ux_test_dcd_sim_slave_disconnect(); + /* No wait because below. */ + ux_test_disconnect_host_wait_for_enum_completion(); + ux_test_memory_test_check(); +} + +VOID ux_test_disconnect_slave() +{ + + ux_test_dcd_sim_slave_disconnect(); +} + +VOID ux_test_connect_slave() +{ + + ux_test_dcd_sim_slave_connect(UX_FULL_SPEED_DEVICE); +} + +VOID ux_test_connect_host_wait_for_enum_completion() +{ + + /* No wait because ux_test_wait_for_enum_completion() expects the enum thread + to be running when it's called. */ + ux_test_hcd_sim_host_connect_no_wait(UX_FULL_SPEED_DEVICE); + ux_test_wait_for_enum_thread_completion(); +} + +VOID ux_test_connect_slave_and_host_wait_for_enum_completion() +{ + + ux_test_dcd_sim_slave_connect(UX_FULL_SPEED_DEVICE); + ux_test_connect_host_wait_for_enum_completion(); +} + +/* This is supposed to only change these parameters. In other words, the same + classes should still be registered after calling this. */ +VOID ux_test_change_device_parameters(UCHAR * device_framework_high_speed, ULONG device_framework_length_high_speed, + UCHAR * device_framework_full_speed, ULONG device_framework_length_full_speed, + UCHAR * string_framework, ULONG string_framework_length, + UCHAR * language_id_framework, ULONG language_id_framework_length, + UINT(*ux_system_slave_change_function)(ULONG)) +{ + +UX_SLAVE_CLASS *class; +UX_SLAVE_CLASS classes_copy[UX_MAX_SLAVE_CLASS_DRIVER]; +ULONG class_index; + + /* Disconnect device. */ + ux_test_dcd_sim_slave_disconnect(); + + /* First, save the classes. */ + memcpy(classes_copy, _ux_system_slave->ux_system_slave_class_array, sizeof(classes_copy)); + + /* Uninitialize classes. */ + for (class_index = 0; class_index < UX_SYSTEM_DEVICE_MAX_CLASS_GET(); class_index++) + { + + class = &_ux_system_slave->ux_system_slave_class_array[class_index]; + if (class->ux_slave_class_status == UX_USED) + UX_TEST_CHECK_SUCCESS(ux_device_stack_class_unregister((UCHAR *)class->ux_slave_class_name, class->ux_slave_class_entry_function)); + } + + /* Uninitialize the stack. */ + UX_TEST_CHECK_SUCCESS(ux_device_stack_uninitialize()); + + /* Now re-initialize stack with correct stuff. */ + UX_TEST_CHECK_SUCCESS(ux_device_stack_initialize(device_framework_high_speed, device_framework_length_high_speed, + device_framework_full_speed, device_framework_length_full_speed, + string_framework, string_framework_length, + language_id_framework, language_id_framework_length, + ux_system_slave_change_function)); + + /* Now re-register the classes. */ + for (class_index = 0; class_index < UX_SYSTEM_DEVICE_MAX_CLASS_GET(); class_index++) + { + + class = &classes_copy[class_index]; + if (class->ux_slave_class_status == UX_USED) + UX_TEST_CHECK_SUCCESS(ux_device_stack_class_register((UCHAR *)class->ux_slave_class_name, + class->ux_slave_class_entry_function, + class->ux_slave_class_configuration_number, + class->ux_slave_class_interface_number, + class->ux_slave_class_interface_parameter)); + } +} + +UINT ux_test_host_stack_class_instance_get(UX_HOST_CLASS *host_class, UINT class_index, VOID **class_instance) +{ + +UINT status; +UINT timeout_ms = UX_TEST_TIMEOUT_MS; +#if defined(UX_HOST_STANDALONE) +ULONG t0, t, elapsed; + t0 = ux_utility_time_get(); +#endif + + do + { + + status = ux_host_stack_class_instance_get(host_class, class_index, class_instance); +#if defined(UX_HOST_STANDALONE) + ux_system_tasks_run(); + tx_thread_relinquish(); + t = ux_utility_time_get(); + elapsed = ux_utility_time_elapsed(t0, t); + if (elapsed >= UX_MS_TO_TICK_NON_ZERO(timeout_ms)) + return(UX_ERROR); +#else + tx_thread_sleep(MS_TO_TICK(10)); + timeout_ms -= 10; +#endif + + } while (status != UX_SUCCESS && timeout_ms); + + if (timeout_ms == 0) + return UX_ERROR; + + return UX_SUCCESS; +} + +UINT ux_test_wait_for_value_uint(UINT *current_value_ptr, UINT desired_value) +{ + +UINT timeout_ms = UX_TEST_TIMEOUT_MS; +#if defined(UX_HOST_STANDALONE) +ULONG t0, t, elapsed; + t0 = ux_utility_time_get(); +#endif + + while (*current_value_ptr != desired_value && timeout_ms) + { + +#if defined(UX_HOST_STANDALONE) + ux_system_tasks_run(); + tx_thread_relinquish(); + t = ux_utility_time_get(); + elapsed = ux_utility_time_elapsed(t0, t); + if (elapsed >= UX_MS_TO_TICK_NON_ZERO(timeout_ms)) + return(UX_ERROR); +#else + tx_thread_sleep(MS_TO_TICK(10)); + timeout_ms -= 10; +#endif + } + + if (timeout_ms == 0) + return UX_ERROR; + + return UX_SUCCESS; +} + +UINT ux_test_wait_for_value_ulong(ULONG *current_value_ptr, ULONG desired_value) +{ + +UINT timeout_ms = UX_TEST_TIMEOUT_MS; +#if defined(UX_HOST_STANDALONE) +ULONG t0, t, elapsed; + t0 = ux_utility_time_get(); +#endif + + while (*current_value_ptr != desired_value && timeout_ms) + { + +#if defined(UX_HOST_STANDALONE) + ux_system_tasks_run(); + tx_thread_relinquish(); + t = ux_utility_time_get(); + elapsed = ux_utility_time_elapsed(t0, t); + if (elapsed >= UX_MS_TO_TICK_NON_ZERO(timeout_ms)) + return(UX_ERROR); +#else + tx_thread_sleep(MS_TO_TICK(10)); + timeout_ms -= 10; +#endif + } + + if (timeout_ms == 0) + return UX_ERROR; + + return UX_SUCCESS; +} + +UINT ux_test_wait_for_value_uchar(UCHAR *current_value_ptr, UCHAR desired_value) +{ + +UINT timeout_ms = UX_TEST_TIMEOUT_MS; +#if defined(UX_HOST_STANDALONE) +ULONG t0, t, elapsed; + t0 = ux_utility_time_get(); +#endif + + while (*current_value_ptr != desired_value && timeout_ms) + { + +#if defined(UX_HOST_STANDALONE) + ux_system_tasks_run(); + tx_thread_relinquish(); + t = ux_utility_time_get(); + elapsed = ux_utility_time_elapsed(t0, t); + if (elapsed >= UX_MS_TO_TICK_NON_ZERO(timeout_ms)) + return(UX_ERROR); +#else + tx_thread_sleep(MS_TO_TICK(10)); + timeout_ms -= 10; +#endif + } + + if (timeout_ms == 0) + return UX_ERROR; + + return UX_SUCCESS; +} + +UINT ux_test_wait_for_non_null(VOID **current_value_ptr) +{ + +UINT timeout_ms = UX_TEST_TIMEOUT_MS; +#if defined(UX_HOST_STANDALONE) +ULONG t0, t, elapsed; + t0 = ux_utility_time_get(); +#endif + + while (*current_value_ptr == UX_NULL && timeout_ms) + { + +#if defined(UX_HOST_STANDALONE) + ux_system_tasks_run(); + tx_thread_relinquish(); + t = ux_utility_time_get(); + elapsed = ux_utility_time_elapsed(t0, t); + if (elapsed >= UX_MS_TO_TICK_NON_ZERO(timeout_ms)) + return(UX_ERROR); +#else + tx_thread_sleep(MS_TO_TICK(10)); + timeout_ms -= 10; +#endif + } + + if (timeout_ms == 0) + return UX_ERROR; + + return UX_SUCCESS; +} + +UINT ux_test_wait_for_null_wait_time(VOID **current_value_ptr, UINT wait_time_ms) +{ + +UINT timeout_ms = wait_time_ms; +#if defined(UX_HOST_STANDALONE) +ULONG t0, t, elapsed; + t0 = ux_utility_time_get(); +#endif + + while (*current_value_ptr != UX_NULL && timeout_ms) + { + +#if defined(UX_HOST_STANDALONE) + ux_system_tasks_run(); + tx_thread_relinquish(); + t = ux_utility_time_get(); + elapsed = ux_utility_time_elapsed(t0, t); + if (elapsed >= UX_MS_TO_TICK_NON_ZERO(timeout_ms)) + return(UX_ERROR); +#else + tx_thread_sleep(MS_TO_TICK(10)); + timeout_ms -= 10; +#endif + } + + if (*current_value_ptr == UX_NULL) + return(UX_SUCCESS); + + return UX_ERROR; +} + +UINT ux_test_wait_for_null(VOID **current_value_ptr) +{ + return ux_test_wait_for_null_wait_time(current_value_ptr, UX_TEST_TIMEOUT_MS); +} + +char *ux_test_file_base_name(char *path, int n) +{ + char *ptr = path, *slash = path; + int i; + for (i = 0; i < n; i ++) + { + if (*ptr == 0) + break; + if (*ptr == '\\' || *ptr == '/') + slash = ptr + 1; + ptr ++; + } + return(slash); +} + +void ux_test_assert_hit_hint(UCHAR on_off) +{ + ux_test_assert_hit_hint_off = !on_off; +} +void ux_test_assert_hit_exit(UCHAR on_off) +{ + ux_test_assert_hit_exit_off = !on_off; +} +ULONG ux_test_assert_hit_count_get(void) +{ + return ux_test_assert_hit_count; +} +void ux_test_assert_hit_count_reset(void) +{ + ux_test_assert_hit_count = 0; +} +void ux_test_assert_hit(char* file, INT line) +{ + ux_test_assert_hit_count ++; + if (!ux_test_assert_hit_hint_off) + printf("%s:%d Assert HIT!\n", file, line); + if (!ux_test_assert_hit_exit_off) + test_control_return(ux_test_assert_hit_exit_code); +} + +UINT ux_test_host_endpoint_write(UX_ENDPOINT *endpoint, UCHAR *buffer, ULONG length, ULONG *actual_length) +{ +UINT status; +UX_TRANSFER *transfer = &endpoint->ux_endpoint_transfer_request; + transfer->ux_transfer_request_data_pointer = buffer; + transfer->ux_transfer_request_requested_length = length; + status = ux_host_stack_transfer_request(transfer); +#if !defined(UX_HOST_STANDALONE) + if (status == UX_SUCCESS) + { + status = tx_semaphore_get(&transfer->ux_transfer_request_semaphore, + transfer->ux_transfer_request_timeout_value); + if (status != TX_SUCCESS) + { + ux_host_stack_transfer_request_abort(transfer); + status = UX_TRANSFER_TIMEOUT; + } + } +#endif + if (actual_length) + *actual_length = transfer->ux_transfer_request_actual_length; + return(status); +} diff --git a/test/regression_samples_rtos/ux_test.h b/test/regression_samples_rtos/ux_test.h new file mode 100644 index 000000000..a496aa231 --- /dev/null +++ b/test/regression_samples_rtos/ux_test.h @@ -0,0 +1,540 @@ +/* USBX common things for testing. */ + +#ifndef _UX_TEST_H +#define _UX_TEST_H + +#include "nx_api.h" +#include "ux_api.h" + +#if 0 +#define stepinfo printf +#else +#define stepinfo(...) +#endif + + +/* Compile options check. */ +/* +UX_MAX_HCD=1 +UX_MAX_CLASS_DRIVER=1 +UX_MAX_DEVICES=1 +UX_DEVICE_INITIALIZE_FRAMEWORK_SCAN_DISABLE +UX_MAX_DEVICE_ENDPOINTS=2 +UX_MAX_DEVICE_INTERFACES=1 +UX_MAX_SLAVE_INTERFACES=1 +UX_MAX_SLAVE_CLASS_DRIVER=1 +UX_MAX_SLAVE_LUN=1 +*/ +#define UX_TEST_MULTI_HCD_ON (UX_MAX_HCD > 1) +#define UX_TEST_MULTI_IFC_OVER(n) ( \ + (UX_MAX_SLAVE_INTERFACES > (n)) && \ + (!defined(UX_DEVICE_INITIALIZE_FRAMEWORK_SCAN_DISABLE) || \ + (UX_MAX_DEVICE_INTERFACES > (n))) ) +#define UX_TEST_MULTI_IFC_ON UX_TEST_MULTI_IFC_OVER(1) +#define UX_TEST_MULTI_ALT_ON ( \ + !defined(UX_DEVICE_ALTERNATE_SETTING_SUPPORT_DISABLE)) +#define UX_TEST_MULTI_CLS_OVER(n) ( \ + UX_MAX_CLASS_DRIVER > (n) && \ + UX_MAX_SLAVE_CLASS_DRIVER > (n) ) +#define UX_TEST_MULTI_CLS_ON UX_TEST_MULTI_CLS_OVER(1) +#define UX_TEST_MULTI_DEV_ON (UX_MAX_DEVICES > 1) +#define UX_TEST_MULTI_EP_OVER(n) ( \ + (UX_MAX_DEVICE_ENDPOINTS > (n)) || \ + (!defined(UX_DEVICE_INITIALIZE_FRAMEWORK_SCAN_DISABLE))) + +/* Define macros. */ +#define ARRAY_COUNT(array) (sizeof(array)/sizeof(array[0])) + +extern char *ux_test_file_base_name(char *path, int n); +#define UX_TEST_FILE_BASE_NAME(s) (ux_test_file_base_name(s, sizeof(s))) +#define UX_TEST_FILE UX_TEST_FILE_BASE_NAME(__FILE__) + +#define UX_TEST_ASSERT(expression) if (!(expression)) { printf("%s:%d Assert failed\n", UX_TEST_FILE, __LINE__); test_control_return(1); } +#define UX_TEST_ASSERT_MESSAGE(expression, ...) if (!(expression)) { printf("%s:%d Assert failed; ", UX_TEST_FILE, __LINE__); printf(__VA_ARGS__); test_control_return(1); } +#define UX_TEST_CHECK_CODE(code, expression) \ + { \ + UINT __status__ = (expression); \ + if (__status__ != (code)) \ + { \ + printf("%s : %d, error: 0x%x\n", UX_TEST_FILE_BASE_NAME(__FILE__), __LINE__, __status__); \ + test_control_return(1); \ + } \ + } +#define UX_TEST_CHECK_NOT_CODE(code, expression) \ + { \ + UINT __status__ = (expression); \ + if (__status__ == (code)) \ + { \ + printf("%s:%d error: 0x%x\n", UX_TEST_FILE, __LINE__, __status__); \ + test_control_return(1); \ + } \ + } +#define UX_TEST_CHECK_SUCCESS(expression) UX_TEST_CHECK_CODE(UX_SUCCESS, expression) +#define UX_TEST_CHECK_NOT_SUCCESS(expression) UX_TEST_CHECK_NOT_CODE(UX_SUCCESS, expression) + +typedef struct UX_TEST_ERROR_CALLBACK_ERROR_STRUCT +{ + UINT system_level; + UINT system_context; + UINT error_code; + + struct UX_TEST_ERROR_CALLBACK_ERROR_STRUCT *next; +} UX_TEST_ERROR_CALLBACK_ERROR; + +typedef struct UX_TEST_ERROR_CALLBACK_ACTION_STRUCT +{ + UX_TEST_ERROR_CALLBACK_ERROR *error; + VOID (*entry_func_error_callback_action)(); + UINT do_return; + + struct UX_TEST_ERROR_CALLBACK_ACTION *next; +} UX_TEST_ERROR_CALLBACK_ACTION; + +/* General setup request */ + +typedef struct _UX_TEST_SETUP_STRUCT { + UCHAR ux_test_setup_type; + UCHAR ux_test_setup_request; + USHORT ux_test_setup_value; + USHORT ux_test_setup_index; +} UX_TEST_SETUP; + +/* Interaction descriptor to insert action on specific entry function call. */ +/* function, request to match, port action, port status, request action, request EP, request data, request actual length, request status, status, additional callback, no return, thread */ + +typedef struct UX_TEST_ACTION_STRUCT { + + /* Function code to match */ + ULONG function; + /* Setup request to match */ + UX_TEST_SETUP *req_setup; + + /* Port action flags */ + ULONG port_action; + ULONG port_status; + + /* Request action flags */ + ULONG req_action; + ULONG req_ep_address; + UCHAR *req_data; + /* For comparing data, use this, not req_requested_len. */ + ULONG req_actual_len; + ULONG req_status; + + /* Status to return */ + ULONG status; + + /* Additional callback */ + VOID (*action_func)(struct UX_TEST_ACTION_STRUCT *action, VOID *params); + + /* No return */ + ULONG no_return; + + ULONG req_requested_len; + + /* _ux_host_stack_class_instance_create */ + UCHAR *class_name; + UINT class_name_length; + + /* _ux_device_stack_transfer_request */ + UCHAR endpoint_address; + ULONG slave_length; + ULONG host_length; + + /* ux_host_stack_transfer_request_action */ + ULONG requested_length; + UINT type; + UINT value; + UINT index; + UCHAR *data; + /* For matching parts of a transfer. */ + UCHAR *req_data_match_mask; + + /* _ux_device_stack_transfer_abort (none) */ + + /* _ux_host_class_storage_media_write */ + ULONG sector_start; + ULONG sector_count; + UCHAR *data_pointer; + + /* _ux_host_stack_interface_set */ + ULONG bInterfaceNumber; + ULONG bAlternateSetting; + + /* _ux_utility_memory_allocate */ + ULONG memory_alignment; + ULONG memory_cache_flag; + ULONG memory_size_requested; + + /* if there is name. */ + CHAR *name_ptr; + + /* tx_semaphore_get */ + TX_SEMAPHORE *semaphore_ptr; + ULONG wait_option; + + /* tx_semaphore_get */ + TX_MUTEX *mutex_ptr; + UINT inherit; + + /* tx_semaphore_create */ + CHAR *semaphore_name; + + /* ux_test_error_callback */ + UINT system_level; + UINT system_context; + UINT error_code; + + /* ux_slave_class_storage_media_read/write */ + VOID *storage; + ULONG lun; + USHORT number_blocks; + ULONG lba; + ULONG *media_status; + + /* ux_slave_class_storage_media_status */ + ULONG media_id; + + /* tx_thread_preemption_change */ + TX_THREAD *thread_ptr; + UINT new_threshold; + + /* _ux_host_stack_interface_set */ + UX_INTERFACE *interface; + + /* Thread to match. Necessary due to the following case: The storage class has a background thread that runs every 2 + seconds and performs a bulk transaction. What could happen is that we call ux_test_hcd_sim_host_set_actions and + get preempted before actually starting the test. This could cause the action to match a transaction made by + storage's background thread. */ + TX_THREAD *thread_to_match; + + /* Whether we invoke our callback before or after we call the real HCD. */ + UCHAR do_after; + + /* User data. */ + ALIGN_TYPE user_data; + + /* Additional check. */ + UCHAR (*check_func)(); + + /* If set, the params are ignored. */ + UCHAR ignore_params; + + struct UX_TEST_ACTION_STRUCT *next; + struct UX_TEST_ACTION_STRUCT *created_list_next; + UCHAR matched; + ULONG usbx_function; +} UX_TEST_ACTION; + +typedef UX_TEST_ACTION UX_TEST_SIM_ENTRY_ACTION; + +typedef enum UX_TEST_FUNCTIONS +{ + UX_TEST_NULL, + UX_TEST_OVERRIDE_TX_SEMAPHORE_GET, + UX_TEST_OVERRIDE_TX_SEMAPHORE_CREATE, + UX_TEST_OVERRIDE_TX_THREAD_PREEMPTION_CHANGE, + UX_TEST_OVERRIDE_TX_THREAD_CREATE, + UX_TEST_OVERRIDE_NX_PACKET_POOL_CREATE, + UX_TEST_OVERRIDE_NX_PACKET_ALLOCATE, + UX_TEST_OVERRIDE_TX_MUTEX_CREATE, + UX_TEST_OVERRIDE_TX_MUTEX_PUT, + UX_TEST_OVERRIDE_TX_MUTEX_GET, + UX_TEST_OVERRIDE_UX_HCD_SIM_HOST_ENTRY, + UX_TEST_OVERRIDE_UX_DCD_SIM_SLAVE_FUNCTION, + UX_TEST_OVERRIDE_UX_UTILITY_MEMORY_ALLOCATE, + + /* Define functions where it's okay to return/change the logic. This should + only include user callbacks. */ + UX_TEST_OVERRIDE_USER_CALLBACKS, + UX_TEST_OVERRIDE_ERROR_CALLBACK, + UX_TEST_OVERRIDE_UX_DEVICE_MEDIA_READ, + UX_TEST_OVERRIDE_UX_DEVICE_MEDIA_WRITE, + UX_TEST_OVERRIDE_UX_DEVICE_MEDIA_FLUSH, + UX_TEST_OVERRIDE_UX_DEVICE_MEDIA_STATUS, + + /* Only for race condition tests. */ + UX_TEST_OVERRIDE_RACE_CONDITION_OVERRIDES, + UX_TEST_OVERRIDE_UX_HOST_STACK_TRANSFER_REQUEST, + UX_TEST_OVERRIDE_UX_HOST_STACK_INTERFACE_SET, + + UX_TEST_NUMBER_OVERRIDES, +} UX_TEST_FUNCTION; + +typedef struct UX_TEST_OVERRIDE_NX_PACKET_ALLOCATE_PARAMS +{ + NX_PACKET_POOL *pool_ptr; +} UX_TEST_OVERRIDE_NX_PACKET_ALLOCATE_PARAMS; + +typedef struct UX_TEST_OVERRIDE_NX_PACKET_POOL_CREATE_PARAMS +{ + CHAR *name_ptr; +} UX_TEST_OVERRIDE_NX_PACKET_POOL_CREATE_PARAMS; + +typedef struct UX_TEST_OVERRIDE_UX_DEVICE_MEDIA_READ_WRITE_PARAMS +{ + VOID *storage; + ULONG lun; + UCHAR *data_pointer; + ULONG number_blocks; + ULONG lba; + ULONG *media_status; +} UX_TEST_OVERRIDE_UX_DEVICE_MEDIA_READ_WRITE_FLUSH_PARAMS; + +typedef struct UX_TEST_OVERRIDE_UX_DEVICE_MEDIA_STATUS_PARAMS +{ + VOID *storage; + ULONG lun; + ULONG media_id; + ULONG *media_status; +} UX_TEST_OVERRIDE_UX_DEVICE_MEDIA_STATUS_PARAMS; + +typedef struct UX_TEST_OVERRIDE_UX_UTILITY_MEMORY_ALLOCATE_PARAMS +{ + ULONG memory_alignment; + ULONG memory_cache_flag; + ULONG memory_size_requested; +} UX_TEST_OVERRIDE_UX_UTILITY_MEMORY_ALLOCATE_PARAMS; + +typedef struct UX_TEST_OVERRIDE_UX_DCD_SIM_SLAVE_FUNCTION_PARAMS +{ + UX_SLAVE_DCD *dcd; + UINT function; + VOID *parameter; +} UX_TEST_OVERRIDE_UX_DCD_SIM_SLAVE_FUNCTION_PARAMS; + +typedef struct UX_TEST_OVERRIDE_UX_HCD_SIM_HOST_ENTRY_PARAMS +{ + UX_HCD *hcd; + UINT function; + VOID *parameter; +} UX_TEST_OVERRIDE_UX_HCD_SIM_HOST_ENTRY_PARAMS; + +typedef struct UX_TEST_OVERRIDE_TX_SEMAPHORE_GET_PARAMS +{ + TX_SEMAPHORE *semaphore_ptr; + ULONG wait_option; +} UX_TEST_OVERRIDE_TX_SEMAPHORE_GET_PARAMS; + +typedef struct UX_TEST_OVERRIDE_TX_MUTEX_CREATE_PARAMS +{ + TX_MUTEX *mutex_ptr; + CHAR *name_ptr; + UINT inherit; +} UX_TEST_OVERRIDE_TX_MUTEX_CREATE_PARAMS; + +typedef struct UX_TEST_OVERRIDE_TX_MUTEX_GET_PARAMS +{ + TX_MUTEX *mutex_ptr; + ULONG wait_option; +} UX_TEST_OVERRIDE_TX_MUTEX_GET_PARAMS; + +typedef struct UX_TEST_OVERRIDE_TX_MUTEX_PUT_PARAMS +{ + TX_MUTEX *mutex_ptr; +} UX_TEST_OVERRIDE_TX_MUTEX_PUT_PARAMS; + +typedef struct UX_TEST_OVERRIDE_TX_SEMAPHORE_CREATE_PARAMS +{ + TX_SEMAPHORE *semaphore; + CHAR *semaphore_name; + UINT initial_count; +} UX_TEST_OVERRIDE_TX_SEMAPHORE_CREATE_PARAMS; + +typedef struct UX_TEST_OVERRIDE_TX_THREAD_CREATE_PARAMS +{ + /* We only care about the name. */ + CHAR *name_ptr; +} UX_TEST_OVERRIDE_TX_THREAD_CREATE_PARAMS; + +typedef struct UX_TEST_OVERRIDE_UX_HOST_STACK_TRANSFER_REQUEST_PARAMS +{ + UX_TRANSFER *transfer_request; +} UX_TEST_OVERRIDE_UX_HOST_STACK_TRANSFER_REQUEST_PARAMS; + +typedef struct UX_TEST_OVERRIDE_TX_THREAD_PREEMPTION_CHANGE_PARAMS +{ + TX_THREAD *thread_ptr; + UINT new_threshold; +} UX_TEST_OVERRIDE_TX_THREAD_PREEMPTION_CHANGE_PARAMS; + +typedef struct UX_TEST_OVERRIDE_UX_HOST_STACK_INTERFACE_SET_PARAMS +{ + UX_INTERFACE *interface; +} UX_TEST_OVERRIDE_UX_HOST_STACK_INTERFACE_SET_PARAMS; + +typedef struct UX_TEST_OVERRIDE_NX_PACKET_TRANSMIT_RELEASE_PARAMS +{ + NX_PACKET **packet_ptr_ptr; +} UX_TEST_OVERRIDE_NX_PACKET_TRANSMIT_RELEASE_PARAMS; + +typedef struct UX_TEST_ERROR_CALLBACK_PARAMS +{ + UINT system_level; + UINT system_context; + UINT error_code; +} UX_TEST_ERROR_CALLBACK_PARAMS; + +typedef struct UX_TEST_GENERIC_CD +{ + VOID *controller_driver; + UINT function; + VOID *parameter; +} UX_TEST_GENERIC_CD; + +typedef struct UX_MEMORY_BLOCK_STRUCT +{ + struct UX_MEMORY_BLOCK_STRUCT *ux_memory_block_next; + UX_MEMORY_BYTE_POOL *ux_memory_byte_pool; +} UX_MEMORY_BLOCK; + +UINT ux_test_list_action_compare(UX_TEST_ACTION *list_item, UX_TEST_ACTION *action); + +VOID ux_test_remove_hook(UX_TEST_ACTION *action); +VOID ux_test_remove_hooks_from_array(UX_TEST_ACTION *actions); +VOID ux_test_remove_hooks_from_list(UX_TEST_ACTION *actions); + +UINT ux_test_link_hook(UX_TEST_ACTION *action); +VOID ux_test_link_hooks_from_array(UX_TEST_ACTION *actions); +VOID ux_test_link_hooks_from_list(UX_TEST_ACTION *actions); + +UINT ux_test_add_hook(UX_TEST_ACTION action); +VOID ux_test_add_hooks_from_array(UX_TEST_ACTION *actions); +VOID ux_test_add_hooks_from_list(UX_TEST_ACTION *actions); + +ULONG ux_test_do_hooks_before(UX_TEST_FUNCTION usbx_function, VOID *params); +VOID ux_test_do_hooks_after(UX_TEST_FUNCTION usbx_function, VOID *params); + +VOID ux_test_free_hook_actions(); + +VOID ux_test_free_user_list_actions(); +VOID ux_test_set_main_action_list_from_array(UX_TEST_ACTION *action); +VOID ux_test_set_main_action_list_from_list(UX_TEST_ACTION *new_actions); +VOID ux_test_add_action_to_user_list(UX_TEST_ACTION *list, UX_TEST_ACTION new_action); +VOID ux_test_add_action_to_main_list(UX_TEST_ACTION action); +VOID ux_test_add_action_to_main_list_multiple(UX_TEST_ACTION action, UINT); +VOID ux_test_hcd_sim_host_set_actions(UX_TEST_ACTION *action); +VOID ux_test_dcd_sim_slave_set_actions(UX_TEST_ACTION *action); +UX_TEST_ACTION ux_test_action_handler(UX_TEST_FUNCTION usbx_function, void *_params); +VOID ux_test_do_action_before(UX_TEST_ACTION *action, VOID *params); +VOID ux_test_do_action_after(UX_TEST_ACTION *action, VOID *params); +VOID ux_test_ignore_all_errors(); +VOID ux_test_unignore_all_errors(); +VOID ux_test_clear_main_list_actions(); +VOID ux_test_cleanup_everything(VOID); +VOID ux_test_turn_off_expedient(UCHAR *); +VOID ux_test_turn_on_expedient(UCHAR *); +UCHAR ux_test_is_expedient_on(); +VOID ux_test_set_expedient(UCHAR); +ULONG ux_test_calc_total_memory_allocated(ULONG memory_alignment, ULONG memory_cache_flag, ULONG memory_size_requested); +UCHAR ux_test_check_actions_empty(); +UINT ux_test_wait_for_empty_actions(); +UINT ux_test_get_num_actions_left(); +extern UX_TEST_ERROR_CALLBACK_ERROR ux_error_hcd_transfer_stalled; +VOID ux_test_error_callback(UINT system_level, UINT system_context, UINT error_code); +void ux_test_disconnect_slave_and_host_wait_for_enum_completion(); +VOID ux_test_connect_slave_and_host_wait_for_enum_completion(); +void ux_test_memory_test_initialize(); +void ux_test_memory_test_check(); +void ux_test_disconnect_slave_and_host_wait_for_enum_completion(); +VOID ux_test_wait_for_enum_thread_completion(); +UINT ux_test_host_stack_class_instance_get(UX_HOST_CLASS *host_class, UINT class_index, VOID **class_instance); +UINT ux_test_wait_for_value_uint(UINT *current_value, UINT desired_value); +UINT ux_test_wait_for_value_ulong(ULONG *current_value, ULONG desired_value); +UINT ux_test_wait_for_value_uchar(UCHAR *current_value_ptr, UCHAR desired_value); +UINT ux_test_wait_for_non_null(VOID **current_value_ptr); +UINT ux_test_wait_for_null(VOID **current_value_ptr); +VOID ux_test_connect_host_wait_for_enum_completion(); +VOID ux_test_disconnect_host_no_wait(); +VOID ux_test_disconnect_slave(); +VOID ux_test_connect_slave(); +VOID ux_test_disconnect_host_wait_for_enum_completion(); +VOID ux_test_change_device_parameters(UCHAR * device_framework_high_speed, ULONG device_framework_length_high_speed, + UCHAR * device_framework_full_speed, ULONG device_framework_length_full_speed, + UCHAR * string_framework, ULONG string_framework_length, + UCHAR * language_id_framework, ULONG language_id_framework_length, + UINT(*ux_system_slave_change_function)(ULONG)); +UINT ux_test_wait_for_empty_actions_wait_time(UINT wait_time_ms); +UINT ux_test_wait_for_null_wait_time(VOID **current_value_ptr, UINT wait_time_ms); + +VOID _ux_test_main_action_list_thread_update(TX_THREAD *old, TX_THREAD *new); +VOID _ux_test_main_action_list_semaphore_update(TX_SEMAPHORE *old, TX_SEMAPHORE *new); +VOID _ux_test_main_action_list_mutex_update(TX_MUTEX *old, TX_MUTEX *new); + +UINT ux_test_host_endpoint_write(UX_ENDPOINT *endpoint, UCHAR *buffer, ULONG length, ULONG *actual_length); + +/* Action flags */ + +#define UX_TEST_SIM_REQ_ANSWER 0x80 + +#define UX_TEST_MATCH_EP 0x01 +#define UX_TEST_MATCH_REQ_LEN 0x40 + +#define UX_TEST_SETUP_MATCH_REQUEST 0x02 +#define UX_TEST_SETUP_MATCH_VALUE 0x04 +#define UX_TEST_SETUP_MATCH_INDEX 0x08 + +#define UX_TEST_SETUP_MATCH_REQ (UX_TEST_SETUP_MATCH_REQUEST) +#define UX_TEST_SETUP_MATCH_REQ_V (UX_TEST_SETUP_MATCH_REQ | UX_TEST_SETUP_MATCH_VALUE) +#define UX_TEST_SETUP_MATCH_REQ_V_I (UX_TEST_SETUP_MATCH_REQ_V | UX_TEST_SETUP_MATCH_INDEX) + +#define UX_TEST_SETUP_MATCH_EP_REQ (UX_TEST_SETUP_MATCH_REQUEST | UX_TEST_MATCH_EP) +#define UX_TEST_SETUP_MATCH_EP_REQ_V (UX_TEST_SETUP_MATCH_EP_REQ | UX_TEST_SETUP_MATCH_VALUE) +#define UX_TEST_SETUP_MATCH_EP_REQ_V_I (UX_TEST_SETUP_MATCH_EP_REQ_V | UX_TEST_SETUP_MATCH_INDEX) + +/* Expected Requests */ + +#define UX_TEST_SETUP_SetAddress {0x00,0x05,0x0001,0x0000} +#define UX_TEST_SETUP_GetDevDescr {0x80,0x06,0x0100,0x0000} +#define UX_TEST_SETUP_GetCfgDescr {0x80,0x06,0x0200,0x0000} +#define UX_TEST_SETUP_SetConfigure {0x00,0x09,0x0001,0x0000} + +#define UX_TEST_SETUP_CDCACM_GetLineCoding {0xa1, 0x21, 0x0000, 0x0000} +#define UX_TEST_SETUP_CDCACM_SetLineCoding {0x21, 0x20, 0x0000, 0x0000} +#define UX_TEST_SETUP_CDCACM_SetLineState {0x21, 0x22, 0x0003, 0x0000} + +#define UX_TEST_SETUP_STORAGE_GetMaxLun {0xa1, 0xfe, 0x0000, 0x0001} +#define UX_TEST_SETUP_STORAGE_Reset {0x21, 0xff, 0x0000, 0x0000} + +#define UX_TEST_PORT_STATUS_DISC 0 +#define UX_TEST_PORT_STATUS_LS_CONN UX_PS_CCS | UX_PS_DS_LS +#define UX_TEST_PORT_STATUS_FS_CONN UX_PS_CCS | UX_PS_DS_FS +#define UX_TEST_PORT_STATUS_HS_CONN UX_PS_CCS | UX_PS_DS_HS + + + +UINT ux_test_breakable_sleep(ULONG tick, UINT (*sleep_break_check_callback)(VOID)); +UINT ux_test_sleep_break_if(ULONG tick, UINT (*check)(VOID), UINT break_on_match_or_not, UINT rc_to_match); +#define ux_test_sleep(t) ux_test_sleep_break_if(t, UX_NULL, 1, UX_SUCCESS) +#define ux_test_sleep_break_on_success(t,c) ux_test_sleep_break_if(t, c, 1, UX_SUCCESS) +#define ux_test_sleep_break_on_error(t,c) ux_test_sleep_break_if(t, c, 0, UX_SUCCESS) + +void test_control_return(UINT status); + +void ux_test_assert_hit_hint(UCHAR on_off); +void ux_test_assert_hit_exit(UCHAR on_off); +ULONG ux_test_assert_hit_count_get(void); +void ux_test_assert_hit_count_reset(void); + +static inline int ux_test_memory_is_freed(void *memory) +{ +UCHAR *work_ptr; +UCHAR *temp_ptr; +ALIGN_TYPE *free_ptr; +int is_free = 0; + UX_TEST_ASSERT(memory); + _ux_system_mutex_on(&_ux_system -> ux_system_mutex); + work_ptr = UX_VOID_TO_UCHAR_POINTER_CONVERT(memory); + { + work_ptr = UX_UCHAR_POINTER_SUB(work_ptr, UX_MEMORY_BLOCK_HEADER_SIZE); + temp_ptr = UX_UCHAR_POINTER_ADD(work_ptr, (sizeof(UCHAR *))); + free_ptr = UX_UCHAR_TO_ALIGN_TYPE_POINTER_CONVERT(temp_ptr); + if ((*free_ptr) == UX_BYTE_BLOCK_FREE) + is_free = 1; + } + _ux_system_mutex_off(&_ux_system -> ux_system_mutex); + return(is_free); +} +#define ux_test_regular_memory_free() _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available + +#endif /* _UX_TEST_H */ diff --git a/test/regression_samples_rtos/ux_test_actions.h b/test/regression_samples_rtos/ux_test_actions.h new file mode 100644 index 000000000..bbe0f0670 --- /dev/null +++ b/test/regression_samples_rtos/ux_test_actions.h @@ -0,0 +1,21 @@ +#ifndef UX_TEST_ACTIONS_H +#define UX_TEST_ACTIONS_H + +#include "ux_test.h" + +static UX_TEST_ACTION create_error_match_action(UINT system_level, UINT system_context, UINT error_code) +{ + +UX_TEST_ACTION action = { 0 }; + + + action.usbx_function = UX_TEST_OVERRIDE_ERROR_CALLBACK; + action.system_level = system_level; + action.system_context = system_context; + action.error_code = error_code; + action.no_return = 1; + + return action; +} + +#endif \ No newline at end of file