1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/include/villas/format.hpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

152 lines
4.1 KiB
C++
Raw Permalink Normal View History

2021-05-10 00:12:30 +02:00
/* Read / write sample data in different formats.
*
2022-03-15 09:18:01 -04:00
* Author: Steffen Vogel <post@steffenvogel.de>
2022-03-15 09:28:57 -04:00
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
2022-07-04 18:20:03 +02:00
* SPDX-License-Identifier: Apache-2.0
2021-05-10 00:12:30 +02:00
*/
#pragma once
#include <memory>
#include <villas/list.hpp>
2021-05-10 00:12:30 +02:00
#include <villas/plugin.hpp>
#include <villas/sample.hpp>
#include <villas/signal_list.hpp>
2021-05-10 00:12:30 +02:00
namespace villas {
namespace node {
// Forward declarations
class FormatFactory;
2021-05-10 00:12:30 +02:00
class Format {
friend FormatFactory;
public:
using Ptr = std::unique_ptr<Format>;
2021-05-10 00:12:30 +02:00
protected:
int flags; // A set of flags which is automatically used.
int real_precision; // Number of digits used for floatint point numbers
2021-05-10 00:12:30 +02:00
Logger logger;
2021-05-10 00:12:30 +02:00
struct {
char *buffer;
size_t buflen;
} in, out;
2021-06-18 14:24:28 -04:00
SignalList::Ptr
signals; // Signal meta data for parsed samples by Format::scan()
2021-05-10 00:12:30 +02:00
public:
Format(int fl);
2021-05-10 00:12:30 +02:00
virtual ~Format();
2021-06-18 14:28:31 -04:00
const SignalList::Ptr getSignals() const { return signals; }
2021-05-10 00:12:30 +02:00
int getFlags() const { return flags; }
2021-05-10 00:12:30 +02:00
void start(const SignalList::Ptr sigs, int fl = (int)SampleFlags::ALL);
void start(const std::string &dtypes, int fl = (int)SampleFlags::ALL);
2021-05-10 00:12:30 +02:00
virtual void start() {}
2021-05-10 00:12:30 +02:00
virtual void parse(json_t *json);
2021-06-18 14:28:31 -04:00
virtual int print(FILE *f, const struct Sample *const smps[], unsigned cnt);
2021-05-10 00:12:30 +02: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.
2021-05-10 00:12:30 +02:00
*
* @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.
*/
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.
2021-05-10 00:12:30 +02:00
*
* @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.
*/
virtual int sscan(const char *buf, size_t len, size_t *rbytes,
struct Sample *const smps[], unsigned cnt) = 0;
// 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);
}
2021-05-10 00:12:30 +02:00
};
class BinaryFormat : public Format {
public:
using Format::Format;
2021-05-10 00:12:30 +02:00
};
class FormatFactory : public plugin::Plugin {
public:
using plugin::Plugin::Plugin;
virtual Format *make() = 0;
static Format *make(json_t *json);
static Format *make(const std::string &format);
virtual void init(Format *f) { f->logger = getLogger(); }
virtual std::string getType() const { return "format"; }
virtual bool isHidden() const { return false; }
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;
2021-05-10 00:12:30 +02:00
virtual Format *make() {
auto *f = new T(flags);
2021-06-18 14:24:28 -04:00
init(f);
2021-06-18 14:24:28 -04:00
return f;
}
2021-05-10 00:12:30 +02:00
virtual std::string getName() const { return name; }
2021-05-10 00:12:30 +02:00
virtual std::string getDescription() const { return desc; }
2021-05-10 00:12:30 +02:00
};
} // namespace node
} // namespace villas