| 1 | /* |
| 2 | * Copyright 2017 Philippe Proulx <pproulx@efficios.com> |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to deal |
| 6 | * in the Software without restriction, including without limitation the rights |
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | * copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in |
| 12 | * all copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | * SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #define BT_COMP_LOG_SELF_COMP self_comp |
| 24 | #define BT_LOG_OUTPUT_LEVEL log_level |
| 25 | #define BT_LOG_TAG "PLUGIN/SINK.UTILS.DUMMY" |
| 26 | #include "logging/comp-logging.h" |
| 27 | |
| 28 | #include <babeltrace2/babeltrace.h> |
| 29 | #include "common/macros.h" |
| 30 | #include "common/assert.h" |
| 31 | #include "dummy.h" |
| 32 | #include "plugins/common/param-validation/param-validation.h" |
| 33 | |
| 34 | static |
| 35 | const char * const in_port_name = "in"; |
| 36 | |
| 37 | void destroy_private_dummy_data(struct dummy *dummy) |
| 38 | { |
| 39 | bt_self_component_port_input_message_iterator_put_ref(dummy->msg_iter); |
| 40 | g_free(dummy); |
| 41 | |
| 42 | } |
| 43 | |
| 44 | BT_HIDDEN |
| 45 | void dummy_finalize(bt_self_component_sink *comp) |
| 46 | { |
| 47 | struct dummy *dummy; |
| 48 | |
| 49 | BT_ASSERT(comp); |
| 50 | dummy = bt_self_component_get_data( |
| 51 | bt_self_component_sink_as_self_component(comp)); |
| 52 | BT_ASSERT(dummy); |
| 53 | destroy_private_dummy_data(dummy); |
| 54 | } |
| 55 | |
| 56 | struct bt_param_validation_map_value_entry_descr dummy_params[] = { |
| 57 | BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END |
| 58 | }; |
| 59 | |
| 60 | BT_HIDDEN |
| 61 | bt_component_class_initialize_method_status dummy_init( |
| 62 | bt_self_component_sink *self_comp_sink, |
| 63 | bt_self_component_sink_configuration *config, |
| 64 | const bt_value *params, |
| 65 | __attribute__((unused)) void *init_method_data) |
| 66 | { |
| 67 | bt_self_component *self_comp = |
| 68 | bt_self_component_sink_as_self_component(self_comp_sink); |
| 69 | const bt_component *comp = bt_self_component_as_component(self_comp); |
| 70 | bt_logging_level log_level = bt_component_get_logging_level(comp); |
| 71 | bt_component_class_initialize_method_status status; |
| 72 | bt_self_component_add_port_status add_port_status; |
| 73 | struct dummy *dummy = g_new0(struct dummy, 1); |
| 74 | enum bt_param_validation_status validation_status; |
| 75 | gchar *validate_error = NULL; |
| 76 | |
| 77 | if (!dummy) { |
| 78 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; |
| 79 | goto end; |
| 80 | } |
| 81 | |
| 82 | validation_status = bt_param_validation_validate(params, |
| 83 | dummy_params, &validate_error); |
| 84 | if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) { |
| 85 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR; |
| 86 | goto error; |
| 87 | } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) { |
| 88 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR; |
| 89 | BT_COMP_LOGE_APPEND_CAUSE(self_comp, "%s", validate_error); |
| 90 | goto error; |
| 91 | } |
| 92 | |
| 93 | add_port_status = bt_self_component_sink_add_input_port(self_comp_sink, |
| 94 | "in", NULL, NULL); |
| 95 | if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) { |
| 96 | status = (int) add_port_status; |
| 97 | goto error; |
| 98 | } |
| 99 | |
| 100 | bt_self_component_set_data(self_comp, dummy); |
| 101 | |
| 102 | status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; |
| 103 | goto end; |
| 104 | |
| 105 | error: |
| 106 | destroy_private_dummy_data(dummy); |
| 107 | |
| 108 | end: |
| 109 | g_free(validate_error); |
| 110 | |
| 111 | return status; |
| 112 | } |
| 113 | |
| 114 | BT_HIDDEN |
| 115 | bt_component_class_sink_graph_is_configured_method_status dummy_graph_is_configured( |
| 116 | bt_self_component_sink *comp) |
| 117 | { |
| 118 | bt_component_class_sink_graph_is_configured_method_status status; |
| 119 | bt_self_component_port_input_message_iterator_create_from_sink_component_status |
| 120 | msg_iter_status; |
| 121 | struct dummy *dummy; |
| 122 | bt_self_component_port_input_message_iterator *iterator; |
| 123 | |
| 124 | dummy = bt_self_component_get_data( |
| 125 | bt_self_component_sink_as_self_component(comp)); |
| 126 | BT_ASSERT(dummy); |
| 127 | msg_iter_status = bt_self_component_port_input_message_iterator_create_from_sink_component( |
| 128 | comp, bt_self_component_sink_borrow_input_port_by_name(comp, |
| 129 | in_port_name), &iterator); |
| 130 | if (msg_iter_status != BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) { |
| 131 | status = (int) msg_iter_status; |
| 132 | goto end; |
| 133 | } |
| 134 | |
| 135 | BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF( |
| 136 | dummy->msg_iter, iterator); |
| 137 | |
| 138 | status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK; |
| 139 | |
| 140 | end: |
| 141 | return status; |
| 142 | } |
| 143 | |
| 144 | BT_HIDDEN |
| 145 | bt_component_class_sink_consume_method_status dummy_consume( |
| 146 | bt_self_component_sink *component) |
| 147 | { |
| 148 | bt_component_class_sink_consume_method_status status = |
| 149 | BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK; |
| 150 | bt_message_array_const msgs; |
| 151 | uint64_t count; |
| 152 | struct dummy *dummy; |
| 153 | bt_message_iterator_next_status next_status; |
| 154 | uint64_t i; |
| 155 | |
| 156 | dummy = bt_self_component_get_data( |
| 157 | bt_self_component_sink_as_self_component(component)); |
| 158 | BT_ASSERT(dummy); |
| 159 | |
| 160 | if (G_UNLIKELY(!dummy->msg_iter)) { |
| 161 | status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END; |
| 162 | goto end; |
| 163 | } |
| 164 | |
| 165 | /* Consume one message */ |
| 166 | next_status = bt_self_component_port_input_message_iterator_next( |
| 167 | dummy->msg_iter, &msgs, &count); |
| 168 | switch (next_status) { |
| 169 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK: |
| 170 | status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK; |
| 171 | |
| 172 | for (i = 0; i < count; i++) { |
| 173 | bt_message_put_ref(msgs[i]); |
| 174 | } |
| 175 | |
| 176 | break; |
| 177 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN: |
| 178 | status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN; |
| 179 | goto end; |
| 180 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_END: |
| 181 | status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END; |
| 182 | goto end; |
| 183 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR: |
| 184 | status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR; |
| 185 | goto end; |
| 186 | case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR: |
| 187 | status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR; |
| 188 | goto end; |
| 189 | default: |
| 190 | break; |
| 191 | } |
| 192 | |
| 193 | end: |
| 194 | return status; |
| 195 | } |