#include "TouchConfig.h"
#include <string.h>
#include "TouchCommon.h"
#include "HidReport.h"
#include "bsp_param.h"
#include "api.h"

#if USBCONFIG
ST_CONFIG const_config ={
    24,//sizeof(ST_CONFIG) - 13,//0x18 + 10,
    EN_USB_UART,
    0,
    0,
    0,
    EN_MULTITOUCH_ONLY,
    EN_UART_PROTOCAL_10,  //0x60
    0x27,
    0xC6,
    0x09,
    0x28,
    0,
    0,
    0,
    0x08,
    0x79,
    0x03,
    0x8a,
    0x40,
    0x00,  //16384
    0x25,
    0x80,  //9600
    {0,0,0,0,0,0},
    {0,0},
};

__attribute__ ((aligned(4))) ST_CONFIG  config;

UINT16 GetCheckSum(PUINT8 cfg)
{
    UINT8 i;
    UINT16 chk_sum = 0;

    for(i = 0; i< cfg[0] - 2; i++)
    {
        chk_sum += *(cfg + i);
    }
    return chk_sum;
}

BOOL CheckConfigValid(PUINT8 cfg)
{
    UINT16 chk_sum;
    UINT16 tmp;

    if((cfg[0] == 0) || (cfg[0] > CONFIG_SIZE))
    {
        PRINT( "CONFIG NG\n" );
        return 0;
    }
    if(cfg[0] <= CONFIG_SIZE)
    {
        chk_sum = GetCheckSum(cfg);

        tmp = cfg[cfg[0] - 2];

        tmp = (tmp << 8) + cfg[cfg[0] - 1];

        if(chk_sum != tmp)
        {
           PRINT( "CONFIG CHKSUM ERROR\n" );

           return 0;
        }
    }

    return 1;
}

void ConfigInit()
{
    UINT16 temp;

    PRINT("Load config\r\n");

    GetConfigFromFlash((PUINT8)&config);

    if(!CheckConfigValid((PUINT8)&config))
    {
      PRINT("Config invalid\r\n");

      {
          UINT16 chksum;
          PRINT("Reset config\r\n");

          config = const_config;
          config.len = CONFIG_SIZE;
          chksum = GetCheckSum((PUINT8)&config);
          //config.check_sum = chksum;
          ((PUINT8)&config)[CONFIG_SIZE-2] = chksum >> 8;
          ((PUINT8)&config)[CONFIG_SIZE-1] = chksum;
      }

      SaveConfig((PUINT8)&config);
    }


    temp = ((UINT16)config.resolution_x_h << 8) + config.resolution_x_l;
    if (0 == temp)
    {
        config.resolution_x_h = 0x40;  //16384
        config.resolution_x_l = 0x00;
    }

    temp = ((UINT16)config.resolution_y_h << 8) + config.resolution_y_l;
    if (0 == temp)
    {
        config.resolution_y_h = 0x25;  //9600
        config.resolution_y_l = 0x80;
    }
}

void GetConfigFromFlash(PUINT8 pBuf)
{
    bsp_param_read(pBuf, TOUCH_CONFIG_ADDR, sizeof(ST_CONFIG));
}

void SaveConfig(PUINT8 cfg)
{
    if(CheckConfigValid(cfg))
    {
        bsp_param_write(cfg, TOUCH_CONFIG_ADDR, sizeof(ST_CONFIG));

        bsp_param_sync();
    }
}

UINT32 GetHandsShakeValue(UINT8* buf)
{
    UINT8 i;
    UINT32 HandsShakeValue = 0x0928;

    for(i = 0; i < 16; i++)
    {
        HandsShakeValue = (HandsShakeValue * 9897) + (buf[i]);
    }

    return HandsShakeValue;
}

void CheckFlag()
{
  //与AP程序的握手标记
    UINT32 uid[4];

    UINT32 hash_flag;

    bsp_param_read((UINT8*)&hash_flag, TOUCH_HASH_FLAG_ADDR, 4);

    os_spiflash_id_get((UINT8*)uid);

    uid[3] = GetHandsShakeValue((UINT8*)uid);

    if(hash_flag!=uid[3])
    {
        //SoftResetChip();
    }
}

CHIP_TYPE GetChipTypeFromFlash(void)
{
    UINT8 ct;

    bsp_param_read((u8 *)&ct, TOUCH_CHIP_TYPE_ADDR, 1);

    return ct;
}

void SaveChipTypeToFlash(CHIP_TYPE chip_type)
{
    UINT8 ct = chip_type;

    bsp_param_write((u8 *)&ct, TOUCH_CHIP_TYPE_ADDR, 1);

    bsp_param_sync();
}
#endif




