Remove Babeltrace 1 files and reorganize the tree
[babeltrace.git] / lib / component / port.c
1 /*
2 * port.c
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
29 #include <babeltrace/graph/component-internal.h>
30 #include <babeltrace/graph/port-internal.h>
31 #include <babeltrace/graph/connection-internal.h>
32 #include <babeltrace/object-internal.h>
33 #include <babeltrace/compiler.h>
34
35 static
36 void bt_port_destroy(struct bt_object *obj)
37 {
38 struct bt_port *port = container_of(obj, struct bt_port, base);
39
40 if (port->name) {
41 g_string_free(port->name, TRUE);
42 }
43 g_free(port);
44 }
45
46 struct bt_port *bt_port_from_private_port(
47 struct bt_private_port *private_port)
48 {
49 return bt_get(bt_port_from_private(private_port));
50 }
51
52 BT_HIDDEN
53 struct bt_port *bt_port_create(struct bt_component *parent_component,
54 enum bt_port_type type, const char *name)
55 {
56 struct bt_port *port = NULL;
57
58 assert(name);
59 assert(parent_component);
60 assert(type == BT_PORT_TYPE_INPUT || type == BT_PORT_TYPE_OUTPUT);
61
62 if (strlen(name) == 0) {
63 goto end;
64 }
65
66 port = g_new0(struct bt_port, 1);
67 if (!port) {
68 goto end;
69 }
70
71 bt_object_init(port, bt_port_destroy);
72 port->name = g_string_new(name);
73 if (!port->name) {
74 BT_PUT(port);
75 goto end;
76 }
77
78 port->type = type;
79
80 bt_object_set_parent(port, &parent_component->base);
81 end:
82 return port;
83 }
84
85 const char *bt_port_get_name(struct bt_port *port)
86 {
87 return port ? port->name->str : NULL;
88 }
89
90 enum bt_port_type bt_port_get_type(struct bt_port *port)
91 {
92 return port ? port->type : BT_PORT_TYPE_UNKOWN;
93 }
94
95 struct bt_connection *bt_port_get_connection(struct bt_port *port)
96 {
97 struct bt_connection *connection = NULL;
98
99 if (!port || !port->connection) {
100 goto end;
101 }
102
103 connection = bt_get(port->connection);
104 end:
105 return connection;
106 }
107
108 struct bt_component *bt_port_get_component(struct bt_port *port)
109 {
110 return (struct bt_component *) bt_object_get_parent(port);
111 }
112
113 struct bt_private_connection *bt_private_port_get_private_connection(
114 struct bt_private_port *private_port)
115 {
116 return bt_private_connection_from_connection(bt_port_get_connection(
117 bt_port_from_private(private_port)));
118 }
119
120 struct bt_private_component *bt_private_port_get_private_component(
121 struct bt_private_port *private_port)
122 {
123 return bt_private_component_from_component(bt_port_get_component(
124 bt_port_from_private(private_port)));
125 }
126
127 BT_HIDDEN
128 void bt_port_set_connection(struct bt_port *port,
129 struct bt_connection *connection)
130 {
131 /*
132 * Don't take a reference on connection as its existence is
133 * guaranteed by the existence of the graph in which the
134 * connection exists.
135 */
136 port->connection = connection;
137 }
138
139 int bt_private_port_remove_from_component(
140 struct bt_private_port *private_port)
141 {
142 int ret = 0;
143 struct bt_port *port = bt_port_from_private(private_port);
144 struct bt_component *comp = NULL;
145
146 if (!port) {
147 ret = -1;
148 goto end;
149 }
150
151 comp = (void *) bt_object_get_parent(port);
152 ret = bt_component_remove_port(comp, port);
153
154 end:
155 bt_put(comp);
156 return ret;
157 }
158
159 int bt_port_disconnect(struct bt_port *port)
160 {
161 int ret = 0;
162
163 if (!port) {
164 ret = -1;
165 goto end;
166 }
167
168 if (port->connection) {
169 bt_connection_disconnect_ports(port->connection);
170 }
171
172 end:
173 return ret;
174 }
175
176 int bt_port_is_connected(struct bt_port *port)
177 {
178 int ret;
179
180 if (!port) {
181 ret = -1;
182 goto end;
183 }
184
185 ret = port->connection ? 1 : 0;
186
187 end:
188 return ret;
189 }
190
191 int bt_private_port_set_user_data(
192 struct bt_private_port *private_port, void *user_data)
193 {
194 int ret = 0;
195
196 if (!private_port) {
197 ret = -1;
198 goto end;
199 }
200
201 bt_port_from_private(private_port)->user_data = user_data;
202
203 end:
204 return ret;
205 }
206
207 void *bt_private_port_get_user_data(
208 struct bt_private_port *private_port)
209 {
210 return private_port ?
211 bt_port_from_private(private_port)->user_data : NULL;
212 }
This page took 0.036918 seconds and 4 git commands to generate.