Skip to content

Commit

Permalink
caf: modules: added shell module to common application framework
Browse files Browse the repository at this point in the history
Added a shell module to the Common Application Framework, and
added a custom command to that shell to simulate button press
event.

Signed-off-by: Tony Le <tony.le@nordicsemi.no>
  • Loading branch information
tony-le-24 committed Aug 17, 2023
1 parent c2b7e6b commit a5cb384
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 0 deletions.
38 changes: 38 additions & 0 deletions doc/nrf/libraries/caf/caf_shell.rst
Original file line number Diff line number Diff line change
@@ -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``.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions subsys/caf/modules/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions subsys/caf/modules/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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"
18 changes: 18 additions & 0 deletions subsys/caf/modules/Kconfig.caf_shell
Original file line number Diff line number Diff line change
@@ -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
49 changes: 49 additions & 0 deletions subsys/caf/modules/caf_shell.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright (c) 2023 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/

#include <stdlib.h>
#include <zephyr/shell/shell.h>
#include <caf/events/button_event.h>

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);

0 comments on commit a5cb384

Please sign in to comment.