8
8
9
9
LOG_MODULE_DECLARE (zbus , CONFIG_ZBUS_LOG_LEVEL );
10
10
11
+ #if defined(CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_ALLOC_DYNAMIC )
12
+
13
+ static inline int _zbus_runtime_observer_node_alloc (struct zbus_observer_node * * node ,
14
+ k_timeout_t timeout )
15
+ {
16
+ ARG_UNUSED (timeout );
17
+
18
+ * node = k_malloc (sizeof (struct zbus_observer_node ));
19
+
20
+ _ZBUS_ASSERT (* node != NULL , "could not allocate observer node the heap is full!" );
21
+
22
+ if (* node == NULL ) {
23
+ return - ENOMEM ;
24
+ }
25
+
26
+ return 0 ;
27
+ }
28
+
29
+ static inline void _zbus_runtime_observer_node_free (struct zbus_observer_node * node )
30
+ {
31
+ k_free (node );
32
+ }
33
+ #else
34
+
35
+ K_MEM_SLAB_DEFINE_STATIC (_zbus_runtime_observers_slab , sizeof (struct zbus_observer_node ),
36
+ CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_POOL_SIZE , 8 );
37
+
38
+ static inline int _zbus_runtime_observer_node_alloc (struct zbus_observer_node * * node ,
39
+ k_timeout_t timeout )
40
+ {
41
+ int err = k_mem_slab_alloc (& _zbus_runtime_observers_slab , (void * * )node , timeout );
42
+
43
+ _ZBUS_ASSERT (* node != NULL , "not enough runtime observer nodes in the pool. Increase the "
44
+ "ZBUS_RUNTIME_OBSERVERS_NODE_POOL_SIZE" );
45
+
46
+ return err ;
47
+ }
48
+
49
+ static inline void _zbus_runtime_observer_node_free (struct zbus_observer_node * node )
50
+ {
51
+ k_mem_slab_free (& _zbus_runtime_observers_slab , (void * )node );
52
+ }
53
+ #endif /* CONFIG_ZBUS_RUNTIME_OBSERVERS_NODE_ALLOC_DYNAMIC */
54
+
11
55
int zbus_chan_add_obs (const struct zbus_channel * chan , const struct zbus_observer * obs ,
12
- struct zbus_observer_node * node , k_timeout_t timeout )
56
+ k_timeout_t timeout )
13
57
{
14
58
int err ;
59
+
15
60
struct zbus_observer_node * obs_nd , * tmp ;
16
61
struct zbus_channel_observation * observation ;
17
62
18
63
_ZBUS_ASSERT (!k_is_in_isr (), "ISR blocked" );
19
64
_ZBUS_ASSERT (chan != NULL , "chan is required" );
20
65
_ZBUS_ASSERT (obs != NULL , "obs is required" );
21
- _ZBUS_ASSERT (node != NULL , "node is required" );
66
+
67
+ k_timepoint_t end_time = sys_timepoint_calc (timeout );
22
68
23
69
err = k_sem_take (& chan -> data -> sem , timeout );
24
70
if (err ) {
@@ -47,9 +93,19 @@ int zbus_chan_add_obs(const struct zbus_channel *chan, const struct zbus_observe
47
93
}
48
94
}
49
95
50
- node -> obs = obs ;
96
+ struct zbus_observer_node * new_obs_nd = NULL ;
51
97
52
- sys_slist_append (& chan -> data -> observers , & node -> node );
98
+ err = _zbus_runtime_observer_node_alloc (& new_obs_nd , sys_timepoint_timeout (end_time ));
99
+
100
+ if (err ) {
101
+ k_sem_give (& chan -> data -> sem );
102
+
103
+ return err ;
104
+ }
105
+
106
+ new_obs_nd -> obs = obs ;
107
+
108
+ sys_slist_append (& chan -> data -> observers , & new_obs_nd -> node );
53
109
54
110
k_sem_give (& chan -> data -> sem );
55
111
@@ -76,6 +132,8 @@ int zbus_chan_rm_obs(const struct zbus_channel *chan, const struct zbus_observer
76
132
if (obs_nd -> obs == obs ) {
77
133
sys_slist_remove (& chan -> data -> observers , & prev_obs_nd -> node , & obs_nd -> node );
78
134
135
+ _zbus_runtime_observer_node_free (obs_nd );
136
+
79
137
k_sem_give (& chan -> data -> sem );
80
138
81
139
return 0 ;
0 commit comments