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