| 1 | /* |
| 2 | * sink.c |
| 3 | * |
| 4 | * Babeltrace Sink Component |
| 5 | * |
| 6 | * Copyright 2015 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/component/sink-internal.h> |
| 32 | #include <babeltrace/component/component-internal.h> |
| 33 | #include <babeltrace/component/notification/notification.h> |
| 34 | |
| 35 | BT_HIDDEN |
| 36 | enum bt_component_status bt_component_sink_validate( |
| 37 | struct bt_component *component) |
| 38 | { |
| 39 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 40 | struct bt_component_sink *sink; |
| 41 | |
| 42 | if (!component) { |
| 43 | ret = BT_COMPONENT_STATUS_INVALID; |
| 44 | goto end; |
| 45 | } |
| 46 | |
| 47 | if (!component->class) { |
| 48 | ret = BT_COMPONENT_STATUS_INVALID; |
| 49 | goto end; |
| 50 | } |
| 51 | |
| 52 | if (component->class->type != BT_COMPONENT_TYPE_SINK) { |
| 53 | ret = BT_COMPONENT_STATUS_INVALID; |
| 54 | goto end; |
| 55 | } |
| 56 | |
| 57 | sink = container_of(component, struct bt_component_sink, parent); |
| 58 | if (!sink->consume) { |
| 59 | printf_error("Invalid sink component; no notification consumption callback defined."); |
| 60 | ret = BT_COMPONENT_STATUS_INVALID; |
| 61 | goto end; |
| 62 | } |
| 63 | |
| 64 | ret = component_input_validate(&sink->input); |
| 65 | if (ret) { |
| 66 | goto end; |
| 67 | } |
| 68 | end: |
| 69 | return ret; |
| 70 | } |
| 71 | |
| 72 | static |
| 73 | void bt_component_sink_destroy(struct bt_component *component) |
| 74 | { |
| 75 | struct bt_component_sink *sink = container_of(component, |
| 76 | struct bt_component_sink, parent); |
| 77 | |
| 78 | component_input_fini(&sink->input); |
| 79 | } |
| 80 | |
| 81 | BT_HIDDEN |
| 82 | struct bt_component *bt_component_sink_create( |
| 83 | struct bt_component_class *class, struct bt_value *params) |
| 84 | { |
| 85 | struct bt_component_sink *sink = NULL; |
| 86 | enum bt_component_status ret; |
| 87 | |
| 88 | sink = g_new0(struct bt_component_sink, 1); |
| 89 | if (!sink) { |
| 90 | goto end; |
| 91 | } |
| 92 | |
| 93 | sink->parent.class = bt_get(class); |
| 94 | ret = bt_component_init(&sink->parent, bt_component_sink_destroy); |
| 95 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 96 | goto error; |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | ret = bt_component_sink_register_notification_type(&sink->parent, |
| 101 | BT_NOTIFICATION_TYPE_EVENT); |
| 102 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 103 | goto error; |
| 104 | } |
| 105 | */ |
| 106 | if (component_input_init(&sink->input)) { |
| 107 | goto error; |
| 108 | } |
| 109 | end: |
| 110 | return sink ? &sink->parent : NULL; |
| 111 | error: |
| 112 | BT_PUT(sink); |
| 113 | return NULL; |
| 114 | } |
| 115 | |
| 116 | static |
| 117 | enum bt_component_status validate_inputs(struct bt_component_sink *sink) |
| 118 | { |
| 119 | size_t array_size = sink->input.iterators->len; |
| 120 | |
| 121 | if (array_size < sink->input.min_count || |
| 122 | array_size > sink->input.max_count) { |
| 123 | return BT_COMPONENT_STATUS_INVALID; |
| 124 | } |
| 125 | |
| 126 | return BT_COMPONENT_STATUS_OK; |
| 127 | } |
| 128 | |
| 129 | enum bt_component_status bt_component_sink_consume( |
| 130 | struct bt_component *component) |
| 131 | { |
| 132 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 133 | struct bt_component_sink *sink = NULL; |
| 134 | |
| 135 | if (!component) { |
| 136 | ret = BT_COMPONENT_STATUS_INVALID; |
| 137 | goto end; |
| 138 | } |
| 139 | |
| 140 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_SINK) { |
| 141 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; |
| 142 | goto end; |
| 143 | } |
| 144 | |
| 145 | sink = container_of(component, struct bt_component_sink, parent); |
| 146 | if (!sink->input.validated) { |
| 147 | ret = validate_inputs(sink); |
| 148 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 149 | goto end; |
| 150 | } |
| 151 | sink->input.validated = true; |
| 152 | } |
| 153 | |
| 154 | assert(sink->consume); |
| 155 | ret = sink->consume(component); |
| 156 | end: |
| 157 | return ret; |
| 158 | } |
| 159 | /* |
| 160 | static |
| 161 | enum bt_component_status bt_component_sink_register_notification_type( |
| 162 | struct bt_component *component, enum bt_notification_type type) |
| 163 | { |
| 164 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 165 | struct bt_component_sink *sink = NULL; |
| 166 | |
| 167 | if (!component) { |
| 168 | ret = BT_COMPONENT_STATUS_INVALID; |
| 169 | goto end; |
| 170 | } |
| 171 | |
| 172 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_SINK) { |
| 173 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; |
| 174 | goto end; |
| 175 | } |
| 176 | |
| 177 | if (type <= BT_NOTIFICATION_TYPE_UNKNOWN || |
| 178 | type >= BT_NOTIFICATION_TYPE_NR) { |
| 179 | ret = BT_COMPONENT_STATUS_INVALID; |
| 180 | goto end; |
| 181 | } |
| 182 | sink = container_of(component, struct bt_component_sink, parent); |
| 183 | if (type == BT_NOTIFICATION_TYPE_ALL) { |
| 184 | sink->registered_notifications_mask = ~(notification_mask_t) 0; |
| 185 | } else { |
| 186 | sink->registered_notifications_mask |= |
| 187 | (notification_mask_t) 1 << type; |
| 188 | } |
| 189 | end: |
| 190 | return ret; |
| 191 | } |
| 192 | */ |
| 193 | enum bt_component_status bt_component_sink_set_consume_cb( |
| 194 | struct bt_component *component, |
| 195 | bt_component_sink_consume_cb consume) |
| 196 | { |
| 197 | struct bt_component_sink *sink; |
| 198 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 199 | |
| 200 | if (!component) { |
| 201 | ret = BT_COMPONENT_STATUS_INVALID; |
| 202 | goto end; |
| 203 | } |
| 204 | |
| 205 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_SINK) { |
| 206 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; |
| 207 | goto end; |
| 208 | } |
| 209 | |
| 210 | if (!component->initializing) { |
| 211 | ret = BT_COMPONENT_STATUS_INVALID; |
| 212 | goto end; |
| 213 | } |
| 214 | |
| 215 | sink = container_of(component, struct bt_component_sink, parent); |
| 216 | sink->consume = consume; |
| 217 | end: |
| 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | enum bt_component_status bt_component_sink_set_add_iterator_cb( |
| 222 | struct bt_component *component, |
| 223 | bt_component_sink_add_iterator_cb add_iterator) |
| 224 | { |
| 225 | struct bt_component_sink *sink; |
| 226 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 227 | |
| 228 | if (!component) { |
| 229 | ret = BT_COMPONENT_STATUS_INVALID; |
| 230 | goto end; |
| 231 | } |
| 232 | |
| 233 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_SINK) { |
| 234 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; |
| 235 | goto end; |
| 236 | } |
| 237 | |
| 238 | if (!component->initializing) { |
| 239 | ret = BT_COMPONENT_STATUS_INVALID; |
| 240 | goto end; |
| 241 | } |
| 242 | |
| 243 | sink = container_of(component, struct bt_component_sink, parent); |
| 244 | sink->add_iterator = add_iterator; |
| 245 | end: |
| 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | enum bt_component_status bt_component_sink_set_minimum_input_count( |
| 250 | struct bt_component *component, |
| 251 | unsigned int minimum) |
| 252 | { |
| 253 | struct bt_component_sink *sink; |
| 254 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 255 | |
| 256 | if (!component) { |
| 257 | ret = BT_COMPONENT_STATUS_INVALID; |
| 258 | goto end; |
| 259 | } |
| 260 | |
| 261 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_SINK) { |
| 262 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; |
| 263 | goto end; |
| 264 | } |
| 265 | |
| 266 | if (!component->initializing) { |
| 267 | ret = BT_COMPONENT_STATUS_INVALID; |
| 268 | goto end; |
| 269 | } |
| 270 | |
| 271 | sink = container_of(component, struct bt_component_sink, parent); |
| 272 | sink->input.min_count = minimum; |
| 273 | end: |
| 274 | return ret; |
| 275 | } |
| 276 | |
| 277 | enum bt_component_status bt_component_sink_set_maximum_input_count( |
| 278 | struct bt_component *component, |
| 279 | unsigned int maximum) |
| 280 | { |
| 281 | struct bt_component_sink *sink; |
| 282 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 283 | |
| 284 | if (!component) { |
| 285 | ret = BT_COMPONENT_STATUS_INVALID; |
| 286 | goto end; |
| 287 | } |
| 288 | |
| 289 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_SINK) { |
| 290 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; |
| 291 | goto end; |
| 292 | } |
| 293 | |
| 294 | if (!component->initializing) { |
| 295 | ret = BT_COMPONENT_STATUS_INVALID; |
| 296 | goto end; |
| 297 | } |
| 298 | |
| 299 | sink = container_of(component, struct bt_component_sink, parent); |
| 300 | sink->input.max_count = maximum; |
| 301 | end: |
| 302 | return ret; |
| 303 | } |
| 304 | |
| 305 | enum bt_component_status |
| 306 | bt_component_sink_get_input_count(struct bt_component *component, |
| 307 | unsigned int *count) |
| 308 | { |
| 309 | struct bt_component_sink *sink; |
| 310 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 311 | |
| 312 | if (!component || !count) { |
| 313 | ret = BT_COMPONENT_STATUS_INVALID; |
| 314 | goto end; |
| 315 | } |
| 316 | |
| 317 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_SINK) { |
| 318 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; |
| 319 | goto end; |
| 320 | } |
| 321 | |
| 322 | sink = container_of(component, struct bt_component_sink, parent); |
| 323 | *count = (unsigned int) sink->input.iterators->len; |
| 324 | end: |
| 325 | return ret; |
| 326 | } |
| 327 | |
| 328 | enum bt_component_status |
| 329 | bt_component_sink_get_input_iterator(struct bt_component *component, |
| 330 | unsigned int input, struct bt_notification_iterator **iterator) |
| 331 | { |
| 332 | struct bt_component_sink *sink; |
| 333 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 334 | |
| 335 | if (!component || !iterator) { |
| 336 | ret = BT_COMPONENT_STATUS_INVALID; |
| 337 | goto end; |
| 338 | } |
| 339 | |
| 340 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_SINK) { |
| 341 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; |
| 342 | goto end; |
| 343 | } |
| 344 | |
| 345 | sink = container_of(component, struct bt_component_sink, parent); |
| 346 | if (input >= (unsigned int) sink->input.iterators->len) { |
| 347 | ret = BT_COMPONENT_STATUS_INVALID; |
| 348 | goto end; |
| 349 | } |
| 350 | |
| 351 | *iterator = bt_get(g_ptr_array_index(sink->input.iterators, input)); |
| 352 | end: |
| 353 | return ret; |
| 354 | } |
| 355 | |
| 356 | enum bt_component_status |
| 357 | bt_component_sink_add_iterator(struct bt_component *component, |
| 358 | struct bt_notification_iterator *iterator) |
| 359 | { |
| 360 | struct bt_component_sink *sink; |
| 361 | enum bt_component_status ret = BT_COMPONENT_STATUS_OK; |
| 362 | |
| 363 | if (!component || !iterator) { |
| 364 | ret = BT_COMPONENT_STATUS_INVALID; |
| 365 | goto end; |
| 366 | } |
| 367 | |
| 368 | if (bt_component_get_type(component) != BT_COMPONENT_TYPE_SINK) { |
| 369 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; |
| 370 | goto end; |
| 371 | } |
| 372 | |
| 373 | sink = container_of(component, struct bt_component_sink, parent); |
| 374 | if (sink->input.iterators->len == sink->input.max_count) { |
| 375 | ret = BT_COMPONENT_STATUS_UNSUPPORTED; |
| 376 | goto end; |
| 377 | } |
| 378 | |
| 379 | if (sink->add_iterator) { |
| 380 | ret = sink->add_iterator(component, iterator); |
| 381 | if (ret != BT_COMPONENT_STATUS_OK) { |
| 382 | goto end; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | g_ptr_array_add(sink->input.iterators, bt_get(iterator)); |
| 387 | end: |
| 388 | return ret; |
| 389 | } |