test_ctf_writer.c: put statements outside BT_ASSERT()
[babeltrace.git] / lib / graph / port.c
CommitLineData
8b285017 1/*
7d55361f 2 * port.c
8b285017
JG
3 *
4 * Babeltrace Port
5 *
6 * Copyright 2017 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
f3034355
PP
29#define BT_LOG_TAG "PORT"
30#include <babeltrace/lib-logging-internal.h>
31
b65cfd0f 32#include <babeltrace/graph/port.h>
b2e0c907
PP
33#include <babeltrace/graph/component-internal.h>
34#include <babeltrace/graph/port-internal.h>
35#include <babeltrace/graph/connection-internal.h>
8b285017 36#include <babeltrace/object-internal.h>
3d9990ac 37#include <babeltrace/compiler-internal.h>
f6ccaed9 38#include <babeltrace/assert-internal.h>
8b285017
JG
39
40static
41void bt_port_destroy(struct bt_object *obj)
42{
43 struct bt_port *port = container_of(obj, struct bt_port, base);
44
f3034355
PP
45 BT_LOGD("Destroying port: addr=%p, name=\"%s\", comp-addr=%p",
46 port, bt_port_get_name(port), obj->parent);
47
8b285017
JG
48 if (port->name) {
49 g_string_free(port->name, TRUE);
50 }
f3034355 51
8b285017
JG
52 g_free(port);
53}
54
5c563278 55struct bt_port *bt_port_borrow_from_private(
890882ef
PP
56 struct bt_private_port *private_port)
57{
5c563278 58 return (void *) private_port;
890882ef
PP
59}
60
8b285017
JG
61BT_HIDDEN
62struct bt_port *bt_port_create(struct bt_component *parent_component,
3e9b0023 63 enum bt_port_type type, const char *name, void *user_data)
8b285017 64{
72b913fb 65 struct bt_port *port = NULL;
8b285017 66
f6ccaed9
PP
67 BT_ASSERT(name);
68 BT_ASSERT(parent_component);
69 BT_ASSERT(type == BT_PORT_TYPE_INPUT || type == BT_PORT_TYPE_OUTPUT);
8b285017 70
72b913fb 71 if (strlen(name) == 0) {
f3034355 72 BT_LOGW_STR("Invalid parameter: name is an empty string.");
8b285017
JG
73 goto end;
74 }
75
76 port = g_new0(struct bt_port, 1);
77 if (!port) {
f3034355 78 BT_LOGE_STR("Failed to allocate one port.");
8b285017
JG
79 goto end;
80 }
81
f3034355
PP
82 BT_LOGD("Creating port for component: "
83 "comp-addr=%p, comp-name=\"%s\", port-type=%s, "
84 "port-name=\"%s\"",
85 parent_component, bt_component_get_name(parent_component),
86 bt_port_type_string(type), name);
87
8b285017
JG
88 bt_object_init(port, bt_port_destroy);
89 port->name = g_string_new(name);
90 if (!port->name) {
f3034355 91 BT_LOGE_STR("Failed to allocate one GString.");
8b285017
JG
92 BT_PUT(port);
93 goto end;
94 }
95
96 port->type = type;
3e9b0023 97 port->user_data = user_data;
8b285017 98 bt_object_set_parent(port, &parent_component->base);
f3034355
PP
99 BT_LOGD("Created port for component: "
100 "comp-addr=%p, comp-name=\"%s\", port-type=%s, "
101 "port-name=\"%s\", port-addr=%p",
102 parent_component, bt_component_get_name(parent_component),
103 bt_port_type_string(type), name, port);
104
8b285017
JG
105end:
106 return port;
107}
108
109const char *bt_port_get_name(struct bt_port *port)
110{
111 return port ? port->name->str : NULL;
112}
113
114enum bt_port_type bt_port_get_type(struct bt_port *port)
115{
116 return port ? port->type : BT_PORT_TYPE_UNKOWN;
117}
118
72b913fb 119struct bt_connection *bt_port_get_connection(struct bt_port *port)
8b285017 120{
72b913fb 121 struct bt_connection *connection = NULL;
8b285017 122
f3034355
PP
123 if (!port) {
124 BT_LOGW_STR("Invalid parameter: port is NULL.");
125 goto end;
126 }
127
128 if (!port->connection) {
129 /* Not an error: means disconnected */
8b285017
JG
130 goto end;
131 }
132
72b913fb 133 connection = bt_get(port->connection);
f3034355 134
8b285017
JG
135end:
136 return connection;
137}
138
139struct bt_component *bt_port_get_component(struct bt_port *port)
140{
141 return (struct bt_component *) bt_object_get_parent(port);
142}
143
890882ef
PP
144struct bt_private_connection *bt_private_port_get_private_connection(
145 struct bt_private_port *private_port)
146{
147 return bt_private_connection_from_connection(bt_port_get_connection(
6d137876 148 bt_port_borrow_from_private(private_port)));
890882ef
PP
149}
150
151struct bt_private_component *bt_private_port_get_private_component(
152 struct bt_private_port *private_port)
153{
154 return bt_private_component_from_component(bt_port_get_component(
6d137876 155 bt_port_borrow_from_private(private_port)));
890882ef
PP
156}
157
8b285017 158BT_HIDDEN
72b913fb 159void bt_port_set_connection(struct bt_port *port,
8b285017 160 struct bt_connection *connection)
72b913fb
PP
161{
162 /*
163 * Don't take a reference on connection as its existence is
164 * guaranteed by the existence of the graph in which the
165 * connection exists.
166 */
167 port->connection = connection;
f3034355
PP
168 BT_LOGV("Set port's connection: "
169 "port-addr=%p, port-name=\"%s\", conn-addr=%p",
170 port, bt_port_get_name(port), connection);
72b913fb
PP
171}
172
7d62a2ab 173enum bt_port_status bt_private_port_remove_from_component(
890882ef 174 struct bt_private_port *private_port)
8b285017 175{
7d62a2ab 176 enum bt_port_status status = BT_PORT_STATUS_OK;
6d137876 177 struct bt_port *port = bt_port_borrow_from_private(private_port);
72b913fb 178 struct bt_component *comp = NULL;
7d62a2ab 179 enum bt_component_status comp_status;
8b285017 180
72b913fb 181 if (!port) {
f3034355 182 BT_LOGW_STR("Invalid parameter: private port is NULL.");
7d62a2ab 183 status = BT_PORT_STATUS_INVALID;
8b285017
JG
184 goto end;
185 }
186
72b913fb 187 comp = (void *) bt_object_get_parent(port);
7d62a2ab
PP
188 if (!comp) {
189 BT_LOGV("Port already removed from its component: "
190 "port-addr=%p, port-name=\"%s\", ",
191 port, bt_port_get_name(port));
192 goto end;
193 }
f3034355
PP
194
195 /* bt_component_remove_port() logs details */
7d62a2ab 196 comp_status = bt_component_remove_port(comp, port);
f6ccaed9 197 BT_ASSERT(comp_status != BT_COMPONENT_STATUS_INVALID);
7d62a2ab
PP
198 if (comp_status < 0) {
199 status = BT_PORT_STATUS_ERROR;
200 goto end;
201 }
72b913fb 202
8b285017 203end:
72b913fb 204 bt_put(comp);
7d62a2ab 205 return status;
8b285017
JG
206}
207
7d62a2ab 208enum bt_port_status bt_port_disconnect(struct bt_port *port)
8b285017 209{
7d62a2ab 210 enum bt_port_status status = BT_PORT_STATUS_OK;
8b285017 211
72b913fb 212 if (!port) {
f3034355 213 BT_LOGW_STR("Invalid parameter: port is NULL.");
7d62a2ab 214 status = BT_PORT_STATUS_INVALID;
8b285017
JG
215 goto end;
216 }
217
72b913fb 218 if (port->connection) {
c42ea0af 219 bt_connection_end(port->connection, true);
f3034355
PP
220 BT_LOGV("Disconnected port: "
221 "port-addr=%p, port-name=\"%s\"",
222 port, bt_port_get_name(port));
72b913fb
PP
223 }
224
8b285017 225end:
7d62a2ab 226 return status;
8b285017
JG
227}
228
b65cfd0f 229bt_bool bt_port_is_connected(struct bt_port *port)
8b285017 230{
72b913fb 231 int ret;
8b285017 232
72b913fb 233 if (!port) {
f3034355 234 BT_LOGW_STR("Invalid parameter: port is NULL.");
72b913fb 235 ret = -1;
8b285017
JG
236 goto end;
237 }
238
72b913fb
PP
239 ret = port->connection ? 1 : 0;
240
8b285017 241end:
72b913fb 242 return ret;
8b285017 243}
a45a0b60 244
a45a0b60
PP
245void *bt_private_port_get_user_data(
246 struct bt_private_port *private_port)
247{
248 return private_port ?
6d137876 249 bt_port_borrow_from_private(private_port)->user_data : NULL;
a45a0b60 250}
This page took 0.043587 seconds and 4 git commands to generate.