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
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
#include <villas/list.hpp>
|
2021-05-10 00:12:30 +02:00
|
|
|
#include <villas/plugin.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/sample.hpp>
|
|
|
|
#include <villas/signal_list.hpp>
|
2021-05-10 00:12:30 +02:00
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
// Forward declarations
|
|
|
|
class FormatFactory;
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
class Format {
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
friend FormatFactory;
|
2021-08-10 10:12:48 -04:00
|
|
|
|
|
|
|
public:
|
2023-09-07 11:46:39 +02:00
|
|
|
using Ptr = std::unique_ptr<Format>;
|
2021-08-10 10:12:48 -04:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
protected:
|
2023-09-07 11:46:39 +02:00
|
|
|
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
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
Logger logger;
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
struct {
|
|
|
|
char *buffer;
|
|
|
|
size_t buflen;
|
|
|
|
} in, out;
|
2021-06-18 14:24:28 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
SignalList::Ptr
|
|
|
|
signals; // Signal meta data for parsed samples by Format::scan()
|
2021-05-10 00:12:30 +02:00
|
|
|
|
|
|
|
public:
|
2023-09-07 11:46:39 +02:00
|
|
|
Format(int fl);
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual ~Format();
|
2021-06-18 14:28:31 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
const SignalList::Ptr getSignals() const { return signals; }
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int getFlags() const { return flags; }
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2023-09-13 10:16:39 +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
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual void start() {}
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2024-05-27 12:49:58 +02:00
|
|
|
virtual void reset() {}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual void parse(json_t *json);
|
2021-06-18 14:28:31 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual int print(FILE *f, const struct Sample *const smps[], unsigned cnt);
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual int scan(FILE *f, struct Sample *const smps[], unsigned cnt);
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2024-05-27 12:49:58 +02:00
|
|
|
virtual void printMetadata(FILE *f, json_t *json) {}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
/* Print \p cnt samples from \p smps into buffer \p buf of length \p len.
|
2024-02-29 21:47:13 +01: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.
|
|
|
|
*/
|
2023-09-07 11:46:39 +02: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
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
/* Parse samples from the buffer \p buf with a length of \p len bytes.
|
2024-02-29 21:47:13 +01: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.
|
|
|
|
*/
|
2023-09-07 11:46:39 +02:00
|
|
|
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:
|
2023-09-07 11:46:39 +02:00
|
|
|
using Format::Format;
|
2021-05-10 00:12:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class FormatFactory : public plugin::Plugin {
|
|
|
|
|
|
|
|
public:
|
2023-09-07 11:46:39 +02:00
|
|
|
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:
|
2023-09-07 11:46:39 +02:00
|
|
|
using FormatFactory::FormatFactory;
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual Format *make() {
|
|
|
|
auto *f = new T(flags);
|
2021-06-18 14:24:28 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
init(f);
|
2021-06-18 14:24:28 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return f;
|
|
|
|
}
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual std::string getName() const { return name; }
|
2021-05-10 00:12:30 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
virtual std::string getDescription() const { return desc; }
|
2021-05-10 00:12:30 +02:00
|
|
|
};
|
|
|
|
|
2023-08-28 09:34:02 +02:00
|
|
|
} // namespace node
|
|
|
|
} // namespace villas
|