Commit | Line | Data |
---|---|---|
34ac9eaf JG |
1 | /* |
2 | * filter.c | |
3 | * | |
4 | * Babeltrace Filter Component | |
5 | * | |
6 | * Copyright 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
7 | * | |
8 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to deal | |
12 | * in the Software without restriction, including without limitation the rights | |
13 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
14 | * copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
25 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
26 | * SOFTWARE. | |
27 | */ | |
28 | ||
29 | #include <babeltrace/compiler.h> | |
30 | #include <babeltrace/values.h> | |
31 | #include <babeltrace/plugin/filter-internal.h> | |
32 | #include <babeltrace/plugin/component-internal.h> | |
33 | #include <babeltrace/plugin/notification/notification.h> | |
34a9ed19 | 34 | #include <babeltrace/plugin/notification/iterator-internal.h> |
34ac9eaf | 35 | |
34a9ed19 JG |
36 | enum bt_component_status bt_component_filter_set_iterator_init_cb( |
37 | struct bt_component *component, | |
38 | bt_component_filter_init_iterator_cb init_iterator) | |
39 | { | |
40 | struct bt_component_filter *filter; | |
41 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
34ac9eaf | 42 | |
34a9ed19 JG |
43 | if (component->class->type != BT_COMPONENT_TYPE_FILTER || |
44 | !component->initializing) { | |
45 | ret = BT_COMPONENT_STATUS_INVALID; | |
46 | goto end; | |
47 | } | |
48 | ||
49 | filter = container_of(component, struct bt_component_filter, parent); | |
50 | filter->init_iterator = init_iterator; | |
51 | end: | |
52 | return ret; | |
53 | } | |
54 | ||
55 | enum bt_component_status bt_component_filter_set_add_iterator_cb( | |
56 | struct bt_component *component, | |
57 | bt_component_filter_add_iterator_cb add_iterator) | |
58 | { | |
59 | struct bt_component_filter *filter; | |
60 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
61 | ||
62 | if (!component) { | |
63 | ret = BT_COMPONENT_STATUS_INVALID; | |
64 | goto end; | |
65 | } | |
66 | ||
67 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_FILTER) { | |
68 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; | |
69 | goto end; | |
70 | } | |
71 | ||
72 | if (!component->initializing) { | |
73 | ret = BT_COMPONENT_STATUS_INVALID; | |
74 | goto end; | |
75 | } | |
76 | ||
77 | filter = container_of(component, struct bt_component_filter, parent); | |
78 | filter->add_iterator = add_iterator; | |
79 | end: | |
80 | return ret; | |
81 | } | |
82 | ||
83 | enum bt_component_status bt_component_filter_set_minimum_input_count( | |
84 | struct bt_component *component, | |
85 | unsigned int minimum) | |
86 | { | |
87 | struct bt_component_filter *filter; | |
88 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
89 | ||
90 | if (!component) { | |
91 | ret = BT_COMPONENT_STATUS_INVALID; | |
92 | goto end; | |
93 | } | |
94 | ||
95 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_FILTER) { | |
96 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; | |
97 | goto end; | |
98 | } | |
99 | ||
100 | if (!component->initializing) { | |
101 | ret = BT_COMPONENT_STATUS_INVALID; | |
102 | goto end; | |
103 | } | |
104 | ||
105 | filter = container_of(component, struct bt_component_filter, parent); | |
106 | filter->input.min_count = minimum; | |
107 | end: | |
108 | return ret; | |
109 | } | |
110 | ||
111 | enum bt_component_status bt_component_filter_set_maximum_input_count( | |
112 | struct bt_component *component, | |
113 | unsigned int maximum) | |
114 | { | |
115 | struct bt_component_filter *filter; | |
116 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
117 | ||
118 | if (!component) { | |
119 | ret = BT_COMPONENT_STATUS_INVALID; | |
120 | goto end; | |
121 | } | |
122 | ||
123 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_FILTER) { | |
124 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; | |
125 | goto end; | |
126 | } | |
127 | ||
128 | if (!component->initializing) { | |
129 | ret = BT_COMPONENT_STATUS_INVALID; | |
130 | goto end; | |
131 | } | |
132 | ||
133 | filter = container_of(component, struct bt_component_filter, parent); | |
134 | filter->input.max_count = maximum; | |
135 | end: | |
136 | return ret; | |
137 | } | |
138 | ||
139 | enum bt_component_status | |
140 | bt_component_filter_get_input_count(struct bt_component *component, | |
141 | unsigned int *count) | |
142 | { | |
143 | struct bt_component_filter *filter; | |
144 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
145 | ||
146 | if (!component || !count) { | |
147 | ret = BT_COMPONENT_STATUS_INVALID; | |
148 | goto end; | |
149 | } | |
150 | ||
151 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_FILTER) { | |
152 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; | |
153 | goto end; | |
154 | } | |
155 | ||
156 | filter = container_of(component, struct bt_component_filter, parent); | |
157 | *count = (unsigned int) filter->input.iterators->len; | |
158 | end: | |
159 | return ret; | |
160 | } | |
161 | ||
162 | enum bt_component_status | |
163 | bt_component_filter_get_input_iterator(struct bt_component *component, | |
164 | unsigned int input, struct bt_notification_iterator **iterator) | |
165 | { | |
166 | struct bt_component_filter *filter; | |
167 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
168 | ||
169 | if (!component || !iterator) { | |
170 | ret = BT_COMPONENT_STATUS_INVALID; | |
171 | goto end; | |
172 | } | |
173 | ||
174 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_FILTER) { | |
175 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; | |
176 | goto end; | |
177 | } | |
178 | ||
179 | filter = container_of(component, struct bt_component_filter, parent); | |
180 | if (input >= (unsigned int) filter->input.iterators->len) { | |
181 | ret = BT_COMPONENT_STATUS_INVALID; | |
182 | goto end; | |
183 | } | |
184 | ||
185 | *iterator = bt_get(g_ptr_array_index(filter->input.iterators, input)); | |
186 | end: | |
187 | return ret; | |
188 | } | |
189 | ||
190 | enum bt_component_status bt_component_filter_add_iterator( | |
191 | struct bt_component *component, | |
192 | struct bt_notification_iterator *iterator) | |
193 | { | |
194 | struct bt_component_filter *filter; | |
195 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; | |
196 | ||
197 | if (!component || !iterator) { | |
198 | ret = BT_COMPONENT_STATUS_INVALID; | |
199 | goto end; | |
200 | } | |
201 | ||
202 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_FILTER) { | |
203 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; | |
204 | goto end; | |
205 | } | |
206 | ||
207 | filter = container_of(component, struct bt_component_filter, parent); | |
208 | if (filter->input.iterators->len == filter->input.max_count) { | |
209 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; | |
210 | goto end; | |
211 | } | |
212 | ||
213 | if (filter->add_iterator) { | |
214 | ret = filter->add_iterator(component, iterator); | |
215 | if (ret != BT_COMPONENT_STATUS_OK) { | |
216 | goto end; | |
217 | } | |
218 | } | |
219 | ||
220 | g_ptr_array_add(filter->input.iterators, bt_get(iterator)); | |
221 | end: | |
222 | return ret; | |
223 | } | |
224 | ||
225 | struct bt_notification_iterator *bt_component_filter_create_iterator( | |
226 | struct bt_component *component) | |
227 | { | |
228 | enum bt_component_status ret_component; | |
229 | enum bt_notification_iterator_status ret_iterator; | |
230 | struct bt_component_filter *filter; | |
231 | struct bt_notification_iterator *iterator = NULL; | |
232 | ||
233 | if (!component) { | |
234 | goto end; | |
235 | } | |
236 | ||
237 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_FILTER) { | |
238 | goto end; | |
239 | } | |
240 | ||
241 | iterator = bt_notification_iterator_create(component); | |
242 | if (!iterator) { | |
243 | goto end; | |
244 | } | |
245 | ||
246 | filter = container_of(component, struct bt_component_filter, parent); | |
247 | assert(filter->init_iterator); | |
248 | ret_component = filter->init_iterator(component, iterator); | |
249 | if (ret_component != BT_COMPONENT_STATUS_OK) { | |
250 | goto error; | |
251 | } | |
252 | ||
253 | ret_iterator = bt_notification_iterator_validate(iterator); | |
254 | if (ret_iterator != BT_NOTIFICATION_ITERATOR_STATUS_OK) { | |
255 | goto error; | |
256 | } | |
257 | end: | |
258 | return iterator; | |
259 | error: | |
260 | BT_PUT(iterator); | |
261 | return iterator; | |
262 | } |