Commit | Line | Data |
---|---|---|
8b285017 | 1 | /* |
8b285017 JG |
2 | * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
3 | * | |
4 | * Author: Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
22 | * SOFTWARE. | |
23 | */ | |
24 | ||
f3034355 PP |
25 | #define BT_LOG_TAG "PORT" |
26 | #include <babeltrace/lib-logging-internal.h> | |
27 | ||
7b53201c PP |
28 | #include <babeltrace/graph/port-const.h> |
29 | #include <babeltrace/graph/port-input-const.h> | |
30 | #include <babeltrace/graph/port-output-const.h> | |
834e9996 PP |
31 | #include <babeltrace/graph/self-component-port.h> |
32 | #include <babeltrace/graph/self-component-port-input.h> | |
33 | #include <babeltrace/graph/self-component-port-output.h> | |
b2e0c907 PP |
34 | #include <babeltrace/graph/component-internal.h> |
35 | #include <babeltrace/graph/port-internal.h> | |
36 | #include <babeltrace/graph/connection-internal.h> | |
8b285017 | 37 | #include <babeltrace/object-internal.h> |
834e9996 | 38 | #include <babeltrace/object.h> |
3d9990ac | 39 | #include <babeltrace/compiler-internal.h> |
8b45963b | 40 | #include <babeltrace/assert-internal.h> |
834e9996 | 41 | #include <babeltrace/assert-pre-internal.h> |
8b285017 JG |
42 | |
43 | static | |
834e9996 | 44 | void destroy_port(struct bt_object *obj) |
8b285017 | 45 | { |
834e9996 | 46 | struct bt_port *port = (void *) obj; |
8b285017 | 47 | |
834e9996 | 48 | BT_LIB_LOGD("Destroying port: %!+p", port); |
f3034355 | 49 | |
8b285017 JG |
50 | if (port->name) { |
51 | g_string_free(port->name, TRUE); | |
834e9996 | 52 | port->name = NULL; |
8b285017 | 53 | } |
f3034355 | 54 | |
8b285017 JG |
55 | g_free(port); |
56 | } | |
57 | ||
58 | BT_HIDDEN | |
59 | struct bt_port *bt_port_create(struct bt_component *parent_component, | |
3e9b0023 | 60 | enum bt_port_type type, const char *name, void *user_data) |
8b285017 | 61 | { |
72b913fb | 62 | struct bt_port *port = NULL; |
8b285017 | 63 | |
8b45963b PP |
64 | BT_ASSERT(name); |
65 | BT_ASSERT(parent_component); | |
66 | BT_ASSERT(type == BT_PORT_TYPE_INPUT || type == BT_PORT_TYPE_OUTPUT); | |
834e9996 | 67 | BT_ASSERT(strlen(name) > 0); |
8b285017 JG |
68 | port = g_new0(struct bt_port, 1); |
69 | if (!port) { | |
f3034355 | 70 | BT_LOGE_STR("Failed to allocate one port."); |
8b285017 JG |
71 | goto end; |
72 | } | |
73 | ||
834e9996 PP |
74 | BT_LIB_LOGD("Creating port for component: %![comp-]+c, port-type=%s, " |
75 | "port-name=\"%s\"", parent_component, bt_port_type_string(type), | |
76 | name); | |
77 | bt_object_init_shared_with_parent(&port->base, destroy_port); | |
8b285017 JG |
78 | port->name = g_string_new(name); |
79 | if (!port->name) { | |
f3034355 | 80 | BT_LOGE_STR("Failed to allocate one GString."); |
8138bfe1 | 81 | BT_OBJECT_PUT_REF_AND_RESET(port); |
8b285017 JG |
82 | goto end; |
83 | } | |
84 | ||
85 | port->type = type; | |
3e9b0023 | 86 | port->user_data = user_data; |
1d7bf349 | 87 | bt_object_set_parent(&port->base, &parent_component->base); |
834e9996 PP |
88 | BT_LIB_LOGD("Created port for component: " |
89 | "%![comp-]+c, %![port-]+p", parent_component, port); | |
f3034355 | 90 | |
8b285017 JG |
91 | end: |
92 | return port; | |
93 | } | |
94 | ||
7b53201c | 95 | const char *bt_port_get_name(const struct bt_port *port) |
8b285017 | 96 | { |
834e9996 PP |
97 | BT_ASSERT_PRE_NON_NULL(port, "Port"); |
98 | return port->name->str; | |
8b285017 JG |
99 | } |
100 | ||
7b53201c | 101 | enum bt_port_type bt_port_get_type(const struct bt_port *port) |
8b285017 | 102 | { |
834e9996 PP |
103 | BT_ASSERT_PRE_NON_NULL(port, "Port"); |
104 | return port->type; | |
8b285017 JG |
105 | } |
106 | ||
7b53201c PP |
107 | const struct bt_connection *bt_port_borrow_connection_const( |
108 | const struct bt_port *port) | |
8b285017 | 109 | { |
834e9996 PP |
110 | BT_ASSERT_PRE_NON_NULL(port, "Port"); |
111 | return port->connection; | |
8b285017 JG |
112 | } |
113 | ||
7b53201c PP |
114 | const struct bt_component *bt_port_borrow_component_const( |
115 | const struct bt_port *port) | |
890882ef | 116 | { |
834e9996 | 117 | BT_ASSERT_PRE_NON_NULL(port, "Port"); |
7b53201c | 118 | return bt_port_borrow_component_inline(port); |
890882ef PP |
119 | } |
120 | ||
834e9996 PP |
121 | struct bt_self_component *bt_self_component_port_borrow_component( |
122 | struct bt_self_component_port *port) | |
890882ef | 123 | { |
834e9996 PP |
124 | BT_ASSERT_PRE_NON_NULL(port, "Port"); |
125 | return (void *) bt_object_borrow_parent((void *) port); | |
890882ef PP |
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; | |
834e9996 PP |
138 | BT_LIB_LOGV("Set port's connection: %![port-]+p, %![conn-]+x", port, |
139 | connection); | |
72b913fb PP |
140 | } |
141 | ||
834e9996 PP |
142 | enum bt_self_component_port_status bt_self_component_port_remove_from_component( |
143 | struct bt_self_component_port *self_port) | |
8b285017 | 144 | { |
834e9996 | 145 | struct bt_port *port = (void *) self_port; |
72b913fb | 146 | struct bt_component *comp = NULL; |
8b285017 | 147 | |
834e9996 | 148 | BT_ASSERT_PRE_NON_NULL(port, "Port"); |
8b285017 | 149 | |
834e9996 | 150 | comp = (void *) bt_object_borrow_parent(&port->base); |
a5fb1227 | 151 | if (!comp) { |
834e9996 PP |
152 | BT_LIB_LOGV("Port already removed from its component: %!+p", |
153 | port); | |
a5fb1227 PP |
154 | goto end; |
155 | } | |
f3034355 PP |
156 | |
157 | /* bt_component_remove_port() logs details */ | |
834e9996 | 158 | bt_component_remove_port(comp, port); |
72b913fb | 159 | |
8b285017 | 160 | end: |
834e9996 | 161 | return BT_SELF_PORT_STATUS_OK; |
8b285017 JG |
162 | } |
163 | ||
7b53201c | 164 | bt_bool bt_port_is_connected(const struct bt_port *port) |
8b285017 | 165 | { |
834e9996 PP |
166 | BT_ASSERT_PRE_NON_NULL(port, "Port"); |
167 | return port->connection ? BT_TRUE : BT_FALSE; | |
8b285017 | 168 | } |
a45a0b60 | 169 | |
7b53201c | 170 | void *bt_self_component_port_get_data(const struct bt_self_component_port *port) |
a45a0b60 | 171 | { |
834e9996 PP |
172 | BT_ASSERT_PRE_NON_NULL(port, "Port"); |
173 | return ((struct bt_port *) port)->user_data; | |
a45a0b60 | 174 | } |