
#ifndef ISO15765_3_TASK_H_
#define ISO15765_3_TASK_H_

/*----------------------------- Icludes ----------------------------------*/

//#include "Global.h"
#include "Std_Types.h"
#include "DiagnosticR/Comp_ISO_15765_3/Iso15765_3.h"

/*----------------------------- Defines ----------------------------------*/

#if(UDS_SERVICE_SECURITY_ACCESS_STATUS==SERVICE_ENABLED)
/* Security Control */
#define SECURITY_DELAY		((UI_8)0)
#define SECURITY_NO_DELAY	((UI_8)1)
#endif

/*----------------------------- Globals ----------------------------------*/

extern BOOL session_expired;

/*-------------------------- Function Prototypes -------------------------*/

/* Response Control */
void Iso15765_3_RequestRespondCtrlInicialitza(void);
void Iso15765_3_RequestRespondCtrl(void);
void SessionChangeRequest(UI_8 session_change);

/* Session Control */
#ifdef UDS_SERVICE_SESSION_CONTROL
void Iso15765_3_SessionCtrlTaskInicialitza(void);
void Iso15765_3_SessionCtrlTask(void);
void UDSSecuritySessionChanged(void);
#endif

#ifdef UDS_SERVICE_ECU_RESET
void Iso15765_3_EcuResetTaskInicialitza(void);
void UDSResetRequest(UI_8 req_reset_type, UI_8 size);
#endif

/* Security Access */
#ifdef UDS_SERVICE_SECURITY_ACCESS
void Iso15765_3_SecurityTaskInicialitza(void);
void Iso15765_3_SecurityTask(void);
void UDSSecurityRequest(UI_8 req_acces_mode, UI_8* sec_buf);
void UDSSecuritySessionChanged(void);
void UdsSecurityTaskRequestSeed(UI_8 level);
void UdsSecurityTaskCheckKey(UI_8 level, UI_8* key);
void UDSSecurityTaskNotifySessionChanged(void);
void UdsSecurityTaskForceLockState(UI_8 state);
#endif

/* Reset Control */
#ifdef UDS_SERVICE_ECU_RESET
void Iso15765_3_SEcuResetTaskInicialitza(void);
void Iso15765_3_EcuResetTask(void);
void Iso15765_3_ResetRequest(UI_8 req_reset_type);
#endif

/*---------------- Configurable LIN Services Prototypes -------------------*/
#ifdef LIN_SERVICE_ASSIGN_FRAME_IDENTIFIER
void LIN_ASSIGN_FRAME_IDENTIFIER_CALLBACK(UI_8 supplierID_LSB, UI_8 supplierID_MSB, UI_8 messageID_LSB, UI_8 messageID_MSB, UI_8 new_pid);
#endif

#ifdef LIN_SERVICE_ASSIGN_FRAME_IDENTIFIER_RANGE
void LIN_ASSIGN_FRAME_IDENTIFIER_RANGE_CALLBACK(UI_8 index, const UI_8* new_pid_list);
#endif

#ifdef LIN_SERVICE_READ_BY_IDENTIFIER
void LIN_READ_BY_IDENTIFIER_CALLBACK(UI_8 id);
#endif

#ifdef LIN_SERVICE_ASSIGN_NAD
void LIN_ASSIGN_NAD_CALLBACK(UI_16 supplierId, UI_16 functionId, UI_8 new_nad);
#endif

#ifdef LIN_SERVICE_CONDITIONAL_CHANGE_NAD
void LIN_CONDITIONAL_CHANGE_NAD_CALLBACK(UI_8 id, UI_8 byte, UI_8 mask, UI_8 invert, UI_8 new_nad);
#endif

/*---------------- Configurable UDS Services Prototypes -------------------*/

#ifdef UDS_INI_SESSION_CALLBACK
void UDS_INI_SESSION_CALLBACK(void);
#endif

#ifdef UDS_SERVICE_ECU_RESET
BOOL CHECK_RESET_POSSIBLE(UI_8 reset_type);
BOOL CHECK_RESET_TYPE(UI_8 reset_type);
void EXECUTE_RESET(UI_8 reset_type);
#endif

#ifdef UDS_SERVICE_SECURITY_ACCESS
UI_8 UDS_SECURITY_REQUEST_SEED_CALLBACK(UI_8 seed_type, UI_8* p_local_seed);
BOOL UDS_SECURITY_CHECK_KEY_CALLBACK(UI_8 key_type, UI_8* p_local_seed, UI_8* p_tool_key, UI_8* response);
#endif

#ifdef UDS_SERVICE_CONTROL_DTC_SETTING
void UDS_SERVICE_CONTROL_DTC_SETTING_CALLBACK(UI_8 dtc_setting_type, UI_8* buf_data_rx, UI_16 size);
#endif

#ifdef UDS_SERVICE_CLEAR_DIAGNOSTIC_INFORMATION 
void UDS_SERVICE_CLEAR_DIAGNOSTIC_INFORMATION_CALLBACK(UI_8 groupOfDTCHighByte, UI_8 groupOfDTCMiddleByte, UI_8 groupOfDTCLowByte);
#endif

#ifdef UDS_SERVICE_READ_DTC_INFORMATION
void UDS_SERVICE_READ_DTC_INFORMATION_CALLBACK(UI_8 id, UI_8 *data, UI_16 size);
#endif

#ifdef UDS_SERVICE_READ_MEMORY_BY_ADDRESS
void UDS_SERVICE_READ_MEMORY_BY_ADDRESS_CALLBACK(UI_8 mem_size_len,
        UI_8 mem_addr_len, UI_8* mem_addr, UI_8* mem_size);
#endif

#ifdef UDS_SERVICE_WRITE_DATA_BY_IDENTIFIER
void UDS_SERVICE_WRITE_DATA_BY_IDENTIFIER_CALLBACK(UI_8 id_high, UI_8 id_low, UI_8* buf_data_rx, UI_16 size);
#endif

#ifdef UDS_SERVICE_READ_DATA_BY_IDENTIFIER
UI_8 UDS_SERVICE_READ_DATA_BY_IDENTIFIER_CALLBACK(UI_8 id_high, UI_8 id_low);
#endif

#ifdef UDS_SERVICE_WRITE_MEMORY_BY_ADDRESS
void UDS_SERVICE_WRITE_MEMORY_BY_ADDRESS_CALLBACK(UI_8 data_len_format,
        UI_8* p_buf, UI_16 size);
#endif

#ifdef UDS_SERVICE_IO_CTRL_BY_ID 
void UdsInputOutputControlByIdentifier(UI_8 id_h, UI_8 id_l, UI_8 io_ctrl_type, UI_8* buf_data_rx, UI_16 size);
#endif

#ifdef UDS_SERVICE_ROUTINE_CONTROL
void UDS_SERVICE_ROUTINE_CONTROL_CALLBACK(UI_8 routine_ctrl_type, UI_8 id_high, UI_8 id_low, UI_8 *routine_entry_option, UI_16 size);
#endif

#ifdef UDS_SERVICE_COMMUNICATION_CONTROL
#ifndef ISO15765_3_GEELY                        
void UDS_COMMUNICATION_CONTROL_CALLBACK(UI_8 ctrl_type, UI_8 com_type);
#else
void UDS_COMMUNICATION_CONTROL_CALLBACK(UI_8 ctrl_type, UI_8 com_type, UI_8 size);
#endif
#endif

#ifdef UDS_REQUEST_DOWNLOAD_DEVICE_READY
BOOL UDS_REQUEST_DOWNLOAD_DEVICE_READY(UI_8 dev);
#endif

#ifdef UDS_REQUEST_DOWNLOAD_ERROR_CALLBACK
UI_8 UDS_REQUEST_DOWNLOAD_ERROR_CALLBACK(UI_8 error_code);
#endif

#ifdef DEVICE_0_REQUEST_DOWNLOAD_CALLBACK
UI_8 DEVICE_0_REQUEST_DOWNLOAD_CALLBACK(t_iso3_addr base_addr, t_iso3_addr last_addr);
#endif    
#ifdef DEVICE_1_REQUEST_DOWNLOAD_CALLBACK
UI_8 DEVICE_1_REQUEST_DOWNLOAD_CALLBACK(t_iso3_addr base_addr, t_iso3_addr last_addr);
#endif
#ifdef DEVICE_2_REQUEST_DOWNLOAD_CALLBACK
UI_8 DEVICE_2_REQUEST_DOWNLOAD_CALLBACK(t_iso3_addr base_addr, t_iso3_addr last_addr);
#endif
#ifdef DEVICE_3_REQUEST_DOWNLOAD_CALLBACK
UI_8 DEVICE_3_REQUEST_DOWNLOAD_CALLBACK(t_iso3_addr base_addr, t_iso3_addr last_addr);
#endif

#ifdef DEVICE_0_TRANSFER_DATA_CALLBACK
UI_8 DEVICE_0_TRANSFER_DATA_CALLBACK(UI_8* pData, UI_16 size);
#endif    
#ifdef DEVICE_1_TRANSFER_DATA_CALLBACK
UI_8 DEVICE_1_TRANSFER_DATA_CALLBACK(UI_8* pData, UI_16 size);
#endif
#ifdef DEVICE_2_TRANSFER_DATA_CALLBACK
UI_8 DEVICE_2_TRANSFER_DATA_CALLBACK(UI_8* pData, UI_16 size);
#endif
#ifdef DEVICE_3_TRANSFER_DATA_CALLBACK
UI_8 DEVICE_3_TRANSFER_DATA_CALLBACK(UI_8* pData, UI_16 size);
#endif

#ifdef UDS_TRANSFER_DATA_ERROR_CALLBACK
UI_8 UDS_TRANSFER_DATA_ERROR_CALLBACK(UI_8 error_code);
#endif

#ifdef DEVICE_0_REQUEST_TRANSFER_EXIT_CALLBACK
UI_8 DEVICE_0_REQUEST_TRANSFER_EXIT_CALLBACK(UI_8* pData, UI_16 size);
#endif    
#ifdef DEVICE_1_REQUEST_TRANSFER_EXIT_CALLBACK
UI_8 DEVICE_1_REQUEST_TRANSFER_EXIT_CALLBACK(UI_8* pData, UI_16 size);
#endif
#ifdef DEVICE_2_REQUEST_TRANSFER_EXIT_CALLBACK
UI_8 DEVICE_2_REQUEST_TRANSFER_EXIT_CALLBACK(UI_8* pData, UI_16 size);
#endif
#ifdef DEVICE_3_REQUEST_TRANSFER_EXIT_CALLBACK
UI_8 DEVICE_3_REQUEST_TRANSFER_EXIT_CALLBACK(UI_8* pData, UI_16 size);
#endif


/*****************************************************************************
 * Security attempts counter section
 *****************************************************************************/

void Iso15765_3SecurityAttemptsIncrement(void);        /*!< Function that increments the attempts counter */
void Iso15765_3SecurityAttemptsDecrement(void);        /*!< Function that decrements the attempts counter, f.i. when penalty time expires */
BOOL Iso15765_3SecurityAttemptsExceeded(void);         /*!< Function to determine if the allowed attempts have been exceeded */
BOOL Iso15765_3SecurityAttemptsReached(void);          /*!< Function to determine if the allowed attempts have been reached (about to be exceeded on next try) */
void Iso15765_3SecurityAttemptsClear(void);            /*!< Function to clear the attempt counter, f.i. when successful attempt is done */
void Iso15765_3SecurityAttemptsInitialize(void);       /*!< Function to initialize the counter structure, loading (if defined) the NVM counter */
UI_8 Iso15765_3SecurityAttemptsGet(void);              /*!< Accessor getter function for the counter */

/****************************************************************************
* Variant section                                                             
*****************************************************************************/

BOOL Iso15765_3VariantConsecutiveSeedIncrAttempt(void); /*!< Function to know if the UDS variant considers consecutive seed requests as failed security attempts */

#endif

