2014-07-14 11:49:44 +00:00
|
|
|
/** Generate random packages on stdout.
|
2014-06-05 09:34:47 +00:00
|
|
|
*
|
2015-05-06 11:36:51 +02:00
|
|
|
* @file
|
2015-06-02 21:53:04 +02:00
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2017-03-03 20:20:13 -04:00
|
|
|
* @copyright 2017, 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-05-05 19:24:16 +00:00
|
|
|
*
|
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-05-05 19:24:16 +00:00
|
|
|
*
|
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
|
|
|
*
|
2015-05-06 11:36:51 +02:00
|
|
|
* @addtogroup tools Test and debug tools
|
|
|
|
* @{
|
2015-06-02 21:53:04 +02:00
|
|
|
**********************************************************************************/
|
2014-06-05 09:34:47 +00:00
|
|
|
|
|
|
|
#include <unistd.h>
|
2015-12-02 13:54:23 +01:00
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
2014-06-05 09:34:47 +00:00
|
|
|
|
2016-11-20 02:45:39 -05:00
|
|
|
#include <villas/utils.h>
|
|
|
|
#include <villas/sample.h>
|
2017-03-27 12:28:13 +02:00
|
|
|
#include <villas/sample_io.h>
|
2016-11-20 02:45:39 -05:00
|
|
|
#include <villas/timing.h>
|
2017-07-06 23:14:38 +02:00
|
|
|
#include <villas/node.h>
|
|
|
|
#include <villas/nodes/signal.h>
|
|
|
|
|
|
|
|
/* Some default values */
|
|
|
|
struct signal s = {
|
|
|
|
.rate = 10,
|
|
|
|
.frequency = 1,
|
|
|
|
.amplitude = 1,
|
|
|
|
.stddev = 0.02,
|
|
|
|
.type = SIGNAL_TYPE_MIXED,
|
|
|
|
.rt = 1,
|
|
|
|
.values = 1,
|
|
|
|
.limit = -1
|
|
|
|
};
|
2016-11-20 02:45:39 -05:00
|
|
|
|
2017-07-06 23:14:38 +02:00
|
|
|
struct node n = {
|
|
|
|
._vd = &s
|
2015-12-02 13:54:23 +01:00
|
|
|
};
|
|
|
|
|
2017-07-06 23:14:38 +02:00
|
|
|
struct log l;
|
|
|
|
|
2016-10-22 20:37:02 -04:00
|
|
|
void usage()
|
2016-02-07 03:17:45 +01:00
|
|
|
{
|
2017-05-03 19:26:58 +02:00
|
|
|
printf("Usage: villas-signal [OPTIONS] SIGNAL\n");
|
|
|
|
printf(" SIGNAL is on of the following signal types:\n");
|
|
|
|
printf(" mixed\n");
|
|
|
|
printf(" random\n");
|
|
|
|
printf(" sine\n");
|
|
|
|
printf(" triangle\n");
|
|
|
|
printf(" square\n");
|
|
|
|
printf(" ramp\n");
|
|
|
|
printf("\n");
|
|
|
|
printf(" OPTIONS is one or more of the following options:\n");
|
|
|
|
printf(" -d LVL set debug level\n");
|
|
|
|
printf(" -v NUM specifies how many values a message should contain\n");
|
|
|
|
printf(" -r HZ how many messages per second\n");
|
|
|
|
printf(" -n non real-time mode. do not throttle output.\n");
|
|
|
|
printf(" -f HZ the frequency of the signal\n");
|
|
|
|
printf(" -a FLT the amplitude\n");
|
|
|
|
printf(" -D FLT the standard deviation for 'random' signals\n");
|
|
|
|
printf(" -l NUM only send LIMIT messages and stop\n\n");
|
2016-02-07 03:17:45 +01:00
|
|
|
|
|
|
|
print_copyright();
|
|
|
|
}
|
|
|
|
|
2017-07-06 23:14:38 +02:00
|
|
|
static void quit(int signal, siginfo_t *sinfo, void *ctx)
|
|
|
|
{
|
|
|
|
signal_close(&n);
|
|
|
|
|
2017-07-12 00:50:29 +02:00
|
|
|
info(CLR_GRN("Goodbye!"));
|
2017-07-06 23:14:38 +02:00
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2017-07-06 23:20:57 +02:00
|
|
|
int signal_parse_cli(struct signal *s, int argc, char *argv[])
|
2014-06-05 09:34:47 +00:00
|
|
|
{
|
2017-07-06 23:14:38 +02:00
|
|
|
char *type;
|
2015-03-21 18:03:55 +01:00
|
|
|
|
2016-02-07 03:17:45 +01:00
|
|
|
/* Parse optional command line arguments */
|
|
|
|
char c, *endptr;
|
2017-05-03 19:26:58 +02:00
|
|
|
while ((c = getopt(argc, argv, "hv:r:f:l:a:D:d:n")) != -1) {
|
2016-02-07 03:17:45 +01:00
|
|
|
switch (c) {
|
2017-03-06 19:14:24 -04:00
|
|
|
case 'd':
|
2017-07-06 23:20:57 +02:00
|
|
|
l.level = strtoul(optarg, &endptr, 10);
|
2017-03-06 19:14:24 -04:00
|
|
|
goto check;
|
2017-07-06 23:14:38 +02:00
|
|
|
case 'n':
|
2017-07-06 23:20:57 +02:00
|
|
|
s->rt = 0;
|
2017-07-06 23:14:38 +02:00
|
|
|
break;
|
2016-02-07 03:17:45 +01:00
|
|
|
case 'l':
|
2017-07-06 23:20:57 +02:00
|
|
|
s->limit = strtoul(optarg, &endptr, 10);
|
2016-02-07 03:17:45 +01:00
|
|
|
goto check;
|
|
|
|
case 'v':
|
2017-07-06 23:20:57 +02:00
|
|
|
s->values = strtoul(optarg, &endptr, 10);
|
2016-02-07 03:17:45 +01:00
|
|
|
goto check;
|
|
|
|
case 'r':
|
2017-07-06 23:20:57 +02:00
|
|
|
s->rate = strtof(optarg, &endptr);
|
2016-02-07 03:17:45 +01:00
|
|
|
goto check;
|
|
|
|
case 'f':
|
2017-07-06 23:20:57 +02:00
|
|
|
s->frequency = strtof(optarg, &endptr);
|
2016-02-07 03:17:45 +01:00
|
|
|
goto check;
|
|
|
|
case 'a':
|
2017-07-06 23:20:57 +02:00
|
|
|
s->amplitude = strtof(optarg, &endptr);
|
2016-02-07 03:17:45 +01:00
|
|
|
goto check;
|
2017-03-06 19:14:24 -04:00
|
|
|
case 'D':
|
2017-07-06 23:20:57 +02:00
|
|
|
s->stddev = strtof(optarg, &endptr);
|
2016-02-07 03:17:45 +01:00
|
|
|
goto check;
|
|
|
|
case 'h':
|
|
|
|
case '?':
|
2016-10-22 20:37:02 -04:00
|
|
|
usage();
|
2016-10-30 22:05:29 -04:00
|
|
|
exit(c == '?' ? EXIT_FAILURE : EXIT_SUCCESS);
|
2016-02-07 03:17:45 +01:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-02-07 03:17:45 +01:00
|
|
|
continue;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-02-07 03:17:45 +01:00
|
|
|
check: if (optarg == endptr)
|
|
|
|
error("Failed to parse parse option argument '-%c %s'", c, optarg);
|
|
|
|
}
|
2017-05-03 19:26:58 +02:00
|
|
|
|
|
|
|
if (argc != optind + 1) {
|
|
|
|
usage();
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2017-07-06 23:14:38 +02:00
|
|
|
|
|
|
|
type = argv[optind];
|
|
|
|
|
2017-07-06 23:20:57 +02:00
|
|
|
s->type = signal_lookup_type(type);
|
|
|
|
if (s->type == -1)
|
2017-07-06 23:14:38 +02:00
|
|
|
error("Invalid signal type: %s", type);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-07-06 23:20:57 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = signal_parse_cli(&s, argc, argv);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to parse command line options");
|
|
|
|
|
|
|
|
ret = log_init(&l, l.level, LOG_ALL);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to initialize log");
|
|
|
|
|
|
|
|
ret = signals_init(quit);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to intialize signals");
|
2017-07-06 23:14:38 +02:00
|
|
|
|
|
|
|
info("Starting signal generation: %s", signal_print(&n));
|
2016-11-20 12:59:37 -05:00
|
|
|
|
2016-02-07 03:17:45 +01:00
|
|
|
/* Allocate memory for message buffer */
|
2017-07-06 23:14:38 +02:00
|
|
|
struct sample *t = alloc(SAMPLE_LEN(s.values));
|
|
|
|
|
|
|
|
t->capacity = s.values;
|
2014-06-05 09:34:47 +00:00
|
|
|
|
2016-02-07 01:11:51 +01:00
|
|
|
/* Print header */
|
2016-06-08 23:21:42 +02:00
|
|
|
printf("# VILLASnode signal params: type=%s, values=%u, rate=%f, limit=%d, amplitude=%f, freq=%f\n",
|
2017-07-06 23:14:38 +02:00
|
|
|
argv[optind], s.values, s.rate, s.limit, s.amplitude, s.frequency);
|
2016-02-07 03:17:45 +01:00
|
|
|
printf("# %-20s\t\t%s\n", "sec.nsec(seq)", "data[]");
|
2016-02-07 01:11:51 +01:00
|
|
|
|
2017-07-06 23:14:38 +02:00
|
|
|
ret = signal_open(&n);
|
|
|
|
if (ret)
|
|
|
|
serror("Failed to start node");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-07-06 23:14:38 +02:00
|
|
|
for (;;) {
|
|
|
|
signal_read(&n, &t, 1);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-07-06 23:14:38 +02:00
|
|
|
sample_io_villas_fprint(stdout, t, SAMPLE_IO_ALL & ~SAMPLE_IO_OFFSET);
|
2015-05-06 12:05:07 +02:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
2014-06-05 09:34:47 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-05-06 11:36:51 +02:00
|
|
|
|
2015-08-07 01:11:43 +02:00
|
|
|
/** @} */
|