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

/* ----------------------------------------------------------------------------
 * Uses
 */
//#include "CommonMacros.h"
//#include "Platform_Types.h"
//#include "ProjectCfg.h"
//#include "Global.h"


#include "UDS_CoreServices_CB.h"
#include "DiagnosticL/UDS/UDS_Services_Common.h"
#include <string.h>
#include "TouchPanel.h"
//#include "TouchPanel_Cfg.h"
#include "Std_Types.h"
#include "device.h"
#include "touch/touch.h"
#include "math.h"
#include "stdlib.h"
#include <stddef.h>                     // Defines NULL
#include <stdbool.h>                    // Defines true
#include <stdlib.h>                     // Defines EXIT_FAILURE
#include "definitions.h"                // SYS function prototypes
#include "OsekCom/OsekCom.h"
#include "Speaker/Speaker.h"
#include "P417_SWTL_App_ert_rtw/P417_SWTL_App.h"
#include "TouchPanel/TouchPanel.h"
#include "forceSnsr/forcedetect.h"
#include "TLE9263/TLE926x_Main.h"
#include "DiagnosticL/Comp_ISO_15765_3/Iso15765_3.h"
#include "ModelsInterfaces/CommonMacros.h"
#include "calib_public.h"
/* ----------------------------------------------------------------------------
 * Private defines
 */

/* ----------------------------------------------------------------------------
 * Private types
 */
//typedef struct {
//    uint16        did;
//    uint8         size;
//    t_UDS_Session allowed_session;
//    t_UDS_ERR     (*Getter)(uint8 * buf_data_rx);
//} t_UDSReadDidSubCommands;
extern UI_8 frm_diag_physresp_swtl[8];

/* ----------------------------------------------------------------------------
 * Private macros
 */
/********************* MACRO for support DID read *****************************/
#define SUPPORT_ID_FOR_22_SERVICE				((uint8)0xDF)
#define SUPPORT_ID_FOR_2E_SERVICE				((uint8)0xEC)
#define SUPPORT_ID_FOR_2F_SERVICE				((uint8)0xED)
#define SUPPORT_ID_FOR_31_SERVICE				((uint8)0xEE)

#define    BAT_MODE_A         ((uint8)0)
#define    BAT_MODE_B  		  ((uint8)1)
#define    BAT_MODE_C         ((uint8)2)

/* ----------------------------------------------------------------------------
 * Forward declarations
 */
 UI_16 g16_DiagTouchPressSenRes[3][15] = {0};
#if 1
//static void SupportIDCheckFor_22(uint8 * resp, uint8 id_l);
//static boolean isSessionALlowed(t_UDS_Session did_session);

static t_UDS_ERR Get_DID_GAC_Active_Diagnostic_session(uint8 *data);

t_UDS_ERR Get_DID_Fun1(uint8 *data)
{
    data[0] = Calib_GetData(data0)>>8;
    data[1] = Calib_GetData(data0);
    return ISO15765_3_POSITIVE_RESPONSE;
}
t_UDS_ERR Get_DID_Fun2(uint8 *data)
{
    data[0] = Calib_GetData(data1)>>8;
    data[1] = Calib_GetData(data1);
    return ISO15765_3_POSITIVE_RESPONSE;
}
t_UDS_ERR Get_DID_Fun3(uint8 *data)
{
    data[0] = Calib_GetData(data2)>>8;
    data[1] = Calib_GetData(data2);    
    return ISO15765_3_POSITIVE_RESPONSE;
}
t_UDS_ERR Get_DID_UP(uint8 *data)
{
    data[0] = Calib_GetData(data3)>>8;
    data[1] = Calib_GetData(data3);    
    return ISO15765_3_POSITIVE_RESPONSE;
}
t_UDS_ERR Get_DID_DOWN(uint8 *data)
{
    data[0] = Calib_GetData(data4)>>8;
    data[1] = Calib_GetData(data4);    
    return ISO15765_3_POSITIVE_RESPONSE;
}
t_UDS_ERR Get_DID_LEFT(uint8 *data)
{
    data[0] = Calib_GetData(data5)>>8;
    data[1] = Calib_GetData(data5);    
    return ISO15765_3_POSITIVE_RESPONSE;
}
t_UDS_ERR Get_DID_RIGHT(uint8 *data)
{
    data[0] = Calib_GetData(data6)>>8;
    data[1] = Calib_GetData(data6);    
    return ISO15765_3_POSITIVE_RESPONSE;
}
t_UDS_ERR Get_DID_OK(uint8 *data)
{
    data[0] = Calib_GetData(data7)>>8;
    data[1] = Calib_GetData(data7);    
    return ISO15765_3_POSITIVE_RESPONSE;
}

/******************************************************************************
 *
 * DIDs
 *
 */
/** Dynamic **/


/** Company **/

/* ----------------------------------------------------------------------------
 * Private variables
 */

void * memcpy( void * dest, void const * src, size_t len)
{
    uint8_t* dst8 = (uint8_t*)dest;
    uint8_t* src8 = (uint8_t*)src;

    while (len--) {
        *dst8++ = *src8++;
    }
    return dest;
}


t_UDSReadDidSubCommands uds_did_read_commands[] =
{
  /** ECU Identification ***********************************************************************************/
  { DID_CD_SYSID_ACTIVE_DIAG_SESSION,     1, UDS_DEFAULT_SESSION, Get_DID_GAC_Active_Diagnostic_session    },
  { DID_CD_SYSID_VIN,                    17, UDS_DEFAULT_SESSION, Get_DID_CD_SYSID_VIN        		       },
  { DID_CD_SYSID_REPROGRAMING_DATE ,      4, UDS_DEFAULT_SESSION, Get_DID_Reprogramming_Date               },
  { DID_CD_SYSID_SYSTEMSUPPLIER_ID,      6 , UDS_DEFAULT_SESSION, Get_DID_System_Supplier_Id               },
  { DID_CD_SYSID_SPARE_PART_NUMBER,      21, UDS_DEFAULT_SESSION, Get_DID_Gac_Spare_Number                 },
  { DID_CD_SYSID_REPAIR_SHOP_CODE,                    16, UDS_DEFAULT_SESSION, Get_DID_GAC_Repair_Shop_Id        		       },
  { DID_CD_SYSID_ECU_HW_VER_NUMBER,       16, UDS_DEFAULT_SESSION, Get_DID_Gac_Hw_Version                   },
  { DID_CD_SYSID_ECU_SW_VER_NUMBER , 	 16, UDS_DEFAULT_SESSION, Get_DID_Gac_Ecusw_Version                 },
  /*Application*/
  { DID_TOUCH_SEN1_BASE_LINE , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen1_BaseLine                },
  { DID_TOUCH_SEN2_BASE_LINE , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen2_BaseLine                },
  { DID_TOUCH_SEN3_BASE_LINE , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen3_BaseLine                },
  { DID_TOUCH_SEN4_BASE_LINE , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen4_BaseLine                },
  { DID_TOUCH_SEN5_BASE_LINE , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen5_BaseLine                },
  { DID_TOUCH_SEN6_BASE_LINE , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen6_BaseLine                },
  { DID_TOUCH_SEN7_BASE_LINE , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen7_BaseLine                },
  { DID_TOUCH_SEN8_BASE_LINE , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen8_BaseLine                },
  { DID_TOUCH_SEN9_BASE_LINE , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen9_BaseLine                },
  { DID_TOUCH_SEN10_BASE_LINE , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen10_BaseLine                },
  { DID_TOUCH_SEN11_BASE_LINE , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen11_BaseLine                },
  { DID_TOUCH_SEN12_BASE_LINE , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen12_BaseLine                },
  { DID_TOUCH_SEN13_BASE_LINE , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen13_BaseLine                },

   { DID_TOUCH_SEN1_SIGNAL , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen1_Signal                },
   { DID_TOUCH_SEN2_SIGNAL , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen2_Signal                },
   { DID_TOUCH_SEN3_SIGNAL , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen3_Signal                },
   { DID_TOUCH_SEN4_SIGNAL , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen4_Signal                },
   { DID_TOUCH_SEN5_SIGNAL , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen5_Signal                },
   { DID_TOUCH_SEN6_SIGNAL , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen6_Signal                },
   { DID_TOUCH_SEN7_SIGNAL , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen7_Signal                },
   { DID_TOUCH_SEN8_SIGNAL , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen8_Signal                },
   { DID_TOUCH_SEN9_SIGNAL , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen9_Signal                },
   { DID_TOUCH_SEN10_SIGNAL , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen10_Signal                },
   { DID_TOUCH_SEN11_SIGNAL , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen11_Signal                },
   { DID_TOUCH_SEN12_SIGNAL , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen12_Signal                },
   { DID_TOUCH_SEN13_SIGNAL , 	 2, UDS_DEFAULT_SESSION, Get_DID_TouchSen13_Signal                },

   { DID_PRESEE_SEN_BASELINE , 	 2, UDS_DEFAULT_SESSION, Get_DID_PressSen_Baseline                },
   { DID_PRESEE_SEN_SIGNAL , 	 2, UDS_DEFAULT_SESSION, Get_DID_PressSen_Signal                },
   { DID_PRESEE_SEN_PRE_VAL , 	 2, UDS_DEFAULT_SESSION, Get_DID_Pre_Value                },
	
   
   {DID_FUN1, 2, UDS_DEFAULT_SESSION, Get_DID_Fun1},
   {DID_FUN2, 2, UDS_DEFAULT_SESSION, Get_DID_Fun2},  
   {DID_FUN3, 2, UDS_DEFAULT_SESSION, Get_DID_Fun3},
   {DID_UP, 2, UDS_DEFAULT_SESSION, Get_DID_UP},
   {DID_DOWN, 2, UDS_DEFAULT_SESSION, Get_DID_DOWN},
   {DID_LEFT, 2, UDS_DEFAULT_SESSION, Get_DID_LEFT},
   {DID_RIGHT, 2, UDS_DEFAULT_SESSION, Get_DID_RIGHT},
   {DID_OK, 2, UDS_DEFAULT_SESSION, Get_DID_OK},   
};



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

/* ----------------------------------------------------------------------------
 * Public functions
 */
//uint8 UDS_ReadDiD_CB(uint8 id_h, uint8 id_l)
//{
//    uint16   requested_id;
//    uint16   resp_pos;
//    tp_uds_read_data_by_identifier_resp resp;
//    t_UDS_ERR response_mode = UDS_ERR_REQUEST_OUT_OF_RANGE;
//    boolean  id_data_read;
//    uint8 *buf_data_rx = NULL;
//    uint16 did_index = 0;
//    static uint16 last_resp_pos = 0;
//
//    resp_pos = 0;//( - (UI_16) 1);
//    id_data_read = FALSE;
//    buf_data_rx = &tp_frm[DIAG_ISO15765_2_HDL].data_rx[0];
//
//    for(did_index = 1;did_index< 4096;did_index+=2)
//    {
//    	if((buf_data_rx[did_index]==id_h)&&(buf_data_rx[did_index+1] == id_l))
//    	{
//    		if(did_index != 1)
//    		{
//    			resp_pos = last_resp_pos;
//    		}else{
//    			last_resp_pos = 0;
//    		}
//    		break;
//    	}
//    }
//    resp = ISO15765_3_GET_RESP_DATA(tp_uds_read_data_by_identifier_resp);
//    /* Send correct Ids */
//    resp->buffer_dades[resp_pos + 0] = id_h;
//    resp->buffer_dades[resp_pos + 1] = id_l;
//
//    resp_pos = resp_pos + 2;
//
//    /* Support DID read check
//     * DID(MSB)   Service
//     *   $DF        $22
//     *   $EC        $2E
//     *   $ED        $2F
//     *   $EE        $31      */
//    switch (id_h) {
//      case SUPPORT_ID_FOR_22_SERVICE:
//    	  SupportIDCheckFor_22((t_UDS_ERR*)&resp->buffer_dades[resp_pos], id_l);
//    	  Iso15765_3IncrementResponseSize((UI_16) 32);
//    	  response_mode = ISO15765_3_POSITIVE_RESPONSE;
//    	break;
//      case SUPPORT_ID_FOR_2E_SERVICE:
//    	  SupportIDCheckFor_2E((t_UDS_ERR*)&resp->buffer_dades[resp_pos], id_l);
//    	  Iso15765_3IncrementResponseSize((UI_16) 32);
//    	  response_mode = ISO15765_3_POSITIVE_RESPONSE;
//    	break;
//      case SUPPORT_ID_FOR_2F_SERVICE:
//    	//  SupportIDCheckFor_2F((t_UDS_ERR*)&resp->buffer_dades[resp_pos], id_l);
//    	  Iso15765_3IncrementResponseSize((UI_16) 32);
//    	  response_mode = ISO15765_3_POSITIVE_RESPONSE;
//    	break;
//      case SUPPORT_ID_FOR_31_SERVICE:
//    	 // SupportIDCheckFor_31((t_UDS_ERR*)&resp->buffer_dades[resp_pos], id_l);
//    	  Iso15765_3IncrementResponseSize((UI_16) 32);
//    	  response_mode = ISO15765_3_POSITIVE_RESPONSE;
//    	break;
//      default:
//    	  id_data_read = TRUE;
//    	  break;
//    }
//    if (id_data_read == TRUE) {
//    	/* Get the complete id */
//    	requested_id = ((uint16) id_h) << 8;
//    	requested_id = requested_id | (uint16) id_l;
//
//    	for (uint8 idx = 0; idx < NOOF(uds_did_read_commands); idx++) {
//
//    		if (requested_id == uds_did_read_commands[idx].did) {
//
//    			if (TRUE == isSessionALlowed(uds_did_read_commands[idx].allowed_session)) {
//
//    				(void)uds_did_read_commands[idx].Getter((t_UDS_ERR*)&resp->buffer_dades[resp_pos]);
//    				Iso15765_3IncrementResponseSize(uds_did_read_commands[idx].size);
//    				/* assuming no errors reading */
//    				response_mode = ISO15765_3_POSITIVE_RESPONSE;
//    				last_resp_pos += uds_did_read_commands[idx].size + 2;
//
//    			}
//    			else {
//    				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) {
//		Iso15765_3IncrementResponseSize((uint8) 2);
//	} else {
//
//		/* Do nothing */
//	}
//
//	/* Send the response to ISO_3 layer */
//	return (uint8)(response_mode);
//}

/* ----------------------------------------------------------------------------
 * Private functions
 */
//static void SupportIDCheckFor_22(uint8 * resp, uint8 id_l)
//{
//	uint8 idmsb;
//	uint8 idlsb;
//	uint8 bytepos;
//	uint8 bitpos;
//
//	memset(resp, (uint8) 0, (uint8) 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_read_commands) ; idx++) {
//    		idmsb   = uds_did_read_commands[idx].did >> (uint8) 8;
//    		bytepos = idmsb / (uint8) 8;
//    		bitpos  = idmsb % (uint8) 8;
//    		resp[bytepos] |= ((uint8) 1 << (7 - bitpos));
//    	}
//    } else {
//    	/* DID LSM Support Check for Assigned MSB */
//    	idmsb = id_l;
//    	for (uint8 idx = 0; idx < NOOF(uds_did_read_commands) ; idx++) {
//    		if (idmsb == uds_did_read_commands[idx].did >> (uint8) 8) {
//    			idlsb = ((uint8) uds_did_read_commands[idx].did);
//        		bytepos = idlsb / (uint8) 8;
//        		bitpos  = idlsb % (uint8) 8;
//        		resp[bytepos] |= ((uint8) 1 << (7 - bitpos));
//    		}
//    	}
//    }
//}
/* ----------------------------------------------------------------------------
 * Private functions
 */
//static boolean isSessionALlowed(t_UDS_Session did_session)
//{
//    return TRUE;
//}
/* ----------------------------------------------------------------------------
 * Private functions
 */

static t_UDS_ERR Get_DID_GAC_Active_Diagnostic_session(uint8 *data)
{
	data[0] = Iso15765_3QueryActiveSession();
    return ISO15765_3_POSITIVE_RESPONSE;
}

/* ----------------------------------------------------------------------------
 * PUBLIC functions
 */



uint8 GetReadDidServiceNumber(void)
{
    return NOOF(uds_did_read_commands);
}

#endif
