forked from nrfconnect/sdk-nrf
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent_manager.h
More file actions
307 lines (238 loc) · 7.46 KB
/
Copy pathevent_manager.h
File metadata and controls
307 lines (238 loc) · 7.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
* Copyright (c) 2018 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
*/
/** @file
* @brief Event manager header.
*/
#ifndef _EVENT_MANAGER_H_
#define _EVENT_MANAGER_H_
/**
* @defgroup event_manager Event Manager
* @brief Event Manager
*
* @{
*/
#include <zephyr.h>
#include <zephyr/types.h>
#include <power/reboot.h>
#include <sys/__assert.h>
#include <logging/log_ctrl.h>
#include <event_manager_priv.h>
#include <profiler.h>
#ifdef __cplusplus
extern "C" {
#endif
/** @def SUBS_PRIO_MIN
*
* @brief Index of the highest subscriber priority level.
*/
#define SUBS_PRIO_MIN _SUBS_PRIO_FIRST
/** @def SUBS_PRIO_MAX
*
* @brief Index of the lowest subscriber priority level.
*/
#define SUBS_PRIO_MAX _SUBS_PRIO_FINAL
/** @def SUBS_PRIO_COUNT
*
* @brief Number of subscriber priority levels.
*/
#define SUBS_PRIO_COUNT (SUBS_PRIO_MAX - SUBS_PRIO_MIN + 1)
/** @brief Event header.
*
* When defining an event structure, the event header
* must be placed as the first field.
*/
struct event_header {
/** Linked list node used to chain events. */
sys_snode_t node;
/** Pointer to the event type object. */
const struct event_type *type_id;
};
/** @brief Dynamic event data.
*
* When defining an event structure, the dynamic event data
* must be placed as the last field.
*/
struct event_dyndata {
/** Size of the dynamic data. */
size_t size;
/** Dynamic data. */
u8_t data[0];
};
/** @brief Event listener.
*
* All event listeners must be defined using @ref EVENT_LISTENER.
*/
struct event_listener {
/** Name of this listener. */
const char *name;
/** Pointer to the function that is called when an event
* is handled. */
bool (*notification)(const struct event_header *eh);
};
/** @brief Event subscriber.
*/
struct event_subscriber {
/** Pointer to the listener. */
const struct event_listener *listener;
};
/** @brief Event description for profiling or logging.
*/
struct event_info {
/** Function for profiling this event. */
void (*profile_fn)(struct log_event_buf *buf,
const struct event_header *eh);
/** Number of logged data fields. */
const u8_t log_arg_cnt;
/** Labels of logged data fields. */
const char **log_arg_labels;
/** Types of logged data fields. */
const enum profiler_arg *log_arg_types;
};
/** @brief Event type.
*/
struct event_type {
/** Event name. */
const char *name;
/** Array of pointers to the array of subscribers. */
const struct event_subscriber *subs_start[SUBS_PRIO_COUNT];
/** Array of pointers to the element directly after the array of
* subscribers. */
const struct event_subscriber *subs_stop[SUBS_PRIO_COUNT];
/** Bool indicating if the event is logged by default. */
bool init_log_enable;
/** Function to log data from this event. */
int (*log_event)(const struct event_header *eh, char *buf,
size_t buf_len);
/** Logging and formatting information. */
const struct event_info *ev_info;
};
extern const struct event_listener __start_event_listeners[];
extern const struct event_listener __stop_event_listeners[];
extern const struct event_type __start_event_types[];
extern const struct event_type __stop_event_types[];
/** Create an event listener object.
*
* @param lname Module name.
* @param cb_fn Pointer to the event handler function.
*/
#define EVENT_LISTENER(lname, cb_fn) _EVENT_LISTENER(lname, cb_fn)
/** Subscribe a listener to the early notification list for an
* event type.
*
* @param lname Name of the listener.
* @param ename Name of the event.
*/
#define EVENT_SUBSCRIBE_EARLY(lname, ename) \
_EVENT_SUBSCRIBE(lname, ename, _SUBS_PRIO_ID(_SUBS_PRIO_FIRST))
/** Subscribe a listener to the normal notification list for an event
* type.
*
* @param lname Name of the listener.
* @param ename Name of the event.
*/
#define EVENT_SUBSCRIBE(lname, ename) \
_EVENT_SUBSCRIBE(lname, ename, _SUBS_PRIO_ID(_SUBS_PRIO_NORMAL))
/** Subscribe a listener to an event type as final module that is
* being notified.
*
* @param lname Name of the listener.
* @param ename Name of the event.
*/
#define EVENT_SUBSCRIBE_FINAL(lname, ename) \
_EVENT_SUBSCRIBE(lname, ename, _SUBS_PRIO_ID(_SUBS_PRIO_FINAL)); \
const struct {} _CONCAT(_CONCAT(__event_subscriber_, ename), final_sub_redefined) = {}
/** Encode event data types or labels.
*
* @param ... Data types or labels to be encoded.
*/
#define ENCODE(...) __VA_ARGS__
/** Define event profiling information.
*
* This macro provides definitions required for an event to be profiled.
*
* @note Types and labels of the profiled values should be wrapped
* with the @ref ENCODE macro.
*
* @param ename Name of the event.
* @param types Types of values to profile (represented as @ref profiler_arg).
* @param labels Labels of values to profile.
* @param log_arg_func Function used to profile event data.
*/
#define EVENT_INFO_DEFINE(ename, types, labels, profile_func) \
_EVENT_INFO_DEFINE(ename, ENCODE(types), ENCODE(labels), profile_func)
/** Declare an event type.
*
* This macro provides declarations required for an event to be used
* by other modules.
*
* @param ename Name of the event.
*/
#define EVENT_TYPE_DECLARE(ename) _EVENT_TYPE_DECLARE(ename)
/** Declare an event type with dynamic data size.
*
* This macro provides declarations required for an event to be used
* by other modules.
* Declared event will use dynamic data.
*
* @param ename Name of the event.
*/
#define EVENT_TYPE_DYNDATA_DECLARE(ename) _EVENT_TYPE_DYNDATA_DECLARE(ename)
/** Define an event type.
*
* This macro defines an event type. In addition, it defines functions
* specific to the event type and the event type structure.
*
* For every defined event, the following functions are created, where
* <i>%event_type</i> is replaced with the given event type name @p ename
* (for example, button_event):
* - new_<i>%event_type</i> - Allocates an event of a given type.
* - is_<i>%event_type</i> - Checks if the event header that is provided
* as argument represents the given event type.
* - cast_<i>%event_type</i> - Casts the event header that is provided
* as argument to an event of the given type.
*
* @param ename Name of the event.
* @param init_log_en Bool indicating if the event is logged
* by default.
* @param log_fn Function to stringify an event of this type.
* @param ev_info_struct Data structure describing the event type.
*/
#define EVENT_TYPE_DEFINE(ename, init_log_en, log_fn, ev_info_struct) \
_EVENT_TYPE_DEFINE(ename, init_log_en, log_fn, ev_info_struct)
/** Verify if an event ID is valid.
*
* The pointer to an event type structure is used as its ID. This macro
* validates that the provided pointer is within the range where event
* type structures are defined.
*
* @param id ID.
*/
#define ASSERT_EVENT_ID(id) \
__ASSERT_NO_MSG((id >= __start_event_types) && (id < __stop_event_types))
/** Submit an event to the Event Manager.
*
* @param eh Pointer to the event header element in the event object.
*/
void _event_submit(struct event_header *eh);
/** Submit an event.
*
* This helper macro simplifies the event submission.
*
* @param event Pointer to the event object.
*/
#define EVENT_SUBMIT(event) _event_submit(&event->header)
/** Initialize the Event Manager.
*
* @retval 0 If the operation was successful.
*/
int event_manager_init(void);
#ifdef __cplusplus
}
#endif
/**
* @}
*/
#endif /* _EVENT_MANAGER_H_ */