| 1 | /* |
| 2 | * SPDX-License-Identifier: GPL-2.0-only |
| 3 | * |
| 4 | * Copyright (C) 2020-2023 EfficiOS, inc. |
| 5 | */ |
| 6 | |
| 7 | #include <utility> |
| 8 | |
| 9 | #include "run-in.hpp" |
| 10 | #include "common/assert.h" |
| 11 | |
| 12 | struct RunInData final |
| 13 | { |
| 14 | RunInCompClsQueryFunc compClsCtxFunc; |
| 15 | RunInCompClsInitFunc compCtxFunc; |
| 16 | RunInMsgIterClsInitFunc msgIterCtxFunc; |
| 17 | }; |
| 18 | |
| 19 | static const RunInData& runInDataFromMethodData(void * const methodData) |
| 20 | { |
| 21 | return *static_cast<const RunInData *>(methodData); |
| 22 | } |
| 23 | |
| 24 | static bt_component_class_initialize_method_status |
| 25 | compClsInit(bt_self_component_source * const selfComp, bt_self_component_source_configuration *, |
| 26 | const bt_value *, void * const initMethodData) |
| 27 | { |
| 28 | const auto status = |
| 29 | bt_self_component_source_add_output_port(selfComp, "out", initMethodData, nullptr); |
| 30 | |
| 31 | BT_ASSERT(status == BT_SELF_COMPONENT_ADD_PORT_STATUS_OK); |
| 32 | |
| 33 | auto& data = runInDataFromMethodData(initMethodData); |
| 34 | |
| 35 | if (data.compCtxFunc) { |
| 36 | data.compCtxFunc(bt_self_component_source_as_self_component(selfComp)); |
| 37 | } |
| 38 | |
| 39 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; |
| 40 | } |
| 41 | |
| 42 | static bt_component_class_query_method_status |
| 43 | compClsQuery(bt_self_component_class_source * const selfCompCls, bt_private_query_executor *, |
| 44 | const char *, const bt_value *, void * const methodData, |
| 45 | const bt_value ** const result) |
| 46 | { |
| 47 | auto& data = runInDataFromMethodData(methodData); |
| 48 | |
| 49 | if (data.compClsCtxFunc) { |
| 50 | data.compClsCtxFunc(bt_self_component_class_source_as_self_component_class(selfCompCls)); |
| 51 | } |
| 52 | |
| 53 | *result = bt_value_null; |
| 54 | return BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; |
| 55 | } |
| 56 | |
| 57 | static bt_message_iterator_class_initialize_method_status |
| 58 | msgIterClsInit(bt_self_message_iterator * const selfMsgIter, |
| 59 | bt_self_message_iterator_configuration *, bt_self_component_port_output * const port) |
| 60 | { |
| 61 | auto& data = runInDataFromMethodData(bt_self_component_port_get_data( |
| 62 | bt_self_component_port_output_as_self_component_port(port))); |
| 63 | |
| 64 | if (data.msgIterCtxFunc) { |
| 65 | data.msgIterCtxFunc(selfMsgIter); |
| 66 | } |
| 67 | |
| 68 | return BT_MESSAGE_ITERATOR_CLASS_INITIALIZE_METHOD_STATUS_OK; |
| 69 | } |
| 70 | |
| 71 | static bt_message_iterator_class_next_method_status |
| 72 | msgIterClsNext(bt_self_message_iterator *, bt_message_array_const, uint64_t, uint64_t *) |
| 73 | { |
| 74 | return BT_MESSAGE_ITERATOR_CLASS_NEXT_METHOD_STATUS_END; |
| 75 | } |
| 76 | |
| 77 | struct DummySinkData |
| 78 | { |
| 79 | bt_message_iterator *msgIter; |
| 80 | }; |
| 81 | |
| 82 | static bt_component_class_initialize_method_status |
| 83 | dummySinkInit(bt_self_component_sink * const self, bt_self_component_sink_configuration * const, |
| 84 | const bt_value * const, void * const initMethodData) |
| 85 | { |
| 86 | const auto status = bt_self_component_sink_add_input_port(self, "in", NULL, nullptr); |
| 87 | BT_ASSERT(status == BT_SELF_COMPONENT_ADD_PORT_STATUS_OK); |
| 88 | bt_self_component_set_data(bt_self_component_sink_as_self_component(self), initMethodData); |
| 89 | return BT_COMPONENT_CLASS_INITIALIZE_METHOD_STATUS_OK; |
| 90 | } |
| 91 | |
| 92 | static DummySinkData& dummySinkDataFromSelfCompSink(bt_self_component_sink * const self) |
| 93 | { |
| 94 | return *static_cast<DummySinkData *>( |
| 95 | bt_self_component_get_data(bt_self_component_sink_as_self_component(self))); |
| 96 | } |
| 97 | |
| 98 | static bt_component_class_sink_graph_is_configured_method_status |
| 99 | dummySinkGraphIsConfigured(bt_self_component_sink * const self) |
| 100 | { |
| 101 | auto& data = dummySinkDataFromSelfCompSink(self); |
| 102 | const auto port = bt_self_component_sink_borrow_input_port_by_name(self, "in"); |
| 103 | BT_ASSERT(port); |
| 104 | const auto status = bt_message_iterator_create_from_sink_component(self, port, &data.msgIter); |
| 105 | BT_ASSERT(status == BT_MESSAGE_ITERATOR_CREATE_FROM_SINK_COMPONENT_STATUS_OK); |
| 106 | return BT_COMPONENT_CLASS_SINK_GRAPH_IS_CONFIGURED_METHOD_STATUS_OK; |
| 107 | } |
| 108 | |
| 109 | static bt_component_class_sink_consume_method_status |
| 110 | dummySinkConsume(bt_self_component_sink * const self) |
| 111 | { |
| 112 | const auto& data = dummySinkDataFromSelfCompSink(self); |
| 113 | bt_message_array_const msgs; |
| 114 | uint64_t msgCount; |
| 115 | const auto status = bt_message_iterator_next(data.msgIter, &msgs, &msgCount); |
| 116 | BT_ASSERT(status == BT_MESSAGE_ITERATOR_NEXT_STATUS_END); |
| 117 | return BT_COMPONENT_CLASS_SINK_CONSUME_METHOD_STATUS_END; |
| 118 | } |
| 119 | |
| 120 | void runIn(RunInCompClsQueryFunc compClsCtxFunc, RunInCompClsInitFunc compCtxFunc, |
| 121 | RunInMsgIterClsInitFunc msgIterCtxFunc) |
| 122 | { |
| 123 | RunInData data {std::move(compClsCtxFunc), std::move(compCtxFunc), std::move(msgIterCtxFunc)}; |
| 124 | |
| 125 | /* Create and configure custom source component class */ |
| 126 | const auto msgIterCls = bt_message_iterator_class_create(msgIterClsNext); |
| 127 | |
| 128 | BT_ASSERT(msgIterCls); |
| 129 | |
| 130 | { |
| 131 | const auto status = |
| 132 | bt_message_iterator_class_set_initialize_method(msgIterCls, msgIterClsInit); |
| 133 | |
| 134 | BT_ASSERT(status == BT_MESSAGE_ITERATOR_CLASS_SET_METHOD_STATUS_OK); |
| 135 | } |
| 136 | |
| 137 | const auto srcCompCls = bt_component_class_source_create("yo", msgIterCls); |
| 138 | |
| 139 | BT_ASSERT(srcCompCls); |
| 140 | |
| 141 | { |
| 142 | const auto status = |
| 143 | bt_component_class_source_set_initialize_method(srcCompCls, compClsInit); |
| 144 | |
| 145 | BT_ASSERT(status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK); |
| 146 | } |
| 147 | |
| 148 | { |
| 149 | const auto status = bt_component_class_source_set_query_method(srcCompCls, compClsQuery); |
| 150 | |
| 151 | BT_ASSERT(status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK); |
| 152 | } |
| 153 | |
| 154 | /* Execute a query (executes `compClsCtxFunc`) */ |
| 155 | { |
| 156 | const auto queryExec = bt_query_executor_create_with_method_data( |
| 157 | bt_component_class_source_as_component_class(srcCompCls), "", nullptr, &data); |
| 158 | |
| 159 | BT_ASSERT(queryExec); |
| 160 | |
| 161 | const bt_value *queryRes; |
| 162 | const auto status = bt_query_executor_query(queryExec, &queryRes); |
| 163 | |
| 164 | BT_ASSERT(status == BT_QUERY_EXECUTOR_QUERY_STATUS_OK); |
| 165 | |
| 166 | bt_value_put_ref(queryRes); |
| 167 | bt_query_executor_put_ref(queryExec); |
| 168 | } |
| 169 | |
| 170 | /* Create a dummy sink component */ |
| 171 | const auto sinkCompCls = bt_component_class_sink_create("dummy", dummySinkConsume); |
| 172 | BT_ASSERT(sinkCompCls); |
| 173 | |
| 174 | { |
| 175 | const auto status = |
| 176 | bt_component_class_sink_set_initialize_method(sinkCompCls, dummySinkInit); |
| 177 | BT_ASSERT(status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK); |
| 178 | } |
| 179 | |
| 180 | { |
| 181 | const auto status = bt_component_class_sink_set_graph_is_configured_method( |
| 182 | sinkCompCls, dummySinkGraphIsConfigured); |
| 183 | BT_ASSERT(status == BT_COMPONENT_CLASS_SET_METHOD_STATUS_OK); |
| 184 | } |
| 185 | |
| 186 | /* Create graph */ |
| 187 | const auto graph = bt_graph_create(0); |
| 188 | |
| 189 | BT_ASSERT(graph); |
| 190 | |
| 191 | /* Add custom source component (executes `compCtxFunc`) */ |
| 192 | const bt_component_source *srcComp; |
| 193 | |
| 194 | { |
| 195 | const auto status = bt_graph_add_source_component_with_initialize_method_data( |
| 196 | graph, srcCompCls, "the-source", NULL, &data, BT_LOGGING_LEVEL_NONE, &srcComp); |
| 197 | |
| 198 | BT_ASSERT(status == BT_GRAPH_ADD_COMPONENT_STATUS_OK); |
| 199 | } |
| 200 | |
| 201 | /* Add dummy sink component */ |
| 202 | const bt_component_sink *sinkComp; |
| 203 | DummySinkData dummySinkData; |
| 204 | |
| 205 | { |
| 206 | const auto status = bt_graph_add_sink_component_with_initialize_method_data( |
| 207 | graph, sinkCompCls, "the-sink", NULL, &dummySinkData, BT_LOGGING_LEVEL_NONE, &sinkComp); |
| 208 | BT_ASSERT(status == BT_GRAPH_ADD_COMPONENT_STATUS_OK); |
| 209 | } |
| 210 | |
| 211 | /* Connect ports */ |
| 212 | { |
| 213 | const auto outPort = bt_component_source_borrow_output_port_by_name_const(srcComp, "out"); |
| 214 | |
| 215 | BT_ASSERT(outPort); |
| 216 | |
| 217 | const auto inPort = bt_component_sink_borrow_input_port_by_name_const(sinkComp, "in"); |
| 218 | |
| 219 | BT_ASSERT(inPort); |
| 220 | |
| 221 | const auto status = bt_graph_connect_ports(graph, outPort, inPort, nullptr); |
| 222 | |
| 223 | BT_ASSERT(status == BT_GRAPH_CONNECT_PORTS_STATUS_OK); |
| 224 | } |
| 225 | |
| 226 | /* Run graph (executes `msgIterCtxFunc`) */ |
| 227 | const auto status = bt_graph_run(graph); |
| 228 | |
| 229 | BT_ASSERT(status == BT_GRAPH_RUN_STATUS_OK); |
| 230 | |
| 231 | /* Discard plugin and graph */ |
| 232 | bt_graph_put_ref(graph); |
| 233 | bt_component_class_source_put_ref(srcCompCls); |
| 234 | bt_component_class_sink_put_ref(sinkCompCls); |
| 235 | bt_message_iterator_class_put_ref(msgIterCls); |
| 236 | } |
| 237 | |
| 238 | void runInCompClsQuery(RunInCompClsQueryFunc func) |
| 239 | { |
| 240 | runIn(std::move(func), nullptr, nullptr); |
| 241 | } |
| 242 | |
| 243 | void runInCompClsInit(RunInCompClsInitFunc func) |
| 244 | { |
| 245 | runIn(nullptr, std::move(func), nullptr); |
| 246 | } |
| 247 | |
| 248 | void runInMsgIterClsInit(RunInMsgIterClsInitFunc func) |
| 249 | { |
| 250 | runIn(nullptr, nullptr, std::move(func)); |
| 251 | } |