diff options
author | Jack Rosenthal <jrosenth@chromium.org> | 2020-10-27 13:14:46 -0600 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2020-11-05 23:07:36 +0000 |
commit | 9d157db4aaa9cb04f33809f170712a244b627b56 (patch) | |
tree | c3d22d52c614cd0f8d38c9cc5ba2a1f08add80ee /zephyr/test | |
parent | 1baff54e05a5d2f9179615f717e3514d37a594cb (diff) | |
download | chrome-ec-9d157db4aaa9cb04f33809f170712a244b627b56.tar.gz |
zephyr: shim in hooks and deferred
Implement deferred calls using the Zephyr's delayed work queues.
Implement hooks using SYS_INIT and a hooks registry created during
init.
BUG=b:168030971
BRANCH=none
TEST=provided unit tests
Build instructions for unit tests:
zmake configure -B/tmp/test \
~/trunk/src/platform/ec/zephyr/test/hooks
zmake build /tmp/test
Signed-off-by: Jack Rosenthal <jrosenth@chromium.org>
Change-Id: Id019cd1fe7bb3354377773d171036767e7efa706
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2504489
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Jett Rink <jettrink@chromium.org>
Diffstat (limited to 'zephyr/test')
-rw-r--r-- | zephyr/test/hooks/CMakeLists.txt | 9 | ||||
-rw-r--r-- | zephyr/test/hooks/hooks.c | 134 | ||||
-rw-r--r-- | zephyr/test/hooks/prj.conf | 11 | ||||
-rw-r--r-- | zephyr/test/hooks/zmake.yaml | 9 |
4 files changed, 163 insertions, 0 deletions
diff --git a/zephyr/test/hooks/CMakeLists.txt b/zephyr/test/hooks/CMakeLists.txt new file mode 100644 index 0000000000..9c6f0058fe --- /dev/null +++ b/zephyr/test/hooks/CMakeLists.txt @@ -0,0 +1,9 @@ +# Copyright 2020 The Chromium OS Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +cmake_minimum_required(VERSION 3.13.1) +project(hooks) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) + +target_sources(app PRIVATE hooks.c) diff --git a/zephyr/test/hooks/hooks.c b/zephyr/test/hooks/hooks.c new file mode 100644 index 0000000000..6b482b30cf --- /dev/null +++ b/zephyr/test/hooks/hooks.c @@ -0,0 +1,134 @@ +/* Copyright 2020 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +#include <stdbool.h> +#include <ztest.h> + +#include "hooks.h" + +static bool h1_called; +static bool h2_called; +static bool h3_called; + +static void h1(void) +{ + zassert_false(h1_called, "h1 was called, but should not have been"); + zassert_false(h2_called, "h2 was called, but should not have been"); + zassert_false(h3_called, "h3 was called, but should not have been"); + h1_called = true; +} +DECLARE_HOOK(HOOK_TEST_1, h1, HOOK_PRIO_FIRST); + +static void h2(void) +{ + zassert_true(h1_called, "h1 was not called, but should have been"); + zassert_false(h2_called, "h2 was called, but should not have been"); + zassert_false(h3_called, "h3 was called, but should not have been"); + h2_called = true; +} +DECLARE_HOOK(HOOK_TEST_1, h2, HOOK_PRIO_DEFAULT); + +static void h3(void) +{ + zassert_true(h1_called, "h1 was not called, but should have been"); + zassert_true(h2_called, "h2 was not called, but should have been"); + zassert_false(h3_called, "h3 was called, but should not have been"); + h3_called = true; +} +DECLARE_HOOK(HOOK_TEST_1, h3, HOOK_PRIO_LAST); + +static void test_hook_list_multiple(void) +{ + hook_notify(HOOK_TEST_1); + zassert_true(h1_called, "h1 was not called, but should have been"); + zassert_true(h2_called, "h2 was not called, but should have been"); + zassert_true(h3_called, "h3 was not called, but should have been"); +} + +static bool h4_called; + +static void h4(void) +{ + zassert_false(h4_called, "h4 was called, but should not have been"); + h4_called = true; +} +DECLARE_HOOK(HOOK_TEST_2, h4, HOOK_PRIO_DEFAULT); + +static void test_hook_list_single(void) +{ + hook_notify(HOOK_TEST_2); + zassert_true(h4_called, "h4 was not called, but should have been"); +} + +static void test_hook_list_empty(void) +{ + hook_notify(HOOK_TEST_3); +} + +static bool deferred_func_called; + +#define DEFERRED_DELAY_US (250 * 1000) +static void deferred_func(void) +{ + deferred_func_called = true; +} +DECLARE_DEFERRED(deferred_func); + +static void test_deferred_func(void) +{ + zassert_false( + deferred_func_called, + "The deferred function was called, but should not have been"); + hook_call_deferred(&deferred_func_data, DEFERRED_DELAY_US); + zassert_false( + deferred_func_called, + "The deferred function was called, but should not have been"); + k_usleep(DEFERRED_DELAY_US * 2); + zassert_true( + deferred_func_called, + "The deferred function was not called, but should have been"); +} + +static bool deferred_func_2_called; + +static void deferred_func_2(void) +{ + deferred_func_2_called = true; +} +DECLARE_DEFERRED(deferred_func_2); + +/* + * Test that repeated calls to hook_call_deferred result in the + * function being pushed out. + */ +static void test_deferred_func_push_out(void) +{ + zassert_false( + deferred_func_2_called, + "The deferred function was called, but should not have been"); + hook_call_deferred(&deferred_func_2_data, DEFERRED_DELAY_US); + hook_call_deferred(&deferred_func_2_data, DEFERRED_DELAY_US * 3); + k_usleep(DEFERRED_DELAY_US * 2); + zassert_false( + deferred_func_2_called, + "The deferred function was called, but should not have been"); + k_usleep(DEFERRED_DELAY_US * 2); + zassert_true( + deferred_func_called, + "The deferred function was not called, but should have been"); +} + +void test_main(void) +{ + ztest_test_suite( + hooks_tests, + ztest_unit_test(test_hook_list_multiple), + ztest_unit_test(test_hook_list_single), + ztest_unit_test(test_hook_list_empty), + ztest_unit_test(test_deferred_func), + ztest_unit_test(test_deferred_func_push_out)); + + ztest_run_test_suite(hooks_tests); +} diff --git a/zephyr/test/hooks/prj.conf b/zephyr/test/hooks/prj.conf new file mode 100644 index 0000000000..43c0c9b8e5 --- /dev/null +++ b/zephyr/test/hooks/prj.conf @@ -0,0 +1,11 @@ +# Copyright 2020 The Chromium OS Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +CONFIG_ZTEST=y +CONFIG_PLATFORM_EC=y + +# TODO(b/172512307): timer introduces dependency on system module, +# which has build issues right now for native_posix board. Remove +# this once the system module has been corrected. +CONFIG_PLATFORM_EC_TIMER=n diff --git a/zephyr/test/hooks/zmake.yaml b/zephyr/test/hooks/zmake.yaml new file mode 100644 index 0000000000..c784549bb0 --- /dev/null +++ b/zephyr/test/hooks/zmake.yaml @@ -0,0 +1,9 @@ +# Copyright 2020 The Chromium OS Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +board: native_posix +supported-zephyr-versions: + - v2.4 +toolchain: zephyr +output-type: elf |