You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

385 lines
16 KiB
C

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/******************************************************************************
* file include/pd_csg.h
* author YuLiang
* version 1.0.0
* date 21-Feb-2023
* brief This file provides all the headers of the csg 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_CSG_H_
#define _PD_CSG_H_
#ifdef CFG_DEV_TYPE_LAND_PD
/* Includes ------------------------------------------------------------------*/
#include "pd_main.h"
#include <semaphore.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/* Define --------------------------------------------------------------------*/
#define CSG_HEAD_LEN (32)
#define CSG_TOTLE_LEN (26)
#define CSG_PKT_LEN (1300)
#define MAX_FILES (128)
#define MAX_PATH_LEN (256)
#define THRESHOLD_MS 10 // 时间差阈值
#define UDP_SEND_TIMEOUT (2)
#define CSG_SEND_ERR_CNT (3)
/* 配置文件结构体 */
#define FILE_FIFO_PATH_LEN 256
/* 命令类型. */
enum CSG_CMD_TYPE
{
CSG_REQUEST = 1,
CSG_REPLY = 2,
CSG_PRV_REQUEST = 121,
CSG_PRV_REPLY = 122
};
/* 共有命令字. */
enum CSG_CMD
{
CSG_C_CONTACT = 1,
CSG_C_ADD_DAU = 2,
CSG_C_RESET = 3,
CSG_C_UPDATE = 5,
CSG_C_DEV_INFO_SET = 6, /* 设备基本信息设置 */
CSG_C_DEV_INFO_GET = 7, /* 设备基本信息获取 */
CSG_C_UPDATE_RESULT = 9,
CSG_C_HEARTBEAT = 10,
CSG_C_MAX
};
/* 私有命令字. */
enum DEBUG_CM_CMD
{
CSG_PRV_CONFIG_GLOBAL_SET = 1, /* 设备全局参数设置 */
CSG_PRV_CONFIG_GLOBAL_GET = 2, /* 设备全局参数获取 */
CSG_PRV_CONFIG_PORT_SET = 3, /* 设备端口参数设置 */
CSG_PRV_CONFIG_PORT_GET = 4, /* 设备端口参数获取 */
CSG_PRV_CONFIG_REAL_WAVE = 5, /* 实时波形配置 */
CSG_PRV_TREND = 10,
CSG_PRV_REAL_PRPS = 11,
CSG_PRV_EVENT = 12
};
// 定义命令字常量
typedef enum {
//CMD_CONTACT = 0x00,
CMD_INVALID
} command_type;
// 定义函数指针类型
typedef void (*command_handler)(int slot, char *data);
// 定义命令结构体
typedef struct {
command_type cmd; // 命令字
command_handler handler; // 处理函数
} command_entry;
/* Exported types ------------------------------------------------------------*/
/* 设备配置的滤波类型. */
typedef enum
{
CSG_FILTER_TYPE_FF = 1,
CSG_FILTER_TYPE_FR,
CSG_FILTER_TYPE_LF,
CSG_FILTER_TYPE_HF
} CSG_FILTER_TYPE_E;
typedef struct {
uint32_t port; /* 端口号 0 ~ 7 */
uint32_t length; /* 端口数据长度 */
}port_info_t;
/* . */
typedef struct
{
int skfd; // 后台通讯使用的 socket.
uint32_t pkt_index; // 报文索引.
char buf_send[1500];
char buf_recv[1500];
char event_booster_buf[1500];
char trend_booster_buf[1500];
struct sockaddr_in server;
int32_t server_ip; // server ip.
uint16_t server_port; // server port.
uint8_t is_connect; // 是否连接上服务器.
int32_t communication_time; // 最后通讯时间.
time_t heartbeat_timeout;
time_t heartbeat_timeout_cnt;
pthread_mutex_t mutex;
pthread_mutex_t lock;
} csg_t;
/* 报文头结构. */
typedef struct{
uint16_t len;
uint8_t dev_type_m;
uint8_t dev_type_s;
uint32_t dev_id;
uint8_t cmd_type;
uint8_t cmd;
uint16_t pkt_id;
uint8_t version;
uint8_t reserve1[2];
uint8_t slot; // 槽位
uint32_t sdev_id; // 从设备id
uint8_t reserve2[12];
} csg_pkt_head_t;
typedef struct {
uint8_t result; // 应答结果. 0:成功 1:失败
uint8_t reserved[3]; // 保留
}csg_ack_t;
/* 心跳报文. */
typedef struct
{
float freq; // 实测同步源工频频率.
uint8_t dau_state[4]; // 采集模块的状态.
uint8_t out_sync; // 外接调频同步 1: 有效 0: 无效.
uint8_t pt_sync; // PT同步 1: 有效 0: 无效.
uint8_t in_sync; // 内同步 1: 有效 0: 无效.
uint8_t reserved1; // 预留 字节对齐.
uint8_t port_link_alarm[PD_PORT_SUM]; // 通道传感器连接状态 断线告警.
uint8_t dau_port_num[4]; // 采集板端口数量.
} csg_heartbeat_t;
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 server_port; // 服务器端口号.
uint32_t server_ipv4; // 服务器 IP.
} csg_dev_info_t;
typedef struct{
uint16_t power_frequency; // 工频频率, 单位: 0.1Hz
uint16_t trend_period; // 趋势数据上送周期, 单位: 秒.
uint8_t sync_mode; // 同步方式 1: PT 同步 2: 内同步(默认) 3: 外接信号同步.
uint8_t heartbeat_period; // 心跳包周期, 单位: 分钟.
uint8_t pps_mode; // pps 主从模式 PD_PPS_XXX.
uint8_t protocol_type; // 0:朗德协议 1:南网协议
uint16_t trend_storage; //趋势存储文件数量阈值
uint16_t event_storage; //事件存储文件数量阈值
} csg_config_global_t;
typedef struct {
uint8_t vport; // 通道编号.
uint8_t port_type; // 采集通道类型 , 1 表示特高频局放 2 表示超声局放 3 表示 TEV 4 表示高频.
uint8_t filter; // 滤波器类型 1: 低频段 2: 全频段 3: 窄频段 4: 高频段
uint8_t sensor_type; // 0: 无配置; 1: UHF信号传感器; 2: UHF噪声传感器 ; 3: UHF信号传感器, 关联噪声降噪.
uint8_t is_auto_noise; // 是否自动调整降噪等级.
uint8_t denoise_type; // 0-无配置 1-自动 2-手动降噪
uint16_t denoise_variance; // 方差降噪系数, 单位: 1%
uint32_t event_counter_h; // 事件次数阀值高.
uint16_t event_sec_h; // 事件每秒次数阀值高.
uint16_t event_thr_h; // 事件值阈值高.
uint32_t event_counter_thr_h; // 事件值阈值高的次数.
uint32_t event_counter_l; // 事件次数阀值低.
uint16_t event_sec_l; // 事件每秒次数阀值低.
uint16_t event_thr_l; // 事件值阈值低.
uint32_t event_counter_thr_l; // 事件值阈值低的次数.
uint8_t burst_time; // 事件突发计算时间
uint8_t reserved1[1];
uint16_t burst_thr; // 事件突发阈值
int16_t denoise_manual; // 手动底噪等级
int16_t denoise_auto; // 自动降噪水平
}csg_config_port_t;
typedef struct
{
uint8_t vport; // 通道编号 1 ~ 8
uint8_t is_concern; // 关注 1: 关注 0: 取消关注
uint8_t filter; // 滤波器类型
uint8_t denoise_correlation; // 关联降噪 1: 关联噪声去噪
uint8_t denoise_type; // 0-无配置 1-自动 2-手动降噪
uint8_t reserved; // 保留
uint16_t denoise_manual; // 手动降噪等级
} csg_real_image_get_t;
/* 实时图谱报文头 28Byte. */
typedef struct{
uint16_t index; // 每10包传完之后自动自加用于区分当前包结束.
uint8_t pkt_sum; // 总包数 10.
uint8_t pkt_index; // 当前包数 0 ~ 9.
uint16_t fre_cnt; // 工频周期计数.
uint8_t vport; // 通道编号.
uint8_t reserved1[1];
int16_t max;
int16_t avg;
uint32_t cnt;
uint32_t utc;
float freq; // 频率.
uint32_t len;
} csg_real_image_t;
typedef struct{
uint16_t index; // 包索引
uint16_t sum; // 总包数
uint8_t type; // 数据类型 0prpd 1:原始波形 210秒prps 3:统计数据
uint8_t vport; // 通道号
uint8_t boosterpack; // 是否补包 0否 1
uint8_t reserved[1]; // 保留
uint32_t identifier; // 数据编号
uint32_t utc; // 同步时间
uint32_t len; // 当前包长度
} csg_trend_t;
typedef struct{
uint16_t data_cnt; // 数据计数.
int16_t max; // 通道的最大值.
uint16_t reserved; // 保留
uint16_t avg; // 通道的平均值.
uint32_t cnt; // 通道的计数值.
uint32_t phase; // 放电相位 .
uint32_t noise; // 趋势数据中的底噪值: 单位 dBm .
uint32_t event_cnt; // 趋势数据中的的事件数量记录.
} csg_trend_stat;
typedef struct {
uint16_t index; // 包索引
uint16_t sum; // 总包数
uint8_t vport; // 通道号
uint8_t boosterpack; // 是否补包 0否 1
int16_t max; // 通道的最大值.
uint32_t power_fre; // 工频周期.
uint8_t type; // 事件类型.
uint8_t reserved[3]; // 保留
uint32_t identifier; // 数据编号: 0 - (2^32-1) 循环.
uint32_t utc; // UTC 时标.
uint32_t cnt; // 通道每秒脉冲计数值.
uint16_t avg_o; // 通道原始信号 (降噪前) 的平均值.
uint16_t avg; // 脉冲平均值.
uint32_t point_cnt; // 数据累计点数
uint32_t len; // 当前包长度
} csg_event_t;
/* 升级文件包结构体 */
typedef struct {
uint8_t type; // 升级类型
uint8_t resverd[3];
uint16_t index; // 报文索引
uint16_t sum; // 总包数.
uint32_t len; // 数据包长度.
} csg_upgrade_data_t;
/* 应答升级结构体 */
typedef struct {
uint16_t index; // 应答包序号.
uint8_t result; // 应答结果. 0:成功 1:失败
uint8_t reserve; // 保留
} upgrade_ack_t;
/* 升级结果通知 */
typedef struct
{
uint8_t result; // 升级结果
uint8_t reserved[3];
char context[128];
} upgrade_res_t;
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 server_port; // 服务器端口号.
uint32_t server_ipv4; // 服务器 IP.
uint8_t port[8];
uint8_t port_type[8];
} csg_contact_t;
typedef struct
{
uint32_t dev_id;
uint8_t slot;
uint8_t status;
uint8_t reserved[2];
uint8_t port[8];
uint8_t port_type[8];
} csg_add_dau_t;
typedef struct
{
uint32_t dev_id;
uint8_t slot;
uint8_t reserved[2];
uint8_t result;
} csg_add_dau_ack_t;
/* Exported macro ------------------------------------------------------------*/
/* Extern global variables ---------------------------------------------------*/
extern csg_t csg;
/* Extern functions ----------------------------------------------------------*/
extern int32_t _csg_pkt_check(char *pkt);
extern int32_t csg_handle_init(void);
extern int32_t csg_handle_init_after(void);
extern void csg_upgrade_result_send(int32_t rv, char *buf);
extern void _print_sockaddr_in(const struct sockaddr_in *addr);
extern command_handler _csg_get_table_handle(command_entry *ptable, command_type cmd);
extern void _csg_send_data(uint8_t cmd_type, uint8_t cmd, char *pkt, int32_t len);
#endif
#endif
/************************ (C) COPYRIGHT LandPower ***** END OF FILE ****************/