| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
| 5 | * Copyright 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 6 | */ |
| 7 | |
| 8 | #define BT_LOG_TAG "LIB/PORT" |
| 9 | #include "lib/logging.h" |
| 10 | |
| 11 | #include "common/assert.h" |
| 12 | #include "lib/assert-cond.h" |
| 13 | #include <babeltrace2/graph/port.h> |
| 14 | #include <babeltrace2/graph/self-component-port.h> |
| 15 | #include "lib/object.h" |
| 16 | #include "compat/compiler.h" |
| 17 | |
| 18 | #include "component.h" |
| 19 | #include "connection.h" |
| 20 | #include "port.h" |
| 21 | |
| 22 | static |
| 23 | void destroy_port(struct bt_object *obj) |
| 24 | { |
| 25 | struct bt_port *port = (void *) obj; |
| 26 | |
| 27 | BT_LIB_LOGI("Destroying port: %!+p", port); |
| 28 | |
| 29 | if (port->name) { |
| 30 | g_string_free(port->name, TRUE); |
| 31 | port->name = NULL; |
| 32 | } |
| 33 | |
| 34 | g_free(port); |
| 35 | } |
| 36 | |
| 37 | BT_HIDDEN |
| 38 | struct bt_port *bt_port_create(struct bt_component *parent_component, |
| 39 | enum bt_port_type type, const char *name, void *user_data) |
| 40 | { |
| 41 | struct bt_port *port = NULL; |
| 42 | |
| 43 | BT_ASSERT(name); |
| 44 | BT_ASSERT(parent_component); |
| 45 | BT_ASSERT(type == BT_PORT_TYPE_INPUT || type == BT_PORT_TYPE_OUTPUT); |
| 46 | BT_ASSERT(strlen(name) > 0); |
| 47 | port = g_new0(struct bt_port, 1); |
| 48 | if (!port) { |
| 49 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one port."); |
| 50 | goto end; |
| 51 | } |
| 52 | |
| 53 | BT_LIB_LOGI("Creating port for component: %![comp-]+c, port-type=%s, " |
| 54 | "port-name=\"%s\"", parent_component, bt_port_type_string(type), |
| 55 | name); |
| 56 | bt_object_init_shared_with_parent(&port->base, destroy_port); |
| 57 | port->name = g_string_new(name); |
| 58 | if (!port->name) { |
| 59 | BT_LIB_LOGE_APPEND_CAUSE("Failed to allocate one GString."); |
| 60 | BT_OBJECT_PUT_REF_AND_RESET(port); |
| 61 | goto end; |
| 62 | } |
| 63 | |
| 64 | port->type = type; |
| 65 | port->user_data = user_data; |
| 66 | bt_object_set_parent(&port->base, &parent_component->base); |
| 67 | BT_LIB_LOGI("Created port for component: " |
| 68 | "%![comp-]+c, %![port-]+p", parent_component, port); |
| 69 | |
| 70 | end: |
| 71 | return port; |
| 72 | } |
| 73 | |
| 74 | const char *bt_port_get_name(const struct bt_port *port) |
| 75 | { |
| 76 | BT_ASSERT_PRE_DEV_NON_NULL(port, "Port"); |
| 77 | return port->name->str; |
| 78 | } |
| 79 | |
| 80 | enum bt_port_type bt_port_get_type(const struct bt_port *port) |
| 81 | { |
| 82 | BT_ASSERT_PRE_DEV_NON_NULL(port, "Port"); |
| 83 | return port->type; |
| 84 | } |
| 85 | |
| 86 | const struct bt_connection *bt_port_borrow_connection_const( |
| 87 | const struct bt_port *port) |
| 88 | { |
| 89 | BT_ASSERT_PRE_DEV_NON_NULL(port, "Port"); |
| 90 | return port->connection; |
| 91 | } |
| 92 | |
| 93 | const struct bt_component *bt_port_borrow_component_const( |
| 94 | const struct bt_port *port) |
| 95 | { |
| 96 | BT_ASSERT_PRE_DEV_NON_NULL(port, "Port"); |
| 97 | return bt_port_borrow_component_inline(port); |
| 98 | } |
| 99 | |
| 100 | struct bt_self_component *bt_self_component_port_borrow_component( |
| 101 | struct bt_self_component_port *port) |
| 102 | { |
| 103 | BT_ASSERT_PRE_DEV_NON_NULL(port, "Port"); |
| 104 | return (void *) bt_object_borrow_parent((void *) port); |
| 105 | } |
| 106 | |
| 107 | BT_HIDDEN |
| 108 | void bt_port_set_connection(struct bt_port *port, |
| 109 | struct bt_connection *connection) |
| 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 | BT_LIB_LOGI("Set port's connection: %![port-]+p, %![conn-]+x", port, |
| 118 | connection); |
| 119 | } |
| 120 | |
| 121 | bt_bool bt_port_is_connected(const struct bt_port *port) |
| 122 | { |
| 123 | BT_ASSERT_PRE_DEV_NON_NULL(port, "Port"); |
| 124 | return port->connection ? BT_TRUE : BT_FALSE; |
| 125 | } |
| 126 | |
| 127 | void *bt_self_component_port_get_data(const struct bt_self_component_port *port) |
| 128 | { |
| 129 | BT_ASSERT_PRE_DEV_NON_NULL(port, "Port"); |
| 130 | return ((struct bt_port *) port)->user_data; |
| 131 | } |
| 132 | |
| 133 | void bt_port_get_ref(const struct bt_port *port) |
| 134 | { |
| 135 | bt_object_get_ref(port); |
| 136 | } |
| 137 | |
| 138 | void bt_port_put_ref(const struct bt_port *port) |
| 139 | { |
| 140 | bt_object_put_ref(port); |
| 141 | } |
| 142 | |
| 143 | void bt_port_input_get_ref(const struct bt_port_input *port_input) |
| 144 | { |
| 145 | bt_object_get_ref(port_input); |
| 146 | } |
| 147 | |
| 148 | void bt_port_input_put_ref(const struct bt_port_input *port_input) |
| 149 | { |
| 150 | bt_object_put_ref(port_input); |
| 151 | } |
| 152 | |
| 153 | void bt_port_output_get_ref(const struct bt_port_output *port_output) |
| 154 | { |
| 155 | bt_object_get_ref(port_output); |
| 156 | } |
| 157 | |
| 158 | void bt_port_output_put_ref(const struct bt_port_output *port_output) |
| 159 | { |
| 160 | bt_object_put_ref(port_output); |
| 161 | } |