Skip to content

Commit

Permalink
wip: villas: Minor fixes and tweaks for VillasInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
stv0g committed Feb 26, 2024
1 parent 694133a commit e544cb2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 34 deletions.
2 changes: 1 addition & 1 deletion dpsim-villas/include/dpsim-villas/InterfaceWorkerVillas.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace DPsim {

public:
using Ptr = std::shared_ptr<InterfaceWorkerVillas>;
using Sample = struct node::Sample;
using Sample = node::Sample;

static UInt villasPriority;
static UInt villasAffinity;
Expand Down
67 changes: 34 additions & 33 deletions dpsim-villas/src/InterfaceWorkerVillas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <dpsim-villas/InterfaceWorkerVillas.h>
#include <dpsim-models/Logger.h>
#include <villas/signal_list.hpp>
#include <villas/path.hpp>
// #include <villas/path.hpp>

using namespace CPS;
using namespace DPsim;
Expand All @@ -32,10 +32,10 @@ InterfaceWorkerVillas::InterfaceWorkerVillas(const String &nodeConfig, UInt queu
{ }

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;
}
Expand All @@ -58,19 +58,19 @@ void InterfaceWorkerVillas::open() {
int ret = 0;
ret = mNode->parse(config);
if (ret < 0) {
SPDLOG_LOGGER_ERROR(mLog, "Error: Node in InterfaceVillas failed to parse config. Parse returned code {}", ret);
SPDLOG_LOGGER_ERROR(mLog, "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;
Expand All @@ -87,30 +87,31 @@ void InterfaceWorkerVillas::open() {
void InterfaceWorkerVillas::prepareNode() {
int ret = node::pool_init(&mSamplePool, mQueueLength, sizeof(Sample) + SAMPLE_DATA_LENGTH(mSampleLength));
if (ret < 0) {
SPDLOG_LOGGER_ERROR(mLog, "Error: InterfaceVillas failed to init sample pool. pool_init returned code {}", ret);
SPDLOG_LOGGER_ERROR(mLog, "Error: VILLASnode interface failed to init sample pool. pool_init returned code {}", ret);
std::exit(1);
}

ret = mNode->prepare();
if (ret < 0) {
SPDLOG_LOGGER_ERROR(mLog, "Error: Node in InterfaceVillas failed to prepare. Prepare returned code {}", ret);
SPDLOG_LOGGER_ERROR(mLog, "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...
mNode->getFactory()->start(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 {}", ret);
SPDLOG_LOGGER_ERROR(mLog, "Fatal error: failed to start node in VILLASnode interface. Start returned code {}", ret);
close();
std::exit(1);
}
}

void InterfaceWorkerVillas::setupNodeSignals() {
mNode->out.path = new node::Path();
mNode->out.path->signals = std::make_shared<node::SignalList>();
node::SignalList::Ptr nodeOutputSignals = mNode->out.path->getOutputSignals(false);
// mNode->out.path = new node::Path();
// mNode->out.path->signals = std::make_shared<node::SignalList>();
// 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) {
Expand All @@ -136,16 +137,16 @@ 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 {}", ret);
SPDLOG_LOGGER_ERROR(mLog, "Error: failed to destroy SamplePool in VILLASnode interface. pool_destroy returned code {}", ret);
std::exit(1);
}

Expand Down Expand Up @@ -174,7 +175,7 @@ void InterfaceWorkerVillas::readValuesFromEnv(std::vector<Interface::AttributePa
ret = ::poll(pfds.data(), pfds.size(), 0);

if (ret < 0) {
SPDLOG_LOGGER_ERROR(mLog, "Fatal error: failed to read sample from InterfaceVillas. Poll returned code {}", ret);
SPDLOG_LOGGER_ERROR(mLog, "Fatal error: failed to read sample from VILLASnode interface. Poll returned code {}", ret);
close();
std::exit(1);
}
Expand All @@ -189,8 +190,8 @@ void InterfaceWorkerVillas::readValuesFromEnv(std::vector<Interface::AttributePa
break;
}
}
} else {
//If the node does not support pollFds just do a blocking read
} else {
// If the node does not support pollFds just do a blocking read
shouldRead = true;
}

Expand All @@ -199,7 +200,7 @@ void InterfaceWorkerVillas::readValuesFromEnv(std::vector<Interface::AttributePa

ret = mNode->read(&sample, 1);
if (ret < 0) {
SPDLOG_LOGGER_ERROR(mLog, "Fatal error: failed to read sample from InterfaceVillas. Read returned code {}", ret);
SPDLOG_LOGGER_ERROR(mLog, "Fatal error: failed to read sample from VILLASnode interface. Read returned code {}", ret);
close();
std::exit(1);
}
Expand All @@ -219,8 +220,8 @@ void InterfaceWorkerVillas::readValuesFromEnv(std::vector<Interface::AttributePa
}

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) {
Expand Down Expand Up @@ -249,14 +250,14 @@ void InterfaceWorkerVillas::readValuesFromEnv(std::vector<Interface::AttributePa
}

void InterfaceWorkerVillas::writeValuesToEnv(std::vector<Interface::AttributePacket>& 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;
});
Expand All @@ -269,14 +270,14 @@ void InterfaceWorkerVillas::writeValuesToEnv(std::vector<Interface::AttributePac
try {
sample = node::sample_alloc(&mSamplePool);
if (sample == nullptr) {
SPDLOG_LOGGER_ERROR(mLog, "InterfaceVillas could not allocate a new sample! Not sending any data!");
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;
Expand All @@ -285,7 +286,7 @@ void InterfaceWorkerVillas::writeValuesToEnv(std::vector<Interface::AttributePac
});
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);
})) {
Expand All @@ -308,7 +309,7 @@ void InterfaceWorkerVillas::writeValuesToEnv(std::vector<Interface::AttributePac
ret = mNode->write(&sample, 1);
} while (ret == 0);
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);

sample_copy(mLastSample, sample);
}
Expand All @@ -328,7 +329,7 @@ void InterfaceWorkerVillas::writeValuesToEnv(std::vector<Interface::AttributePac
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 */
}
Expand All @@ -344,7 +345,7 @@ void InterfaceWorkerVillas::initVillas() const {
void InterfaceWorkerVillas::configureExport(UInt attributeId, const std::type_info& type, UInt idx, Bool waitForOnWrite, const String& name, const String& unit) {
if (mOpened) {
if (mLog != nullptr) {
SPDLOG_LOGGER_WARN(mLog, "InterfaceVillas has already been opened! Configuration will remain unchanged.");
SPDLOG_LOGGER_WARN(mLog, "VILLASnode interface has already been opened! Configuration will remain unchanged.");
}
return;
}
Expand Down Expand Up @@ -425,7 +426,7 @@ void InterfaceWorkerVillas::configureExport(UInt attributeId, const std::type_in
void InterfaceWorkerVillas::configureImport(UInt attributeId, const std::type_info& type, UInt idx) {
if (mOpened) {
if (mLog != nullptr) {
SPDLOG_LOGGER_WARN(mLog, "InterfaceVillas has already been opened! Configuration will remain unchanged.");
SPDLOG_LOGGER_WARN(mLog, "VILLASnode interface has already been opened! Configuration will remain unchanged.");
}
return;
}
Expand Down

0 comments on commit e544cb2

Please sign in to comment.