/*****************************************************************************
 * file    include/pd_dbg.h 
 * author  YuLiang
 * version 1.0.0
 * date    01-June-2023
 * brief   This file provides all the headers of the debug server functions.
 ******************************************************************************
 * Attention
 *
 * <h2><center>&copy; COPYRIGHT(c) 2021 LandPower</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 LandPower 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 __PD_DBG_H__
#define __PD_DBG_H__

#ifdef CFG_DEV_TYPE_LAND_PD
/* Includes ------------------------------------------------------------------*/
#include "pd_main.h"

/* Define --------------------------------------------------------------------*/
#define DEBUG_DAU_FILE_UPGRADE "/lib/firmware/system_wrapper.bin.upgrade"
#define DEBUG_DAU_FILE_BAK "./bak/system_wrapper_bak.bin.bak"
#define DEBUG_DAU_FILE "/lib/firmware/system_wrapper.bin"


#define DEBUG_BUG_SIZE 1512
#define DEBUG_CJSON_BUG_SIZE 1512

//#define DEBUG_NOISE_CAREFOR             (0x0007)    // 关注对应通道的底噪原始值, 校准底噪值.
//#define DEBUG_NOISE_POST                (0x0008)    // 获取底噪原始值, 校准底噪值  服务端主动提交每秒刷新一次.

enum DebugCommand
{
    DEBUG_CONFIG_FACTORY_GET = 1,   /* 获取配置 */
    DEBUG_CONFIG_FACTORY_SET = 2,   /* 设置配置 */
    DEBUG_CONFIG_GLOBAL_GET = 3,    /* 设备全局参数获取 */
    DEBUG_CONFIG_GLOBAL_SET = 4,    /* 设备全局参数设置 */
    DEBUG_CONFIG_PORT_GET = 5,      /* 设备端口参数获取 */
    DEBUG_CONFIG_PORT_SET = 6,      /* 设备端口参数设置 */
    DEBUG_UPGRADE_DATA = 7,         /* 固件升级 */
    DEBUG_DEVICE_STATUS = 8,        /* 查询设备状态 */
    DEBUG_TIME_SET = 9,             /* 对时 */ 
    DEBUG_REBOOT = 10,              /* 重启 */
    DEBUG_SET_WAVE_ADDRESS = 11,    /* 设置波形地址 */
};

#define BITMAP_SAVE_FILE                (1 << 0)
#define BITMAP_IP_CHANGE                (1 << 1)

#define DEBUG_MANAGE_TOOL_PORT          (10050)
/* Exported types ------------------------------------------------------------*/

/* 设备状态 */
typedef struct 
{
    uint32_t utc;                   // 系统当前时间
    uint32_t run_time;              // 运行时间
    uint8_t is_server_link;         // 后台连接状态
    uint8_t reserved[7];
} debug_pkt_status_t;

/* 调试工具全局结构 */
typedef struct{
    int fd;                                 // TCP server 监听使用的 socket.
    int fd_client;                          // TCP client 通讯使用的 socket.
    char buf[DEBUG_BUG_SIZE];               // 通讯使用收法包 buffer.
    char buf_post[DEBUG_BUG_SIZE];          // 主动上传通讯使用收法包 buffer.
    int is_manual_col;                      // 是否手动采集.
} debug_ctrl_t;

/* 报文头结构. */
#pragma pack(push, 1)
typedef struct{
    uint16_t head;                          // 0x55aa.
    uint32_t len;
    uint8_t dev_type_m;
    uint8_t dev_type_s;
    uint16_t dev_id;
    uint8_t cmd_type;
    uint8_t cmd;
    uint16_t pkt_id;
    uint8_t reserve[18];
} dbg_pkt_head_t;
#pragma pack(pop)

/* 基础信息 */
typedef struct{
    uint8_t type_m;                  // 主设备号
    uint8_t type_s;                 // 次设备号
    uint8_t reserved1[2];           // 保留
    uint32_t dev_id;                // 设备ID
    char hostname[FILE_NAME_LEN];   // 设备名 128byte
    uint32_t factory_date;          // 出厂日期.
    uint32_t deployment_date;       // 部署日期.
    uint8_t app_version[32];        // 软件版本
    uint8_t app_compile_time[32];   // 软件编译时间
    uint8_t hardware_version[32];   // 硬件版本
    uint8_t FPGA_version[32];       // fpga版本
    uint32_t ip;                    // 本机（服务器） IP.
    uint32_t mask;                  // 本机（服务器） MASK.
    
    uint32_t gw;                    // 本机（服务器）网关
    uint8_t mac[6];                 // MAC地址.
    uint16_t csg_port;           // 后台端口号.
    
    uint32_t csg_ipv4;           // 后台  IP.
    uint32_t running_time;          //运行时间
} dbg_device_info_t;

/* 全局配置 */
typedef struct{
    uint32_t sample_frequency;                      //采样频率  MHz
    uint32_t trigger_sample_numbers;                //触发采样长度  us
    uint32_t pre_trigger_percent;                   //预触发百分比 0-100%
    uint32_t trigLevel;                             //触发电平  mv
    uint32_t trend_up_period;                       //趋势上升周期
    uint16_t heartbeat_period;                      //心跳包周期 s
    uint8_t ch_en_mask;                             //通道使能  bit0-7对应ch1-ch8
    uint8_t sync_mode;                              //同步模式  0-外同步 1-内同步
    uint32_t pt_internal_period;                    //内同步频率 40~300
    uint8_t reserved[8];                            //预留
} dbg_global_config_t;

/* 端口配置. */
typedef struct{
    uint8_t vport;                  // 通道编号.
    uint8_t channel_type;           // 通道类型.
    uint8_t reserved1[2];
    uint32_t filter_frequency;      // 过滤频率(32位按位设置)
    int16_t rise_time;              // 上升时间(10%到90%峰值)
    int16_t peak_time;              // 峰值时间
    int16_t fall_time;              // 下降时间(90%到10%峰值)
    int16_t pulse_width;            // 脉冲宽度
    int16_t peak_count;             // 波峰数量
    int16_t reserved2;              // 保留字段(2字节)
    int32_t signal_envelope;        // 信号包络面
    float signal_mean;              // 信号平均值
    float signal_variance;          // 信号方差值
    int32_t primary_frequency;      // 第一主频
    int16_t primary_freq_peak;      // 第一主频峰值
    int16_t spectral_peak_count;    // 谱峰个数
    float spectrum_mean;            // 频谱均值
    float spectrum_variance;        // 频谱方差值
    uint8_t reserved3[32];          // 预留字段(32字节)
} dbg_config_port_t;

/* 升级文件包结构体 */
typedef struct
{
    uint8_t type;               // 升级类型
    uint8_t resverd[3];
    uint16_t index;             // 报文索引
    uint16_t sum;               // 总包数.
    uint32_t len;               // 数据包长度.
} dbg_upgrade_data_t;

/* 应答升级结构体 */
typedef struct
{
    uint16_t index;             // 应答包序号.
} dbg_upgrade_ack_t;

typedef struct
{
    char ipv4[16];           // wave  IP.
    uint16_t port;           // wave port.
} dbg_wave_addr_t;

/* Exported macro ------------------------------------------------------------*/

/* Extern global variables ---------------------------------------------------*/
extern debug_ctrl_t debug_ctrl;

/* Extern functions ----------------------------------------------------------*/
extern int32_t debug_handle_init(void);
extern int32_t debug_handle_init_after(void);
extern int32_t debug_pkt_state_post(void);
extern void debug_upgrade_result_send(int32_t rv, char *buf);
#endif
#endif
/************************ (C) COPYRIGHT LandPower ***** END OF FILE ****/
