Skip to content

Commit 2486637

Browse files
zyczrlubos
authored andcommitted
event_manager: Add subscribe first to event
Add first subscriber to event functionality to event manager. Jira: NCSDK-9067 Signed-off-by: Jan Zyczkowski <[email protected]>
1 parent 28ff3f2 commit 2486637

File tree

4 files changed

+44
-12
lines changed

4 files changed

+44
-12
lines changed

doc/nrf/libraries/others/event_manager.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ To turn a module into a listener for specific event types, complete the followin
230230
For subscribing to an event type, the Event Manager provides three types of subscriptions, differing in priority.
231231
They can be registered with the following macros:
232232

233+
* :c:macro:`EVENT_SUBSCRIBE_FIRST` - notification as the first subscriber
233234
* :c:macro:`EVENT_SUBSCRIBE_EARLY` - notification before other listeners
234235
* :c:macro:`EVENT_SUBSCRIBE` - standard notification
235236
* :c:macro:`EVENT_SUBSCRIBE_FINAL` - notification as the last, final subscriber

doc/nrf/releases/release-notes-changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ Modem libraries
228228
Event manager
229229
-------------
230230

231+
* Added:
232+
233+
* ``EVENT_SUBSCRIBE_FIRST`` subscriber priority.
234+
231235
* Updated:
232236

233237
* Modified the sections used by the event manager. Stopped using orphaned sections. Removed forced alignment for x86. Reworked priorities.

include/event_manager.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ extern struct event_type _event_type_list_end[];
119119
#define EVENT_LISTENER(lname, cb_fn) _EVENT_LISTENER(lname, cb_fn)
120120

121121

122+
/** Subscribe a listener to an event type as first module that is
123+
* being notified.
124+
*
125+
* @param lname Name of the listener.
126+
* @param ename Name of the event.
127+
*/
128+
#define EVENT_SUBSCRIBE_FIRST(lname, ename) \
129+
_EVENT_SUBSCRIBE(lname, ename, _EM_MARKER_FIRST_ELEMENT); \
130+
const struct {} _CONCAT(_CONCAT(__event_subscriber_, ename), first_sub_redefined) = {}
131+
132+
122133
/** Subscribe a listener to the early notification list for an
123134
* event type.
124135
*

tests/subsys/event_manager/src/modules/test_subs.c

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@
1515

1616
static enum test_id cur_test_id;
1717

18+
static int first_cnt;
1819
static int early_cnt;
1920
static int normal_cnt;
2021

21-
static bool event_handler_early(const struct event_header *eh)
22+
static bool event_handler_first(const struct event_header *eh)
2223
{
2324
if (is_test_start_event(eh)) {
2425
struct test_start_event *event = cast_test_start_event(eh);
@@ -30,6 +31,28 @@ static bool event_handler_early(const struct event_header *eh)
3031

3132
if (is_order_event(eh)) {
3233
if (cur_test_id == TEST_SUBSCRIBER_ORDER) {
34+
first_cnt++;
35+
}
36+
37+
return false;
38+
}
39+
40+
zassert_true(false, "Event unhandled");
41+
return false;
42+
}
43+
44+
/* Create one first listener. */
45+
EVENT_LISTENER(first, event_handler_first);
46+
EVENT_SUBSCRIBE_FIRST(first, order_event);
47+
EVENT_SUBSCRIBE_EARLY(first, test_start_event);
48+
49+
50+
static bool event_handler_early(const struct event_header *eh)
51+
{
52+
if (is_order_event(eh)) {
53+
if (cur_test_id == TEST_SUBSCRIBER_ORDER) {
54+
zassert_equal(first_cnt, 1, "Incorrect subscriber order"
55+
" - early before first");
3356
early_cnt++;
3457
}
3558

@@ -43,21 +66,20 @@ static bool event_handler_early(const struct event_header *eh)
4366
/* Create 3 early listeners. */
4467
EVENT_LISTENER(early1, event_handler_early);
4568
EVENT_SUBSCRIBE_EARLY(early1, order_event);
46-
EVENT_SUBSCRIBE_EARLY(early1, test_start_event);
4769

4870
EVENT_LISTENER(early2, event_handler_early);
4971
EVENT_SUBSCRIBE_EARLY(early2, order_event);
50-
EVENT_SUBSCRIBE_EARLY(early2, test_start_event);
5172

5273
EVENT_LISTENER(early3, event_handler_early);
5374
EVENT_SUBSCRIBE_EARLY(early3, order_event);
54-
EVENT_SUBSCRIBE_EARLY(early3, test_start_event);
5575

5676

5777
static bool event_handler_normal(const struct event_header *eh)
5878
{
5979
if (is_order_event(eh)) {
6080
if (cur_test_id == TEST_SUBSCRIBER_ORDER) {
81+
zassert_equal(first_cnt, 1, "Incorrect subscriber order"
82+
" - normal before first");
6183
zassert_equal(early_cnt, 3, "Incorrect subscriber order"
6284
" - normal before early");
6385
normal_cnt++;
@@ -83,16 +105,10 @@ EVENT_SUBSCRIBE(listener3, order_event);
83105

84106
static bool event_handler_final(const struct event_header *eh)
85107
{
86-
if (is_test_start_event(eh)) {
87-
struct test_start_event *event = cast_test_start_event(eh);
88-
89-
cur_test_id = event->test_id;
90-
91-
return false;
92-
}
93-
94108
if (is_order_event(eh)) {
95109
if (cur_test_id == TEST_SUBSCRIBER_ORDER) {
110+
zassert_equal(first_cnt, 1, "Incorrect subscriber order"
111+
" - late before first");
96112
zassert_equal(early_cnt, 3, "Incorrect subscriber order"
97113
" - late before early");
98114
zassert_equal(normal_cnt, 3,

0 commit comments

Comments
 (0)