//----------------------------------------------------
/**
	\file		ctk/inc/uprtloom.h
	\ingroup	serial
	\ingroup	all

	\brief		Funkce pro seriovou komunikaci protokol PRT pro loom
				<<< ALL >>>
			
	\author		ConTeK s.r.o., Ing. Vladimir Kastner
	\version	01.01
	\date		12.01.2007
*/
//----------------------------------------------------


#ifndef _UPRT_LOOM_H_
#define _UPRT_LOOM_H_

#include <ctktypes.h>
#include <comm.h>
#include <ucrc16.h>
#include <ucnp1a.h>
#include <hwmodel.h>


//---------------------------------------------------
/**	\name	Definice identifikatoru zprav
	\note	--  smer  pc <-> terminal
	\note	<-  smer  pc <- terminal
	\note	->  smer  pc -> terminal
*/
//@{
#define MSGID_REQ_RSP_MSK			0x80	//!< Maska REQ/RSP, potvrzovaci zprava invertuje bit

#define MSGID_GET_ACTSTATE_REQ		0xD1	//!< -> Zadost o akt. stav stroje
#define MSGID_GET_ACTSTATE_RSP		0x51	//!< <- Informace o akt. stavu stroje
//--
#define MSGID_GET_ACTSTATISTICS_REQ	0xD2	//!< -> Zadost o akt. stav stroje
#define MSGID_GET_ACTSTATISTICS_RSP	0x52	//!< <- Informace o akt. stavu stroje
//--
#define MSGID_GET_STATISTICS_REQ	0xD3	//!< -> Zadost o stav stroje z archivu
#define MSGID_GET_STATISTICS_RSP	0x53	//!< <- Informace o stavu stroje z archivu
//--
#define MSGID_GET_WARP_REQ			0xE1	//!< -> Zadost o jmeno a cislo osnovniho valu
#define MSGID_GET_WARP_RSP			0x61	//!< <- Informace o jmene a cisle osnovniho valu

#define MSGID_SET_WARP_REQ			0x61	//!< -> Nastaveni jmena a cisla osnovniho valu
#define MSGID_SET_WARP_RSP			0xE1	//!< <- Potvrzeni jmena a cisla osnovniho valu
//--
#define MSGID_GET_BEAM_REQ			0xE2	//!< -> Zadost o jmeno a cislo navinu
#define MSGID_GET_BEAM_RSP			0x62	//!< <- Informace o jmene a cisle navinu

#define MSGID_SET_BEAM_REQ			0x62	//!< -> Nastaveni jmena a cisla navinu
#define MSGID_SET_BEAM_RSP			0xE2	//!< <- Potvrzeni jmena a cisla navinu
//--
#define MSGID_GET_WEAVER_REQ		0xE3	//!< -> Zadost o jmeno a cislo tkadleny
#define MSGID_GET_WEAVER_RSP		0x63	//!< <- Informace o jmene a cisle tkadleny

#define MSGID_SET_WEAVER_REQ		0x63	//!< -> Nastaveni jmena a cisla tkadleny
#define MSGID_SET_WEAVER_RSP		0xE3	//!< <- Potvrzeni jmena a cisla tkadleny
//--
#define MSGID_GET_TIME_REQ			0xE4	//!< -> Zadost o cas
#define MSGID_GET_TIME_RSP			0x64	//!< <- Informace o casu

#define MSGID_SET_TIME_REQ			0x64	//!< -> Nastaveni casu
#define MSGID_SET_TIME_RSP			0xE4	//!< <- Potvrzeni casu
//@}


//---------------------------------------------------
//!	\name	Definice typu 
//@{
#ifdef MSVC
//#pragma pack(show)
#pragma pack(push,1)
//#pragma pack(show)
#endif // MSVC


#define STR_LENGTH_lmt		16			//!< Delka textovych retezcu
#define STR_BUFF_LENGTH_lmt	16+1		//!< Delka textoveho bufferu

typedef char8_t		string_lmt[ STR_BUFF_LENGTH_lmt ];
typedef uint32_t	longint_lmt;
typedef uint32_t	time_lmt;
typedef SYSTEMTIME	datetime_lmt;
typedef uint32_t	datetimepack_lmt;	//!< (year-1980)<<25)+(month<<21)+(day<<16)+ (hour<<11)+(min<<5)+(sec>>1)
typedef uint32_t	length_lmt;
typedef uint16_t	integer_lmt;
typedef uint8_t		byte_lmt;
typedef uint32_t	state_lmt;
//@}

//----------------------------------------------------
/**
	\brief	Funkce pro prevod na packovany cas

	\param	datetime	struktura casu

	\return	packovany cas
*/
//----------------------------------------------------

datetimepack_lmt PackDateTime(const datetime_lmt * datetime);


//----------------------------------------------------
/**
	\brief	Funkce pro prevod na cas

	\param	datetimepack	struktura pakovaneho casu

	\return	cas
*/
//----------------------------------------------------

datetime_lmt UnpackDateTime(const datetimepack_lmt * datetimepack);


//----------------------------------------------------
/**
	\name	Definice stavu stroje
*/
//----------------------------------------------------
//@{
#define MST_FAILURE					0		//!< Porucha stroje
#define MST_STOPPED					1		//!< Stroj je zastaven
#define MST_RUNNING					2		//!< Stroj bezi
#define MST_DONE					3		//!< Dokoncena vyroba
#define MST_LOADER					100		//!< Je spusten loader
//@}

//----------------------------------------------------
/**
	\name	Definice typu
*/
//----------------------------------------------------
//@{
//! Statistika stroje merena od urcite udalosti - novy navin, osnova, tkadlena
typedef struct
{
    time_lmt			WorkTime;		//!< Doba chodu stroje v s
    time_lmt			OnTime;			//!< Doba zapnuteho stroje v s
    longint_lmt			ShiftsCount;	//!< Pocet prohozu
    length_lmt			LoomedLength;	//!< Zatkana delka v mm, max 200000 m
    longint_lmt			WeftBreakCount;	//!< Pocet utkovych nedoletu
    time_lmt			WeftBreakTime;	//!< Doba utkovych nedoletu
    longint_lmt			RepairWeftCount;//!< Pocet vyparanych utku
    longint_lmt			WarpBreakCount;	//!< Pocet osnovnich pretrhu
    time_lmt			WarpBreakTime;	//!< Doba osnovnich pretrhu

#ifndef MSVC
} __attribute__((packed)) TIMESTATE_lmt;
#else
} TIMESTATE_lmt;
#endif // MSVC

//! Statistika stroje merena od urcite udalosti - novy navin, osnova, tkadlena
typedef struct
{
    time_lmt			WorkTime;		//!< Doba chodu stroje v s
    time_lmt			OnTime;			//!< Doba zapnuteho stroje v s
    longint_lmt			ShiftsCount;	//!< Pocet prohozu
    length_lmt			LoomedLength;	//!< Zatkana delka v mm, max 200000 m
    longint_lmt			WeftBreakCount;	//!< Pocet utkovych nedoletu
    time_lmt			WeftBreakTime;	//!< Doba utkovych nedoletu
    longint_lmt			RepairWeftCount;//!< Pocet vyparanych utku
    longint_lmt			WarpBreakCount;	//!< Pocet osnovnich pretrhu
    time_lmt			WarpBreakTime;	//!< Doba osnovnich pretrhu

} TIMESTATE_lmtu;


//! Informace o osnove
typedef struct
{
	string_lmt			Number;			//!< Cislo osnovy
	string_lmt			Name;			//!< Jmeno osnovy
	length_lmt			Length;			//!< Celkova delka

#ifndef MSVC
} __attribute__((packed)) WARPINFO_lmt;
#else
} WARPINFO_lmt;
#endif // MSVC


//! Informace o osnove
typedef struct
{
	string_lmt			Number;			//!< Cislo osnovy
	string_lmt			Name;			//!< Jmeno osnovy
	length_lmt			Length;			//!< Celkova delka

} WARPINFO_lmtu;

//! Informace o osnove
typedef WARPINFO_lmt	BEAMINFO_lmt;

//! Informace o osnove
typedef WARPINFO_lmtu	BEAMINFO_lmtu;


//! informace o tkadlene
typedef struct
{
	string_lmt	Number;		//!< Cislo tkadleny                      
	string_lmt	Name;		//!< Jmeno tkadleny                      

#ifndef MSVC
} __attribute__((packed)) WEAVERINFO_lmt;
#else
} WEAVERINFO_lmt;
#endif // MSVC

//! informace o tkadlene
typedef struct
{
	string_lmt	Number;		//!< Cislo tkadleny                      
	string_lmt	Name;		//!< Jmeno tkadleny                      

} WEAVERINFO_lmtu;


//! Statistika osnov
typedef struct
{
	string_lmt			Number;			//!< Cislo osnovy
	string_lmt			Name;			//!< Jmeno osnovy
	datetimepack_lmt	Established;	//!< Datum zalozeni
	datetimepack_lmt	Finalize;		//!< Datum ukonceni
	length_lmt			Length;			//!< Celkova delka
	TIMESTATE_lmt		TimeState;		//!< Dalsi udaje

#ifndef MSVC
} __attribute__((packed)) WARPSTATISTICS_lmt;
#else
} WARPSTATISTICS_lmt;
#endif // MSVC


//! Statistika osnov
typedef struct
{
	string_lmt			Number;			//!< Cislo osnovy
	string_lmt			Name;			//!< Jmeno osnovy
	datetimepack_lmt	Established;	//!< Datum zalozeni
	datetimepack_lmt	Finalize;		//!< Datum ukonceni
	length_lmt			Length;			//!< Celkova delka
	TIMESTATE_lmtu		TimeState;		//!< Dalsi udaje

} WARPSTATISTICS_lmtu;

//! Informace o osnove
typedef WARPSTATISTICS_lmt	BEAMSTATISTICS_lmt;
//! Informace o osnove
typedef WARPSTATISTICS_lmtu	BEAMSTATISTICS_lmtu;


//! informace o tkadlene
typedef struct
{
	string_lmt	Number;		//!< Cislo tkadleny                      
	string_lmt	Name;		//!< Jmeno tkadleny                      

#ifndef MSVC
} __attribute__((packed)) WEAVERSTATISTICS_lmt;
#else
} WEAVERSTATISTICS_lmt;
#endif // MSVC


//! informace o tkadlene
typedef struct
{
	string_lmt	Number;		//!< Cislo tkadleny                      
	string_lmt	Name;		//!< Jmeno tkadleny                      

} WEAVERSTATISTICS_lmtu;


//! Informace o prostojich
typedef struct
{
	longint_lmt		WeftBreakCount;     	//!< pocet utkovych nedoletu
	time_lmt		WeftBreakTime;       	//!< doba utkovych nedoletu
	longint_lmt		WarpBreakCount;     	//!< pocet osnovnich pretrhu
	time_lmt		WarpBreakTime;        	//!< doba osnovniho pretrhu
	longint_lmt		IdentIdle1Count;   		//!< pocet prostoju 1, zakladani osnovy
	time_lmt		IdentIdle1Time;    		//!< doba prostoju 1, zakladani osnovy
	longint_lmt		IdentIdle2Count;    	//!< pocet prostoju 2, lepeni osnovy
	time_lmt		IdentIdle2Time;      	//!< doba prostoju 2, lepeni osnovy
	longint_lmt		IdentIdle3Count;     	//!< pocet prostoju 3, lamelovani
	time_lmt		IdentIdle3Time;      	//!< doba prostoju 3, lamelovani
	longint_lmt		IdentIdle4Count;      	//!< pocet prostoju 4, opravy
	time_lmt		IdentIdle4Time;      	//!< doba prostoju 4, opravy                         
	longint_lmt		IdentIdle5Count;   		//!< pocet prostoju 5, cisteni                       
	time_lmt		IdentIdle5Time;        	//!< doba prostoju 5, cisteni                        
	longint_lmt		IdentIdle6Count;       	//!< pocet prostoju 6, cekani                        
	time_lmt		IdentIdle6Time;       	//!< doba prostoju 6, cekani                         
	longint_lmt		IdentIdle7Count;    	//!< pocet prostooju 7, neidentifikovanych
	time_lmt		IdentIdle7Time;        	//!< doba prostoju 7, neidentifikovanych      

#ifndef MSVC
} __attribute__((packed)) IDENTIDLETIME_lmt;
#else
} IDENTIDLETIME_lmt;
#endif // MSVC


//! Informace o prostojich
typedef struct
{
	longint_lmt		WeftBreakCount;     	//!< pocet utkovych nedoletu
	time_lmt		WeftBreakTime;       	//!< doba utkovych nedoletu
	longint_lmt		WarpBreakCount;     	//!< pocet osnovnich pretrhu
	time_lmt		WarpBreakTime;        	//!< doba osnovniho pretrhu
	longint_lmt		IdentIdle1Count;   		//!< pocet prostoju 1, zakladani osnovy
	time_lmt		IdentIdle1Time;    		//!< doba prostoju 1, zakladani osnovy
	longint_lmt		IdentIdle2Count;    	//!< pocet prostoju 2, lepeni osnovy
	time_lmt		IdentIdle2Time;      	//!< doba prostoju 2, lepeni osnovy
	longint_lmt		IdentIdle3Count;     	//!< pocet prostoju 3, lamelovani
	time_lmt		IdentIdle3Time;      	//!< doba prostoju 3, lamelovani
	longint_lmt		IdentIdle4Count;      	//!< pocet prostoju 4, opravy
	time_lmt		IdentIdle4Time;      	//!< doba prostoju 4, opravy                         
	longint_lmt		IdentIdle5Count;   		//!< pocet prostoju 5, cisteni                       
	time_lmt		IdentIdle5Time;        	//!< doba prostoju 5, cisteni                        
	longint_lmt		IdentIdle6Count;       	//!< pocet prostoju 6, cekani                        
	time_lmt		IdentIdle6Time;       	//!< doba prostoju 6, cekani                         
	longint_lmt		IdentIdle7Count;    	//!< pocet prostooju 7, neidentifikovanych
	time_lmt		IdentIdle7Time;        	//!< doba prostoju 7, neidentifikovanych      

} IDENTIDLETIME_lmtu;


//! Statistika
typedef struct
{
	integer_lmt				LoomNumber;		//!< Cislo stavu
	datetimepack_lmt		RecordTime;		//!< Datum a cas zaznamu
	byte_lmt				RecTimeCounter;	//!< Pocitadlo v ramci stejneho casu
	BEAMSTATISTICS_lmt		Beam;			//!< Informace o nabalu
	WARPSTATISTICS_lmt		Warp;			//!< Informace o osnove
	WEAVERSTATISTICS_lmt	Weaver;			//!< Informace o tkadlene
	IDENTIDLETIME_lmt		Idle;			//!< Informace o prostrojich

#ifndef MSVC
} __attribute__((packed)) STATISTICS_lmt;
#else
} STATISTICS_lmt;
#endif // MSVC


//! Statistika
typedef struct
{
	integer_lmt				LoomNumber;		//!< Cislo stavu
	datetimepack_lmt		RecordTime;		//!< Datum a cas zaznamu
	byte_lmt				RecTimeCounter;	//!< Pocitadlo v ramci stejneho casu
	BEAMSTATISTICS_lmtu		Beam;			//!< Informace o nabalu
	WARPSTATISTICS_lmtu		Warp;			//!< Informace o osnove
	WEAVERSTATISTICS_lmtu	Weaver;			//!< Informace o tkadlene
	IDENTIDLETIME_lmtu		Idle;			//!< Informace o prostrojich

} STATISTICS_lmtu;
//@}

//----------------------------------------------------
/**
	\name	Definice zprav
*/
//----------------------------------------------------
//@{
//@{
//! osnova
typedef struct
{

#ifndef MSVC
} __attribute__((packed)) MSG_GET_WARP_REQ;
#else
} MSG_GET_WARP_REQ;
#endif // MSVC

typedef struct
{
	WARPINFO_lmt	Warp;

#ifndef MSVC
} __attribute__((packed)) MSG_GET_WARP_RSP;
#else
} MSG_GET_WARP_RSP;
#endif // MSVC


typedef struct
{
	WARPINFO_lmt	Warp;

#ifndef MSVC
} __attribute__((packed)) MSG_SET_WARP_REQ;
#else
} MSG_SET_WARP_REQ;
#endif // MSVC

typedef struct
{

#ifndef MSVC
} __attribute__((packed)) MSG_SET_WARP_RSP;
#else
} MSG_SET_WARP_RSP;
#endif // MSVC
//@}

//----------------------------------------------------
//@{
//! navin
typedef struct
{

#ifndef MSVC
} __attribute__((packed)) MSG_GET_BEAM_REQ;
#else
} MSG_GET_BEAM_REQ;
#endif // MSVC

typedef struct
{
	BEAMINFO_lmt	Beam;

#ifndef MSVC
} __attribute__((packed)) MSG_GET_BEAM_RSP;
#else
} MSG_GET_BEAM_RSP;
#endif // MSVC


typedef struct
{
	BEAMINFO_lmt	Beam;

#ifndef MSVC
} __attribute__((packed)) MSG_SET_BEAM_REQ;
#else
} MSG_SET_BEAM_REQ;
#endif // MSVC

typedef struct
{

#ifndef MSVC
} __attribute__((packed)) MSG_SET_BEAM_RSP;
#else
} MSG_SET_BEAM_RSP;
#endif // MSVC
//@}

//----------------------------------------------------
//@{
//! tkadlena

typedef struct
{

#ifndef MSVC
} __attribute__((packed)) MSG_GET_WEAVER_REQ;
#else
} MSG_GET_WEAVER_REQ;
#endif // MSVC

typedef struct
{
	WEAVERINFO_lmt	Weaver;

#ifndef MSVC
} __attribute__((packed)) MSG_GET_WEAVER_RSP;
#else
} MSG_GET_WEAVER_RSP;
#endif // MSVC


typedef struct
{
	WEAVERINFO_lmt	Weaver;

#ifndef MSVC
} __attribute__((packed)) MSG_SET_WEAVER_REQ;
#else
} MSG_SET_WEAVER_REQ;
#endif // MSVC

typedef struct
{

#ifndef MSVC
} __attribute__((packed)) MSG_SET_WEAVER_RSP;
#else
} MSG_SET_WEAVER_RSP;
#endif // MSVC
//@}

//----------------------------------------------------
//@{
//! cas

typedef struct
{

#ifndef MSVC
} __attribute__((packed)) MSG_GET_TIME_REQ;
#else
} MSG_GET_TIME_REQ;
#endif // MSVC

typedef struct
{
	datetimepack_lmt	DateTimePack;		//!< Cas GMT

#ifndef MSVC
} __attribute__((packed)) MSG_GET_TIME_RSP;
#else
} MSG_GET_TIME_RSP;
#endif // MSVC


typedef struct
{
	datetimepack_lmt	DateTimePack;		//!< Cas GMT, synchronizace

#ifndef MSVC
} __attribute__((packed)) SET_TIEM_REQ;
#else
} MSG_SET_TIME_REQ;
#endif // MSVC

typedef struct
{

#ifndef MSVC
} __attribute__((packed)) MSG_SET_TIME_RSP;
#else
} MSG_SET_TIME_RSP;
#endif // MSVC
//@}

//----------------------------------------------------
//@{
// Akt. stav

typedef struct
{

#ifndef MSVC
} __attribute__((packed)) MSG_GET_ACTSTATE_REQ;
#else
} MSG_GET_ACTSTATE_REQ;
#endif // MSVC

typedef struct
{
	state_lmt	ActState;

#ifndef MSVC
} __attribute__((packed)) MSG_GET_ACTSTATE_RSP;
#else
} MSG_GET_ACTSTATE_RSP;
#endif // MSVC
//@}

//----------------------------------------------------
//@{
// Akt. statistika

typedef struct
{

#ifndef MSVC
} __attribute__((packed)) MSG_GET_ACTSTATISTICS_REQ;
#else
} MSG_GET_ACTSTATISTICS_REQ;
#endif // MSVC

typedef struct
{
	STATISTICS_lmt	ActStatistics;			//!< Aktualni stat. zaznam

#ifndef MSVC
} __attribute__((packed)) MSG_GET_ACTSTATISTICS_RSP;
#else
} MSG_GET_ACTSTATISTICS_RSP;
#endif // MSVC
//@}

//----------------------------------------------------
//@{
/** 
	\brief	Statistika z archivu

    \note	Nema-li Master zaznam s pozadovanym RequieredDateTime, zasle zaznam
			s DateTime neblize vyssim, neni-li, zasle zaznam s nejblize nizsim.
			Nema-li Terminal tkalcovského stavu dalsi zaznam, zasle NexDateTime
		    naplnen hodnotou ze zasalaneho Statistics.
*/

typedef struct
{
	datetimepack_lmt	RequiredDateTime;		//!< Datum a cas pozadovaneho stat. zaznamu
	byte_lmt			RequiredRecCounter;		//!< Pocitadlo pozadovaneho stat. zaznamu

#ifndef MSVC
} __attribute__((packed)) MSG_GET_STATISTICS_REQ;
#else
} MSG_GET_STATISTICS_REQ;
#endif // MSVC

typedef struct
{
	STATISTICS_lmt		Statistics;				//!< Statisticky zaznam
    datetimepack_lmt	NextDateTimePack;		//!< Cas dalsiho statistickeho zaznamu
    byte_lmt			NextRecTimeCounter;		//!< Pocitadlo dalsiho stat. zaznamu

#ifndef MSVC
} __attribute__((packed)) MSG_GET_STATISTICS_RSP;
#else
} MSG_GET_STATISTICS_RSP;
#endif // MSVC
//@}
//@}

//----------------------------------------------------
/**
	\brief	Struktura definujici LOOM zpravu

	\note	neodkazovat se na polozku _crc ve strukture SENDBUF
			pouze vyhrazuje 2 bajty pro crc. Nikdy se na
			tuto polozku neodkazujte, protoze obsahuje crc
			pouze u nejdelsi zpravy.
*/
typedef struct
{
	CNP1AMESS		PrtHeader;							//!< zahlavi Prt zpravy, DNode,SNode, DtLength
	byte_lmt		MsgIdent;							//!< Identifikator zpravy

	union
	{
#ifdef MSVC
 #pragma warning(push)
 #pragma warning(disable: 4200)
#endif // MSVC

		uint8_t						_data[0];			//!< Obecny buffer

#ifdef MSVC
 #pragma warning(pop)
#endif // MSVC

		// Osnova
		MSG_GET_WARP_REQ			GetWarpRq;			//!< Zadost MSGID_GET_WARP_REQ
		MSG_GET_WARP_RSP			GetWarpRs;			//!< Odpoved na MSGID_GET_WARP_REQ
		MSG_SET_WARP_REQ			SetWarpRq;			//!< Zadost MSGID_SET_WARP_REQ
		MSG_SET_WARP_RSP			SetWarpRs;			//!< Odpoved na MSGID_SET_WARP_REQ
		// Navin
		MSG_GET_BEAM_REQ			GetBeamRq;			//!< Zadost MSGID_GET_BEAM_REQ
		MSG_GET_BEAM_RSP			GetBeamRs;			//!< Odpoved na MSGID_GET_BEAM_REQ
		MSG_SET_BEAM_REQ			SetBeamRq;			//!< Zadost MSGID_SET_BEAM_REQ
		MSG_SET_BEAM_RSP			SetBeamRs;			//!< Odpoved na MSGID_SET_BEAM_REQ
		// Tkadlena
		MSG_GET_WEAVER_REQ			GetWeaverRq;		//!< Zadost MSGID_GET_WEAVER_REQ
		MSG_GET_WEAVER_RSP			GetWeaverRs;		//!< Odpoved na MSGID_GET_WEAVER_REQ
		MSG_SET_WEAVER_REQ			SetWeaverRq;		//!< Zadost MSGID_SET_WEAVER_REQ
		MSG_SET_WEAVER_RSP			SetWeaverRs;		//!< Odpoved na MSGID_SET_WEAVER_REQ
		// Cas
		MSG_GET_TIME_REQ			GetTimeRq;			//!< Zadost MSGID_GET_TIME_REQ
		MSG_GET_TIME_RSP			GetTimeRs;			//!< Odpoved na MSGID_GET_TIME_REQ
		MSG_SET_TIME_REQ			SetTimeRq;			//!< Zadost MSGID_SET_TIME_REQ
		MSG_SET_TIME_RSP			SetTimeRs;			//!< Odpoved na MSGID_SET_TIME_REQ
		// Stav
		MSG_GET_ACTSTATE_REQ		GetActStateRq;		//!< Zadost MSGID_GET_ACTSTATE_REQ
		MSG_GET_ACTSTATE_RSP		GetActStateRs;		//!< Odpoved na MSGID_GET_ACTSTATE_REQ
		// Akt.statistika
		MSG_GET_ACTSTATISTICS_REQ	GetActStatisticsRq;	//!< Zadost MSGID_GET_ACTSTATISTICS_REQ
		MSG_GET_ACTSTATISTICS_RSP	GetActStatisticsRs;	//!< Odpoved na MSGID_GET_ACTSTATISTICS_REQ
		// Statistika z archivu
		MSG_GET_STATISTICS_REQ		GetStatisticsRq;	//!< Zadost MSGID_GET_STATISTICS_REQ
		MSG_GET_STATISTICS_RSP		GetStatisticsRs;	//!< Odpoved na MSGID_GET_STATISTICS_REQ
	} Msg;

	crc16_t					_crc;						//!< vyhrazeno pro crc	
	
#ifndef MSVC
} __attribute__((packed)) LOOMPRTMESS;
#else
} PRTMESS;
#endif // MSVC

#ifdef MSVC
#pragma pack(pop)
//#pragma pack(show)
#endif // MSVC

//----------------------------------------------------
//! Struktura definujici obecny prijimaci buffer

typedef struct
{
	BUFDESC		Desc;		//!< popisovac bufferu
	PRTMESS		Msg;		//!< PRT zpravy
} PRTBUFDESC;
//@}

//----------------------------------------------------
/**
	\brief	Forward deklarace kontextu
*/
//----------------------------------------------------

struct _PRTDESC;


//----------------------------------------------------
/**
	\name	Metody objektu
*/
//----------------------------------------------------
//@{

typedef void (* cmpTick) (struct _PRTDESC * prt);

//! definice CallBacku
#ifdef UPRT_MST
// master

//! metody
typedef bool_t (* cmpMReadyToSend)(struct _PRTDESC * prt);
//{@
//! definice CallBacku
typedef void (* cmpErrorRs)(struct _PRTDESC * prt, PRTBUFDESC * SndDesc, PRTBUFDESC * RcvDesc);

typedef void (* cmpGetWarpRs)(struct _PRTDESC * prt, bnode_t DNode, bnode_t SNode, WARPINFO_lmtu * Warp);
typedef void (* cmpSetWarpRs)(struct _PRTDESC * prt, bnode_t DNode, bnode_t SNode);
typedef void (* cmpGetBeamRs)(struct _PRTDESC * prt, bnode_t DNode, bnode_t SNode, BEAMINFO_lmtu * Beam);
typedef void (* cmpSetBeamRs)(struct _PRTDESC * prt, bnode_t DNode, bnode_t SNode);
typedef void (* cmpGetWeaverRs)(struct _PRTDESC * prt, bnode_t DNode, bnode_t SNode, WEAVERINFO_lmtu * Weaver);
typedef void (* cmpSetWeaverRs)(struct _PRTDESC * prt, bnode_t DNode, bnode_t SNode);
typedef void (* cmpGetTimeRs)(struct _PRTDESC * prt, bnode_t DNode, bnode_t SNode, datetimepack_lmt * DateTimePack);
typedef void (* cmpSetTimeRs)(struct _PRTDESC * prt, bnode_t DNode, bnode_t SNode);
typedef void (* cmpGetActStateRs)(struct _PRTDESC * prt, bnode_t DNode, bnode_t SNode, state_lmt * State);
typedef void (* cmpGetActStatisticsRs)(struct _PRTDESC * prt, bnode_t DNode, bnode_t SNode, STATISTICS_lmtu * ActStatistics);
typedef void (* cmpGetStatisticsRs)(struct _PRTDESC * prt, bnode_t DNode, bnode_t SNode, STATISTICS_lmtu * Statistics,
					datetimepack_lmt NextDateTimePack, byte_lmt NextRecTimeCounter);

typedef void (* cmpUserDefinedRs)(struct _PRTDESC * prt, PRTBUFDESC * SndDesc, PRTBUFDESC * RcvDesc);

typedef void (* cmpNoResponse)(struct _PRTDESC * prt, PRTBUFDESC * SndDesc, bnode_t DNode, bnode_t SNode);
//@}
#endif // UPRT_MST


#ifdef UPRT_SLV
// slave
//@{
//! definice CallBacku

typedef void (* cmpGetWarpRq)(struct _PRTDESC * prt, bnode_t DNode, bnode_t SNode);
typedef void (* cmpSetWarpRq)(struct _PRTDESC * prt, bnode_t DNode, bnode_t SNode, WARPINFO_lmtu * Warp);
typedef void (* cmpGetBeamRq)(struct _PRTDESC * prt, bnode_t DNode, bnode_t SNode);
typedef void (* cmpSetBeamRq)(struct _PRTDESC * prt, bnode_t DNode, bnode_t SNode, BEAMINFO_lmtu * Beam);
typedef void (* cmpGetWeaverRq)(struct _PRTDESC * prt, bnode_t DNode, bnode_t SNode);
typedef void (* cmpSetWeaverRq)(struct _PRTDESC * prt, bnode_t DNode, bnode_t SNode, WEAVERINFO_lmtu * Weaver);
typedef void (* cmpGetTimeRq)(struct _PRTDESC * prt, bnode_t DNode, bnode_t SNode);
typedef void (* cmpSetTimeRq)(struct _PRTDESC * prt, bnode_t DNode, bnode_t SNode, datetimepack_lmt * DateTimePack);
typedef void (* cmpGetActStateRq)(struct _PRTDESC * prt, bnode_t DNode, bnode_t SNode);
typedef void (* cmpGetActStatisticsRq)(struct _PRTDESC * prt, bnode_t DNode, bnode_t SNode);
typedef void (* cmpGetStatisticsRq)(struct _PRTDESC * prt, bnode_t DNode, bnode_t SNode,
					datetimepack_lmt NextDateTimePack, byte_lmt NextRecTimeCounter);

typedef void (* cmpUserDefinedRq)(struct _PRTDESC * prt, PRTBUFDESC * SndDesc, PRTBUFDESC * RcvDesc);
//@}

#endif // UPRT_SLV

//! chybna zprava	
typedef void (* cmpErrorMsg)(struct _PRTDESC * prt, PRTBUFDESC * SndDesc, PRTBUFDESC * RcvDesc);
//@}


//----------------------------------------------------
//! \brief	Struktura popisujici stav PRT automatu

typedef struct _PRTDESC
{
	volatile bool_t			flRunning;		//!< je inicializovano, po ininializaci na TRUE, pred ukoncenim FALSE

	COMMDESC *			CmmL1;				//!< Ovladac seriove linky 

	volatile bool_t			flTickExecute;	//!< je inicializovano, po ininializaci na TRUE, pred ukoncenim FALSE

#ifdef UPRT_MST
	// master
	volatile uint8_t	mState;				//!< Stav automatu
	bnode_t				mMsgDNode;			//!< Posledni DNode zpravy
	bnode_t				mMsgSNode;			//!< Posledni SNode zpravy
	uint8_t				mMsgIdent;			//!< Posledni identifikator zpravy
	PRTBUFDESC			mSendBuf;			//!< Buffer odesilaneho paketu
	pktsize_t			mSendSize;			//!< Velikost zpravy pro opakovani
	bool_t				mReqPending;		//!< Priznak probihajiciho pozadavku
	ticks_t				mRetryCount;		//!< Pocitadlo opakovani vysilani
	ticks_t				mRetryTimer;		//!< Casovac pro opakovani zpravy
	ticks_t				mRetryTimeout;		//!< Timeout pro opakovani zpravy

	cmpMReadyToSend		MReadyToSend;		//!< ready to master send

	cmpErrorRs			cbErrorRs;			//!< callback pri chybove odpovedi

	cmpGetWarpRs		cbGetWarpRs;		//!< callback pri odpovedi na ctení osnovy
	cmpSetWarpRs		cbSetWarpRs;		//!< callback pri odpovedi na zapis osnovy
	cmpGetBeamRs		cbGetBeamRs;		//!< callback pri odpovedi na ctení navinu
	cmpSetBeamRs		cbSetBeamRs;		//!< callback pri odpovedi na zapis navinu
	cmpGetWeaverRs		cbGetWeaverRs;		//!< callback pri odpovedi na ctení tkadleny
	cmpSetWeaverRs		cbSetWeaverRs;		//!< callback pri odpovedi na zapis tkadleny
	cmpGetTimeRs		cbGetTimeRs;		//!< callback pri odpovedi na ctení casu
	cmpSetTimeRs		cbSetTimeRs;		//!< callback pri odpovedi na zapis casu
	cmpGetActStateRs	cbGetActStateRs;	//!< callback pri odpovedi na ctení akt. stavu
	cmpGetActStatisticsRs	cbGetActStatisticsRs;	//!< callback pri odpovedi na ctení akt. stat. zaznamu
	cmpGetStatisticsRs	cbGetStatisticsRs;	//!< callback pri odpovedi na ctení stat. zaznamu

	cmpUserDefinedRs	cbUserDefinedRs;	//!< callback pri uzivatelem definovane zprave

	cmpNoResponse		cbNoResponse;		//!< callback pri neprijeti odpovedi

#endif // UPRT_MST

#ifdef UPRT_SLV
	// slave
	bnode_t				sMsgDNode;			//!< Posledni prijate DNode zpravy
	bnode_t				sMsgSNode;			//!< Posledni prijate SNode zpravy
	uint8_t				sMsgIdent;			//!< Posledni prijaty identifikator zpravy
	PRTBUFDESC			sSendBuf;			//!< Slave buffer odesilaneho paketu

	cmpGetWarpRq		cbGetWarpRq;		//!< callback pri pozadavku na ctení osnovy
	cmpSetWarpRq		cbSetWarpRq;		//!< callback pri pozadavku na zapis osnovy
	cmpGetBeamRq		cbGetBeamRq;		//!< callback pri pozadavku na ctení navinu
	cmpSetBeamRq		cbSetBeamRq;		//!< callback pri pozadavku na zapis navinu
	cmpGetWeaverRq		cbGetWeaverRq;		//!< callback pri pozadavku na ctení tkadleny
	cmpSetWeaverRq		cbSetWeaverRq;		//!< callback pri pozadavku na zapis tkadleny
	cmpGetTimeRq		cbGetTimeRq;		//!< callback pri pozadavku na ctení casu
	cmpSetTimeRq		cbSetTimeRq;		//!< callback pri pozadavku na zapis casu
	cmpGetActStateRq	cbGetActStateRq;	//!< callback pri pozadavku na ctení akt. stavu
	cmpGetActStatisticsRq	cbGetActStatisticsRq;	//!< callback pri pozadavku na ctení akt. stat. zaznamu
	cmpGetStatisticsRq	cbGetStatisticsRq;	//!< callback pri pozadavku na ctení stat. zaznamu

	cmpUserDefinedRq	cbUserDefinedRq;	//!< callback pri uzivatelem definovane zprave
#endif // UPRT_SLV

 	cmpErrorMsg			cbErrorMsg;			//!< callback pri chybne zprave

	// metody
	cmpTick				Tick;				//!< tick knihovny

	// buffery
	PRTBUFDESC			recvbuf1;
	PRTBUFDESC			recvbuf2;

} PRTDESC; 

#ifdef UPRT_MST
//----------------------------------------------------
//! \brief	Funkce zjistujici, zda je mozne odvysilat dalsi pozadavek

bool_t PrtMReadyToSend(PRTDESC * prt);

//----------------------------------------------------
//! \brief	Funkce pro vyslani pozadavku MSGID_GET_WARP_REQ

void PrtMSendGetWarpReq(PRTDESC * prt,
							bnode_t DNode, bnode_t SNode,
							uint8_t RetryCount, ticks_t RetryTimeout);

//----------------------------------------------------
//! \brief	Funkce pro vyslani pozadavku MSGID_SET_WARP_REQ

void PrtMSendSetWarpReq(PRTDESC * prt,
							bnode_t DNode, bnode_t SNode,
							WARPINFO_lmtu * Warp,
							uint8_t RetryCount, ticks_t RetryTimeout);

//----------------------------------------------------
//! \brief	Funkce pro vyslani pozadavku MSGID_GET_BEAM_REQ

void PrtMSendGetBeamReq(PRTDESC * prt,
							bnode_t DNode, bnode_t SNode,
							uint8_t RetryCount, ticks_t RetryTimeout);

//----------------------------------------------------
//! \brief	Funkce pro vyslani pozadavku MSGID_SET_BEAM_REQ

void PrtMSendSetBeamReq(PRTDESC * prt,
							bnode_t DNode, bnode_t SNode,
							BEAMINFO_lmtu * Beam,
							uint8_t RetryCount, ticks_t RetryTimeout);

//----------------------------------------------------
//! \brief	Funkce pro vyslani pozadavku MSGID_GET_WEAVER_REQ

void PrtMSendGetWeaverReq(PRTDESC * prt,
							bnode_t DNode, bnode_t SNode,
							uint8_t RetryCount, ticks_t RetryTimeout);

//----------------------------------------------------
//! \brief	Funkce pro vyslani pozadavku MSGID_SET_WEAVER_REQ

void PrtMSendSetWeaverReq(PRTDESC * prt,
							bnode_t DNode, bnode_t SNode,
							WEAVERINFO_lmtu * Weaver,
							uint8_t RetryCount, ticks_t RetryTimeout);

//----------------------------------------------------
//! \brief	Funkce pro vyslani pozadavku MSGID_GET_TIME_REQ

void PrtMSendGetTimeReq(PRTDESC * prt,
							bnode_t DNode, bnode_t SNode,
							uint8_t RetryCount, ticks_t RetryTimeout);

//----------------------------------------------------
//! \brief	Funkce pro vyslani pozadavku MSGID_SET_TIME_REQ

void PrtMSendSetTimeReq(PRTDESC * prt,
							bnode_t DNode, bnode_t SNode,
							datetimepack_lmt * DateTimePack,
							uint8_t RetryCount, ticks_t RetryTimeout);

//----------------------------------------------------
//! \brief	Funkce pro vyslani pozadavku MSGID_GET_ACTSTATE_REQ

void PrtMSendGetActStateReq(PRTDESC * prt,
							bnode_t DNode, bnode_t SNode,
							uint8_t RetryCount, ticks_t RetryTimeout);

//----------------------------------------------------
//! \brief	Funkce pro vyslani pozadavku MSGID_GET_ACTSTATISTICS_REQ

void PrtMSendGetActStatisticsReq(PRTDESC * prt,
							bnode_t DNode, bnode_t SNode,
							uint8_t RetryCount, ticks_t RetryTimeout);

//----------------------------------------------------
//! \brief	Funkce pro vyslani pozadavku MSGID_GET_STATISTICS_REQ

void PrtMSendGetStatisticsReq(PRTDESC * prt,
							bnode_t DNode, bnode_t SNode,
							datetimepack_lmt RequiredDateTime,
							byte_lmt RequiredRecCounter,
							uint8_t RetryCount, ticks_t RetryTimeout);

#endif // UPRT_MST

#ifdef UPRT_SLV

//----------------------------------------------------
//! \brief	 Funkce pro vyslani odpovedi na MSGID_GET_WARP_REQ

void PrtSSendGetWarpRsp(PRTDESC * prt,
							bnode_t DNode, bnode_t SNode,
							WARPINFO_lmtu * Warp);

//----------------------------------------------------
//! \brief	Funkce pro vyslani odpovedi na MSGID_SET_WARP_REQ

void PrtSSendSetWarpRsp(PRTDESC * prt,
							bnode_t DNode, bnode_t SNode);

//----------------------------------------------------
//! \brief	Funkce pro vyslani odpovedi na MSGID_GET_BEAM_REQ

void PrtSSendGetBeamRsp(PRTDESC * prt,
							bnode_t DNode, bnode_t SNode,
							BEAMINFO_lmtu * Beam);

//----------------------------------------------------
//! \brief	Funkce pro vyslani odpovedi na MSGID_SET_BEAM_REQ

void PrtSSendSetBeamRsp(PRTDESC * prt,
							bnode_t DNode, bnode_t SNode);

//----------------------------------------------------
//! \brief	Funkce pro vyslani odpovedi na MSGID_GET_WEAVER_REQ

void PrtSSendGetWeaverRsp(PRTDESC * prt,
							bnode_t DNode, bnode_t SNode,
							WEAVERINFO_lmtu * Weaver);

//----------------------------------------------------
//! \brief	Funkce pro vyslani odpovedi na MSGID_SET_WEAVER_REQ

void PrtSSendSetWeaverRsp(PRTDESC * prt,
							bnode_t DNode, bnode_t SNode);

//----------------------------------------------------
//! \brief	Funkce pro vyslani odpovedi na MSGID_GET_TIME_REQ

void PrtSSendGetTimeRsp(PRTDESC * prt,
							bnode_t DNode, bnode_t SNode,
							datetimepack_lmt * DateTimePack);

//----------------------------------------------------
//! \brief	Funkce pro vyslani odpovedi na MSGID_SET_TIME_REQ

void PrtSSendSetTimeRsp(PRTDESC * prt,
							bnode_t DNode, bnode_t SNode);

//----------------------------------------------------
//! \brief	Funkce pro vyslani odpovedi na MSGID_GET_ACTSTATE_REQ

void PrtSSendGetActStateRsp(PRTDESC * prt,
							bnode_t DNode, bnode_t SNode,
							state_lmt * ActState);

//----------------------------------------------------
//! \brief	Funkce pro vyslani odpovedi na MSGID_GET_ACTSTATISTICS_REQ

void PrtSSendGetActStatisticsRsp(PRTDESC * prt,
							bnode_t DNode, bnode_t SNode,
							STATISTICS_lmtu * ActStatistics);

//----------------------------------------------------
//! \brief	Funkce pro vyslani odpovedi na MSGID_GET_STATISTICS_REQ

void PrtSSendGetStatisticsRsp(PRTDESC * prt,
							bnode_t DNode, bnode_t SNode,
							STATISTICS_lmtu * Statistics,
						    datetimepack_lmt NextDateTimePack,
						    byte_lmt NextRecTimeCounter);


#endif // UPRT_SLV

//----------------------------------------------------
/**
	\brief	Funkce pro inicializaci komunikačního automatu 

	\param	prt			Struktura kontextu automatu

	\return	TRUE		inicializace probehla v poradku
			FALSE		chyba
*/
//----------------------------------------------------

bool_t PrtInitialize(PRTDESC * prt, COMMDESC * CmmL1); 

//----------------------------------------------------
/**
	\brief	Funkce pro ukonceni komunikačního automatu 

*/
//----------------------------------------------------

#ifdef ALL_FINALIZE

void PrtFinalize(PRTDESC * prt);

#else
	
 #define PrtFinalize(prt) \
	
#endif // ALL_FINALIZE

#endif // _UPRT_LOOM_H_
