/*
 * UART_NetworkLayer_V10.h
 *
 * Created: 11.10.2015 11:52:04
 *  Author: Chenda
 */ 

#ifndef UART_NETWORKLAYER_V10_H_
	#define UART_NETWORKLAYER_V10_H_

	//#include "conf_uart.h"
	//#include <stdint.h>
	//#include <stdbool.h>

	/************************************************************************/
	/* Network layer protocol version                                       */
	/************************************************************************/
	#define UART_NET_LAYER_VER		101			// V1.00
	
	/************************************************************************/
	/* Network layer definitions                                            */
	/************************************************************************/
	#define UART_DATA_LEN_MAX		(UART_PACKET_LEN_MAX-3)
	#define UART_DATA_LEN_MIN		0
	#define UART_BROADCAST_ADDRESS	0x00
	
	/************************************************************************/
	/* Network layer error number                                           */
	/************************************************************************/
	#define UART_E_NL_REC_PACKET_ID	32
	#define UART_E_NL_TRAN_BIG_MESS	33
	
	/************************************************************************/
	/* Network layer event type                                             */
	/************************************************************************/
	#define UART_NL_NO_ACTION		0		// nothing happened, message transmitted or new action possible
	/* Receiver events */
	#define UART_NL_RECEIVING		1		// when message is receiving now
	#define UART_NL_ERROR_REC		2		// message contain errors
	#define UART_NL_FOREIGN_MESS	3		// message dedicated for foreign device
	#define UART_NL_PUBLIC_MESS		4		// same as broad cast message
	#define UART_NL_GROUP_MESS		5		// sub-broad cast message (dedicated for group)
	#define UART_NL_PRIVATE_MESS	6		// message dedicated for this device
	/* Transmitter events */
	#define UART_NL_TRANSMITTING	1		// when message is transmitting now
	#define UART_NL_TRANSMITED		2		// when message is transmitted 
	#define UART_NL_ERROR_TRANS		3		// error upon transmitting
		/* !!! for initiate sending just set length and data !!! */										
	
	
	/************************************************************************/
	/* Network layer structure                                              */
	/************************************************************************/
	struct stNL_UARTx
	{
		struct NetworkLayer
		{
			// if master periphery Packet ID is incremented internally and compared against incoming packet 
			// else packet ID is got from incoming packet
			uint8_t SubBrdCst;
			uint8_t PeriphAddress;
			uint8_t PacketID_LastIn;
			uint8_t PacketID_LastOut;
		}NL;
		
		struct NetworkLayerInterface
		{
			struct NL_Int_Receiver
			{
				uint8_t Event;
				// -- no error data (data always present if not fatal error like link error upon receiving)
				uint8_t Buff[UART_DATA_LEN_MAX];
				uint8_t Length;					// DataLength > 0 = data received
				uint8_t	RecipientAddress;		// Packet target address
				uint8_t	SenderAddress;			// Packet source address
				// -- error handling
				uint8_t E_Nr;
				uint8_t MissedRxBytes;
			}RX;
			struct NL_Int_Transmitter
			{
				uint8_t Event;
				// -- data to be send
				uint8_t Buff[UART_DATA_LEN_MAX];
				uint8_t Length;					// DataLength > 0 = send data
				uint8_t	RecipientAddress;		// Packet target address
				uint8_t	SenderAddress;			// Packet source address
				// -- respond message expected
				bool ExpectRespond;				// initiates time out timer immediately after message transmit
				// -- error handling
				uint8_t E_Nr;
			}TX;
		}NL_Int;
	};

	/************************************************************************/
	/* Network layer functions                                              */
	/************************************************************************/
	struct stNL_UARTx* UART_NL_U1_INIT (void);
	void UART_NL_U1(void);
	void UART_NL_SEND1_U1(uint8_t *Mess, uint8_t Length);
	void UART_NL_SEND2_U1(uint8_t *Mess, uint8_t Length, uint8_t RecAdd);
	void UART_NL_SEND3_U1(uint8_t *Mess, uint8_t Length, uint8_t RecAdd, uint8_t SrcAdd);
	
	// do not use 
	struct stLL_UARTx* UART_NL_U1_GET_LL (void);

#endif /* UART_NETWORKLAYER_V10_H_ */