Document libbabeltrace2's C API
[babeltrace.git] / src / lib / graph / port.c
CommitLineData
8b285017 1/*
e2f7325d 2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
8b285017 3 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8b285017
JG
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
350ad6c1 24#define BT_LOG_TAG "LIB/PORT"
c2d9d9cf 25#include "lib/logging.h"
f3034355 26
578e048b
MJ
27#include "common/assert.h"
28#include "lib/assert-pre.h"
43c59509 29#include <babeltrace2/graph/port.h>
3fadfbc0 30#include <babeltrace2/graph/self-component-port.h>
578e048b
MJ
31#include "lib/object.h"
32#include "compat/compiler.h"
33
34#include "component.h"
35#include "connection.h"
36#include "port.h"
8b285017
JG
37
38static
d94d92ac 39void destroy_port(struct bt_object *obj)
8b285017 40{
d94d92ac 41 struct bt_port *port = (void *) obj;
8b285017 42
3f7d4d90 43 BT_LIB_LOGI("Destroying port: %!+p", port);
f3034355 44
8b285017
JG
45 if (port->name) {
46 g_string_free(port->name, TRUE);
d94d92ac 47 port->name = NULL;
8b285017 48 }
f3034355 49
8b285017
JG
50 g_free(port);
51}
52
53BT_HIDDEN
54struct bt_port *bt_port_create(struct bt_component *parent_component,
3e9b0023 55 enum bt_port_type type, const char *name, void *user_data)
8b285017 56{
72b913fb 57 struct bt_port *port = NULL;
8b285017 58
f6ccaed9
PP
59 BT_ASSERT(name);
60 BT_ASSERT(parent_component);
61 BT_ASSERT(type == BT_PORT_TYPE_INPUT || type == BT_PORT_TYPE_OUTPUT);
d94d92ac 62 BT_ASSERT(strlen(name) > 0);
8b285017
JG
63 port = g_new0(struct bt_port, 1);
64 if (!port) {
870631a2 65 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one port.");
8b285017
JG
66 goto end;
67 }
68
3f7d4d90 69 BT_LIB_LOGI("Creating port for component: %![comp-]+c, port-type=%s, "
d94d92ac
PP
70 "port-name=\"%s\"", parent_component, bt_port_type_string(type),
71 name);
72 bt_object_init_shared_with_parent(&port->base, destroy_port);
8b285017
JG
73 port->name = g_string_new(name);
74 if (!port->name) {
870631a2 75 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString.");
65300d60 76 BT_OBJECT_PUT_REF_AND_RESET(port);
8b285017
JG
77 goto end;
78 }
79
80 port->type = type;
3e9b0023 81 port->user_data = user_data;
3fea54f6 82 bt_object_set_parent(&port->base, &parent_component->base);
3f7d4d90 83 BT_LIB_LOGI("Created port for component: "
d94d92ac 84 "%![comp-]+c, %![port-]+p", parent_component, port);
f3034355 85
8b285017
JG
86end:
87 return port;
88}
89
0d72b8c3 90const char *bt_port_get_name(const struct bt_port *port)
8b285017 91{
bdb288b3 92 BT_ASSERT_PRE_DEV_NON_NULL(port, "Port");
d94d92ac 93 return port->name->str;
8b285017
JG
94}
95
0d72b8c3 96enum bt_port_type bt_port_get_type(const struct bt_port *port)
8b285017 97{
bdb288b3 98 BT_ASSERT_PRE_DEV_NON_NULL(port, "Port");
d94d92ac 99 return port->type;
8b285017
JG
100}
101
0d72b8c3
PP
102const struct bt_connection *bt_port_borrow_connection_const(
103 const struct bt_port *port)
8b285017 104{
bdb288b3 105 BT_ASSERT_PRE_DEV_NON_NULL(port, "Port");
d94d92ac 106 return port->connection;
8b285017
JG
107}
108
0d72b8c3
PP
109const struct bt_component *bt_port_borrow_component_const(
110 const struct bt_port *port)
890882ef 111{
bdb288b3 112 BT_ASSERT_PRE_DEV_NON_NULL(port, "Port");
0d72b8c3 113 return bt_port_borrow_component_inline(port);
890882ef
PP
114}
115
d94d92ac
PP
116struct bt_self_component *bt_self_component_port_borrow_component(
117 struct bt_self_component_port *port)
890882ef 118{
bdb288b3 119 BT_ASSERT_PRE_DEV_NON_NULL(port, "Port");
d94d92ac 120 return (void *) bt_object_borrow_parent((void *) port);
890882ef
PP
121}
122
8b285017 123BT_HIDDEN
72b913fb 124void bt_port_set_connection(struct bt_port *port,
8b285017 125 struct bt_connection *connection)
72b913fb
PP
126{
127 /*
128 * Don't take a reference on connection as its existence is
129 * guaranteed by the existence of the graph in which the
130 * connection exists.
131 */
132 port->connection = connection;
3f7d4d90 133 BT_LIB_LOGI("Set port's connection: %![port-]+p, %![conn-]+x", port,
d94d92ac 134 connection);
72b913fb
PP
135}
136
0d72b8c3 137bt_bool bt_port_is_connected(const struct bt_port *port)
8b285017 138{
bdb288b3 139 BT_ASSERT_PRE_DEV_NON_NULL(port, "Port");
d94d92ac 140 return port->connection ? BT_TRUE : BT_FALSE;
8b285017 141}
a45a0b60 142
0d72b8c3 143void *bt_self_component_port_get_data(const struct bt_self_component_port *port)
a45a0b60 144{
bdb288b3 145 BT_ASSERT_PRE_DEV_NON_NULL(port, "Port");
d94d92ac 146 return ((struct bt_port *) port)->user_data;
a45a0b60 147}
c5b9b441
PP
148
149void bt_port_get_ref(const struct bt_port *port)
150{
151 bt_object_get_ref(port);
152}
153
154void bt_port_put_ref(const struct bt_port *port)
155{
156 bt_object_put_ref(port);
157}
158
159void bt_port_input_get_ref(const struct bt_port_input *port_input)
160{
161 bt_object_get_ref(port_input);
162}
163
164void bt_port_input_put_ref(const struct bt_port_input *port_input)
165{
166 bt_object_put_ref(port_input);
167}
168
169void bt_port_output_get_ref(const struct bt_port_output *port_output)
170{
171 bt_object_get_ref(port_output);
172}
173
174void bt_port_output_put_ref(const struct bt_port_output *port_output)
175{
176 bt_object_put_ref(port_output);
177}
This page took 0.06664 seconds and 4 git commands to generate.