opc source code
This commit is contained in:
196
docs/softbus_daemon.md
Normal file
196
docs/softbus_daemon.md
Normal file
@@ -0,0 +1,196 @@
|
||||
# softbus_daemon
|
||||
|
||||
**路径:** `src/softbus_daemon`
|
||||
**构建目标:** `softbus_daemon`(EXECUTABLE)
|
||||
**层级:** L3 可执行层
|
||||
|
||||
## 1. 概述
|
||||
|
||||
`softbus_daemon` 是核心守护进程(Orchestrator),串联 uplink 监听、Pipeline 处理、OPC UA 服务、插件加载与命令分发,是整个系统的运行时中枢。
|
||||
|
||||
## 2. CONSTRAINTS
|
||||
|
||||
- `CoreService` 是唯一编排入口,负责按序启动/停止所有子系统。
|
||||
- Pipeline 不得绕过插件接口直接解析协议字节。
|
||||
- 命令分发须经 `CommandDispatcher` 校验后转发,不得直接操作 TCP 连接。
|
||||
- 插件加载路径解析由 `CoreService::resolvePluginPath()` 统一处理。
|
||||
- Phase 1 仅加载 modbus 插件,canopen 插件已编译但未接入。
|
||||
|
||||
## 3. 组件架构
|
||||
|
||||
```
|
||||
CoreService (编排中心)
|
||||
├── ObjectModelRegistry ← 对象模型加载
|
||||
├── PluginLoader (modbus) ← 协议插件
|
||||
├── UaServerEngine ← OPC UA 服务
|
||||
├── UaAddressSpaceBuilder ← 地址空间构建
|
||||
├── UaModelBinder ← 模型绑定
|
||||
├── UaMethodBinder ← 方法绑定
|
||||
├── UaEventBridge ← 事件桥接(stub)
|
||||
├── PipelineEngine ← 数据处理管道
|
||||
├── TcpUplinkServer ← 上行监听
|
||||
└── CommandDispatcher ← 命令分发
|
||||
```
|
||||
|
||||
## 4. CoreService
|
||||
|
||||
### 4.1 启动序列
|
||||
|
||||
`CoreService::start(profilePath, modelPath)` 按以下顺序初始化:
|
||||
|
||||
| 步骤 | 操作 | 失败行为 |
|
||||
|------|------|----------|
|
||||
| 1 | `ProfileLoader::loadFromFile(profilePath)` | 返回 false |
|
||||
| 2 | `ObjectModelRegistry::loadFromFile(modelPath)` | 返回 false |
|
||||
| 3 | `PluginLoader::load(modbusPluginPath)` | 返回 false |
|
||||
| 4 | `UaServerEngine::start(port, appName, security)` | 返回 false |
|
||||
| 5 | 创建 `UaModelBinder`、`UaMethodBinder`、`UaEventBridge` | — |
|
||||
| 6 | `UaAddressSpaceBuilder::buildFromModel(model, binder)` | 返回 false |
|
||||
| 7 | 创建 `PipelineEngine(plugin, binder)` | — |
|
||||
| 8 | 创建 `TcpUplinkServer` 并注册 `rawHandler` | — |
|
||||
| 9 | 创建 `CommandDispatcher` 并绑定 Method 回调 | — |
|
||||
| 10 | `uplink_->start()` + 启动 io_context 线程 | — |
|
||||
|
||||
### 4.2 回调绑定
|
||||
|
||||
```cpp
|
||||
// 上行:RawPacket → Pipeline
|
||||
uplink_->setRawPacketHandler([this](const RawPacket& packet) {
|
||||
pipeline_->process(packet);
|
||||
});
|
||||
|
||||
// 下行:OPC UA Method → CommandDispatcher
|
||||
uaMethodBinder_->bindExecuteCommand([this](const ExecuteCommand& cmd) {
|
||||
return commandDispatcher_->dispatch(cmd);
|
||||
});
|
||||
```
|
||||
|
||||
### 4.3 插件路径解析
|
||||
|
||||
`resolvePluginPath(pluginName)` 按以下顺序搜索:
|
||||
|
||||
1. `{exeDir}/{lib}{name}.so`
|
||||
2. `{exeDir}/../lib/{lib}{name}.so`
|
||||
3. `build/bin/{lib}{name}.so`
|
||||
4. `lib/{lib}{name}.so`
|
||||
5. `bin/{lib}{name}.so`
|
||||
6. `{cwd}/build/bin/{lib}{name}.so`
|
||||
7. `{cwd}/lib/{lib}{name}.so`
|
||||
8. `{cwd}/{lib}{name}.so`
|
||||
|
||||
## 5. PipelineEngine
|
||||
|
||||
### 5.1 职责
|
||||
|
||||
将 `RawPacket` 经插件分帧/解析转换为 `DOMMessage`,并绑定到 OPC UA。
|
||||
|
||||
### 5.2 处理流程
|
||||
|
||||
```
|
||||
process(RawPacket)
|
||||
→ plugin_->framer()->extract(data, size) // Stage 1: 分帧
|
||||
→ plugin_->parser()->parseFrame(data, size, sourceId) // Stage 2: 解析
|
||||
→ 补充 traceId, timestampNs
|
||||
→ binder_->bind(DOMMessage) // Stage 3: OPC UA 绑定
|
||||
```
|
||||
|
||||
### 5.3 错误处理
|
||||
|
||||
- 分帧失败:使用原始 payload 继续解析
|
||||
- 解析失败:记录 `warnTrace` 日志,丢弃消息
|
||||
- 绑定失败:记录 `warnTrace`(如无对应 OPC UA 节点)
|
||||
|
||||
## 6. CommandDispatcher
|
||||
|
||||
### 6.1 职责
|
||||
|
||||
校验 `ExecuteCommand` 并通过 `TcpUplinkServer` 转发至边缘。
|
||||
|
||||
### 6.2 公共 API
|
||||
|
||||
| 方法 | 说明 |
|
||||
|------|------|
|
||||
| `dispatch(ExecuteCommand)` | 校验 + 发送至已连接边缘 |
|
||||
|
||||
## 7. Registry 子系统
|
||||
|
||||
### 7.1 ObjectModelRegistry
|
||||
|
||||
| 方法 | 说明 |
|
||||
|------|------|
|
||||
| `loadFromFile(path)` | 加载 `softbus_model.json` |
|
||||
| `tree()` | 获取 `ObjectModelTree` |
|
||||
| `findPointByRegister(reg)` | 按寄存器地址查找测点 |
|
||||
|
||||
### 7.2 MetadataRegistry
|
||||
|
||||
元数据注册表,Phase 1 已编译但 `CoreService` 未接入。
|
||||
|
||||
## 8. IpcOptionalDBus(Phase 1 stub)
|
||||
|
||||
D-Bus IPC 可选实现,Phase 1 未接入 `CoreService` 主流程。
|
||||
|
||||
## 9. CLI 参数
|
||||
|
||||
| 参数 | 默认值 | 说明 |
|
||||
|------|--------|------|
|
||||
| `--config` | `config/daemon_profile.json` | 守护进程配置 |
|
||||
| `--model` | `config/softbus_model.json` | 对象模型 |
|
||||
| `--self-test` | 关闭 | 启动后自动发送一次写命令 |
|
||||
|
||||
## 10. 主循环
|
||||
|
||||
```cpp
|
||||
while (running) {
|
||||
uaEngine_->iterate(); // 驱动 OPC UA 事件
|
||||
// PlatformSignal 检查
|
||||
}
|
||||
```
|
||||
|
||||
`io_context` 在独立线程中运行,处理 TCP 异步 I/O。
|
||||
|
||||
## 11. 数据流(完整链路)
|
||||
|
||||
### 11.1 上行
|
||||
|
||||
```
|
||||
Edge → TCP → TcpUplinkServer → PipelineEngine
|
||||
→ ModbusFramer → ModbusParser → DOMMessage
|
||||
→ UaModelBinder → OPC UA Variable
|
||||
```
|
||||
|
||||
### 11.2 下行
|
||||
|
||||
```
|
||||
OPC UA Client → UaMethodBinder → CommandDispatcher
|
||||
→ TcpUplinkServer → TCP → Edge
|
||||
```
|
||||
|
||||
## 12. 依赖
|
||||
|
||||
- `softbus_core`
|
||||
- `softbus_transport`
|
||||
- `softbus_api`
|
||||
- `softbus_opcua`
|
||||
- `asio`
|
||||
- 可选:`dbus-1`(`SOFTBUS_HAS_DBUS`)
|
||||
- 构建依赖:`add_dependencies(softbus_daemon protocol_modbus)`
|
||||
|
||||
## 13. 运行示例
|
||||
|
||||
```bash
|
||||
# 正常启动
|
||||
./build/bin/softbus_daemon \
|
||||
--config config/daemon_profile.json \
|
||||
--model config/softbus_model.json
|
||||
|
||||
# 自测模式
|
||||
./build/bin/softbus_daemon --self-test
|
||||
```
|
||||
|
||||
## 14. Phase 2 规划
|
||||
|
||||
- 接入 `MetadataRegistry` 与 `TimeAligner`
|
||||
- 加载 canopen 插件,支持多插件并行
|
||||
- D-Bus IPC 接入主流程
|
||||
- 多边缘连接管理
|
||||
Reference in New Issue
Block a user