ADD 1.增加端口参数设置与获取

main
wangbo 1 month ago
parent 85b7d9e50b
commit efbc575beb

@ -371,9 +371,10 @@ typedef enum {
NOISE_CHANNEL = 2 // 噪声通道 NOISE_CHANNEL = 2 // 噪声通道
} ChannelType; } ChannelType;
//#pragma pack(push, 1)
typedef struct{ typedef struct{
uint8_t vport; // 通道编号. uint8_t vport; // 通道编号.
ChannelType channel_type; // 通道类型. uint8_t channel_type; // 通道类型.
uint8_t reserved1[2]; uint8_t reserved1[2];
uint32_t filter_frequency; // 过滤频率(32位按位设置) uint32_t filter_frequency; // 过滤频率(32位按位设置)
int16_t rise_time; // 上升时间(10%到90%峰值) int16_t rise_time; // 上升时间(10%到90%峰值)
@ -392,6 +393,7 @@ typedef struct{
float spectrum_variance; // 频谱方差值 float spectrum_variance; // 频谱方差值
uint8_t reserved3[32]; // 预留字段(32字节) uint8_t reserved3[32]; // 预留字段(32字节)
} csg_config_port_t; } csg_config_port_t;
//#pragma pack(pop)
typedef struct { typedef struct {
uint8_t vport; // 通道编号. uint8_t vport; // 通道编号.

@ -0,0 +1,213 @@
/*****************************************************************************
* 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_SET = 3, /* 设备全局参数设置 */
DEBUG_CONFIG_GLOBAL_GET = 4, /* 设备全局参数获取 */
DEBUG_CONFIG_PORT_SET = 5, /* 设备端口参数设置 */
DEBUG_CONFIG_PORT_GET = 6, /* 设备端口参数获取 */
DEBUG_REBOOT = 7, // 重启
DEBUG_TIME_SET = 8, // 对时
DEBUG_ADJSUT_COEFFICIENT_GET = 9, // 获取校准系数
DEBUG_ADJSUT_COEFFICIENT_SET = 10, // 设置校准系数
DEBUG_NOISE_POST = 11, // 自动获取采样值
DEBUG_NOISE_CAREFOR = 12, // 使能自动获取采样值功能
DEBUG_DEVICE_STATUS = 13, // 查询设备状态后台连接、4G连接
DEBUG_ARM_UPGRADE = 0x0050, // ARM 升级
DEBUG_FPGA1_UPGRADE = 0x0051, // FPGA 板卡升级
DEBUG_UPGRADE_ALL = 0x0056 // ARM 和 FPGA 板卡升级
};
#define BITMAP_SAVE_FILE (1 << 0)
#define BITMAP_IP_CHANGE (1 << 1)
#define DEBUG_MANAGE_TOOL_PORT (10050)
/* Exported types ------------------------------------------------------------*/
/* 设备状态. */
typedef struct {
uint32_t idx; // 编号.
uint32_t UTC_TimeScale; // UTC 时标
float F50Hz_Frequency; // 同步 50Hz 频率.
uint8_t F50Hz_SynStatus; // 同步状态.
uint8_t dau_status; // 插件状态.
uint16_t sensor_status; // 传感器断线状态.
uint16_t is_server_link; // 是否连接上后台.
uint16_t version; // 软件版本
uint32_t communication_time; // 上一次通讯的时间
uint32_t run_time; // 设备运行时间
} debug_pkt_status_t;
/* 报文头. */
typedef struct{
uint16_t head; // 0x55aa.
uint16_t cmd; // 命令.
uint32_t len; // 包长.
} debug_pkt_head_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;
/* 报文头结构. */
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;
/* 基础信息 */
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;
/* 全局配置 */
#pragma pack(push, 1)
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 reserved[1];
uint16_t trend_storage; // 趋势存储文件数量阈值.
uint16_t event_storage; // 事件存储文件数量阈值
} dbg_global_config_t;
#pragma pack(pop)
/* 端口配置. */
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; // 自动降噪水平
}dbg_port_config_t;
/* 设备状态. */
typedef struct {
uint8_t csg_connect_status; // 后台连接状态.
uint8_t ec20_connect_status; // 4g模块连接状态
} debug_pkt_dev_connect_status_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_port_state_post(void);
extern void debug_upgrade_result_send(int32_t rv, char *buf);
#endif
#endif
/************************ (C) COPYRIGHT LandPower ***** END OF FILE ****/

@ -149,6 +149,22 @@ typedef struct {
uint8_t is_enbale; // 通道是否使能 uint8_t is_enbale; // 通道是否使能
uint8_t type; // 通道类型. uint8_t type; // 通道类型.
uint8_t send_port_num; // 发送通道编号. 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; } pd_config_port_t;
typedef struct { typedef struct {

@ -66,6 +66,7 @@ enum PD_UPG_TYPE
enum PD_UPG_FROM enum PD_UPG_FROM
{ {
PD_UPG_FROM_CSG, PD_UPG_FROM_CSG,
PD_UPG_FROM_DBG,
PD_UPG_FROM_MAX, PD_UPG_FROM_MAX,
}; };

@ -1136,6 +1136,20 @@ int32_t _csg_port_config_set_recv(char *pkt)
pd_config.config_port[unit][port].vport = pnet->vport; pd_config.config_port[unit][port].vport = pnet->vport;
pd_config.config_port[unit][port].type = pnet->channel_type; pd_config.config_port[unit][port].type = pnet->channel_type;
pd_config.config_port[unit][port].filter_frequency = pnet->filter_frequency;
pd_config.config_port[unit][port].rise_time = pnet->rise_time;
pd_config.config_port[unit][port].peak_time = pnet->peak_time;
pd_config.config_port[unit][port].fall_time = pnet->fall_time;
pd_config.config_port[unit][port].pulse_width = pnet->pulse_width;
pd_config.config_port[unit][port].peak_count = pnet->peak_count;
pd_config.config_port[unit][port].signal_envelope = pnet->signal_envelope;
pd_config.config_port[unit][port].signal_mean = pnet->signal_mean;
pd_config.config_port[unit][port].signal_variance = pnet->signal_variance;
pd_config.config_port[unit][port].primary_frequency = pnet->primary_frequency;
pd_config.config_port[unit][port].primary_freq_peak = pnet->primary_freq_peak;
pd_config.config_port[unit][port].spectral_peak_count = pnet->spectral_peak_count;
pd_config.config_port[unit][port].spectrum_mean = pnet->spectrum_mean;
pd_config.config_port[unit][port].spectrum_variance = pnet->spectrum_variance;
vtysh_config_save(); vtysh_config_save();
@ -1162,6 +1176,22 @@ int32_t _csg_port_config_get_recv(char *pkt)
pnet->vport = pd_config.config_port[unit][port].vport; pnet->vport = pd_config.config_port[unit][port].vport;
pnet->channel_type = pd_config.config_port[unit][port].type; pnet->channel_type = pd_config.config_port[unit][port].type;
pnet->vport = pd_config.config_port[unit][port].vport;
pnet->channel_type = pd_config.config_port[unit][port].type;
pnet->filter_frequency = pd_config.config_port[unit][port].filter_frequency;
pnet->rise_time = pd_config.config_port[unit][port].rise_time;
pnet->peak_time = pd_config.config_port[unit][port].peak_time;
pnet->fall_time = pd_config.config_port[unit][port].fall_time;
pnet->pulse_width = pd_config.config_port[unit][port].pulse_width;
pnet->peak_count = pd_config.config_port[unit][port].peak_count;
pnet->signal_envelope = pd_config.config_port[unit][port].signal_envelope;
pnet->signal_mean = pd_config.config_port[unit][port].signal_mean;
pnet->signal_variance = pd_config.config_port[unit][port].signal_variance;
pnet->primary_frequency = pd_config.config_port[unit][port].primary_frequency;
pnet->primary_freq_peak = pd_config.config_port[unit][port].primary_freq_peak;
pnet->spectral_peak_count = pd_config.config_port[unit][port].spectral_peak_count;
pnet->spectrum_mean = pd_config.config_port[unit][port].spectrum_mean;
pnet->spectrum_variance = pd_config.config_port[unit][port].spectrum_variance;
_csg_send_data(CSG_PRV_REPLY, head->cmd, pkt, sizeof(csg_config_port_t)); _csg_send_data(CSG_PRV_REPLY, head->cmd, pkt, sizeof(csg_config_port_t));
return E_NONE; return E_NONE;
} }

@ -160,7 +160,7 @@ CMD(show_dau_status_all,
/* 端口使能配置. */ /* 端口使能配置. */
CMD(pd_port_status, CMD(pd_port_status,
pd_port_status_cmd, pd_port_status_cmd,
"port status (enable|disable)", "port-status (enable|disable)",
"Port\n" "Port\n"
"Status\n" "Status\n"
"enable|disable\n") "enable|disable\n")
@ -217,7 +217,7 @@ CMD(pd_port_status,
/* 南瑞传输端口号设置. */ /* 南瑞传输端口号设置. */
CMD(pd_port_sendnum, CMD(pd_port_sendnum,
pd_port_sendnum_cmd, pd_port_sendnum_cmd,
"sendport num <0-32>", "sendport-num <0-32>",
"Sendport\n" "Sendport\n"
"Num\n" "Num\n"
"Sendport number\n") "Sendport number\n")
@ -246,6 +246,288 @@ CMD(pd_port_sendnum,
return CMD_SUCCESS; return CMD_SUCCESS;
} }
CMD(pd_filter_frequency,
pd_filter_frequency_cmd,
"filter-frequency <0-32767>",
"Filter\n"
"Frequency\n"
"Filter frequency\n")
{
uint8_t unit = 0;
uint8_t port = 0;
/* 取出端口号. */
if (dau_vport_to_port(pd_port_node.param_num, &unit, &port) != E_NONE)
{
return CMD_ERR_NO_MATCH;
}
pd_config.config_port[unit][port].filter_frequency = strtol(argv[0], NULL, 10);
return CMD_SUCCESS;
}
CMD(pd_rise_time,
pd_rise_time_cmd,
"rise-time <0-32767>",
"Rise\n"
"Time\n"
"Rise time\n")
{
uint8_t unit = 0;
uint8_t port = 0;
/* 取出端口号. */
if (dau_vport_to_port(pd_port_node.param_num, &unit, &port) != E_NONE)
{
return CMD_ERR_NO_MATCH;
}
pd_config.config_port[unit][port].rise_time = strtol(argv[0], NULL, 10);
return CMD_SUCCESS;
}
CMD(pd_peak_time,
pd_peak_time_cmd,
"peak-time <0-32767>",
"Peak\n"
"Time\n"
"Peak time\n")
{
uint8_t unit = 0;
uint8_t port = 0;
/* 取出端口号. */
if (dau_vport_to_port(pd_port_node.param_num, &unit, &port) != E_NONE)
{
return CMD_ERR_NO_MATCH;
}
pd_config.config_port[unit][port].peak_time = strtol(argv[0], NULL, 10);
return CMD_SUCCESS;
}
CMD(pd_fall_time,
pd_fall_time_cmd,
"fall-time <0-32767>",
"Fall\n"
"Time\n"
"Fall time\n")
{
uint8_t unit = 0;
uint8_t port = 0;
/* 取出端口号. */
if (dau_vport_to_port(pd_port_node.param_num, &unit, &port) != E_NONE)
{
return CMD_ERR_NO_MATCH;
}
pd_config.config_port[unit][port].fall_time = strtol(argv[0], NULL, 10);
return CMD_SUCCESS;
}
CMD(pd_pulse_width,
pd_pulse_width_cmd,
"pulse-width <0-32767>",
"Pulse\n"
"Width\n"
"Pulse width\n")
{
uint8_t unit = 0;
uint8_t port = 0;
/* 取出端口号. */
if (dau_vport_to_port(pd_port_node.param_num, &unit, &port) != E_NONE)
{
return CMD_ERR_NO_MATCH;
}
pd_config.config_port[unit][port].pulse_width = strtol(argv[0], NULL, 10);
return CMD_SUCCESS;
}
CMD(pd_peak_count,
pd_peak_count_cmd,
"peak-count <0-32767>",
"Peak\n"
"Count\n"
"Peak count\n")
{
uint8_t unit = 0;
uint8_t port = 0;
/* 取出端口号. */
if (dau_vport_to_port(pd_port_node.param_num, &unit, &port) != E_NONE)
{
return CMD_ERR_NO_MATCH;
}
pd_config.config_port[unit][port].peak_count = strtol(argv[0], NULL, 10);
return CMD_SUCCESS;
}
CMD(pd_signal_envelope,
pd_signal_envelope_cmd,
"signal-envelope <0-32767>",
"Signal\n"
"Envelope\n"
"Signal envelope\n")
{
uint8_t unit = 0;
uint8_t port = 0;
/* 取出端口号. */
if (dau_vport_to_port(pd_port_node.param_num, &unit, &port) != E_NONE)
{
return CMD_ERR_NO_MATCH;
}
pd_config.config_port[unit][port].signal_envelope = strtol(argv[0], NULL, 10);
return CMD_SUCCESS;
}
CMD(pd_signal_mean,
pd_signal_mean_cmd,
"signal-mean <0-32767>",
"Signal\n"
"Mean\n"
"Signal mean\n")
{
uint8_t unit = 0;
uint8_t port = 0;
/* 取出端口号. */
if (dau_vport_to_port(pd_port_node.param_num, &unit, &port) != E_NONE)
{
return CMD_ERR_NO_MATCH;
}
pd_config.config_port[unit][port].signal_mean = strtol(argv[0], NULL, 10);
return CMD_SUCCESS;
}
CMD(pd_signal_variance,
pd_signal_variance_cmd,
"signal-variance <0-32767>",
"Signal\n"
"Variance\n"
"Signal variance\n")
{
uint8_t unit = 0;
uint8_t port = 0;
/* 取出端口号. */
if (dau_vport_to_port(pd_port_node.param_num, &unit, &port) != E_NONE)
{
return CMD_ERR_NO_MATCH;
}
pd_config.config_port[unit][port].signal_variance = strtol(argv[0], NULL, 10);
return CMD_SUCCESS;
}
CMD(pd_primary_frequency,
pd_primary_frequency_cmd,
"primary-frequency <0-32767>",
"Primary\n"
"Frequency\n"
"Primary frequency\n")
{
uint8_t unit = 0;
uint8_t port = 0;
/* 取出端口号. */
if (dau_vport_to_port(pd_port_node.param_num, &unit, &port) != E_NONE)
{
return CMD_ERR_NO_MATCH;
}
pd_config.config_port[unit][port].primary_frequency = strtol(argv[0], NULL, 10);
return CMD_SUCCESS;
}
CMD(pd_primary_freq_peak,
pd_primary_freq_peak_cmd,
"primary-freq-peak <0-32767>",
"Primary\n"
"Freq\n"
"Peak\n"
"Primary freq peak\n")
{
uint8_t unit = 0;
uint8_t port = 0;
/* 取出端口号. */
if (dau_vport_to_port(pd_port_node.param_num, &unit, &port) != E_NONE)
{
return CMD_ERR_NO_MATCH;
}
pd_config.config_port[unit][port].primary_freq_peak = strtol(argv[0], NULL, 10);
return CMD_SUCCESS;
}
CMD(pd_spectral_peak_count,
pd_spectral_peak_count_cmd,
"spectral-peak-count <0-32767>",
"Spectral\n"
"Peak\n"
"Count\n"
"Spectral peak count\n")
{
uint8_t unit = 0;
uint8_t port = 0;
/* 取出端口号. */
if (dau_vport_to_port(pd_port_node.param_num, &unit, &port) != E_NONE)
{
return CMD_ERR_NO_MATCH;
}
pd_config.config_port[unit][port].spectral_peak_count = strtol(argv[0], NULL, 10);
return CMD_SUCCESS;
}
CMD(pd_spectrum_mean,
pd_spectrum_mean_cmd,
"spectrum-mean <0-32767>",
"Spectrum\n"
"Mean\n"
"Spectrum mean\n")
{
uint8_t unit = 0;
uint8_t port = 0;
/* 取出端口号. */
if (dau_vport_to_port(pd_port_node.param_num, &unit, &port) != E_NONE)
{
return CMD_ERR_NO_MATCH;
}
pd_config.config_port[unit][port].spectrum_mean = strtol(argv[0], NULL, 10);
return CMD_SUCCESS;
}
CMD(pd_spectrum_variance,
pd_spectrum_variance_cmd,
"spectrum-variance <0-32767>",
"Spectrum\n"
"Variance\n"
"Spectrum variance\n")
{
uint8_t unit = 0;
uint8_t port = 0;
/* 取出端口号. */
if (dau_vport_to_port(pd_port_node.param_num, &unit, &port) != E_NONE)
{
return CMD_ERR_NO_MATCH;
}
pd_config.config_port[unit][port].spectrum_variance = strtol(argv[0], NULL, 10);
return CMD_SUCCESS;
}
void time_get(uint32_t *timesVal, uint32_t *timeUsVal) void time_get(uint32_t *timesVal, uint32_t *timeUsVal)
{ {
@ -932,9 +1214,23 @@ int _dau_port_config_save(vty_t *vty, uint8_t unit, uint8_t port)
uint8_t field = 0; uint8_t field = 0;
field = pd_config.config_port[unit][port].is_enbale; field = pd_config.config_port[unit][port].is_enbale;
vty_out(vty, " port status %s%s", pd_port_status_str[field], VTY_NEWLINE); vty_out(vty, " port-status %s%s", pd_port_status_str[field], VTY_NEWLINE);
field = pd_config.config_port[unit][port].send_port_num; field = pd_config.config_port[unit][port].send_port_num;
vty_out(vty, " sendport num %u%s", field, VTY_NEWLINE); vty_out(vty, " sendport-num %u%s", field, VTY_NEWLINE);
vty_out(vty, " filter-frequency %d%s", pd_config.config_port[unit][port].filter_frequency, VTY_NEWLINE);
vty_out(vty, " rise-time %d%s", pd_config.config_port[unit][port].rise_time, VTY_NEWLINE);
vty_out(vty, " peak-time %d%s", pd_config.config_port[unit][port].peak_time, VTY_NEWLINE);
vty_out(vty, " fall-time %d%s", pd_config.config_port[unit][port].fall_time, VTY_NEWLINE);
vty_out(vty, " pulse-width %d%s", pd_config.config_port[unit][port].pulse_width, VTY_NEWLINE);
vty_out(vty, " peak-count %d%s", pd_config.config_port[unit][port].peak_count, VTY_NEWLINE);
vty_out(vty, " signal-envelope %d%s", pd_config.config_port[unit][port].signal_envelope, VTY_NEWLINE);
vty_out(vty, " signal-mean %f%s", pd_config.config_port[unit][port].signal_mean, VTY_NEWLINE);
vty_out(vty, " signal-variance %f%s", pd_config.config_port[unit][port].signal_variance, VTY_NEWLINE);
vty_out(vty, " primary-frequency %d%s", pd_config.config_port[unit][port].primary_frequency, VTY_NEWLINE);
vty_out(vty, " primary-freq-peak %d%s", pd_config.config_port[unit][port].primary_freq_peak, VTY_NEWLINE);
vty_out(vty, " spectral-peak-count %d%s", pd_config.config_port[unit][port].spectral_peak_count, VTY_NEWLINE);
vty_out(vty, " spectrum-mean %f%s", pd_config.config_port[unit][port].spectrum_mean, VTY_NEWLINE);
vty_out(vty, " spectrum-variance %f%s", pd_config.config_port[unit][port].spectrum_variance, VTY_NEWLINE);
return E_NONE; return E_NONE;
} }
@ -953,7 +1249,21 @@ int32_t dau_handle_init(void)
cmd_install_element(COMMON_NODE, &show_dau_status_all_cmd); cmd_install_element(COMMON_NODE, &show_dau_status_all_cmd);
cmd_install_element(PORT_NODE, &pd_port_status_cmd); cmd_install_element(PORT_NODE, &pd_port_status_cmd);
cmd_install_element(PORT_NODE, &pd_port_sendnum_cmd); cmd_install_element(PORT_NODE, &pd_port_sendnum_cmd);
cmd_install_element(PORT_NODE, &pd_filter_frequency_cmd);
cmd_install_element(PORT_NODE, &pd_rise_time_cmd);
cmd_install_element(PORT_NODE, &pd_peak_time_cmd);
cmd_install_element(PORT_NODE, &pd_fall_time_cmd);
cmd_install_element(PORT_NODE, &pd_pulse_width_cmd);
cmd_install_element(PORT_NODE, &pd_peak_count_cmd);
cmd_install_element(PORT_NODE, &pd_signal_envelope_cmd);
cmd_install_element(PORT_NODE, &pd_signal_mean_cmd);
cmd_install_element(PORT_NODE, &pd_signal_variance_cmd);
cmd_install_element(PORT_NODE, &pd_primary_frequency_cmd);
cmd_install_element(PORT_NODE, &pd_primary_freq_peak_cmd);
cmd_install_element(PORT_NODE, &pd_spectral_peak_count_cmd);
cmd_install_element(PORT_NODE, &pd_spectrum_mean_cmd);
cmd_install_element(PORT_NODE, &pd_spectrum_variance_cmd);
// /* 注册配置保存函数 */ // /* 注册配置保存函数 */
// rv = cmd_config_node_config_register(CONFIG_PRI_DAU, _dau_config_save); // rv = cmd_config_node_config_register(CONFIG_PRI_DAU, _dau_config_save);
// if (rv != E_NONE) // if (rv != E_NONE)

@ -0,0 +1,910 @@
/*****************************************************************************
* file lib/process/pd_dbg.c
* author YuLiang
* version 1.0.0
* date 01-June-2023
* brief This file provides all the debug server related operation 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.
*
******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef CFG_DEV_TYPE_LAND_PD
/* 标准C库头文件. */
#include <sys/socket.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <netinet/tcp.h>
/* 用户代码头文件. */
#include "cJSON.h"
#include "main.h"
#include "mtimer.h"
#include "pd_dbg.h"
#include "pd_csg.h"
#include "pd_dau.h"
#include "pd_upgrade.h"
/* Private define ------------------------------------------------------------*/
#define DEBUG_HEAD 0x55AA
#define DEBUG_TAIL 0x5AA5
#define DEBUG_CMD_LEN 10 * 1024 * 1024
#define DEBUG_CMU_FILE "PDMonitor"
#define DEBUG_CMU_FILE_BAK "./bak/PDMonitor.bak"
#define DEBUG_CMU_FILE_UPGRADE "PDMonitor.upgrade"
/* Private macro -------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
debug_ctrl_t debug_ctrl;
dbg_device_info_t dbg_base_config;
dbg_global_config_t dbg_global_config;
dbg_port_config_t dbg_port_config;
static char buf_cmd[DEBUG_CMD_LEN];
/* Private function prototypes -----------------------------------------------*/
/* Internal functions --------------------------------------------------------*/
void _debug_pkt_head_int(dbg_pkt_head_t *head, uint16_t cmd, uint32_t len)
{
head->head = DEBUG_HEAD;
head->cmd = cmd;
head->len = len;
}
/* 回复报文处理. */
int32_t _debug_pkt_common_send(char *buf, uint16_t cmd, uint32_t data_len)
{
uint16_t total_len;
dbg_pkt_head_t *head = (dbg_pkt_head_t*)buf;
head->len = data_len;
uint16_t *tail = (uint16_t*)(buf + sizeof(dbg_pkt_head_t) + data_len);
_debug_pkt_head_int(head, cmd, data_len);
*tail = DEBUG_TAIL;
total_len = sizeof(dbg_pkt_head_t) + data_len + 2;
DBG(DBG_M_DEBUG, "数据包 (总长度: %u bytes):\r\n", total_len);
DBG(DBG_M_DEBUG, " 头部: head=0x%04X, cmd=0x%04X, len=%u\r\n", head->head, head->cmd, head->len);
DBG(DBG_M_DEBUG, " 尾部: 0x%04X\r\n", *tail);
if (write(debug_ctrl.fd_client, buf, total_len) <= 0)
{
DBG(DBG_M_DEBUG, "Write cmd %x ERROR\r\n", head->cmd);
return E_SYS_CALL;
}
return E_NONE;
}
/* 获取配置报文处理. */
int32_t _debug_pkt_factory_cfg_get(char *buf, int len)
{
uint16_t len_pkt = 0;
dbg_device_info_t *info = (dbg_device_info_t *)(buf+sizeof(dbg_pkt_head_t));
memset(info, 0, sizeof(dbg_device_info_t));
info->type_m = device_info.type_m;
info->type_s = device_info.type_s;
info->dev_id = device_info.dev_id;
snprintf((char*)info->hostname, PD_DEV_NUM_LEN, "%s", device_info.hostname);
info->factory_date = device_info.factory_date;
info->deployment_date = device_info.deployment_date;
info->ip = device_info.ip;
memcpy(info->mac, device_info.mac, MAC_ADDR_LEN);
info->mask = device_info.mask;
info->gw = device_info.gw;
strncpy((char*)info->app_version, host.version, 32);
strncpy((char*)info->app_compile_time, host.compile, 32);
//strncpy((char*)info->hardware_version, host.hardversion, 32);
// 赋值 FPGA_version
info->csg_port = csg.server_port;
info->csg_ipv4 = csg.server_ip;
info->running_time = start_time;
len_pkt += sizeof(dbg_device_info_t);
int32_t ret = _debug_pkt_common_send(buf, DEBUG_CONFIG_FACTORY_GET, len_pkt);
/* 发送数据. */
return ret;
}
int32_t _debug_pkt_factory_cfg_set(char *buf, int len)
{
REBOOT_MSG boottype = REBOOT_NONE;
dbg_device_info_t *info = (dbg_device_info_t *)(buf+sizeof(dbg_pkt_head_t));
if(device_info.type_m != info->type_m||
device_info.type_s != info->type_s||
device_info.dev_id != info->dev_id)
{
device_info.type_m = info->type_m;
device_info.type_s = info->type_s;
device_info.dev_id = info->dev_id;
boottype = REBOOT_LOCAL_HOST_NAME_CHANGE;
}
snprintf(device_info.hostname, PD_DEV_NUM_LEN, "%s", (const char*)info->hostname);
device_info.factory_date = info->factory_date;
device_info.deployment_date = info->deployment_date;
if (device_info.ip != info->ip)
{
DBG(DBG_M_DEBUG, "IP 地址变更: %u.%u.%u.%u -> %u.%u.%u.%u\r\n",
device_info.ip & 0xFF, (device_info.ip >> 8) & 0xFF, (device_info.ip >> 16) & 0xFF, (device_info.ip >> 24) & 0xFF,
info->ip & 0xFF, (info->ip >> 8) & 0xFF, (info->ip >> 16) & 0xFF, (info->ip >> 24) & 0xFF);
device_info.ip = info->ip;
mac_generate_from_ip(device_info.ip, info->mac);
memcpy(device_info.mac, info->mac, MAC_ADDR_LEN);
boottype = REBOOT_REMOTE_IP_CHANGE;
vtysh_eth0_save();
}
device_info.gw = info->gw;
device_info.mask = info->mask;
vtysh_device_save();
if (csg.server_ip != info->csg_ipv4)
{
csg.server_ip = info->csg_ipv4;
boottype = REBOOT_REMOTE_SERVER_IP_CHANGE;
}
if (csg.server_port != info->csg_port)
{
csg.server_port = info->csg_port;
boottype = REBOOT_REMOTE_SERVER_IP_CHANGE;
}
vtysh_config_save();
// 打印接收到的设备信息
DBG(DBG_M_DEBUG, "================================================\r\n");
DBG(DBG_M_DEBUG, "接收到的设备信息:\r\n");
DBG(DBG_M_DEBUG, " 主设备号 (type_m): %u\r\n", info->type_m);
DBG(DBG_M_DEBUG, " 次设备号 (type_s): %u\r\n", info->type_s);
DBG(DBG_M_DEBUG, " 设备ID (dev_id): %u\r\n", info->dev_id);
DBG(DBG_M_DEBUG, " 主机名 (hostname): %s\r\n", info->hostname);
DBG(DBG_M_DEBUG, " 出厂时间 (factory_date): %u\r\n", info->factory_date);
DBG(DBG_M_DEBUG, " 部署时间 (deployment_date): %u\r\n", info->deployment_date);
DBG(DBG_M_DEBUG, " IP 地址 (ip): %u.%u.%u.%u\r\n",
(info->ip) & 0xFF, (info->ip >> 8) & 0xFF, (info->ip >> 16) & 0xFF, (info->ip >> 24) & 0xFF);
DBG(DBG_M_DEBUG, " 子网掩码 (mask): %u.%u.%u.%u\r\n",
(info->mask) & 0xFF, (info->mask >> 8) & 0xFF, (info->mask >> 16) & 0xFF, (info->mask >> 24) & 0xFF);
DBG(DBG_M_DEBUG, " 网关 (gw): %u.%u.%u.%u\r\n",
(info->gw) & 0xFF, (info->gw >> 8) & 0xFF, (info->gw >> 16) & 0xFF, (info->gw >> 24) & 0xFF);
DBG(DBG_M_DEBUG, " MAC 地址 (mac): %02X:%02X:%02X:%02X:%02X:%02X\r\n",
info->mac[0], info->mac[1], info->mac[2], info->mac[3], info->mac[4], info->mac[5]);
DBG(DBG_M_DEBUG, " CSG 端口 (csg_port): %u\r\n", info->csg_port);
DBG(DBG_M_DEBUG, " CSG 服务器 IP (csg_ipv4): %u.%u.%u.%u\r\n",
(info->csg_ipv4 >> 24) & 0xFF, (info->csg_ipv4 >> 16) & 0xFF, (info->csg_ipv4 >> 8) & 0xFF, info->csg_ipv4 & 0xFF);
if (boottype)
{
_debug_pkt_common_send(buf, DEBUG_CONFIG_FACTORY_SET, 0);
sleep(1);
reboot_system(LOG_DEBUG, boottype);
}
return E_NONE;
}
/* 设置报文处理. */
int32_t _debug_pkt_global_cfg_set(char *pkt, int len)
{
dbg_global_config_t *pnet = (dbg_global_config_t *)(pkt + sizeof(dbg_pkt_head_t));
//pd_config.config.power_frequency = pnet->power_frequency * 10;
//pd_config.config.trend_period = pnet->trend_period * 60;
//pd_config.config.sync_mode = pnet->sync_mode;
//pd_config.config.heartbeat_period = pnet->heartbeat_period;
//pd_config.config.pps_mode = pnet->pps_mode;
//pd_config.config.trend_storage = pnet->trend_storage;
//pd_config.config.event_storage = pnet->event_storage;
//pd_config.config.is_4G_enable = pnet->is_4G_enable;
vtysh_config_save();
return _debug_pkt_common_send(pkt, DEBUG_CONFIG_GLOBAL_SET, 0);
}
/* 查询用户参数查询报文处理. */
int32_t _debug_pkt_global_cfg_get(char *pkt, int len)
{
uint16_t len_pkt = 0;
dbg_global_config_t *pnet = (dbg_global_config_t *)(pkt + sizeof(dbg_pkt_head_t));
_debug_pkt_common_send(pkt, DEBUG_CONFIG_GLOBAL_GET, len_pkt);
return E_NONE;
}
/* 设置报文处理. */
int32_t _debug_pkt_port_cfg_set(char *pkt, int len)
{
return E_NONE;
}
/* 查询用户参数查询报文处理. */
int32_t _debug_pkt_port_cfg_get(char *pkt, int len)
{
return E_NONE;
}
/* 重启报文处理. */
int32_t _debug_pkt_reboot(char *buf, int len)
{
/* 发送数据. */
_debug_pkt_common_send(buf, DEBUG_REBOOT, 0);
/* 重启. */
reboot_system(LOG_DEBUG, REBOOT_LOCAL_RESET);
return E_NONE;
}
/* 时间设置报文处理. */
int32_t _debug_pkt_time_set(char *buf, int len)
{
uint32_t *timestamp = (uint32_t*)(buf + sizeof(dbg_pkt_head_t));
/* 配置时间. */
time_set(*timestamp);
/* 发送数据. */
_debug_pkt_common_send(buf, DEBUG_TIME_SET, 0);
return E_NONE;
}
/* 数据手动设置报文处理. */
int32_t _debug_pkt_manual_col(char *buf, int len)
{
char *flag = buf + sizeof(dbg_pkt_head_t);
DBG(DBG_M_DEBUG, " 手动采集 flag : %d\r\n", (*flag) ? TRUE : FALSE);
/* 修改标志位. */
debug_ctrl.is_manual_col = (*flag) ? TRUE : FALSE;
/* 发送数据. */
return _debug_pkt_common_send(buf, DEBUG_NOISE_CAREFOR, 0);
}
/* 获取校准系数报文处理. */
int32_t _debug_pkt_adj_get(char *buf, int len)
{}
/* 获取校准系数报文处理. */
int32_t _debug_pkt_adj_set(char *buf, int len)
{}
/* 获取运行状态报文处理. */
int32_t _debug_pkt_status_get(char *buf, int len)
{
debug_pkt_status_t *status = (debug_pkt_status_t*)(buf + sizeof(debug_pkt_head_t));
status->idx = 0;
status->UTC_TimeScale = time(NULL);
//status->F50Hz_Frequency = dau_power_frequency_get();
status->F50Hz_SynStatus = pd_state.sync;
status->dau_status = dau_connect_get();
status->sensor_status = 0;
status->is_server_link = csg.is_connect;
status->version = version_hex;
status->communication_time = csg.communication_time;
status->run_time = start_time;
/* 发送数据. */
//return _debug_pkt_common_send(buf, DEBUG_RUN_STATUS_GET, sizeof(debug_pkt_status_t));
return E_NONE;
}
/* 获取运行状态报文处理. */
int32_t _debug_pkt_dev_connect_status_get(char *buf, int len)
{
debug_pkt_dev_connect_status_t *status = (debug_pkt_dev_connect_status_t*)(buf + sizeof(dbg_pkt_head_t));
status->csg_connect_status = csg.is_connect;
DBG(DBG_M_DEBUG, " ec20_connect_status %d\r\n", status->ec20_connect_status);
DBG(DBG_M_DEBUG, " csg_connect_status %d\r\n", status->csg_connect_status);
/* 发送数据. */
return _debug_pkt_common_send(buf, DEBUG_DEVICE_STATUS, sizeof(debug_pkt_dev_connect_status_t));
}
/* CMU 升级报文处理. */
int32_t _debug_pkt_mcu_upgrade(char *buf, int len)
{
dbg_pkt_head_t *head = (dbg_pkt_head_t*)buf;
char *type = buf + sizeof(dbg_pkt_head_t);
DBG(DBG_M_DEBUG, "upgrade type is %d\r\n", type[0]);
char *data = buf + sizeof(dbg_pkt_head_t) + 1;
int fd = 0;
/* 保存文件. */
fd = open(DEBUG_CMU_FILE_UPGRADE, O_WRONLY | O_CREAT | O_TRUNC, 0777);
if (fd <= 0)
{
DBG(DBG_M_PD_ERR, "Open " DEBUG_CMU_FILE_UPGRADE " ERROR\r\n");
return E_SYS_CALL;
}
if (write(fd, data, head->len) != head->len)
{
DBG(DBG_M_PD_ERR, "Write " DEBUG_CMU_FILE_UPGRADE " ERROR\r\n");
return E_SYS_CALL;
}
close(fd);
//保存旧文件
if (rename(DEBUG_CMU_FILE, DEBUG_CMU_FILE_BAK) < 0)
{
DBG(DBG_M_PD_ERR, "Rename " DEBUG_CMU_FILE_BAK " ERROR\r\n");
return E_SYS_CALL;
}
// 升级新文件
if (rename(DEBUG_CMU_FILE_UPGRADE, DEBUG_CMU_FILE) < 0)
{
DBG(DBG_M_PD_ERR, "Rename " DEBUG_CMU_FILE_UPGRADE " ERROR\r\n");
return E_SYS_CALL;
}
/* 发送回复. */
_debug_pkt_common_send(buf, DEBUG_ARM_UPGRADE, 0);
DBG(DBG_M_DEBUG, "reboot_system arm upgrade\r\n");
reboot_system(LOG_DEBUG, REBOOT_LOCAL_ARM_UPGRADE);
return E_NONE;
}
/* DAU 升级报文处理. */
int32_t _debug_pkt_dau_upgrade(char *buf, int len, uint8_t bitmap)
{
dbg_pkt_head_t *head = (dbg_pkt_head_t*)buf;
char *type = buf + sizeof(dbg_pkt_head_t);
DBG(DBG_M_DEBUG, "upgrade type is %d\r\n", type[0]);
char *data = buf + sizeof(dbg_pkt_head_t) + 1;
int fd = 0;
fd = open(DEBUG_DAU_FILE_UPGRADE, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (fd <= 0)
{
DBG(DBG_M_DEBUG, "Open " DEBUG_DAU_FILE_UPGRADE " ERROR\r\n");
return E_SYS_CALL;
}
if (write(fd, data, head->len) != head->len)
{
DBG(DBG_M_DEBUG, "Write " DEBUG_DAU_FILE_UPGRADE " ERROR\r\n");
return E_SYS_CALL;
}
close(fd);
//保存旧文件
if (rename(DEBUG_DAU_FILE, DEBUG_DAU_FILE_BAK) < 0)
{
DBG(DBG_M_PD_ERR, "Rename " DEBUG_CMU_FILE_BAK " ERROR\r\n");
return E_SYS_CALL;
}
// 升级新文件
if (rename(DEBUG_DAU_FILE_UPGRADE, DEBUG_DAU_FILE) < 0)
{
DBG(DBG_M_PD_ERR, "Rename " DEBUG_CMU_FILE_UPGRADE " ERROR\r\n");
return E_SYS_CALL;
}
DBG(DBG_M_DEBUG, "reboot_system dau upgrade\r\n");
_debug_pkt_common_send(buf, head->cmd, 1);
reboot_system(LOG_DEBUG, REBOOT_LOCAL_ARM_UPGRADE);
return E_NONE;
}
int32_t _debug_all_upgrade(char *buf, int len)
{
dbg_pkt_head_t *head = (dbg_pkt_head_t*)buf;
char *data = buf + sizeof(dbg_pkt_head_t);
uint8_t type = 0;
int32_t *rt = (int32_t*)data;
int fd = 0;
int32_t rv = 0;
if (buf == NULL)
{
return E_BAD_PARAM;
}
/* 获取升级类型, 更新数据指针 */
type = *data;
data++;
/* 保存文件 */
fd = open(PD_UPG_SOFTWARE, O_WRONLY | O_CREAT | O_TRUNC, 0666);
if (fd <= 0)
{
DBG(DBG_M_DEBUG, "Open " PD_UPG_SOFTWARE " ERROR\r\n");
return E_SYS_CALL;
}
if (write(fd, data, head->len - 1) != (head->len - 1))
{
DBG(DBG_M_DEBUG, "Write " PD_UPG_SOFTWARE " ERROR\r\n");
return E_SYS_CALL;
}
close(fd);
/* 升级 */
rv = pd_upg_start(PD_UPG_FROM_DBG, type);
if (rv != E_NONE)
{
DBG(DBG_M_DEBUG, "Write " PD_UPG_SOFTWARE " ERROR\r\n");
*rt = 1;
_debug_pkt_common_send(buf, head->cmd, 4);
}
return rv;
}
/* 报文校验. */
int32_t _debug_pkt_check(char *buf, int len)
{
dbg_pkt_head_t *head = (dbg_pkt_head_t*)buf;
uint16_t *tail = (uint16_t*)(buf + len - 2);
DBG(DBG_M_DEBUG, "receive data head->head is 0X%X!,tail is 0X%X!\r\n",
head->head, *tail);
if (*tail != DEBUG_TAIL)
{
return E_ERROR;
}
if(head->head != DEBUG_HEAD)
{
return E_ERROR;
}
return E_NONE;
}
/* 调试工具报文数据处理. */
int32_t _debug_pkt_process(char *buf, int32_t len)
{
dbg_pkt_head_t *head = (dbg_pkt_head_t*)buf;
/* 报文格式检查. */
LD_E_RETURN(DBG_M_DEBUG, _debug_pkt_check(buf, len));
/* 报文处理. */
switch(head->cmd)
{
case DEBUG_CONFIG_FACTORY_GET:
DBG(DBG_M_DEBUG, "get DEBUG_CONFIG_FACTORY_GET data!\r\n");
_debug_pkt_factory_cfg_get(buf, len);
break;
case DEBUG_CONFIG_FACTORY_SET:
DBG(DBG_M_DEBUG, "set DEBUG_CONFIG_FACTORY_SET data!\r\n");
_debug_pkt_factory_cfg_set(buf, len);
break;
case DEBUG_CONFIG_GLOBAL_GET:
DBG(DBG_M_DEBUG, "get DEBUG_CONFIG_GLOBAL_GET data!\r\n");
_debug_pkt_global_cfg_get(buf, len);
break;
case DEBUG_CONFIG_GLOBAL_SET:
DBG(DBG_M_DEBUG, "set DEBUG_CONFIG_GLOBAL_SET data!\r\n");
_debug_pkt_global_cfg_set(buf, len);
break;
case DEBUG_CONFIG_PORT_GET:
DBG(DBG_M_DEBUG, "get DEBUG_CONFIG_PORT_GET data!\r\n");
_debug_pkt_port_cfg_get(buf, len);
break;
case DEBUG_CONFIG_PORT_SET:
DBG(DBG_M_DEBUG, "set DEBUG_CONFIG_GLOBAL_SET data!\r\n");
_debug_pkt_port_cfg_set(buf, len);
break;
case DEBUG_NOISE_CAREFOR:
_debug_pkt_manual_col(buf, len);
break;
case DEBUG_REBOOT:
_debug_pkt_reboot(buf, len);
break;
case DEBUG_TIME_SET:
_debug_pkt_time_set(buf, len);
break;
case DEBUG_ADJSUT_COEFFICIENT_GET:
_debug_pkt_adj_get(buf, len);
break;
case DEBUG_ADJSUT_COEFFICIENT_SET:
_debug_pkt_adj_set(buf, len);
break;
case DEBUG_DEVICE_STATUS:
_debug_pkt_dev_connect_status_get(buf, len);
break;
case DEBUG_ARM_UPGRADE:
_debug_pkt_mcu_upgrade(buf, len);
break;
case DEBUG_FPGA1_UPGRADE:
_debug_pkt_dau_upgrade(buf, len, 0x01);
break;
case DEBUG_UPGRADE_ALL:
_debug_all_upgrade(buf, len);
break;
default:
DBG(DBG_M_DEBUG, "Debug not support cmd:%x\n", head->cmd);
break;
}
return E_NONE;
}
/* tcp 连接粘包处理函数. */
int32_t _debug_pkt_recv_adhesion(char *buf, int len, int *len_recv)
{
dbg_pkt_head_t *head = NULL;
static int len_pkt = 0;
static int bytes_cnt = 0;
static int state = 0;
if (0 == state)
{
/* 首包处理. */
head = (dbg_pkt_head_t*)buf;
bytes_cnt = 0;
if (head->len > 10485000)
{
/* 报文太长不处理. */
return -1;
}
else if (0x55aa == head->head)
{
DBG(DBG_M_DEBUG, "pkt datalen head->len is %d\r\n", head->len);
/* 报文头处理, 置标志位. */
state = 1;
/* 计算剩余报文长度, 复制数据, 并计算收包总长度. */
len_pkt = head->len + 10 - len;
memcpy(buf_cmd + bytes_cnt, buf, len);
bytes_cnt += len;
/* 计算下一包应该接收多大的数据. */
if (len_pkt > DEBUG_BUG_SIZE)
{
*len_recv = DEBUG_BUG_SIZE;
}
else
{
*len_recv = len_pkt;
}
}
else
{
/* 在状体 0 下, 收到的报文不是以 0x55aa 开头, 无效报文不处理. */
return -1;
}
}
else if(1 == state)
{
/* 报文内容处理. */
/* 计算剩余报文长度, 复制数据, 并计算收包总长度. */
len_pkt -= len;
memcpy(buf_cmd + bytes_cnt, buf, len);
bytes_cnt += len;
/* 计算下一包应该接收多大的数据. */
if (len_pkt > DEBUG_BUG_SIZE)
{
*len_recv = DEBUG_BUG_SIZE;
}
else
{
*len_recv = len_pkt;
}
}
/* 报文没有收全, 继续等待数据. */
if (len_pkt > 0)
{
return 0;
}
state = 0;
return bytes_cnt;
}
int _debug_keep_alive(int fd)
{
// 开启保活,保活参数 表示60秒内无交互后每隔6秒检测一次40次都没得到响应时会断开连接。
int keep_alive = 1;
int keep_idle = 60;
int keep_interval = 6;
int keep_count = 10;
if (setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &keep_alive, sizeof(keep_alive)))
{
log_err(LOG_DEBUG, "Error setsockopt(SO_KEEPALIVE) failed, return %s!", safe_strerror(errno));
return -1;
}
if (setsockopt(fd, IPPROTO_TCP, TCP_KEEPIDLE, &keep_idle, sizeof(keep_idle)))
{
log_err(LOG_DEBUG, "Error setsockopt(TCP_KEEPIDLE) failed, return %s!", safe_strerror(errno));
return -1;
}
if (setsockopt(fd, SOL_TCP, TCP_KEEPINTVL, (void *)&keep_interval, sizeof(keep_interval)))
{
log_err(LOG_DEBUG, "Error setsockopt(TCP_KEEPINTVL) failed, return %s!", safe_strerror(errno));
return -1;
}
if (setsockopt(fd, SOL_TCP, TCP_KEEPCNT, (void *)&keep_count, sizeof(keep_count)))
{
log_err(LOG_DEBUG, "Error setsockopt(TCP_KEEPCNT) failed, return %s!", safe_strerror(errno));
return -1;
}
return 0;
}
/* 调试工具报文接收处理线程. */
void *_debug_pkt_recv_handle(void *arg)
{
char *buf = debug_ctrl.buf;
int len = 0;
int len_pkt = 0;
int len_recv = DEBUG_BUG_SIZE;
while(1)
{
/* 连接成功. */
len_recv = DEBUG_BUG_SIZE;
if (debug_ctrl.fd_client > 0)
{
/* 读取数据. */
len = read(debug_ctrl.fd_client, buf, len_recv);
if (len <= 0)
{
/* 连接中断, 关闭 socket 和手动采样. */
DBG(DBG_M_DEBUG, "Read len %d, close fd!\r\n", len);
debug_ctrl.is_manual_col = FALSE;
close(debug_ctrl.fd_client);
debug_ctrl.fd_client = -1;
break;
}
/* 粘包处理. */
len_pkt = _debug_pkt_recv_adhesion(buf, len, &len_recv);
if (len_pkt <= 0)
{
continue;
}
_debug_pkt_process(buf_cmd, len_pkt);
len_recv = DEBUG_BUG_SIZE;
}
usleep(10*1000);
}
pthread_exit("thread exit");
return NULL;
}
int32_t _debug_create_recv_thread()
{
struct sched_param param;
pthread_attr_t attr;
int32_t rv = 0;
pthread_t thread;
/* 初始化报文处理线程. */
/* 配置线程RR调度, 优先级25. */
pthread_attr_init(&attr);
param.sched_priority = 25;
pthread_attr_setschedpolicy(&attr, SCHED_RR);
pthread_attr_setschedparam(&attr, &param);
pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
rv = pthread_create(&thread, &attr, _debug_pkt_recv_handle, NULL);
if (rv != 0)
{
log_err(LOG_DEBUG, "PD can't create debug pthread %d!", rv);
return E_SYS_CALL;
}
else
{
thread_m_add("PD_DEBUG", thread);
}
pthread_attr_destroy(&attr);
return E_NONE;
}
void *_debug_accept_connect_handle()
{
struct sockaddr_in cliaddr = {0};
socklen_t clilen;
int connfd;
/* 等待初始化完成 */
while(!is_system_init)
{
usleep(100000);
}
/* 监听端口. */
if ((listen(debug_ctrl.fd, 5)) != 0)
{
log_err(LOG_DEBUG, "ERROR at socket listen return %s!", safe_strerror(errno));
return NULL;
}
while (1)
{
clilen = sizeof(cliaddr);
connfd = accept(debug_ctrl.fd, (struct sockaddr*)&cliaddr, &clilen);
if (connfd < 0)
{
log_err(LOG_DEBUG, "ERROR at socket accept return %s!", safe_strerror(errno));
continue;
}
char buf[20] = {0};
DBG(DBG_M_DEBUG, "new client: %s, port: %d\n",
inet_ntop(AF_INET, &cliaddr.sin_addr, buf, sizeof(buf)),
ntohs(cliaddr.sin_port));
if (debug_ctrl.fd_client < 0)
{
debug_ctrl.fd_client = connfd;
_debug_create_recv_thread();
_debug_keep_alive(debug_ctrl.fd_client);
}
else
{
DBG(DBG_M_DEBUG, "There is a link trying to connect...\n");
close(connfd);
connfd = -1;
}
}
return NULL;
}
/* 用于定时将端口的状态发送给调试软件. */
void* _debug_port_state_get(void *arg)
{
/* 如果手动采样开启, 则发送数据. */
if (debug_ctrl.is_manual_col)
{
debug_pkt_port_state_post();
}
/* 重新加入定时器. */
mtimer_add(_debug_port_state_get, NULL, 1, "DEBUG_PORT_STATE_GET");
return NULL;
}
/* Interface functions -------------------------------------------------------*/
/* 调试工具初始化. */
int32_t debug_handle_init(void)
{
return E_NONE;
}
int32_t debug_handle_init_after(void)
{
struct sockaddr_in server;
struct sched_param param;
pthread_attr_t attr;
pthread_t pid;
int32_t rv = 0;
int fd = 0;
int opt = 1;
/* 创建 socket. */
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0)
{
log_err(LOG_DEBUG, "ERROR at socket create return %s!", safe_strerror(errno));
return E_SYS_CALL;
}
/* fd 为需要端口复用的套接字. */
opt = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (const void *)&opt, sizeof(opt));
/* 绑定端口. */
bzero(&server, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(DEBUG_MANAGE_TOOL_PORT);
if(bind(fd, (struct sockaddr*)&server, sizeof(server)) < 0)
{
log_err(LOG_DEBUG, "ERROR at socket bind return %s!", safe_strerror(errno));
close(fd);
return E_SYS_CALL;
}
/* 保存数据. */
debug_ctrl.fd = fd;
debug_ctrl.fd_client = -1;
pthread_attr_init(&attr);
param.sched_priority = 25;
pthread_attr_setschedpolicy(&attr, SCHED_RR);
pthread_attr_setschedparam(&attr, &param);
pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
rv = pthread_create(&pid, &attr, _debug_accept_connect_handle, NULL);
if (rv != 0)
{
log_err(LOG_DEBUG, "PD can't create debug pthread %d!", rv);
return E_SYS_CALL;
}
else
{
thread_m_add("PD_DEBUG_ACCEPT", pid);
}
pthread_attr_destroy(&attr);
/* 每秒上传 DAU 状态寄存器. */
mtimer_add(_debug_port_state_get, NULL, 1, "DEBUG_PORT_STATE_GET");
return E_NONE;
}
/* 数据手动上传报文处理. */
int32_t debug_pkt_port_state_post(void)
{}
/* 升级结果回复处理 */
void debug_upgrade_result_send(int32_t rv, char *buf)
{
char buffer[32] = {0};
int32_t *rt = (int32_t*)(buffer + sizeof(debug_pkt_head_t));
*rt = rv;
_debug_pkt_common_send(buffer, DEBUG_UPGRADE_ALL, 4);
}
#endif
/************************ (C) COPYRIGHT LandPower ***** END OF FILE ****************/

@ -715,6 +715,7 @@ int32_t pd_main(void)
rv |= _pd_main_init(); rv |= _pd_main_init();
rv |= dau_handle_init(); rv |= dau_handle_init();
rv |= csg_handle_init(); rv |= csg_handle_init();
rv |= debug_handle_init();
return E_NONE; return E_NONE;
} }
@ -726,6 +727,7 @@ int32_t pd_main_after(void)
rv |= _pd_main_init_after(); rv |= _pd_main_init_after();
rv |= dau_handle_init_after(); rv |= dau_handle_init_after();
rv |= csg_handle_init_after(); rv |= csg_handle_init_after();
rv |= debug_handle_init_after();
rv |= _pd_broadcast_init(); rv |= _pd_broadcast_init();
return rv; return rv;

Loading…
Cancel
Save