@@ -848,29 +848,36 @@ 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, a memory slab to allocate the nodes statically, or user-provided
854
+ nodes. It depends on the :kconfig:option: `CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_ALLOC `, which can be
855
+ :kconfig:option: `CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_ALLOC_DYNAMIC `,
856
+ :kconfig:option: `CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_ALLOC_STATIC `, and
857
+ :kconfig:option: `CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_ALLOC_NONE `. The dynamic is the default. 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.
856
862
857
863
.. code-block :: c
858
864
859
865
ZBUS_LISTENER_DEFINE(my_listener, callback);
860
866
// ...
861
867
void thread_entry(void) {
862
- static struct zbus_observer_node obs_node;
863
868
// ...
864
869
/* Adding the observer to channel chan1 */
865
- zbus_chan_add_obs(&chan1, &my_listener, &obs_node, K_NO_WAIT);
870
+ zbus_chan_add_obs(&chan1, &my_listener, K_NO_WAIT);
866
871
/* Removing the observer from channel chan1 */
867
872
zbus_chan_rm_obs(&chan1, &my_listener, K_NO_WAIT);
868
873
874
+
869
875
.. warning ::
870
876
871
- The :c:struct: `zbus_observer_node ` can only be re-used in :c:func: `zbus_chan_add_obs ` after removing
877
+ The :c:struct: `zbus_observer_node ` can only be re-used in :c:func: `zbus_chan_add_obs_with_node ` after removing
872
878
the channel observer it was first associated with through :c:func: `zbus_chan_rm_obs `.
873
879
880
+
874
881
Samples
875
882
*******
876
883
@@ -936,7 +943,15 @@ Related configuration options:
936
943
a pool for the message subscriber for a set of channels;
937
944
* :kconfig:option: `CONFIG_ZBUS_MSG_SUBSCRIBER_NET_BUF_STATIC_DATA_SIZE ` the biggest message of zbus
938
945
channels to be transported into a message buffer;
939
- * :kconfig:option: `CONFIG_ZBUS_RUNTIME_OBSERVERS ` enables the runtime observer registration.
946
+ * :kconfig:option: `CONFIG_ZBUS_RUNTIME_OBSERVERS ` enables the runtime observer registration;
947
+ * :kconfig:option: `CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_ALLOC_DYNAMIC ` allocate the runtime observers
948
+ dynamically using the heap;
949
+ * :kconfig:option: `CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_ALLOC_STATIC ` allocate the runtime observers
950
+ statically using a memory slab;
951
+ * :kconfig:option: `CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_POOL_SIZE ` the amount of enabled runtime
952
+ observers to statically allocate.
953
+ * :kconfig:option: `CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_ALLOC_NONE ` use user-provided runtime
954
+ observers nodes;
940
955
941
956
API Reference
942
957
*************
0 commit comments