Make bt_private_component_*_add_*_port() return a status code
[babeltrace.git] / lib / graph / sink.c
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 #define BT_LOG_TAG "COMP-SINK"
30 #include <babeltrace/lib-logging-internal.h>
31
32 #include <babeltrace/compiler-internal.h>
33 #include <babeltrace/values.h>
34 #include <babeltrace/graph/component-sink-internal.h>
35 #include <babeltrace/graph/component-internal.h>
36 #include <babeltrace/graph/notification.h>
37
38 BT_HIDDEN
39 void bt_component_sink_destroy(struct bt_component *component)
40 {
41 }
42
43 BT_HIDDEN
44 struct bt_component *bt_component_sink_create(
45 struct bt_component_class *class, struct bt_value *params)
46 {
47 struct bt_component_sink *sink = NULL;
48
49 sink = g_new0(struct bt_component_sink, 1);
50 if (!sink) {
51 BT_LOGE_STR("Failed to allocate one sink component.");
52 goto end;
53 }
54
55 end:
56 return sink ? &sink->parent : NULL;
57 }
58
59 BT_HIDDEN
60 enum bt_component_status bt_component_sink_consume(
61 struct bt_component *component)
62 {
63 enum bt_component_status ret = BT_COMPONENT_STATUS_OK;
64 struct bt_component_class_sink *sink_class = NULL;
65
66 if (!component) {
67 BT_LOGW_STR("Invalid parameter: component is NULL.");
68 ret = BT_COMPONENT_STATUS_INVALID;
69 goto end;
70 }
71
72 if (bt_component_get_class_type(component) != BT_COMPONENT_CLASS_TYPE_SINK) {
73 BT_LOGW("Invalid parameter: component's class is not a sink component class: "
74 "comp-addr=%p, comp-name=\"%s\", comp-class-type=%s",
75 component, bt_component_get_name(component),
76 bt_component_class_type_string(component->class->type));
77 ret = BT_COMPONENT_STATUS_UNSUPPORTED;
78 goto end;
79 }
80
81 sink_class = container_of(component->class, struct bt_component_class_sink, parent);
82 assert(sink_class->methods.consume);
83 BT_LOGD("Calling user's consume method: "
84 "comp-addr=%p, comp-name=\"%s\"",
85 component, bt_component_get_name(component));
86 ret = sink_class->methods.consume(bt_private_component_from_component(component));
87 BT_LOGD("User method returned: status=%s",
88 bt_component_status_string(ret));
89 if (ret < 0) {
90 BT_LOGW_STR("Consume method failed.");
91 }
92
93 end:
94 return ret;
95 }
96
97 int64_t bt_component_sink_get_input_port_count(struct bt_component *component)
98 {
99 int64_t ret;
100
101 if (!component) {
102 BT_LOGW_STR("Invalid parameter: component is NULL.");
103 ret = (int64_t) -1;
104 goto end;
105 }
106
107 if (component->class->type != BT_COMPONENT_CLASS_TYPE_SINK) {
108 BT_LOGW("Invalid parameter: component's class is not a sink component class: "
109 "comp-addr=%p, comp-name=\"%s\", comp-class-type=%s",
110 component, bt_component_get_name(component),
111 bt_component_class_type_string(component->class->type));
112 ret = (int64_t) -1;
113 goto end;
114 }
115
116 /* bt_component_get_input_port_count() logs details/errors */
117 ret = bt_component_get_input_port_count(component);
118
119 end:
120 return ret;
121 }
122
123 struct bt_port *bt_component_sink_get_input_port_by_name(
124 struct bt_component *component, const char *name)
125 {
126 struct bt_port *port = NULL;
127
128 if (!component) {
129 BT_LOGW_STR("Invalid parameter: component is NULL.");
130 goto end;
131 }
132
133 if (!name) {
134 BT_LOGW_STR("Invalid parameter: name is NULL.");
135 goto end;
136 }
137
138 if (component->class->type != BT_COMPONENT_CLASS_TYPE_SINK) {
139 BT_LOGW("Invalid parameter: component's class is not a sink component class: "
140 "comp-addr=%p, comp-name=\"%s\", comp-class-type=%s",
141 component, bt_component_get_name(component),
142 bt_component_class_type_string(component->class->type));
143 goto end;
144 }
145
146 /* bt_component_get_input_port_by_name() logs details/errors */
147 port = bt_component_get_input_port_by_name(component, name);
148
149 end:
150 return port;
151 }
152
153 struct bt_port *bt_component_sink_get_input_port_by_index(
154 struct bt_component *component, uint64_t index)
155 {
156 struct bt_port *port = NULL;
157
158 if (!component) {
159 BT_LOGW_STR("Invalid parameter: component is NULL.");
160 goto end;
161 }
162
163 if (component->class->type != BT_COMPONENT_CLASS_TYPE_SINK) {
164 BT_LOGW("Invalid parameter: component's class is not a sink component class: "
165 "comp-addr=%p, comp-name=\"%s\", comp-class-type=%s",
166 component, bt_component_get_name(component),
167 bt_component_class_type_string(component->class->type));
168 goto end;
169 }
170
171 /* bt_component_get_input_port_by_index() logs details/errors */
172 port = bt_component_get_input_port_by_index(component, index);
173
174 end:
175 return port;
176 }
177
178 struct bt_private_port *
179 bt_private_component_sink_get_input_private_port_by_index(
180 struct bt_private_component *private_component, uint64_t index)
181 {
182 /* bt_component_sink_get_input_port_by_index() logs details/errors */
183 return bt_private_port_from_port(
184 bt_component_sink_get_input_port_by_index(
185 bt_component_from_private(private_component), index));
186 }
187
188 struct bt_private_port *
189 bt_private_component_sink_get_input_private_port_by_name(
190 struct bt_private_component *private_component,
191 const char *name)
192 {
193 /* bt_component_sink_get_input_port_by_name() logs details/errors */
194 return bt_private_port_from_port(
195 bt_component_sink_get_input_port_by_name(
196 bt_component_from_private(private_component), name));
197 }
198
199 enum bt_component_status bt_private_component_sink_add_input_private_port(
200 struct bt_private_component *private_component,
201 const char *name, void *user_data,
202 struct bt_private_port **user_priv_port)
203 {
204 enum bt_component_status status = BT_COMPONENT_STATUS_OK;
205 struct bt_port *port = NULL;
206 struct bt_component *component =
207 bt_component_from_private(private_component);
208
209 if (!component) {
210 BT_LOGW_STR("Invalid parameter: component is NULL.");
211 status = BT_COMPONENT_STATUS_INVALID;
212 goto end;
213 }
214
215 if (component->class->type != BT_COMPONENT_CLASS_TYPE_SINK) {
216 BT_LOGW("Invalid parameter: component's class is not a sink component class: "
217 "comp-addr=%p, comp-name=\"%s\", comp-class-type=%s",
218 component, bt_component_get_name(component),
219 bt_component_class_type_string(component->class->type));
220 status = BT_COMPONENT_STATUS_INVALID;
221 goto end;
222 }
223
224 /* bt_component_add_input_port() logs details/errors */
225 port = bt_component_add_input_port(component, name, user_data);
226 if (!port) {
227 status = BT_COMPONENT_STATUS_NOMEM;
228 goto end;
229 }
230
231 if (user_priv_port) {
232 /* Move reference to user */
233 *user_priv_port = bt_private_port_from_port(port);
234 port = NULL;
235 }
236
237 end:
238 bt_put(port);
239 return status;
240 }
This page took 0.037173 seconds and 4 git commands to generate.