@@ -848,28 +848,30 @@ 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 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 ``.
851
+ It is possible to add observers to channels in runtime. Set the
852
+ :kconfig:option: `CONFIG_ZBUS_RUNTIME_OBSERVERS ` to enable the feature. This feature uses the heap to
853
+ allocate the nodes dynamically or a memory slab to allocate the nodes statically. It depends on the
854
+ :kconfig:option: `CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_ALLOC ` which can be
855
+ :kconfig:option: `CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_ALLOC_DYNAMIC ` and
856
+ :kconfig:option: `CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_ALLOC_STATIC `. When the heap is enabled, the
857
+ dynamic one is automatically chosen. When
858
+ :kconfig:option: `CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_ALLOC_STATIC ` is enabled, you need to set the
859
+ number of runtime observers you are going to use by setting the
860
+ :kconfig:option: `CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_POOL_SIZE ` configuration. The following example
861
+ illustrates the runtime registration usage.
862
+
856
863
857
864
.. code-block :: c
858
865
859
866
ZBUS_LISTENER_DEFINE(my_listener, callback);
860
867
// ...
861
868
void thread_entry(void) {
862
- static struct zbus_observer_node obs_node;
863
869
// ...
864
870
/* Adding the observer to channel chan1 */
865
- zbus_chan_add_obs(&chan1, &my_listener, &obs_node, K_NO_WAIT);
871
+ zbus_chan_add_obs(&chan1, &my_listener, K_NO_WAIT);
866
872
/* Removing the observer from channel chan1 */
867
873
zbus_chan_rm_obs(&chan1, &my_listener, K_NO_WAIT);
868
874
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 `.
873
875
874
876
Samples
875
877
*******
@@ -936,7 +938,13 @@ Related configuration options:
936
938
a pool for the message subscriber for a set of channels;
937
939
* :kconfig:option: `CONFIG_ZBUS_MSG_SUBSCRIBER_NET_BUF_STATIC_DATA_SIZE ` the biggest message of zbus
938
940
channels to be transported into a message buffer;
939
- * :kconfig:option: `CONFIG_ZBUS_RUNTIME_OBSERVERS ` enables the runtime observer registration.
941
+ * :kconfig:option: `CONFIG_ZBUS_RUNTIME_OBSERVERS ` enables the runtime observer registration;
942
+ * :kconfig:option: `CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_ALLOC_STATIC ` allocate the runtime observers
943
+ statically using a memory slab;
944
+ * :kconfig:option: `CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_ALLOC_DYNAMIC ` allocate the runtime observers
945
+ dynamically using the heap;
946
+ * :kconfig:option: `CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_POOL_SIZE ` the amount of enabled runtime
947
+ observers to statically allocate.
940
948
941
949
API Reference
942
950
*************
0 commit comments