diff --git a/doc/nrf/libraries/caf/caf_shell.rst b/doc/nrf/libraries/caf/caf_shell.rst new file mode 100644 index 00000000000..742797a5ae4 --- /dev/null +++ b/doc/nrf/libraries/caf/caf_shell.rst @@ -0,0 +1,38 @@ +.. _caf_shell: + +CAF: Shell module +################# + +.. contents:: + :local: + :depth: 2 + +The shell module of the :ref:`lib_caf` (CAF) allows interactions with CAF modules and events using a predefined set of Zephyr's :ref:`zephyr:shell_api` commands. +The module introduces a shell command for submitting events informing about button press or release (:c:struct:`button_event`). +Submitting :c:struct:`button_event` related to the hardware button presses can be handled by the :ref:`caf_buttons`. + +Configuration +************* + +To use the module, enable the following Kconfig options: + +* :kconfig:option:`CONFIG_CAF_SHELL` - This option is mandatory as it enables this module. +* :kconfig:option:`CONFIG_SHELL` - This option enables Zephyr shell that is used by the module to receive commands from user. +* :kconfig:option:`CONFIG_CAF` - This option enables the Common Application Framework. + +This module selects the Kconfig option :kconfig:option:`CONFIG_CAF_BUTTON_EVENTS`. + +Usage +***** + +All shell commands of this module are subcommands of the :command:`caf_events` command. +To send a :c:struct:`button_event`, use the following syntax: + + .. code-block:: console + + caf_events button_event [button_id] [pressed] + +The :command:`button_event` subcommand has two required arguments: + +* ``button_id`` - A decimal number represents the ID of a button. +* ``pressed`` - Button press state, can only be either ``y`` or ``n``. diff --git a/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst b/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst index 9ca2cfadac9..7ee1fde77b6 100644 --- a/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst +++ b/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst @@ -742,6 +742,8 @@ Other libraries Common Application Framework (CAF) ---------------------------------- +* Added :ref:`caf_shell` for triggering CAF events. + * :ref:`caf_buttons`: * Added selective wakeup functionality. diff --git a/subsys/caf/modules/CMakeLists.txt b/subsys/caf/modules/CMakeLists.txt index 6b05f12642b..55397ebca1b 100644 --- a/subsys/caf/modules/CMakeLists.txt +++ b/subsys/caf/modules/CMakeLists.txt @@ -37,4 +37,7 @@ zephyr_library_sources_ifdef(CONFIG_CAF_SENSOR_DATA_AGGREGATOR sensor_data_aggre zephyr_library_sources_ifdef(CONFIG_CAF_SETTINGS_LOADER settings_loader.c) zephyr_library_sources_ifdef(CONFIG_CAF_BLE_SMP ble_smp.c) + +zephyr_library_sources_ifdef(CONFIG_CAF_SHELL caf_shell.c) + zephyr_library_link_libraries_ifdef(CONFIG_MCUMGR mgmt_mcumgr) diff --git a/subsys/caf/modules/Kconfig b/subsys/caf/modules/Kconfig index 50a24c03916..99b99c87964 100644 --- a/subsys/caf/modules/Kconfig +++ b/subsys/caf/modules/Kconfig @@ -17,3 +17,4 @@ rsource "Kconfig.power_manager" rsource "Kconfig.sensor_data_aggregator" rsource "Kconfig.sensor_manager" rsource "Kconfig.settings_loader" +rsource "Kconfig.caf_shell" diff --git a/subsys/caf/modules/Kconfig.caf_shell b/subsys/caf/modules/Kconfig.caf_shell new file mode 100644 index 00000000000..7575358a524 --- /dev/null +++ b/subsys/caf/modules/Kconfig.caf_shell @@ -0,0 +1,18 @@ +# +# Copyright (c) 2023 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# + +menuconfig CAF_SHELL + bool "Shell interface for triggering CAF events" + depends on SHELL + select CAF_BUTTON_EVENTS + +if CAF_SHELL + +module = CAF_SHELL +module-str = caf shell +source "subsys/logging/Kconfig.template.log_config" + +endif # CAF_SHELL diff --git a/subsys/caf/modules/caf_shell.c b/subsys/caf/modules/caf_shell.c new file mode 100644 index 00000000000..95720c27e39 --- /dev/null +++ b/subsys/caf/modules/caf_shell.c @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +#include +#include +#include + +static const char button_event_cmd_help_str[] = + "Submit a button_event with user-defined key ID and pressed state\n" + " Key ID Decimal numeric ID of the button\n" + " Pressed state 'y' or 'n'\n"; + +static int button_event_handler(const struct shell *shell, size_t argc, char **argv) +{ + /* The string part of button_id param, for invalidation check */ + char *str_part; + + /* Convert button_id from string to decimal number */ + long button_id = strtol(argv[1], &str_part, 10); + + if (strlen(str_part) > 0 || button_id < 0 || button_id > UINT16_MAX) { + shell_error(shell, "Invalid key ID"); + return -EINVAL; + } + + if ((strlen(argv[2]) > 1) || (*argv[2] != 'y' && *argv[2] != 'n')) { + shell_error(shell, "Invalid button press state"); + return -EINVAL; + } + + struct button_event *event = new_button_event(); + + event->key_id = (uint16_t)button_id; + event->pressed = *argv[2] != 'n'; + APP_EVENT_SUBMIT(event); + + return 0; +} + +SHELL_STATIC_SUBCMD_SET_CREATE(sub_caf_shell_command, + SHELL_CMD_ARG(button_event, NULL, button_event_cmd_help_str, button_event_handler, 3, 0), + SHELL_SUBCMD_SET_END +); + +SHELL_CMD_REGISTER(caf_events, &sub_caf_shell_command, + "Submit a CAF event with user-defined parameters", NULL);