cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[babeltrace.git] / src / lib / graph / port.c
CommitLineData
8b285017 1/*
0235b0db
MJ
2 * SPDX-License-Identifier: MIT
3 *
e2f7325d 4 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
8b285017 5 * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
8b285017
JG
6 */
7
350ad6c1 8#define BT_LOG_TAG "LIB/PORT"
c2d9d9cf 9#include "lib/logging.h"
f3034355 10
578e048b 11#include "common/assert.h"
d98421f2 12#include "lib/assert-cond.h"
43c59509 13#include <babeltrace2/graph/port.h>
3fadfbc0 14#include <babeltrace2/graph/self-component-port.h>
578e048b
MJ
15#include "lib/object.h"
16#include "compat/compiler.h"
17
18#include "component.h"
19#include "connection.h"
20#include "port.h"
8b285017
JG
21
22static
d94d92ac 23void destroy_port(struct bt_object *obj)
8b285017 24{
d94d92ac 25 struct bt_port *port = (void *) obj;
8b285017 26
3f7d4d90 27 BT_LIB_LOGI("Destroying port: %!+p", port);
f3034355 28
8b285017
JG
29 if (port->name) {
30 g_string_free(port->name, TRUE);
d94d92ac 31 port->name = NULL;
8b285017 32 }
f3034355 33
8b285017
JG
34 g_free(port);
35}
36
8b285017 37struct bt_port *bt_port_create(struct bt_component *parent_component,
3e9b0023 38 enum bt_port_type type, const char *name, void *user_data)
8b285017 39{
72b913fb 40 struct bt_port *port = NULL;
8b285017 41
f6ccaed9
PP
42 BT_ASSERT(name);
43 BT_ASSERT(parent_component);
44 BT_ASSERT(type == BT_PORT_TYPE_INPUT || type == BT_PORT_TYPE_OUTPUT);
d94d92ac 45 BT_ASSERT(strlen(name) > 0);
8b285017
JG
46 port = g_new0(struct bt_port, 1);
47 if (!port) {
870631a2 48 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one port.");
8b285017
JG
49 goto end;
50 }
51
3f7d4d90 52 BT_LIB_LOGI("Creating port for component: %![comp-]+c, port-type=%s, "
d94d92ac
PP
53 "port-name=\"%s\"", parent_component, bt_port_type_string(type),
54 name);
55 bt_object_init_shared_with_parent(&port->base, destroy_port);
8b285017
JG
56 port->name = g_string_new(name);
57 if (!port->name) {
870631a2 58 BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString.");
65300d60 59 BT_OBJECT_PUT_REF_AND_RESET(port);
8b285017
JG
60 goto end;
61 }
62
63 port->type = type;
3e9b0023 64 port->user_data = user_data;
3fea54f6 65 bt_object_set_parent(&port->base, &parent_component->base);
3f7d4d90 66 BT_LIB_LOGI("Created port for component: "
d94d92ac 67 "%![comp-]+c, %![port-]+p", parent_component, port);
f3034355 68
8b285017
JG
69end:
70 return port;
71}
72
1353b066 73BT_EXPORT
0d72b8c3 74const char *bt_port_get_name(const struct bt_port *port)
8b285017 75{
d5b13b9b 76 BT_ASSERT_PRE_DEV_PORT_NON_NULL(port);
d94d92ac 77 return port->name->str;
8b285017
JG
78}
79
1353b066 80BT_EXPORT
0d72b8c3 81enum bt_port_type bt_port_get_type(const struct bt_port *port)
8b285017 82{
d5b13b9b 83 BT_ASSERT_PRE_DEV_PORT_NON_NULL(port);
d94d92ac 84 return port->type;
8b285017
JG
85}
86
1353b066 87BT_EXPORT
0d72b8c3
PP
88const struct bt_connection *bt_port_borrow_connection_const(
89 const struct bt_port *port)
8b285017 90{
d5b13b9b 91 BT_ASSERT_PRE_DEV_PORT_NON_NULL(port);
d94d92ac 92 return port->connection;
8b285017
JG
93}
94
1353b066 95BT_EXPORT
0d72b8c3
PP
96const struct bt_component *bt_port_borrow_component_const(
97 const struct bt_port *port)
890882ef 98{
d5b13b9b 99 BT_ASSERT_PRE_DEV_PORT_NON_NULL(port);
0d72b8c3 100 return bt_port_borrow_component_inline(port);
890882ef
PP
101}
102
1353b066 103BT_EXPORT
d94d92ac
PP
104struct bt_self_component *bt_self_component_port_borrow_component(
105 struct bt_self_component_port *port)
890882ef 106{
d5b13b9b 107 BT_ASSERT_PRE_DEV_PORT_NON_NULL(port);
d94d92ac 108 return (void *) bt_object_borrow_parent((void *) port);
890882ef
PP
109}
110
72b913fb 111void bt_port_set_connection(struct bt_port *port,
8b285017 112 struct bt_connection *connection)
72b913fb
PP
113{
114 /*
115 * Don't take a reference on connection as its existence is
116 * guaranteed by the existence of the graph in which the
117 * connection exists.
118 */
119 port->connection = connection;
3f7d4d90 120 BT_LIB_LOGI("Set port's connection: %![port-]+p, %![conn-]+x", port,
d94d92ac 121 connection);
72b913fb
PP
122}
123
1353b066 124BT_EXPORT
0d72b8c3 125bt_bool bt_port_is_connected(const struct bt_port *port)
8b285017 126{
d5b13b9b 127 BT_ASSERT_PRE_DEV_PORT_NON_NULL(port);
d94d92ac 128 return port->connection ? BT_TRUE : BT_FALSE;
8b285017 129}
a45a0b60 130
1353b066 131BT_EXPORT
0d72b8c3 132void *bt_self_component_port_get_data(const struct bt_self_component_port *port)
a45a0b60 133{
d5b13b9b 134 BT_ASSERT_PRE_DEV_PORT_NON_NULL(port);
d94d92ac 135 return ((struct bt_port *) port)->user_data;
a45a0b60 136}
c5b9b441 137
1353b066 138BT_EXPORT
c5b9b441
PP
139void bt_port_get_ref(const struct bt_port *port)
140{
141 bt_object_get_ref(port);
142}
143
1353b066 144BT_EXPORT
c5b9b441
PP
145void bt_port_put_ref(const struct bt_port *port)
146{
147 bt_object_put_ref(port);
148}
149
1353b066 150BT_EXPORT
c5b9b441
PP
151void bt_port_input_get_ref(const struct bt_port_input *port_input)
152{
153 bt_object_get_ref(port_input);
154}
155
1353b066 156BT_EXPORT
c5b9b441
PP
157void bt_port_input_put_ref(const struct bt_port_input *port_input)
158{
159 bt_object_put_ref(port_input);
160}
161
1353b066 162BT_EXPORT
c5b9b441
PP
163void bt_port_output_get_ref(const struct bt_port_output *port_output)
164{
165 bt_object_get_ref(port_output);
166}
167
1353b066 168BT_EXPORT
c5b9b441
PP
169void bt_port_output_put_ref(const struct bt_port_output *port_output)
170{
171 bt_object_put_ref(port_output);
172}
This page took 0.097593 seconds and 4 git commands to generate.