From ec424d7f0f0504c632cf5a669b259bcc9658bc3f Mon Sep 17 00:00:00 2001 From: ypc <15051963820@163.com> Date: Mon, 15 Jun 2026 15:14:43 +0800 Subject: [PATCH] =?UTF-8?q?<=E4=BF=AE=E6=94=B9>=201=E3=80=81=E5=90=88?= =?UTF-8?q?=E5=B9=B6=E5=BA=94=E7=94=A8=E6=A8=A1=E5=9D=97=E4=B8=AD=E7=9A=84?= =?UTF-8?q?com=5Fsacn=E4=B8=8Ecom=5Fchannel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mimo/plan/libcom_decode合并计划.md | 138 +++++ mimo/问题处理文档.md | 15 + release/inc/mySystem.h | 19 +- release/src/system/RTU/makefile | 2 +- release/src/system/libcom_channel/makefile | 85 --- .../{libcom_scan => libcom_decode}/makefile | 10 +- release/src/system/makefile | 3 +- src/system/RTU/src/app_sys.cpp | 3 +- src/system/libcom_channel/src/com_channel.cpp | 488 ------------------ .../inc/.gitkeep | 0 src/system/libcom_decode/inc/com_decode.h | 13 + .../libcom_decode/src/decode_channel_mgr.cpp | 207 ++++++++ .../libcom_decode/src/decode_data_router.cpp | 142 +++++ .../libcom_decode/src/decode_frame_codec.cpp | 235 +++++++++ src/system/libcom_decode/src/decode_main.cpp | 90 ++++ src/system/libcom_scan/inc/.gitkeep | 0 src/system/libcom_scan/src/com_scan.cpp | 192 ------- src/system/libiec/src/iec.cpp | 2 +- src/system/libself_ptl/src/self_ptl.cpp | 2 +- 19 files changed, 852 insertions(+), 794 deletions(-) create mode 100644 mimo/plan/libcom_decode合并计划.md delete mode 100644 release/src/system/libcom_channel/makefile rename release/src/system/{libcom_scan => libcom_decode}/makefile (86%) delete mode 100644 src/system/libcom_channel/src/com_channel.cpp rename src/system/{libcom_channel => libcom_decode}/inc/.gitkeep (100%) create mode 100644 src/system/libcom_decode/inc/com_decode.h create mode 100644 src/system/libcom_decode/src/decode_channel_mgr.cpp create mode 100644 src/system/libcom_decode/src/decode_data_router.cpp create mode 100644 src/system/libcom_decode/src/decode_frame_codec.cpp create mode 100644 src/system/libcom_decode/src/decode_main.cpp delete mode 100644 src/system/libcom_scan/inc/.gitkeep delete mode 100644 src/system/libcom_scan/src/com_scan.cpp diff --git a/mimo/plan/libcom_decode合并计划.md b/mimo/plan/libcom_decode合并计划.md new file mode 100644 index 0000000..ae76b3b --- /dev/null +++ b/mimo/plan/libcom_decode合并计划.md @@ -0,0 +1,138 @@ +# libcom_decode 模块合并计划 + +> **目标**: 将 libcom_channel(488行) + libcom_scan(192行) 合并为 libcom_decode,按功能拆分文件,整合为一个 app 线程 + +**涉及文件**: ~15个 + +--- + +## 新模块结构 + +``` +src/system/libcom_decode/ +├── inc/ +│ └── .gitkeep +└── src/ + ├── decode_channel_mgr.cpp # 通道管理:参数配置、创建、启动、状态回调 + ├── decode_frame_codec.cpp # ICP66 帧解码:协议转换、环形缓冲 + ├── decode_data_router.cpp # 数据路由:recv 分发、send 转发 + └── decode_main.cpp # app 线程入口:事件循环 +``` + +--- + +## 文件功能划分 + +### decode_channel_mgr.cpp (~150行) +- g_tcp_para[3] / g_uart_para[1] / g_channel_para[4] 参数配置 +- com_channel_create() → comm_create + register callbacks +- com_channel_start() → pthread_create per channel +- com_start() → comm_connect 入口 +- com_channel_state_cb() → 状态回调,通知 self_ptl +- com_channel_interface_get() → 供外部查询接口标识符 + +### decode_frame_codec.cpp (~120行) +- g_rx 环形缓冲区 + put_rx_data / get_rx_data +- crc_check_sum() +- icp66_search_frame() → 帧头+CRC+帧尾检测 +- icp66_to_icp67() → 协议头转换 +- icp66_search_first_head() +- icp66_frame_decode() → 帧解码主循环 + +### decode_data_router.cpp (~130行) +- com_recv_data() → 协议识别 + 路由到 iec/self_ptl +- com_scan_send_data_to_app() → 消息入队 + 事件通知 +- com_channel_recv_cb() → 接收回调:透传/协议解析/直接投递 +- com_send_data() → 下行发送 + +### decode_main.cpp (~80行) +- app_com_decode_init1() → com_channel_create + com_channel_start +- app_com_decode_init2() → return 0 +- app_com_decode() → 事件循环: + ``` + wait EV_TIMER1|TIMER2|TIMER3|EV_COM_RX_IEC|EV_COM_RX_SELF_PTL + → TIMER1/2: 空操作 + → TIMER3: run_cnt++ + → EV_COM_RX_IEC/EV_COM_RX_SELF_PTL: com_send_data() + ``` + +--- + +## 外部接口变更 + +### mySystem.h 枚举 +```c +// 旧 +ENUM_APP_COMM = 2, // 删除 +ENUM_APP_COM_SCAN = 3, // 删除 +ENUM_APP_IEC = 4, // → 3 +ENUM_APP_SELF_PTL = 5, // → 4 +ENUM_APP_WEB_SERVER = 6, // → 5 +ENUM_APP_IEC61850M = 7, // → 6 +ENUM_APP_IEC61850S = 8, // → 7 +ENUM_APP_MAX = 9 // → 8 + +// 新 +ENUM_APP_COM_DECODE = 2, // 新增 +ENUM_APP_IEC = 3, +... +ENUM_APP_MAX = 8 +``` + +### mySystem.h extern 声明 +- 删除: `app_com_channel_init1/2`, `app_com_channel` +- 删除: `app_com_scan_init1/2`, `app_com_scan` +- 删除: `com_recv_data`(不再对外暴露) +- 新增: `app_com_decode_init1`, `app_com_decode_init2`, `app_com_decode` +- 保留: `com_channel_interface_get` + +### 所有引用 ENUM_APP_* 的文件需要更新 +影响: +- `self_ptl.cpp`: ENUM_APP_COM_SCAN → ENUM_APP_COM_DECODE +- `iec.cpp`: ENUM_APP_COM_SCAN → ENUM_APP_COM_DECODE +- `app_sys.cpp`: g_vec_app 替换两个条目为一个 + +--- + +## 编译系统变更 + +### Makefile +- 删除: `release/src/system/libcom_channel/makefile` +- 删除: `release/src/system/libcom_scan/makefile` +- 新增: `release/src/system/libcom_decode/makefile` +- 更新: `release/src/system/makefile`(子目录列表) +- 更新: `release/src/system/RTU/makefile`(链接库:-lcom_channel -lcom_scan → -lcom_decode) + +--- + +## Task 分解 + +### T1: 创建新模块目录和文件 +- 创建 `src/system/libcom_decode/` 目录结构 +- 创建 4 个源文件(从两个旧文件拆出) +- 创建 makefile + +### T2: 更新 mySystem.h +- 修改 enum_app 枚举值 +- 替换 extern 声明 + +### T3: 更新 app_sys.cpp +- 替换 g_vec_app 中两个条目为一个 + +### T4: 更新所有引用 ENUM_APP_COM_SCAN 的文件 +- self_ptl.cpp, iec.cpp 中的 ENUM_APP_COM_SCAN → ENUM_APP_COM_DECODE + +### T5: 更新编译系统 +- 修改父级 makefile 和 RTU 链接 makefile + +### T6: 更新 .gitignore(如有需要) + +### T7: 编译验证 + 更新文档 + +--- + +## 验证 + +| 步骤 | 命令 | 预期 | +|------|------|------| +| 全量编译 | `./release/build.sh` | `Build complete`,零错误零警告 | diff --git a/mimo/问题处理文档.md b/mimo/问题处理文档.md index 0bf0f41..d821078 100644 --- a/mimo/问题处理文档.md +++ b/mimo/问题处理文档.md @@ -4,6 +4,21 @@ ## 2026-06-15 +### #11 libcom_channel + libcom_scan 合并为 libcom_decode + +**问题**:两个模块紧耦合(com_channel 调 com_recv_data,com_scan 调 com_channel_interface_get),且 app_comm_channel 线程为空壳 + +**修复**: +1. 合并为 `libcom_decode` 模块,按功能拆分 4 个文件 +2. 整合为 1 个 app 线程(删除空壳线程) +3. 枚举值合并(ENUM_APP_COMM + ENUM_APP_COM_SCAN → ENUM_APP_COM_DECODE) + +**状态**:✅ 已完成 +**涉及文件**:新建 `src/system/libcom_decode/`(4源文件+makefile),修改 `mySystem.h`、`app_sys.cpp`、`self_ptl.cpp`、`iec.cpp`、`makefile` +**验证**:`./release/build.sh` 编译通过 + +--- + ### #10 libcomm + 调用者缺陷修复 **问题**: diff --git a/release/inc/mySystem.h b/release/inc/mySystem.h index 71e1000..34dbb2e 100644 --- a/release/inc/mySystem.h +++ b/release/inc/mySystem.h @@ -55,8 +55,7 @@ typedef enum { ENUM_APP_SYS, // 系统应用 ENUM_APP_CMD, // 命令行应用 - ENUM_APP_COMM, // 通讯通道应用 - ENUM_APP_COM_SCAN, // 通讯扫描应用 + ENUM_APP_COM_DECODE, // 通讯编解码应用 ENUM_APP_IEC, // iec101104应用 ENUM_APP_SELF_PTL, // icp67应用 ENUM_APP_WEB_SERVER, // web服务器应用 @@ -191,10 +190,10 @@ extern int app_cmd_init1(void *arg); extern int app_cmd_init2(void *arg); extern void *app_cmd(void *arg); -// 通讯转发应用初始化与线程函数声明 -extern int app_com_scan_init1(void *arg); -extern int app_com_scan_init2(void *arg); -extern void *app_com_scan(void *arg); +// 通讯编解码应用初始化与线程函数声明 +extern int app_com_decode_init1(void *arg); +extern int app_com_decode_init2(void *arg); +extern void *app_com_decode(void *arg); // iec101104应用初始化与线程函数声明 extern int app_iec_init1(void *arg); @@ -206,11 +205,6 @@ extern int app_self_ptl_init1(void *arg); extern int app_self_ptl_init2(void *arg); extern void *app_self_ptl(void *arg); -// 通讯通道应用初始化与线程函数声明 -extern int app_com_channel_init1(void *arg); -extern int app_com_channel_init2(void *arg); -extern void *app_com_channel(void *arg); - // web服务器应用初始化与线程函数声明 extern int app_web_server_init1(void *arg); extern int app_web_server_init2(void *arg); @@ -234,9 +228,6 @@ void self_ptl_set_interface(uint32_t interface); // 通讯通道获取接口类型,通过通道ID获取 uint32_t com_channel_interface_get(uint32_t comm_id); -// 通讯转发线程收到协议待发送的数据 -extern void com_recv_data(uint32_t itfs, const uint8_t *p_rx, uint16_t rx_len); - diff --git a/release/src/system/RTU/makefile b/release/src/system/RTU/makefile index 2978f36..e190e60 100644 --- a/release/src/system/RTU/makefile +++ b/release/src/system/RTU/makefile @@ -15,7 +15,7 @@ MakeDirCommand := mkdir -p # 静态库链接(注意顺序很重要,被依赖的库放在后面) # Libs := -lcom_channel -lcom_scan -liec -lself_ptl -lfunc -ltask -lcomm -lshell -lpthread -Libs := -lcom_channel -lcom_scan -liec -liec61850m -liec61850s -lself_ptl -lweb_server -ldatacenter +Libs := -lcom_decode -liec -liec61850m -liec61850s -lself_ptl -lweb_server -ldatacenter Libs += -l60870 -licp67 -lmms_m -lmms_s -lmongoose Libs += -liec61850 Libs += -lmy_xxhash -lcmd -lmd5 -lxml -lcJSON -lcomm -ltask -lfunc diff --git a/release/src/system/libcom_channel/makefile b/release/src/system/libcom_channel/makefile deleted file mode 100644 index b7e24cf..0000000 --- a/release/src/system/libcom_channel/makefile +++ /dev/null @@ -1,85 +0,0 @@ -# 包含外部Makefile -include ./../../../linux.mk - -ProjectName := libcom_channel - -DIR := $(realpath $(CURDIR)/..) -LAST_DIR := $(notdir $(DIR)) - - -# 静态库文件目标 -OutputFile := $(LIB_REL)/$(ProjectName).a -ArchiveCommand := ar rcs # 静态库打包命令 - -MakeDirCommand := mkdir -p - -OBJ := $(DIR)/$(ProjectName)/obj -SRC := $(SRC_ROOT_DIR)/$(LAST_DIR)/$(ProjectName)/src -INC := $(SRC_ROOT_DIR)/$(LAST_DIR)/$(ProjectName)/inc - -# print_dir: -# @echo "Shell层面打印DIR: $(SRC_ROOT_DIR) $(OBJ) $(SRC) $(INC) " - -# 定义源文件和对象文件 -SOURCES := $(wildcard $(SRC)/*.c) $(wildcard $(SRC)/*.cpp) -OBJECTS := $(patsubst $(SRC)/%.c, $(OBJ)/%.o,$(filter %.c,$(SOURCES))) -OBJECTSCPP := $(patsubst $(SRC)/%.cpp, $(OBJ)/%.o,$(filter %.cpp,$(SOURCES))) - -ALL_OBJECTS := $(OBJECTS) $(OBJECTSCPP) - -# 编译选项 -APP_C_FLAGS := $(C_FLAGS) -APP_C_FLAGS += -I $(INC) -g - -APP_C_FLAGS += -O2 - -# 伪目标 -.PHONY : all clean veryclean rebuild - -# 默认目标 -all: $(OutputFile) - -# 构建静态库 -$(OutputFile): $(ALL_OBJECTS) - @$(MakeDirCommand) $(dir $@) - $(ArchiveCommand) $@ $^ - @echo "Static library built: $@" - -# 重新构建 -rebuild: veryclean all - -# 清理 -clean: - rm -f $(OBJ)/*.o - rm -f $(OBJ)/*.d - rm -f $(OutputFile) - rm -f ./out/*.* - -# 非常彻底的清理 -veryclean: clean - rm -f ./out/$(OutputFile) - rm -f $(SRC)/*.bak - rm -f $(INC)/*.bak - -# C对象文件规则 -$(OBJECTS): $(OBJ)/%.o: $(SRC)/%.c - @mkdir -p $(dir $@) - $(CC) $(APP_C_FLAGS) -o $@ -c $< - -# C++对象文件规则 -$(OBJECTSCPP): $(OBJ)/%.o: $(SRC)/%.cpp - @mkdir -p $(dir $@) - $(CPP) $(APP_C_FLAGS) -o $@ -c $< - -# 可选:生成依赖文件(用于自动重新编译) -# -include $(ALL_OBJECTS:.o=.d) - -# C依赖文件规则 -$(OBJ)/%.d: $(SRC)/%.c - @mkdir -p $(dir $@) - $(CC) $(APP_C_FLAGS) -MM -MT '$(OBJ)/$*.o' $< > $@ - -# C++依赖文件规则 -$(OBJ)/%.d: $(SRC)/%.cpp - @mkdir -p $(dir $@) - $(CPP) $(APP_C_FLAGS) -MM -MT '$(OBJ)/$*.o' $< > $@ \ No newline at end of file diff --git a/release/src/system/libcom_scan/makefile b/release/src/system/libcom_decode/makefile similarity index 86% rename from release/src/system/libcom_scan/makefile rename to release/src/system/libcom_decode/makefile index d3e05e4..9c09237 100644 --- a/release/src/system/libcom_scan/makefile +++ b/release/src/system/libcom_decode/makefile @@ -1,7 +1,7 @@ # 包含外部Makefile include ./../../../linux.mk -ProjectName := libcom_scan +ProjectName := libcom_decode DIR := $(realpath $(CURDIR)/..) LAST_DIR := $(notdir $(DIR)) @@ -17,9 +17,6 @@ OBJ := $(DIR)/$(ProjectName)/obj SRC := $(SRC_ROOT_DIR)/$(LAST_DIR)/$(ProjectName)/src INC := $(SRC_ROOT_DIR)/$(LAST_DIR)/$(ProjectName)/inc -# print_dir: -# @echo "Shell层面打印DIR: $(SRC_ROOT_DIR) $(OBJ) $(SRC) $(INC) " - # 定义源文件和对象文件 SOURCES := $(wildcard $(SRC)/*.c) $(wildcard $(SRC)/*.cpp) OBJECTS := $(patsubst $(SRC)/%.c, $(OBJ)/%.o,$(filter %.c,$(SOURCES))) @@ -71,9 +68,6 @@ $(OBJECTSCPP): $(OBJ)/%.o: $(SRC)/%.cpp @mkdir -p $(dir $@) $(CPP) $(APP_C_FLAGS) -o $@ -c $< -# 可选:生成依赖文件(用于自动重新编译) -# -include $(ALL_OBJECTS:.o=.d) - # C依赖文件规则 $(OBJ)/%.d: $(SRC)/%.c @mkdir -p $(dir $@) @@ -82,4 +76,4 @@ $(OBJ)/%.d: $(SRC)/%.c # C++依赖文件规则 $(OBJ)/%.d: $(SRC)/%.cpp @mkdir -p $(dir $@) - $(CPP) $(APP_C_FLAGS) -MM -MT '$(OBJ)/$*.o' $< > $@ \ No newline at end of file + $(CPP) $(APP_C_FLAGS) -MM -MT '$(OBJ)/$*.o' $< > $@ diff --git a/release/src/system/makefile b/release/src/system/makefile index fe5a88e..eb8b5d8 100644 --- a/release/src/system/makefile +++ b/release/src/system/makefile @@ -3,8 +3,7 @@ SUBDIRS := -SUBDIRS += ./libcom_channel -SUBDIRS += ./libcom_scan +SUBDIRS += ./libcom_decode SUBDIRS += ./libdatacenter SUBDIRS += ./libiec SUBDIRS += ./libiec61850m diff --git a/src/system/RTU/src/app_sys.cpp b/src/system/RTU/src/app_sys.cpp index caf5940..a2fb3c2 100644 --- a/src/system/RTU/src/app_sys.cpp +++ b/src/system/RTU/src/app_sys.cpp @@ -27,8 +27,7 @@ LOCAL std::vector g_vec_app = { {"app_sys", 0, app_sys_init1, app_sys_init2, (app_func_cb)app_sys, NULL, 0, NULL}, {"app_cmd", 0, app_cmd_init1, app_cmd_init2, (app_func_cb)app_cmd, NULL, 0, NULL}, - {"app_comm_channel",0, app_com_channel_init1, app_com_channel_init2, (app_func_cb)app_com_channel, NULL, 0, NULL}, - {"app_com_scan", 0, app_com_scan_init1, app_com_scan_init2, (app_func_cb)app_com_scan, NULL, 0, NULL}, + {"app_com_decode", 0, app_com_decode_init1, app_com_decode_init2, (app_func_cb)app_com_decode, NULL, 0, NULL}, {"app_iec", 0, app_iec_init1, app_iec_init2, (app_func_cb)app_iec, NULL, 0, NULL}, {"app_self_ptl", 0, app_self_ptl_init1, app_self_ptl_init2, (app_func_cb)app_self_ptl, NULL, 0, NULL}, {"app_web_server", 0, app_web_server_init1, app_web_server_init2, (app_func_cb)app_web_server, NULL, 0, NULL}, diff --git a/src/system/libcom_channel/src/com_channel.cpp b/src/system/libcom_channel/src/com_channel.cpp deleted file mode 100644 index b006430..0000000 --- a/src/system/libcom_channel/src/com_channel.cpp +++ /dev/null @@ -1,488 +0,0 @@ -#include "mySystem.h" -#include "myComm.h" -#include "myLog.h" -#include "myIcp67.h" - -#include - -typedef struct -{ - uint16_t rptr; - uint16_t wptr; - uint16_t size; - uint16_t cnt; - uint8_t buf[4096]; -}stru_rx; - -LOCAL stru_rx g_rx = { - .rptr = 0, - .wptr = 0, - .size = 4096, - .cnt = 0, -}; - -uint8_t g_tx[4096]; -uint16_t g_tx_len = 0; - -LOCAL stru_tcp_para g_tcp_para[ENUM_TCP_MAX] = -{ - [ENUM_TCP_SERVER_0] = { - #ifdef RK356x - // .local_ip = "192.168.80.100", - .local_ip = "192.168.2.54", - #else - .local_ip = "192.168.80.253", - #endif - .local_port = 2404, - }, - [ENUM_TCP_CLIENT_0] = { - .remote_ip = "192.168.80.102", - .remote_port = 2404, - }, - [ENUM_TCP_SERVER_1] = { - #ifdef RK356x - .local_ip = "198.121.0.100", - #else - .local_ip = "198.120.10.2", - #endif - .local_port = 2404, - } -}; - -LOCAL stru_uart_para g_uart_para[ENUM_UART_MAX] = -{ - [ENUM_UART_0] = { - #ifdef RK356x - .device = "/dev/ttyS3", - #else - .device = "/dev/ttyUSB1", - #endif - .baudrate = 115200, - .data_bits = 8, - .stop_bits = 1, - .parity = 'N', - } -}; - -LOCAL struct -{ - CommType type; - CommDebugShow debug_show; - void *p_para; - int id; - int socket_fd; - pthread_t thread_id; - void* (*start_cb)(void *arg); -}g_channel_para[ENUM_COMM_MAX] = -{ - [ENUM_COMM_TCP_S_0] = { - .type = CommType::tcp_server, - .debug_show = CommDebugShow::off, - .p_para = &g_tcp_para[ENUM_TCP_SERVER_0], - .id = -1, - .socket_fd = -1, - // .thread_id = -1, - // .start_cb = comm_channel_tcp_server_0_start, - }, - [ENUM_COMM_TCP_C_0] = { - .type = CommType::tcp_client, - .debug_show = CommDebugShow::off, - .p_para = &g_tcp_para[ENUM_TCP_CLIENT_0], - .id = -1, - .socket_fd = -1, - // .thread_id = -1, - // .start_cb = comm_channel_tcp_client_0_start, - }, - [ENUM_COMM_UART_0] = { - .type = CommType::uart, - .debug_show = CommDebugShow::off, - .p_para = &g_uart_para[ENUM_UART_0], - .id = -1, - .socket_fd = -1, - }, - [ENUM_COMM_TCP_S_1] = { - .type = CommType::tcp_server, - .debug_show = CommDebugShow::off, - .p_para = &g_tcp_para[ENUM_TCP_SERVER_1], - .id = -1, - .socket_fd = -1, - } -}; - -uint32_t com_channel_interface_get(uint32_t comm_id) -{ - if(comm_id >= ENUM_COMM_MAX) - { - return 0; - } - - uint32_t itfs = ((g_channel_para[comm_id].id & 0xFFFF) << 16) | (g_channel_para[comm_id].socket_fd & 0xFFFF); - - // LOG_I("com_channel_interface_get comm_id %d, itfs %d, id %d, socket_fd %d", comm_id, itfs, g_channel_para[comm_id].id, g_channel_para[comm_id].socket_fd); - - return itfs; -} - -LOCAL uint8_t crc_check_sum(uint8_t *p_data, uint16_t len) -{ - uint8_t crc = 0; - uint16_t i; - for(i = 0; i < len; i++) - { - crc += p_data[i]; - } - return crc; -} - -int icp66_search_frame(uint8_t *p_rx, uint16_t rx_len, uint16_t *p_pos, uint16_t *p_len) -{ - *p_pos = 0; - *p_len = 0; - - for(uint16_t i = 0; i < rx_len; i++) - { - stru_head *p_head = (stru_head *)&p_rx[i]; - - if(i + 2 + sizeof(stru_head) > g_rx.cnt) - { - break; - } - - if(0x66 != p_head->head1 || 0x66 != p_head->head2) - { - continue; - } - - if(p_head->len + 6 > g_rx.cnt - i) - { - break; - } - - if(p_rx[i + p_head->len + 4] != crc_check_sum(&p_rx[i + 4], p_head->len)) - { - continue; - } - - if(p_rx[i + p_head->len + 5] != 0x16) - { - continue; - } - - *p_pos = i; - *p_len = p_head->len + 6; - return 0; - } - - return -1; -} - -void icp66_to_icp67(uint8_t *p_data) -{ - stru_head *p_head = (stru_head *)p_data; - - p_head->head1 = 0x67; - p_head->head2 = 0x67; - p_head->dst = 1; - p_head->dev_addr = 1; - - p_data[4 + p_head->len] = crc_check_sum(&p_data[4], p_head->len); -} - -LOCAL void icp67_show_frame(const uint8_t *p_data, uint16_t len) -{ - if(NULL == p_data || 0 == len) - { - return; - } - - stru_head *p_head = (stru_head *)p_data; - - const char *str[5] = {"维护软件", "主板", "采样板", "LCD", "XTU"}; - - printf("%s -> %s, len = %d, dev_addr %d\n", str[p_head->src-1], str[p_head->dst-1], len, p_head->dev_addr); - for(uint16_t i = 0; i < len; i++) - { - printf("%02X ", p_data[i]); - } - printf("\n"); -} - -LOCAL void put_rx_data(stru_rx *p_slef_rx, const uint8_t *p_rx, uint16_t len) -{ - if(NULL == p_slef_rx || NULL == p_rx || 0 == len) - { - LOG_E("put_rx_data p_slef_rx NULL or p_rx NULL or len 0"); - return; - } - - for(uint16_t i = 0; i < len; i++) - { - p_slef_rx->buf[p_slef_rx->wptr] = p_rx[i]; - p_slef_rx->wptr = (p_slef_rx->wptr + 1) % p_slef_rx->size; - p_slef_rx->cnt++; - - if(p_slef_rx->cnt > p_slef_rx->size) - { - p_slef_rx->cnt--; - p_slef_rx->rptr = (p_slef_rx->rptr + 1) % p_slef_rx->size; - - LOG_E("put_rx_data cnt %d, rptr %d, wptr %d", p_slef_rx->cnt, p_slef_rx->rptr, p_slef_rx->wptr); - } - } -} - -LOCAL void get_rx_data(stru_rx *p_slef_rx, uint8_t *p_data, uint16_t *p_data_len) -{ - if(NULL == p_slef_rx || NULL == p_data || NULL == p_data_len) - { - LOG_E("get_rx_data p_slef_rx NULL or p_data NULL or p_data_len NULL"); - return; - } - - uint16_t pos = p_slef_rx->rptr; - - for(uint16_t i = 0; i < p_slef_rx->cnt; i++) - { - p_data[i] = p_slef_rx->buf[pos]; - pos = (pos + 1) % p_slef_rx->size; - } - - (*p_data_len) = p_slef_rx->cnt; -} - -LOCAL void icp66_search_first_head(uint8_t *p_data, uint16_t len, uint16_t *p_pos) -{ - *p_pos = 0; - - for(uint16_t i = 0; i < len; i++) - { - if(p_data[i] == 0x66) - { - (*p_pos) = i; - return; - } - } - - (*p_pos) = len; -} - -void icp66_frame_decode(uint32_t itfs, const uint8_t *p_rx, uint16_t rx_len) -{ - put_rx_data(&g_rx, p_rx, rx_len); - - uint16_t pos = 0; - uint16_t valid_len = 0; - int ret = -1; - uint8_t temp_buf[2048] = {0}; - do - { - pos = 0; - valid_len = 0; - - get_rx_data(&g_rx, g_tx, &g_tx_len); - ret = icp66_search_frame(g_tx, g_tx_len, &pos, &valid_len); - - if(ret == 0) - { - g_rx.rptr += (pos + valid_len); - g_rx.cnt -= (pos + valid_len); - g_rx.rptr %= g_rx.size; - - // icp67_show_frame(g_tx + pos, valid_len); - memcpy(temp_buf, &g_tx[pos], valid_len); - icp66_to_icp67(temp_buf); - - com_recv_data(itfs, temp_buf, valid_len); - } - }while(ret == 0); - - get_rx_data(&g_rx, g_tx, &g_tx_len); - icp66_search_first_head(g_tx, g_tx_len, &pos); - g_rx.rptr += pos; - g_rx.cnt -= pos; - g_rx.rptr %= g_rx.size; -} - - - -LOCAL void com_channel_recv_cb(int id, int socket_fd, const uint8_t *p_rx, uint16_t rx_len) -{ - - int send_id = -1; - int send_socket_fd = -1; - - if(id == g_channel_para[ENUM_COMM_TCP_S_0].id) - { - send_id = g_channel_para[ENUM_COMM_TCP_C_0].id; - send_socket_fd = g_channel_para[ENUM_COMM_TCP_C_0].socket_fd; - - if(send_socket_fd < 0) - { - LOG_E("com_channel_recv_cb TCP_C_0 disconnected, drop data"); - return; - } - - comm_send(send_id, send_socket_fd, p_rx, rx_len); - } - else if(id == g_channel_para[ENUM_COMM_UART_0].id) - { - // uint32_t itfs = com_channel_interface_get(ENUM_COMM_TCP_C_0); - - uint32_t itfs = ((id << 16) | (socket_fd)); - icp66_frame_decode(itfs, p_rx, rx_len); - } - else - { - uint32_t itfs = ((id << 16) | (socket_fd)); - com_recv_data(itfs, p_rx, rx_len); - } -} - -LOCAL void com_channel_state_cb(int id, int socket_fd, CommState state) -{ - LOG_I("comm_channel_state_cb id %d, socket_fd %d, state %d", id, socket_fd, state); - - for(int i = 0; i < ENUM_COMM_MAX; i++) - { - if(g_channel_para[i].id == id) - { - if(state == CommState::connected) - { - g_channel_para[i].socket_fd = socket_fd; - } - else if(state == CommState::disconnected) - { - g_channel_para[i].socket_fd = -1; - } - break; - } - } - - if(id == g_channel_para[ENUM_COMM_TCP_C_0].id) - { - uint32_t itfs = com_channel_interface_get(ENUM_COMM_TCP_C_0); - self_ptl_set_interface(itfs); - } -} - -LOCAL int com_channel_create() -{ - for(int i = 0; i < ENUM_COMM_MAX; i++) - { - if((g_channel_para[i].id = comm_create(g_channel_para[i].type, g_channel_para[i].debug_show, g_channel_para[i].p_para)) < 0) - { - LOG_E("comm_create failed, channel id %d", i); - return -1; - } - - if(0 != comm_recv_register(g_channel_para[i].id, com_channel_recv_cb)) - { - LOG_E("comm_recv_register failed, channel id %d", i); - return -1; - } - - if(0 != comm_state_register(g_channel_para[i].id, com_channel_state_cb)) - { - LOG_E("comm_state_register failed, channel id %d", i); - return -1; - } - } - - return 0; -} - -LOCAL void *com_start(void *arg) -{ - if(NULL == arg) - { - LOG_E("arg is NULL"); - return NULL; - } - - int id = *(int *)arg; - - if(0 != comm_connect(id)) - { - LOG_E("comm_connect failed, channel id %d", id); - return NULL; - } - - return NULL; -} - -LOCAL void com_channel_start() -{ - for(int i = 0; i < ENUM_COMM_MAX; i++) - { - if(g_channel_para[i].id >= 0) - { - if(0 != pthread_create(&g_channel_para[i].thread_id, NULL, /*g_channel_para[i].start_cb*/com_start, &g_channel_para[i].id)) - { - LOG_E("pthread_create failed, channel id %d", i); - return; - } - } - } -} - - -int app_com_channel_init1(void *arg) -{ - if(0 != com_channel_create()) - { - LOG_E("comm_channel_create failed"); - return -1; - } - com_channel_start(); - - return 0; -} - -int app_com_channel_init2(void *arg) -{ - - return 0; -} - -void *app_com_channel(void *arg) -{ - if(NULL == arg) - { - LOG_E("app_com_scan arg null"); - return NULL; - } - - stru_app *p_app = (stru_app *)arg; - - uint32_t event; - - - - while(1) - { - task_event_recv(p_app->p_event, - EV_TIMER1 | EV_TIMER2 | EV_TIMER3, - TASK_EVENT_FLAG_OR | TASK_EVENT_FLAG_CLEAR, - TASK_EVENT_WAIT_FOREVER, - &event); - - if(event & EV_TIMER1) - { - ; - } - - if(event & EV_TIMER2) - { - ; - } - - if(event & EV_TIMER3) - { - p_app->run_cnt++; - } - } - - - return NULL; -} - diff --git a/src/system/libcom_channel/inc/.gitkeep b/src/system/libcom_decode/inc/.gitkeep similarity index 100% rename from src/system/libcom_channel/inc/.gitkeep rename to src/system/libcom_decode/inc/.gitkeep diff --git a/src/system/libcom_decode/inc/com_decode.h b/src/system/libcom_decode/inc/com_decode.h new file mode 100644 index 0000000..646e4a7 --- /dev/null +++ b/src/system/libcom_decode/inc/com_decode.h @@ -0,0 +1,13 @@ +#pragma once + +#include "myBase.h" + + +int com_channel_create(); +void com_channel_start(); +void com_send_data(uint8_t *data); +void com_channel_recv_cb(int id, int socket_fd, const uint8_t *p_rx, uint16_t rx_len); + +int com_channel_get_send_info(int src_id, int *send_id, int *send_fd); +void icp66_frame_decode(uint32_t itfs, const uint8_t *p_rx, uint16_t rx_len); +void com_recv_data(uint32_t itfs, const uint8_t *p_rx, uint16_t rx_len); \ No newline at end of file diff --git a/src/system/libcom_decode/src/decode_channel_mgr.cpp b/src/system/libcom_decode/src/decode_channel_mgr.cpp new file mode 100644 index 0000000..d9a2e47 --- /dev/null +++ b/src/system/libcom_decode/src/decode_channel_mgr.cpp @@ -0,0 +1,207 @@ +#include "mySystem.h" +#include "myComm.h" +#include "myLog.h" + +#include "com_decode.h" + +LOCAL stru_tcp_para g_tcp_para[ENUM_TCP_MAX] = +{ + [ENUM_TCP_SERVER_0] = { + #ifdef RK356x + .local_ip = "192.168.2.54", + #else + .local_ip = "192.168.80.253", + #endif + .local_port = 2404, + }, + [ENUM_TCP_CLIENT_0] = { + .remote_ip = "192.168.80.102", + .remote_port = 2404, + }, + [ENUM_TCP_SERVER_1] = { + #ifdef RK356x + .local_ip = "198.121.0.100", + #else + .local_ip = "198.120.10.2", + #endif + .local_port = 2404, + } +}; + +LOCAL stru_uart_para g_uart_para[ENUM_UART_MAX] = +{ + [ENUM_UART_0] = { + #ifdef RK356x + .device = "/dev/ttyS3", + #else + .device = "/dev/ttyUSB1", + #endif + .baudrate = 115200, + .data_bits = 8, + .stop_bits = 1, + .parity = 'N', + } +}; + +typedef struct +{ + CommType type; + CommDebugShow debug_show; + void *p_para; + int id; + int socket_fd; + pthread_t thread_id; + void* (*start_cb)(void *arg); +}stru_channel_para; + +LOCAL stru_channel_para g_channel_para[ENUM_COMM_MAX] = +{ + [ENUM_COMM_TCP_S_0] = { + .type = CommType::tcp_server, + .debug_show = CommDebugShow::off, + .p_para = &g_tcp_para[ENUM_TCP_SERVER_0], + .id = -1, + .socket_fd = -1, + }, + [ENUM_COMM_TCP_C_0] = { + .type = CommType::tcp_client, + .debug_show = CommDebugShow::off, + .p_para = &g_tcp_para[ENUM_TCP_CLIENT_0], + .id = -1, + .socket_fd = -1, + }, + [ENUM_COMM_UART_0] = { + .type = CommType::uart, + .debug_show = CommDebugShow::off, + .p_para = &g_uart_para[ENUM_UART_0], + .id = -1, + .socket_fd = -1, + }, + [ENUM_COMM_TCP_S_1] = { + .type = CommType::tcp_server, + .debug_show = CommDebugShow::off, + .p_para = &g_tcp_para[ENUM_TCP_SERVER_1], + .id = -1, + .socket_fd = -1, + } +}; + + +uint32_t com_channel_interface_get(uint32_t comm_id) +{ + if(comm_id >= ENUM_COMM_MAX) + { + return 0; + } + + uint32_t itfs = ((g_channel_para[comm_id].id & 0xFFFF) << 16) | (g_channel_para[comm_id].socket_fd & 0xFFFF); + + return itfs; +} + + +LOCAL void com_channel_state_cb(int id, int socket_fd, CommState state) +{ + LOG_I("comm_channel_state_cb id %d, socket_fd %d, state %d", id, socket_fd, state); + + for(int i = 0; i < ENUM_COMM_MAX; i++) + { + if(g_channel_para[i].id == id) + { + if(state == CommState::connected) + { + g_channel_para[i].socket_fd = socket_fd; + } + else if(state == CommState::disconnected) + { + g_channel_para[i].socket_fd = -1; + } + break; + } + } + + if(id == g_channel_para[ENUM_COMM_TCP_C_0].id) + { + uint32_t itfs = com_channel_interface_get(ENUM_COMM_TCP_C_0); + self_ptl_set_interface(itfs); + } +} + + +LOCAL void *com_start(void *arg) +{ + if(NULL == arg) + { + LOG_E("arg is NULL"); + return NULL; + } + + int id = *(int *)arg; + + if(0 != comm_connect(id)) + { + LOG_E("comm_connect failed, channel id %d", id); + return NULL; + } + + return NULL; +} + + +void com_channel_start() +{ + for(int i = 0; i < ENUM_COMM_MAX; i++) + { + if(g_channel_para[i].id >= 0) + { + if(0 != pthread_create(&g_channel_para[i].thread_id, NULL, com_start, &g_channel_para[i].id)) + { + LOG_E("pthread_create failed, channel id %d", i); + return; + } + } + } +} + + +int com_channel_create() +{ + for(int i = 0; i < ENUM_COMM_MAX; i++) + { + if((g_channel_para[i].id = comm_create(g_channel_para[i].type, g_channel_para[i].debug_show, g_channel_para[i].p_para)) < 0) + { + LOG_E("comm_create failed, channel id %d", i); + return -1; + } + + if(0 != comm_recv_register(g_channel_para[i].id, com_channel_recv_cb)) + { + LOG_E("comm_recv_register failed, channel id %d", i); + return -1; + } + + if(0 != comm_state_register(g_channel_para[i].id, com_channel_state_cb)) + { + LOG_E("comm_state_register failed, channel id %d", i); + return -1; + } + } + + return 0; +} + + +int com_channel_get_send_info(int src_id, int *send_id, int *send_fd) +{ + if(src_id == g_channel_para[ENUM_COMM_TCP_S_0].id) + { + *send_id = g_channel_para[ENUM_COMM_TCP_C_0].id; + *send_fd = g_channel_para[ENUM_COMM_TCP_C_0].socket_fd; + return 1; + } + if(src_id == g_channel_para[ENUM_COMM_UART_0].id) + { + return 0; + } + return -1; +} diff --git a/src/system/libcom_decode/src/decode_data_router.cpp b/src/system/libcom_decode/src/decode_data_router.cpp new file mode 100644 index 0000000..6f585ec --- /dev/null +++ b/src/system/libcom_decode/src/decode_data_router.cpp @@ -0,0 +1,142 @@ +#include "mySystem.h" +#include "myComm.h" +#include "myLog.h" +#include "com_decode.h" +#include + + + + +LOCAL int com_scan_send_data_to_app(stru_msg_head *p_head, uint16_t len, uint32_t mq_id) +{ + if(NULL == p_head || len < sizeof(stru_msg_head)) + { + LOG_E("com_scan_send_data_to_app p_head null or len %d error", len); + return -1; + } + + stru_app_msg_queue *p_mq = NULL; + stru_app *p_app = NULL; + uint32_t event = 0; + + if(mq_id == ENUM_MQ_COM_TO_IEC && NULL != (p_mq = app_get_msg_queue_ptr(ENUM_MQ_COM_TO_IEC)) && len < p_mq->msg_size) + { + p_app = app_get_ptr(ENUM_APP_IEC); + event = EV_IEC_RX_COM; + } + else if(mq_id == ENUM_MQ_COM_TO_SELF_PTL && NULL != (p_mq = app_get_msg_queue_ptr(ENUM_MQ_COM_TO_SELF_PTL)) && len < p_mq->msg_size) + { + p_app = app_get_ptr(ENUM_APP_SELF_PTL); + event = EV_SELF_PTL_RX_COM; + } + else + { + LOG_E("com_scan_send_data_to_app mq_id %d or len %d error", mq_id, len); + return -1; + } + + if(0 != task_msg_queue_send(p_mq->p_msg_queue, p_head, p_mq->msg_size)) + { + LOG_E("com_scan_send_data_to_app send to queue failed"); + return -1; + } + + if(0 != task_event_send(p_app->p_event, event)) + { + LOG_E("com_scan_send_data_to_app send event failed"); + task_msg_queue_try_recv(p_mq->p_msg_queue, p_app->rtx.tx, p_mq->msg_size); + return -1; + } + + return 0; +} + + +void com_recv_data(uint32_t itfs, const uint8_t *p_rx, uint16_t rx_len) +{ + if(NULL == p_rx || 0 == rx_len) + { + LOG_E("com_recv_data p_rx null or len 0"); + return; + } + stru_app *p_app = NULL; + if(NULL == (p_app = app_get_ptr(ENUM_APP_COM_DECODE))) + { + LOG_E("com_recv_data app_get_ptr failed"); + return; + } + + stru_msg_head *p_head = (stru_msg_head *)p_app->rtx.tx; + p_head->interface = itfs; + p_head->len = rx_len; + memcpy(p_head->data, p_rx, rx_len); + + uint32_t interface = com_channel_interface_get(ENUM_COMM_TCP_C_0); + + if(interface == itfs) + { + com_scan_send_data_to_app(p_head, sizeof(stru_msg_head) + rx_len, ENUM_MQ_COM_TO_SELF_PTL); + } + else + { + stru_protocol_head *p_proto = (stru_protocol_head *)p_head->data; + + if(0x67 == p_proto->icp67.head1 && 0x67 == p_proto->icp67.head2) + { + com_scan_send_data_to_app(p_head, sizeof(stru_msg_head) + rx_len, ENUM_MQ_COM_TO_SELF_PTL); + } + else if(0x10 == p_proto->iec.head || 0x68 == p_proto->iec.head) + { + com_scan_send_data_to_app(p_head, sizeof(stru_msg_head) + rx_len, ENUM_MQ_COM_TO_IEC); + } + else + { + LOG_E("com_recv_data mq_id error, head:%02x", p_head->data[0]); + } + } +} + + +void com_channel_recv_cb(int id, int socket_fd, const uint8_t *p_rx, uint16_t rx_len) +{ + + int send_id = -1; + int send_fd = -1; + int result = com_channel_get_send_info(id, &send_id, &send_fd); + + if(1 == result) // 用于中转数据,不解析 + { + if(send_fd < 0) + { + LOG_E("com_channel_recv_cb TCP_C_0 disconnected, drop data"); + return; + } + + comm_send(send_id, send_fd, p_rx, rx_len); + } + else if(0 == result) // 暂时处理,当前串口接收的报文是0x66的,后续要求也传输0x67 + { + uint32_t itfs = ((id << 16) | (socket_fd)); + icp66_frame_decode(itfs, p_rx, rx_len); + } + else + { + uint32_t itfs = ((id << 16) | (socket_fd)); + com_recv_data(itfs, p_rx, rx_len); + } +} + + +void com_send_data(uint8_t *data) +{ + if(NULL == data) + { + return; + } + + stru_msg_head *p_head = (stru_msg_head *)data; + int id = p_head->interface >> 16; + int fd = p_head->interface & 0xFFFF; + + comm_send(id, fd, p_head->data, p_head->len); +} diff --git a/src/system/libcom_decode/src/decode_frame_codec.cpp b/src/system/libcom_decode/src/decode_frame_codec.cpp new file mode 100644 index 0000000..c8d9d9a --- /dev/null +++ b/src/system/libcom_decode/src/decode_frame_codec.cpp @@ -0,0 +1,235 @@ +#include "mySystem.h" +#include "myLog.h" +#include "myIcp67.h" +#include "com_decode.h" + +typedef struct +{ + uint16_t rptr; + uint16_t wptr; + uint16_t size; + uint16_t cnt; + uint8_t buf[4096]; +}stru_rx; + +LOCAL stru_rx g_rx = { + .rptr = 0, + .wptr = 0, + .size = 4096, + .cnt = 0, +}; + +uint8_t g_tx[4096]; +uint16_t g_tx_len = 0; + + +/* 累加和校验 */ +LOCAL uint8_t crc_check_sum(uint8_t *p_data, uint16_t len) +{ + uint8_t crc = 0; + uint16_t i; + for(i = 0; i < len; i++) + { + crc += p_data[i]; + } + return crc; +} + + +/* + * 在数据流中搜索一个完整的 ICP66 帧 + * 帧格式: 0x66 0x66 len(2B) data(lenB) crc(1B) 0x16 + * 对端使用 stru_head 结构体封帧,因此通过 stru_head 字段解析 + */ +int icp66_search_frame(uint8_t *p_rx, uint16_t rx_len, uint16_t *p_pos, uint16_t *p_len) +{ + *p_pos = 0; + *p_len = 0; + + for(uint16_t i = 0; i < rx_len; i++) + { + stru_head *p_head = (stru_head *)&p_rx[i]; + + /* 剩余数据不够容纳 stru_head,无法继续搜索 */ + if(i + 2 + sizeof(stru_head) > g_rx.cnt) + { + break; + } + + /* 帧头必须为 0x66 0x66 */ + if(0x66 != p_head->head1 || 0x66 != p_head->head2) + { + continue; + } + + /* 数据长度超出剩余数据,帧不完整,退出等待更多数据 */ + if(p_head->len + 6 > g_rx.cnt - i) + { + break; + } + + /* 校验数据段的累加和 */ + if(p_rx[i + p_head->len + 4] != crc_check_sum(&p_rx[i + 4], p_head->len)) + { + continue; + } + + /* 帧尾必须为 0x16 */ + if(p_rx[i + p_head->len + 5] != 0x16) + { + continue; + } + + *p_pos = i; + *p_len = p_head->len + 6; + return 0; + } + + return -1; +} + + +/* 将 ICP66 帧转换为 ICP67 帧:修改帧头、设置 dst/dev_addr、重算 CRC */ +void icp66_to_icp67(uint8_t *p_data) +{ + stru_head *p_head = (stru_head *)p_data; + + p_head->head1 = 0x67; + p_head->head2 = 0x67; + p_head->dst = 1; + p_head->dev_addr = 1; + + p_data[4 + p_head->len] = crc_check_sum(&p_data[4], p_head->len); +} + + +/* 调试用:打印 ICP67 帧的源/目标设备、长度和十六进制内容 */ +LOCAL void icp67_show_frame(const uint8_t *p_data, uint16_t len) +{ + if(NULL == p_data || 0 == len) + { + return; + } + + stru_head *p_head = (stru_head *)p_data; + + const char *str[5] = {"维护软件", "主板", "采样板", "LCD", "XTU"}; + + printf("%s -> %s, len = %d, dev_addr %d\n", str[p_head->src-1], str[p_head->dst-1], len, p_head->dev_addr); + for(uint16_t i = 0; i < len; i++) + { + printf("%02X ", p_data[i]); + } + printf("\n"); +} + + +/* 将数据写入环形接收缓冲区,溢出时丢弃最早的数据 */ +LOCAL void put_rx_data(stru_rx *p_slef_rx, const uint8_t *p_rx, uint16_t len) +{ + if(NULL == p_slef_rx || NULL == p_rx || 0 == len) + { + LOG_E("put_rx_data p_slef_rx NULL or p_rx NULL or len 0"); + return; + } + + for(uint16_t i = 0; i < len; i++) + { + p_slef_rx->buf[p_slef_rx->wptr] = p_rx[i]; + p_slef_rx->wptr = (p_slef_rx->wptr + 1) % p_slef_rx->size; + p_slef_rx->cnt++; + + /* 溢出时丢弃最早数据,保持 rptr 在有效数据起始 */ + if(p_slef_rx->cnt > p_slef_rx->size) + { + p_slef_rx->cnt--; + p_slef_rx->rptr = (p_slef_rx->rptr + 1) % p_slef_rx->size; + + LOG_E("put_rx_data cnt %d, rptr %d, wptr %d", p_slef_rx->cnt, p_slef_rx->rptr, p_slef_rx->wptr); + } + } +} + + +/* 从环形缓冲区拷贝数据到线性缓冲区,不消费数据(不修改 rptr/cnt) */ +LOCAL void get_rx_data(stru_rx *p_slef_rx, uint8_t *p_data, uint16_t *p_data_len) +{ + if(NULL == p_slef_rx || NULL == p_data || NULL == p_data_len) + { + LOG_E("get_rx_data p_slef_rx NULL or p_data NULL or p_data_len NULL"); + return; + } + + uint16_t pos = p_slef_rx->rptr; + + for(uint16_t i = 0; i < p_slef_rx->cnt; i++) + { + p_data[i] = p_slef_rx->buf[pos]; + pos = (pos + 1) % p_slef_rx->size; + } + + (*p_data_len) = p_slef_rx->cnt; +} + + +/* 搜索数据流中第一个 0x66 字节的位置,用于丢弃帧头前的无效数据 */ +LOCAL void icp66_search_first_head(uint8_t *p_data, uint16_t len, uint16_t *p_pos) +{ + *p_pos = 0; + + for(uint16_t i = 0; i < len; i++) + { + if(p_data[i] == 0x66) + { + (*p_pos) = i; + return; + } + } + + (*p_pos) = len; +} + + +/* + * ICP66 帧解码主循环 + * 1. 新数据追加到环形缓冲区 + * 2. 循环搜索完整帧 → 转 ICP67 → 通过 com_recv_data 分发 + * 3. 未完成的数据留在环形缓冲区等待下次追加 + */ +void icp66_frame_decode(uint32_t itfs, const uint8_t *p_rx, uint16_t rx_len) +{ + put_rx_data(&g_rx, p_rx, rx_len); + + uint16_t pos = 0; + uint16_t valid_len = 0; + int ret = -1; + uint8_t temp_buf[2048] = {0}; + do + { + pos = 0; + valid_len = 0; + + get_rx_data(&g_rx, g_tx, &g_tx_len); + ret = icp66_search_frame(g_tx, g_tx_len, &pos, &valid_len); + + if(ret == 0) + { + /* 从环形缓冲区消费已识别的一帧数据 */ + g_rx.rptr += (pos + valid_len); + g_rx.cnt -= (pos + valid_len); + g_rx.rptr %= g_rx.size; + + memcpy(temp_buf, &g_tx[pos], valid_len); + icp66_to_icp67(temp_buf); + + com_recv_data(itfs, temp_buf, valid_len); + } + }while(ret == 0); + + /* 丢弃帧头前的无效字节,保留未完成的数据 */ + get_rx_data(&g_rx, g_tx, &g_tx_len); + icp66_search_first_head(g_tx, g_tx_len, &pos); + g_rx.rptr += pos; + g_rx.cnt -= pos; + g_rx.rptr %= g_rx.size; +} diff --git a/src/system/libcom_decode/src/decode_main.cpp b/src/system/libcom_decode/src/decode_main.cpp new file mode 100644 index 0000000..c6f7694 --- /dev/null +++ b/src/system/libcom_decode/src/decode_main.cpp @@ -0,0 +1,90 @@ +#include "mySystem.h" +#include "myLog.h" +#include "com_decode.h" + + + + +int app_com_decode_init1(void *arg) +{ + if(0 != com_channel_create()) + { + LOG_E("com_channel_create failed"); + return -1; + } + com_channel_start(); + + return 0; +} + + +int app_com_decode_init2(void *arg) +{ + return 0; +} + + +void *app_com_decode(void *arg) +{ + if(NULL == arg) + { + LOG_E("app_com_decode arg null"); + return NULL; + } + + stru_app *p_app = (stru_app *)arg; + + uint32_t event; + stru_app_msg_queue *p_mq = NULL; + stru_rtx_data *p_rtx = &p_app->rtx; + + while(1) + { + task_event_recv(p_app->p_event, + EV_TIMER1 | EV_TIMER2 | EV_TIMER3 | EV_COM_RX_IEC | EV_COM_RX_SELF_PTL, + TASK_EVENT_FLAG_OR | TASK_EVENT_FLAG_CLEAR, + TASK_EVENT_WAIT_FOREVER, + &event); + + if(event & EV_TIMER1) + { + ; + } + + if(event & EV_TIMER2) + { + ; + } + + if(event & EV_TIMER3) + { + p_app->run_cnt++; + } + + if(event & EV_COM_RX_IEC && NULL != (p_mq = app_get_msg_queue_ptr(ENUM_MQ_IEC_TO_COM))) + { + while(1) + { + if(0 != task_msg_queue_try_recv(p_mq->p_msg_queue, p_rtx->rx, p_mq->msg_size)) + { + break; + } + com_send_data(p_rtx->rx); + } + } + + if(event & EV_COM_RX_SELF_PTL && NULL != (p_mq = app_get_msg_queue_ptr(ENUM_MQ_SELF_PTL_TO_COM))) + { + while(1) + { + if(0 != task_msg_queue_try_recv(p_mq->p_msg_queue, p_rtx->rx, p_mq->msg_size)) + { + break; + } + com_send_data(p_rtx->rx); + } + } + } + + return NULL; +} diff --git a/src/system/libcom_scan/inc/.gitkeep b/src/system/libcom_scan/inc/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/src/system/libcom_scan/src/com_scan.cpp b/src/system/libcom_scan/src/com_scan.cpp deleted file mode 100644 index 45187f1..0000000 --- a/src/system/libcom_scan/src/com_scan.cpp +++ /dev/null @@ -1,192 +0,0 @@ -#include "mySystem.h" -#include "myLog.h" -#include "myComm.h" - - -#include - - - -LOCAL int com_scan_send_data_to_app(stru_msg_head *p_head, uint16_t len, uint32_t mq_id) -{ - if(NULL == p_head || 0 == len) - { - LOG_E("com_scan_send_data_to_app p_head null or len 0"); - return -1; - } - - stru_app_msg_queue *p_mq = NULL; - stru_app *p_app = NULL; - uint32_t event = 0; - - if(mq_id == ENUM_MQ_COM_TO_IEC && NULL != (p_mq = app_get_msg_queue_ptr(ENUM_MQ_COM_TO_IEC)) && len < p_mq->msg_size) - { - p_app = app_get_ptr(ENUM_APP_IEC); - event = EV_IEC_RX_COM; - } - else if(mq_id == ENUM_MQ_COM_TO_SELF_PTL && NULL != (p_mq = app_get_msg_queue_ptr(ENUM_MQ_COM_TO_SELF_PTL)) && len < p_mq->msg_size) - { - p_app = app_get_ptr(ENUM_APP_SELF_PTL); - event = EV_SELF_PTL_RX_COM; - } - else - { - LOG_E("com_scan_send_data_to_app mq_id %d or len %d error", mq_id, len); - return -1; - } - - if(0 != task_msg_queue_send(p_mq->p_msg_queue, p_head, p_mq->msg_size)) - { - LOG_E("com_scan_send_data_to_app send to queue failed"); - return -1; - } - - if(0 != task_event_send(p_app->p_event, event)) - { - LOG_E("com_scan_send_data_to_app send to iec event failed"); - task_msg_queue_try_recv(p_mq->p_msg_queue, p_app->rtx.rx, p_mq->msg_size); - return -1; - } - - return 0; -} - - -void com_recv_data(uint32_t itfs, const uint8_t *p_rx, uint16_t rx_len) -{ - if(NULL == p_rx || 0 == rx_len) - { - LOG_E("com_recv_data p_rx null or len 0"); - return; - } - stru_app *p_app = NULL; - if(NULL == (p_app = app_get_ptr(ENUM_APP_COM_SCAN))) - { - LOG_E("com_recv_data app_get_ptr failed"); - return; - } - - stru_msg_head *p_head = (stru_msg_head *)p_app->rtx.tx; - p_head->interface = itfs; - p_head->len = rx_len; - memcpy(p_head->data, p_rx, rx_len); - - uint32_t interface = com_channel_interface_get(ENUM_COMM_TCP_C_0); - if(interface == itfs) - { - com_scan_send_data_to_app(p_head, rx_len + sizeof(stru_msg_head), ENUM_MQ_COM_TO_SELF_PTL); - } - else - { - stru_protocol_head *p= (stru_protocol_head *)p_head->data; - - if(p->icp67.head1 == 0x67 && p->icp67.head2 == 0x67) - { - com_scan_send_data_to_app(p_head, rx_len + sizeof(stru_msg_head), ENUM_MQ_COM_TO_SELF_PTL); - } - else if(p->iec.head == 0x10 || p->iec.head == 0x68) - { - com_scan_send_data_to_app(p_head, rx_len + sizeof(stru_msg_head), ENUM_MQ_COM_TO_IEC); - } - } -} - -void com_send_data(uint8_t *data) -{ - if(NULL == data) - { - LOG_E("com_send_data data null"); - return; - } - - stru_msg_head *p_head = (stru_msg_head *)data; - - int id = p_head->interface >> 16; - int fd = p_head->interface & 0xFFFF; - - // LOG_I("com_send_data interface %x, id %d fd %d len %d", p_head->interface, id, fd, p_head->len); - - comm_send(id, fd, p_head->data, p_head->len); -} - - - -int app_com_scan_init1(void *arg) -{ - return 0; -} - -int app_com_scan_init2(void *arg) -{ - return 0; -} - - -void *app_com_scan(void *arg) -{ - if(NULL == arg) - { - LOG_E("app_com_scan arg null"); - return NULL; - } - - stru_app *p_app = (stru_app *)arg; - - uint32_t event; - stru_app_msg_queue *p_mq = NULL; - stru_msg_head *p_head = NULL; - stru_rtx_data *p_rtx = &p_app->rtx; - - while (1) - { - task_event_recv(p_app->p_event, - EV_TIMER1 | EV_TIMER2 | EV_TIMER3 | EV_COM_RX_IEC | EV_COM_RX_SELF_PTL, - TASK_EVENT_FLAG_OR | TASK_EVENT_FLAG_CLEAR, - TASK_EVENT_WAIT_FOREVER, - &event); - - if((event & EV_COM_RX_IEC) && NULL != (p_mq = app_get_msg_queue_ptr(ENUM_MQ_IEC_TO_COM))) - { - while (1) - { - if(0 != task_msg_queue_try_recv(p_mq->p_msg_queue, p_rtx->rx, p_mq->msg_size)) - { - break; - } - - com_send_data(p_rtx->rx); - } - } - - if((event & EV_COM_RX_SELF_PTL) && NULL != (p_mq = app_get_msg_queue_ptr(ENUM_MQ_SELF_PTL_TO_COM))) - { - while (1) - { - if(0 != task_msg_queue_try_recv(p_mq->p_msg_queue, p_rtx->rx, p_mq->msg_size)) - { - break; - } - - com_send_data(p_rtx->rx); - } - - } - - if(event & EV_TIMER1) - { - ; - } - - if(event & EV_TIMER2) - { - ; - } - - if(event & EV_TIMER3) - { - p_app->run_cnt++; - } - } -} - - diff --git a/src/system/libiec/src/iec.cpp b/src/system/libiec/src/iec.cpp index 0110c7b..e0a8e43 100644 --- a/src/system/libiec/src/iec.cpp +++ b/src/system/libiec/src/iec.cpp @@ -339,7 +339,7 @@ int iec_data_tx(uint8_t *p_tx, uint16_t tx_len, void *arg) return -1; } - if(NULL == (p_app = app_get_ptr(ENUM_APP_COM_SCAN)) || 0 != task_event_send(p_app->p_event, EV_COM_RX_IEC)) + if(NULL == (p_app = app_get_ptr(ENUM_APP_COM_DECODE)) || 0 != task_event_send(p_app->p_event, EV_COM_RX_IEC)) { LOG_E("iec_data_tx app_get_ptr or task_event_send failed"); task_msg_queue_try_recv(p_mq->p_msg_queue, p_rtx->rx, p_mq->msg_size); diff --git a/src/system/libself_ptl/src/self_ptl.cpp b/src/system/libself_ptl/src/self_ptl.cpp index c4c5fa2..c968e4b 100644 --- a/src/system/libself_ptl/src/self_ptl.cpp +++ b/src/system/libself_ptl/src/self_ptl.cpp @@ -320,7 +320,7 @@ void self_ptl_data_tx(uint8_t *p_tx, uint16_t len, void *arg) return; } - if(NULL == (p_app = app_get_ptr(ENUM_APP_COM_SCAN)) || 0 != task_event_send(p_app->p_event, EV_COM_RX_SELF_PTL)) + if(NULL == (p_app = app_get_ptr(ENUM_APP_COM_DECODE)) || 0 != task_event_send(p_app->p_event, EV_COM_RX_SELF_PTL)) { LOG_E("self_ptl_data_tx task_event_send failed"); task_msg_queue_try_recv(p_mq->p_msg_queue, p_rtx->rx, p_mq->msg_size);