|
3 | 3 | <br />
|
4 | 4 |
|
5 | 5 | Callbacks
|
6 |
| -================ |
| 6 | +========= |
7 | 7 |
|
8 |
| -Every module needs 5 functions that must be defined by developer. |br| |
9 |
| -If using :ref:`module_easy`, they are automatically declared by MODULE macro. |br| |
10 |
| -Moreover, a module_pre_start function is declared too, but it is not needed by libmodule interface, ie: it can be left undefined. Your compiler may warn you about that though. |br| |
11 |
| -When using :ref:`module_complex` API, libmodule only mandates init() and receive() callbacks. A NULL check() and evaluate() functions mean to avoid checking and evaluating and thus starting module right away. |
| 8 | +Every module requires following functions to be defined by developers. |br| |
| 9 | +If using :ref:`easy` API, they are automatically declared by M_MOD() macro. |br| |
| 10 | +Moreover, a m_mod_pre_start function is declared too, but it is not needed by libmodule interface, ie: it can be left undefined. Your compiler may warn you about that though. |br| |
| 11 | +Note that libmodule only mandates m_evt_cb() callback. |br| |
| 12 | +Leaving other callbacks as NULL means starting module right away with no further checks. |br| |
12 | 13 |
|
13 | 14 | .. code::
|
14 | 15 |
|
15 |
| - static void module_pre_start(void); |
16 |
| - static bool init(void); |
17 |
| - static bool check(void); |
18 |
| - static bool evaluate(void); |
19 |
| - static void receive(const msg_t *const msg, const void *userdata); |
20 |
| - static void destroy(void); |
| 16 | + void m_mod_pre_start(void); |
| 17 | + bool m_start_cb(m_mod_t *mod); |
| 18 | + bool m_eval_cb(m_mod_t *mod); |
| 19 | + void m_evt_cb(m_mod_t *mod, const m_evt_t *const evt); |
| 20 | + void m_stop_cb(m_mod_t *mod); |
21 | 21 |
|
22 |
| -.. c:function:: module_pre_start(void) |
| 22 | +.. c:function:: m_mod_pre_start(void) |
23 | 23 |
|
24 | 24 | This function will be called before any module is registered. |br|
|
25 |
| - It is the per-module version of :ref:`modules_pre_start <modules_pre_start>` function. |
| 25 | + It is the per-module version of :ref:`m_ctx_pre_start <m_ctx_pre_start>` function. |
26 | 26 |
|
27 |
| -.. c:function:: init(void) |
| 27 | +.. c:function:: m_start_cb(mod) |
28 | 28 |
|
29 |
| - Initializes module state; useful to register any fd to be polled or to register any topic. |br| |
30 |
| - Note that init() is called each time module is started. |br| |
31 |
| - If init callback returns false, module is automatically stopped as its initialization failed. |
| 29 | + Initializes module state; useful to register any event source. |br| |
| 30 | + It is called whenever module is started. |br| |
| 31 | + If m_mod_on_start callback returns false, module is automatically stopped as its initialization failed. |
32 | 32 |
|
33 |
| -.. c:function:: check(void) |
| 33 | + :param: :c:type:`m_mod_t *` mod: pointer to module. |
34 | 34 |
|
35 |
| - Startup filter to check whether this module should be actually created and managed by libmodule. |br| |
36 |
| - |
37 |
| - :returns: true if the module should be registered, false otherwise. |
| 35 | + :returns: true if the module should be started, false otherwise. |
38 | 36 |
|
39 |
| -.. c:function:: evaluate(void) |
| 37 | +.. c:function:: m_eval_cb(mod) |
40 | 38 |
|
41 |
| - Similar to check() function but at runtime: this function is called for each IDLE module after evey state machine update |
42 |
| - and it should check whether a module is now ready to be start (ie: init should be called on this module and its state should be set to RUNNING). |br| |
43 |
| - Use this to check intra-modules dependencies or any other env variable. |
44 |
| - |
45 |
| - :returns: true if module is now ready to be started, else false. |
46 |
| - |
47 |
| -.. c:function:: receive(msg, userdata) |
| 39 | + This function is called for each IDLE module after evey state machine update, |br| |
| 40 | + and it should check whether a module is now ready to be started. |br| |
| 41 | + |
| 42 | + :param: :c:type:`m_mod_t *` mod: pointer to module. |
48 | 43 |
|
49 |
| - Poll callback, called when any event is ready on module's fd or when a PubSub message is received by a module. |br| |
50 |
| - Use msg->is_pubsub to decide which internal message should be read (ie: ps_msg_t or fd_msg_t). |
| 44 | + :returns: true if module is now ready to be started, false otherwise. |
51 | 45 |
|
52 |
| - :param: :c:type:`const msg_t * const` msg: pointer to msg_t struct. |
53 |
| - :param: :c:type:`const void *` userdata: pointer to userdata as set by m_set_userdata. |
| 46 | +.. c:function:: m_evt_cb(mod, evt) |
| 47 | +
|
| 48 | + Poll callback, called when any event is ready to be received by a module. |br| |
| 49 | + Use evt->type to establish which event source triggered the event. |br| |
| 50 | + Note that evt is memory-ref'd. Thus, if you want to keep a message alive, you need to m_mem_ref() it. |br| |
| 51 | + Remember to unref it when done or you will cause a leak. |br| |
| 52 | + |
| 53 | + :param: :c:type:`m_mod_t *` mod: pointer to module. |
| 54 | + :param: :c:type:`const m_evt_t * const` evt: pointer to event. |
| 55 | + |
| 56 | +.. c:function:: m_stop_cb(mod) |
54 | 57 |
|
55 |
| -.. c:function:: destroy(void) |
| 58 | + Called whenever module gets stopped, either by deregistration or m_mod_stop(). |br| |
| 59 | + Useful to cleanup module state, eg: freeing some data or closing fds registered without M_SRC_FD_AUTOCLOSE flag. |br| |
56 | 60 |
|
57 |
| - Destroys module, called automatically at module deregistration. Please note that module's fds set as autoclose will be closed. |
| 61 | + :param: :c:type:`m_mod_t *` mod: pointer to module. |
0 commit comments