Move to kernel style SPDX license identifiers
[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 BT_HIDDEN
30 void dummy_finalize(bt_self_component_sink *comp)
31 {
32 struct dummy *dummy;
33
34 BT_ASSERT(comp);
35 dummy = bt_self_component_get_data(
36 bt_self_component_sink_as_self_component(comp));
37 BT_ASSERT(dummy);
38 destroy_private_dummy_data(dummy);
39 }
40
41 static
42 struct bt_param_validation_map_value_entry_descr dummy_params[] = {
43 BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END
44 };
45
46 BT_HIDDEN
47 bt_component_class_initialize_method_status dummy_init(
48 bt_self_component_sink *self_comp_sink,
49 bt_self_component_sink_configuration *config,
50 const bt_value *params,
51 __attribute__((unused)) void *init_method_data)
52 {
53 bt_self_component *self_comp =
54 bt_self_component_sink_as_self_component(self_comp_sink);
55 const bt_component *comp = bt_self_component_as_component(self_comp);
56 bt_logging_level log_level = bt_component_get_logging_level(comp);
57 bt_component_class_initialize_method_status status;
58 bt_self_component_add_port_status add_port_status;
59 struct dummy *dummy = g_new0(struct dummy, 1);
60 enum bt_param_validation_status validation_status;
61 gchar *validate_error = NULL;
62
63 if (!dummy) {
64 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
65 goto end;
66 }
67
68 validation_status = bt_param_validation_validate(params,
69 dummy_params, &validate_error);
70 if (validation_status == BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR) {
71 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_MEMORY_ERROR;
72 goto error;
73 } else if (validation_status == BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR) {
74 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_ERROR;
75 BT_COMP_LOGE_APPEND_CAUSE(self_comp, "%s", validate_error);
76 goto error;
77 }
78
79 add_port_status = bt_self_component_sink_add_input_port(self_comp_sink,
80 "in", NULL, NULL);
81 if (add_port_status != BT_SELF_COMPONENT_ADD_PORT_STATUS_OK) {
82 status = (int) add_port_status;
83 goto error;
84 }
85
86 bt_self_component_set_data(self_comp, dummy);
87
88 status = BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK;
89 goto end;
90
91 error:
92 destroy_private_dummy_data(dummy);
93
94 end:
95 g_free(validate_error);
96
97 return status;
98 }
99
100 BT_HIDDEN
101 bt_component_class_sink_graph_is_configured_method_status dummy_graph_is_configured(
102 bt_self_component_sink *comp)
103 {
104 bt_component_class_sink_graph_is_configured_method_status status;
105 bt_message_iterator_create_from_sink_component_status
106 msg_iter_status;
107 struct dummy *dummy;
108 bt_message_iterator *iterator;
109
110 dummy = bt_self_component_get_data(
111 bt_self_component_sink_as_self_component(comp));
112 BT_ASSERT(dummy);
113 msg_iter_status = bt_message_iterator_create_from_sink_component(
114 comp, bt_self_component_sink_borrow_input_port_by_name(comp,
115 in_port_name), &iterator);
116 if (msg_iter_status != BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK) {
117 status = (int) msg_iter_status;
118 goto end;
119 }
120
121 BT_MESSAGE_ITERATOR_MOVE_REF(
122 dummy->msg_iter, iterator);
123
124 status = BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK;
125
126 end:
127 return status;
128 }
129
130 BT_HIDDEN
131 bt_component_class_sink_consume_method_status dummy_consume(
132 bt_self_component_sink *component)
133 {
134 bt_component_class_sink_consume_method_status status =
135 BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
136 bt_message_array_const msgs;
137 uint64_t count;
138 struct dummy *dummy;
139 bt_message_iterator_next_status next_status;
140 uint64_t i;
141
142 dummy = bt_self_component_get_data(
143 bt_self_component_sink_as_self_component(component));
144 BT_ASSERT_DBG(dummy);
145
146 if (G_UNLIKELY(!dummy->msg_iter)) {
147 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
148 goto end;
149 }
150
151 /* Consume one message */
152 next_status = bt_message_iterator_next(
153 dummy->msg_iter, &msgs, &count);
154 switch (next_status) {
155 case BT_MESSAGE_ITERATOR_NEXT_STATUS_OK:
156 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_OK;
157
158 for (i = 0; i < count; i++) {
159 bt_message_put_ref(msgs[i]);
160 }
161
162 break;
163 case BT_MESSAGE_ITERATOR_NEXT_STATUS_AGAIN:
164 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_AGAIN;
165 goto end;
166 case BT_MESSAGE_ITERATOR_NEXT_STATUS_END:
167 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END;
168 goto end;
169 case BT_MESSAGE_ITERATOR_NEXT_STATUS_ERROR:
170 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_ERROR;
171 goto end;
172 case BT_MESSAGE_ITERATOR_NEXT_STATUS_MEMORY_ERROR:
173 status = BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_MEMORY_ERROR;
174 goto end;
175 default:
176 break;
177 }
178
179 end:
180 return status;
181 }
This page took 0.050089 seconds and 5 git commands to generate.