/*
 * Module_CCS.h
 *
 *  Created on: 2020�~3��16��
 *      Author: foluswen
 */

#ifndef MODULE_CCS_H_
#define MODULE_CCS_H_

#include    <sys/types.h>
#include    <sys/stat.h>
#include 	<sys/time.h>
#include 	<sys/timeb.h>
#include 	<sys/ipc.h>
#include 	<sys/shm.h>
#include 	<sys/mman.h>
#include 	<sys/socket.h>
#include 	<netinet/in.h>
#include 	<net/if.h>
#include 	<net/ethernet.h>
#include 	<linux/if_packet.h>

#include 	<unistd.h>
#include 	<stdarg.h>
#include    <stdio.h>
#include    <stdlib.h>
#include    <unistd.h>
#include    <fcntl.h>
#include    <errno.h>
#include 	<errno.h>
#include 	<string.h>
#include	<time.h>
#include	<ctype.h>

#include 	"SystemLogMessage.h"
#include 	"NidNmk.h"
#include	"v2g/api/api.h"
#include	"v2g/transport/v2gtp.h"

//#define TEST_WITH_ETH0

#define SAVE_SYS_LOG_MSG_EVCOMM_SWITCH		ENABLE
#define DEBUG_PRINTF_EVCOMM_SHOW			ENABLE
#define DEBUG_PRINTF_EVCOMM_BRIEF_SHOW		ENABLE
#define DEBUG_PRINTF_EVCOMM_DETAIL_SHOW		DISABLE
#define	DEBUG_PRINTF_EVCOMM_SYSTEM_LOG_SHOW	ENABLE


#define V2GT_MSG_PROTOCOL_DIN70121                  0
#define V2GT_MSG_PROTOCOL_ISO15118_2014             1    //i.e. ISO15118-2 (ISO 1)
#define V2GT_MSG_PROTOCOL_ISO15118_2018             2    //i.e. ISO15118-20 (ISO 2)

#define	QcaInterface						"eth1"
#define SupportBcbToggle
#define	SdpUdpServerPort					15118
#define	SdpTcpServerPort					59438		//49152-65535
#define	IPV6_LINK_LOCAL_PREFIX				0xFE80
#define V2GTP_MSG_RX_BUFFER_SIZE            (64*1024)   //65,536 = 65.5KB
#define V2GTP_MSG_TX_BUFFER_SIZE            (64*1024)   //65,536 = 65.5KB
#define V2GTP_MSG_HEADER_LENGTH             8           //8 Bytes

/***********************************************/
/*********** Ethernet Type ********************/
/**********************************************/
#define EtherType_HomePlug					0x88E1

/***********************************************/
/*** MMTYPE (Management Message Type) **/
/**********************************************/
#define MMTYPE_CM_SET_KEY_REQ					0x6008
#define MMTYPE_CM_SET_KEY_CNF					0x6009
#define MMTYPE_CM_SLAC_PARM_REQ					0x6064
#define MMTYPE_CM_SLAC_PARM_CNF					0x6065
#define MMTYPE_CM_START_ATTEN_CHAR_IND			0x606A
#define MMTYPE_CM_MNBC_SOUND_IND				0x6076
#define MMTYPE_CM_ATTEN_CHAR_IND				0x606E
#define MMTYPE_CM_ATTEN_CHAR_RSP				0x606F
#define MMTYPE_CM_VALIDATE_REQ					0x6078
#define MMTYPE_CM_VALIDATE_CNF					0x6079
#define MMTYPE_CM_SLAC_MATCH_REQ				0x607C
#define MMTYPE_CM_SLAC_MATCH_CNF				0x607D
#define MMTYPE_CM_ATTEN_PROFILE_IND				0x6086
//following are the vendor specific type
#define MMTYPE_VENDOR_VS_HOST_ACTION	 		0xA062		//Embedded Host Action Requested Indication MME
#define MMTYPE_VENDOR_ATTEN_CHAR				0xA14E		//Attenuation Characteristics MME (VS_ATTEN_CHAR)
#define MMTYPE_VENDOR_VS_NW_INFO				0xA038		//Network Info MME
#define MMTYPE_VENDOR_VS_NW_INFO_CNF			0xA039		//Network Info MME
#define MMTYPE_VENDOR_VS_PL_LNK_STATUS       	0xA0B8      //Powerline Link Status /*+++ 20200808, vern, for PLC connection monitoring ---*/
#define MMTYPE_VENDOR_VS_PL_LNK_STATUS_CNF    	0xA0B9      //Powerline Link Status /*+++ 20200808, vern, for PLC connection monitoring ---*/

/***********************************************/
/************** V2GTP Payload Type ************/
/**********************************************/
#define V2GTP_PAYLOAD_TYPE_SDP_REQUEST		0x9000		//SDP request message
#define V2GTP_PAYLOAD_TYPE_SDP_RESPONSE		0x9001		//SDP response message
#define V2GTP_PAYLOAD_TYPE_EXI_MSG			0x8001		//EXI encoded V2G Message
//Payload
#define SDP_PAYLOAD_SECURITY_TLS			0x00		//0x00 = secured with TLS
#define SDP_PAYLOAD_SECURITY_NONE			0x10		//0x10 = No transport layer security
#define SDP_PAYLOAD_TRANS_PROTOCOL_TCP		0x00		//0x00= TCP
#define SDP_PAYLOAD_TRANS_PROTOCOL_UDP		0x10		//0x10 = reserved for UDP

/***********************************************/
/****** Timing and constant values **********/
/*********** [2015] ISO 15118_3 ***************/
/**********************************************/
#define TT_EVSE_match_MNBC                  (600+400)   //unit: 1ms (default: 600ms)
#define C_EV_match_MNBC                     0x0A
#define TT_EVSE_SLAC_init                   50          //unit: sec.
#define TT_match_sequence                   400         //unit: ms
#define TP_match_response                   100         //unit: ms, General performance time for a response to a request
#define TT_match_response                   (200*3)     //unit: ms, Time that the EV/EVSE shall wait for a response from the EVSE/EV
#define TP_EV_batch_msg_interval            (50*3)      //unit: ms
#define TT_EVSE_match_session               10000       //unit: ms
#define TT_match_join                       (12000*2)   //unit: ms

#define V2G_SECC_ReadyToCharge_Performance_Time             148000  //unit: ms (148 sec)
#define V2G_SECC_CommunicationSetup_Performance_Time        18000   //unit: ms (18 sec)
#define V2G_SECC_ChargingParameter_Performance_Time         60000   //unit: ms (60 sec)
//#define V2G_SECC_ChargingParameter_Performance_Time       120000   //unit: ms (120 sec)
#define V2G_SECC_CableCheck_Performance_Time                38000   //unit: ms (38 sec)
//#define V2G_SECC_CableCheck_Performance_Time              60000   //unit: ms (extend to 60 sec)
//#define V2G_SECC_PreCharge_Performance_Time                 5000    //unit: ms (5 sec)
#define V2G_SECC_PreCharge_Performance_Time                 10000   //unit: ms (extend to 10 sec)
#define V2G_SECC_WeldingDetection_Performance_Time          20000   //unit: ms (20 sec)
#define V2G_SECC_CPState_Detection_Timeout                  1500    //unit: ms (1.5 sec)
#define V2G_SECC_CPOscillator_Retain_Timeout                1500    //unit: ms (1.5 sec)

#define V2G_EVCC_Msg_Timeout_SupportedAppProtocolReq        2000    //unit: ms (1.5 sec)
#define V2G_EVCC_Msg_Timeout_SessionSetupReq                2000    //unit: ms (1.5 sec)
#define V2G_EVCC_Msg_Timeout_ServiceDiscoveryReq            2000
#define V2G_EVCC_Msg_Timeout_ServicePaymentSelectionReq     2000
#define V2G_EVCC_Msg_Timeout_ContractAuthenticationReq      2000
#define V2G_EVCC_Msg_Timeout_ChargeParameterDiscoveryReq    2000
#define V2G_EVCC_Msg_Timeout_PowerDeliveryReq               2000
#define V2G_EVCC_Msg_Timeout_CableCheckReq                  2000
#define V2G_EVCC_Msg_Timeout_PreChargeReq                   2000
#define V2G_EVCC_Msg_Timeout_CurrentDemandReq               25
#define V2G_EVCC_Msg_Timeout_WeldingDetectionReq            2000
#define V2G_EVCC_Msg_Timeout_SessionStopReq                 2000
#define V2G_SECC_Sequence_Timeout                           60000   //unit: ms (default: 60000, i.e. 60 sec)
#define V2G_SECC_QCA7000_GET_MAC_ADDR_REQ_RETRY_PERIOD      3000    //unit: ms (default: 3000, i.e. 3 sec)
#define V2G_SECC_QCA7000_SEND_SET_KEY_PERIOD                2000    //unit: ms (default: 2000, i.e. 2 sec)
#define V2G_SECC_QCA7000_COMM_TIMEOUT                       10000   //unit: ms (default: 10000, i.e. 20 sec)
//[To-Do] to be continued. (DIN 70121, Table 75)

#define V2G_SECC_CP_Timeout_CableCheck                      2000    //unit: ms (default: 2 sec)

#define DIN_SPEC_70121_2012_VersionNumberMajor              2
#define DIN_SPEC_70121_2012_VersionNumberMinor              0
#define ISO1_15118_2013_VersionNumberMajor                  2
#define ISO1_15118_2013_VersionNumberMinor                  0
#define ISO2_15118_2018_VersionNumberMajor                  2
#define ISO2_15118_2018_VersionNumberMinor                  0


//Protocol format : Message ID
#define CAN_CMD_ADDRESS_REQUEST                     0x00000100
#define CAN_CMD_ADDRESS_ASSIGN                      0x00000200
#define CAN_CMD_EV_BOARD_STATUS                     0x00000300
#define CAN_CMD_GET_FW_VERSION                      0x00000400
#define CAN_CMD_GET_HW_VERSION                      0x00000500
#define CAN_CMD_CHARGING_PERMISSION                 0x00000600
#define CAN_CMD_EVSE_OUTPUT_STATUS_ANNOUNCEMENT     0x00000700
#define CAN_CMD_EVSE_CAPACITY_ANNOUNCEMENT          0x00000800
#define CAN_CMD_GET_EV_TARGET_INFO                  0x00000900
#define CAN_CMD_GET_EV_BATTERY_INFO                 0x00000A00
#define CAN_CMD_EV_STOP_EVENT                       0x00000B00
#define CAN_CMD_EVSE_STOP_EVENT                     0x00000C00
#define CAN_CMD_GET_MISC_INFO                       0x00000D00
#define CAN_CMD_DOWNLOAD_REQUEST                    0x00000E00
#define CAN_CMD_START_BLOCK_TRANSFER                0x00000F00
#define CAN_CMD_DATA_TRANSFER                       0x00001000
#define CAN_CMD_DOWNLOAD_FINISH                     0x00001100
#define CAN_CMD_ISOLATION_STATUS                    0x00001200
#define CAN_CMD_CCS_CONNECTOR_INFO                  0x00001300
#define CAN_CMD_RTC_INFO                            0x00001400
#define CAN_CMD_EVSE_PRECHARGE_INFO                 0x00001500
#define CAN_CMD_EVCCID_REQUEST                      0x00001600
#define CAN_CMD_PCBA_TEST                           0x0000AA00

/***********************************************/
/*************** Structure *********************/
/**********************************************/
struct MmeHeader
{
    unsigned char ODA[6];           //Original Destination Address
    unsigned char OSA[6];           //Original source Address
    //unsigned int VLAN Tag;        //IEEE802.1Q VLAN Tag (optional)
    unsigned short MTYPE;           //Ethernet Type for HomePlug, should be 0x88E1
    unsigned char MMV;              //Management Message Version
    unsigned short MMTYPE;          //Management Message Type
    unsigned char FMI[2];           //Fragmentation Management Information, Fragmentation Message Sequence Number
    unsigned char MMENTRY[256];     //Management Message Entry Data
} __attribute__((packed));

//Qualcomm Atheros Vendor Specific MME Format
struct QcaVendorMmeHeader
{
    unsigned char ODA[6];           //Original Destination Address
    unsigned char OSA[6];           //Original source Address
    //unsigned int VLAN Tag;        //IEEE802.1Q VLAN Tag (optional)
    unsigned short MTYPE;           //Ethernet Type for HomePlug, should be 0x88E1
    unsigned char MMV;              //Management Message Version
    unsigned short MMTYPE;          //Management Message Type
    unsigned char OUI[3];           //Fragmentation Management Information, Fragmentation Message Sequence Number
    unsigned char MBODY[1024];      //Management Message Entry Data
} __attribute__((packed));

struct V2gtpHeader                          //8 Bytes (see Figure 25 in DIN 70121)
{
    unsigned char ProtocolVersion;          //0x01: V2GTP version 1, 0x00, 0x02-0xFF reserved
    unsigned char InverseProtocolVersion;   //Equals the <Protocol_Version> XOR 0xFF, 0xFE: V2GTP Version 1
    unsigned short PayloadType;             //0x8001:	EXI encoded V2G Message,
                                            //0x9000:	SDP request message,
                                            //0x9001:	SDP response message,
                                            //0xA000 - 0xFFFF:	Manufacturer specific use
    unsigned int PayloadLength;             //excluding the generic V2GTP header byte
} __attribute__((packed));

typedef struct MODULE_INFO
{
	struct timeb	startTime[10];
	uint8_t 	    QCA7K_SetKey_status;
}Module_Info;

int Proc_din_supportedAppProtocolRes(int AcceptFd);
void SHM_Init_din_ChargeParameterDiscoveryRes(struct CcsData *shm_ccs);
void SHM_Init_din_CableCheckRes(struct CcsData *shm_ccs);
void SHM_Init_din_PreChargeRes(struct CcsData *shm_ccs);
void SHM_Init_din_PowerDeliveryRes(struct CcsData *shm_ccs);
void SHM_Init_din_CurrentDemandRes(struct CcsData *shm_ccs);
void SHM_Init_din_WeldingDetectionRes(struct CcsData *shm_ccs);
void SHM_Init_din_SessionStopRes(struct CcsData *shm_ccs);
int OutputCpPwmDuty(unsigned char Duty);
void Check_EVErrorCode(int code);
unsigned char Check_V2G_Flow_Status_pre();
int End_Process();
int SdpUdpConnected();
int V2gTcpConnected();



#endif /* MODULE_CCS_H_ */