RTU/mimo/plan/C接口化改造方案.md

113 lines
3.5 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# public/protocol 模块 C 接口化改造方案
## 背景
开发团队要求 `src/public/``src/protocol/` 下所有模块提供纯 C 接口(头文件可被 C 编译器解析)。
当前除 `lib60870` 外,逐一排查后发现有 5 个模块不符合要求。
## 检查结果汇总
| 模块 | 目录 | C接口? | 主要问题 |
|------|------|--------|----------|
| libcJSON | public | ✅ | — |
| libfunc | public | ✅ | — |
| libmd5 | public | ✅ | — |
| libmy_xxhash | public | ✅ | — |
| libtask | public | ✅ | — |
| libmy_mosquitto | public | ✅ | — |
| libmodbus | protocol | ✅ | — |
| libmongoose | protocol | ✅ | — |
| libmms_m | protocol | ✅ | 接口已是C有extern "C" |
| libmms_s | protocol | ✅ | 接口已是C有extern "C" |
| **libicp67** | protocol | ❌ | 缺extern "C",含`bool test` |
| **libcmd** | public | ❌ | C++默认参数无extern "C" |
| **libcomm** | public | ❌ | enum class + std::string |
| **libdatacenter** | public | ❌ | 大面积std::string/vector/map |
| **libxml** | public | ❌ | 第三方C++类库 |
## 实施优先级
P0 → P1 → P2 → P3 → P4逐步推进每一步完成后编译验证。
---
## P0: libicp67 — 添加 extern "C" + 替换 bool 类型
### 改动点
1. `release/inc/myIcp67.h`: 添加 `extern "C"` 包裹,`bool test;` → `uint8_t test;`
2. `src/protocol/libicp67/src/icp67.cpp`: `bool test``uint8_t test`(同步修改所有引用)
### 难度: 低
---
## P1: libcmd — 移除 C++ 默认参数 + 添加 extern "C"
### 改动点
1. `release/inc/myCmd.h`:
- 移除 `= NULL` 默认参数
- 新增便捷宏供纯 C 调用方使用
- 添加 `extern "C"` 包裹
2. `src/public/libcmd/src/my_cmd.cpp`: 无改动C++ 实现内部仍可用 STL
### 难度: 低
---
## P2: libcomm — enum class → C enum + std::string → char[]
### 改动点
1. `release/inc/myComm.h`:
- `enum class CommType``typedef enum { COMM_TYPE_... } CommType;`
- `enum class CommDebugShow``typedef enum { COMM_DEBUG_... } CommDebugShow;`
- `enum class CommState``typedef enum { COMM_STATE_... } CommState;`
- `enum class CommFdType``typedef enum { COMM_FD_... } CommFdType;`
- `std::string` 成员 → `char[]` 定长数组
- 添加 `extern "C"` 包裹
2. `src/public/libcomm/src/comm*.cpp`: 同步修改 STL string → char[] 引用
### 难度: 中
---
## P3: libdatacenter — 全量 STL → C 类型
### 改动点
1. `release/inc/myDatacenter.h`:
- `std::string` 参数 → `const char*`
- `std::string` 返回值 → `const char*``int` + `char*` 输出参数
- `std::string &desc``char *desc, int desc_len`
- `std::vector<void*>*``void **, int *count`
- `std::vector<std::string>``char **, int *count` 或定长数组
- 结构体 `std::string` 成员 → `char xxx[N]` 定长数组
- 回调签名中 `std::string``const char*`
- 移除 `<vector> <string> <map>` include
- 添加 `extern "C"` 包裹
2. `src/public/libdatacenter/src/*.cpp`: 同步修改所有调用处
### 难度: 高
---
## P4: libxml (tinyxml2) — 薄 C 封装层
### 方案: 新建 C 封装层
新建文件:
- `src/public/libxml/src/xml_c_wrapper.h` — C 接口声明
- `src/public/libxml/src/xml_c_wrapper.cpp` — C 封装实现(内部调用 tinyxml2
对外使用不透明指针:
```c
typedef struct xml_doc xml_doc;
xml_doc* xml_open(const char *filepath);
void xml_close(xml_doc *doc);
const char* xml_get_child_text(xml_doc *doc, const char *path);
```
### 难度: 中