1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-23 00:00:01 +01:00
VILLASnode/src/villas-signal.cpp

324 lines
7.7 KiB
C++
Raw Normal View History

/** Generate random packages on stdout.
*
* @file
2015-06-02 21:53:04 +02:00
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2019-01-13 00:42:39 +01:00
* @copyright 2014-2019, Institute for Automation of Complex Power Systems, EONERC
2017-04-27 12:56:43 +02:00
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
2017-04-27 12:56:43 +02:00
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
2017-04-27 12:56:43 +02:00
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2015-06-02 21:53:04 +02:00
*
* @addtogroup tools Test and debug tools
* @{
2015-06-02 21:53:04 +02:00
**********************************************************************************/
#include <unistd.h>
#include <math.h>
#include <string.h>
2018-07-03 21:04:28 +02:00
#include <iostream>
2018-10-20 14:20:51 +02:00
#include <atomic>
2017-09-04 16:20:07 +02:00
#include <villas/io.h>
2018-10-20 14:20:51 +02:00
#include <villas/utils.hpp>
#include <villas/exceptions.hpp>
#include <villas/copyright.hpp>
2018-10-20 14:20:51 +02:00
#include <villas/log.hpp>
2016-11-20 02:45:39 -05:00
#include <villas/sample.h>
#include <villas/timing.h>
#include <villas/node.h>
2017-09-03 10:52:00 +02:00
#include <villas/pool.h>
2017-07-13 01:56:01 +02:00
#include <villas/plugin.h>
#include <villas/nodes/signal_generator.h>
2018-10-20 14:20:51 +02:00
using namespace villas;
static std::atomic<bool> stop(false);
2019-03-31 19:58:53 +02:00
static 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;
print_copyright();
}
json_t * parse_cli(int argc, char *argv[], char **format)
{
Logger logger = logging.get("signal");
/* Default values */
double rate = 10;
double frequency = 1;
double amplitude = 1;
double stddev = 0.02;
double offset = 0;
char *type;
int rt = 1;
int values = 1;
int limit = -1;
/* Parse optional command line arguments */
2018-11-30 19:57:22 +01:00
int c;
char *endptr;
while ((c = getopt(argc, argv, "v:r:F:f:l:a:D:no:d:hV")) != -1) {
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;
2018-10-20 14:20:51 +02:00
case 'd':
logging.setLevel(optarg);
break;
2019-03-31 19:58:53 +02:00
case 'V':
print_version();
exit(EXIT_SUCCESS);
case 'h':
case '?':
2019-03-31 19:58:53 +02:00
usage();
exit(c == '?' ? EXIT_FAILURE : EXIT_SUCCESS);
}
continue;
check: if (optarg == endptr)
2019-03-09 00:32:31 +01:00
logger->warn("Failed to parse parse option argument '-{} {}'", c, optarg);
}
if (argc != optind + 1)
2018-08-27 11:21:57 +02:00
return nullptr;
type = argv[optind];
return json_pack("{ s: s, s: s, s: f, s: f, s: f, s: f, s: f, s: b, s: i, s: i }",
"type", "signal",
"signal", type,
"rate", rate,
"frequency", frequency,
"amplitude", amplitude,
"stddev", stddev,
"offset", offset,
"realtime", rt,
"values", values,
"limit", limit
);
}
static void quit(int signal, siginfo_t *sinfo, void *ctx)
{
2019-02-11 16:39:30 +01:00
Logger logger = logging.get("signal");
switch (signal) {
case SIGALRM:
logger->info("Reached timeout. Terminating...");
break;
default:
logger->info("Received {} signal. Terminating...", strsignal(signal));
}
2018-10-20 14:20:51 +02:00
stop = true;
2017-07-06 23:20:57 +02:00
}
int main(int argc, char *argv[])
{
Logger logger = logging.get("signal");
2019-04-17 18:46:18 +02:00
try {
int ret;
json_t *cfg;
struct node_type *nt;
struct format_type *ft;
char *format = (char *) "villas.human"; /** @todo hardcoded for now */
struct node n;
struct io io;
struct pool q;
struct sample *t;
n.state = STATE_DESTROYED;
n.in.state = STATE_DESTROYED;
n.out.state = STATE_DESTROYED;
io.state = STATE_DESTROYED;
q.state = STATE_DESTROYED;
q.queue.state = STATE_DESTROYED;
ret = utils::signals_init(quit);
if (ret)
throw RuntimeError("Failed to intialize signals");
ret = memory_init(0);
if (ret)
throw RuntimeError("Failed to initialize memory");
nt = node_type_lookup("signal");
if (!nt)
throw RuntimeError("Signal generation is not supported.");
ret = node_init(&n, nt);
if (ret)
throw RuntimeError("Failed to initialize node");
cfg = parse_cli(argc, argv, &format);
if (!cfg) {
usage();
exit(EXIT_FAILURE);
}
2018-08-20 18:31:27 +02:00
2019-04-17 18:46:18 +02:00
ret = node_parse(&n, cfg, "cli");
if (ret) {
usage();
exit(EXIT_FAILURE);
}
2017-08-05 21:02:09 +02:00
2019-04-17 18:46:18 +02:00
ft = format_type_lookup(format);
if (!ft)
throw RuntimeError("Invalid output format '{}'", format);
2017-07-13 01:56:01 +02:00
2019-04-17 18:46:18 +02:00
// nt == n._vt
ret = node_type_start(nt, nullptr);
if (ret)
throw RuntimeError("Failed to initialize node type: {}", node_type_name(nt));
2019-04-17 18:46:18 +02:00
ret = node_check(&n);
if (ret)
throw RuntimeError("Failed to verify node configuration");
2019-04-17 18:46:18 +02:00
ret = pool_init(&q, 16, SAMPLE_LENGTH(vlist_length(&n.in.signals)), &memory_heap);
if (ret)
throw RuntimeError("Failed to initialize pool");
2017-07-13 01:56:01 +02:00
2019-04-17 18:46:18 +02:00
ret = node_prepare(&n);
if (ret)
throw RuntimeError("Failed to start node {}: reason={}", node_name(&n), ret);
2019-04-17 18:46:18 +02:00
ret = node_start(&n);
if (ret)
throw RuntimeError("Failed to start node {}: reason={}", node_name(&n), ret);
2018-08-17 12:40:03 +02:00
2019-04-17 18:46:18 +02:00
ret = io_init(&io, ft, &n.in.signals, IO_FLUSH | (SAMPLE_HAS_ALL & ~SAMPLE_HAS_OFFSET));
if (ret)
throw RuntimeError("Failed to initialize output");
2018-08-20 18:31:27 +02:00
2019-04-17 18:46:18 +02:00
ret = io_check(&io);
if (ret)
throw RuntimeError("Failed to validate IO configuration");
2018-08-20 18:31:27 +02:00
2019-04-17 18:46:18 +02:00
ret = io_open(&io, nullptr);
if (ret)
throw RuntimeError("Failed to open output");
2018-08-20 18:31:27 +02:00
2019-04-17 18:46:18 +02:00
while (!stop && n.state == STATE_STARTED) {
t = sample_alloc(&q);
2019-04-17 18:46:18 +02:00
unsigned release = 1; // release = allocated
2017-09-03 10:52:00 +02:00
2019-04-17 18:46:18 +02:00
retry: ret = node_read(&n, &t, 1, &release);
if (ret == 0)
goto retry;
else if (ret < 0)
goto out;
2019-04-17 18:46:18 +02:00
io_print(&io, &t, 1);
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
2019-04-17 18:46:18 +02:00
ret = node_stop(&n);
if (ret)
throw RuntimeError("Failed to stop node");
2019-04-17 18:46:18 +02:00
ret = node_destroy(&n);
if (ret)
throw RuntimeError("Failed to destroy node");
2018-10-20 14:20:51 +02:00
2019-04-17 18:46:18 +02:00
ret = io_close(&io);
if (ret)
throw RuntimeError("Failed to close IO");
2018-10-20 14:20:51 +02:00
2019-04-17 18:46:18 +02:00
ret = io_destroy(&io);
if (ret)
throw RuntimeError("Failed to destroy IO");
2018-10-20 14:20:51 +02:00
2019-04-17 18:46:18 +02:00
ret = pool_destroy(&q);
if (ret)
throw RuntimeError("Failed to destroy pool");
2018-10-20 14:20:51 +02:00
2019-04-17 18:46:18 +02:00
logger->info(CLR_GRN("Goodbye!"));
2018-10-20 14:20:51 +02:00
2019-04-17 18:46:18 +02:00
return 0;
}
catch (std::runtime_error &e) {
logger->error("{}", e.what());
2018-10-20 14:20:51 +02:00
2019-04-17 18:46:18 +02:00
return -1;
}
}
2015-08-07 01:11:43 +02:00
/** @} */