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

	\brief		Implementace protokolu SerialModbus (v1.1c3)
				<<< ALL >>>
	
	\author		ConTeK s.r.o., Bc. Jakub Kastner
	\version	01.01
	\date		05.01.2016
*/
//----------------------------------------------------

#ifndef _SerialModbus_LA_H_
#define _SerialModbus_LA_H_

#include <ctktypes.h>
#include <stmachine.h>
#include <appsets.h>

#include <modbusL2sl.h>

//----------------------------------------------------
//! Stavy automatu komunikace
typedef enum _MODBUS_FUNCTION_CODE{
	Read_discrete_inp 	= 0x02,
	Read_coils 			= 0x01,
	Write_single_coil	= 0x05,
	Write_Multi_coils	= 0x0F,
	Read_inp_reg		= 0x04,
	Read_holding_reg	= 0x03,
	Write_single_reg	= 0x06,
	Write_Multi_reg		= 0x10,
	RW_Multi_reg		= 0x17,
	Mask_Write_Register	= 0x16,
	Read_FIFO_queue		= 0x18,
//	Read_File_record	= 0x14,
//	Write_File_record	= 0x15,
	Read_excetion_sts	= 0x07,
	Diagnostic			= 0x08,
	Get_com_event_cnt	= 0x0B,
	Get_com_event_log	= 0x0C,
	Report_Server_ID	= 0x11,
	Encapsulated_interface_transpor = 0x2B	//MEI
} MODBUS_FUNCTION_CODE;

typedef enum _DIAGNOSTIC_SUBCODE{
	Return_query_data			= 0x00,
	Restart_com_option			= 0x01,
	Return_diagnostic_reg		= 0x02,
	Change_Ascii_inp_delimiter	= 0x03,
	Force_listen_only_mode		= 0x04,
	//0x05 - 0x09 	RESERVED
	Clear_cnt_and_diag_regs		= 0x0A,
	Return_bus_msg_cnt			= 0x0B,
	Return_bus_com_err_cnt		= 0x0C,
	Return_bus_Except_err_cnt	= 0x0D,
	Return_server_msg_cnt		= 0x0E,
	Return_server_no_resp_cnt	= 0x0F,
	Return_server_NAK_cnt		= 0x10,
	Return_server_Busy_cnt		= 0x11,
	Return_bus_char_overrun_cnt	= 0x12,
	//0x13 RESERVED
	Clear_overrun_cnt			= 0x14
} DIAGNOSTIC_SUBCODE;

typedef enum _MEI_SUBCODE{
	CANopen_general_reference 	= 0x0D,
	Read_device_Identification	= 0x0E,
} MEI_SUBCODE;

typedef enum _MODBUS_EXCEPTION_CODES{
	NO_EXCEPTION			= 0x00,
	Illegal_function		= 0x01,
	Illegal_data_address	= 0x02,
	Illegal_data_value		= 0x03,
	Server_device_failure	= 0x04,
	Acknowledge				= 0x05,
	Server_device_busy		= 0x06,
	Memory_parity_error		= 0x07,
	Gateway_path_unavailable				= 0x0A,
	Gateway_target_device_failed_respond	= 0x0B
} MODBUS_EXCEPTION_CODES;


//----------------------------------------------------
//! Struktura kontextu automatu komunikace

typedef struct _MODBUS_LA_CTX
{
	MODBUS_CTX *	Cmm;	//!< ukazatel na linkovou vrstu

	
} MODBUS_LA_CTX;

//----------------------------------------------------
/**
	\brief	Funkce pro inicializaci komunikacniho automatu 

	\param	_ctx		Struktura kontextu automatu komunikace
	\param	_cmm		Adresa knihovny fyzicke vrstvy
*/
//----------------------------------------------------

bool_t MBLAInitialize( MODBUS_LA_CTX * const _ctx, MODBUS_CTX * const _cmm );


//----------------------------------------------------
/**
	\brief	Funkce pro ukonceni komunikacniho automatu 

	\param	_ctx		Struktura kontextu automatu komunikace
*/
//----------------------------------------------------

void MBLAFinalize( MODBUS_LA_CTX * const _ctx );

#endif //_SerialModbus_LA_H_


















