/*****************************************************************************
 * file    Core/Inc/dev_config.h 
 * author  YuLiang
 * version 1.0.0
 * date    13-Jun-2022
 * brief   This file provides all the headers of the dev_config functions.
 ******************************************************************************
 * Attention
 *
 * <h2><center>&copy; COPYRIGHT(c) 2019 WTOE</center></h2>
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *   1. Redistributions of source code must retain the above copyright notice,
 *      this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright notice,
 *      this list of conditions and the following disclaimer in the documentation
 *      and/or other materials provided with the distribution.
 *   3. Neither the name of WTOE nor the names of its contributors may be used to 
 *      endorse or promote products derived from this software without specific
 *      prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ******************************************************************************/

#ifndef __DEV_CONFIG_H__
#define __DEV_CONFIG_H__

/* Includes ------------------------------------------------------------------*/
#include "stm32l4xx_hal.h"

#include "common.h"
#include "flash_log.h"

/* Define --------------------------------------------------------------------*/
/* 版本信息. */
#define VERSION_MAJOR 128
#define VERSION_MINOR 25
#define COMPILE_TIME (__DATE__" "__TIME__)

#define DEV_CAPABILITY_LEN 16
#define DEV_CAPABILITY_BIT_LEN 128
#define DEV_PASSWORD_LEN 16
#define DEV_COMPILE_TIME_LEN 32
#define DEV_VERSION_STR_LEN 32
#define DEV_NAME_LEN 128
#define DEV_RESET_INFO_NUM 10
#define DEV_APN_LEN 16
#define DEV_APN_LEN_LONG 64

#define DEV_CONFIG_SIZE 1024
#define DEV_RECORD_SIZE 512

/* 默认串口密码. */
#define DEV_DEFAULT_PASSWORD "ld.admin"

/* Exported types ------------------------------------------------------------*/
/* 重启类型号. */
typedef enum
{
    RESET_WATCHDOG = 0,
    RESET_CLI,
    RESET_IAP,
    RESET_RESET,
    RESET_UPDATE,
    RESET_SET_DEFAULT_CLI,
    RESET_SET_DEFAULT
} RESET_INFO_E;

/* dev_config flag 类型号. */
typedef enum
{
    DEV_FLAG_CLI = 0,   // 是否开启命令行模式.
    DEV_FLAG_ADJ,       // 是否开启校准模式.
    DEV_FLAG_FACTORY    // 工厂持续采样模式.
} DEV_FLAG_E;

/* 能力集类型. */
typedef enum
{
    CAPABILITY_ = 0,
} CAPABILITY_SET_E;

/* 能力集类型. */
typedef enum
{
    DEV_TYPE_NONE = 0,
    DEV_TYPE_CT,        //CT取电
    DEV_TYPE_BAT,       //电池取电
    DEV_TYPE_ACDC,      //直流交流取电
    DEV_TYPE_CSG,       //南网
    DEV_TYPE_SCAP,      //超级电容
    DEV_TYPE_CT_P       //故障定位
} DEV_INFO_TYPE_E;

/* 时间信息.8byte. */
typedef struct
{
    uint16_t year;
    uint8_t mon;
    uint8_t day;
    uint8_t hour;
    uint8_t min;
    uint8_t sec;
    uint8_t state; /* 空开状态.用于断电报警,其他代码可做无效位. */
} dev_time_t;

/* 校准参数信息. */
typedef struct
{
    uint16_t ADCRatio[CFG_ADC_CH_CNT];
    uint32_t ADCBase_elec[CFG_ADC_CH_CNT][CFG_ADC_ADJ_CNT];
    uint16_t ADCAdj[CFG_ADC_CH_CNT][CFG_ADC_ADJ_CNT];
    float ADCBase[CFG_ADC_CH_CNT][CFG_ADC_ADJ_CNT];
    uint16_t vin;
    uint16_t vin_adj;
    uint8_t ADC_turns[CFG_ADC_CH_CNT];
} dev_adj_t;

/* 设备状态信息. */
typedef struct
{
    uint8_t energy_mode;                // 节能模式.
    uint8_t is_adc_collect;             // ADC 采集完成.
    uint8_t is_adc_up;                  // ADC 上传完成.
    uint8_t adci_cnt;                   // ADCi 错误计数.
    uint8_t adci_state;                 // ADCi 状态.
    uint8_t adc_cnt;                    // ADC 错误计数.
    uint8_t adc_state;                  // ADC 状态.
    uint8_t wave_state;                 // 高频录波状态.
    uint32_t wave_time;                 // 高频录波开始时间.
    uint8_t wave_start;                 // 高频录波完成.
    uint8_t wave_up_start;              // 高频录波上传完成.
    uint16_t wave_idx;                  // 高频录波续传索引.
    uint8_t wave_rt;                    // 高频录波结果.
    uint8_t wave_force;                 // 是否强制录波.
    uint8_t wl_state;                   // 4G 状态.
    uint8_t wl_cnt;                     // 4G 错误计数.
    uint16_t wl_wave_idx;               // 4G 上传录波文件索引.
    uint8_t is_wl_update;               // 4G 升级.
    uint8_t is_adc_thr;                 // 是否超过工频阈值.
    uint8_t force_sleep_up;             // 强制睡眠报文是否上传.
    uint8_t is_bat_charge;              // 电池充电状态.
    uint16_t wave_adj;                  // 高频校准系数.
    int16_t wave_min;                   // 高频采样最大值.
    int16_t wave_max;                   // 高频采样最小值.
    uint32_t sys_time;                  // 系统运行时间.
    uint16_t sc_max;                    // 超级电容最大值.
    uint16_t sc_min;                    // 超级电容最小值.
    uint32_t sc_max_time;               // 超级电容最大值时间.
    uint32_t sc_min_time;               // 超级电容最小值时间.
    uint32_t sc_up_time;                // 超级电容充电时间.
    uint32_t sc_down_time;              // 超级电容放电时间.
} dev_state_t;

/* 软件配置保存. */
typedef struct
{
    /* INIT_DONE_MAGIC. 4byte */
    uint32_t magic;
    /* 进入串口cli的密码. 16byte */
    uint8_t password[DEV_PASSWORD_LEN];
    /* 器件名字. 128byte */
    uint8_t host[DEV_NAME_LEN];
    /* ADC 校准. 418byte */
    uint16_t ADC_ratio[CFG_ADC_CH_CNT];
    uint32_t ADC_base_elec[CFG_ADC_CH_CNT][CFG_ADC_ADJ_CNT];
    uint16_t ADC_adj[CFG_ADC_CH_CNT][CFG_ADC_ADJ_CNT];
    float ADC_base[CFG_ADC_CH_CNT][CFG_ADC_ADJ_CNT];
    uint16_t vin_adj;
    /* 各种 bool 类型配置, 见 DEV_FLAG_E. 2byte */
    uint16_t flag;
    /* 8 路传感器 id 配置. 8byte */
    uint8_t sensor_id[SENSOR_SUM];
    /* 采样间隔配置. 4byte */
    uint16_t collect_interval;
    uint16_t wave_interval;
    /* ADCi 电压校准配置. 2byte */
    uint16_t vin;
    /* 工作模式配置. 2byte */
    uint8_t energy_mode;
    uint8_t is_voltage_col;
    /* 上传阈值. 8byte */
    uint16_t threshold;
    uint16_t wave_threshold;
    uint16_t wave_adj;
    uint8_t main_cable;
    uint8_t normal_sleep;
    uint8_t APN[DEV_APN_LEN];
    uint8_t is_temp_col;
    uint8_t is_wave_col;
    uint8_t csg_beat_interval;
    uint8_t reserve0[1];
    uint8_t ADC_turns[CFG_ADC_CH_CNT];
    uint8_t csg_ciphertext[CSG_CIPHERTEXT_LEN];
    uint8_t csg_passwd[8];
    uint16_t csg_sleep;
    uint16_t csg_online;
    uint8_t csg_reset_time[4];
    uint8_t APN_long[DEV_APN_LEN_LONG];
    /* 保证配置区 8K 的保留位. */
    uint8_t reserve[316];
    /* INIT_DONE_MAGIC. 4byte */
    uint32_t magic_bak;
} dev_config_t;

/* 信息记录保存. */
typedef struct
{
    /* UPDATE_MAGIC,INIT_DONE_MAGIC. 4byte */
    uint32_t magic;
    /* 重启信息保存. 84byte */
    dev_time_t reset_info[DEV_RESET_INFO_NUM];
    uint8_t reset_index;
    uint8_t reset_type;
    /* 保证配置区 512byte 的保留位. */
    uint8_t reserve[420];
    /* INIT_DONE_MAGIC. 4byte */
    uint32_t magic_bak;
} dev_record_t;

/* 器件信息保存. */
typedef struct
{
    /* INIT_DONE_MAGIC. 4byte */
    uint32_t magic;
    /* 设备信息. 36byte */
    uint8_t type_m;
    uint8_t type_s;
    uint8_t mac[6];
    uint8_t id[4];
    uint8_t ip[4];
    uint8_t ip_mask[4];
    uint8_t ip_gw[4];
    uint8_t server_ip[4];
    uint8_t wireless_server_ip[4];
    uint16_t server_port;
    uint16_t wireless_server_port;
    /* 版本信息. 224byte */
    uint8_t boot_version[DEV_VERSION_STR_LEN];
    uint8_t boot_compile_time[DEV_COMPILE_TIME_LEN];
    uint8_t img_version[DEV_VERSION_STR_LEN];
    uint8_t img_compile_time[DEV_COMPILE_TIME_LEN];
    uint8_t hardware_version[DEV_VERSION_STR_LEN];
    uint8_t FPGA_version[DEV_VERSION_STR_LEN];
    uint8_t factory_time[DEV_COMPILE_TIME_LEN];
    /* 内部flash和spi flash中img的大小. 32byte */
    uint32_t fireware_size;
    uint32_t spi_fireware_size;
    uint8_t update_ip_addr[4];
    uint16_t update_port;
    uint16_t csg_version;
    unsigned char capability_set[DEV_CAPABILITY_LEN];
    uint8_t csg_id[8];
    uint8_t csg_card[8];
    /* 保留空间,以便以后添加成员的时候不用再升级IAP程序. */
    uint8_t reserved[196];
    /* INIT_DONE_MAGIC. 4byte */
    uint32_t magic_bak;
} dev_info_t;

/* Exported macro ------------------------------------------------------------*/    
/* Extern global variables ---------------------------------------------------*/
extern dev_config_t dev_config;
extern dev_record_t dev_record;
extern dev_info_t dev_info;
extern st_date_t st_data;

/* Extern functions ----------------------------------------------------------*/
extern void dev_config_init_os(void);
extern void dev_config_init(void);
extern void dev_config_host_name_set(const uint8_t *str);
extern void dev_config_password_set(const uint8_t *str);
extern void dev_config_flag_assgin(uint16_t flag);
extern void dev_config_flag_set(uint8_t flag_bit);
extern void dev_config_flag_unset(uint8_t flag_bit);
extern void dev_config_collect_interval_set(uint16_t interval);
extern void dev_config_wave_interval_set(uint16_t interval);
extern void dev_config_defaults_set(void);
extern HAL_StatusTypeDef dev_config_save(void);

extern void dev_record_reset_type_set(uint8_t type);
extern HAL_StatusTypeDef dev_record_save(void);

extern void dev_info_ip_set(uint8_t *ip, uint8_t *mask, uint8_t *gw);
extern void dev_info_server_ip_set(uint8_t *ip);
extern void dev_info_type_set(uint8_t id_m, uint8_t id_s);
extern void dev_info_mac_set(uint8_t *mac);
extern void dev_info_id_set(uint32_t id);
extern void dev_info_wireless_server_ip_set(uint8_t *ip);
extern void dev_info_capability_set(uint8_t *data);
extern void dev_info_capability_add(uint8_t cap);
extern void dev_info_capability_del(uint8_t cap);
extern void dev_info_capability_remove(void);
extern void dev_info_fireware_size_set(uint32_t size);
extern void dev_info_update_ip_set(uint8_t *ip, uint16_t port);
extern void dev_info_magic_set(uint32_t magic);
extern HAL_StatusTypeDef dev_info_save(void);
extern void dev_info_print(void);
extern void dev_csg_print(void);
#endif
/******************* (C) COPYRIGHT LandPower ***** END OF FILE ****************/
