@@ -848,25 +848,28 @@ The following code has the exact behavior of the code in :ref:`reading from a ch
848
848
Runtime observer registration
849
849
-----------------------------
850
850
851
- It is possible to add observers to channels in runtime. This feature uses the heap to allocate the
852
- nodes dynamically. The heap size limits the number of dynamic observers zbus can create. Therefore,
853
- set the :kconfig:option: `CONFIG_ZBUS_RUNTIME_OBSERVERS ` to enable the feature. It is possible to
854
- adjust the heap size by changing the configuration :kconfig:option: `CONFIG_HEAP_MEM_POOL_SIZE `. The
855
- following example illustrates the runtime registration usage.
856
-
857
-
851
+ It is possible to add observers to channels at runtime if
852
+ :kconfig:option: `CONFIG_ZBUS_RUNTIME_OBSERVERS ` is enabled. In addition to the channel and observer
853
+ references, :c:func: `zbus_chan_add_obs ` also requires a :c:struct: `zbus_observer_node ` to link the two
854
+ together, which must remain valid in memory for the duration that the observer is attached to the
855
+ channel. The simplest way to achieve this is to make the structure ``static ``.
858
856
859
857
.. code-block :: c
860
858
861
859
ZBUS_LISTENER_DEFINE(my_listener, callback);
862
860
// ...
863
861
void thread_entry(void) {
862
+ static struct zbus_observer_node obs_node;
864
863
// ...
865
864
/* Adding the observer to channel chan1 */
866
- zbus_chan_add_obs(&chan1, &my_listener, K_NO_WAIT);
865
+ zbus_chan_add_obs(&chan1, &my_listener, &obs_node, K_NO_WAIT);
867
866
/* Removing the observer from channel chan1 */
868
867
zbus_chan_rm_obs(&chan1, &my_listener, K_NO_WAIT);
869
868
869
+ .. warning ::
870
+
871
+ The :c:struct: `zbus_observer_node ` can only be re-used in :c:func: `zbus_chan_add_obs ` after removing
872
+ the channel observer it was first associated with through :c:func: `zbus_chan_rm_obs `.
870
873
871
874
Samples
872
875
*******
0 commit comments