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