Always evaluate BT_ASSERT(); add BT_ASSERT_DBG() for debug mode only
[babeltrace.git] / src / plugins / utils / dummy / dummy.c
CommitLineData
e0dfa761
PP
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
92962796
SM
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
71c5da58 28#include <babeltrace2/babeltrace.h>
85e7137b 29#include "common/macros.h"
57952005 30#include "common/assert.h"
c2b71c92 31#include "dummy.h"
92962796 32#include "plugins/common/param-validation/param-validation.h"
c2b71c92 33
1043fdea
PP
34static
35const char * const in_port_name = "in";
36
c2b71c92
JG
37void destroy_private_dummy_data(struct dummy *dummy)
38{
b09a5592 39 bt_self_component_port_input_message_iterator_put_ref(dummy->msg_iter);
c2b71c92 40 g_free(dummy);
834e9996 41
c2b71c92
JG
42}
43
834e9996 44BT_HIDDEN
8eee8ea2 45void dummy_finalize(bt_self_component_sink *comp)
c2b71c92
JG
46{
47 struct dummy *dummy;
48
834e9996
PP
49 BT_ASSERT(comp);
50 dummy = bt_self_component_get_data(
bb61965b 51 bt_self_component_sink_as_self_component(comp));
8b45963b 52 BT_ASSERT(dummy);
c2b71c92
JG
53 destroy_private_dummy_data(dummy);
54}
55
92962796
SM
56struct bt_param_validation_map_value_entry_descr dummy_params[] = {
57 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
58};
59
834e9996 60BT_HIDDEN
4175c1d5 61bt_component_class_initialize_method_status dummy_init(
92962796 62 bt_self_component_sink *self_comp_sink,
e3250e61 63 bt_self_component_sink_configuration *config,
8eee8ea2 64 const bt_value *params,
f807570c 65 __attribute__((unused)) void *init_method_data)
c2b71c92 66{
92962796
SM
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;
fb25b9e3 72 bt_self_component_add_port_status add_port_status;
c2b71c92 73 struct dummy *dummy = g_new0(struct dummy, 1);
92962796
SM
74 enum bt_param_validation_status validation_status;
75 gchar *validate_error = NULL;
c2b71c92
JG
76
77 if (!dummy) {
4175c1d5 78 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
c2b71c92
JG
79 goto end;
80 }
81
92962796
SM
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) {
4175c1d5 88 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
92962796 89 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "%s", validate_error);
c2b71c92 90 goto error;
92962796
SM
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;
fb25b9e3 97 goto error;
c2b71c92 98 }
d83d78e7 99
92962796
SM
100 bt_self_component_set_data(self_comp, dummy);
101
102 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
d83d78e7
PP
103 goto end;
104
c2b71c92
JG
105error:
106 destroy_private_dummy_data(dummy);
d83d78e7
PP
107
108end:
92962796
SM
109 g_free(validate_error);
110
fb25b9e3 111 return status;
c2b71c92
JG
112}
113
834e9996 114BT_HIDDEN
fb25b9e3 115bt_component_class_sink_graph_is_configured_method_status dummy_graph_is_configured(
1043fdea 116 bt_self_component_sink *comp)
c2b71c92 117{
ab8b2b1b
SM
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;
c2b71c92 121 struct dummy *dummy;
b09a5592 122 bt_self_component_port_input_message_iterator *iterator;
c2b71c92 123
834e9996 124 dummy = bt_self_component_get_data(
bb61965b 125 bt_self_component_sink_as_self_component(comp));
8b45963b 126 BT_ASSERT(dummy);
ab8b2b1b 127 msg_iter_status = bt_self_component_port_input_message_iterator_create_from_sink_component(
692f1a01 128 comp, bt_self_component_sink_borrow_input_port_by_name(comp,
ab8b2b1b
SM
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;
c2b71c92
JG
132 goto end;
133 }
134
b09a5592
PP
135 BT_SELF_COMPONENT_PORT_INPUT_MESSAGE_ITERATOR_MOVE_REF(
136 dummy->msg_iter, iterator);
0d8b4d8e 137
ab8b2b1b
SM
138 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
139
c2b71c92 140end:
634f394c 141 return status;
c2b71c92 142}
e0dfa761 143
834e9996 144BT_HIDDEN
fb25b9e3 145bt_component_class_sink_consume_method_status dummy_consume(
8eee8ea2 146 bt_self_component_sink *component)
e0dfa761 147{
fb25b9e3
PP
148 bt_component_class_sink_consume_method_status status =
149 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
b09a5592 150 bt_message_array_const msgs;
3fd7b79d 151 uint64_t count;
c2b71c92 152 struct dummy *dummy;
fb25b9e3 153 bt_message_iterator_next_status next_status;
3fd7b79d 154 uint64_t i;
e0dfa761 155
834e9996 156 dummy = bt_self_component_get_data(
bb61965b 157 bt_self_component_sink_as_self_component(component));
ec4a3354 158 BT_ASSERT_DBG(dummy);
e0dfa761 159
85e7137b 160 if (G_UNLIKELY(!dummy->msg_iter)) {
fb25b9e3 161 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
d83d78e7 162 goto end;
e0dfa761
PP
163 }
164
b09a5592 165 /* Consume one message */
fb25b9e3 166 next_status = bt_self_component_port_input_message_iterator_next(
b09a5592 167 dummy->msg_iter, &msgs, &count);
fb25b9e3
PP
168 switch (next_status) {
169 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
170 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
3fd7b79d
PP
171
172 for (i = 0; i < count; i++) {
b09a5592 173 bt_message_put_ref(msgs[i]);
3fd7b79d
PP
174 }
175
176 break;
fb25b9e3
PP
177 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN:
178 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN;
d83d78e7 179 goto end;
fb25b9e3
PP
180 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END:
181 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
d83d78e7 182 goto end;
fb25b9e3
PP
183 case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR:
184 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR;
834e9996 185 goto end;
fb25b9e3
PP
186 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR:
187 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR;
3fd7b79d 188 goto end;
d83d78e7
PP
189 default:
190 break;
e0dfa761 191 }
d83d78e7 192
e0dfa761 193end:
fb25b9e3 194 return status;
e0dfa761 195}
This page took 0.060512 seconds and 4 git commands to generate.