2017-03-05 10:06:32 -04:00
|
|
|
/** Convert hook.
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
|
|
|
* @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/>.
|
2017-03-05 10:06:32 -04:00
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
/** @addtogroup hooks Hook functions
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2017-11-02 13:08:12 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/hook.h>
|
|
|
|
#include <villas/plugin.h>
|
|
|
|
#include <villas/sample.h>
|
2017-03-05 10:06:32 -04:00
|
|
|
|
2017-03-20 09:13:01 -03:00
|
|
|
struct convert {
|
|
|
|
enum {
|
|
|
|
TO_FIXED,
|
|
|
|
TO_FLOAT
|
|
|
|
} mode;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-29 04:25:30 +02:00
|
|
|
double scale;
|
|
|
|
|
|
|
|
long long mask;
|
2017-03-20 09:13:01 -03:00
|
|
|
};
|
|
|
|
|
2017-03-29 04:25:30 +02:00
|
|
|
static int convert_init(struct hook *h)
|
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct convert *p = (struct convert *) h->_vd;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-29 04:25:30 +02:00
|
|
|
p->scale = 1;
|
|
|
|
p->mask = -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
static int convert_parse(struct hook *h, json_t *cfg)
|
2017-03-05 10:06:32 -04:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct convert *p = (struct convert *) h->_vd;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
int ret;
|
|
|
|
json_error_t err;
|
|
|
|
const char *mode = NULL;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-08-03 00:19:27 +02:00
|
|
|
ret = json_unpack_ex(cfg, &err, 0, "{ s?: F, s?: i, s: s }",
|
|
|
|
"scale", &p->scale,
|
|
|
|
"mask", &p->mask,
|
|
|
|
"mode", &mode
|
|
|
|
);
|
|
|
|
if (ret)
|
|
|
|
jerror(&err, "Failed to parse configuration of hook '%s'", plugin_name(h->_vt));
|
2017-03-29 04:25:30 +02:00
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
if (!strcmp(mode, "fixed"))
|
|
|
|
p->mode = TO_FIXED;
|
|
|
|
else if (!strcmp(mode, "float"))
|
|
|
|
p->mode = TO_FLOAT;
|
|
|
|
else
|
2017-08-03 00:19:27 +02:00
|
|
|
error("Invalid 'mode' setting '%s' for hook '%s'", mode, plugin_name(h->_vt));
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-05 22:18:47 +02:00
|
|
|
static int convert_read(struct hook *h, struct sample *smps[], unsigned *cnt)
|
2017-03-27 12:26:11 +02:00
|
|
|
{
|
2017-10-18 15:39:53 +02:00
|
|
|
struct convert *p = (struct convert *) h->_vd;
|
2017-03-27 12:26:11 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < *cnt; i++) {
|
|
|
|
for (int k = 0; k < smps[i]->length; k++) {
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-29 04:25:30 +02:00
|
|
|
/* Only convert values which are not masked */
|
|
|
|
if ((k < sizeof(p->mask) * 8) && !(p->mask & (1LL << k)))
|
|
|
|
continue;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-27 12:26:11 +02:00
|
|
|
switch (p->mode) {
|
2017-03-29 04:25:30 +02:00
|
|
|
case TO_FIXED:
|
|
|
|
smps[i]->data[k].i = smps[i]->data[k].f * p->scale;
|
|
|
|
sample_set_data_format(smps[i], k, SAMPLE_DATA_FORMAT_INT);
|
|
|
|
break;
|
|
|
|
case TO_FLOAT:
|
|
|
|
smps[i]->data[k].f = smps[i]->data[k].i * p->scale;
|
|
|
|
sample_set_data_format(smps[i], k, SAMPLE_DATA_FORMAT_FLOAT);
|
|
|
|
break;
|
2017-03-05 10:06:32 -04:00
|
|
|
}
|
2017-03-27 12:26:11 +02:00
|
|
|
}
|
2017-03-05 10:06:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct plugin p = {
|
|
|
|
.name = "convert",
|
|
|
|
.description = "Convert message from / to floating-point / integer",
|
|
|
|
.type = PLUGIN_TYPE_HOOK,
|
|
|
|
.hook = {
|
2017-09-02 14:27:58 +02:00
|
|
|
.flags = HOOK_PATH | HOOK_NODE,
|
2017-03-05 10:06:32 -04:00
|
|
|
.priority = 99,
|
2017-03-29 04:25:30 +02:00
|
|
|
.init = convert_init,
|
2017-03-27 12:26:11 +02:00
|
|
|
.parse = convert_parse,
|
|
|
|
.read = convert_read,
|
|
|
|
.size = sizeof(struct convert)
|
2017-03-05 10:06:32 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
REGISTER_PLUGIN(&p)
|
|
|
|
|
2017-04-26 11:52:22 +02:00
|
|
|
/** @} */
|