#ifndef _CFGINFO_H_
#define _CFGINFO_H_

#include "ptz_type_select.h"

#include "comm_types.h"
#ifdef __cplusplus
extern "C" {
#endif
#pragma pack(push,1)

#define EXT_FLASH_SECTOR_SIZE   (1024*4)
#define EXT_FLASH_BLOCK_NUM     (0)
#define EXT_FLASH_BLOCK_NUM_1   (1)  
   
//升级包在片外flash缓存时使用的地址信息
#define UPGRADE_FLASH_BUFF_BEGIN_ADDR (1024*1024*1)//1M-2M      
#define UPGRADE_FLASH_BUFF_END_ADDR (1024*1024*2-1)
#define UPGRADE_FLASH_BUFF_SIZE (1024*1024*1)
#define UPGRADE_FLASH_BUFF_BEGIN_BLOCK (UPGRADE_FLASH_BUFF_BEGIN_ADDR / Flash_BLOCKBYTE_LENGTH)

////bootloader地址信息
//#define BOOTLOADER_ADDR_BEGIN (0)
//#define BOOTLOADER_ADDR_END (1024*58-1)
//#define BOOTLOADER_SECTOR_BEGIN (BOOTLOADER_ADDR_BEGIN / ON_CHIP_FLASH_SECTOR_SIZE) //Bootloader起始sector

////应用程序地址信息
//#define APP_ADDR_BEGIN (1024*64)
//#define APP_ADDR_END (1024*500-1)//500k
//#define APP_SECTOR_BEGIN (APP_ADDR_BEGIN / ON_CHIP_FLASH_SECTOR_SIZE) //应用程序起始sector

  
/*
**将片内rom进行重新分配
*/
//bootloader地址信息--片内
#define BOOTLOADER_ADDR_BEGIN     (0x08000000)//扇区0，，0x08000000
#define BOOTLOADER_ADDR_END       (0x0800FFFF)//2扇区
#define BOOTLOADER_SECTOR_BEGIN    CTL_SECTOR_NUMBER_0//0 //Bootloader起始sector

//应用程序地址信息--片内
#define APP_ADDR_BEGIN            (0x08020000)//扇区4，，0x08010000
#define APP_ADDR_END              (0x0807FFFF)//448KB，，扇区7
#define APP_SECTOR_BEGIN           CTL_SECTOR_NUMBER_5//4 //应用程序起始sector

#define APP_ADD_BEGIN_OFFEST      (0x08000000)

//应用程序包头存储地址信息，在应用程序前4K保存--片内
#define APP_PKG_HEAD_BEGIN         0x08010000//(APP_ADDR_BEGIN - 4096)
#define APP_PKG_HEAD_END           0x0801FFFF//(APP_ADDR_BEGIN-1)
#define APP_PKG_HEAD_SECTOR_BEGIN   CTL_SECTOR_NUMBER_4//3  //头存储的sector(APP_PKG_HEAD_BEGIN/ON_CHIP_FLASH_SECTOR_SIZE)
#define APP_PGK_HEAD               ((hy_release_file_head*)APP_PKG_HEAD_BEGIN)

  
  
  
  
  
//安全证书地址信息--放在w25q128中
#define CERT_ADDR_BEGIN (1024*4*2)//0block 2sector(4k) 包头放在1sector
#define CERT_ADDR_END   (1024*4*3-1)
#define CERT_SECTOR_BEGIN (CERT_ADDR_BEGIN / EXT_FLASH_SECTOR_SIZE)//安全加密芯片证书

//密钥地址信息--放在w25q128中
#define KEY_ADDR_BEGIN  (1024*4*4)//0block 4sector(4k) 包头放在3sector
#define KEY_ADDR_END    (1024*4*5-1)
#define KEY_SECTOR_BEGIN (KEY_ADDR_BEGIN / EXT_FLASH_SECTOR_SIZE)//安全加密芯片密钥

//配置文件地址信息--放在w25q128中
#define CFG_FILE_ADDR_BEGIN  (1024*4*6)//0block 6sector(4k) 包头放在5sector
#define CFG_FILE_ADDR_END    (1024*4*7-1)
#define CFG_FILE_SECTOR_BEGIN (CFG_FILE_ADDR_BEGIN / EXT_FLASH_SECTOR_SIZE)//配置文件

////应用程序包头存储地址信息，在应用程序前2个sector保存
//#define APP_PKG_HEAD_BEGIN (APP_ADDR_BEGIN - ON_CHIP_FLASH_SECTOR_SIZE)
//#define APP_PKG_HEAD_END (APP_ADDR_BEGIN-1)
//#define APP_PKG_HEAD_SECTOR_BEGIN (APP_PKG_HEAD_BEGIN/ON_CHIP_FLASH_SECTOR_SIZE)//头存储的sector
//#define APP_PGK_HEAD ((hy_release_file_head*)APP_PKG_HEAD_BEGIN)

//安全证书包头存储地址信息，在芯片安全证书前1个sector保存
#define CERT_HEAD_BEGIN (CERT_ADDR_BEGIN - EXT_FLASH_SECTOR_SIZE)
#define CERT_HEAD_END (CERT_ADDR_BEGIN-1)
#define CERT_HEAD_SECTOR_BEGIN (CERT_HEAD_BEGIN/EXT_FLASH_SECTOR_SIZE)//头存储的sector

//密钥包头存储地址信息，在密钥前2个sector保存
#define KEY_HEAD_BEGIN (KEY_ADDR_BEGIN - EXT_FLASH_SECTOR_SIZE)
#define KEY_HEAD_END (KEY_ADDR_BEGIN-1)
#define KEY_HEAD_SECTOR_BEGIN (KEY_HEAD_BEGIN/EXT_FLASH_SECTOR_SIZE)//头存储的sector

//配置文件包头存储地址信息，在配置文件前2个sector保存
#define CFG_FILE_HEAD_BEGIN (CFG_FILE_ADDR_BEGIN - EXT_FLASH_SECTOR_SIZE)
#define CFG_FILE_HEAD_END (CFG_FILE_ADDR_BEGIN-1)
#define CFG_FILE_HEAD_SECTOR_BEGIN (CFG_FILE_HEAD_BEGIN/EXT_FLASH_SECTOR_SIZE)//头存储的sector  
    
#define HY_RELEASE_FILE_FLAG (*((u_int32_t *)"HYPK"))
  
#define HY_PKG_TYPE_APP         1 //升级文件类型是application，应用程序
#define HY_PKG_TYPE_BOOT_LOADER 2 //升级文件类型是bootloader
#define HY_PKG_TYPE_CERT        3 //加密芯片证书
#define HY_PKG_TYPE_PUB_KEY     4 //公钥
#define HY_PKG_TYPE_CFG_FILE    5 //配置文件 


//通过指令直接修改云台IP地址 
#define ENTER                     "\r\n"
#define STR_END                   "\0"  
/**/  
#define HOST_MK60_CFG             "[host_mk60_cfg]"  
  
#define HARDWAREVER               "hardwarever="
#define HARDWAREVER_DEFAULT       "00000000000000"
  
#define CMD_ID                    "cmd_id="
#define CMD_ID_DEFAULT	          "HY_OLMS_000000000" 
  
#define COMPONENT_ID              "component_id="
#define COMPONENT_ID_DEFAULT	  "HY_OLMS_000000000"	
  
/**/
#define LOCAL                     "[local]"
 
#define LOCAL_IP                  "ip="
#define LOCAL_IP_DEFAULT          "192.168.8.200"  
 
#define LOCAL_MAC                 "mac="
#define LOCAL_MAC_DEFAULT         "42:00:00:00:00:01"  
                          
#define LOCAL_NETMASK             "netmask="
#define LOCAL_NETMASK_DEFAULT     "255.255.255.0"                          
                          
#define LOCAL_GATEWAY             "gateway="
#define LOCAL_GATEWAY_DEFAULT     "172.16.19.254"                          
/**/
  
////新增功能配置信息,可通过更改配置文件实现相关功能的使用-lqc-20220316
#define FUNCTION                         "[self_check]"
//简易自检
#define SIMPLIFY_SELF_CHECK              "simplify_check="
  
#ifdef PTZ_LIGHT_GEAR_L6235D_AS5047D       //直齿云台默认自检
  #define SIMPLIFY_SELF_CHECK_DEFAULT      "both_enabled"   //both_disabled,both_enabled,hori_enabled,vert_enabled
#else
  #define SIMPLIFY_SELF_CHECK_DEFAULT      "both_disabled"   //both_disabled,both_enabled,hori_enabled,vert_enabled
#endif
  
//全自检
#define COMPLETE_SELF_CHECK              "complete_check="
#define COMPLETE_SELF_CHECK_DEFAULT      "enable"  //enable:初次上电或读取数据失败时可进行全自检，disabled:在任何情况下都不进行全自检

//拨码开关
#define BAUD_AND_ADD               "[baud_and_add]"
#define UART_422_BAUD              "uart_422_baud="
#define UART_422_BAUD_DEFAULT      "9600"
#define UART_485_BAUD              "uart_485_baud="
#define UART_485_BAUD_DEFAULT      "9600"
#define HYT_ADDRESS                "hyt_address="
#define HYT_ADDRESS_DEFAULT        "1"
#define ERROR_compensation         "compensation_switch="
#ifdef PTZ_BLDC_MOTOR
  #define ERROR_compensation_switch  "on"//误差补偿开关,直流无刷电机原则上不使用误差补偿，而使用重复定位
#endif
  
/**/
#define END                       "[end]"
/**/

//配置文件存放位置
#define CFG_HEAD_BLOCK_NUM         0
#define CFG_HEAD_SECTOR_NUM        5
#define CFG_DATA_BLOCK_NUM         0
#define CFG_DATA_SECTOR_NUM        6

//配置文件连续保存次数
#define CFG_SAVE_FLASH_NUM       3
  
#define SET_LOCAL_IP             0X01//设置IP
#define SET_LOCAL_MAC            0X02//设置MAC
#define SET_LOCAL_NETMASK        0X03//设置NETMASK
#define SET_LOCAL_GATEWAY        0X04//设置GATEWAY
#define SET_TERM_IP              0X05
#define SET_WEB_IP               0X06
#define SET_A9_IP                0X07
#define SET_HARDWAREVER          0X08
#define SET_CMD_ID               0X09
#define SET_COMPONENT_ID         0X0A  
#define SET_SIMPLIFY_SELF_CHECK  0X0B//设置简易自检功能
#define SET_COMPLETE_SELF_CHECK  0X0C//设置全自检功能 
#define SET_DIP_SWITCH_ENABLE     0X0D//设置串口波特率使能
#define SET_UART_422_BAUD        0X0E//设置422串口波特率
#define SET_UART_485_BAUD        0X0F//设置485串口波特率
#define SET_HYT_ADDRESS          0X10//设置云台地址
#define SET_ERROR_compensation   0x11//设置误差补偿开关
  
//extern u_int8_t         g_local_ip[4];
////extern u_int8_t         g_local_mac[];
//extern u_int8_t         g_netmask[4];
//extern u_int8_t         g_gateway[4];
extern u_int8_t         g_web_ip[4];
extern u_int8_t         g_server_ip[4];
extern u_int8_t         g_device_web_ip[21];
  
//发布包文件头--------------------------------------------------------------------
typedef struct _hy_release_file_head{
        u_int32_t flag;                 // 4byte 标志，必须为"HYPK",如果文件不是以此开头，则认为是非法文件
        u_int32_t head_len;             //4byte	头长度,件头长度，包含"标志"
        u_int8_t file_type;             //1byte	文件类型，1,1：应用程序升级包 2:bootloader
        u_int16_t file_format_ver;      //2byte 文件格式版本号，1用于确定文件的处理方式
        u_int8_t hardware_type[2];      //2byte	硬件类型,升级包适用的硬件类型
        u_int16_t hardware_ver;         //2byte	硬件版本,升级包适用的硬件版本
        u_int32_t software_ver;         //4byte	软件版本,本升级包的应用软件版本
        u_int32_t data_len;             //4byte 数据长度,不包含2个字节的crc
        u_int32_t addr;                 //4byte 烧写地址
        u_int8_t head_checksum;         //1byte	头校验,以上内容的checksum，包括"标志"，单字节异或
        u_int8_t data[1];               //数据
}hy_release_file_head;

typedef struct _system_cfg_info{
   u_int8_t  software_version[28];//软件版本
   u_int8_t  hardware_version[14];//硬件版本
   u_int8_t  cmd_id[17];
   u_int8_t  component_id[17];
   
   u_int8_t simplify_self_check[15];//简易自检功能
   u_int8_t complete_self_check[15];//全自检功能

   u_int8_t dip_switch_enable[10];
   u_int8_t uart_422_baud[10];
   u_int8_t uart_485_baud[10];
   u_int8_t hyt_address[5];
   
   u_int8_t error_compensation[5];//设置误差补偿开关

   unsigned char add;
   int uart_422baud;
   int uart_485baud;
   
   u_int8_t  l;
}system_cfg_info;
extern system_cfg_info system_info;

#define RELEASE_PKG_HEAD_SIZE(x) ((char *)(&(x)->head_checksum) - (char *)(x) + sizeof(u_int8_t))//不包含数据
#define RELEASE_PKG_CRC16(x) (*(u_int16_t *)(&((x)->data[(x)->data_len])))
#define RELEASE_PKG_TOTAL_LEN(x) ((x)->data_len + RELEASE_PKG_HEAD_SIZE(x)+sizeof(u_int16_t))//总长度

//函数声明
BOOL get_system_config_from_file(void);
BOOL write_file_to_ext_flash(hy_release_file_head *pkg_head);
BOOL write_pack_head_to_ext_flash(hy_release_file_head *pkg_head);
BOOL app_crc_OK(u_int8_t file_type);
BOOL ext_flash_erase(hy_release_file_head *pkg_head);
u_int8_t upgrade_pkg_head_checksum_OK(hy_release_file_head *pkg_head);
BOOL pack_head_verify(u_int8_t file_type);
BOOL ptz_set_cfg(char *str, char type);
void erase_ext_flash(u_int8_t block_begin,u_int32_t total_len);
unsigned short int ptz_read_cfg_data(char* buff);
#pragma pack(pop)

#ifdef __cplusplus
}
#endif 
#endif //_CONFIG_INFO_H_