From 5c3d801b79d2337b4eb8cf1e8da667168179674d Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Mon, 26 Feb 2024 08:59:56 +0000 Subject: [PATCH] wip: villas: Minor fixes and tweaks for VillasInterface --- .../dpsim-villas/InterfaceWorkerVillas.h | 2 +- dpsim-villas/src/InterfaceWorkerVillas.cpp | 95 +++++++++---------- 2 files changed, 48 insertions(+), 49 deletions(-) diff --git a/dpsim-villas/include/dpsim-villas/InterfaceWorkerVillas.h b/dpsim-villas/include/dpsim-villas/InterfaceWorkerVillas.h index 713b2c590e..b094d17bcf 100644 --- a/dpsim-villas/include/dpsim-villas/InterfaceWorkerVillas.h +++ b/dpsim-villas/include/dpsim-villas/InterfaceWorkerVillas.h @@ -22,7 +22,7 @@ class InterfaceWorkerVillas : public InterfaceWorker, public: using Ptr = std::shared_ptr; - using Sample = struct node::Sample; + using Sample = node::Sample; static UInt villasPriority; static UInt villasAffinity; diff --git a/dpsim-villas/src/InterfaceWorkerVillas.cpp b/dpsim-villas/src/InterfaceWorkerVillas.cpp index 9c7a6c15a4..1e55547e3c 100644 --- a/dpsim-villas/src/InterfaceWorkerVillas.cpp +++ b/dpsim-villas/src/InterfaceWorkerVillas.cpp @@ -12,9 +12,8 @@ #include #include -#include -#include #include +// #include using namespace CPS; using namespace DPsim; @@ -31,10 +30,10 @@ InterfaceWorkerVillas::InterfaceWorkerVillas(const String &nodeConfig, mSampleLength(sampleLength) {} void InterfaceWorkerVillas::open() { - SPDLOG_LOGGER_INFO(mLog, "Opening InterfaceWorkerVillas..."); + SPDLOG_LOGGER_INFO(mLog, "Opening VILLASnode interface worker."); if (!InterfaceWorkerVillas::villasInitialized) { - SPDLOG_LOGGER_INFO(mLog, "Initializing Villas..."); + SPDLOG_LOGGER_INFO(mLog, "Initializing VILLASnode."); initVillas(); InterfaceWorkerVillas::villasInitialized = true; } @@ -58,24 +57,24 @@ void InterfaceWorkerVillas::open() { ret = mNode->parse(config); if (ret < 0) { SPDLOG_LOGGER_ERROR(mLog, - "Error: Node in InterfaceVillas failed to parse " + "Error: Node in VILLASnode interface failed to parse " "config. Parse returned code {}", ret); std::exit(1); } ret = mNode->check(); if (ret < 0) { - SPDLOG_LOGGER_ERROR( - mLog, - "Error: Node in InterfaceVillas failed check. Check returned code {}", - ret); + SPDLOG_LOGGER_ERROR(mLog, + "Error: Node in VILLASnode interface failed check. " + "Check returned code {}", + ret); std::exit(1); } - SPDLOG_LOGGER_INFO(mLog, "Preparing VILLASNode instance..."); + SPDLOG_LOGGER_INFO(mLog, "Preparing VILLASnode node instance."); setupNodeSignals(); prepareNode(); - SPDLOG_LOGGER_INFO(mLog, "Node is ready to send / receive data!"); + SPDLOG_LOGGER_INFO(mLog, "VILLASnode node is ready to send / receive data!"); mOpened = true; mSequence = 0; @@ -93,8 +92,8 @@ void InterfaceWorkerVillas::prepareNode() { sizeof(Sample) + SAMPLE_DATA_LENGTH(mSampleLength)); if (ret < 0) { SPDLOG_LOGGER_ERROR(mLog, - "Error: InterfaceVillas failed to init sample pool. " - "pool_init returned code {}", + "Error: VILLASnode interface failed to init sample " + "pool. pool_init returned code {}", ret); std::exit(1); } @@ -102,20 +101,20 @@ void InterfaceWorkerVillas::prepareNode() { ret = mNode->prepare(); if (ret < 0) { SPDLOG_LOGGER_ERROR(mLog, - "Error: Node in InterfaceVillas failed to prepare. " - "Prepare returned code {}", + "Error: Node in VILLASnode interface failed to " + "prepare. Prepare returned code {}", ret); std::exit(1); } mNode->getFactory()->start( - nullptr); //We have no SuperNode, so just hope type_start doesnt use it... + nullptr); // We have no SuperNode, so just hope type_start doesnt use it... ret = mNode->start(); if (ret < 0) { SPDLOG_LOGGER_ERROR(mLog, - "Fatal error: failed to start node in InterfaceVillas. " - "Start returned code {}", + "Fatal error: failed to start node in VILLASnode " + "interface. Start returned code {}", ret); close(); std::exit(1); @@ -123,10 +122,10 @@ void InterfaceWorkerVillas::prepareNode() { } void InterfaceWorkerVillas::setupNodeSignals() { - mNode->out.path = new node::Path(); - mNode->out.path->signals = std::make_shared(); - node::SignalList::Ptr nodeOutputSignals = - mNode->out.path->getOutputSignals(false); + // mNode->out.path = new node::Path(); + // mNode->out.path->signals = std::make_shared(); + // node::SignalList::Ptr nodeOutputSignals = mNode->out.path->getOutputSignals(false); + node::SignalList::Ptr nodeOutputSignals = mNode->out.signals; nodeOutputSignals->clear(); int idx = 0; for (const auto &[id, signal] : mExportSignals) { @@ -154,21 +153,21 @@ void InterfaceWorkerVillas::setupNodeSignals() { } void InterfaceWorkerVillas::close() { - SPDLOG_LOGGER_INFO(mLog, "Closing InterfaceVillas..."); + SPDLOG_LOGGER_INFO(mLog, "Closing VILLASnode interface."); int ret = mNode->stop(); if (ret < 0) { - SPDLOG_LOGGER_ERROR( - mLog, - "Error: failed to stop node in InterfaceVillas. Stop returned code {}", - ret); + SPDLOG_LOGGER_ERROR(mLog, + "Error: failed to stop node in VILLASnode interface. " + "Stop returned code {}", + ret); std::exit(1); } mOpened = false; ret = node::pool_destroy(&mSamplePool); if (ret < 0) { SPDLOG_LOGGER_ERROR(mLog, - "Error: failed to destroy SamplePool in " - "InterfaceVillas. pool_destroy returned code {}", + "Error: failed to destroy SamplePool in VILLASnode " + "interface. pool_destroy returned code {}", ret); std::exit(1); } @@ -198,7 +197,7 @@ void InterfaceWorkerVillas::readValuesFromEnv( if (ret < 0) { SPDLOG_LOGGER_ERROR(mLog, "Fatal error: failed to read sample from " - "InterfaceVillas. Poll returned code {}", + "VILLASnode interface. Poll returned code {}", ret); close(); std::exit(1); @@ -215,7 +214,7 @@ void InterfaceWorkerVillas::readValuesFromEnv( } } } else { - //If the node does not support pollFds just do a blocking read + // If the node does not support pollFds just do a blocking read shouldRead = true; } @@ -226,7 +225,7 @@ void InterfaceWorkerVillas::readValuesFromEnv( if (ret < 0) { SPDLOG_LOGGER_ERROR(mLog, "Fatal error: failed to read sample from " - "InterfaceVillas. Read returned code {}", + "VILLASnode interface. Read returned code {}", ret); close(); std::exit(1); @@ -244,8 +243,8 @@ void InterfaceWorkerVillas::readValuesFromEnv( } if (!pollFds.empty()) { - //Manually clear the event file descriptor since Villas does not do that for some reason - //See https://github.com/VILLASframework/node/issues/309 + // Manually clear the event file descriptor since VILLASnode does not do that for some reason + // See https://github.com/VILLASframework/node/issues/309 uint64_t result = 0; ret = (int)::read(pollFds[0], &result, 8); if (ret < 0) { @@ -278,14 +277,14 @@ void InterfaceWorkerVillas::readValuesFromEnv( void InterfaceWorkerVillas::writeValuesToEnv( std::vector &updatedAttrs) { - //Update export sequence IDs + // Update export sequence IDs for (const auto &packet : updatedAttrs) { if (std::get<1>(mExports[packet.attributeId]) < packet.sequenceId) { std::get<1>(mExports[packet.attributeId]) = packet.sequenceId; } } - //Remove outdated packets + // Remove outdated packets auto beginOutdated = std::remove_if( updatedAttrs.begin(), updatedAttrs.end(), [this](auto packet) { return std::get<1>(mExports[packet.attributeId]) > packet.sequenceId; @@ -299,17 +298,17 @@ void InterfaceWorkerVillas::writeValuesToEnv( try { sample = node::sample_alloc(&mSamplePool); if (sample == nullptr) { - SPDLOG_LOGGER_ERROR(mLog, "InterfaceVillas could not allocate a new " + SPDLOG_LOGGER_ERROR(mLog, "VILLASnode interface could not allocate a new " "sample! Not sending any data!"); return; } - sample->signals = mNode->getOutputSignals(false); + sample->signals = mNode->out.signals; auto beginExported = std::remove_if( updatedAttrs.begin(), updatedAttrs.end(), [this, &sampleFilled, &sample](auto packet) { if (!std::get<2>(mExports[packet.attributeId])) { - //Write attribute to sample ASAP + // Write attribute to sample ASAP std::get<0>(mExports[packet.attributeId])(packet.value, sample); sampleFilled = true; return true; @@ -318,7 +317,7 @@ void InterfaceWorkerVillas::writeValuesToEnv( }); updatedAttrs.erase(beginExported, updatedAttrs.end()); - //Check if the remaining packets form a complete set + // Check if the remaining packets form a complete set if (((long)updatedAttrs.size()) == std::count_if(mExports.cbegin(), mExports.cend(), [this](auto x) { return std::get<2>(x); })) { @@ -342,8 +341,8 @@ void InterfaceWorkerVillas::writeValuesToEnv( } while (ret == 0); if (ret < 0) SPDLOG_LOGGER_ERROR(mLog, - "Failed to write samples to InterfaceVillas. Write " - "returned code {}", + "Failed to write samples to VILLASnode interface. " + "Write returned code {}", ret); sample_copy(mLastSample, sample); @@ -363,10 +362,10 @@ void InterfaceWorkerVillas::writeValuesToEnv( sample_decref(sample); if (ret < 0) - SPDLOG_LOGGER_ERROR( - mLog, - "Failed to write samples to InterfaceVillas. Write returned code {}", - ret); + SPDLOG_LOGGER_ERROR(mLog, + "Failed to write samples to VILLASnode interface. " + "Write returned code {}", + ret); /* Don't throw here, because we managed to send something */ } @@ -386,7 +385,7 @@ void InterfaceWorkerVillas::configureExport(UInt attributeId, const String &unit) { if (mOpened) { if (mLog != nullptr) { - SPDLOG_LOGGER_WARN(mLog, "InterfaceVillas has already been opened! " + SPDLOG_LOGGER_WARN(mLog, "VILLASnode interface has already been opened! " "Configuration will remain unchanged."); } return; @@ -489,7 +488,7 @@ void InterfaceWorkerVillas::configureImport(UInt attributeId, UInt idx) { if (mOpened) { if (mLog != nullptr) { - SPDLOG_LOGGER_WARN(mLog, "InterfaceVillas has already been opened! " + SPDLOG_LOGGER_WARN(mLog, "VILLASnode interface has already been opened! " "Configuration will remain unchanged."); } return;