From 286ae19de022cf8aa5bdfe01aac6eb42fe413315 Mon Sep 17 00:00:00 2001 From: wangbo Date: Thu, 31 Jul 2025 05:58:02 +0000 Subject: [PATCH] =?UTF-8?q?FIX=20=E4=BC=98=E5=8C=96=E6=8E=89=E7=BA=BF?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E8=B6=8B=E5=8A=BF=E5=92=8C=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E7=9A=84=E8=A1=A5=E4=BC=A0=E6=95=B0=E6=8D=AE;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/include/cmd.h | 1 + app/include/pd_modbus.h | 11 ++--- app/lib/a_process/pd_csg.c | 78 +++++++++++++++++++++++++++++++---- app/lib/a_process/pd_modbus.c | 28 ++++++++++--- app/lib/m_management/cmd.c | 68 ++++++++++++++++++++++++++++++ app/lib/m_management/dbg.c | 2 +- 6 files changed, 166 insertions(+), 22 deletions(-) diff --git a/app/include/cmd.h b/app/include/cmd.h index 44bb6ca..a43f77f 100755 --- a/app/include/cmd.h +++ b/app/include/cmd.h @@ -322,6 +322,7 @@ extern char *vtysh_completion_entry_function(const char *ignore, int invoking_ke extern void vtysh_config_recovery(void); extern void vtysh_device_save(void); extern void vtysh_eth0_save(void); +extern void vtysh_eth1_save(char *ip, char *mask, uint8_t *mac, char *gateway); extern int32_t vtysh_config_save(void); extern int32_t vtysh_host_addr_set(char *addr, char *mask); extern int32_t vtysh_gateway_set(char *gateway); diff --git a/app/include/pd_modbus.h b/app/include/pd_modbus.h index 89d9521..ccaef8b 100755 --- a/app/include/pd_modbus.h +++ b/app/include/pd_modbus.h @@ -9,17 +9,16 @@ #define READ 0 #define MODBUS_DEVICE_TYPE_ADDR 0x0 -#define MODBUS_IR_CURRENT_ADDR 0x10 // 铁芯 +#define MODBUS_DEVICE_TYPE_LEN 2 +#define MODBUS_IR_CURRENT_ADDR 0x10 // 铁芯电流 #define MODBUS_IR_CURRENT_LEN 2 +#define MODBUS_IR_ALARM_ADDR 0x20 // 铁芯报警 +#define MODBUS_IR_ALARM_LEN 1 #define MODBUS_DC_CURRENT_ADDR 0x34 // 直流偏磁 #define MODBUS_DC_CURRENT_LEN 2 #define MODBUS_UNIT_IR 0x01 -#define MODBUS_CURRENT_LEN 4 -#define MODBUS_ADDR_ALARM 0x32 -#define MODBUS_ALARM_LEN 2 - #define DEVICE_M_TYPE 0x03 #define DEVICE_IR_TYPE 0x04 #define DEVICE_DC_TYPE 0x05 @@ -28,8 +27,6 @@ #define POS_UINT_ID 6 #define POS_FUNCTION 7 -//#define MAX_SLOT 6 - typedef struct { uint8_t unit_id; diff --git a/app/lib/a_process/pd_csg.c b/app/lib/a_process/pd_csg.c index ef23343..7bc26a4 100755 --- a/app/lib/a_process/pd_csg.c +++ b/app/lib/a_process/pd_csg.c @@ -159,12 +159,26 @@ void _print_sockaddr_in(const struct sockaddr_in *addr) void _csg_show() { + char cmd[512] = {0}; printh("CSG connect: %s \n", (csg.is_connect == 1)? "OK" : "FAIL"); for (int i = 0; i < PD_SLOTS_MAX; i++) { printh("dau[%d]:is_connect=%d is_insert=%d\n", i, daus[i].state.is_connect, daus[i].state.is_insert); } + + printh("-----------Event----------------\r\n"); + printh("min:%lld max:%lld count=%lld max=%d\r\n", csg.event_file.index_min, csg.event_file.index_max, + csg.event_file.index_max - csg.event_file.index_min, csg.event_file.files_max); + sprintf(cmd, "ls -l %s | grep \"^-\" | wc -l", csg.event_file.dir); + system(cmd); + + printh("-----------Trend----------------\r\n"); + printh("min:%lld max:%lld count=%lld max=%d\r\n", csg.trend_file.index_min, csg.trend_file.index_max, + csg.trend_file.index_max - csg.trend_file.index_min, csg.trend_file.files_max); + sprintf(cmd, "ls -l %s | grep \"^-\" | wc -l", csg.trend_file.dir); + system(cmd); + printh("--------------------------------\r\n\n"); } void _csg_server_set(int32_t ip, uint16_t port) @@ -365,10 +379,28 @@ void _csg_add_dau_recv(char *pkt) } slot--; - printf("_csg_add_dau_recv slot = %d\n", slot); + log_out(LOG_CSG, LOG_LVL_WARN, "_csg_add_dau_recv slot = %d\n", slot); daus[slot].state.is_insert = TRUE; modbus.card[slot].is_insert = TRUE; } + +void _csg_remove_dau_recv(char *pkt) +{ + csg_pkt_head_t *head = (csg_pkt_head_t*)pkt; + uint8_t slot = head->slot; + if (slot >= PD_SLOTS_MAX || slot == 0) + { + DBG(DBG_M_PD_CSG_ERR, "Remove DAU %d error!\r\n", slot); + return; + } + + slot--; + log_out(LOG_CSG, LOG_LVL_WARN, "_csg_remove_dau_recv slot = %d\n", slot); + daus[slot].state.is_insert = FALSE; + modbus.card[slot].is_insert = FALSE; + modbus.card[slot].is_connect = FALSE; +} + /* 解析心跳报文. */ void _csg_heartbeat_recv(char *pkt) @@ -530,6 +562,7 @@ int32_t _csg_port_config_set_recv(char *pkt) DBG(DBG_M_PD_CSG, "slot:%d error\r\n", head->slot); return E_BAD_PARAM; } + DBG(DBG_M_PD_CSG, "_csg_port_config_set_recv slot:%d \r\n", head->slot); data = XMALLOC(MTYPE_CSG, sizeof(csg_config_port_t)); if (!data) @@ -774,6 +807,9 @@ int32_t _csg_recv_process(char *pkt, uint32_t len) case CSG_C_RESET: _csg_reboot_recv(pkt); break; + case CSG_C_DEL_DAU: + _csg_remove_dau_recv(pkt); + break; case CSG_C_UPDATE: _csg_upgrade_recv(pkt); break; @@ -836,6 +872,14 @@ int32_t _csg_send_add_dau(uint8_t slot, void *data) return E_NONE; } +int32_t _csg_send_del_dau(uint8_t slot, void *data) +{ + char *pkt = csg.buf_send; + _csg_send_data(CSG_REPLY, CSG_C_DEL_DAU, pkt, 0, slot + 1); + return E_NONE; +} + + int32_t _csg_send_reset(uint8_t slot, void *data) { char *pkt = csg.buf_send; @@ -1477,9 +1521,9 @@ int32_t _csg_send_trend_statistics_data(uint8_t slot, void *data) int32_t _csg_send_trend_data(uint8_t slot, void *data) { - _csg_send_trend_prps_data(slot, data); - _csg_send_trend_prpd_data(slot, data); - _csg_send_trend_statistics_data(slot, data); + LD_E_RETURN(DBG_M_PD_CSG_ERR, _csg_send_trend_prps_data(slot, data)); + LD_E_RETURN(DBG_M_PD_CSG_ERR, _csg_send_trend_prpd_data(slot, data)); + LD_E_RETURN(DBG_M_PD_CSG_ERR, _csg_send_trend_statistics_data(slot, data)); return E_NONE; } @@ -1585,6 +1629,7 @@ int32_t _csg_write_file(char *filename, char *filehead, int headsize, char *file /* 确保数据落盘 */ fsync(fileno(file)); fclose(file); + return E_NONE; } @@ -1701,7 +1746,7 @@ int32_t _csg_trend_write_file(uint8_t slot, void *data) uint8_t port = *(uint32_t *)data; if (port >= 8) return E_BAD_PARAM; - + if (csg.trend_file.index_max - csg.trend_file.index_min >= csg.trend_file.files_max) { return E_FULL; @@ -2028,9 +2073,11 @@ void _csg_report_modbus() csg_modbus_magneto_t magneto; for (int i = 0; i < PD_SLOTS_MAX; i++) { + if (0 == modbus.card[i].status) + continue; if (modbus.card[i].is_connect == TRUE - && modbus.card[i].is_insert == TRUE - && modbus.card[i].type_m == DEVICE_M_TYPE) + && modbus.card[i].is_insert == TRUE + && modbus.card[i].type_m == DEVICE_M_TYPE) { switch (modbus.card[i].type_s) { @@ -2112,9 +2159,18 @@ void *_csg_heartbeat_handle(void *arg) info.type_s = modbus.card[i].type_s; _csg_send_add_dau(i, &info); } + + if (modbus.card[i].status == 0) + { + if (modbus.card[i].is_connect == TRUE + && modbus.card[i].is_insert == TRUE) + { + _csg_send_del_dau(i, NULL); + } + } } - if (count++ > 60) + if (count++ > 10) { count = 0; _csg_report_modbus(); @@ -2267,7 +2323,7 @@ int32_t csg_handle_init_after(void) snprintf(param.thread_name, THREAD_NAME_LEN, "CSG_HEARTBEAT"); create_thread(_csg_heartbeat_handle, ¶m); - param.priority = 60; + param.priority = 65; snprintf(param.thread_name, THREAD_NAME_LEN, "CSG_SEND_CMD"); create_thread(_csg_send_cmd_handle, ¶m); @@ -2282,6 +2338,10 @@ int32_t csg_handle_init_after(void) param.priority = 75; snprintf(param.thread_name, THREAD_NAME_LEN, "CSG_SEND_EVENT"); create_thread(_csg_send_event_handle, ¶m); + + param.priority = 60; + snprintf(param.thread_name, THREAD_NAME_LEN, "CSG_SEND_BOOSTER"); + create_thread(_csg_boosterpack_handle, ¶m); return E_NONE; } diff --git a/app/lib/a_process/pd_modbus.c b/app/lib/a_process/pd_modbus.c index 3e96354..47337bc 100755 --- a/app/lib/a_process/pd_modbus.c +++ b/app/lib/a_process/pd_modbus.c @@ -40,14 +40,15 @@ CMD(modbus_show, } -int32_t _modbus_tcp_transaction(uint16_t transaction_id, uint8_t unit_id, uint16_t start_addr, uint16_t data_num) +int32_t _modbus_tcp_transaction(uint8_t unit_id, uint16_t start_addr, uint16_t data_num) { int txlen = 0; int rxlen = 0; modbus_tcp_t *frame = (modbus_tcp_t *)modbus.txbuf; uint8_t *pdata = NULL; uint8_t datalen = 0; - frame->transaction_id = htons(transaction_id); + static uint16_t transaction_id = 0; + frame->transaction_id = htons(++transaction_id); frame->protocol_id = htons(0); // Modbus // 长度:unit_id(1字节) + 后续PDU(5字节:功能码1 + 起始地址2 + 数量2) frame->length = htons(6); @@ -81,6 +82,7 @@ int32_t _modbus_tcp_transaction(uint16_t transaction_id, uint8_t unit_id, uint16 if (rxlen > 0) { uint16_t length; + //DBG(DBG_M_PD_MODBUS, "Modbus read len = %d\r\n", rxlen); //buf_print((char *)modbus.rxbuf, rxlen); @@ -136,7 +138,7 @@ int32_t _modbus_rtu_transaction(void) int32_t _modbus_get_device_type(uint8_t unit, uint8_t *typem, uint8_t *types) { - if (!_modbus_tcp_transaction(5, unit, MODBUS_DEVICE_TYPE_ADDR, 1)) + if (!_modbus_tcp_transaction(unit, MODBUS_DEVICE_TYPE_ADDR, MODBUS_DEVICE_TYPE_LEN)) { *typem = modbus.rxbuf[9]; *types = modbus.rxbuf[10]; @@ -147,7 +149,7 @@ int32_t _modbus_get_device_type(uint8_t unit, uint8_t *typem, uint8_t *types) int32_t _modbus_get_current(uint8_t unit, uint16_t addr, uint16_t len, uint32_t *current) { uint32_t cur = 0; - if (!_modbus_tcp_transaction(5, unit, addr, len)) + if (!_modbus_tcp_transaction(unit, addr, len)) { //cur = modbus.rxbuf[9] << 24; //cur += modbus.rxbuf[10] << 16; @@ -159,6 +161,18 @@ int32_t _modbus_get_current(uint8_t unit, uint16_t addr, uint16_t len, uint32_t return E_NONE; } +int32_t _modbus_get_alarm(uint8_t unit, uint16_t addr, uint16_t len, uint16_t *alarm) +{ + uint16_t val = 0; + if (!_modbus_tcp_transaction(unit, addr, len)) + { + memcpy(&val, &modbus.rxbuf[9], sizeof(uint16_t)); + } + *alarm = val; + return E_NONE; +} + + void _modbus_read_slot_stat(card_t *pcard) { card_t *ptmp = pcard; @@ -217,6 +231,10 @@ void *_modbus_send_handle() _modbus_get_current(pcard->unit_id, MODBUS_IR_CURRENT_ADDR, MODBUS_IR_CURRENT_LEN, &pcard->current); //DBG(DBG_M_PD_MODBUS, "current:%d\n", pcard->current); + + _modbus_get_alarm(pcard->unit_id, + MODBUS_IR_ALARM_ADDR, MODBUS_IR_ALARM_LEN, &pcard->alarm); + //DBG(DBG_M_PD_MODBUS, "alarm:%d\n", pcard->alarm); } else if (DEVICE_DC_TYPE == pcard->type_s && DEVICE_M_TYPE == pcard->type_m) { @@ -230,7 +248,7 @@ void *_modbus_send_handle() } #endif - sleep(2); + sleep(5); } if (modbus.fd > 0) diff --git a/app/lib/m_management/cmd.c b/app/lib/m_management/cmd.c index 0319e8a..6214558 100755 --- a/app/lib/m_management/cmd.c +++ b/app/lib/m_management/cmd.c @@ -481,6 +481,34 @@ CMD(default_route_set, return CMD_SUCCESS; } +CMD(gw_ip_set, + gw_ip_set_cmd, + "gw ip A.B.C.D A.B.C.D A.B.C.D", + "GW ip for produce\n" + "IPv4 type address\n" + "Ip address of gw\n" + "Ip mask of gw\n" + "Ip gateway of gw\n") +{ + uint8_t mac[MAC_ADDR_LEN] = {0}; + struct sockaddr_in server; + + /* 配置 ip. */ + if (inet_aton((char *)argv[0], &server.sin_addr) < 0) + { + DBG(DBG_M_CLI, "inet_aton ip is error!\r\n"); + return E_BAD_PARAM; + } + + mac_generate_from_ip(server.sin_addr.s_addr, mac); + + vtysh_eth1_save((char *)argv[0], (char *)argv[1], mac, (char *)argv[2]); + + system("./script/S80SetEth1"); + return CMD_SUCCESS; +} + + CMD(factory_date_set, factory_date_set_cmd, "factory date WORD WORD", @@ -2368,6 +2396,7 @@ void cmd_init(void) cmd_install_element(ENABLE_NODE, &deployment_date_set_cmd); cmd_install_element(ENABLE_NODE, &logoff_cmd); cmd_install_element(ENABLE_NODE, &reboot_cmd); + cmd_install_element(ENABLE_NODE, &gw_ip_set_cmd); cmd_install_element(CONFIG_NODE, &hostname_set_cmd); cmd_install_element(CONFIG_NODE, &config_log_level_cmd); @@ -2780,6 +2809,45 @@ void vtysh_eth0_save(void) return; } +void vtysh_eth1_save(char *ip, char *mask, uint8_t *mac, char *gateway) +{ + //struct sockaddr_in server; + int32_t fd = 0; + int32_t rv = 0; + uint16_t len = 0; + char *str = NULL; + + /* 获取文件名. */ + fd = open("./script/Eth1Setting", O_RDWR | O_CREAT | O_TRUNC, 0777); + if (-1 == fd) + { + DBG(DBG_M_CLI, "Can't open config file Eth0Setting\r\n"); + return; + } + chmod("./script/Eth1Setting", 0666); + + str = XMALLOC(MTYPE_VTY_TMP, 512); + //server.sin_addr.s_addr = device_info.ip; + len += snprintf(str, 512, "ip=%s\n", ip); + //server.sin_addr.s_addr = device_info.mask; + len += snprintf(str + len, 512 - len, "netmask=%s\n", mask); + len += snprintf(str + len, 512 - len, "mac=%02x:%02x:%02x:%02x:%02x:%02x\n", + mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + //server.sin_addr.s_addr = device_info.gw; + len += snprintf(str + len, 512 - len, "gateway=%s\n", gateway); + rv = write(fd, str, len); + if (rv != len) + { + DBG(DBG_M_CLI, "Can't write config file Eth1Setting\r\n"); + } + + /* 回收资源 */ + XFREE(MTYPE_VTY_TMP, str); + close(fd); + return; +} + + /* 打印 device_info 结构体的函数 */ void print_device_info(const device_info_t *info) { diff --git a/app/lib/m_management/dbg.c b/app/lib/m_management/dbg.c index f9c79fd..f9aa343 100755 --- a/app/lib/m_management/dbg.c +++ b/app/lib/m_management/dbg.c @@ -72,7 +72,7 @@ dbg_module_t _dbg_module[DBG_M_COUNT] = {DBG_M_PD_UPGRADE, FALSE, "upgrade"}, {DBG_M_PD_HF, FALSE, "pd_hf"}, {DBG_M_PD_HF_ERR, TRUE, "pd_hf_err"}, - {DBG_M_SERIAL, TRUE, "serial"}, + {DBG_M_SERIAL, FALSE, "serial"}, {DBG_M_PD_MODBUS, TRUE, "pd_modbus"}, {DBG_M_PD_MODBUS_ERR, TRUE, "pd_modbus_err"}, {DBG_M_FILE_FIFO, TRUE, "file_fifo"},