a1294c3d64a788ee71e3682447d2547b9f0d48c6
[babeltrace.git] / src / plugins / utils / dummy / dummy.c
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
5 */
6
7 #define BT_COMP_LOG_SELF_COMP self_comp
8 #define BT_LOG_OUTPUT_LEVEL log_level
9 #define BT_LOG_TAG "PLUGIN/SINK.UTILS.DUMMY"
10 #include "logging/comp-logging.h"
11
12 #include <babeltrace2/babeltrace.h>
13 #include "common/macros.h"
14 #include "common/assert.h"
15 #include "dummy.h"
16 #include "plugins/common/param-validation/param-validation.h"
17
18 static
19 const char * const in_port_name = "in";
20
21 static
22 void destroy_private_dummy_data(struct dummy *dummy)
23 {
24 bt_message_iterator_put_ref(dummy->msg_iter);
25 g_free(dummy);
26
27 }
28
29 void dummy_finalize(bt_self_component_sink *comp)
30 {
31 struct dummy *dummy;
32
33 BT_ASSERT(comp);
34 dummy = bt_self_component_get_data(
35 bt_self_component_sink_as_self_component(comp));
36 BT_ASSERT(dummy);
37 destroy_private_dummy_data(dummy);
38 }
39
40 static
41 struct bt_param_validation_map_value_entry_descr dummy_params[] = {
42 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
43 };
44
45 bt_component_class_initialize_method_status dummy_init(
46 bt_self_component_sink *self_comp_sink,
47 bt_self_component_sink_configuration *config __attribute__((unused)),
48 const bt_value *params,
49 void *init_method_data __attribute__((unused)))
50 {
51 bt_self_component *self_comp =
52 bt_self_component_sink_as_self_component(self_comp_sink);
53 const bt_component *comp = bt_self_component_as_component(self_comp);
54 bt_logging_level log_level = bt_component_get_logging_level(comp);
55 bt_component_class_initialize_method_status status;
56 bt_self_component_add_port_status add_port_status;
57 struct dummy *dummy = g_new0(struct dummy, 1);
58 enum bt_param_validation_status validation_status;
59 gchar *validate_error = NULL;
60
61 if (!dummy) {
62 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
63 goto end;
64 }
65
66 validation_status = bt_param_validation_validate(params,
67 dummy_params, &validate_error);
68 if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) {
69 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
70 goto error;
71 } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) {
72 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
73 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "%s", validate_error);
74 goto error;
75 }
76
77 add_port_status = bt_self_component_sink_add_input_port(self_comp_sink,
78 "in", NULL, NULL);
79 if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
80 status = (int) add_port_status;
81 goto error;
82 }
83
84 bt_self_component_set_data(self_comp, dummy);
85
86 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
87 goto end;
88
89 error:
90 destroy_private_dummy_data(dummy);
91
92 end:
93 g_free(validate_error);
94
95 return status;
96 }
97
98 bt_component_class_sink_graph_is_configured_method_status dummy_graph_is_configured(
99 bt_self_component_sink *comp)
100 {
101 bt_component_class_sink_graph_is_configured_method_status status;
102 bt_message_iterator_create_from_sink_component_status
103 msg_iter_status;
104 struct dummy *dummy;
105 bt_message_iterator *iterator;
106
107 dummy = bt_self_component_get_data(
108 bt_self_component_sink_as_self_component(comp));
109 BT_ASSERT(dummy);
110 msg_iter_status = bt_message_iterator_create_from_sink_component(
111 comp, bt_self_component_sink_borrow_input_port_by_name(comp,
112 in_port_name), &iterator);
113 if (msg_iter_status != BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) {
114 status = (int) msg_iter_status;
115 goto end;
116 }
117
118 BT_MESSAGE_ITERATOR_MOVE_REF(
119 dummy->msg_iter, iterator);
120
121 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
122
123 end:
124 return status;
125 }
126
127 bt_component_class_sink_consume_method_status dummy_consume(
128 bt_self_component_sink *component)
129 {
130 bt_component_class_sink_consume_method_status status =
131 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
132 bt_message_array_const msgs;
133 uint64_t count;
134 struct dummy *dummy;
135 bt_message_iterator_next_status next_status;
136 uint64_t i;
137
138 dummy = bt_self_component_get_data(
139 bt_self_component_sink_as_self_component(component));
140 BT_ASSERT_DBG(dummy);
141
142 if (G_UNLIKELY(!dummy->msg_iter)) {
143 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
144 goto end;
145 }
146
147 /* Consume one message */
148 next_status = bt_message_iterator_next(
149 dummy->msg_iter, &msgs, &count);
150 switch (next_status) {
151 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
152 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
153
154 for (i = 0; i < count; i++) {
155 bt_message_put_ref(msgs[i]);
156 }
157
158 break;
159 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN:
160 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN;
161 goto end;
162 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END:
163 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
164 goto end;
165 case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR:
166 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR;
167 goto end;
168 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR:
169 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR;
170 goto end;
171 default:
172 break;
173 }
174
175 end:
176 return status;
177 }
This page took 0.034289 seconds and 3 git commands to generate.