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 | ||
29 | #include <babeltrace/component/component-internal.h> | |
7d55361f | 30 | #include <babeltrace/component/port-internal.h> |
72b913fb | 31 | #include <babeltrace/component/connection-internal.h> |
8b285017 JG |
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 | } | |
8b285017 JG |
43 | g_free(port); |
44 | } | |
45 | ||
46 | BT_HIDDEN | |
47 | struct bt_port *bt_port_create(struct bt_component *parent_component, | |
48 | enum bt_port_type type, const char *name) | |
49 | { | |
72b913fb | 50 | struct bt_port *port = NULL; |
8b285017 JG |
51 | |
52 | assert(name); | |
53 | assert(parent_component); | |
54 | assert(type == BT_PORT_TYPE_INPUT || type == BT_PORT_TYPE_OUTPUT); | |
55 | ||
72b913fb | 56 | if (strlen(name) == 0) { |
8b285017 JG |
57 | goto end; |
58 | } | |
59 | ||
60 | port = g_new0(struct bt_port, 1); | |
61 | if (!port) { | |
62 | goto end; | |
63 | } | |
64 | ||
65 | bt_object_init(port, bt_port_destroy); | |
66 | port->name = g_string_new(name); | |
67 | if (!port->name) { | |
68 | BT_PUT(port); | |
69 | goto end; | |
70 | } | |
71 | ||
72 | port->type = type; | |
8b285017 JG |
73 | |
74 | bt_object_set_parent(port, &parent_component->base); | |
75 | end: | |
76 | return port; | |
77 | } | |
78 | ||
79 | const char *bt_port_get_name(struct bt_port *port) | |
80 | { | |
81 | return port ? port->name->str : NULL; | |
82 | } | |
83 | ||
84 | enum bt_port_type bt_port_get_type(struct bt_port *port) | |
85 | { | |
86 | return port ? port->type : BT_PORT_TYPE_UNKOWN; | |
87 | } | |
88 | ||
72b913fb | 89 | struct bt_connection *bt_port_get_connection(struct bt_port *port) |
8b285017 | 90 | { |
72b913fb | 91 | struct bt_connection *connection = NULL; |
8b285017 | 92 | |
72b913fb | 93 | if (!port || !port->connection) { |
8b285017 JG |
94 | goto end; |
95 | } | |
96 | ||
72b913fb | 97 | connection = bt_get(port->connection); |
8b285017 JG |
98 | end: |
99 | return connection; | |
100 | } | |
101 | ||
102 | struct bt_component *bt_port_get_component(struct bt_port *port) | |
103 | { | |
104 | return (struct bt_component *) bt_object_get_parent(port); | |
105 | } | |
106 | ||
107 | BT_HIDDEN | |
72b913fb | 108 | void bt_port_set_connection(struct bt_port *port, |
8b285017 | 109 | struct bt_connection *connection) |
72b913fb PP |
110 | { |
111 | /* | |
112 | * Don't take a reference on connection as its existence is | |
113 | * guaranteed by the existence of the graph in which the | |
114 | * connection exists. | |
115 | */ | |
116 | port->connection = connection; | |
117 | } | |
118 | ||
119 | int bt_port_remove_from_component(struct bt_port *port) | |
8b285017 JG |
120 | { |
121 | int ret = 0; | |
72b913fb | 122 | struct bt_component *comp = NULL; |
8b285017 | 123 | |
72b913fb | 124 | if (!port) { |
8b285017 JG |
125 | ret = -1; |
126 | goto end; | |
127 | } | |
128 | ||
72b913fb PP |
129 | comp = (void *) bt_object_get_parent(port); |
130 | ret = bt_component_remove_port(comp, port); | |
131 | ||
8b285017 | 132 | end: |
72b913fb | 133 | bt_put(comp); |
8b285017 JG |
134 | return ret; |
135 | } | |
136 | ||
72b913fb | 137 | int bt_port_disconnect(struct bt_port *port) |
8b285017 | 138 | { |
72b913fb | 139 | int ret = 0; |
8b285017 | 140 | |
72b913fb PP |
141 | if (!port) { |
142 | ret = -1; | |
8b285017 JG |
143 | goto end; |
144 | } | |
145 | ||
72b913fb PP |
146 | if (port->connection) { |
147 | bt_connection_disconnect_ports(port->connection, NULL); | |
148 | } | |
149 | ||
8b285017 | 150 | end: |
72b913fb | 151 | return ret; |
8b285017 JG |
152 | } |
153 | ||
72b913fb | 154 | int bt_port_is_connected(struct bt_port *port) |
8b285017 | 155 | { |
72b913fb | 156 | int ret; |
8b285017 | 157 | |
72b913fb PP |
158 | if (!port) { |
159 | ret = -1; | |
8b285017 JG |
160 | goto end; |
161 | } | |
162 | ||
72b913fb PP |
163 | ret = port->connection ? 1 : 0; |
164 | ||
8b285017 | 165 | end: |
72b913fb | 166 | return ret; |
8b285017 | 167 | } |