2023-08-31 11:25:01 +02:00
|
|
|
/* Generate random packages on stdout.
|
2014-06-05 09:34:47 +00:00
|
|
|
*
|
2023-08-31 11:25:01 +02: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
|
|
|
|
*/
|
2014-06-05 09:34:47 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cmath>
|
|
|
|
#include <cstring>
|
2018-07-03 21:04:28 +02:00
|
|
|
#include <iostream>
|
2018-10-20 14:20:51 +02:00
|
|
|
#include <atomic>
|
2014-06-05 09:34:47 +00:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
#include <villas/tool.hpp>
|
2021-05-10 00:12:30 +02:00
|
|
|
#include <villas/format.hpp>
|
2018-10-20 14:20:51 +02:00
|
|
|
#include <villas/utils.hpp>
|
2019-04-23 13:11:08 +02:00
|
|
|
#include <villas/colors.hpp>
|
2018-10-20 14:20:51 +02:00
|
|
|
#include <villas/exceptions.hpp>
|
|
|
|
#include <villas/log.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/sample.hpp>
|
|
|
|
#include <villas/timing.hpp>
|
|
|
|
#include <villas/node.hpp>
|
|
|
|
#include <villas/pool.hpp>
|
|
|
|
#include <villas/nodes/signal.hpp>
|
2017-07-06 23:14:38 +02:00
|
|
|
|
2018-10-20 14:20:51 +02:00
|
|
|
using namespace villas;
|
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
|
|
|
namespace tools {
|
2018-10-20 14:20:51 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
class Signal : public Tool {
|
2019-04-17 18:35:22 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
public:
|
|
|
|
Signal(int argc, char *argv[]) :
|
|
|
|
Tool(argc, argv, "signal"),
|
|
|
|
stop(false),
|
2021-05-10 00:12:30 +02:00
|
|
|
node(),
|
|
|
|
formatter(nullptr),
|
|
|
|
pool(),
|
2019-04-19 14:52:52 +02:00
|
|
|
format("villas.human")
|
|
|
|
{
|
|
|
|
int ret;
|
2018-08-07 18:28:55 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
ret = memory::init(DEFAULT_NR_HUGEPAGES);
|
2019-04-19 14:52:52 +02:00
|
|
|
if (ret)
|
|
|
|
throw RuntimeError("Failed to initialize memory");
|
|
|
|
}
|
2018-10-20 14:20:51 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
protected:
|
|
|
|
std::atomic<bool> stop;
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
Node *node;
|
2021-05-10 00:12:30 +02:00
|
|
|
Format *formatter;
|
2021-08-10 10:12:48 -04:00
|
|
|
struct Pool pool;
|
2019-04-19 14:52:52 +02:00
|
|
|
|
|
|
|
std::string format;
|
|
|
|
|
|
|
|
void usage()
|
|
|
|
{
|
|
|
|
std::cout << "Usage: villas-signal [OPTIONS] SIGNAL" << std::endl
|
|
|
|
<< " SIGNAL is on of the following signal types:" << std::endl
|
|
|
|
<< " mixed" << std::endl
|
|
|
|
<< " random" << std::endl
|
|
|
|
<< " sine" << std::endl
|
|
|
|
<< " triangle" << std::endl
|
|
|
|
<< " square" << std::endl
|
|
|
|
<< " ramp" << std::endl
|
|
|
|
<< " constants" << std::endl
|
|
|
|
<< " counter" << std::endl << std::endl
|
|
|
|
<< " OPTIONS is one or more of the following options:" << std::endl
|
|
|
|
<< " -d LVL set debug level" << std::endl
|
|
|
|
<< " -f FMT set the format" << std::endl
|
|
|
|
<< " -v NUM specifies how many values a message should contain" << std::endl
|
|
|
|
<< " -r HZ how many messages per second" << std::endl
|
|
|
|
<< " -n non real-time mode. do not throttle output." << std::endl
|
|
|
|
<< " -F HZ the frequency of the signal" << std::endl
|
|
|
|
<< " -a FLT the amplitude" << std::endl
|
|
|
|
<< " -D FLT the standard deviation for 'random' signals" << std::endl
|
|
|
|
<< " -o OFF the DC bias" << std::endl
|
|
|
|
<< " -l NUM only send LIMIT messages and stop" << std::endl << std::endl;
|
|
|
|
|
|
|
|
printCopyright();
|
|
|
|
}
|
2019-03-31 19:58:53 +02:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
json_t * parse_cli(int argc, char *argv[])
|
|
|
|
{
|
2023-08-31 11:25:01 +02:00
|
|
|
// Default values
|
2019-04-19 14:52:52 +02:00
|
|
|
double rate = 10;
|
|
|
|
double frequency = 1;
|
|
|
|
double amplitude = 1;
|
|
|
|
double stddev = 0.02;
|
|
|
|
double offset = 0;
|
2021-08-10 10:12:48 -04:00
|
|
|
double phase = 0.0;
|
|
|
|
double pulse_low = 0.0;
|
|
|
|
double pulse_high = 1.0;
|
|
|
|
double pulse_width = 1.0;
|
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
std::string type;
|
|
|
|
int rt = 1;
|
|
|
|
int values = 1;
|
|
|
|
int limit = -1;
|
|
|
|
|
2023-08-31 11:25:01 +02:00
|
|
|
// Parse optional command line arguments
|
2019-04-19 14:52:52 +02:00
|
|
|
int c;
|
|
|
|
char *endptr;
|
2021-08-10 10:12:48 -04:00
|
|
|
while ((c = getopt(argc, argv, "v:r:F:f:l:a:D:no:d:hVp:")) != -1) {
|
2019-04-19 14:52:52 +02:00
|
|
|
switch (c) {
|
|
|
|
case 'n':
|
|
|
|
rt = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
format = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
|
|
|
limit = strtoul(optarg, &endptr, 10);
|
|
|
|
goto check;
|
|
|
|
|
|
|
|
case 'v':
|
|
|
|
values = strtoul(optarg, &endptr, 10);
|
|
|
|
goto check;
|
|
|
|
|
|
|
|
case 'r':
|
|
|
|
rate = strtof(optarg, &endptr);
|
|
|
|
goto check;
|
|
|
|
|
|
|
|
case 'o':
|
|
|
|
offset = strtof(optarg, &endptr);
|
|
|
|
goto check;
|
|
|
|
|
|
|
|
case 'F':
|
|
|
|
frequency = strtof(optarg, &endptr);
|
|
|
|
goto check;
|
|
|
|
|
|
|
|
case 'a':
|
|
|
|
amplitude = strtof(optarg, &endptr);
|
|
|
|
goto check;
|
|
|
|
|
|
|
|
case 'D':
|
|
|
|
stddev = strtof(optarg, &endptr);
|
|
|
|
goto check;
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
case 'p':
|
|
|
|
phase = strtof(optarg, &endptr);
|
|
|
|
goto check;
|
|
|
|
|
|
|
|
case 'w':
|
|
|
|
pulse_width = strtof(optarg, &endptr);
|
|
|
|
goto check;
|
|
|
|
|
|
|
|
case 'L':
|
|
|
|
pulse_low = strtof(optarg, &endptr);
|
|
|
|
goto check;
|
|
|
|
|
|
|
|
case 'H':
|
|
|
|
pulse_high = strtof(optarg, &endptr);
|
|
|
|
goto check;
|
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
case 'd':
|
|
|
|
logging.setLevel(optarg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'V':
|
|
|
|
printVersion();
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
case '?':
|
|
|
|
usage();
|
|
|
|
exit(c == '?' ? EXIT_FAILURE : EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
check: if (optarg == endptr)
|
|
|
|
logger->warn("Failed to parse parse option argument '-{} {}'", c, optarg);
|
2018-08-07 18:28:55 +02:00
|
|
|
}
|
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
if (argc != optind + 1)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
type = argv[optind];
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
json_t *json_signals = json_array();
|
|
|
|
|
|
|
|
for (int i = 0; i < values; i++) {
|
|
|
|
json_t *json_signal = json_pack("{ s: s, s: f, s: f, s: f, s: f, s: f, s: f, s: f, s: f }",
|
|
|
|
"signal", strdup(type.c_str()),
|
|
|
|
"frequency", frequency,
|
|
|
|
"amplitude", amplitude,
|
|
|
|
"stddev", stddev,
|
|
|
|
"offset", offset,
|
|
|
|
"pulse_width", pulse_width,
|
|
|
|
"pulse_low", pulse_low,
|
|
|
|
"pulse_high", pulse_high,
|
|
|
|
"phase", phase
|
|
|
|
);
|
|
|
|
|
|
|
|
json_array_append_new(json_signals, json_signal);
|
|
|
|
}
|
|
|
|
|
|
|
|
return json_pack("{ s: s, s: f, s: b, s: i, s: { s: o } }",
|
2019-04-19 14:52:52 +02:00
|
|
|
"type", "signal",
|
|
|
|
"rate", rate,
|
|
|
|
"realtime", rt,
|
2021-08-10 10:12:48 -04:00
|
|
|
"limit", limit,
|
|
|
|
"in",
|
|
|
|
"signals", json_signals
|
2019-04-19 14:52:52 +02:00
|
|
|
);
|
2018-08-07 18:28:55 +02:00
|
|
|
}
|
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
void handler(int signal, siginfo_t *sinfo, void *ctx)
|
|
|
|
{
|
|
|
|
switch (signal) {
|
|
|
|
case SIGALRM:
|
|
|
|
logger->info("Reached timeout. Terminating...");
|
|
|
|
break;
|
2019-02-11 16:39:30 +01:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
default:
|
|
|
|
logger->info("Received {} signal. Terminating...", strsignal(signal));
|
|
|
|
}
|
2019-02-11 16:39:30 +01:00
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
stop = true;
|
2019-02-11 16:39:30 +01:00
|
|
|
}
|
|
|
|
|
2019-04-19 14:52:52 +02:00
|
|
|
int main()
|
|
|
|
{
|
2019-04-17 18:46:18 +02:00
|
|
|
int ret;
|
2021-05-10 00:12:30 +02:00
|
|
|
json_t *json, *json_format;
|
|
|
|
json_error_t err;
|
2019-04-17 18:46:18 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
struct Sample *t;
|
2019-04-17 18:46:18 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
node = NodeFactory::make("signal.v2");
|
|
|
|
if (!node)
|
|
|
|
throw MemoryAllocationError();
|
2019-04-17 18:46:18 +02:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
json = parse_cli(argc, argv);
|
|
|
|
if (!json) {
|
2019-04-17 18:46:18 +02:00
|
|
|
usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2018-08-20 18:31:27 +02:00
|
|
|
|
2023-06-30 11:45:38 +02:00
|
|
|
ret = node->parse(json);
|
2019-04-17 18:46:18 +02:00
|
|
|
if (ret) {
|
|
|
|
usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
ret = node->getFactory()->start(nullptr);
|
2019-04-17 18:46:18 +02:00
|
|
|
if (ret)
|
2023-01-11 12:40:47 +01:00
|
|
|
throw RuntimeError("Failed to intialize node type {}: reason={}", node->getFactory()->getName(), ret);
|
2019-04-17 18:35:22 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
ret = node->check();
|
2019-04-17 18:46:18 +02:00
|
|
|
if (ret)
|
|
|
|
throw RuntimeError("Failed to verify node configuration");
|
2018-07-19 16:53:44 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
ret = node->prepare();
|
2019-04-17 18:46:18 +02:00
|
|
|
if (ret)
|
2023-01-10 15:25:27 +01:00
|
|
|
throw RuntimeError("Failed to prepare node {}: reason={}", node->getName(), ret);
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2023-08-31 11:25:01 +02:00
|
|
|
// Try parsing format config as JSON
|
2021-05-10 00:12:30 +02:00
|
|
|
json_format = json_loads(format.c_str(), 0, &err);
|
|
|
|
formatter = json_format
|
|
|
|
? FormatFactory::make(json_format)
|
|
|
|
: FormatFactory::make(format);
|
|
|
|
if (!formatter)
|
2019-04-17 18:46:18 +02:00
|
|
|
throw RuntimeError("Failed to initialize output");
|
2018-08-20 18:31:27 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
formatter->start(node->getInputSignals(), ~(int) SampleFlags::HAS_OFFSET);
|
2019-06-18 18:50:24 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
ret = pool_init(&pool, 16, SAMPLE_LENGTH(node->getInputSignals()->size()), &memory::heap);
|
2019-04-17 18:46:18 +02:00
|
|
|
if (ret)
|
2021-05-10 00:12:30 +02:00
|
|
|
throw RuntimeError("Failed to initialize pool");
|
2018-08-20 18:31:27 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
ret = node->start();
|
2019-06-18 18:50:24 +01:00
|
|
|
if (ret)
|
2023-01-10 15:25:27 +01:00
|
|
|
throw RuntimeError("Failed to start node {}: reason={}", node->getName(), ret);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
while (!stop && node->getState() == State::STARTED) {
|
2021-05-10 00:12:30 +02:00
|
|
|
t = sample_alloc(&pool);
|
2017-09-03 10:52:00 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
retry: ret = node->read(&t, 1);
|
2019-04-17 18:46:18 +02:00
|
|
|
if (ret == 0)
|
|
|
|
goto retry;
|
|
|
|
else if (ret < 0)
|
|
|
|
goto out;
|
2018-07-11 18:14:29 +02:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
formatter->print(stdout, t);
|
|
|
|
fflush(stdout);
|
2019-02-11 16:39:30 +01:00
|
|
|
|
2019-04-17 18:46:18 +02:00
|
|
|
out: sample_decref(t);
|
|
|
|
}
|
2017-09-03 10:52:00 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
ret = node->stop();
|
2019-04-17 18:46:18 +02:00
|
|
|
if (ret)
|
|
|
|
throw RuntimeError("Failed to stop node");
|
2014-06-05 09:34:47 +00:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
ret = node->getFactory()->stop();
|
2019-04-17 18:46:18 +02:00
|
|
|
if (ret)
|
2023-01-10 16:41:58 +01:00
|
|
|
throw RuntimeError("Failed to de-intialize node type {}: reason={}", node->getFactory()->getName(), ret);
|
2018-10-20 14:20:51 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
delete node;
|
2021-05-10 00:12:30 +02:00
|
|
|
delete formatter;
|
2018-10-20 14:20:51 +02:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
ret = pool_destroy(&pool);
|
2019-04-17 18:46:18 +02:00
|
|
|
if (ret)
|
|
|
|
throw RuntimeError("Failed to destroy pool");
|
2018-10-20 14:20:51 +02:00
|
|
|
|
2019-04-17 18:46:18 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2019-04-19 14:52:52 +02:00
|
|
|
};
|
2018-10-20 14:20:51 +02:00
|
|
|
|
2023-08-28 09:34:02 +02:00
|
|
|
} // namespace tools
|
|
|
|
} // namespace node
|
|
|
|
} // namespace villas
|
2019-04-19 14:52:52 +02:00
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2019-06-18 17:52:54 +02:00
|
|
|
villas::node::tools::Signal t(argc, argv);
|
2019-04-19 14:52:52 +02:00
|
|
|
|
|
|
|
return t.run();
|
2014-06-05 09:34:47 +00:00
|
|
|
}
|