|
| 1 | +#include "iolib.h" |
| 2 | +#include <zephyr/drivers/hwinfo.h> |
| 3 | +#include <zephyr/input/input.h> |
| 4 | +#include <zephyr/logging/log.h> |
| 5 | + |
| 6 | +#include <bitset> |
| 7 | +#include "testcases.hpp" |
| 8 | +#include <port/zephyr/message_queue.hpp> |
| 9 | +#include <port/zephyr/udc_mac.hpp> |
| 10 | +#include <port/zephyr/usb_shell.hpp> |
| 11 | +#include <usb/df/class/hid.hpp> |
| 12 | +#include <usb/df/device.hpp> |
| 13 | + |
| 14 | +LOG_MODULE_REGISTER(main, LOG_LEVEL_INF); |
| 15 | + |
| 16 | +using namespace magic_enum::bitwise_operators; |
| 17 | +using namespace std::chrono_literals; |
| 18 | + |
| 19 | +static std::bitset<TESTCASE_COUNT> test_results{}; |
| 20 | +static std::size_t testcase_index{}; |
| 21 | +static bool boot_protocol_host{}; |
| 22 | + |
| 23 | +auto& host_feedback_msgq() |
| 24 | +{ |
| 25 | + static os::zephyr::message_queue_instance<bool, 2> msgq; |
| 26 | + return msgq; |
| 27 | +} |
| 28 | + |
| 29 | +auto& keyboard_app() |
| 30 | +{ |
| 31 | + static simple_keyboard<TEST_REPORT_ID> keyb{ |
| 32 | + [](const simple_keyboard<TEST_REPORT_ID>::kb_leds_report& report) |
| 33 | + { |
| 34 | + auto led_state = report.leds.test(hid::page::leds::CAPS_LOCK); |
| 35 | + LOG_INF("LED report received: %d", (int)led_state); |
| 36 | + host_feedback_msgq().post(led_state); |
| 37 | + }}; |
| 38 | + return keyb; |
| 39 | +} |
| 40 | + |
| 41 | +void send_keypress() |
| 42 | +{ |
| 43 | + LOG_INF("Sending key press"); |
| 44 | + keyboard_app().send_key(hid::page::keyboard_keypad::KEYBOARD_CAPS_LOCK, 1); |
| 45 | + k_msleep(50); |
| 46 | + keyboard_app().send_key(hid::page::keyboard_keypad::KEYBOARD_CAPS_LOCK, 0); |
| 47 | + k_msleep(50); |
| 48 | +} |
| 49 | + |
| 50 | +auto& device() |
| 51 | +{ |
| 52 | + static constexpr usb::product_info product_info{ |
| 53 | + CONFIG_DEMO_MANUFACTURER_ID, CONFIG_DEMO_MANUFACTURER, CONFIG_DEMO_PRODUCT_ID, |
| 54 | + CONFIG_DEMO_PRODUCT, usb::version("1.0")}; |
| 55 | + static usb::zephyr::udc_mac mac{DEVICE_DT_GET(DT_NODELABEL(zephyr_udc0))}; |
| 56 | + static usb::df::device_instance<usb::speed::FULL> device{mac, product_info}; |
| 57 | + return device; |
| 58 | +} |
| 59 | + |
| 60 | +bool advance_test() |
| 61 | +{ |
| 62 | + constexpr auto speed = usb::speed::FULL; |
| 63 | + static usb::df::hid::function usb_kb{keyboard_app(), "keyboard", |
| 64 | + usb::hid::boot_protocol_mode::KEYBOARD}; |
| 65 | + |
| 66 | + static const auto hid_config = usb::df::config::make_config( |
| 67 | + usb::df::config::header(usb::df::config::power::bus(500), "HID config"), |
| 68 | + usb::df::hid::config(usb_kb, speed, usb::endpoint::address(0x81), 1 |
| 69 | +#if 0 // TODO: kconfig flag? |
| 70 | + , usb::endpoint::address(0x01), 10 |
| 71 | +#endif |
| 72 | + )); |
| 73 | + |
| 74 | + static const auto shell_config = usb::df::config::make_config( |
| 75 | + usb::df::config::header(usb::df::config::power::bus(500), "shell config"), |
| 76 | + usb::df::cdc::config( |
| 77 | + usb::zephyr::usb_shell::handle(), speed, usb::endpoint::address(0x01), |
| 78 | + usb::endpoint::address(0x81), |
| 79 | + usb::endpoint::address(0x82) // note that notification endpoint is unused here |
| 80 | + )); |
| 81 | + |
| 82 | + if (device().is_open()) |
| 83 | + { |
| 84 | + // soft disconnect from host |
| 85 | + device().close(); |
| 86 | + k_msleep(1000); |
| 87 | + |
| 88 | + testcase_index++; |
| 89 | + } |
| 90 | + |
| 91 | + if (boot_protocol_host) |
| 92 | + { |
| 93 | + // host is in boot protocol mode, skip further tests |
| 94 | + } |
| 95 | + else if (testcase_index < TESTCASE_COUNT) |
| 96 | + { |
| 97 | + auto& tc = testcases()[testcase_index]; |
| 98 | + keyboard_app().set_report_protocol(tc.protocol); |
| 99 | + LOG_INF("Starting test %u/%u: %s", testcase_index + 1, TESTCASE_COUNT, tc.description); |
| 100 | + |
| 101 | + device().set_config(hid_config); |
| 102 | + device().open(); |
| 103 | + return true; |
| 104 | + } |
| 105 | + // else |
| 106 | + { |
| 107 | + LOG_INF("All tests done: %u/%u passed", test_results.count(), |
| 108 | + boot_protocol_host ? 1 : TESTCASE_COUNT); |
| 109 | + device().set_config(shell_config); |
| 110 | + device().open(); |
| 111 | + return false; |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +static k_tid_t main_thread_id{}; |
| 116 | + |
| 117 | +int main(void) |
| 118 | +{ |
| 119 | + main_thread_id = k_current_get(); |
| 120 | + device().set_power_event_delegate( |
| 121 | + [](usb::df::device& dev, usb::df::device::event ev) |
| 122 | + { |
| 123 | + if (ev == usb::df::device::event::CONFIGURATION_CHANGE) |
| 124 | + { |
| 125 | + LOG_INF("USB configured: %d", (int)dev.configured()); |
| 126 | + if (dev.configured()) |
| 127 | + { |
| 128 | + k_wakeup(main_thread_id); |
| 129 | + } |
| 130 | + } |
| 131 | + }); |
| 132 | + |
| 133 | + while (advance_test()) |
| 134 | + { |
| 135 | + // wait until device is configured |
| 136 | + k_sleep(K_FOREVER); |
| 137 | + |
| 138 | + auto testcase_start_time = os::zephyr::tick_timer::now(); |
| 139 | + |
| 140 | + std::array<std::optional<bool>, 2> replies{}; |
| 141 | + auto* reply = &replies[0]; |
| 142 | + |
| 143 | + // wait for initial LED state from host |
| 144 | + *reply = host_feedback_msgq().try_get_for(500ms); |
| 145 | + if (!reply->has_value()) |
| 146 | + { |
| 147 | + LOG_WRN("No initial LED state from host"); |
| 148 | + } |
| 149 | + |
| 150 | + // send key press and check that host changes LED state |
| 151 | + send_keypress(); |
| 152 | + reply++; |
| 153 | + *reply = host_feedback_msgq().try_get_for(500ms); |
| 154 | + if (!reply->has_value()) |
| 155 | + { |
| 156 | + LOG_ERR("No host response to key press"); |
| 157 | + continue; |
| 158 | + } |
| 159 | + if ((reply - 1)->has_value() && (reply->value() == (reply - 1)->value())) |
| 160 | + { |
| 161 | + LOG_ERR("Host did not change LED state on key press"); |
| 162 | + continue; |
| 163 | + } |
| 164 | + |
| 165 | + // restore original state |
| 166 | + send_keypress(); |
| 167 | + |
| 168 | + test_results.set(testcase_index); |
| 169 | + LOG_INF("Test #%u passed in %u ms", testcase_index, |
| 170 | + unsigned(std::chrono::duration_cast<std::chrono::milliseconds>( |
| 171 | + os::zephyr::tick_timer::now() - testcase_start_time) |
| 172 | + .count())); |
| 173 | + |
| 174 | + if (keyboard_app().get_protocol() == hid::protocol::BOOT) |
| 175 | + { |
| 176 | + boot_protocol_host = true; |
| 177 | + LOG_INF("Host uses boot protocol mode, skipping further tests"); |
| 178 | + break; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + // visual indication that tests are done |
| 183 | + iolib_set_led(0, 1); |
| 184 | + |
| 185 | + // TODO: or simply return? |
| 186 | + while (true) |
| 187 | + { |
| 188 | + k_sleep(K_FOREVER); |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +static int cmd_test_results(const ::shell* sh, [[maybe_unused]] size_t argc, |
| 193 | + [[maybe_unused]] char* argv[]) |
| 194 | +{ |
| 195 | + auto index = testcase_index; |
| 196 | + auto results = test_results; |
| 197 | + auto pass_count = results.count(); |
| 198 | + if (index < (boot_protocol_host ? 1 : TESTCASE_COUNT)) |
| 199 | + { |
| 200 | + shell_warn(sh, "Tests are still in progress, results are incomplete"); |
| 201 | + } |
| 202 | + if (boot_protocol_host) |
| 203 | + { |
| 204 | + shell_print(sh, pass_count ? "Results: test passed" : "Results: test failed"); |
| 205 | + return 0; |
| 206 | + } |
| 207 | + |
| 208 | + shell_print(sh, "Results: %u/%u test passed", pass_count, index); |
| 209 | + if (pass_count == index) |
| 210 | + { |
| 211 | + return 0; |
| 212 | + } |
| 213 | + shell_print(sh, "Failed test cases:"); |
| 214 | + for (std::size_t i = 0; i < index; ++i) |
| 215 | + { |
| 216 | + if (!results.test(i)) |
| 217 | + { |
| 218 | + shell_print(sh, " %u. %s", i + 1, testcases()[i].description); |
| 219 | + } |
| 220 | + } |
| 221 | + return 0; |
| 222 | +} |
| 223 | + |
| 224 | +SHELL_STATIC_SUBCMD_SET_CREATE(sub_test, |
| 225 | + // |
| 226 | + SHELL_CMD(results, NULL, "Print available results", |
| 227 | + &cmd_test_results), |
| 228 | + SHELL_SUBCMD_SET_END); |
| 229 | + |
| 230 | +SHELL_CMD_REGISTER(test, &sub_test, "Test control", NULL); |
0 commit comments