nRF Connect SDK API 0.1.0
Loading...
Searching...
No Matches
module_state_event.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018-2021 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5 */
6
7#ifndef _MODULE_STATE_EVENT_H_
8#define _MODULE_STATE_EVENT_H_
9
18#include <zephyr/sys/atomic.h>
19#include <app_event_manager.h>
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26#define MODULE_ID_LIST_SECTION_PREFIX module_id_list
27
28#define MODULE_ID_PTR_VAR(mname) _CONCAT(__module_, mname)
29#define MODULE_ID_LIST_SECTION_NAME STRINGIFY(MODULE_ID_LIST_SECTION_PREFIX)
30#define MODULE_ID_PTR_VAR_EXTERN_DEC(mname) \
31 extern const void * const MODULE_ID_PTR_VAR(mname)
32
33extern const void * const __start_module_id_list[];
34extern const void * const __stop_module_id_list[];
35
36
42static inline size_t module_count(void)
43{
44 return (__stop_module_id_list - __start_module_id_list);
45}
46
54static inline const void *module_id_get(size_t idx)
55{
56 __ASSERT_NO_MSG(idx < module_count());
57
58 return (__start_module_id_list + idx);
59}
60
68static inline size_t module_idx_get(const void *module_id)
69{
70 return ((const size_t *)module_id - (const size_t *)__start_module_id_list);
71}
72
80static inline const char *module_name_get(const void *id)
81{
82 __ASSERT_NO_MSG(id);
83
84 return *((const char **)id);
85}
86
95#define MODULE_IDX(mname) ({ \
96 MODULE_ID_PTR_VAR_EXTERN_DEC(mname); \
97 &MODULE_ID_PTR_VAR(mname) - __start_module_id_list; \
98 })
99
104 ATOMIC_DEFINE(f, CONFIG_CAF_MODULES_FLAGS_COUNT);
105};
106
115static inline bool module_check_id_valid(size_t module_idx)
116{
117 return (module_idx < module_count());
118}
119
128static inline bool module_flags_check_zero(const struct module_flags *mf)
129{
130 const atomic_t *target = mf->f;
131
132 for (size_t n = ATOMIC_BITMAP_SIZE(CONFIG_CAF_MODULES_FLAGS_COUNT); n > 0; --n) {
133 if (atomic_get(target++) != 0) {
134 return false;
135 }
136 }
137 return true;
138}
139
149static inline bool module_flags_test_bit(const struct module_flags *mf, size_t module_idx)
150{
151 return atomic_test_bit(mf->f, module_idx);
152}
153
160static inline void module_flags_clear_bit(struct module_flags *mf, size_t module_idx)
161{
162 atomic_clear_bit(mf->f, module_idx);
163}
164
171static inline void module_flags_set_bit(struct module_flags *mf, size_t module_idx)
172{
173 atomic_set_bit(mf->f, module_idx);
174}
175
183static inline void module_flags_set_bit_to(struct module_flags *mf, size_t module_idx, bool val)
184{
185 atomic_set_bit_to(mf->f, module_idx, val);
186}
187
194
199
204
207
210
212 APP_EM_ENFORCE_ENUM_SIZE(MODULE_STATE)
214
234 struct app_event_header header;
235
237 const void *module_id;
238
241};
242
244
245
259static inline bool check_state(const struct module_state_event *event,
260 const void *module_id, enum module_state state)
261{
262 if ((event->module_id == module_id) && (event->state == state)) {
263 return true;
264 }
265 return false;
266}
267
268
277#define MODULE_ID(mname) ({ \
278 MODULE_ID_PTR_VAR_EXTERN_DEC(mname); \
279 &MODULE_ID_PTR_VAR(mname); \
280 })
281
282
283#ifdef __cplusplus
284}
285#endif
286
287#endif /* _MODULE_STATE_EVENT_H_ */
288
289/* Declare elements required in the module C file,
290 * even if the file was already included before.
291 */
292#if defined(MODULE) && !defined(MODULE_NAME)
293
294#ifdef __cplusplus
295extern "C" {
296#endif
297
298#define MODULE_NAME STRINGIFY(MODULE)
299
301const void * const MODULE_ID_PTR_VAR(MODULE)
302 __used __attribute__((__section__(MODULE_ID_LIST_SECTION_NAME)))
303 = MODULE_NAME;
304
305
312static inline void module_set_state(enum module_state state)
313{
314 __ASSERT_NO_MSG(state < MODULE_STATE_COUNT);
315
316 struct module_state_event *event = new_module_state_event();
317
318 event->module_id = MODULE_ID(MODULE);
319 event->state = state;
320 APP_EVENT_SUBMIT(event);
321}
322
323#ifdef __cplusplus
324}
325#endif
326
331#endif /* defined(MODULE) && !defined(MODULE_NAME) */
Application Event Manager header.
Application Event Manager profiler tracer header.
#define APP_EVENT_TYPE_DECLARE(ename)
Declare an event type.
Definition: app_event_manager.h:143
#define APP_EVENT_SUBMIT(event)
Submit an event.
Definition: app_event_manager.h:198
#define MODULE_ID_PTR_VAR(mname)
Definition: module_state_event.h:28
#define MODULE_NAME
Definition: module_state_event.h:298
static void module_flags_set_bit_to(struct module_flags *mf, size_t module_idx, bool val)
Set single module bit to specified value.
Definition: module_state_event.h:183
module_state
Definition: module_state_event.h:189
@ MODULE_STATE_OFF
Definition: module_state_event.h:198
@ MODULE_STATE_STANDBY
Definition: module_state_event.h:203
@ MODULE_STATE_COUNT
Definition: module_state_event.h:209
@ APP_EM_ENFORCE_ENUM_SIZE
Definition: module_state_event.h:212
@ MODULE_STATE_READY
Definition: module_state_event.h:193
@ MODULE_STATE_ERROR
Definition: module_state_event.h:206
static const char * module_name_get(const void *id)
Get name of the module with given id.
Definition: module_state_event.h:80
static size_t module_idx_get(const void *module_id)
Get IDX of module with given id.
Definition: module_state_event.h:68
static size_t module_count(void)
Get number of modules.
Definition: module_state_event.h:42
static bool module_flags_check_zero(const struct module_flags *mf)
Check if there is 0 in all the flags.
Definition: module_state_event.h:128
#define MODULE_ID(mname)
Get module ID.
Definition: module_state_event.h:277
#define MODULE_ID_PTR_VAR_EXTERN_DEC(mname)
Definition: module_state_event.h:30
static void module_flags_set_bit(struct module_flags *mf, size_t module_idx)
Set single module bit.
Definition: module_state_event.h:171
static const void * module_id_get(size_t idx)
Get ID of module with given index.
Definition: module_state_event.h:54
static bool module_check_id_valid(size_t module_idx)
Check if given module index is valid.
Definition: module_state_event.h:115
static void module_flags_clear_bit(struct module_flags *mf, size_t module_idx)
Clear single module bit.
Definition: module_state_event.h:160
static bool check_state(const struct module_state_event *event, const void *module_id, enum module_state state)
Check if the selected module reported the selected state.
Definition: module_state_event.h:259
static void module_set_state(enum module_state state)
Submit module state event to inform that state of the module changed.
Definition: module_state_event.h:312
static bool module_flags_test_bit(const struct module_flags *mf, size_t module_idx)
Test single module bit.
Definition: module_state_event.h:149
#define MODULE_ID_LIST_SECTION_NAME
Definition: module_state_event.h:29
atomic_t f[ATOMIC_BITMAP_SIZE(CONFIG_CAF_MODULES_FLAGS_COUNT)]
Definition: module_state_event.h:104
Structure that provides a flag for every module available in application.
Definition: module_state_event.h:103
struct app_event_header header
Definition: module_state_event.h:234
const void * module_id
Definition: module_state_event.h:237
enum module_state state
Definition: module_state_event.h:240
Module state event.
Definition: module_state_event.h:232
enum state_type state