2014-07-14 11:49:44 +00:00
|
|
|
/** Nodes.
|
2014-06-05 09:34:29 +00:00
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2016-02-09 05:33:19 +01:00
|
|
|
* @copyright 2014-2016, Institute for Automation of Complex Power Systems, EONERC
|
2015-06-02 21:53:04 +02:00
|
|
|
* This file is part of S2SS. All Rights Reserved. Proprietary and confidential.
|
2015-08-07 01:11:43 +02:00
|
|
|
* Unauthorized copying of this file, via any medium is strictly prohibited.
|
2015-06-02 21:53:04 +02:00
|
|
|
*********************************************************************************/
|
2014-06-05 09:34:29 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
#include "sample.h"
|
2014-12-05 12:39:52 +01:00
|
|
|
#include "node.h"
|
2014-06-05 09:34:56 +00:00
|
|
|
#include "cfg.h"
|
2014-06-05 09:34:29 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
/** List of registered node-types */
|
|
|
|
struct list node_types = LIST_INIT();
|
2014-12-05 12:39:52 +01:00
|
|
|
|
2015-11-23 16:42:43 +01:00
|
|
|
int node_parse(struct node *n, config_setting_t *cfg)
|
|
|
|
{
|
2015-12-04 17:34:57 +01:00
|
|
|
return n->_vt->parse ? n->_vt->parse(n, cfg) : 0;
|
2015-11-23 16:42:43 +01:00
|
|
|
}
|
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
int node_read(struct node *n, struct sample *smps[], unsigned cnt)
|
2015-11-23 16:42:43 +01:00
|
|
|
{
|
2016-06-08 22:38:21 +02:00
|
|
|
int nread = 0;
|
2015-11-23 16:42:43 +01:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
if (!n->_vt->read)
|
|
|
|
return -1;
|
2016-01-14 22:59:57 +01:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
/* Send in parts if vector not supported */
|
|
|
|
if (n->_vt->vectorize > 0 && n->_vt->vectorize < cnt) {
|
|
|
|
while (cnt - nread > 0) {
|
|
|
|
nread += n->_vt->read(n, &smps[nread], MIN(cnt - nread, n->_vt->vectorize));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
nread = n->_vt->read(n, smps, cnt);
|
|
|
|
}
|
2016-01-14 22:59:57 +01:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
return nread;
|
2016-01-14 22:59:57 +01:00
|
|
|
}
|
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
int node_write(struct node *n, struct sample *smps[], unsigned cnt)
|
2016-01-14 22:59:57 +01:00
|
|
|
{
|
2016-06-08 22:38:21 +02:00
|
|
|
int nsent = 0;
|
|
|
|
|
|
|
|
if (!n->_vt->write)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Send in parts if vector not supported */
|
|
|
|
if (n->_vt->vectorize > 0 && n->_vt->vectorize < cnt) {
|
|
|
|
while (cnt - nsent > 0)
|
|
|
|
nsent += n->_vt->write(n, &smps[nsent], MIN(cnt - nsent, n->_vt->vectorize));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
nsent = n->_vt->write(n, smps, cnt);
|
|
|
|
}
|
2016-01-14 22:59:57 +01:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
return nsent;
|
2016-01-14 22:59:57 +01:00
|
|
|
}
|
2015-11-23 16:42:43 +01:00
|
|
|
|
2015-12-13 00:45:20 +01:00
|
|
|
int node_init(struct node_type *vt, int argc, char *argv[], config_setting_t *cfg)
|
2015-12-11 17:56:14 +01:00
|
|
|
{
|
2016-06-08 22:38:21 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (vt->state != NODE_TYPE_UNINITIALIZED)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
info("Initializing " YEL("%s") " node type", vt->name);
|
|
|
|
{ INDENT
|
|
|
|
ret = vt->init ? vt->init(argc, argv, cfg) : -1;
|
|
|
|
}
|
2015-08-07 01:11:43 +02:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
if (ret == 0)
|
|
|
|
vt->state = NODE_TYPE_INITIALIZED;
|
|
|
|
|
|
|
|
return ret;
|
2015-03-31 13:54:04 +02:00
|
|
|
}
|
|
|
|
|
2015-12-13 00:45:20 +01:00
|
|
|
int node_deinit(struct node_type *vt)
|
2015-12-11 17:56:14 +01:00
|
|
|
{
|
2016-06-08 22:38:21 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (vt->state != NODE_TYPE_INITIALIZED)
|
|
|
|
return -1;
|
2015-12-13 00:45:20 +01:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
info("De-initializing " YEL("%s") " node type", vt->name);
|
|
|
|
{ INDENT
|
|
|
|
ret = vt->deinit ? vt->deinit() : -1;
|
2015-03-31 13:54:04 +02:00
|
|
|
}
|
2016-06-08 22:38:21 +02:00
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
vt->state = NODE_TYPE_UNINITIALIZED;
|
2015-10-09 13:04:52 +02:00
|
|
|
|
2016-06-08 22:38:21 +02:00
|
|
|
return ret;
|
2015-03-31 13:54:04 +02:00
|
|
|
}
|
|
|
|
|
2014-12-05 12:39:52 +01:00
|
|
|
int node_start(struct node *n)
|
2015-12-11 17:56:14 +01:00
|
|
|
{
|
2015-11-16 10:51:00 +01:00
|
|
|
int ret;
|
2016-02-09 05:35:23 +01:00
|
|
|
|
|
|
|
if (n->state != NODE_CREATED && n->state != NODE_STOPPED)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
n->state = NODE_STARTING;
|
2014-12-05 12:39:52 +01:00
|
|
|
|
2015-11-29 22:45:46 +01:00
|
|
|
info("Starting node %s", node_name_long(n));
|
2014-12-05 12:39:52 +01:00
|
|
|
{ INDENT
|
2015-12-04 01:54:33 +01:00
|
|
|
ret = n->_vt->open ? n->_vt->open(n) : -1;
|
2014-06-05 09:34:56 +00:00
|
|
|
}
|
2015-11-16 10:51:00 +01:00
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
n->state = NODE_RUNNING;
|
|
|
|
|
|
|
|
return ret;
|
2014-12-05 12:39:52 +01:00
|
|
|
}
|
2014-06-05 09:34:29 +00:00
|
|
|
|
2014-12-05 12:39:52 +01:00
|
|
|
int node_stop(struct node *n)
|
2015-12-11 17:56:14 +01:00
|
|
|
{
|
2015-11-16 10:51:00 +01:00
|
|
|
int ret;
|
|
|
|
|
2015-11-29 22:45:46 +01:00
|
|
|
if (n->state != NODE_RUNNING)
|
|
|
|
return -1;
|
2016-02-09 05:35:23 +01:00
|
|
|
|
|
|
|
n->state = NODE_STOPPING;
|
2015-11-29 22:45:46 +01:00
|
|
|
|
|
|
|
info("Stopping node %s", node_name(n));
|
2014-12-05 12:39:52 +01:00
|
|
|
{ INDENT
|
2015-12-04 01:54:33 +01:00
|
|
|
ret = n->_vt->close ? n->_vt->close(n) : -1;
|
2014-12-05 12:39:52 +01:00
|
|
|
}
|
2015-11-16 10:51:00 +01:00
|
|
|
|
|
|
|
if (ret == 0)
|
|
|
|
n->state = NODE_STOPPED;
|
|
|
|
|
|
|
|
return ret;
|
2014-06-05 09:34:29 +00:00
|
|
|
}
|
2015-03-18 16:18:10 +01:00
|
|
|
|
2015-12-12 12:40:55 +01:00
|
|
|
char * node_name(struct node *n)
|
2015-10-17 19:05:15 +02:00
|
|
|
{
|
2015-11-29 22:45:46 +01:00
|
|
|
if (!n->_name)
|
2015-12-04 01:54:33 +01:00
|
|
|
strcatf(&n->_name, RED("%s") "(" YEL("%s") ")", n->name, n->_vt->name);
|
2015-11-29 22:45:46 +01:00
|
|
|
|
|
|
|
return n->_name;
|
|
|
|
}
|
2015-10-17 19:05:15 +02:00
|
|
|
|
2015-12-12 12:40:55 +01:00
|
|
|
char * node_name_long(struct node *n)
|
2015-11-29 22:45:46 +01:00
|
|
|
{
|
2015-12-11 18:19:35 +01:00
|
|
|
if (!n->_name_long) {
|
|
|
|
if (n->_vt->print) {
|
|
|
|
char *name_long = n->_vt->print(n);
|
|
|
|
strcatf(&n->_name_long, "%s: %s", node_name(n), name_long);
|
|
|
|
free(name_long);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
n->_name_long = node_name(n);
|
|
|
|
}
|
2015-11-29 22:45:46 +01:00
|
|
|
|
|
|
|
return n->_name_long;
|
2015-10-17 19:05:15 +02:00
|
|
|
}
|
|
|
|
|
2015-12-13 02:02:29 +01:00
|
|
|
const char * node_name_short(struct node *n)
|
|
|
|
{
|
|
|
|
return n->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * node_name_type(struct node *n)
|
|
|
|
{
|
|
|
|
return n->_vt->name;
|
|
|
|
}
|
|
|
|
|
2015-11-23 16:42:43 +01:00
|
|
|
int node_reverse(struct node *n)
|
2015-03-18 16:18:10 +01:00
|
|
|
{
|
2015-11-23 16:42:43 +01:00
|
|
|
return n->_vt->reverse ? n->_vt->reverse(n) : -1;
|
2015-03-21 15:29:00 +01:00
|
|
|
}
|
2015-03-21 15:23:57 +01:00
|
|
|
|
2015-10-13 15:05:48 +02:00
|
|
|
struct node * node_create(struct node_type *vt)
|
2015-03-21 15:23:57 +01:00
|
|
|
{
|
2015-10-13 15:05:48 +02:00
|
|
|
struct node *n = alloc(sizeof(struct node));
|
|
|
|
|
2015-11-23 16:32:24 +01:00
|
|
|
list_push(&vt->instances, n);
|
|
|
|
|
2015-10-13 15:05:48 +02:00
|
|
|
n->_vt = vt;
|
2015-11-29 22:47:57 +01:00
|
|
|
n->_vd = alloc(n->_vt->size);
|
|
|
|
|
|
|
|
if (n->_vt->create)
|
|
|
|
n->_vt->create(n);
|
|
|
|
|
2015-11-16 10:51:00 +01:00
|
|
|
n->state = NODE_CREATED;
|
2015-10-13 15:05:48 +02:00
|
|
|
|
|
|
|
return n;
|
2015-03-21 15:23:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void node_destroy(struct node *n)
|
|
|
|
{
|
2015-11-23 16:42:43 +01:00
|
|
|
if (n->_vt->destroy)
|
|
|
|
n->_vt->destroy(n);
|
2015-12-13 00:42:59 +01:00
|
|
|
|
|
|
|
list_remove(&n->_vt->instances, n);
|
2015-03-21 15:23:57 +01:00
|
|
|
|
2015-11-29 22:47:57 +01:00
|
|
|
free(n->_vd);
|
2015-11-29 22:45:46 +01:00
|
|
|
free(n->_name);
|
2015-03-21 15:23:57 +01:00
|
|
|
free(n);
|
2015-03-18 16:18:10 +01:00
|
|
|
}
|