2016-10-22 20:43:36 -04:00
|
|
|
/** Receive messages from server snd print them on stdout.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2017-04-27 13:08:17 +02:00
|
|
|
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
* @license GNU General Public License (version 3)
|
2016-10-22 20:43:36 -04:00
|
|
|
*
|
2017-04-27 13:08:17 +02:00
|
|
|
* 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 13:08:17 +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 13:08:17 +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/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
/**
|
2016-10-22 20:43:36 -04:00
|
|
|
* @addtogroup tools Test and debug tools
|
|
|
|
* @{
|
2017-04-27 13:08:17 +02:00
|
|
|
*/
|
2016-10-22 20:43:36 -04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2017-03-20 09:03:48 -03:00
|
|
|
#include <villas/timing.h>
|
2016-10-22 20:43:36 -04:00
|
|
|
#include <villas/sample.h>
|
2017-03-27 12:28:13 +02:00
|
|
|
#include <villas/sample_io.h>
|
2016-11-20 13:11:37 -05:00
|
|
|
#include <villas/hook.h>
|
2016-10-22 20:43:36 -04:00
|
|
|
#include <villas/utils.h>
|
|
|
|
#include <villas/pool.h>
|
|
|
|
#include <villas/log.h>
|
2017-03-05 10:06:32 -04:00
|
|
|
#include <villas/plugin.h>
|
|
|
|
|
2016-11-20 13:00:23 -05:00
|
|
|
#include <villas/kernel/rt.h>
|
2016-11-20 02:45:39 -05:00
|
|
|
|
|
|
|
#include "config.h"
|
2016-10-22 20:43:36 -04:00
|
|
|
|
2017-03-20 09:03:03 -03:00
|
|
|
config_t cfg;
|
|
|
|
|
|
|
|
static int hook_parse_cli(struct hook *h, char *params[], int paramlen)
|
2017-03-17 02:52:59 -03:00
|
|
|
{
|
|
|
|
int ret;
|
2017-03-20 09:03:03 -03:00
|
|
|
char *str;
|
2017-03-17 02:52:59 -03:00
|
|
|
config_setting_t *cfg_root;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-20 09:03:03 -03:00
|
|
|
/* Concat all params */
|
|
|
|
str = NULL;
|
|
|
|
for (int i = 0; i < paramlen; i++)
|
2017-06-17 03:13:42 +02:00
|
|
|
str = strcatf(&str, "%s", params[i]);
|
2017-03-17 02:52:59 -03:00
|
|
|
|
|
|
|
config_set_auto_convert(&cfg, 1);
|
|
|
|
|
2017-03-20 09:03:03 -03:00
|
|
|
if (str) {
|
|
|
|
ret = config_read_string(&cfg, str);
|
|
|
|
if (ret != CONFIG_TRUE)
|
|
|
|
error("Failed to parse argument '%s': %s", str, config_error_text(&cfg));
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-20 09:03:03 -03:00
|
|
|
//config_write(&cfg, stdout);
|
2017-03-17 02:52:59 -03:00
|
|
|
|
2017-03-20 09:03:03 -03:00
|
|
|
cfg_root = config_root_setting(&cfg);
|
2017-03-17 02:52:59 -03:00
|
|
|
ret = hook_parse(h, cfg_root);
|
|
|
|
|
2017-03-20 09:03:03 -03:00
|
|
|
free(str);
|
|
|
|
|
2017-03-27 12:50:01 +02:00
|
|
|
return ret;
|
2017-03-17 02:52:59 -03:00
|
|
|
}
|
|
|
|
|
2016-10-22 20:43:36 -04:00
|
|
|
static void usage()
|
|
|
|
{
|
2017-05-03 19:26:58 +02:00
|
|
|
printf("Usage: villas-hook [OPTIONS] NAME [[PARAM1] [PARAM2] ...]\n");
|
|
|
|
printf(" NAME the name of the hook function\n");
|
|
|
|
printf(" PARAM* a string of configuration settings for the hook\n");
|
|
|
|
printf(" OPTIONS is one or more of the following options:\n");
|
2016-10-22 20:43:36 -04:00
|
|
|
printf(" -h show this help\n");
|
2017-03-12 17:10:04 -03:00
|
|
|
printf(" -d LVL set debug level to LVL\n");
|
2017-03-17 02:52:59 -03:00
|
|
|
printf(" -v CNT process CNT samples at once\n");
|
2017-05-03 19:26:58 +02:00
|
|
|
printf("\n");
|
2017-03-27 12:26:11 +02:00
|
|
|
printf("The following hook functions are supported:\n");
|
|
|
|
plugin_dump(PLUGIN_TYPE_HOOK);
|
2017-03-17 02:52:59 -03:00
|
|
|
printf("\n");
|
|
|
|
printf("Example:");
|
|
|
|
printf(" villas-signal random | villas-hook skip_first seconds=10\n");
|
|
|
|
printf("\n");
|
2016-10-22 20:43:36 -04:00
|
|
|
|
|
|
|
print_copyright();
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2017-03-27 12:26:11 +02:00
|
|
|
int ret, level;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
size_t cnt, recv;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-14 11:19:59 -03:00
|
|
|
/* Default values */
|
|
|
|
level = V;
|
|
|
|
cnt = 1;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-05 10:06:32 -04:00
|
|
|
struct log log;
|
|
|
|
struct plugin *p;
|
2017-03-17 01:08:48 -03:00
|
|
|
struct sample *samples[cnt];
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
struct pool q = { .state = STATE_DESTROYED };
|
2017-03-17 01:08:48 -03:00
|
|
|
struct hook h = { .state = STATE_DESTROYED };
|
2017-03-05 10:06:32 -04:00
|
|
|
|
2016-10-22 20:43:36 -04:00
|
|
|
char c;
|
|
|
|
while ((c = getopt(argc, argv, "hv:d:")) != -1) {
|
|
|
|
switch (c) {
|
|
|
|
case 'v':
|
|
|
|
cnt = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'd':
|
2017-03-14 11:19:59 -03:00
|
|
|
level = atoi(optarg);
|
2016-10-22 20:43:36 -04:00
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
case '?':
|
2016-10-30 22:05:29 -04:00
|
|
|
usage();
|
|
|
|
exit(c == '?' ? EXIT_FAILURE : EXIT_SUCCESS);
|
2016-10-22 20:43:36 -04:00
|
|
|
}
|
|
|
|
}
|
2017-03-12 17:10:04 -03:00
|
|
|
|
2017-03-14 11:18:12 -03:00
|
|
|
if (argc < optind + 1) {
|
2017-03-12 17:10:04 -03:00
|
|
|
usage();
|
2016-10-22 20:43:36 -04:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-05-03 19:26:58 +02:00
|
|
|
char *hookstr = argv[optind];
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-22 20:43:36 -04:00
|
|
|
if (cnt < 1)
|
|
|
|
error("Vectorize option must be greater than 0");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-05-03 19:07:56 +02:00
|
|
|
log_init(&log, level, LOG_ALL);
|
|
|
|
log_start(&log);
|
|
|
|
|
|
|
|
memory_init(DEFAULT_NR_HUGEPAGES);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-15 22:42:24 +02:00
|
|
|
ret = pool_init(&q, 10 * cnt, SAMPLE_LEN(DEFAULT_SAMPLELEN), &memtype_hugepage);
|
2016-10-22 20:43:36 -04:00
|
|
|
if (ret)
|
|
|
|
error("Failed to initilize memory pool");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
|
|
|
|
2016-10-22 20:43:36 -04:00
|
|
|
|
2017-05-03 19:26:58 +02:00
|
|
|
p = plugin_lookup(PLUGIN_TYPE_HOOK, hookstr);
|
2017-03-17 01:08:48 -03:00
|
|
|
if (!p)
|
2017-05-03 19:26:58 +02:00
|
|
|
error("Unknown hook function '%s'", hookstr);
|
2017-03-20 09:03:03 -03:00
|
|
|
|
|
|
|
config_init(&cfg);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-07-12 00:59:52 +02:00
|
|
|
/** @todo villas-hook does not use the path structure */
|
2017-03-27 12:26:11 +02:00
|
|
|
ret = hook_init(&h, &p->hook, NULL);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to initialize hook");
|
|
|
|
|
|
|
|
ret = hook_parse_cli(&h, &argv[optind + 1], argc - optind - 1);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to parse hook config");
|
|
|
|
|
2017-07-09 14:38:58 +02:00
|
|
|
ret = hook_start(&h);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to start hook");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-22 20:43:36 -04:00
|
|
|
while (!feof(stdin)) {
|
2017-03-27 12:26:11 +02:00
|
|
|
ret = sample_alloc(&q, samples, cnt);
|
2017-03-14 11:21:05 -03:00
|
|
|
if (ret != cnt)
|
2017-03-27 12:26:11 +02:00
|
|
|
error("Failed to allocate %zu samples from pool", cnt);
|
2017-03-14 11:21:05 -03:00
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
recv = 0;
|
2017-03-20 09:04:23 -03:00
|
|
|
for (int j = 0; j < cnt && !feof(stdin); j++) {
|
2017-03-27 12:28:13 +02:00
|
|
|
ret = sample_io_villas_fscan(stdin, samples[j], NULL);
|
2017-03-17 01:08:48 -03:00
|
|
|
if (ret < 0)
|
|
|
|
break;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
samples[j]->ts.received = time_now();
|
|
|
|
recv++;
|
2017-03-17 01:08:48 -03:00
|
|
|
}
|
2017-03-20 09:04:23 -03:00
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
debug(15, "Read %zu samples from stdin", recv);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
hook_read(&h, samples, &recv);
|
|
|
|
hook_write(&h, samples, &recv);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-27 12:28:13 +02:00
|
|
|
for (int j = 0; j < recv; j++)
|
|
|
|
sample_io_villas_fprint(stdout, samples[j], SAMPLE_IO_ALL);
|
2017-03-20 09:04:23 -03:00
|
|
|
fflush(stdout);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-14 11:21:05 -03:00
|
|
|
sample_free(samples, cnt);
|
2016-10-22 20:43:36 -04:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-07-09 14:38:58 +02:00
|
|
|
ret = hook_stop(&h);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to stop hook");
|
|
|
|
|
|
|
|
ret = hook_destroy(&h);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to destroy hook");
|
|
|
|
|
2017-03-20 09:03:03 -03:00
|
|
|
config_destroy(&cfg);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-12 17:10:04 -03:00
|
|
|
sample_free(samples, cnt);
|
2017-03-27 12:26:11 +02:00
|
|
|
pool_destroy(&q);
|
2016-10-22 20:43:36 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|