nRF Connect SDK API 0.1.0
Loading...
Searching...
No Matches
app_event_manager.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5 */
6
11#ifndef _APP_EVENT_MANAGER_H_
12#define _APP_EVENT_MANAGER_H_
13
14
22#include <zephyr/kernel.h>
23#include <zephyr/types.h>
24#include <zephyr/sys/__assert.h>
25#include <zephyr/logging/log.h>
26
27#include <app_event_manager_priv.h>
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
40typedef bool (*cb_fn)(const struct app_event_header *aeh);
62};
63
70static inline bool app_event_get_type_flag(const struct event_type *et,
71 enum app_event_type_flags flag)
72{
73 return (et->flags & BIT(flag)) != 0;
74}
75
84#define APP_EVENT_ID(ename) _EVENT_ID(ename)
85
91#define APP_EVENT_LISTENER(lname, cb_fn) _APP_EVENT_LISTENER(lname, cb_fn)
92
93
100#define APP_EVENT_SUBSCRIBE_FIRST(lname, ename) \
101 _APP_EVENT_SUBSCRIBE(lname, ename, _APP_EM_MARKER_FIRST_ELEMENT); \
102 const struct {} _CONCAT(_CONCAT(__event_subscriber_, ename), first_sub_redefined) = {}
103
104
111#define APP_EVENT_SUBSCRIBE_EARLY(lname, ename) \
112 _APP_EVENT_SUBSCRIBE(lname, ename, _APP_EM_SUBS_PRIO_ID(_APP_EM_SUBS_PRIO_EARLY))
113
114
121#define APP_EVENT_SUBSCRIBE(lname, ename) \
122 _APP_EVENT_SUBSCRIBE(lname, ename, _APP_EM_SUBS_PRIO_ID(_APP_EM_SUBS_PRIO_NORMAL))
123
124
131#define APP_EVENT_SUBSCRIBE_FINAL(lname, ename) \
132 _APP_EVENT_SUBSCRIBE(lname, ename, _APP_EM_MARKER_FINAL_ELEMENT); \
133 const struct {} _CONCAT(_CONCAT(__event_subscriber_, ename), final_sub_redefined) = {}
134
135
143#define APP_EVENT_TYPE_DECLARE(ename) _APP_EVENT_TYPE_DECLARE(ename)
144
145
154#define APP_EVENT_TYPE_DYNDATA_DECLARE(ename) _APP_EVENT_TYPE_DYNDATA_DECLARE(ename)
155
156
177#define APP_EVENT_TYPE_DEFINE(ename, log_fn, ev_info_struct, app_event_type_flags) \
178 _APP_EVENT_TYPE_DEFINE(ename, log_fn, ev_info_struct, app_event_type_flags)
179
180
189#define APP_EVENT_ASSERT_ID(id) \
190 __ASSERT_NO_MSG((id >= _event_type_list_start) && (id < _event_type_list_end))
191
198#define APP_EVENT_SUBMIT(event) _event_submit(&event->header)
199
210#define APP_EVENT_MANAGER_HOOK_POSTINIT_REGISTER(hook_fn) \
211 _APP_EVENT_MANAGER_HOOK_POSTINIT_REGISTER(hook_fn, \
212 _APP_EM_SUBS_PRIO_ID(_APP_EM_SUBS_PRIO_NORMAL))
213
226static inline size_t app_event_manager_event_size(const struct app_event_header *aeh)
227{
228#if IS_ENABLED(CONFIG_APP_EVENT_MANAGER_PROVIDE_EVENT_SIZE)
229 size_t size = aeh->type_id->struct_size;
230
232 size += ((const struct event_dyndata *)
233 (((const uint8_t *)aeh) + size - sizeof(struct event_dyndata)))->size;
234 }
235 return size;
236#else
237 __ASSERT_NO_MSG(false);
238 return 0;
239#endif
240}
241
242
258#define APP_EVENT_HOOK_ON_SUBMIT_REGISTER_FIRST(hook_fn) \
259 const struct {} __event_hook_on_submit_first_sub_redefined = {}; \
260 _APP_EVENT_HOOK_ON_SUBMIT_REGISTER(hook_fn, _APP_EM_MARKER_FIRST_ELEMENT)
261
275#define APP_EVENT_HOOK_ON_SUBMIT_REGISTER(hook_fn) \
276 _APP_EVENT_HOOK_ON_SUBMIT_REGISTER(hook_fn, _APP_EM_SUBS_PRIO_ID(_APP_EM_SUBS_PRIO_NORMAL))
277
293#define APP_EVENT_HOOK_ON_SUBMIT_REGISTER_LAST(hook_fn) \
294 const struct {} __event_hook_on_submit_last_sub_redefined = {}; \
295 _APP_EVENT_HOOK_ON_SUBMIT_REGISTER(hook_fn, _APP_EM_MARKER_FINAL_ELEMENT)
296
306#define APP_EVENT_HOOK_PREPROCESS_REGISTER_FIRST(hook_fn) \
307 const struct {} __event_hook_preprocess_first_sub_redefined = {}; \
308 _APP_EVENT_HOOK_PREPROCESS_REGISTER(hook_fn, _APP_EM_MARKER_FIRST_ELEMENT)
309
318#define APP_EVENT_HOOK_PREPROCESS_REGISTER(hook_fn) \
319 _APP_EVENT_HOOK_PREPROCESS_REGISTER(hook_fn, _APP_EM_SUBS_PRIO_ID(_APP_EM_SUBS_PRIO_NORMAL))
320
331#define APP_EVENT_HOOK_PREPROCESS_REGISTER_LAST(hook_fn) \
332 const struct {} __event_hook_preprocess_last_sub_redefined = {}; \
333 _APP_EVENT_HOOK_PREPROCESS_REGISTER(hook_fn, _APP_EM_MARKER_FINAL_ELEMENT)
334
345#define APP_EVENT_HOOK_POSTPROCESS_REGISTER_FIRST(hook_fn) \
346 const struct {} __event_hook_postprocess_first_sub_redefined = {}; \
347 _APP_EVENT_HOOK_POSTPROCESS_REGISTER(hook_fn, _APP_EM_MARKER_FIRST_ELEMENT)
348
357#define APP_EVENT_HOOK_POSTPROCESS_REGISTER(hook_fn) \
358 _APP_EVENT_HOOK_POSTPROCESS_REGISTER(hook_fn, \
359 _APP_EM_SUBS_PRIO_ID(_APP_EM_SUBS_PRIO_NORMAL))
360
371#define APP_EVENT_HOOK_POSTPROCESS_REGISTER_LAST(hook_fn) \
372 const struct {} __event_hook_postprocess_last_sub_redefined = {}; \
373 _APP_EVENT_HOOK_POSTPROCESS_REGISTER(hook_fn, _APP_EM_MARKER_FINAL_ELEMENT)
374
375
382
392void *app_event_manager_alloc(size_t size);
393
394
403void app_event_manager_free(void *addr);
404
405
415#define APP_EVENT_MANAGER_LOG(aeh, ...) do { \
416 LOG_MODULE_DECLARE(app_event_manager, CONFIG_APP_EVENT_MANAGER_LOG_LEVEL); \
417 if (IS_ENABLED(CONFIG_APP_EVENT_MANAGER_LOG_EVENT_TYPE)) { \
418 LOG_INF("e:%s " GET_ARG_N(1, __VA_ARGS__), aeh->type_id->name \
419 COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), \
420 (), \
421 (, GET_ARGS_LESS_N(1, __VA_ARGS__)) \
422 )); \
423 } else { \
424 LOG_INF(__VA_ARGS__); \
425 } \
426} while (0)
427
428
435 #define APP_EVENT_FLAGS_CREATE(...) \
436 (FOR_EACH_NONEMPTY_TERM(_APP_EVENT_FLAGS_JOIN, (|), __VA_ARGS__) 0)
437
438
439#ifdef __cplusplus
440}
441#endif
442
447#endif /* _APP_EVENT_MANAGER_H_ */
void * app_event_manager_alloc(size_t size)
Allocate event.
int app_event_manager_init(void)
Initialize the Application Event Manager.
static bool app_event_get_type_flag(const struct event_type *et, enum app_event_type_flags flag)
Get event type flag's value.
Definition: app_event_manager.h:70
bool(* cb_fn)(const struct app_event_header *aeh)
Pointer to the event handler function.
Definition: app_event_manager.h:40
app_event_type_flags
List of bits in event type flags.
Definition: app_event_manager.h:44
@ APP_EVENT_TYPE_FLAGS_USER_SETTABLE_START
Definition: app_event_manager.h:52
@ APP_EVENT_TYPE_FLAGS_INIT_LOG_ENABLE
Definition: app_event_manager.h:56
@ APP_EVENT_TYPE_FLAGS_HAS_DYNDATA
Definition: app_event_manager.h:50
@ APP_EVENT_TYPE_FLAGS_USER_DEFINED_START
Definition: app_event_manager.h:61
@ APP_EVENT_TYPE_FLAGS_COUNT
Definition: app_event_manager.h:59
@ APP_EVENT_TYPE_FLAGS_SYSTEM_START
Definition: app_event_manager.h:46
static size_t app_event_manager_event_size(const struct app_event_header *aeh)
Get the event size.
Definition: app_event_manager.h:226
void app_event_manager_free(void *addr)
Free memory occupied by the event.