|
| 1 | +/* Node type: OPAL-RT Asynchronous Process (libOpalAsyncApi) |
| 2 | + * |
| 3 | + * Author: Steffen Vogel <[email protected]> |
| 4 | + * SPDX-FileCopyrightText: 2023-2025 OPAL-RT Germany GmbH |
| 5 | + * SPDX-License-Identifier: Apache-2.0 |
| 6 | + */ |
| 7 | + |
| 8 | +#pragma once |
| 9 | + |
| 10 | +#include <condition_variable> |
| 11 | +#include <mutex> |
| 12 | + |
| 13 | +#include <spdlog/details/log_msg_buffer.h> |
| 14 | +#include <spdlog/details/null_mutex.h> |
| 15 | +#include <spdlog/sinks/base_sink.h> |
| 16 | + |
| 17 | +// Define RTLAB before including OpalPrint.h for messages to be sent |
| 18 | +// to the OpalDisplay. Otherwise stdout will be used. */ |
| 19 | +#define RTLAB |
| 20 | + |
| 21 | +#include <AsyncApi.h> |
| 22 | +#include <OpalGenAsyncParamCtrl.h> |
| 23 | +#include <OpalPrint.h> |
| 24 | + |
| 25 | +#include <villas/format.hpp> |
| 26 | +#include <villas/node.hpp> |
| 27 | +#include <villas/popen.hpp> |
| 28 | +#include <villas/sample.hpp> |
| 29 | + |
| 30 | +namespace villas { |
| 31 | +namespace node { |
| 32 | +namespace opal { |
| 33 | + |
| 34 | +class LogSink final : public spdlog::sinks::base_sink<std::mutex> { |
| 35 | +private: |
| 36 | + std::string shmemSystemCtrlName; |
| 37 | + |
| 38 | +public: |
| 39 | + explicit LogSink(const std::string &shmemName); |
| 40 | + |
| 41 | + ~LogSink() override; |
| 42 | + |
| 43 | +protected: |
| 44 | + void sink_it_(const spdlog::details::log_msg &msg) override; |
| 45 | + |
| 46 | + void flush_() override {} |
| 47 | +}; |
| 48 | + |
| 49 | +} // namespace opal |
| 50 | + |
| 51 | +// Forward declarations |
| 52 | +struct Sample; |
| 53 | + |
| 54 | +class OpalAsyncNode : public Node { |
| 55 | + |
| 56 | +protected: |
| 57 | + // RT-LAB -> VILLASnode |
| 58 | + // Corresponds to AsyncAPI's *Send* direction |
| 59 | + struct { |
| 60 | + unsigned id; |
| 61 | + bool present; |
| 62 | + unsigned length; |
| 63 | + |
| 64 | + bool reply; |
| 65 | + int mode; |
| 66 | + |
| 67 | + Opal_SendAsyncParam params; |
| 68 | + } in; |
| 69 | + |
| 70 | + // VILLASnode -> RT-LAB |
| 71 | + // Corresponds to AsyncAPI's *Recv* direction |
| 72 | + struct { |
| 73 | + unsigned id; |
| 74 | + bool present; |
| 75 | + unsigned length; |
| 76 | + |
| 77 | + Opal_RecvAsyncParam params; |
| 78 | + } out; |
| 79 | + |
| 80 | + bool ready; |
| 81 | + std::mutex readyLock; |
| 82 | + std::condition_variable readyCv; |
| 83 | + |
| 84 | + virtual int _read(struct Sample *smps[], unsigned cnt) override; |
| 85 | + |
| 86 | + virtual int _write(struct Sample *smps[], unsigned cnt) override; |
| 87 | + |
| 88 | +public: |
| 89 | + OpalAsyncNode(const uuid_t &id = {}, const std::string &name = "") |
| 90 | + : Node(id, name), ready(false) { |
| 91 | + in.id = 1; |
| 92 | + in.present = false; |
| 93 | + in.length = 0; |
| 94 | + in.reply = true; |
| 95 | + |
| 96 | + out.id = 1; |
| 97 | + out.present = false; |
| 98 | + out.length = 0; |
| 99 | + } |
| 100 | + |
| 101 | + virtual const std::string &getDetails() override; |
| 102 | + |
| 103 | + virtual int start() override; |
| 104 | + virtual int stop() override; |
| 105 | + |
| 106 | + virtual int parse(json_t *json) override; |
| 107 | + |
| 108 | + void markReady(); |
| 109 | + void waitReady(); |
| 110 | +}; |
| 111 | + |
| 112 | +class OpalAsyncNodeFactory : public NodeFactory { |
| 113 | + |
| 114 | +public: |
| 115 | + using NodeFactory::NodeFactory; |
| 116 | + |
| 117 | + virtual Node *make(const uuid_t &id = {}, |
| 118 | + const std::string &nme = "") override { |
| 119 | + auto *n = new OpalAsyncNode(id, nme); |
| 120 | + |
| 121 | + init(n); |
| 122 | + |
| 123 | + return n; |
| 124 | + } |
| 125 | + |
| 126 | + virtual int getFlags() const override { |
| 127 | + return (int)NodeFactory::Flags::SUPPORTS_READ | |
| 128 | + (int)NodeFactory::Flags::SUPPORTS_WRITE | |
| 129 | + (int)NodeFactory::Flags::PROVIDES_SIGNALS; |
| 130 | + } |
| 131 | + |
| 132 | + virtual std::string getName() const override { return "opal.async"; } |
| 133 | + |
| 134 | + virtual std::string getDescription() const override { |
| 135 | + return "OPAL Asynchronous Process (libOpalAsyncApi)"; |
| 136 | + } |
| 137 | + |
| 138 | + virtual int getVectorize() const override { return 1; } |
| 139 | + |
| 140 | + virtual int start(SuperNode *sn) override; |
| 141 | + |
| 142 | + virtual int stop() override; |
| 143 | +}; |
| 144 | + |
| 145 | +} // namespace node |
| 146 | +} // namespace villas |
0 commit comments