/*****************************************************************************
 * file    include/pd_main.h 
 * author  YuLiang
 * version 1.0.0
 * date    07-Feb-2023
 * brief   This file provides all the headers of the partial discharge 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_MAIN_H__
#define __PD_MAIN_H__

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

/* Define --------------------------------------------------------------------*/
#define PD_DAU_SUM 1
#define PD_DAU_PORT_SUM 8
#define PD_PORT_PROMPT_LEN 64                   // DAU 端口节点前标长度.

#define PD_DEV_NUM_LEN 16
#define PD_DEV_TYPE_LEN 8

#define PD_DAU_5GAD_DATA_NUM 20000

#define HARDWARE_VERSON     "DAU5G_1.0"

#define PD_BIT_PT       0
#define PD_BIT_BCODE    1 

/* Exported types ------------------------------------------------------------*/
/* 用于命令行模式节点注册配置保存函数 */
typedef int pd_port_cmd_save_config_f(vty_t*, uint8_t, uint8_t);

/* 端口类型. */
typedef enum
{
    PD_PORT_TYPE_UHF = 1,
    PD_PORT_TYPE_COUNT
} PD_PORT_TYPE_E;

/* 端口节点配置优先级 */
typedef enum
{
    PD_PORT_CMD_PRI_DAU = 0,
    PD_PORT_CMD_PRI_COUNT
} PD_PORT_CMD_PRI_E;

/* 端口状态. */
typedef enum
{
    PD_PORT_DISABLE = 0,
    PD_PORT_ENABLE ,
    PD_PORT_STATUS_COUNT
} PD_PORT_STATUS_E;

/* 向服务器发送消息的类型. */
typedef enum
{
    PD_SEND_TYPE_ORIGINAL_DATA = 1,
    PD_SEND_TYPE_TREND = 2,
} PD_SEND_TYPE_E;

typedef struct {
    uint32_t interrupt_count;                     // 通道中断计数
    uint32_t single_frame_length;                 // 单帧波形数据长度
    uint32_t frame_numbers;                       // 帧数
    uint32_t century_second;                      // 采样时间世纪秒
    uint32_t nano_second;                         // 纳秒
    uint32_t total_pulse_nums;                    // 总放电次数
    uint32_t data_vaild;                          // 是否有波形数据
    int32_t ground_noise;                         // 底噪
    char *original_pointer;                       // 原始波形数据存放位置
} pd_original_port_t;

/* 趋势数据结构体 */
typedef struct
{
    uint32_t port;
    uint32_t century_second;
    uint32_t pulse_num;
    int16_t pulse_max;
    int32_t pulse_avg;
    uint32_t noise;
    uint16_t phase;
} pd_trend_port_t;

typedef struct
{
    pd_trend_port_t port[PD_DAU_PORT_SUM];
} pd_trend_t;

typedef struct {
    pd_original_port_t pd_original_port[PD_DAU_PORT_SUM];    // 8个通道的数据
    //pd_trend_t *trend;
    pd_trend_port_t *trend_port[PD_DAU_PORT_SUM];
} pd_data_t;

typedef struct {
    uint32_t type;
    void *data;
} pd_csg_msg_t;

typedef struct{
    uint32_t trig_gap;                              //阈值触发间隔 us
    uint32_t interrupt_interval;                    //中断传输间隔 us
    uint32_t sample_frequency;                      //采样频率  MHz
    uint32_t trigger_sample_nums;                   //触发采样点数
    uint32_t trig_location;                         //预触位置
    uint32_t trig_threshold;                        //触发电平  mv
    uint32_t irigB_polarity;                        //irigB极性
    uint32_t pt_B_sync_mode;                        //pt和B码同步模式bit0-1
    uint32_t pt_internal_period;                    //pt内同步周期
    uint32_t trend_up_period;                       //趋势上升周期
    uint16_t heartbeat_period;                      //心跳包周期 s
    uint16_t ch_en_mask;                            //通道使能  bit0-7对应ch1-ch8
} pd_config_global_t;

/* 端口配置. */
typedef struct {
    uint8_t vport;                                  // 通道编号.
    uint8_t is_enbale;                              // 通道是否使能
    uint8_t type;                                   // 通道类型.
    uint8_t send_port_num;                          // 发送通道编号.
    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字节)
} pd_config_port_t;

typedef struct {
    pd_config_global_t config;                                      // 全局配置
    pd_config_port_t config_port[PD_DAU_SUM][PD_DAU_PORT_SUM];      // 端口配置
} pd_config_t;

typedef struct
{
    uint8_t state;
    uint8_t sync;
} pd_state_t;

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

/* Extern global variables ---------------------------------------------------*/
extern pd_data_t pd_data;
extern pd_config_t pd_config;
extern pd_state_t pd_state;
extern cmd_node_t pd_port_node;

/* Extern functions ----------------------------------------------------------*/
extern int32_t pd_main(void);
extern int32_t pd_main_after(void);
extern int32_t pd_port_cmd_config_register(int32_t pri, pd_port_cmd_save_config_f *func);
extern void pd_show(void);
extern uint32_t _pd_get_frequency();
#endif
#endif
/************************ (C) COPYRIGHT LandPower ***** END OF FILE ****/
