| 1 | /* |
| 2 | * SPDX-License-Identifier: MIT |
| 3 | * |
| 4 | * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com> |
| 5 | * Copyright 2015 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 6 | */ |
| 7 | |
| 8 | #define BT_LOG_TAG "LIB/COMPONENT-SOURCE" |
| 9 | #include "lib/logging.h" |
| 10 | |
| 11 | #include "common/assert.h" |
| 12 | #include "lib/assert-cond.h" |
| 13 | #include "compat/compiler.h" |
| 14 | #include <babeltrace2/graph/self-component.h> |
| 15 | #include <babeltrace2/graph/component.h> |
| 16 | #include <babeltrace2/graph/graph.h> |
| 17 | |
| 18 | #include "component-source.h" |
| 19 | #include "component.h" |
| 20 | #include "port.h" |
| 21 | #include "iterator.h" |
| 22 | #include "lib/func-status.h" |
| 23 | |
| 24 | struct bt_component *bt_component_source_create(void) |
| 25 | { |
| 26 | struct bt_component_source *source = NULL; |
| 27 | |
| 28 | source = g_new0(struct bt_component_source, 1); |
| 29 | if (!source) { |
| 30 | BT_LIB_LOGE_APPEND_CAUSE( |
| 31 | "Failed to allocate one source component."); |
| 32 | goto end; |
| 33 | } |
| 34 | |
| 35 | end: |
| 36 | return (void *) source; |
| 37 | } |
| 38 | |
| 39 | BT_EXPORT |
| 40 | const bt_component_class_source * |
| 41 | bt_component_source_borrow_class_const( |
| 42 | const bt_component_source *component) |
| 43 | { |
| 44 | struct bt_component_class *cls; |
| 45 | |
| 46 | BT_ASSERT_PRE_DEV_COMP_NON_NULL(component); |
| 47 | |
| 48 | cls = component->parent.class; |
| 49 | |
| 50 | BT_ASSERT_DBG(cls); |
| 51 | BT_ASSERT_DBG(cls->type == BT_COMPONENT_CLASS_TYPE_SOURCE); |
| 52 | |
| 53 | return (bt_component_class_source *) cls; |
| 54 | } |
| 55 | |
| 56 | BT_EXPORT |
| 57 | uint64_t bt_component_source_get_output_port_count( |
| 58 | const struct bt_component_source *comp) |
| 59 | { |
| 60 | /* bt_component_get_output_port_count() checks preconditions */ |
| 61 | return bt_component_get_output_port_count((void *) comp, __func__); |
| 62 | } |
| 63 | |
| 64 | BT_EXPORT |
| 65 | const struct bt_port_output * |
| 66 | bt_component_source_borrow_output_port_by_name_const( |
| 67 | const struct bt_component_source *comp, const char *name) |
| 68 | { |
| 69 | /* |
| 70 | * bt_component_borrow_output_port_by_name() logs details/errors |
| 71 | * and checks preconditions. |
| 72 | */ |
| 73 | return bt_component_borrow_output_port_by_name((void *) comp, name, |
| 74 | __func__); |
| 75 | } |
| 76 | |
| 77 | BT_EXPORT |
| 78 | struct bt_self_component_port_output * |
| 79 | bt_self_component_source_borrow_output_port_by_name( |
| 80 | struct bt_self_component_source *comp, const char *name) |
| 81 | { |
| 82 | /* |
| 83 | * bt_component_borrow_output_port_by_name() logs details/errors |
| 84 | * and checks preconditions. |
| 85 | */ |
| 86 | return (void *) bt_component_borrow_output_port_by_name( |
| 87 | (void *) comp, name, __func__); |
| 88 | } |
| 89 | |
| 90 | BT_EXPORT |
| 91 | const struct bt_port_output * |
| 92 | bt_component_source_borrow_output_port_by_index_const( |
| 93 | const struct bt_component_source *comp, uint64_t index) |
| 94 | { |
| 95 | /* |
| 96 | * bt_component_borrow_output_port_by_index() logs |
| 97 | * details/errors and checks preconditions. |
| 98 | */ |
| 99 | return bt_component_borrow_output_port_by_index((void *) comp, index, |
| 100 | __func__); |
| 101 | } |
| 102 | |
| 103 | BT_EXPORT |
| 104 | struct bt_self_component_port_output * |
| 105 | bt_self_component_source_borrow_output_port_by_index( |
| 106 | struct bt_self_component_source *comp, uint64_t index) |
| 107 | { |
| 108 | /* |
| 109 | * bt_component_borrow_output_port_by_index() logs |
| 110 | * details/errors and checks preconditions. |
| 111 | */ |
| 112 | return (void *) bt_component_borrow_output_port_by_index( |
| 113 | (void *) comp, index, __func__); |
| 114 | } |
| 115 | |
| 116 | BT_EXPORT |
| 117 | enum bt_self_component_add_port_status bt_self_component_source_add_output_port( |
| 118 | struct bt_self_component_source *self_comp, |
| 119 | const char *name, void *user_data, |
| 120 | struct bt_self_component_port_output **self_port) |
| 121 | { |
| 122 | struct bt_component *comp = (void *) self_comp; |
| 123 | enum bt_self_component_add_port_status status; |
| 124 | struct bt_port *port = NULL; |
| 125 | |
| 126 | /* |
| 127 | * bt_component_add_output_port() logs details/errors and checks |
| 128 | * preconditions. |
| 129 | */ |
| 130 | status = bt_component_add_output_port(comp, name, user_data, &port, |
| 131 | __func__); |
| 132 | if (status != BT_FUNC_STATUS_OK) { |
| 133 | goto end; |
| 134 | } |
| 135 | |
| 136 | if (self_port) { |
| 137 | /* Move reference to user */ |
| 138 | *self_port = (void *) port; |
| 139 | } |
| 140 | |
| 141 | end: |
| 142 | bt_object_put_ref(port); |
| 143 | return status; |
| 144 | } |
| 145 | |
| 146 | BT_EXPORT |
| 147 | void bt_component_source_get_ref( |
| 148 | const struct bt_component_source *component_source) |
| 149 | { |
| 150 | bt_object_get_ref(component_source); |
| 151 | } |
| 152 | |
| 153 | BT_EXPORT |
| 154 | void bt_component_source_put_ref( |
| 155 | const struct bt_component_source *component_source) |
| 156 | { |
| 157 | bt_object_put_ref(component_source); |
| 158 | } |