/***************  COPYRIGHT (c) 2007-2011 FICOSA INTERNATIONAL   **************
| Language:       |  MISRA C
| Controller:     |  
| Spec. Document: |  ISO15765_3 ISO14229
|-----------------|------------------------------------------------------------
| Project:        |  Unified Diagnostics Services Layer
| Reference:      |  
|------------------------------------------------------------------------------
| HISTORY OF MODIFICATIONS
|   Date  -  Coder -                   Description
| 25/02/10    AC         File creation
| 21/06/12   DM                           Integrated with TP tag 20120530.
|------------------------------------------------------------------------------
| FILE DESCRIPTION:
|   Implementacio dels serveis de diagnostics segons el standard ISO 15765-3 
******************************************************************************/
#ifndef __UDSSERVERCALLBACKS_H 
#define __UDSSERVERCALLBACKS_H 

/* ------------------------------ Includes --------------------------------- */
#include "Global.h"
#include "Iso15765_3_CFG.h"
#include "ProjectCFG.h"

/* --------------------------- Type Definitions ---------------------------- */

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

/* POSSIBLE START ROUTINE BY LOCAL ID TYPES */
#define START_ROUTINE_CONTROL    ((UI_8)0x00)
#define REQUEST_ROUTINE_CONTROL  ((UI_8)0x01)

/* POSSIBLE START ROUTINE BY LOCAL ID RESPONSE STATUS */
#define ROUTINE_IN_PROGRESS             ((UI_8)1)
#define ROUTINE_COMPLETED_AND_OK        ((UI_8)2)
#define ROUTINE_COMPLETED_AND_NO_OK     ((UI_8)3)
#define ROUTINE_TO_BE_ANSWERED_BY_TELEM ((UI_8)4)

/* Reset types accepted with the EcuReset diagnostic service */
#define RESET_ALL_HW                    ((UI_8)0x01)
#define RESET_KEY_ON_OFF                ((UI_8)0x02)
#define RESET_CONFIGURATION_PARAMETERS  ((UI_8)0x40)
#define RESET_TEST_PARAMETERS           ((UI_8)0x41)
#define RESET_TELEM                     ((UI_8)0x60)
#define RESET_PMC                       ((UI_8)0x61)
#define RESET_RTC                       ((UI_8)0x62)
#define RESET_WIFI                      ((UI_8)0x63)

/* SUPPORTED LOCAL DATA IDENTIFIERS FOR READDATABYLOCALID AND WRITEDATABYLOCAL SERVICES */
#define USER_COLLATION_R_ID                         ((UI_8)0x01)


/* LENGTH OF LOCAL DATA IDENTIFIERS FOR READDATABYID AND WRITEDATABYID SERVICES */
#define USER_COLLATION_R_LEN                        ((UI_8)100)


/* SUPPORTED DATA IDENTIFIERS FOR READDATABYID AND WRITEDATABYID SERVICES */
#define CURRENT_AND_VOLTAGE_STATUS_ID                 ((UI_16)0x0100)
#define TIP_HALL_SENSORS_STATUS_ID                    ((UI_16)0x0101)
#define DUAL_DIE_MELEXIS_ANGLE_STATUS_ID              ((UI_16)0x0102)
#define PARKING_SWITCH_VOLTAGE_STATUS_ID              ((UI_16)0x0103)
#define SHIFT_LOCK_FAIL_STATUS_ID                     ((UI_16)0x0104)



/* LENGTH OF DATA IDENTIFIERS FOR READDATABYID AND WRITEDATABYID SERVICES */
#define CURRENT_AND_VOLTAGE_STATUS_LEN                ((UI_8)34)
#define TIP_HALL_SENSORS_STATUS_LEN                   ((UI_8)8)
#define DUAL_DIE_MELEXIS_ANGLE_STATUS_LEN             ((UI_8)16)
#define PARKING_SWITCH_VOLTAGE_STATUS_LEN             ((UI_8)4)
#define SHIFT_LOCK_FAIL_STATUS_LEN                     ((UI_8)1)



/* SUPPORTED LOCAL IDENTIFIERS FOR INPUTOUTPUTCONTROLBYLOCALID SERVICE */
#define SHIFTLOCK_ACTIVATION_IOCTRL_ID            ((UI_8)0x01)
#define LED_M_ACTIVATION_IOCTRL_ID                ((UI_8)0x02)
#define LED_D_ACTIVATION_IOCTRL_ID                ((UI_8)0x03)
#define LED_N_ACTIVATION_IOCTRL_ID                ((UI_8)0x04)
#define LED_R_ACTIVATION_IOCTRL_ID                ((UI_8)0x05)
#define LED_P_ACTIVATION_IOCTRL_ID                ((UI_8)0x06)
#define BACKLIGHT_ACTIVATION_IOCTRL_ID            ((UI_8)0x07)
#define OVERLIGHT_ACTIVATION_IOCTRL_ID            ((UI_8)0x08)


/* LENGTH OF LOCAL IDENTIFIERS FOR INPUTOUTPUTCONTROLBYLOCALID SERVICE */
#define SHIFTLOCK_IOCTRL_LEN                       ((UI_8)1)
#define LED_M_ACTIVATION_IOCTRL_LEN                ((UI_8)1)
#define LED_D_ACTIVATION_IOCTRL_LEN                ((UI_8)1)
#define LED_N_ACTIVATION_IOCTRL_LEN                ((UI_8)1)
#define LED_R_ACTIVATION_IOCTRL_LEN                ((UI_8)1)
#define LED_P_ACTIVATION_IOCTRL_LEN                ((UI_8)1)
#define BACKLIGHT_ACTIVATION_IOCTRL_LEN            ((UI_8)1)
#define OVERLIGHT_ACTIVATION_IOCTRL_LEN            ((UI_8)1)



/* SUPPORTED LOCAL IDENTIFIERS FOR STARTROUTINEBYLOCALID SERVICE */
#define GLOBAL_CHECK_CONNECTION_SRBLID_ID           ((UI_8)0x02)


/* LENGTH OF LOCAL IDENTIFIERS FOR STARTROUTINEBYLOCALID SERVICE */
#define GLOBAL_CHECK_CONNECTION_SRBLID_LEN          ((UI_8)0)




/*---------------------------  Global variables ---------------------------- */

/* ------------------------- Function prototypes --------------------------- */

/*****************************************************************************
| Portability:  TCU Project
|----------------------------------------------------------------------------
| Description:
|  * Routine that commands all the ReadDataByIdentifier diagnostics and is
|    responsible for giving the final answer to ISO_3 layer.
|---------------------------------------------------------------------------
| Arguments:
|  - id_h: higher byte of the identifier requested
|  - id_h: lower byte of the identifier requested
|---------------------------------------------------------------------------
| Timing:
| Tmax Int Dis:   ? cicles cpu | O(n): CTE 
| Tmax Int En :   ? cicles cpu | O(n): CTE 
| Tmax Total  :   ? cicles cpu | O(n): CTE 
/---------------------------------------------------------------------------*/
void UdsReadDataByIdentifier(UI_8 id_h, UI_8 id_l);


/*****************************************************************************
| Portability:  TCU Project
|----------------------------------------------------------------------------
| Description:
|  * Routine that commands all the WriteDataByIdentifier diagnostics and is
|    responsible for giving the final answer to ISO_3 layer.
|---------------------------------------------------------------------------
| Arguments:
|  - id_h: higher byte of the identifier requested
|  - id_h: lower byte of the identifier requested
|  - data_buffer: data pointer of the received parameters for the write
|  - size: length of the parameters received for the write
|---------------------------------------------------------------------------
| Timing:
| Tmax Int Dis:   ? cicles cpu | O(n): CTE 
| Tmax Int En :   ? cicles cpu | O(n): CTE 
| Tmax Total  :   ? cicles cpu | O(n): CTE 
/---------------------------------------------------------------------------*/
void UdsWriteDataByIdentifier(UI_8 id_h, UI_8 id_l, UI_8 *data_buffer, UI_16 size);


/*****************************************************************************
| Portability:  TCU Project
|----------------------------------------------------------------------------
| Description:
|  * Routine that commands all the RoutineControl diagnostics and is
|    responsible for giving the final answer to ISO_3 layer.
|---------------------------------------------------------------------------
| Arguments:
|  - routine_ctrl_type: type of the routine control requested. Can be a start,
|                       a stop or a request results
|  - id: identifier requested
|  - size: length of the parameters received for the write
|  - routine_entry_option: data pointer of the received parameters for the write
|---------------------------------------------------------------------------
| Timing:
| Tmax Int Dis:   ? cicles cpu | O(n): CTE 
| Tmax Int En :   ? cicles cpu | O(n): CTE 
| Tmax Total  :   ? cicles cpu | O(n): CTE 
/---------------------------------------------------------------------------*/
void StartRoutineByLocalId(UI_8 routine_ctrl_type, UI_8 id, 
                              UI_16 size, UI_8 *routine_entry_option);
                              

/*****************************************************************************
| Portability:  TCU Project
|----------------------------------------------------------------------------
| Description:
|  * Routine that commands UdsStopRoutineByLocalId diagnostic service and is
|    responsible for giving the final answer to ISO_3 layer.
|---------------------------------------------------------------------------
| Arguments:
|  - id: identifier of the DTC requested
|  - size: length of the data received
|  - data: data pointer to the buffer of the requested information
|---------------------------------------------------------------------------
| Timing:
| Tmax Int Dis:   ? cicles cpu | O(n): CTE 
| Tmax Int En :   ? cicles cpu | O(n): CTE 
| Tmax Total  :   ? cicles cpu | O(n): CTE 
/---------------------------------------------------------------------------*/
void StopRoutineByLocalId(UI_8 id, UI_16 size, UI_8 *data);

/*****************************************************************************
| Portability:  
|----------------------------------------------------------------------------
| Description:
|  * Routine that updates Routine status.
|---------------------------------------------------------------------------
| Arguments:
|  - id: identifier requested
|  - status: status of the routine, allowed values:
|                   * ROUTINE_IN_PROGRESS
|                   * ROUTINE_COMPLETED_AND_OK
|                   * ROUTINE_COMPLETED_AND_NO_OK
|                   * ROUTINE_TO_BE_ANSWERED_BY_TELEM

|---------------------------------------------------------------------------
| Timing:
| Tmax Int Dis:   ? cicles cpu | O(n): CTE 
| Tmax Int En :   ? cicles cpu | O(n): CTE 
| Tmax Total  :   ? cicles cpu | O(n): CTE 
/---------------------------------------------------------------------------*/
void StartRoutineUpdateStatusByLocalId(UI_8 id,UI_8 status);


/*****************************************************************************
| Portability:  
|----------------------------------------------------------------------------
| Description:
|  * Routine that commands the ClearDiagnosticInfo diagnostic service and is
|    responsible for giving the final answer to ISO_3 layer.
|---------------------------------------------------------------------------
| Arguments:
|  - dtc_hb: higher byte of the requested clear dignostic type received
|  - dtc_mb: medium byte of the requested clear dignostic type received
|  - dtc_lb: lower byte of the requested clear dignostic type received
|---------------------------------------------------------------------------
| Timing:
| Tmax Int Dis:   ? cicles cpu | O(n): CTE 
| Tmax Int En :   ? cicles cpu | O(n): CTE 
| Tmax Total  :   ? cicles cpu | O(n): CTE 
/---------------------------------------------------------------------------*/
void UdsClearDiagnosticInformation(UI_8 dtc_hb, UI_8 dtc_mb, UI_8 dtc_lb);


/*****************************************************************************
| Portability:  TCU Project
|----------------------------------------------------------------------------
| Description:
|  * Routine that commands the TcuUdsReadMemoryByAddress diagnostic service 
|    and is responsible for giving the final answer to ISO_3 layer.
|---------------------------------------------------------------------------
| Arguments:
|  - mem_size_len: length of the requested size in bytes
|  - mem_addr_len: length of the requested address in bytes
|  - mem_addr: data pointer to the requested initial memory address to be read
|  - mem_size: data pointer to the requested memory size to be read
|---------------------------------------------------------------------------
| Timing:
| Tmax Int Dis:   ? cicles cpu | O(n): CTE 
| Tmax Int En :   ? cicles cpu | O(n): CTE 
| Tmax Total  :   ? cicles cpu | O(n): CTE 
/---------------------------------------------------------------------------*/
void UdsReadMemoryByAddress(UI_8 mem_size_len, UI_8 mem_addr_len, 
                               UI_8 *mem_addr, UI_8 *mem_size);


/*****************************************************************************
| Portability:  TCU Project
|----------------------------------------------------------------------------
| Description:
|  * Routine that commands the TcuUdsWriteMemoryByAddress diagnostic service 
|    and is responsible for giving the final answer to ISO_3 layer.
|---------------------------------------------------------------------------
| Arguments:
|  - addr_data_size: the first four bits cointains the data size length in
|                    bytes and the last four bits contains the address size
|                    length in bytes
|  - data_buffer: data pointer where to find the requested initial write
|                 address, the requested write length and the values to write
|                 in memory
|---------------------------------------------------------------------------
| Timing:
| Tmax Int Dis:   ? cicles cpu | O(n): CTE 
| Tmax Int En :   ? cicles cpu | O(n): CTE 
| Tmax Total  :   ? cicles cpu | O(n): CTE 
/---------------------------------------------------------------------------*/
void UdsWriteMemoryByAddress(UI_8 addr_data_size, UI_8 *data_buffer);


/*****************************************************************************
| Portability:  TCU Project
|----------------------------------------------------------------------------
| Description:
|  * Configured callback routine to be notified about the expiration of the
|    previous non default session. This routine must undo all the special
|    diagnostic features done during the especial session 
|---------------------------------------------------------------------------
| Arguments:
|---------------------------------------------------------------------------
| Timing:
| Tmax Int Dis:   ? cicles cpu | O(n): CTE 
| Tmax Int En :   ? cicles cpu | O(n): CTE 
| Tmax Total  :   ? cicles cpu | O(n): CTE 
/---------------------------------------------------------------------------*/
void UdsReturnToDefaultSession(UI_8 old_session, UI_8 new_session);


/*****************************************************************************
| Portability:  TCU Project
|----------------------------------------------------------------------------
| Description:
|  * Routine that commands the TcuUdsTesterPresent diagnostic service and is 
|    responsible for giving the final answer to ISO_3 layer.
|---------------------------------------------------------------------------
| Arguments:
|  - tp_type: tester present type requested
|---------------------------------------------------------------------------
| Timing:
| Tmax Int Dis:   ? cicles cpu | O(n): CTE 
| Tmax Int En :   ? cicles cpu | O(n): CTE 
| Tmax Total  :   ? cicles cpu | O(n): CTE 
/---------------------------------------------------------------------------*/
void UdsTesterPresent(UI_8 tp_type);


/*****************************************************************************
| Portability:  TCU Project
|----------------------------------------------------------------------------
| Description:
|  * Routine that commands the TcuUdsDiagnosticSessionControl diagnostic 
|    service and is responsible for giving the final answer to ISO_3 layer.
|---------------------------------------------------------------------------
| Arguments:
|  - session: requested new session
|---------------------------------------------------------------------------
| Timing:
| Tmax Int Dis:   ? cicles cpu | O(n): CTE 
| Tmax Int En :   ? cicles cpu | O(n): CTE 
| Tmax Total  :   ? cicles cpu | O(n): CTE 
/---------------------------------------------------------------------------*/
void UdsDiagnosticSessionControl(UI_8 session);


/*****************************************************************************
| Portability:  TCU Project
|----------------------------------------------------------------------------
| Description:
|  * Routine that commands the ReadDataByLocalIdentifier diagnostic 
|    service and is responsible for giving the final answer to ISO_3 layer.
|---------------------------------------------------------------------------
| Arguments:
|  - id: identifier requested to read his data
|---------------------------------------------------------------------------
| Timing:
| Tmax Int Dis:   ? cicles cpu | O(n): CTE 
| Tmax Int En :   ? cicles cpu | O(n): CTE 
| Tmax Total  :   ? cicles cpu | O(n): CTE 
/---------------------------------------------------------------------------*/
void UdsRsaReadDataByLocalId(UI_8 id);


/*****************************************************************************
| Portability:  TCU Project
|----------------------------------------------------------------------------
| Description:
|  * Routine that commands the WriteByLocalIdentifier diagnostic 
|    service and is responsible for giving the final answer to ISO_3 layer.
|---------------------------------------------------------------------------
| Arguments:
|  - id: identifier of the data to be written
|  - data: data pointer to the received parameters to be written
|  - size: length of the data to be written
|---------------------------------------------------------------------------
| Timing:
| Tmax Int Dis:   ? cicles cpu | O(n): CTE 
| Tmax Int En :   ? cicles cpu | O(n): CTE 
| Tmax Total  :   ? cicles cpu | O(n): CTE 
/---------------------------------------------------------------------------*/
void UdsRsaWriteDataByLocalId(UI_8 id, UI_8 *data, UI_16 size);


/*****************************************************************************
| Portability:  TCU Project
|----------------------------------------------------------------------------
| Description:
|  * Routine that commands the EcuReset diagnostic service and is responsible 
|    for giving the final answer to ISO_3 layer before performing the reset
|---------------------------------------------------------------------------
| Arguments:
|  - reset_mode: type of reset requested
|---------------------------------------------------------------------------
| Timing:
| Tmax Int Dis:   ? cicles cpu | O(n): CTE 
| Tmax Int En :   ? cicles cpu | O(n): CTE 
| Tmax Total  :   ? cicles cpu | O(n): CTE 
/---------------------------------------------------------------------------*/
void UdsEcuReset(UI_8 reset_mode);


/*****************************************************************************
| Portability:  TCU Project
|----------------------------------------------------------------------------
| Description:
|  * Routine that commands the InputOutputControlByLocalId diagnostic 
|    service and is responsible for giving the final answer to ISO_3 layer
|---------------------------------------------------------------------------
| Arguments:
|  - id: identifier of the requested input output control
|  - ctr_type: requested control type for the received id
|  - size: length of the data received to control
|  - data: data pointer to the received control data
|---------------------------------------------------------------------------
| Timing:
| Tmax Int Dis:   ? cicles cpu | O(n): CTE 
| Tmax Int En :   ? cicles cpu | O(n): CTE 
| Tmax Total  :   ? cicles cpu | O(n): CTE 
/---------------------------------------------------------------------------*/
void UdsInputOutputControlByIdentifier(UI_16 id, UI_8 ctr_type, UI_16 size, UI_8 *data);


/*****************************************************************************
| Portability:  TCU Project
|----------------------------------------------------------------------------
| Description:
|  * UdServices diagnostic initialitzation task. This routine should only
|    be called one time at the microcontroller initialization
|---------------------------------------------------------------------------
| Arguments:
|---------------------------------------------------------------------------
| Timing:
| Tmax Int Dis:   ? cicles cpu | O(n): CTE 
| Tmax Int En :   ? cicles cpu | O(n): CTE 
| Tmax Total  :   ? cicles cpu | O(n): CTE 
/---------------------------------------------------------------------------*/
void UdsServerCallbacksInit(void);

/*****************************************************************************
| Portability:  TCU Project
|----------------------------------------------------------------------------
| Description:
|  * UdServices diagnostic task executed in every main cycle
|---------------------------------------------------------------------------
| Arguments:
|---------------------------------------------------------------------------
| Timing:
| Tmax Int Dis:   ? cicles cpu | O(n): CTE 
| Tmax Int En :   ? cicles cpu | O(n): CTE 
| Tmax Total  :   ? cicles cpu | O(n): CTE 
/---------------------------------------------------------------------------*/
void UdsServerCallbacksTask(void);

/*************** Extra services example of implementation ******************/

#if (UDS_SERVICE_GEELY_ECHO_STATUS == SERVICE_ENABLED)
/**
 * Extra services dispatcher.  Will be executed in case of unrecognized service
 * @param service current unrecognized service
 * @param subfunction current unrecognized subfunction
 * @param buf_data_rx rx data buffer with the request
 * @param size size of the request
 */
void UdsExtraServices(UI_8 service,UI_8 subfunction,UI_8 *buf_data_rx, UI_8 size);

/**
 * Geely ECHO service.  It returns same data has received from the request
 * @param data buffer data with the request
 * @param size size of the request
 * @return response mode
 */
UI_8 UdsGeelyEcho(UI_8 *data, UI_8 size);
#endif
#endif
