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