2025-01-14 14:42:39 +00:00
|
|
|
/* An example get started with new implementations of new node-types.
|
2022-04-01 14:55:08 +02:00
|
|
|
*
|
|
|
|
* This example does not do any particulary useful.
|
|
|
|
* It is just a skeleton to get you started with new node-types.
|
2020-01-21 14:20:25 +01:00
|
|
|
*
|
2025-01-14 14:42:39 +00:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
#include <villas/exceptions.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/node_compat.hpp>
|
2020-01-21 14:20:25 +01:00
|
|
|
#include <villas/nodes/example.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/sample.hpp>
|
2020-01-21 14:20:25 +01:00
|
|
|
#include <villas/super_node.hpp>
|
2025-01-14 14:42:39 +00:00
|
|
|
#include <villas/utils.hpp>
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
using namespace villas;
|
2020-01-21 14:20:25 +01:00
|
|
|
using namespace villas::node;
|
|
|
|
using namespace villas::utils;
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
ExampleNode::ExampleNode(const uuid_t &id, const std::string &name)
|
|
|
|
: Node(id, name), setting1(72), setting2("something"), state1(0) {}
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
ExampleNode::~ExampleNode() {}
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int ExampleNode::prepare() {
|
|
|
|
state1 = setting1;
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (setting2 == "double")
|
|
|
|
state1 *= 2;
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return 0;
|
2020-01-21 14:20:25 +01:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int ExampleNode::parse(json_t *json) {
|
|
|
|
// TODO: Add implementation here. The following is just an example
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
const char *setting2_str = nullptr;
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
json_error_t err;
|
|
|
|
int ret = json_unpack_ex(json, &err, 0, "{ s?: i, s?: s }", "setting1",
|
|
|
|
&setting1, "setting2", &setting2_str);
|
|
|
|
if (ret)
|
|
|
|
throw ConfigError(json, err, "node-config-node-example");
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (setting2_str)
|
|
|
|
setting2 = setting2_str;
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return 0;
|
2020-01-21 14:20:25 +01:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int ExampleNode::check() {
|
|
|
|
if (setting1 > 100 || setting1 < 0)
|
|
|
|
return -1;
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
if (setting2.empty() || setting2.size() > 10)
|
|
|
|
return -1;
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return 0;
|
2020-01-21 14:20:25 +01:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int ExampleNode::start() {
|
|
|
|
// TODO add implementation here
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
start_time = time_now();
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return 0;
|
2020-01-21 14:20:25 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 14:55:08 +02:00
|
|
|
// int ExampleNode::stop()
|
|
|
|
// {
|
|
|
|
// // TODO add implementation here
|
|
|
|
// return 0;
|
|
|
|
// }
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2022-04-01 14:55:08 +02:00
|
|
|
// int ExampleNode::pause()
|
|
|
|
// {
|
|
|
|
// // TODO add implementation here
|
|
|
|
// return 0;
|
|
|
|
// }
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2022-04-01 14:55:08 +02:00
|
|
|
// int ExampleNode::resume()
|
|
|
|
// {
|
|
|
|
// // TODO add implementation here
|
|
|
|
// return 0;
|
|
|
|
// }
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2022-04-01 14:55:08 +02:00
|
|
|
// int ExampleNode::restart()
|
|
|
|
// {
|
|
|
|
// // TODO add implementation here
|
|
|
|
// return 0;
|
|
|
|
// }
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2022-04-01 14:55:08 +02:00
|
|
|
// int ExampleNode::reverse()
|
|
|
|
// {
|
|
|
|
// // TODO add implementation here
|
|
|
|
// return 0;
|
|
|
|
// }
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2022-04-01 14:55:08 +02:00
|
|
|
// std::vector<int> ExampleNode::getPollFDs()
|
|
|
|
// {
|
|
|
|
// // TODO add implementation here
|
|
|
|
// return {};
|
|
|
|
// }
|
2020-01-21 14:26:56 +01:00
|
|
|
|
2022-04-01 14:55:08 +02:00
|
|
|
// std::vector<int> ExampleNode::getNetemFDs()
|
|
|
|
// {
|
|
|
|
// // TODO add implementation here
|
|
|
|
// return {};
|
|
|
|
// }
|
2020-01-21 14:26:56 +01:00
|
|
|
|
2022-04-01 14:55:08 +02:00
|
|
|
// struct villas::node::memory::Type * ExampleNode::getMemoryType()
|
|
|
|
// {
|
|
|
|
// // TODO add implementation here
|
|
|
|
// }
|
2020-01-21 14:26:56 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
const std::string &ExampleNode::getDetails() {
|
|
|
|
details = fmt::format("setting1={}, setting2={}", setting1, setting2);
|
|
|
|
return details;
|
2020-01-21 14:26:56 +01:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int ExampleNode::_read(struct Sample *smps[], unsigned cnt) {
|
|
|
|
int read;
|
|
|
|
struct timespec now;
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// TODO: Add implementation here. The following is just an example
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
assert(cnt >= 1 && smps[0]->capacity >= 1);
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
now = time_now();
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
smps[0]->data[0].f = time_delta(&now, &start_time);
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
/* Dont forget to set other flags in struct Sample::flags
|
|
|
|
* E.g. for sequence no, timestamps... */
|
|
|
|
smps[0]->flags = (int)SampleFlags::HAS_DATA;
|
|
|
|
smps[0]->signals = getInputSignals(false);
|
2020-07-06 15:07:05 +02:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
read = 1; // The number of samples read
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return read;
|
2020-01-21 14:20:25 +01:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
int ExampleNode::_write(struct Sample *smps[], unsigned cnt) {
|
|
|
|
int written;
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// TODO: Add implementation here.
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
written = 0; // The number of samples written
|
2020-01-21 14:20:25 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
return written;
|
2020-01-21 14:20:25 +01:00
|
|
|
}
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// Register node
|
2022-04-01 14:55:08 +02:00
|
|
|
static char n[] = "example";
|
|
|
|
static char d[] = "An example for staring new node-type implementations";
|
2025-01-14 14:42:39 +00:00
|
|
|
static NodePlugin<ExampleNode, n, d,
|
|
|
|
(int)NodeFactory::Flags::SUPPORTS_READ |
|
|
|
|
(int)NodeFactory::Flags::SUPPORTS_WRITE |
|
|
|
|
(int)NodeFactory::Flags::SUPPORTS_POLL |
|
|
|
|
(int)NodeFactory::Flags::HIDDEN>
|
|
|
|
p;
|