-
Notifications
You must be signed in to change notification settings - Fork 8.2k
event manager: Implement event manager #38611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
e18f64b
e634a0f
10aba05
c9bfdbd
9d9a31a
9dd42fc
3fdc2bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # | ||
| # Copyright (c) 2021 Nordic Semiconductor | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
|
|
||
| cmake_minimum_required(VERSION 3.20.0) | ||
|
|
||
| find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) | ||
| project("Event Manager sample") | ||
|
|
||
| # Include application event headers | ||
| zephyr_library_include_directories(src/events) | ||
|
|
||
| # Application sources | ||
| # NORDIC SDK APP START | ||
| target_sources(app PRIVATE src/main.c) | ||
|
|
||
| target_sources(app PRIVATE | ||
| src/events/ack_event.c | ||
| src/events/config_event.c | ||
| src/events/control_event.c | ||
| src/events/measurement_event.c | ||
| ) | ||
|
|
||
| target_sources(app PRIVATE | ||
| src/modules/controller.c | ||
| src/modules/sensor_simulated.c | ||
| src/modules/stats.c | ||
| ) | ||
| # NORDIC SDK APP END |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| .. _event_manager_sample: | ||
|
|
||
| Event Manager | ||
| ############# | ||
|
|
||
| .. contents:: | ||
| :local: | ||
| :depth: 2 | ||
|
|
||
| The Event Manager sample demonstrates the functionality of the :ref:`event_manager` subsystem. | ||
| It uses an event-driven architecture, where different modules communicate through sending and processing events. | ||
|
|
||
|
|
||
| Overview | ||
| ******** | ||
|
|
||
| The sample application consists of three modules that communicate using events: | ||
|
|
||
| Sensor (``sensor_simulated.c``): | ||
| This module waits for a configuration event (which is sent by ``main.c``). | ||
| After receiving this event, it simulates measured data at constant intervals. | ||
| Every time the data is updated, the module sends the current values as measurement event. | ||
| When the module receives a control event from the Controller, it responds with an ACK event. | ||
|
|
||
| Controller (``controller.c``): | ||
| This module waits for measurement events from the sensor. | ||
| Every time a measurement event is received, the module checks one of the measurement values that are transmitted as part of the event and, if the value exceeds a static threshold, sends a control event. | ||
|
|
||
| Statistics (``stats.c``): | ||
| This module waits for measurement events from the sensor. | ||
| The module calculates and logs basic statistics about one of the measurement values that are transmitted as part of the event. | ||
|
|
||
| Building and testing | ||
| ******************** | ||
|
|
||
| 1.Using development board: | ||
| Build and flash Event Manager as follows, changing nrf52840dk_nrf52840 for your board: | ||
| west build -b nrf52840dk_nrf52840 samples/subsys/event_manager | ||
| west flash | ||
| Then connect to the kit with a terminal emulator (for example, PuTTY). | ||
| #. Using qemu | ||
| If you use qemu platform changing qemu_leon3 for your board: | ||
| west build -b quemu_leon3 samples/subsys/event_manager | ||
| west build -t run | ||
| #. Observe that output similar to the following is logged on UART in case of development kit and in terminal in case of qemu:: | ||
|
|
||
| ***** Booting Zephyr OS v1.13.99-ncs1-4741-g1d6219f ***** | ||
| [00:00:00.000,854] <inf> event_manager: e: config_event init_val_1=3 | ||
| [00:00:00.001,068] <inf> event_manager: e: measurement_event val1=3 val2=3 val3=3 | ||
| [00:00:00.509,063] <inf> event_manager: e: measurement_event val1=3 val2=6 val3=9 | ||
| [00:00:01.018,005] <inf> event_manager: e: measurement_event val1=3 val2=9 val3=18 | ||
| [00:00:01.526,947] <inf> event_manager: e: measurement_event val1=3 val2=12 val3=30 | ||
| [00:00:02.035,888] <inf> event_manager: e: measurement_event val1=3 val2=15 val3=45 | ||
| [00:00:02.035,949] <inf> event_manager: e: control_event | ||
| [00:00:02.035,980] <inf> event_manager: e: ack_event | ||
| [00:00:02.544,830] <inf> event_manager: e: measurement_event val1=-3 val2=12 val3=57 | ||
| [00:00:03.053,771] <inf> event_manager: e: measurement_event val1=-3 val2=9 val3=66 | ||
| [00:00:03.562,713] <inf> event_manager: e: measurement_event val1=-3 val2=6 val3=72 | ||
| [00:00:04.071,655] <inf> event_manager: e: measurement_event val1=-3 val2=3 val3=75 | ||
| [00:00:04.580,596] <inf> event_manager: e: measurement_event val1=-3 val2=0 val3=75 | ||
| [00:00:04.580,596] <inf> stats: Average value3: 45 | ||
|
|
||
|
|
||
| Dependencies | ||
| ************ | ||
|
|
||
| This sample uses the following Zephyr subsystems: | ||
|
|
||
| * :ref:`event_manager` | ||
| * :ref:`logging_api` |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # | ||
| # Copyright (c) 2021 Nordic Semiconductor ASA | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Enabling assert | ||
| CONFIG_ASSERT=y | ||
|
|
||
| # Logger configuration | ||
| CONFIG_LOG=y | ||
| CONFIG_LOG_DEFAULT_LEVEL=2 | ||
|
|
||
| # Configuration required by Event Manager | ||
| CONFIG_EVENT_MANAGER=y | ||
| CONFIG_HEAP_MEM_POOL_SIZE=2048 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| sample: | ||
| description: Sample showing Event Manager usage | ||
| name: Event Manager sample | ||
| tests: | ||
| samples.event_manager: | ||
| build_only: true | ||
| arch_exclude: xtensa | ||
| integration_platforms: | ||
| - nrf52840dk_nrf52840 | ||
| - qemu_x86 | ||
| tags: ci_build |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| /* | ||
| * Copyright (c) 2021 Nordic Semiconductor ASA | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include <stdio.h> | ||
|
|
||
| #include "ack_event.h" | ||
|
|
||
|
|
||
| EVENT_TYPE_DEFINE(ack_event, | ||
| true, | ||
| NULL, | ||
| NULL); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| * Copyright (c) 2021 Nordic Semiconductor ASA | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #ifndef _ACK_EVENT_H_ | ||
| #define _ACK_EVENT_H_ | ||
|
|
||
| /** | ||
| * @brief ACK Event | ||
| * @defgroup ack_event ACK Event | ||
| * @{ | ||
| */ | ||
|
|
||
| #include "event_manager/event_manager.h" | ||
|
||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| struct ack_event { | ||
| struct event_header header; | ||
| }; | ||
|
|
||
| EVENT_TYPE_DECLARE(ack_event); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this need to run on C++ code?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I expect sample simply copied code from some previously defined event. Is there any harm coming out of it? |
||
|
|
||
| /** | ||
| * @} | ||
| */ | ||
|
|
||
| #endif /* _ACK_EVENT_H_ */ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| /* | ||
| * Copyright (c) 2021 Nordic Semiconductor ASA | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include <stdio.h> | ||
|
|
||
| #include "config_event.h" | ||
|
|
||
|
|
||
| static void log_config_event(const struct event_header *eh) | ||
| { | ||
| struct config_event *event = cast_config_event(eh); | ||
|
||
|
|
||
| EVENT_MANAGER_LOG(eh, "init_val_1=%d", event->init_value1); | ||
| } | ||
|
|
||
| EVENT_TYPE_DEFINE(config_event, | ||
| true, | ||
| log_config_event, | ||
| NULL); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| /* | ||
| * Copyright (c) 2021 Nordic Semiconductor ASA | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #ifndef _CONFIG_EVENT_H_ | ||
| #define _CONFIG_EVENT_H_ | ||
|
|
||
| /** | ||
| * @brief Config Event | ||
| * @defgroup config_event Config Event | ||
| * @{ | ||
| */ | ||
|
|
||
| #include "event_manager/event_manager.h" | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| struct config_event { | ||
| struct event_header header; | ||
|
|
||
| int8_t init_value1; | ||
| }; | ||
|
|
||
| EVENT_TYPE_DECLARE(config_event); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| /** | ||
| * @} | ||
| */ | ||
|
|
||
| #endif /* _CONFIG_EVENT_H_ */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /* | ||
| * Copyright (c) 2021 Nordic Semiconductor ASA | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include "control_event.h" | ||
|
|
||
|
|
||
| EVENT_TYPE_DEFINE(control_event, | ||
| true, | ||
| NULL, | ||
| NULL); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| * Copyright (c) 2021 Nordic Semiconductor ASA | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #ifndef _CONTROL_EVENT_H_ | ||
| #define _CONTROL_EVENT_H_ | ||
|
|
||
| /** | ||
| * @brief Control Event | ||
| * @defgroup control_event Control Event | ||
| * @{ | ||
| */ | ||
|
|
||
| #include "event_manager/event_manager.h" | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| struct control_event { | ||
| struct event_header header; | ||
| }; | ||
|
|
||
| EVENT_TYPE_DECLARE(control_event); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| /** | ||
| * @} | ||
| */ | ||
|
|
||
| #endif /* _CONTROL_EVENT_H_ */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* | ||
| * Copyright (c) 2021 Nordic Semiconductor ASA | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include <stdio.h> | ||
|
|
||
| #include "measurement_event.h" | ||
|
|
||
| static void log_measurement_event(const struct event_header *eh) | ||
| { | ||
| struct measurement_event *event = cast_measurement_event(eh); | ||
|
|
||
| EVENT_MANAGER_LOG(eh, "val1=%d val2=%d val3=%d", event->value1, | ||
| event->value2, event->value3); | ||
| } | ||
|
|
||
|
|
||
| EVENT_TYPE_DEFINE(measurement_event, | ||
| true, | ||
| log_measurement_event, | ||
| NULL); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| * Copyright (c) 2021 Nordic Semiconductor ASA | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #ifndef _MEASUREMENT_EVENT_H_ | ||
| #define _MEASUREMENT_EVENT_H_ | ||
|
|
||
| /** | ||
| * @brief Measurement Event | ||
| * @defgroup measurement_event Measurement Event | ||
| * @{ | ||
| */ | ||
|
|
||
| #include "event_manager/event_manager.h" | ||
|
|
||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
|
|
||
| struct measurement_event { | ||
| struct event_header header; | ||
|
|
||
| int8_t value1; | ||
| int16_t value2; | ||
| int32_t value3; | ||
| }; | ||
|
|
||
| EVENT_TYPE_DECLARE(measurement_event); | ||
|
|
||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
|
|
||
| /** | ||
| * @} | ||
| */ | ||
|
|
||
| #endif /* _MEASUREMENT_EVENT_H_ */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably we need to have another move, this is being introduced as a subsystem, so it needs to end up in samples/subsys/event_manager. Also, I am not sure why you have it in this file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, it can be moved there.