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>
|
|
|
|
* @copyright 2014-2016, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
* This file is part of VILLASnode. All Rights Reserved. Proprietary and confidential.
|
|
|
|
* Unauthorized copying of this file, via any medium is strictly prohibited.
|
|
|
|
*
|
|
|
|
* @addtogroup tools Test and debug tools
|
|
|
|
* @{
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
#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>
|
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-03-20 09:03:03 -03:00
|
|
|
|
|
|
|
/* Concat all params */
|
|
|
|
str = NULL;
|
|
|
|
for (int i = 0; i < paramlen; i++)
|
|
|
|
str = strcatf(&str, 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-03-17 02:52:59 -03: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);
|
|
|
|
if (ret)
|
|
|
|
error("Failed to parse hook settings");
|
|
|
|
|
2017-03-20 09:03:03 -03:00
|
|
|
free(str);
|
|
|
|
|
2017-03-17 02:52:59 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-10-22 20:43:36 -04:00
|
|
|
static void usage()
|
|
|
|
{
|
2017-03-20 09:03:03 -03:00
|
|
|
printf("Usage: villas-hook [OPTIONS] NAME [PARAM] \n");
|
2017-03-27 12:26:11 +02:00
|
|
|
printf(" PARAM a string of configuration settings for the hook\n");
|
2016-10-22 20:43:36 -04:00
|
|
|
printf(" OPTIONS are:\n");
|
|
|
|
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-03-27 12:26:11 +02:00
|
|
|
printf(" NAME the name of the hook function\n\n");
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
size_t cnt, recv;
|
2017-03-14 11:19:59 -03:00
|
|
|
|
|
|
|
/* Default values */
|
|
|
|
level = V;
|
|
|
|
cnt = 1;
|
2016-10-22 20:43:36 -04:00
|
|
|
|
2017-03-17 02:52:59 -03:00
|
|
|
char *name;
|
2016-10-22 20:43:36 -04: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-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-14 11:19:59 -03:00
|
|
|
log_init(&log, level, LOG_ALL);
|
2017-03-27 12:26:11 +02:00
|
|
|
log_start(&log);
|
|
|
|
|
2017-03-12 17:10:04 -03:00
|
|
|
memory_init(DEFAULT_NR_HUGEPAGES);
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cnt < 1)
|
|
|
|
error("Vectorize option must be greater than 0");
|
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
ret = pool_init(&q, 10 * cnt, SAMPLE_LEN(DEFAULT_VALUES), &memtype_hugepage);
|
2016-10-22 20:43:36 -04:00
|
|
|
if (ret)
|
|
|
|
error("Failed to initilize memory pool");
|
2017-03-17 01:08:48 -03:00
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
name = argv[optind];
|
2016-10-22 20:43:36 -04:00
|
|
|
|
2017-03-17 01:08:48 -03:00
|
|
|
p = plugin_lookup(PLUGIN_TYPE_HOOK, name);
|
|
|
|
if (!p)
|
2017-03-27 12:26:11 +02:00
|
|
|
error("Unknown hook function '%s'", name);
|
2017-03-20 09:03:03 -03:00
|
|
|
|
|
|
|
config_init(&cfg);
|
2016-10-22 20:43:36 -04:00
|
|
|
|
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");
|
|
|
|
|
|
|
|
hook_start(&h);
|
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-17 01:08:48 -03:00
|
|
|
ret = sample_fscan(stdin, hi.samples[j], NULL);
|
|
|
|
if (ret < 0)
|
|
|
|
break;
|
2017-03-20 09:03:48 -03: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);
|
2016-10-22 20:43:36 -04:00
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
hook_read(&h, samples, &recv);
|
|
|
|
hook_write(&h, samples, &recv);
|
2017-03-12 17:10:04 -03:00
|
|
|
|
2017-03-20 09:04:23 -03:00
|
|
|
for (int j = 0; j < hi.count; j++)
|
2017-03-17 01:08:48 -03:00
|
|
|
sample_fprint(stdout, hi.samples[j], SAMPLE_ALL);
|
2017-03-20 09:04:23 -03:00
|
|
|
fflush(stdout);
|
2017-03-14 11:21:05 -03:00
|
|
|
|
|
|
|
sample_free(samples, cnt);
|
2016-10-22 20:43:36 -04:00
|
|
|
}
|
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
hook_stop(&h);
|
2017-03-17 01:08:48 -03:00
|
|
|
hook_destroy(&h);
|
2017-03-20 09:03:03 -03:00
|
|
|
config_destroy(&cfg);
|
2016-10-22 20:43:36 -04: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;
|
|
|
|
}
|