2021-05-10 00:12:30 +02:00
|
|
|
/** Read / write sample data in different formats.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
|
|
|
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <villas/list.h>
|
|
|
|
#include <villas/plugin.hpp>
|
|
|
|
#include <villas/sample.h>
|
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
|
|
|
|
|
|
|
class Format {
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
int flags; /**< A set of flags which is automatically used. */
|
|
|
|
int real_precision; /**< Number of digits used for floatint point numbers */
|
|
|
|
bool destroy_signals;
|
|
|
|
|
2021-06-18 14:24:28 -04:00
|
|
|
Logger logger;
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
struct {
|
|
|
|
char *buffer;
|
|
|
|
size_t buflen;
|
|
|
|
} in, out;
|
|
|
|
|
|
|
|
struct vlist *signals; /**< Signal meta data for parsed samples by Format::scan() */
|
|
|
|
|
|
|
|
public:
|
|
|
|
Format(int fl);
|
|
|
|
|
2021-06-18 14:28:31 -04:00
|
|
|
virtual
|
|
|
|
~Format();
|
|
|
|
|
|
|
|
virtual
|
|
|
|
bool isBinaryPayload()
|
2021-05-10 00:12:30 +02:00
|
|
|
{ return false; }
|
|
|
|
|
|
|
|
struct vlist * getSignals() const
|
|
|
|
{ return signals; }
|
|
|
|
|
|
|
|
int getFlags() const
|
|
|
|
{ return flags; }
|
|
|
|
|
2021-06-18 14:24:28 -04:00
|
|
|
void setLogger(Logger log)
|
|
|
|
{ logger = log; }
|
2021-05-10 00:12:30 +02:00
|
|
|
|
|
|
|
void start(struct vlist *sigs, int fl = (int) SampleFlags::HAS_ALL);
|
|
|
|
void start(const std::string &dtypes, int fl = (int) SampleFlags::HAS_ALL);
|
|
|
|
|
2021-06-18 14:28:31 -04:00
|
|
|
virtual
|
|
|
|
void start()
|
2021-05-10 00:12:30 +02:00
|
|
|
{ }
|
|
|
|
|
2021-06-18 14:28:31 -04:00
|
|
|
virtual
|
|
|
|
void parse(json_t *json);
|
|
|
|
|
|
|
|
virtual
|
|
|
|
int print(FILE *f, const struct sample * const smps[], unsigned cnt);
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2021-06-18 14:28:31 -04:00
|
|
|
virtual
|
|
|
|
int scan(FILE *f, struct sample * const smps[], unsigned cnt);
|
2021-05-10 00:12:30 +02:00
|
|
|
|
|
|
|
/** Print \p cnt samples from \p smps into buffer \p buf of length \p len.
|
|
|
|
*
|
|
|
|
* @param buf[out] The buffer which should be filled with serialized data.
|
|
|
|
* @param len[in] The length of the buffer \p buf.
|
|
|
|
* @param rbytes[out] The number of bytes which have been written to \p buf. Ignored if nullptr.
|
|
|
|
* @param smps[in] The array of pointers to samples.
|
|
|
|
* @param cnt[in] The number of pointers in the array \p smps.
|
|
|
|
*
|
|
|
|
* @retval >=0 The number of samples from \p smps which have been written into \p buf.
|
|
|
|
* @retval <0 Something went wrong.
|
|
|
|
*/
|
2021-06-18 14:28:31 -04:00
|
|
|
virtual
|
|
|
|
int sprint(char *buf, size_t len, size_t *wbytes, const struct sample * const smps[], unsigned cnt) = 0;
|
2021-05-10 00:12:30 +02:00
|
|
|
|
|
|
|
/** Parse samples from the buffer \p buf with a length of \p len bytes.
|
|
|
|
*
|
|
|
|
* @param buf[in] The buffer of data which should be parsed / de-serialized.
|
|
|
|
* @param len[in] The length of the buffer \p buf.
|
|
|
|
* @param rbytes[out] The number of bytes which have been read from \p buf.
|
|
|
|
* @param smps[out] The array of pointers to samples.
|
|
|
|
* @param cnt[in] The number of pointers in the array \p smps.
|
|
|
|
*
|
|
|
|
* @retval >=0 The number of samples which have been parsed from \p buf and written into \p smps.
|
|
|
|
* @retval <0 Something went wrong.
|
|
|
|
*/
|
2021-06-18 14:28:31 -04:00
|
|
|
virtual
|
|
|
|
int sscan(const char *buf, size_t len, size_t *rbytes, struct sample * const smps[], unsigned cnt) = 0;
|
2021-05-10 00:12:30 +02:00
|
|
|
|
|
|
|
/* Wrappers for sending a (un)parsing single samples */
|
|
|
|
|
|
|
|
int print(FILE *f, const struct sample *smp)
|
|
|
|
{
|
|
|
|
return print(f, &smp, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int scan(FILE *f, struct sample *smp)
|
|
|
|
{
|
|
|
|
return scan(f, &smp, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int sprint(char *buf, size_t len, size_t *wbytes, const struct sample *smp)
|
|
|
|
{
|
|
|
|
return sprint(buf, len, wbytes, &smp, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int sscan(const char *buf, size_t len, size_t *rbytes, struct sample *smp)
|
|
|
|
{
|
|
|
|
return sscan(buf, len, rbytes, &smp, 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class BinaryFormat : public Format {
|
|
|
|
|
|
|
|
public:
|
|
|
|
using Format::Format;
|
|
|
|
|
|
|
|
virtual bool isBinaryPayload()
|
|
|
|
{ return true; }
|
|
|
|
};
|
|
|
|
|
|
|
|
class FormatFactory : public plugin::Plugin {
|
|
|
|
|
|
|
|
public:
|
|
|
|
using plugin::Plugin::Plugin;
|
|
|
|
|
2021-06-18 14:28:31 -04:00
|
|
|
virtual
|
|
|
|
Format * make() = 0;
|
2021-05-10 00:12:30 +02:00
|
|
|
|
|
|
|
static
|
|
|
|
Format * make(json_t *json);
|
|
|
|
|
|
|
|
static
|
|
|
|
Format * make(const std::string &format);
|
2021-06-18 14:24:28 -04:00
|
|
|
|
|
|
|
virtual
|
|
|
|
std::string getType() const
|
|
|
|
{ return "format"; }
|
2021-05-10 00:12:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, const char *name, const char *desc, int flags = 0>
|
|
|
|
class FormatPlugin : public FormatFactory {
|
|
|
|
|
|
|
|
public:
|
|
|
|
using FormatFactory::FormatFactory;
|
|
|
|
|
|
|
|
virtual Format * make()
|
|
|
|
{
|
2021-06-18 14:24:28 -04:00
|
|
|
auto *f = new T(flags);
|
|
|
|
|
|
|
|
f->setLogger(getLogger());
|
|
|
|
|
|
|
|
return f;
|
2021-05-10 00:12:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get plugin name
|
|
|
|
virtual std::string
|
|
|
|
getName() const
|
|
|
|
{ return name; }
|
|
|
|
|
|
|
|
/// Get plugin description
|
|
|
|
virtual std::string
|
|
|
|
getDescription() const
|
|
|
|
{ return desc; }
|
|
|
|
};
|
|
|
|
|
|
|
|
} /* namespace node */
|
|
|
|
} /* namespace villas */
|