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