2025-01-14 14:42:39 +00:00
|
|
|
/* Shift timestamps of samples.
|
2019-03-26 15:33:47 +01:00
|
|
|
*
|
2025-01-14 14:42:39 +00:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstring>
|
2019-03-26 15:33:47 +01:00
|
|
|
|
|
|
|
#include <villas/hook.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/sample.hpp>
|
2025-01-14 14:42:39 +00:00
|
|
|
#include <villas/timing.hpp>
|
2019-03-26 15:33:47 +01:00
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
|
|
|
|
|
|
|
class ShiftTimestampHook : public Hook {
|
|
|
|
|
|
|
|
protected:
|
2025-01-14 14:42:39 +00:00
|
|
|
timespec offset;
|
|
|
|
enum { SHIFT_ORIGIN, SHIFT_RECEIVED } mode;
|
2019-03-26 15:33:47 +01:00
|
|
|
|
|
|
|
public:
|
2025-01-14 14:42:39 +00:00
|
|
|
ShiftTimestampHook(Path *p, Node *n, int fl, int prio, bool en = true)
|
|
|
|
: Hook(p, n, fl, prio, en), mode(SHIFT_ORIGIN) {}
|
|
|
|
|
|
|
|
virtual void parse(json_t *json) {
|
|
|
|
double o;
|
|
|
|
const char *m = nullptr;
|
|
|
|
int ret;
|
|
|
|
json_error_t err;
|
|
|
|
|
|
|
|
assert(state != State::STARTED);
|
|
|
|
|
|
|
|
Hook::parse(json);
|
|
|
|
|
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s?: s, s: F }", "mode", &m, "offset",
|
|
|
|
&o);
|
|
|
|
if (ret)
|
|
|
|
throw ConfigError(json, err, "node-config-hook-shift_ts");
|
|
|
|
|
|
|
|
if (m) {
|
|
|
|
if (!strcmp(m, "origin"))
|
|
|
|
mode = SHIFT_ORIGIN;
|
|
|
|
else if (!strcmp(m, "received"))
|
|
|
|
mode = SHIFT_RECEIVED;
|
|
|
|
else
|
|
|
|
throw ConfigError(json, "node-config-hook-shift_ts-mode",
|
|
|
|
"Invalid mode parameter '{}'", m);
|
|
|
|
}
|
|
|
|
|
|
|
|
offset = time_from_double(o);
|
|
|
|
|
|
|
|
state = State::PARSED;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual Hook::Reason process(struct Sample *smp) {
|
|
|
|
timespec *ts;
|
|
|
|
|
|
|
|
assert(state == State::STARTED);
|
|
|
|
|
|
|
|
switch (mode) {
|
|
|
|
case SHIFT_ORIGIN:
|
|
|
|
ts = &smp->ts.origin;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SHIFT_RECEIVED:
|
|
|
|
ts = &smp->ts.received;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return Hook::Reason::ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ts = time_add(ts, &offset);
|
|
|
|
|
|
|
|
return Reason::OK;
|
|
|
|
}
|
2019-03-26 15:33:47 +01:00
|
|
|
};
|
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
// Register hook
|
2020-06-14 15:00:02 +02:00
|
|
|
static char n[] = "shift_ts";
|
|
|
|
static char d[] = "Shift timestamps of samples";
|
2025-01-14 14:42:39 +00:00
|
|
|
static HookPlugin<ShiftTimestampHook, n, d,
|
|
|
|
(int)Hook::Flags::NODE_READ | (int)Hook::Flags::PATH>
|
|
|
|
p;
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2025-01-14 14:42:39 +00:00
|
|
|
} // namespace node
|
|
|
|
} // namespace villas
|