| 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 | #define BT_LOG_TAG "PORT" |
| 30 | #include <babeltrace/lib-logging-internal.h> |
| 31 | |
| 32 | #include <babeltrace/graph/port.h> |
| 33 | #include <babeltrace/graph/component-internal.h> |
| 34 | #include <babeltrace/graph/port-internal.h> |
| 35 | #include <babeltrace/graph/connection-internal.h> |
| 36 | #include <babeltrace/object-internal.h> |
| 37 | #include <babeltrace/compiler-internal.h> |
| 38 | |
| 39 | static |
| 40 | void bt_port_destroy(struct bt_object *obj) |
| 41 | { |
| 42 | struct bt_port *port = container_of(obj, struct bt_port, base); |
| 43 | |
| 44 | BT_LOGD("Destroying port: addr=%p, name=\"%s\", comp-addr=%p", |
| 45 | port, bt_port_get_name(port), obj->parent); |
| 46 | |
| 47 | if (port->name) { |
| 48 | g_string_free(port->name, TRUE); |
| 49 | } |
| 50 | |
| 51 | g_free(port); |
| 52 | } |
| 53 | |
| 54 | struct bt_port *bt_port_from_private( |
| 55 | struct bt_private_port *private_port) |
| 56 | { |
| 57 | return bt_get(bt_port_borrow_from_private(private_port)); |
| 58 | } |
| 59 | |
| 60 | BT_HIDDEN |
| 61 | struct bt_port *bt_port_create(struct bt_component *parent_component, |
| 62 | enum bt_port_type type, const char *name, void *user_data) |
| 63 | { |
| 64 | struct bt_port *port = NULL; |
| 65 | |
| 66 | assert(name); |
| 67 | assert(parent_component); |
| 68 | assert(type == BT_PORT_TYPE_INPUT || type == BT_PORT_TYPE_OUTPUT); |
| 69 | |
| 70 | if (strlen(name) == 0) { |
| 71 | BT_LOGW_STR("Invalid parameter: name is an empty string."); |
| 72 | goto end; |
| 73 | } |
| 74 | |
| 75 | port = g_new0(struct bt_port, 1); |
| 76 | if (!port) { |
| 77 | BT_LOGE_STR("Failed to allocate one port."); |
| 78 | goto end; |
| 79 | } |
| 80 | |
| 81 | BT_LOGD("Creating port for component: " |
| 82 | "comp-addr=%p, comp-name=\"%s\", port-type=%s, " |
| 83 | "port-name=\"%s\"", |
| 84 | parent_component, bt_component_get_name(parent_component), |
| 85 | bt_port_type_string(type), name); |
| 86 | |
| 87 | bt_object_init(port, bt_port_destroy); |
| 88 | port->name = g_string_new(name); |
| 89 | if (!port->name) { |
| 90 | BT_LOGE_STR("Failed to allocate one GString."); |
| 91 | BT_PUT(port); |
| 92 | goto end; |
| 93 | } |
| 94 | |
| 95 | port->type = type; |
| 96 | port->user_data = user_data; |
| 97 | bt_object_set_parent(port, &parent_component->base); |
| 98 | BT_LOGD("Created port for component: " |
| 99 | "comp-addr=%p, comp-name=\"%s\", port-type=%s, " |
| 100 | "port-name=\"%s\", port-addr=%p", |
| 101 | parent_component, bt_component_get_name(parent_component), |
| 102 | bt_port_type_string(type), name, port); |
| 103 | |
| 104 | end: |
| 105 | return port; |
| 106 | } |
| 107 | |
| 108 | const char *bt_port_get_name(struct bt_port *port) |
| 109 | { |
| 110 | return port ? port->name->str : NULL; |
| 111 | } |
| 112 | |
| 113 | enum bt_port_type bt_port_get_type(struct bt_port *port) |
| 114 | { |
| 115 | return port ? port->type : BT_PORT_TYPE_UNKOWN; |
| 116 | } |
| 117 | |
| 118 | struct bt_connection *bt_port_get_connection(struct bt_port *port) |
| 119 | { |
| 120 | struct bt_connection *connection = NULL; |
| 121 | |
| 122 | if (!port) { |
| 123 | BT_LOGW_STR("Invalid parameter: port is NULL."); |
| 124 | goto end; |
| 125 | } |
| 126 | |
| 127 | if (!port->connection) { |
| 128 | /* Not an error: means disconnected */ |
| 129 | goto end; |
| 130 | } |
| 131 | |
| 132 | connection = bt_get(port->connection); |
| 133 | |
| 134 | end: |
| 135 | return connection; |
| 136 | } |
| 137 | |
| 138 | struct bt_component *bt_port_get_component(struct bt_port *port) |
| 139 | { |
| 140 | return (struct bt_component *) bt_object_get_parent(port); |
| 141 | } |
| 142 | |
| 143 | struct bt_private_connection *bt_private_port_get_private_connection( |
| 144 | struct bt_private_port *private_port) |
| 145 | { |
| 146 | return bt_private_connection_from_connection(bt_port_get_connection( |
| 147 | bt_port_borrow_from_private(private_port))); |
| 148 | } |
| 149 | |
| 150 | struct bt_private_component *bt_private_port_get_private_component( |
| 151 | struct bt_private_port *private_port) |
| 152 | { |
| 153 | return bt_private_component_from_component(bt_port_get_component( |
| 154 | bt_port_borrow_from_private(private_port))); |
| 155 | } |
| 156 | |
| 157 | BT_HIDDEN |
| 158 | void bt_port_set_connection(struct bt_port *port, |
| 159 | struct bt_connection *connection) |
| 160 | { |
| 161 | /* |
| 162 | * Don't take a reference on connection as its existence is |
| 163 | * guaranteed by the existence of the graph in which the |
| 164 | * connection exists. |
| 165 | */ |
| 166 | port->connection = connection; |
| 167 | BT_LOGV("Set port's connection: " |
| 168 | "port-addr=%p, port-name=\"%s\", conn-addr=%p", |
| 169 | port, bt_port_get_name(port), connection); |
| 170 | } |
| 171 | |
| 172 | enum bt_port_status bt_private_port_remove_from_component( |
| 173 | struct bt_private_port *private_port) |
| 174 | { |
| 175 | enum bt_port_status status = BT_PORT_STATUS_OK; |
| 176 | struct bt_port *port = bt_port_borrow_from_private(private_port); |
| 177 | struct bt_component *comp = NULL; |
| 178 | enum bt_component_status comp_status; |
| 179 | |
| 180 | if (!port) { |
| 181 | BT_LOGW_STR("Invalid parameter: private port is NULL."); |
| 182 | status = BT_PORT_STATUS_INVALID; |
| 183 | goto end; |
| 184 | } |
| 185 | |
| 186 | comp = (void *) bt_object_get_parent(port); |
| 187 | if (!comp) { |
| 188 | BT_LOGV("Port already removed from its component: " |
| 189 | "port-addr=%p, port-name=\"%s\", ", |
| 190 | port, bt_port_get_name(port)); |
| 191 | goto end; |
| 192 | } |
| 193 | |
| 194 | /* bt_component_remove_port() logs details */ |
| 195 | comp_status = bt_component_remove_port(comp, port); |
| 196 | assert(comp_status != BT_COMPONENT_STATUS_INVALID); |
| 197 | if (comp_status < 0) { |
| 198 | status = BT_PORT_STATUS_ERROR; |
| 199 | goto end; |
| 200 | } |
| 201 | |
| 202 | end: |
| 203 | bt_put(comp); |
| 204 | return status; |
| 205 | } |
| 206 | |
| 207 | enum bt_port_status bt_port_disconnect(struct bt_port *port) |
| 208 | { |
| 209 | enum bt_port_status status = BT_PORT_STATUS_OK; |
| 210 | |
| 211 | if (!port) { |
| 212 | BT_LOGW_STR("Invalid parameter: port is NULL."); |
| 213 | status = BT_PORT_STATUS_INVALID; |
| 214 | goto end; |
| 215 | } |
| 216 | |
| 217 | if (port->connection) { |
| 218 | bt_connection_end(port->connection, true); |
| 219 | BT_LOGV("Disconnected port: " |
| 220 | "port-addr=%p, port-name=\"%s\"", |
| 221 | port, bt_port_get_name(port)); |
| 222 | } |
| 223 | |
| 224 | end: |
| 225 | return status; |
| 226 | } |
| 227 | |
| 228 | bt_bool bt_port_is_connected(struct bt_port *port) |
| 229 | { |
| 230 | int ret; |
| 231 | |
| 232 | if (!port) { |
| 233 | BT_LOGW_STR("Invalid parameter: port is NULL."); |
| 234 | ret = -1; |
| 235 | goto end; |
| 236 | } |
| 237 | |
| 238 | ret = port->connection ? 1 : 0; |
| 239 | |
| 240 | end: |
| 241 | return ret; |
| 242 | } |
| 243 | |
| 244 | void *bt_private_port_get_user_data( |
| 245 | struct bt_private_port *private_port) |
| 246 | { |
| 247 | return private_port ? |
| 248 | bt_port_borrow_from_private(private_port)->user_data : NULL; |
| 249 | } |