/*
 * @file hv_comm_database.h
 * @brief Header file of database.
 *
 * @verbatim
 * ==============================================================================
 *                        ##### How to use #####
 * ==============================================================================
 * (+) 
 *
 * @author HiView SoC Software Team
 * @version 1.0.0
 * @date 2022-08-18
 */

#ifndef __HV_DATA_BASE_H__
#define __HV_DATA_BASE_H__

#include "hv_comm_DataType.h"
#include <string.h>
#include "hv_comm_Log.h"
#include "hv_comm_Define.h"
#include "hv_comm_FlashConfig.h"
#include "ProjectConfig.h"
#include "hv_drv_Eeprom.h"
#include "hv_app_Function.h"

#define DATABASE_VALID_MAGIC_NUMBER  0x48547315  /*"HT7315" ASCII magic code for check Database valid*/

#define GAMMA_END_MAGIC_NUMBER      0x73154849
#define GAMMA_END_MAGIC_INDEX       (OFFSET_OF(GammaDataDBRegion, uiEndMagicNumber))
/*unit millisecond*/
#define DATABASE_PERIOD_SYNC_GLOBAL_TO_FLASH_TIMER_LENGTH      5000

#define ROCK_MODE   0
#define POP_MODE    1
#define LIVE_MODE   2
#define DANCE_MODE  3
#define CLUB_MODE   4
#define POWER_MODE  5
#define MODE_MAX    6

#define EDID_DESC_SN_LENTH  13
#define PRODUCT_SN_LENTH    25

#define LPF0        0
#define HPF0        1
#define BPF0        2
#define BPF1        3
#define BPF2        4
#define BPF3        5
#define BPF4        6

#define EDID_DB_MAX_NUMBER    (7)
#define HDMI_EDID_MAX_NUMBER  EDID_DB_MAX_NUMBER
#define DP_EDID_MAX_NUMBER    EDID_DB_MAX_NUMBER

#ifndef OFFSET_OF
 #define OFFSET_OF(type, member) \
	((UINT32)(&((type *)0)->member))
#endif

typedef struct _AudioLpfData
{
    INT32   iFCut;
    INT32   iGain;
    INT32   iA1Hex;
    INT32   iA2Hex;
    INT32   iHHex;
}AudioLpfData;

typedef struct _AudioHpfData
{
    INT32   iFCut;
    INT32   iGain;
    INT32   iA1Hex;
    INT32   iA2Hex;
    INT32   iHHex;
}AudioHpfData;

typedef struct _AudioBpfData
{
    INT32   iCenter;
    INT32   iBw;
    INT32   iGain;
    INT32   iA1Hex;
    INT32   iA2Hex;
    INT32   iHHex;
}AudioBpfData;

typedef struct _AudioEqData
{
    AudioLpfData   stAudioEqLpf0;
    AudioHpfData   stAudioEqHpf0;
    AudioBpfData   stAudioEqBpf0;
    AudioBpfData   stAudioEqBpf1;
    AudioBpfData   stAudioEqBpf2;
    AudioBpfData   stAudioEqBpf3;
    AudioBpfData   stAudioEqBpf4;
}AudioEqData;

typedef struct _SubWindowBorderCfg
{
    UCHAR8      ucPxpBorderEnable;
    UCHAR8      ucSubWindowBorderWidth;
    UCHAR8      ucSubWindowBorderColorR;
    UCHAR8      ucSubWindowBorderColorG;
    UCHAR8      ucSubWindowBorderColorB;
}SubWindowBorderCfg;

typedef struct _DpuClockParam
{
    UINT32 uiFrcFbdivInt;
    UINT32 uiFrcFbdivFrac;
    UINT32 uiFrcPostdiv;
    UINT32 uiVrrFbdivInt;
    UINT32 uiVrrFbdivFrac;
    UINT32 uiVrrPostdiv;
} DpuClockParam;

/* Userdata database struct define */
typedef struct _UserDataDBRegion
{
    USHORT16 usCheckCrc;
    USHORT16 usStructSize;
    UINT32 uiMagicNumber; /*code for check Database valid*/
    USHORT16 usVersion;
    USHORT16 usRWVersion;
    DisplayModeParam stMode[USERDATA_DISPLAY_MODE_MAX + 1];
    UCHAR8 ucDisplayMode;
    UCHAR8 ucPoweroffHours;

    UCHAR8 ucBrightness;
    UCHAR8 ucContrast;
    UCHAR8 ucSaturation;
    UCHAR8 ucSharpness;
#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_DARK_STABILIZER)
    UCHAR8 ucDarkStabilizer;
#endif
    UCHAR8 ucGameMode;
    UCHAR8 ucColorMode;
    UCHAR8 ucColorTemperature;
    UCHAR8 ucColorSpace;
    UCHAR8 ucRed;
    UCHAR8 ucGreen;
    UCHAR8 ucBlue;
    UCHAR8 ucGamma;
    UCHAR8 ucLowBlueLight;
#if (HV_PROJECT_CONFIG_COLOR_FILTER == HV_CONFIG_ON)
    UCHAR8 ucColorFilter;
#endif
    UCHAR8 ucVideoSource;
    UCHAR8 ucHdmi0Format;
    UCHAR8 ucHdmi1Format;
    BOOL   bHdmiAllmEn;
    BOOL   bQms;

    UCHAR8 ucProportion;
    UCHAR8 ucResponseTime;
#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_MPRT)
    UCHAR8 ucMprt;
#endif

#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_FREESYNC)
    UCHAR8 ucFreeSync;
#endif

#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_ADAPTIVESYNC)
    UCHAR8 ucAdaptiveSync;
#endif

#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_GSYNC)
    UCHAR8 ucGSync;
#endif

#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_LDR)
    UCHAR8 ucLdr;
    UCHAR8 ucLdrRgb;
#endif

#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_DCR)
    UCHAR8 ucDcr;
#endif

#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_EYESHIELD)
    UCHAR8 ucEyeshield;
#endif

#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_CROSSHAIR)
    UCHAR8 ucOsdCrosshair;
    UCHAR8 ucDynCrosshair;
#endif

#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_REFRESH_RATE)
    UCHAR8 ucOsdFrameCounter;
#endif
    UCHAR8 ucOsdTimerCounter;

    UCHAR8 ucOsdLanguage;
    UCHAR8 ucOsdMenuHPos;
    UCHAR8 ucOsdMenuVPos;
    UCHAR8 ucOsdTime;
    UCHAR8 ucOsdTrans;
    BOOL ucOsdLock;

    UCHAR8 ucDPVersion;
    UCHAR8 ucDP1Version;
    UCHAR8 ucHDMIVersion;

#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_HDR)
    HDRSwitch enHdrSwitch;
    HDRSwitch enHdrSwitchBackup;
#endif

#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_CUSTOM_KEY)
    UCHAR8 ucOsdCustomKey1;
    UCHAR8 ucOsdCustomKey2;
    UCHAR8 ucOsdCustomKey3;
#endif

#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_LIGHT_EFFECT)
    UCHAR8 ucLightEffect;
#endif

#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_AMBIENT_LIGHT)
    UCHAR8 ucAmbientLight;
    UCHAR8 ucMonoLight;
#endif

#if ((HV_CONFIG_ON == HV_PROJECT_CONFIG_VARIABLEBACKLIGHT) || (HV_CONFIG_ON == HV_PROJECT_CONFIG_FULL_LOCALDIMMING))
    BOOL bVariableBackLight;
#endif

#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_OSD_ROTATION)
    UCHAR8 ucOsdRotation;
#endif

    UCHAR8 ucHue;

#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_CM)
    UCHAR8 ucHueByR;
    UCHAR8 ucHueByG;
    UCHAR8 ucHueByB;
    UCHAR8 ucHueByC;
    UCHAR8 ucHueByM;
    UCHAR8 ucHueByY;

    UCHAR8 ucSatByR;
    UCHAR8 ucSatByG;
    UCHAR8 ucSatByB;
    UCHAR8 ucSatByC;
    UCHAR8 ucSatByM;
    UCHAR8 ucSatByY;

    UCHAR8 ucLumByR;
    UCHAR8 ucLumByG;
    UCHAR8 ucLumByB;
    UCHAR8 ucLumByC;
    UCHAR8 ucLumByM;
    UCHAR8 ucLumByY;
#endif

#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_MAGNIFY)
    BOOL        bMagnifyGlassSw;
    FLOAT32     fMagnifyGlassScalerUpRatio;
    USHORT16    usMagnifyGlassHsize;
    USHORT16    usMagnifyGlassVsize;
    USHORT16    usMagnifyGlassPositionH;
    USHORT16    usMagnifyGlassPositionV;
    USHORT16    usMagnifyGlassSamplingLocH;
    USHORT16    usMagnifyGlassSamplingLocV;
#endif

#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_MAIN_MONTAGE)
    BOOL        bMainMontageSw;
    UCHAR8      ucMontageMode;
    USHORT16    usMontageInHsta;
    USHORT16    usMontageInHend;
    USHORT16    usMontageInVsta;
    USHORT16    usMontageInVend;
#endif

#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_MAIN_FLIP)
    BOOL        bMainHfilpEn;
    BOOL        bMainVfilpEn;
#endif
    BOOL        bAutoVideoSrcSelSw;
    BOOL        bAutoVideoBackup;

    UCHAR8      ucVolume;
    BOOL        bMute;
#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_AUDIO_SETTING)
    UCHAR8      ucAudioSource; /*0主通道/左通道 1:副通道/右通道*/
    UINT32      uiHeadsetVolume;
    UINT32      uiSpeakerVolume;
    UINT32      uiSoundDelayTime;
    UINT32      uiEQMode;
#endif

#ifdef CONFIG_USER_DOUBLE_CHANNEL_ON
    UCHAR8      ucPxPMode;
    UCHAR8      ucPxPColor;
    UCHAR8      ucPxPVideoSource;
    BOOL        bPxPSwitch;
    UCHAR8      ucPipPosition;          /*0:左上 1：左下 2：右上 3右下*/
    UCHAR8      ucPipSize;              /*0:small 1:middle 2:big*/
    USHORT16    usPipSmallWindowHSize;  /*PANEL_H_ACTIVE * 2 / 5*/
    USHORT16    usPipSmallWindowVSize;  /*PANEL_V_ACTIVE * 2 / 5*/
    USHORT16    usPipMiddleWindowHSize; /*PANEL_H_ACTIVE/2*/
    USHORT16    usPipMiddleWindowVSize; /*PANEL_V_ACTIVE/2*/
    USHORT16    usPipBigWindowHSize;    /*PANEL_H_ACTIVE/2*/
    USHORT16    usPipBigWindowVSize;    /*PANEL_V_ACTIVE/2*/
    USHORT16    usPipWindowHSize;       /*当前使用的pip h大小*/
    USHORT16    usPipWindowVSize;       /*当前使用的pip v大小*/

    UCHAR8      ucPbpSize;              /*0:small 1:middle 2:big*/
    USHORT16    usPbpSmallWindowHSize;
    USHORT16    usPbpMiddleWindowHSize;
    USHORT16    usPbpBigWindowHSize;
    USHORT16    usPbpWindowHSize;       /*当前使用的pbp h大小*/
    USHORT16    usOsdPipHPosLevel;
    USHORT16    usOsdPipVPosLevel;
#endif

#if (HV_BOARD_CONFIG_HDMI_0 == PORT_VALID)
    UCHAR8      ucHdmi0EdidCfg;
#endif
#if (HV_BOARD_CONFIG_HDMI_1 == PORT_VALID)
    UCHAR8      ucHdmi1EdidCfg;
#endif
    /* 图像比例的参数 */
    PanelInchParam  astPanelInch[OUTPUTRATIO_MAX];

#if (0 != HV_PROJECT_CONFIG_DPU_OVER_SCAN_SIDE_SIZE)
    BOOL   bOverScan;
#endif
    UCHAR8 ucProEsportSwitch;
    USHORT16 usProEsportHSize;
    USHORT16 usProEsportVSize;
    USHORT16 usProEsportHPos;
    USHORT16 usProEsportVPos;
#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_DPU_BRIGHT_FRAME)
    BOOL        bBrightFrameEn;
    USHORT16    usBrightFrameRatio;
    USHORT16    usBrightFrameHsize;
    USHORT16    usBrightFrameVsize;
    USHORT16    usBrightFramePosH;
    USHORT16    usBrightFramePosV;
    UCHAR8      ucOsdBfSizeLevel;
    UCHAR8      ucOsdBfHPosLevel;
    UCHAR8      ucOsdBfVPosLevel;
#endif

#if (HV_CONFIG_ON == HV_PROJECT_CONFIG_OSD_BRIGHT_FRAME)
    BOOL bBrightFrame;
    UCHAR8 ucFrameSize;
    UCHAR8 ucBFBrightness;
    UCHAR8 ucBFContrast;
    UCHAR8 ucBFHPos;
    UCHAR8 ucBFVPos;
#endif
    UCHAR8 ucDDCCI;
    /*注意EEPROM存储使用witre-thru模式，不使用bModifyFlag机制*/
    BOOL        bModifyFlag;/*非用户配置,记录userdata 数据是否被修改，同步flash需要使用*/
}__attribute__((aligned(4)))UserDataDBRegion;

typedef struct _SystemDataDBRegion
{
    USHORT16 usCheckCrc;
    USHORT16 usStructSize;
    UINT32 uiMagicNumber; /*code for check Database valid*/
    USHORT16 usVersion;

    /*system data define here*/
    AudioEqData stAudioEqDataBase[MODE_MAX];

    BOOL        bFactoryMode; /*0:normal mode 1:Factory mode*/
    UCHAR8      ucAutoUpdataEdidEnable;

    SubWindowBorderCfg  stSubWindowBorderCfg;

    BOOL        bBurnInEnable;      /*0:disable 1:enable*/
    BOOL        bEdidWp;
    BOOL        bShowLogoFlag;
    BOOL        bDeveloperMode;
    UCHAR8      ucPowerLedMode;
    /*注意EEPROM存储使用witre-thru模式，不使用bModifyFlag机制*/
    BOOL        bModifyFlag;
}__attribute__((aligned(4)))SystemDataDBRegion;

typedef struct _ColorTemperature
{
    UCHAR8      ucColorTempRGain;
    UCHAR8      ucColorTempGGain;
    UCHAR8      ucColorTempBGain;
}ColorTemperature;

typedef struct _PicCurve
{
    UCHAR8 ucCurve0;
    UCHAR8 ucCurve25;
    UCHAR8 ucCurve50;
    UCHAR8 ucCurve75;
    UCHAR8 ucCurve100;
}PicCurve;

typedef struct _TimeRecDBRegion
{
    USHORT16    usCheckCrc;
    USHORT16    usStructSize;
    UINT32      uiMagicNumber;
    USHORT16    usPanelOnTimeHour;
    USHORT16    usPanelOnTimeMin;
    UCHAR8      ucPmPowerMode;
    BOOL        bModifyFlag;
}__attribute__((aligned(4)))TimeRecDBRegion;

typedef struct _FactoryDataDBRegion
{
    /*Factory data define here*/
    USHORT16            usCheckCrc;
    USHORT16            usStructSize;
    UINT32              uiMagicNumber; /*code for check Database valid*/
    USHORT16            usVersion;
    BOOL                bUartEnable;
    BOOL                bFactoryPanelSscEnable;
    UCHAR8              ucFactoryPanelSscFreq;
    UCHAR8              ucFactoryPanelSsSetp;
    BOOL                bFactoryDdrSscEnable;
    UCHAR8              ucFactoryDdrSscFreq;
    UCHAR8              ucFactoryDdrSsSetp;

    UCHAR8              ucFactWeek;
    UCHAR8              ucFactYear;
    BOOL                bFactWeekUpdate;
    BOOL                bFactYearUpdate;

    UINT32              uiProductSN;
    CHAR8               acDescSn[EDID_DESC_SN_LENTH]; /* 最后需要补齐0x0A 0x20 0x20 0x20;没有可以不用补齐 */
    UCHAR8              aucReservForSn1[3];
    CHAR8               acProductSn[PRODUCT_SN_LENTH];/* 最多24个字节，用不上的字节补充为0, 最后需要用0来作为结束符 */
    UCHAR8              aucReservForSn2[3];
    UCHAR8              ucFactoryBrightness;
    UCHAR8              ucFactoryContrast;
    UCHAR8              ucFactorySharpness;
    UCHAR8              ucFactoryEnergy;
    UCHAR8              ucDefaultLanguage;
    BOOL                bDynamicPanelEn;
    UCHAR8              ucDynamicPanelIndex;

    /*color tempterature calibration data define here*/
    ColorTemperature    stColTempCustom;
    ColorTemperature    stColTempLowBlueLvl1;
    ColorTemperature    stColTempLowBlueLvl2;
    ColorTemperature    stColTempLowBlueLvl3;
    ColorTemperature    stColTempLowBlueLvl4;
    /*auto calibrition*/
    ColorTemperature    stColTempStandard;
    ColorTemperature    stColTempWarm;
    ColorTemperature    stColTempCool;
    ColorTemperature    stColTempSrgb;
    ColorTemperature    stColTempEyeCare;

    ColorTemperature    stColTempP3;
    GamutSet stGamutNative;
    GamutSet stGamutSrgb;
    GamutSet stGamut709;
    GamutSet stGamutP3;
    GamutSet stGamutHdr;
    GamutSet stGamutAdobe;
    /*
    PicCurve stBlackLevel;
    PicCurve stContrast;
    PicCurve stSharpness;
    PicCurve stSaturation;
    */

    UCHAR8              ucUseSeperatorHDCPKey;/*0:use default 1:use Seperator hdcp key*/
    UCHAR8              ucUseExtEdid;

    /*注意EEPROM存储使用witre-thru模式，不使用bModifyFlag机制*/
    BOOL        bModifyFlag;
}__attribute__((aligned(4)))FactoryDataDBRegion;

typedef struct _DisplayDebug
{
    UCHAR8      ucVideoDataPathMode;   /*"0-close 1:framesync 2:frc-uncompress-yuv444 3:frc-uncompress-yuv422 
                                        4:frc-uncompress-yuv420 5:KTX 6:free_run_node1 7:compress-yuv444 8:compress-yuv422"*/
    BOOL        bHflipEnable;
    BOOL        bVflipEnable;
    USHORT16    usMaxMainFrameBufferHsize;
    USHORT16    usMaxMainFrameBufferVsize;

    USHORT16    usMaxPipFrameBufferHsize;
    USHORT16    usMaxPipFrameBufferVsize;
}__attribute__((aligned(4)))DisplayDebug;

typedef struct _DiagDataDBRegion
{
    /*Diag data define here*/
    DisplayDebug stDisplayDebug;

    //CM
    UCHAR8 ucDBHueR;
    UCHAR8 ucDBHueG;
    UCHAR8 ucDBHueB;
    UCHAR8 ucDBHueC;
    UCHAR8 ucDBHueY;
    UCHAR8 ucDBHueM;

    UCHAR8 ucDBSatR;
    UCHAR8 ucDBSatG;
    UCHAR8 ucDBSatB;
    UCHAR8 ucDBSatC;
    UCHAR8 ucDBSatY;
    UCHAR8 ucDBSatM;

    UCHAR8 ucDBLumR;
    UCHAR8 ucDBLumG;
    UCHAR8 ucDBLumB;
    UCHAR8 ucDBLumC;
    UCHAR8 ucDBLumY;
    UCHAR8 ucDBLumM;
    USHORT16 usDBBrightness;
    UCHAR8 ucDBHueAll[64];
    UCHAR8 ucDBSatAll[64];
    UCHAR8 ucDBLumAll[64];

    UCHAR8 ucDBSharpness;
    //GC
    UCHAR8 ucDBGcMode;
    UCHAR8 ucDBGcBlack;
    /////LD

    ////WCG
    UCHAR8 ucDBGamut;
    ///CSCMTX
    UCHAR8 ucDBBrightRange;
    UCHAR8 ucDBColorSpace;
    UINT32 uiDBContrast;
    UCHAR8 ucDBHue;
    USHORT16 usDBRGain;
    USHORT16 usDBGGain;
    USHORT16 usDBBGain;
    USHORT16 usDBROffset;
    USHORT16 usDBGOffset;
    USHORT16 usDBBOffset;
    UCHAR8 ucDBBlackLevel;
    UCHAR8 ucDBSaturation;

    //GAMMA
    UCHAR8 ucDBGammaIndex;
    
    UCHAR8 ucDBPqBypassEn;

    //TBD
    //UCHAR8 ucDBColorTemp;
    //UCHAR8 ucDBPicMode;

   BOOL        bModifyFlag;
}__attribute__((aligned(4)))DiagDataDBRegion;

typedef struct _GammaDataDBRegion
{
    USHORT16 usCheckCrc;
    UINT32 uiMagicNumber;   /*code for check Database valid*/
    UCHAR8 ucGmaNum;            /*real gamma Num*/
    UCHAR8 aucGammaIndex[7];    /*max 7 gamma Tables, 0x18 0x20 0x22 0x24 0x26 0x28*/ 
    UINT32 auiGamma[3*1025];    /*dispCtrl Gamma size per table*/
    UCHAR8 ucGammaCompress;     /*0 orig, 1 compress*/
	UINT32 uiEndMagicNumber;
    BOOL        bModifyFlag;
}__attribute__((aligned(4)))GammaDataDBRegion;

typedef struct _HDCPKeyDataDBRegion
{
    Hdcp1xRxKey stHdcp1xRxKey; /* 5+280 = 285 bytes */
    Hdcp2xRxKey stHdcp2xRxKey; /* 522+16+320 = 858 bytes */
    UCHAR8      ucRsved;       /* 4Byte aligned Set 0x00*/
    UINT32      uiRsved1;      /* For AES 16Byte Aligned */
    UINT32      uiCheckSum;    /* CheckSum of the Above Data */
}__attribute__((aligned(4)))HDCPKeyDataDBRegion; /*Total 72 * 16 = 1152 = 285+858+1+8 Byte*/

/*this is for Database*/
typedef struct _HDCPDataDBRegion
{
    USHORT16 usCheckCrc;
    USHORT16 usVersion;
    UINT32   uiMagicNumber; /*code for check Database valid*/
    HDCPKeyDataDBRegion stKey;
    UCHAR8      aucHdcp1XKeyId[8];
    UCHAR8      aucHdcp2XKeyId[8];
    BOOL        bUseDefaultKey;
    BOOL        bHdcp1XValid;
    BOOL        bHdcp2XValid;
    UINT32      uiAlignUsed;
    BOOL        bModifyFlag;
}__attribute__((aligned(4)))HDCPDataDBRegion; 

typedef struct _EdidDataDBRegion
{
    EDIDType    eEdidType;
    UINT32      uiEdidLen;
    UCHAR8      acEdid[512];
}__attribute__((aligned(4)))EdidDataDBRegion;

typedef struct EdidDBRegion
{
    USHORT16 usCheckCrc;
    UINT32 uiMagicNumber; /*code for check Database valid*/
    union
    {
        EdidDataDBRegion    astDpEdid[EDID_DB_MAX_NUMBER];
        EdidDataDBRegion    astHdmiEdid[EDID_DB_MAX_NUMBER];
        EdidDataDBRegion    astEdid[EDID_DB_MAX_NUMBER];
    };
    UCHAR8              ucNumber;
    UINT32              uiAlignUsed;
    BOOL                bModifyFlag;
}__attribute__((aligned(4)))EdidDBRegion;

typedef EdidDBRegion DpEdidDBRegion;
typedef EdidDBRegion HdmiEdidDBRegion;

#if 0
typedef struct _DpEdidDBRegion
{
    USHORT16 usCheckCrc;
    UINT32 uiMagicNumber; /*code for check Database valid*/
    EdidDataDBRegion    astDpEdid[DP_EDID_MAX_NUMBER];
    UCHAR8              ucNumber;
    UINT32              uiAlignUsed;
    BOOL                bModifyFlag;
}__attribute__((aligned(4)))DpEdidDBRegion;

typedef struct _HdmiEdidDBRegion
{
    USHORT16 usCheckCrc;
    UINT32 uiMagicNumber; /*code for check Database valid*/
    EdidDataDBRegion    astHdmiEdid[HDMI_EDID_MAX_NUMBER];
    UCHAR8              ucNumber;
    UINT32              uiAlignUsed;
    BOOL                bModifyFlag;
}__attribute__((aligned(4)))HdmiEdidDBRegion;
#endif

#define FLASH_PANLE_PARAM_SUIT_MAX_NUM     5
typedef struct _PanelDataDBRegion
{
    /*130个字节*/
    UINT32          uiMagicNumber;          /*code for check Database valid*/
    USHORT16        usVersion;
    UCHAR8          ucPanelNumber;
    PanelParam      stPanelParam[FLASH_PANLE_PARAM_SUIT_MAX_NUM];
    BOOL            bModifyFlag;/*非用户配置,记录userdata 数据是否被修改，同步flash需要使用*/
}__attribute__((aligned(4)))PanelDataDBRegion;

#define SYSTEM_DATA_SIZE                sizeof(SystemDataDBRegion)
#define USER_DATA_SIZE                  sizeof(UserDataDBRegion)
#define FACTORY_DATA_SIZE               sizeof(FactoryDataDBRegion)
#define DIAG_DATA_SIZE                  sizeof(DiagDataDBRegion)
#define HDCP_DATA_SIZE                  sizeof(HDCPKeyDataDBRegion)
#define GAMMA_DATA_SIZE                 sizeof(GammaDataDBRegion)
#define TIMEREC_DATA_SIZE               sizeof(TimeRecDBRegion)
#define DPEDID_DATA_SIZE                sizeof(DpEdidDBRegion)
#define HDMIEDID_DATA_SIZE              sizeof(HdmiEdidDBRegion)
#define PANEL_DATA_SIZE                 sizeof(PanelDataDBRegion)

#ifndef HV_BOARD_CONFIG_USER_IN_E2P
#define TIME_DATA_START_ADDRESS         (HV_FLASH_CONFIG_MONITOR_DATA_PART_START + HV_FLASH_CONFIG_TIMER_START)
#define SYSTEM_DATA_START_ADDRESS       (HV_FLASH_CONFIG_MONITOR_DATA_PART_START + HV_FLASH_CONFIG_SYSTEMDATA_START)
#define USER_DATA_START_ADDRESS         (HV_FLASH_CONFIG_MONITOR_DATA_PART_START + HV_FLASH_CONFIG_USERDATA_START)
#define USER_BACK_START_ADDRESS         (HV_FLASH_CONFIG_MONITOR_DATA_PART_START + HV_FLASH_CONFIG_USERBACK_START)
#define FACTORY_DATA_START_ADDRESS      (HV_FLASH_CONFIG_MONITOR_DATA_PART_START + HV_FLASH_CONFIG_FACDATA_START)

#define HDCP_DATA_START_ADDRESS         (HV_FLASH_CONFIG_MONITOR_DATA_PART_START + HV_FLASH_CONFIG_HDCP_START)
#define GAMMA_DATA_START_ADDRESS        (HV_FLASH_CONFIG_MONITOR_DATA_PART_START + HV_FLASH_CONFIG_GAMMA_START)
#define DPEDID_DATA_START_ADDRESS       (HV_FLASH_CONFIG_MONITOR_DATA_PART_START + HV_FLASH_CONFIG_DPEDID_START)
#define HDMIEDID_DATA_START_ADDRESS     (HV_FLASH_CONFIG_MONITOR_DATA_PART_START + HV_FLASH_CONFIG_HDMIEDID_START)
#define PANEL_DATA_START_ADDRESS        (HV_FLASH_CONFIG_MONITOR_DATA_PART_START + HV_FLASH_CONFIG_PANEL_START)
#else
#define SYSTEM_DATA_START_ADDRESS       0
#define USER_DATA_START_ADDRESS         (SYSTEM_DATA_START_ADDRESS + SYSTEM_DATA_SIZE)
#define FACTORY_DATA_START_ADDRESS      (USER_DATA_START_ADDRESS + USER_DATA_SIZE)
#define DIAG_DATA_START_ADDRESS         (FACTORY_DATA_START_ADDRESS + FACTORY_DATA_SIZE)
#define HDCP_DATA_START_ADDRESS         (DIAG_DATA_START_ADDRESS + DIAG_DATA_SIZE)
#define GAMMA_DATA_START_ADDRESS        (HDCP_DATA_START_ADDRESS + HDCP_DATA_SIZE)
#define TIME_DATA_START_ADDRESS         (GAMMA_DATA_START_ADDRESS + GAMMA_DATA_SIZE)
#define DPEDID_DATA_START_ADDRESS       (TIME_DATA_START_ADDRESS + TIMEREC_DATA_SIZE)
#define HDMIEDID_DATA_START_ADDRESS     (DPEDID_DATA_START_ADDRESS + DPEDID_DATA_SIZE)
#define PANEL_DATA_START_ADDRESS        (HDMIEDID_DATA_START_ADDRESS + PANEL_DATA_SIZE)
#endif

#define DECLARE_MODULE_GET_DATA_PTR(module)   module##DBRegion* Hv_Common_Db_Get##module##DBRegionPtr()
DECLARE_MODULE_GET_DATA_PTR(UserData);
DECLARE_MODULE_GET_DATA_PTR(FactoryData);
DECLARE_MODULE_GET_DATA_PTR(DiagData);
DECLARE_MODULE_GET_DATA_PTR(SystemData);
DECLARE_MODULE_GET_DATA_PTR(HDCPKeyData);
DECLARE_MODULE_GET_DATA_PTR(HDCPData);
DECLARE_MODULE_GET_DATA_PTR(GammaData);
DECLARE_MODULE_GET_DATA_PTR(TimeRec);
DECLARE_MODULE_GET_DATA_PTR(DpEdid);
DECLARE_MODULE_GET_DATA_PTR(HdmiEdid);
DECLARE_MODULE_GET_DATA_PTR(PanelData);

#define DECLARE_MODULE_GET_DATA_ADDRESS(module)   UINT32 Hv_Common_Db_Get##module##DBRegionAddress()
DECLARE_MODULE_GET_DATA_ADDRESS(UserData);
DECLARE_MODULE_GET_DATA_ADDRESS(FactoryData);
DECLARE_MODULE_GET_DATA_ADDRESS(DiagData);
DECLARE_MODULE_GET_DATA_ADDRESS(SystemData);
DECLARE_MODULE_GET_DATA_ADDRESS(HDCPKeyData);
DECLARE_MODULE_GET_DATA_ADDRESS(HDCPData);
DECLARE_MODULE_GET_DATA_ADDRESS(GammaData);
DECLARE_MODULE_GET_DATA_ADDRESS(TimeRec);
DECLARE_MODULE_GET_DATA_ADDRESS(DpEdid);
DECLARE_MODULE_GET_DATA_PTR(HdmiEdid);
DECLARE_MODULE_GET_DATA_ADDRESS(PanelData);

/*moule name fill as : UserData, FactoryData, SystemData*/
/*use such as HV_COMMON_DATABASE_GET(UserData, uiCrc), OSD task和System task共享该数据，所以需要增加临界资源保护*/
/*禁止在中断中调用该接口因为进退临界区会开关中断使能开关!!!*/

#define HV_COMMON_DATABASE_GET(module, param)                      \
({\
    Hv_Vos_TaskEnterCritical();\
    typeof(Hv_Common_Db_Get##module##DBRegionPtr()->param) temp = Hv_Common_Db_Get##module##DBRegionPtr()->param;\
    Hv_Vos_TaskExitCritical();\
    temp;\
})

#define HV_COMMON_DATABASE_GET_STARRAY(module, param,index, param2)                      \
({\
    Hv_Vos_TaskEnterCritical();\
    typeof(Hv_Common_Db_Get##module##DBRegionPtr()->param[index].param2) temp = Hv_Common_Db_Get##module##DBRegionPtr()->param[index].param2;\
    Hv_Vos_TaskExitCritical();\
    temp;\
})


#define HV_COMMON_DATABASE_SET(module, param, value)                \
do                                                                  \
{                                                                   \
    HV_LOG_DEBUG(DB, "%s->%s value=%x",#module, #param, #value);    \
    Hv_Vos_TaskEnterCritical();\
    typeof(Hv_Common_Db_Get##module##DBRegionPtr()->param) temp = Hv_Common_Db_Get##module##DBRegionPtr()->param;\
    Hv_Common_Db_Get##module##DBRegionPtr()->param = value;         \
    if (0 != memcmp(&temp, &(Hv_Common_Db_Get##module##DBRegionPtr()->param), sizeof(Hv_Common_Db_Get##module##DBRegionPtr()->param)))\
    {                                                               \
        if (STORAGE_SPI_FLASH == HV_BOARD_CONFIG_DATABASE_STORAGE_TYPE)\
        {\
            Hv_Common_Db_Get##module##DBRegionPtr()->bModifyFlag = HV_TRUE;\
        }\
        else\
        {\
            Hv_Drv_Eeprom_Write(0, (USHORT16)(Hv_Common_Db_Get##module##DBRegionAddress() + offsetof(module##DBRegion, param)), (UCHAR8*)&temp, sizeof(temp));\
        }\
    }                                                               \
    Hv_Vos_TaskExitCritical();\
}while(0)

#define HV_COMMON_DATABASE_SET_STARRAY(module, param, index, param2, value)                \
do                                                                  \
{                                                                   \
    HV_LOG_DEBUG(DB, "%s->%s value=%x",#module, #param, #value);    \
    Hv_Vos_TaskEnterCritical();\
    typeof(Hv_Common_Db_Get##module##DBRegionPtr()->param[index].param2) temp = Hv_Common_Db_Get##module##DBRegionPtr()->param[index].param2;\
    Hv_Common_Db_Get##module##DBRegionPtr()->param[index].param2 = value;         \
    if (0 != memcmp(&temp, &(Hv_Common_Db_Get##module##DBRegionPtr()->param[index].param2), sizeof(Hv_Common_Db_Get##module##DBRegionPtr()->param[index].param2)))\
    {                                                               \
        if (STORAGE_SPI_FLASH == HV_BOARD_CONFIG_DATABASE_STORAGE_TYPE)\
        {\
            Hv_Common_Db_Get##module##DBRegionPtr()->bModifyFlag = HV_TRUE;\
        }\
        else\
        {\
            Hv_Drv_Eeprom_Write(0, (USHORT16)(Hv_Common_Db_Get##module##DBRegionAddress() + offsetof(module##DBRegion, param[index].param2)), (UCHAR8*)&temp, sizeof(temp));\
        }\
    }                                                               \
    Hv_Vos_TaskExitCritical();\
}while(0)

/**
 * @brief load data form flash.
 * @return Status, 0-HV_SUCCESS, 1-HV_FAILURE.
 */
Status Hv_Mw_DataBase_MonitorData_Load(UINT32 uiAddress, UCHAR8 *pucData, UINT32 uiDataSize);

/**
 * @brief database init.
 * @param[in] void
 * @return Status, 0-HV_SUCCESS, 1-HV_FAILURE.
 */
Status Hv_Common_Db_Init( VOID );

/**
 * @brief Sync database data config to flash/EEPROM.
 * @param[in] void
 * @return  Status, 0-HV_SUCCESS, 1-HV_FAILURE.
 */
Status Hv_Mw_DataBase_Sync( VOID );

/**
 * @brief Resume user data config to default.
 * @param[in] void
 * @return  Status, 0-HV_SUCCESS, 1-HV_FAILURE.
 */
Status Hv_Mw_DataBase_ResumeUserData( VOID );

/**
 * @brief Resume user data config to default.
 * @param[in] range ：0-pannel Vactive
 * @return  Status, 0-HV_SUCCESS, 1-HV_FAILURE.
 */
Status Hv_Mw_DataBase_ResumeSystemData( VOID );

/**
 * @brief Get default user data ptr.
 * @return  Default user data ptr.
 */
const UserDataDBRegion *Hv_Mw_DataBase_GetDefaultUserDataPtr(VOID);


UCHAR8 HV_Common_GetHdcpStatus(VOID);
VOID HV_Common_SetHdcpStatus(UCHAR8 ucStatus);

UCHAR8 HV_Common_GetHdmiEdidStatus(VOID);
VOID HV_Common_SetHdmiEdidStatus(UCHAR8 ucStatus);

UCHAR8 HV_Common_GetHdmi1EdidStatus(VOID);
VOID HV_Common_SetHdmi1EdidStatus(UCHAR8 ucStatus);

UCHAR8 HV_Common_GetDpEdidStatus();
VOID HV_Common_SetDpEdidStatus(UCHAR8 ucStatus);

UCHAR8 HV_Common_GetDp1EdidStatus();
VOID HV_Common_SetDp1EdidStatus(UCHAR8 ucStatus);

UCHAR8 HV_Common_GetGammaStatus(VOID);
VOID HV_Common_SetGammaStatus(UCHAR8 ucStatus);

GammaDataDBRegion* Hv_Common_Db_GetGammaDataDBRegionPtr(VOID);
FactoryDataDBRegion* Hv_Common_Db_GetFactoryDataDBRegionPtr(VOID);

HdmiEdidDBRegion* Hv_Common_Db_GetHdmiEdidDBRegionPtr(VOID);
HdmiEdidDBRegion* Hv_Common_Db_GetHdmi1EdidDBRegionPtr(VOID);
DpEdidDBRegion* Hv_Common_Db_GetDpEdidDBRegionPtr(VOID);
DpEdidDBRegion* Hv_Common_Db_GetDp1EdidDBRegionPtr(VOID);
volatile FactoryDataDBRegion* Hv_Common_Db_GetDefaultFactoryDataDBRegionPtr(VOID);
Status Hv_Mw_DataBase_SyncHdmiEdid(VOID);
Status Hv_Mw_DataBase_SyncHdmi1Edid(VOID);

VOID HV_Common_SetHdcp1Valid(UCHAR8 ucValid);
VOID HV_Common_SetHdcp2Valid(UCHAR8 ucValid);
Status Hv_Mw_DataBase_SyncFactory(VOID);
Status Hv_Mw_DataBase_SyncHdcp(VOID);

Status Hv_Mw_DataBase_DdcSync(VOID);
Status Hv_Mw_DataBase_DdcSyncHdcp(VOID);
Status Hv_Mw_DataBase_DdcSyncFactory(VOID);
Status Hv_Mw_DataBase_DdcSyncGamma(VOID);
Status Hv_Mw_DataBase_DdcSyncHdmiEdid(VOID);
Status Hv_Mw_DataBase_DdcSyncHdmi1Edid(VOID);
Status Hv_Mw_DataBase_DdcSyncDPEdid(VOID);
Status Hv_Mw_DataBase_DdcSyncDP1Edid(VOID);

#endif
