2016-06-08 22:26:40 +02:00
|
|
|
/** The internal datastructure for a sample of simulation data.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2017-03-03 20:20:13 -04:00
|
|
|
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
|
2017-04-27 12:56:43 +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.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* 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.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-03-03 20:20:13 -04:00
|
|
|
*********************************************************************************/
|
2016-06-08 22:26:40 +02:00
|
|
|
|
2017-02-16 09:04:12 -03:00
|
|
|
#pragma once
|
2016-06-08 22:26:40 +02:00
|
|
|
|
2017-04-26 11:48:58 +02:00
|
|
|
#include "atomic.h"
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2016-06-08 22:26:40 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2016-09-10 22:16:23 -04:00
|
|
|
/* Forward declarations */
|
|
|
|
struct pool;
|
|
|
|
|
2016-06-08 22:26:40 +02:00
|
|
|
/** The length of a sample datastructure with \p values values in bytes. */
|
2016-07-11 18:19:23 +02:00
|
|
|
#define SAMPLE_LEN(len) (sizeof(struct sample) + SAMPLE_DATA_LEN(len))
|
2016-06-08 22:26:40 +02:00
|
|
|
|
|
|
|
/** The length of a sample data portion of a sample datastructure with \p values values in bytes. */
|
2016-07-11 18:19:23 +02:00
|
|
|
#define SAMPLE_DATA_LEN(len) ((len) * sizeof(float))
|
2016-06-08 22:26:40 +02:00
|
|
|
|
|
|
|
/** The offset to the beginning of the data section. */
|
2016-07-11 18:19:23 +02:00
|
|
|
#define SAMPLE_DATA_OFFSET(smp) ((char *) (smp) + offsetof(struct sample, data))
|
2016-06-08 22:26:40 +02:00
|
|
|
|
2017-03-27 12:50:39 +02:00
|
|
|
enum sample_data_format {
|
|
|
|
SAMPLE_DATA_FORMAT_FLOAT= 0,
|
|
|
|
SAMPLE_DATA_FORMAT_INT = 1
|
2016-06-08 22:26:40 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct sample {
|
2017-04-16 23:00:30 +02:00
|
|
|
int sequence; /**< The sequence number of this sample. */
|
|
|
|
int length; /**< The number of values in sample::values which are valid. */
|
|
|
|
int capacity; /**< The number of values in sample::values for which memory is reserved. */
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-04-16 23:00:30 +02:00
|
|
|
atomic_int refcnt; /**< Reference counter. */
|
|
|
|
off_t pool_off; /**< This sample belongs to this memory pool (relative pointer). */
|
|
|
|
struct node *source; /**< The node from which this sample originates. */
|
2016-06-08 22:26:40 +02:00
|
|
|
|
|
|
|
/** All timestamps are seconds / nano seconds after 1.1.1970 UTC */
|
|
|
|
struct {
|
|
|
|
struct timespec origin; /**< The point in time when this data was sampled. */
|
|
|
|
struct timespec received; /**< The point in time when this data was received. */
|
2017-04-16 23:00:30 +02:00
|
|
|
struct timespec sent; /**< The point in time when this data was send for the last time. */
|
2016-06-08 22:26:40 +02:00
|
|
|
} ts;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-27 12:50:39 +02:00
|
|
|
uint64_t format; /**< A long bitfield indicating the number representation of the first 64 values in sample::data[] */
|
2016-09-10 22:16:23 -04:00
|
|
|
|
2016-06-08 22:26:40 +02:00
|
|
|
/** The values. */
|
|
|
|
union {
|
2016-11-07 22:19:30 -05:00
|
|
|
float f; /**< Floating point values. */
|
|
|
|
uint32_t i; /**< Integer values. */
|
|
|
|
} data[]; /**< Data is in host endianess! */
|
2016-06-08 22:26:40 +02:00
|
|
|
};
|
|
|
|
|
2016-10-22 20:47:36 -04:00
|
|
|
/** Request \p cnt samples from memory pool \p p and initialize them.
|
2017-05-12 11:35:21 +02:00
|
|
|
* The reference count will already be set to 1.
|
2016-10-22 20:47:36 -04:00
|
|
|
* Use the sample_get() function to increase it. */
|
|
|
|
int sample_alloc(struct pool *p, struct sample *smps[], int cnt);
|
|
|
|
|
|
|
|
/** Release an array of samples back to their pools */
|
|
|
|
void sample_free(struct sample *smps[], int cnt);
|
2016-09-10 22:16:23 -04:00
|
|
|
|
2016-08-28 23:57:11 -04:00
|
|
|
/** Increase reference count of sample */
|
|
|
|
int sample_get(struct sample *s);
|
|
|
|
|
|
|
|
/** Decrease reference count and release memory if last reference was held. */
|
|
|
|
int sample_put(struct sample *s);
|
|
|
|
|
2017-04-07 17:46:50 +02:00
|
|
|
int sample_copy(struct sample *dst, struct sample *src);
|
|
|
|
|
|
|
|
int sample_copy_many(struct sample *dsts[], struct sample *srcs[], int cnt);
|
|
|
|
int sample_get_many(struct sample *smps[], int cnt);
|
|
|
|
int sample_put_many(struct sample *smps[], int cnt);
|
|
|
|
|
2017-04-16 23:00:30 +02:00
|
|
|
/** Get number representation for a single value of a sample. */
|
2017-03-27 12:50:39 +02:00
|
|
|
int sample_get_data_format(struct sample *s, int idx);
|
|
|
|
|
2017-04-16 23:00:30 +02:00
|
|
|
/** Set number representation for a single value of a sample. */
|
2017-04-06 12:12:56 +02:00
|
|
|
int sample_set_data_format(struct sample *s, int idx, enum sample_data_format fmt);
|
2017-04-26 11:48:58 +02:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|