/*
 * sv.h
 *
 *  Created on: 21-01-2021
 *      Author: Krzysztof Jakubczyk
 */

#ifndef SV_H_
#define SV_H_

#include "analog_in.h"
#include "tdefs.h"
#include "helper.h"
#include "comm.h"
#include "config.h"

#define SV_NORMAL	 2
#define SV_VLAN		 3



#define SV_PDU						0x60
#define SV_ASDU						0xA2
#define SV_ASDU_DATA				0x30
#define SV_TAG_ID					0x80
#define SV_TAG_SMPCNT				0x82
#define SV_TAG_CONFREF				0x83
#define SV_TAG_SMPSYNCH				0x85
#define SV_TAG_SEQDATA				0x87


#define SV_QUALITY_TEST				0x800
#define SV_QUALITY_VALIDITY_MASK	0x3


struct sv_seqdata
{
	int32_t CurA_Amp;
	int32_t CurA_Q;
	int32_t CurB_Amp;
	int32_t CurB_Q;
	int32_t CurC_Amp;
	int32_t CurC_Q;
	int32_t CurN_Amp;
	int32_t CurN_Q;
	int32_t VoltA_Amp;
	int32_t VoltA_Q;
	int32_t VoltB_Amp;
	int32_t VoltB_Q;
	int32_t VoltC_Amp;
	int32_t VoltC_Q;
	int32_t VoltN_Amp;
	int32_t VoltN_Q;
};

struct sv_queue
{
	u32 cur;
	u32 next;
	u32 lost;
	char buf[SV_QUEUE_DEPTH][SV_MAX_FR_SIZE];
	char status[SV_QUEUE_DEPTH];
};

#define SV_TO_PROCESS			0x02

struct sv_logic
{
	float *sv_cnt;
	float *IL[4];
	float *UL[4];
	char *sv_id;
	char *sv_dstaddr;
	u32 sv_processed;
	float *lost;
	struct analog_in_params_extended I_params;
	struct analog_in_params_extended U_params;
	struct binary_io kob;
	struct binary_io no_sync;
	struct binary_io qual_good[8];
	struct binary_io test[8];
	float i_mul;
	float i_mul2;
	float u_mul;
	float u_mul2;
	u32 smp_cnt_prev;
	u32 sv_timeout_cnt;
	u32 lost_smp_cnt;
};

struct sv_io
{
	u32 lost_float_out;
	u32 sv_cnt_float_out;

	u32 I_params_ptr_out;
	u32 IL1_float_out;
	u32 IL1_buf_ptr_out;
	u32 IL1_qual_good_out;
	u32 IL1_test_out;

	u32 IL2_float_out;
	u32 IL2_buf_ptr_out;
	u32 IL2_qual_good_out;
	u32 IL2_test_out;

	u32 IL3_float_out;
	u32 IL3_buf_ptr_out;
	u32 IL3_qual_good_out;
	u32 IL3_test_out;

	u32 IL4_float_out;
	u32 IL4_buf_ptr_out;
	u32 IL4_qual_good_out;
	u32 IL4_test_out;

	u32 U_params_ptr_out;
	u32 UL1_float_out;
	u32 UL1_buf_ptr_out;
	u32 UL1_qual_good_out;
	u32 UL1_test_out;

	u32 UL2_float_out;
	u32 UL2_buf_ptr_out;
	u32 UL2_qual_good_out;
	u32 UL2_test_out;

	u32 UL3_float_out;
	u32 UL3_buf_ptr_out;
	u32 UL3_qual_good_out;
	u32 UL3_test_out;

	u32 UL4_float_out;
	u32 UL4_buf_ptr_out;
	u32 UL4_qual_good_out;
	u32 UL4_test_out;

	u32 kob_out;
	u32 no_sync_out;
}__attribute__((__packed__));


struct sv_params
{
	u32 bits;
	u32 sv_appid;
	u32 sv_cfg_rev;
    double I_znam_wtor; ///< watosc znamionowa wtorna
    double I_znam_pierw; ///< wartosc znamionowa pierwotna
    double I_zakr;
    double U_znam_wtor; ///< watosc znamionowa wtorna
    double U_znam_pierw; ///< wartosc znamionowa pierwotna
    double U_zakr;
	u32 strings_start;
}__attribute__((__packed__));

struct sv_args
{
	struct sv_io io;
	struct sv_params params;
//	u16 crc;
}__attribute__((__packed__));

extern void sv(void *args, void *logic);
extern int sv_initlog(void *arguments, void *logic);

extern void iec61850_sv_recv(struct goose_frame *gfr,int len,char type);

#endif
