2017-07-28 18:11:52 +02:00
|
|
|
/** Read / write sample data in different formats.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2020-01-20 17:17:00 +01:00
|
|
|
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
|
2017-07-28 18:11:52 +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.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
|
2020-03-04 13:07:20 +01:00
|
|
|
#include <villas/common.hpp>
|
2018-06-29 09:06:04 +02:00
|
|
|
#include <villas/node.h>
|
2019-03-09 13:34:51 +01:00
|
|
|
#include <villas/signal.h>
|
2017-07-28 18:11:52 +02:00
|
|
|
|
|
|
|
/* Forward declarations */
|
|
|
|
struct sample;
|
2018-05-12 13:56:12 +02:00
|
|
|
struct format_type;
|
2017-07-28 18:11:52 +02:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
enum class IOFlags {
|
2018-08-20 18:31:27 +02:00
|
|
|
/* Bits 0-7 are reserved for for flags defined by enum sample_flags */
|
2019-06-23 16:13:23 +02:00
|
|
|
FLUSH = (1 << 8), /**< Flush the output stream after each chunk of samples. */
|
|
|
|
NONBLOCK = (1 << 9), /**< Dont block io_read() while waiting for new samples. */
|
|
|
|
NEWLINES = (1 << 10), /**< The samples of this format are newline delimited. */
|
|
|
|
DESTROY_SIGNALS = (1 << 11), /**< Signal descriptors are managed by this IO instance. Destroy them in io_destoy() */
|
|
|
|
HAS_BINARY_PAYLOAD = (1 << 12) /**< This IO instance en/decodes binary payloads. */
|
2017-07-28 18:11:52 +02:00
|
|
|
};
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
enum class IOMode {
|
|
|
|
STDIO,
|
|
|
|
CUSTOM
|
2018-10-20 14:21:44 +02:00
|
|
|
};
|
|
|
|
|
2017-07-28 18:11:52 +02:00
|
|
|
struct io {
|
2019-06-23 16:13:23 +02:00
|
|
|
enum State state;
|
2017-07-28 18:11:52 +02:00
|
|
|
int flags;
|
2018-06-29 08:37:14 +02:00
|
|
|
char delimiter; /**< Newline delimiter. */
|
|
|
|
char separator; /**< Column separator (used by csv and villas.human formats only) */
|
2017-07-28 18:11:52 +02:00
|
|
|
|
2018-08-09 08:39:27 +02:00
|
|
|
struct io_direction {
|
2018-05-12 10:41:40 +02:00
|
|
|
/** A format type can use this file handle or overwrite the
|
|
|
|
* format::{open,close,eof,rewind} functions and the private
|
|
|
|
* data in io::_vd.
|
|
|
|
*/
|
2021-02-16 14:15:14 +01:00
|
|
|
FILE *stream;
|
2018-05-12 10:41:40 +02:00
|
|
|
|
|
|
|
char *buffer;
|
2018-05-12 13:47:35 +02:00
|
|
|
size_t buflen;
|
2018-08-09 08:39:27 +02:00
|
|
|
} in, out;
|
2018-05-12 10:41:40 +02:00
|
|
|
|
2019-01-07 10:28:55 +01:00
|
|
|
struct vlist *signals; /**< Signal meta data for parsed samples by io_scan() */
|
2018-08-20 18:31:27 +02:00
|
|
|
bool header_printed;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
enum IOMode mode;
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2017-07-28 18:11:52 +02:00
|
|
|
void *_vd;
|
2018-08-20 18:31:27 +02:00
|
|
|
const struct format_type *_vt;
|
2017-07-28 18:11:52 +02:00
|
|
|
};
|
|
|
|
|
2020-09-10 11:11:42 +02:00
|
|
|
int io_init(struct io *io, const struct format_type *fmt, struct vlist *signals, int flags) __attribute__ ((warn_unused_result));
|
2018-08-20 18:31:27 +02:00
|
|
|
|
2020-09-10 11:11:42 +02:00
|
|
|
int io_init2(struct io *io, const struct format_type *fmt, const char *dt, int flags) __attribute__ ((warn_unused_result));
|
2017-07-28 18:11:52 +02:00
|
|
|
|
2020-09-10 11:11:42 +02:00
|
|
|
int io_destroy(struct io *io) __attribute__ ((warn_unused_result));
|
2017-07-28 18:11:52 +02:00
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
int io_open(struct io *io, const char *uri);
|
2017-07-28 18:11:52 +02:00
|
|
|
|
|
|
|
int io_close(struct io *io);
|
|
|
|
|
2018-08-20 18:31:27 +02:00
|
|
|
void io_header(struct io *io, const struct sample *smp);
|
2018-05-12 11:07:35 +02:00
|
|
|
|
|
|
|
void io_footer(struct io *io);
|
|
|
|
|
2017-08-22 12:20:55 +02:00
|
|
|
int io_print(struct io *io, struct sample *smps[], unsigned cnt);
|
2017-07-28 18:11:52 +02:00
|
|
|
|
2017-08-22 12:20:55 +02:00
|
|
|
int io_scan(struct io *io, struct sample *smps[], unsigned cnt);
|
2017-07-28 18:11:52 +02:00
|
|
|
|
|
|
|
int io_eof(struct io *io);
|
|
|
|
|
|
|
|
void io_rewind(struct io *io);
|
2017-08-05 21:02:09 +02:00
|
|
|
|
|
|
|
int io_flush(struct io *io);
|
|
|
|
|
2017-08-30 00:22:58 +02:00
|
|
|
int io_fd(struct io *io);
|
2017-08-05 21:02:09 +02:00
|
|
|
|
2018-08-20 18:31:27 +02:00
|
|
|
const struct format_type * io_type(struct io *io);
|
|
|
|
|
2017-08-14 14:42:07 +02:00
|
|
|
int io_stream_open(struct io *io, const char *uri);
|
2017-08-05 21:02:09 +02:00
|
|
|
|
|
|
|
int io_stream_close(struct io *io);
|
|
|
|
|
|
|
|
int io_stream_eof(struct io *io);
|
|
|
|
|
|
|
|
void io_stream_rewind(struct io *io);
|
|
|
|
|
2017-08-30 00:22:58 +02:00
|
|
|
int io_stream_fd(struct io *io);
|
|
|
|
|
2017-08-05 21:02:09 +02:00
|
|
|
int io_stream_flush(struct io *io);
|
2018-05-12 13:47:35 +02:00
|
|
|
|
2018-06-29 08:37:14 +02:00
|
|
|
FILE * io_stream_input(struct io *io);
|
|
|
|
FILE * io_stream_output(struct io *io);
|
2018-05-12 15:25:29 +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.
|
|
|
|
*/
|
2018-08-20 18:31:27 +02:00
|
|
|
int io_sscan(struct io *io, const char *buf, size_t len, size_t *rbytes, struct sample *smps[], unsigned cnt);
|
2018-05-12 15:25:29 +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.
|
2019-06-23 16:58:28 +02:00
|
|
|
* @param rbytes[out] The number of bytes which have been written to \p buf. Ignored if nullptr.
|
2018-05-12 15:25:29 +02:00
|
|
|
* @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.
|
|
|
|
*/
|
|
|
|
int io_sprint(struct io *io, char *buf, size_t len, size_t *wbytes, struct sample *smps[], unsigned cnt);
|