From 3fd8295c0a01757fd1612b3c7ac6a7638bbeae6f Mon Sep 17 00:00:00 2001 From: yuliang Date: Wed, 2 Jul 2025 14:13:37 +0800 Subject: [PATCH] =?UTF-8?q?FIX=20|=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/include/pd_dau.h | 10 ++ app/include/pd_hf.h | 2 + app/include/pd_main.h | 19 ++- app/lib/a_process/pd_dau.c | 238 ++++++++++++++++++++++++++++++++++-- app/lib/a_process/pd_hf.c | 63 +++++++++- app/lib/a_process/pd_main.c | 18 ++- 6 files changed, 328 insertions(+), 22 deletions(-) diff --git a/app/include/pd_dau.h b/app/include/pd_dau.h index 448c945..98421be 100755 --- a/app/include/pd_dau.h +++ b/app/include/pd_dau.h @@ -54,6 +54,8 @@ typedef int32_t (*dau_recv_cb)(uint8_t, char*, uint16_t); typedef int32_t (*dau_send_cb)(uint8_t, uint8_t, void*); typedef void (*dau_data_free_cb)(uint8_t); +typedef void (*dau_show_cfg_cb)(uint8_t); +typedef void (*dau_show_port_cb)(uint8_t, uint8_t); /* 命令类型. */ enum DAU_CMD_TYPE @@ -181,6 +183,7 @@ typedef struct { uint8_t is_insert:1; uint8_t is_connect:1; + uint8_t port_sum; uint16_t beat_cnt; } dau_state_t; @@ -198,6 +201,8 @@ typedef struct dau_recv_cb recv_cb; // dau 收包处理函数 dau_send_cb send_cb; // dau 收包处理函数 dau_data_free_cb free_cb; // 内存释放函数 + dau_show_cfg_cb show_cfg_cb; // 显示全局配置函数 + dau_show_port_cb show_port_cb; // 显示端口配置函数 int32_t fifo_send; // 发包 fifo void *private_data; } dau_t; @@ -206,6 +211,11 @@ typedef struct /* Extern global variables ---------------------------------------------------*/ extern dau_t daus[PD_SLOTS_MAX]; +extern const char *pd_sync_str[PD_SYNC_MAX]; +extern const char *pd_pps_str[PD_PPS_MAX]; +extern const char *pd_protocol_str[PD_PROTOCOL_MAX]; +extern const char *dau_port_type_str[PD_PORT_TYPE_COUNT]; +extern const char *pd_noise_type_str[PD_DENOISE_TYPE_COUNT]; /* Extern functions ----------------------------------------------------------*/ extern int32_t dau_handle_init(void); diff --git a/app/include/pd_hf.h b/app/include/pd_hf.h index e255875..e3974e5 100755 --- a/app/include/pd_hf.h +++ b/app/include/pd_hf.h @@ -327,6 +327,8 @@ extern int32_t hf_recv_process(uint8_t slot, char *pkt, uint16_t len); extern int32_t hf_send_process(uint8_t slot, uint8_t type, void *data); extern void* hf_data_malloc(void); extern void hf_data_free(uint8_t slot); +extern void hf_show_cfg(uint8_t slot); +extern void hf_show_port(uint8_t slot, uint8_t port); #endif #endif /************************ (C) COPYRIGHT LandPower ***** END OF FILE ****/ diff --git a/app/include/pd_main.h b/app/include/pd_main.h index f6a717c..48f9bfd 100755 --- a/app/include/pd_main.h +++ b/app/include/pd_main.h @@ -65,7 +65,7 @@ /* Exported types ------------------------------------------------------------*/ /* 用于命令行模式节点注册配置保存函数 */ -typedef int pd_port_cmd_save_config_f(vty_t*, uint8_t, uint8_t); +typedef int pd_slot_cmd_save_config_f(vty_t*, uint8_t); /* 向服务器发送消息的类型. */ typedef enum @@ -138,7 +138,8 @@ typedef enum { PD_SYNC_PT = 1, PD_SYNC_INSIDE, - PD_SYNC_OUTSIDE + PD_SYNC_OUTSIDE, + PD_SYNC_MAX } PD_SYNC_MODE_E; /* 设备配置的 PPS 同步模式. */ @@ -147,13 +148,15 @@ typedef enum PD_PPS_AUTO = 0, PD_PPS_MASTER, PD_PPS_SLAVE, + PD_PPS_MAX } PD_PPS_MODE_E; /* 协议类型. */ typedef enum { PD_PROTOCOL_LAND = 0, - PD_PROTOCOL_CSG = 1 + PD_PROTOCOL_CSG = 1, + PD_PROTOCOL_MAX } PD_PROTOCOL_TYPE; /* 事件类型. */ @@ -352,6 +355,13 @@ typedef struct { uint16_t auto_noise_cnt; // 自动调整降噪等级时脉冲计数阈值. } pd_config_port_t; +/* 端口配置. */ +typedef struct { + uint8_t slot; // slot id + uint8_t reserved0[1]; + uint16_t timeout; // slot 连接断开监测时间, 单位: 分钟 +} pd_config_slot_t; + /* 实时波形配置. */ typedef struct { uint8_t is_concern; // 是否被关注, 在实时波形中使用. @@ -365,6 +375,7 @@ typedef struct { pd_config_global_t config; // 全局配置 pd_config_port_t config_port[PD_DAU_SUM][PD_DAU_PORT_SUM]; // 端口配置 pd_config_real_t config_real[PD_DAU_SUM][PD_DAU_PORT_SUM]; // 实时波形配置 + pd_config_slot_t config_slot[PD_SLOTS_MAX]; } pd_config_t; typedef struct @@ -385,7 +396,7 @@ extern cmd_node_t pd_slot_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 int32_t pd_slot_cmd_config_register(int32_t pri, pd_slot_cmd_save_config_f *func); extern void pd_sync_state_get(void); extern void pd_pps_mode_set(void); extern void pd_prps_show(void); diff --git a/app/lib/a_process/pd_dau.c b/app/lib/a_process/pd_dau.c index bae96d3..97f3f4f 100755 --- a/app/lib/a_process/pd_dau.c +++ b/app/lib/a_process/pd_dau.c @@ -36,9 +36,186 @@ /* Private variables ---------------------------------------------------------*/ dau_t daus[PD_SLOTS_MAX]; +const char *pd_sync_str[PD_SYNC_MAX] = +{ + "", + "pt", + "inside", + "outside" +}; + +const char *pd_pps_str[PD_PPS_MAX] = +{ + "auto", + "master", + "slave" +}; + +const char *pd_protocol_str[PD_PROTOCOL_MAX] = +{ + "land", + "csg" +}; + +/* DAU 端口类型分类 */ +const char *dau_port_type_str[PD_PORT_TYPE_COUNT] = +{ + "", + "uhf", + "ae", + "tev", + "hf" +}; + +/* DAU 端口降噪类型分类. */ +const char *pd_noise_type_str[PD_DENOISE_TYPE_COUNT] = +{ + "none", + "auto", + "manual", + "variance" +}; + /* Private function prototypes -----------------------------------------------*/ /* Internal functions --------------------------------------------------------*/ +/* 显示 DAU */ +void _dau_show_slot(uint8_t slot) +{ + struct sockaddr_in server; + dau_contact_t *dau = &daus[slot].info; + char buf[128] = {0}; + time_t temp = 0; + + printh("APP version %s (%08x)\r\n", dau->app_version, dau->dev_id); + printh("APP Compile date: %s\r\n", dau->app_compile_time); + printh("FPGA version %s\r\n", dau->FPGA_version); + printh("HW version %s\r\n", dau->hardware_version); + printh("Id: %03d.%03d\r\n", dau->type_m, dau->type_s); + printh("Mac: %02x:%02x:%02x:%02x:%02x:%02x\r\n",dau->mac[0], dau->mac[1], dau->mac[2], + dau->mac[3], dau->mac[4], dau->mac[5]); + server.sin_addr.s_addr = dau->ip; + printh("ip: %s\r\n", inet_ntoa(server.sin_addr)); + server.sin_addr.s_addr = dau->mask; + printh("%s\r\n", inet_ntoa(server.sin_addr)); + server.sin_addr.s_addr = dau->gw; + printh("route: %s\r\n", inet_ntoa(server.sin_addr)); + temp = dau->factory_date; + strftime(buf, 128, "%Y-%m-%d %H:%M:%S", localtime(&temp)); + printh("factory date: %s\r\n", buf); + temp = dau->deployment_date; + strftime(buf, 128, "%Y-%m-%d %H:%M:%S", localtime(&temp)); + printh("deployment date: %s\r\n\n", buf); + + return; +} + +/* 显示 DAU 状态 */ +void _dau_show_state(void) +{ + uint8_t i = 0; + dau_state_t *state = NULL; + + printh("SL IN CO PS TO\r\n"); + for(i = 0; i < PD_SLOTS_MAX; i++) + { + state = &daus[i].state; + printh("%-02d %-02d %-02d %-02d %-05d\r\n", i + 1, state->is_insert, state->is_connect, state->port_sum, state->beat_cnt); + } + printh("\n"); + + return; +} + +/* 连接超时时间 */ +CMD(dau_connect_timeout, + dau_connect_timeout_cmd, + "connect-timeout <1-1440>", + "Connect timeout\n" + "Timeout time: min\n") +{ + uint8_t slot = pd_slot_node.param_num; + + pd_config.config_slot[slot].timeout = strtol(argv[0], NULL, 10); + + return CMD_SUCCESS; +} + +/* Slot 状态显示 */ +CMD(dau_slot_show, + dau_slot_show_cmd, + "show slot <1-8>", + "Show\n" + "Slot\n" + "Slot id\n") +{ + uint8_t slot = strtol(argv[0], NULL, 10); + + _dau_show_slot(slot - 1); + + return CMD_SUCCESS; +} + +/* Slot 状态显示 */ +CMD(dau_state_show, + dau_state_show_cmd, + "show slot state", + "Show\n" + "Slot\n" + "State\n") +{ + _dau_show_state(); + + return CMD_SUCCESS; +} + +/* Slot 全局配置显示 */ +CMD(dau_cfg_show, + dau_cfg_show_cmd, + "show slot cfg <1-8>", + "Show\n" + "Slot\n" + "Config\n" + "Slot id\n") +{ + uint8_t slot = strtol(argv[0], NULL, 10); + dau_t *dau = &daus[slot - 1]; + + if (dau->show_cfg_cb) + { + dau->show_cfg_cb(dau->slot); + } + + return CMD_SUCCESS; +} + +/* Slot 端口配置显示 */ +CMD(dau_port_show, + dau_port_show_cmd, + "show slot <1-8> port <1-8> ", + "Show\n" + "Slot\n" + "Slot id\n" + "Port\n" + "Port id\n") +{ + uint8_t slot = strtol(argv[0], NULL, 10); + uint8_t port = strtol(argv[1], NULL, 10); + dau_t *dau = &daus[slot - 1]; + + if (port > dau->state.port_sum) + { + return CMD_ERR_NO_MATCH; + } + + if (dau->show_port_cb) + { + dau->show_port_cb(dau->slot, port - 1); + } + + return CMD_SUCCESS; +} + /* 包头填充 */ void _dau_head_init(dau_t *dau, dau_head_init_t *head_data) { @@ -60,16 +237,24 @@ int _dau_insert(dau_t *dau) { if (3 == dau->info.type_m && 1 == dau->info.type_s) { + /* 申请和释放放在一起, 避免地址失效 */ + if (dau->private_data && dau->free_cb) + { + dau->free_cb(dau->slot); + } + dau->private_data = hf_data_malloc(); dau->recv_cb = hf_recv_process; dau->send_cb = hf_send_process; dau->free_cb = hf_data_free; - dau->private_data = hf_data_malloc(); + dau->show_cfg_cb = hf_show_cfg; + dau->show_port_cb = hf_show_port; } else { return E_NOT_FOUND; } + dau->state.beat_cnt = 0; dau->state.is_connect = TRUE; return E_NONE; @@ -80,12 +265,8 @@ int _dau_remove(dau_t *dau) { dau->recv_cb = NULL; dau->send_cb = NULL; - if (dau->free_cb) - { - dau->free_cb(dau->slot); - } - dau->free_cb = NULL; - dau->private_data = NULL; + dau->show_cfg_cb = NULL; + dau->show_port_cb = NULL; dau->state.is_connect = FALSE; return E_NONE; @@ -128,6 +309,7 @@ void _dau_recv_connect(dau_t *dau, uint16_t recv_len) dau_contact_t *pnet = (dau_contact_t*)(dau->buf_recv + sizeof(dau_pkt_head_t)); dau_ack_t *ack = (dau_ack_t*)(dau->buf_recv + sizeof(dau_pkt_head_t)); dau_contact_t *info = NULL; + uint8_t i = 0; /* 如果当前状态还是连接状态先释放内存 */ if (dau->state.is_connect) @@ -137,6 +319,14 @@ void _dau_recv_connect(dau_t *dau, uint16_t recv_len) /* 复制设备信息 */ memcpy(&dau->info, pnet, sizeof(dau_contact_t)); + dau->state.port_sum = 0; + for(i = 0; i < PD_DAU_PORT_SUM; i++) + { + if (pnet->port[i]) + { + dau->state.port_sum++; + } + } /* 回复报文 */ ack->result = 0; @@ -289,18 +479,36 @@ void *_dau_state_handle(void *arg) for(i = 0; i < PD_SLOTS_MAX; i++) { state = &daus[i].state; - state->beat_cnt++; + if (state->is_connect) + { + state->beat_cnt++; + if (state->beat_cnt > pd_config.config_slot[i].timeout * 60) + { + log_warn(LOG_DAU, "Slot %d remove!", i); + _dau_remove(&daus[i]); + } + } + } } return NULL; } +/* config模式配置保存函数: vty -- 相应的终端 */ +int _dau_port_config_save(vty_t *vty, uint8_t slot) +{ + vty_out(vty, " connect-timeout %d%s", pd_config.config_slot[slot].timeout, VTY_NEWLINE); + + return E_NONE; +} + /* Interface functions -------------------------------------------------------*/ /* dau 预初始化 */ int32_t dau_handle_init(void) { uint8_t i = 0; + int32_t rv = E_ERROR; memset(&daus, 0, sizeof(dau_t) * PD_SLOTS_MAX); @@ -309,6 +517,20 @@ int32_t dau_handle_init(void) daus[i].slot = i; } + cmd_install_element(PORT_NODE, &dau_connect_timeout_cmd); + + cmd_install_element(COMMON_NODE, &dau_slot_show_cmd); + cmd_install_element(COMMON_NODE, &dau_state_show_cmd); + cmd_install_element(COMMON_NODE, &dau_cfg_show_cmd); + cmd_install_element(COMMON_NODE, &dau_port_show_cmd); + + rv = pd_slot_cmd_config_register(PD_PORT_CMD_PRI_DAU, _dau_port_config_save); + if (rv != E_NONE) + { + log_err(LOG_DAU, "DAU port command save register ERROR %d!", rv); + return rv; + } + return E_NONE; } diff --git a/app/lib/a_process/pd_hf.c b/app/lib/a_process/pd_hf.c index ab34775..2bba323 100755 --- a/app/lib/a_process/pd_hf.c +++ b/app/lib/a_process/pd_hf.c @@ -44,6 +44,7 @@ #include /* 用户代码头文件. */ +#include "fifo.h" #include "pd_main.h" #include "pd_csg.h" #include "pd_dau.h" @@ -182,14 +183,17 @@ void _hf_send_cfg_get_port(uint8_t slot, void *data) { dau_head_init_t head_data; dau_t *dau = &daus[slot]; + uint8_t *param = (uint8_t*)(dau->buf_send + sizeof(dau_pkt_head_t)); /* 回复报文 */ head_data.cmd_type = DAU_PRV_REQUEST; head_data.cmd = DAU_P_CONFIG_PORT_GET; head_data.pkt_id = dau->pkt_id++; head_data.pkt = dau->buf_send; - head_data.len = sizeof(dau_pkt_head_t); - + head_data.len = sizeof(dau_pkt_head_t) + 4; + + *param = *(uint8_t*)data; + dau_data_send(dau, &head_data); return; @@ -800,19 +804,70 @@ void* hf_data_malloc(void) return NULL; } + memset(&p->cfg, 0, sizeof(hf_cfg_t)); + memset(&p->port, 0, sizeof(hf_cfg_port_t) * PD_DAU_PORT_SUM); + return p; } /* 高频私有数据释放 */ void hf_data_free(uint8_t slot) { - dau_t *dau = &daus[slot]; - hf_data_t *hf_data = (hf_data_t*)dau->private_data; + hf_data_t *hf_data = (hf_data_t*)daus[slot].private_data; XFREE(MTYPE_HF, hf_data); return; } +/* 显示全局配置 */ +void hf_show_cfg(uint8_t slot) +{ + hf_data_t *hf_data = (hf_data_t*)daus[slot].private_data; + hf_cfg_t *cfg = &hf_data->cfg; + + dau_send_msg_t msg; + msg.type = DAU_SEND_CFG_GET; + msg.data = XMALLOC_Q(MTYPE_CSG, 4); + fifo_write(daus[slot].fifo_send, &msg, sizeof(dau_send_msg_t)); + sleep(1); + + printh("Power frequency: %fHz\r\n", cfg->power_frequency / 10.0); + printh("Trend period: %dm\r\n", cfg->trend_period); + printh("Sync mode: %s\r\n", pd_sync_str[cfg->sync_mode]); + printh("Heartbeat period: %dm\r\n", cfg->heartbeat_period); + printh("PPS mode: %s\r\n", pd_pps_str[cfg->pps_mode]); + printh("Protocol type: %s\r\n", pd_protocol_str[cfg->protocol_type]); + printh("Trend storage: %d\r\n", cfg->trend_storage); + printh("Event storage: %d\r\n\n", cfg->event_storage); + + return; +} + +/* 显示端口配置 */ +void hf_show_port(uint8_t slot, uint8_t port) +{ + hf_data_t *hf_data = (hf_data_t*)daus[slot].private_data; + hf_cfg_port_t *cfg = &hf_data->port[port]; + + dau_send_msg_t msg; + msg.type = DAU_SEND_PORT_GET; + msg.data = XMALLOC_Q(MTYPE_CSG, 4); + *(uint8_t*)msg.data = port + 1; + fifo_write(daus[slot].fifo_send, &msg, sizeof(dau_send_msg_t)); + sleep(1); + + printh("Port type: %s\r\n", dau_port_type_str[cfg->port_type]); + printh("Auto noise: %s\r\n", cfg->is_auto_noise ? "true" : "false"); + printh("Denoise type: %s\r\n", pd_noise_type_str[cfg->denoise_type]); + printh("Denoise level: %dmv\r\n", cfg->denoise_auto); + printh("Denoise manual: %dmv\r\n", cfg->denoise_manual); + printh("Event cnt high: sum %d sec %d\r\n", cfg->event_counter_h, cfg->event_sec_h); + printh("Event thr high: thr %d sum %d\r\n", cfg->event_thr_h, cfg->event_counter_thr_h); + printh("Event cnt low: sum %d sec %d thr %d sum %d\r\n\n", cfg->event_counter_l, cfg->event_sec_l, + cfg->event_thr_l, cfg->event_counter_thr_l); + + return; +} #endif /************************ (C) COPYRIGHT LandPower ***** END OF FILE ****************/ diff --git a/app/lib/a_process/pd_main.c b/app/lib/a_process/pd_main.c index 2bb5412..cc1d229 100755 --- a/app/lib/a_process/pd_main.c +++ b/app/lib/a_process/pd_main.c @@ -254,7 +254,7 @@ int _pd_config_save(vty_t* vty) void _pd_slot_config_save_all(vty_t *vty, uint8_t unit) { array_t *configs = pd_slot_node.configs; - pd_port_cmd_save_config_f *func = NULL; + pd_slot_cmd_save_config_f *func = NULL; uint8_t i = 0; vty_out(vty, "slot %d%s", unit + 1, VTY_NEWLINE); @@ -267,7 +267,7 @@ void _pd_slot_config_save_all(vty_t *vty, uint8_t unit) continue; } - func(vty, unit, 0); + func(vty, unit); } vty_out(vty, "!%s", VTY_NEWLINE); @@ -276,12 +276,12 @@ void _pd_slot_config_save_all(vty_t *vty, uint8_t unit) /* config模式配置保存函数: vty -- 相应的终端 */ int32_t _pd_slot_config_save(vty_t *vty) { - uint8_t unit = 0; + uint8_t slot = 0; /* 其他配置保存 */ - for(unit = 0; unit < PD_SLOTS_MAX; unit++) + for(slot = 0; slot < PD_SLOTS_MAX; slot++) { - _pd_slot_config_save_all(vty, unit); + _pd_slot_config_save_all(vty, slot); } return E_NONE; @@ -513,6 +513,12 @@ int32_t _pd_main_init(void) int32_t rv = 0; /* 初始化基本参数. */ + for(i = 0; i < PD_SLOTS_MAX; i++) + { + pd_config.config_slot[i].slot = i; + pd_config.config_slot[i].timeout = 3; + } + for(i = 0; i < PD_DAU_SUM; i++) { for(j = 0; j < PD_DAU_PORT_SUM; j++) @@ -635,7 +641,7 @@ int32_t pd_main_after(void) return rv; } -int32_t pd_port_cmd_config_register(int32_t pri, pd_port_cmd_save_config_f *func) +int32_t pd_slot_cmd_config_register(int32_t pri, pd_slot_cmd_save_config_f *func) { cmd_node_t *node = &pd_slot_node;