/* ----------------------------------------------------------------------------
 * Implements
 */
//#include "UDS_Services_Common.h"

/* ----------------------------------------------------------------------------
 * Uses
 */
#include "ModelsInterfaces/CommonMacros.h"
#include "DiagnosticR/ProjectCfg.h"
#include "Std_Types.h"
#include "DiagnosticR/Comp_ISO_15765_3/Iso15765_3.h"
#include "UDS_CoreServices_CB.h"
//#include "SafetyHwAbsDIO.h"
//#include "PaddleStatus_If.h"
#include "DiagnosticR/Comp_ISO_15765_3/Iso15765_3_Task.h"
#include "DiagnosticR/UDS/UDS_Services_Common.h"
#include "calib_public.h"
#include "smartee.h"
#include "Speaker.h"
/* ----------------------------------------------------------------------------
 * Private defines
 */
#define NO_MESSAGE 0xFFFF

/* ----------------------------------------------------------------------------
 * Private types
 */
//typedef struct {
//    uint16        did;
//    uint8         size;
//    t_UDS_Session allowed_session;
//   // t_UDS_ERR       (*Setter)(const uint8 * buf_data_rx);
//    t_UDS_ERR       (*Setter)(uint8 * buf_data_rx);
//
//} t_UDSWriteDiDSubCommandsReference;

/* ----------------------------------------------------------------------------
 * Private macros
 */
uint8* p_dtc_setting = NULL;
/* ----------------------------------------------------------------------------
 * Forward declarations
 */
//static boolean isSessionAllowed(t_UDS_Session did_session);

//static t_UDS_ERR Set_DID_GAC_Manufactory_Mode(uint8 *data);

t_UDS_ERR CalibFun1(uint8 *data)
{
    uint16 value = data[0]<<8 | data[1];
    Calib_Trigger(data0, value);
    
    return TRUE;
}
t_UDS_ERR CalibFun2(uint8 *data)
{
    uint16 value = data[0]<<8 | data[1];
    Calib_Trigger(data1, value);
    return TRUE;
}
t_UDS_ERR CalibFun3(uint8 *data)
{
    uint16 value = data[0]<<8 | data[1];    
    Calib_Trigger(data2, value);
    return TRUE;
}
t_UDS_ERR CalibUp(uint8 *data)
{
    uint16 value = data[0]<<8 | data[1];    
    Calib_Trigger(data3, value);
    return TRUE;
}
t_UDS_ERR CalibDown(uint8 *data)
{
    uint16 value = data[0]<<8 | data[1];    
    Calib_Trigger(data4, value);
    return TRUE;
}
t_UDS_ERR CalibLeft(uint8 *data)
{
    uint16 value = data[0]<<8 | data[1];    
    Calib_Trigger(data5, value);
    return TRUE;
}
t_UDS_ERR CalibRight(uint8 *data)
{
    uint16 value = data[0]<<8 | data[1];    
    Calib_Trigger(data6, value);
    return TRUE;
}
t_UDS_ERR CalibOk(uint8 *data)
{
    uint16 value = data[0]<<8 | data[1];    
    Calib_Trigger(data7, value);
    return TRUE;
}

extern uint8 UDS_Vibration_Gain;
t_UDS_ERR Trigger_Vibration(uint8 *data)
{
    speeker_Tig_Once(49, UDS_Vibration_Gain);
    return TRUE;
}
extern uint8 UDS_DAC_Ref_Voltage;
t_UDS_ERR DAC_Ref_Voltage(uint8 *data)          
{
    t_UDS_ERR rtn = FALSE;
    
    if(*data < 8)
    {
        UDS_DAC_Ref_Voltage = *data;
        SmartEE_Write(0x781, data, 1);
		SUPC_REGS->SUPC_VREF = SUPC_VREF_SEL(UDS_DAC_Ref_Voltage);
        rtn = TRUE;
    }   
    return rtn;   
}

t_UDS_ERR Vibration_Gain(uint8 *data)                     
{
    t_UDS_ERR rtn = FALSE;
    if(*data < 4)
    {
        UDS_Vibration_Gain = *data;
        SmartEE_Write(0x780, data, 1);
        rtn = TRUE;
    }   
    return rtn;
}
extern uint8 UDS_DAC_Timer_Period;
t_UDS_ERR DAC_Timer_Period(uint8 *data)
{
    t_UDS_ERR rtn = FALSE;
    if(*data <= 255 && *data >= 40)
    {
        UDS_DAC_Timer_Period = *data;
        //TC0_REGS->COUNT16.TC_CC[0U] = UDS_DAC_Timer_Period;
        SmartEE_Write(0x782, data, 1);
        rtn = TRUE;
    }   
    return rtn;
}

t_UDS_ERR Measure_Frame_Switch(uint8 *data)               
{
    t_UDS_ERR rtn = FALSE;
    if(*data == 1 || *data == 0)
    {
        
        SmartEE_Write(0x783, data, 1);
        rtn = TRUE;
    }   
    return rtn;
     
}


/* ----------------------------------------------------------------------------
 * Private variables 
 */
t_UDSWriteDiDSubCommandsReference uds_did_write_commands_by_reference[] =
{
	{ DID_CD_SYSID_VIN,                     17, UDS_EXT_DIAG_SESSION, 1,Set_DID_CD_VIN                    },
	{ DID_CD_SYSID_REPROGRAMING_DATE,        4, UDS_EXT_DIAG_SESSION, 1,Set_DID_Reprogramming_Date      	},
	{ DID_CD_SYSID_REPAIR_SHOP_CODE,                     16, UDS_EXT_DIAG_SESSION, 1,Set_DID_Repair_Shop_Code                    },
    {DID_FUN1, 2, UDS_EXT_DIAG_SESSION, 1, CalibFun1},
    {DID_FUN2, 2, UDS_EXT_DIAG_SESSION, 1, CalibFun2},
    {DID_FUN3, 2, UDS_EXT_DIAG_SESSION, 1, CalibFun3},
    {DID_UP, 2, UDS_EXT_DIAG_SESSION, 1, CalibUp},
    {DID_DOWN, 2, UDS_EXT_DIAG_SESSION, 1, CalibDown},
    {DID_LEFT, 2, UDS_EXT_DIAG_SESSION, 1, CalibLeft},
    {DID_RIGHT, 2, UDS_EXT_DIAG_SESSION, 1, CalibRight},
    {DID_OK, 2, UDS_EXT_DIAG_SESSION, 1, CalibOk},
    {DID_Trigger_Vibration, 1, UDS_EXT_DIAG_SESSION, 1, Trigger_Vibration},
    {DID_DAC_Ref_Voltage, 1, UDS_EXT_DIAG_SESSION, 1, DAC_Ref_Voltage},
    {DID_Vibration_Gain, 1, UDS_EXT_DIAG_SESSION, 1, Vibration_Gain},
    {DID_DAC_Timer_Period, 1, UDS_EXT_DIAG_SESSION, 1, DAC_Timer_Period},
    {DID_Measure_Frame_Switch, 1, UDS_EXT_DIAG_SESSION, 1, Measure_Frame_Switch},
};

/* ----------------------------------------------------------------------------
 * Public variables
 */

/* ----------------------------------------------------------------------------
 * Public functions
 */
//uint8 UDS_WriteDiD_CB(uint8 id_h, uint8 id_l, uint8 *buf_data_rx, uint16 size)
//{
//    uint16   requested_id;
//    tp_uds_write_data_by_identifier resp;
//    t_UDS_ERR response_mode = UDS_ERR_REQUEST_OUT_OF_RANGE;
//
//    resp = ISO15765_3_GET_RESP_DATA(tp_uds_write_data_by_identifier);
//
//    /* Get the complete id */
//    requested_id = ((((uint16) (id_h)) << 8) | ((uint16) (id_l)));
//
//    for (uint8 idx = 0; idx < NOOF(uds_did_write_commands_by_reference); idx++) {
//        if (requested_id == uds_did_write_commands_by_reference[idx].did) {
//
//            if (TRUE == isSessionAllowed(uds_did_write_commands_by_reference[idx].allowed_session)) {
//
//                if (NULL != uds_did_write_commands_by_reference[idx].Setter) {
//
//                	if(size == uds_did_write_commands_by_reference[idx].size) {
//
//						//if(UdsSecurityTaskQueryAccessStatus() != UDS_ECU_LOCKED) {
//
//							if (TRUE == uds_did_write_commands_by_reference[idx].Setter(buf_data_rx)) {
//								response_mode = ISO15765_3_POSITIVE_RESPONSE;
//							}
//							else {
//								/** Callback returned error **/
//								response_mode = UDS_ERR_CONDITIONS_NOT_CORRECT;
//							}
//                		//}
//				//else
//				//{
//                			/* Security is not correct*/
//                			//response_mode = UDS_ERR_SECURITY_ACCESS_DENIED;
//                		//}
//                	}
//                	else {
//                		/** Wrong size **/
//                		response_mode = UDS_ERR_INVALID_FORMAT;
//                	}
//
//                }
//                else {
//                	/** Callback not implemented **/
//                	response_mode = UDS_ERR_SUBFUNCTION_NOT_SUPPORTED;
//                }
//
//            }
//            else {
//            	/** Wrong Session **/
//                response_mode = UDS_ERR_SERVICE_NOT_SUPORTED_IN_ACTIVE_SESSION;
//            }
//            break;
//        }
//    }
//
//    /* Finally in case of positive response we set the identifier in the response */
//    if (response_mode == ISO15765_3_POSITIVE_RESPONSE) {
//        resp->id_high = id_h;
//        resp->id_low = id_l;
//        Iso15765_3IncrementResponseSize((UI_8) 2);
//
//    } else {
//
//        /* Do nothing */
//    }
//
//    /* Send the response to ISO_3 layer */
//    Iso15765_3SendResponse(response_mode);
//
//    return response_mode;
//}

/* ----------------------------------------------------------------------------
 * Private functions
 */
//static boolean isSessionAllowed(t_UDS_Session did_session)
//{
//	boolean result = FALSE;
//
//	if (did_session == Iso15765_3QueryActiveSession()) {
//		result = TRUE;
//	}
//	else{
//		result = FALSE;
//	}
//
//    return result;
//}

//#include "ASILB_Common_Lib.h"
//#include "ReactionManager.h"
//#include "SafetyExceptions.h"

//void SupportIDCheckFor_2E(uint8 * resp, uint8 id_l)
//{
//	uint8 idmsb;
//	uint8 idlsb;
//	uint8 bytepos;
//	uint8 bitpos;
//
//	memset(resp, (UI_8) 0, (UI_8) 32);
//
//	if ((id_l == 0xDF) || (id_l == 0xEC) || (id_l == 0xED) || (id_l == 0xEE)) {
//		/* DID MSB Support Check */
//		for (uint8 idx = 0; idx < NOOF(uds_did_write_commands_by_reference) ; idx++) {
//			idmsb   = uds_did_write_commands_by_reference[idx].did >> (UI_8) 8;
//			bytepos = idmsb / (UI_8) 8;
//			bitpos  = idmsb % (UI_8) 8;
//			resp[bytepos] |= ((UI_8) 1 << (7 - bitpos));
//		}
//	} else {
//		/* DID LSM Support Check for Assigned MSB */
//		idmsb = id_l;
//		for (uint8 idx = 0; idx < NOOF(uds_did_write_commands_by_reference) ; idx++) {
//			if (idmsb == uds_did_write_commands_by_reference[idx].did >> (UI_8) 8) {
//				idlsb = ((UI_8) uds_did_write_commands_by_reference[idx].did);
//				bytepos = idlsb / (UI_8) 8;
//				bitpos  = idlsb % (UI_8) 8;
//				resp[bytepos] |= ((UI_8) 1 << (7 - bitpos));
//			}
//		}
//	}
//}

//static void onWriteFinished(BOOL ret)
//{
	/* Do Nothing */
//}


uint8 GetWriteDidServiceNumber(void)
{
    return NOOF(uds_did_write_commands_by_reference);
}
