Files
softbus_OPC/docs/build_and_config.md
flower_linux 710a8c2bd3 phase 1
2026-06-11 18:06:39 +08:00

240 lines
7.1 KiB
Markdown
Raw 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.
# 构建与配置
## 1. 环境要求
| 依赖 | 版本 | 获取方式 |
|------|------|----------|
| CMake | ≥ 3.16 | 系统安装 |
| C++ 编译器 | C++17 | GCC / Clang / MSVC |
| nlohmann_json | v3.11.3 | FetchContent 自动 |
| asio | v1.30.2 | FetchContent 自动 |
| open62541 | v1.4.6 | Vendor`third_party/open62541/``SOFTBUS_WITH_OPCUA=ON` |
| Python 3 | — | open62541 代码生成(构建时) |
| Threads | — | CMake 自动 |
| dbus-1 | 可选 | 系统安装Linux |
## 2. CMake 选项
| 选项 | 默认值 | 说明 |
|------|--------|------|
| `SOFTBUS_WITH_OPCUA` | ON | 启用 open62541 与 softbus_opcua |
| `SOFTBUS_WITH_ZMQ` | OFF | ZeroMQ 传输(未使用) |
## 3. 构建步骤
```bash
# 配置
cmake -B build -DSOFTBUS_WITH_OPCUA=ON
# 编译全部目标
cmake --build build
# 仅编译守护进程(自动构建依赖)
cmake --build build --target softbus_daemon
# 仅编译边缘代理
cmake --build build --target softbus_edge
```
### 3.1 输出目录
| 类型 | 路径 |
|------|------|
| 可执行文件 | `build/bin/` |
| 静态库 | `build/lib/` |
| 插件动态库 | `build/lib/` |
### 3.2 构建目标一览
| Target | 类型 | 输出 |
|--------|------|------|
| `softbus_core` | STATIC | `libsoftbus_core.a` |
| `softbus_api` | STATIC | `libsoftbus_api.a` |
| `softbus_transport` | STATIC | `libsoftbus_transport.a` |
| `softbus_opcua` | STATIC | `libsoftbus_opcua.a` |
| `protocol_modbus` | SHARED | `libprotocol_modbus.so` |
| `protocol_canopen` | SHARED | `libprotocol_canopen.so` |
| `softbus_edge` | EXECUTABLE | `softbus_edge` |
| `softbus_daemon` | EXECUTABLE | `softbus_daemon` |
## 4. CMake 模块
| 文件 | 职责 |
|------|------|
| `cmake/SoftbusPackage.cmake` | 包宏:`softbus_package``softbus_export_include``softbus_add_plugin` |
| `cmake/SoftbusPlatform.cmake` | 平台检测、线程、可选 DBus/UDEV/ZMQ、`softbus_apply_*` |
| `cmake/SoftbusThirdParty.cmake` | 第三方依赖open62541 vendor + FetchContentjson/asio |
## 5. 配置文件
### 5.1 daemon_profile.json
守护进程运行时配置:
```json
{
"opcua": {
"port": 4840,
"applicationName": "SoftBusDaemon"
},
"uplink": {
"listenHost": "127.0.0.1",
"listenPort": 9000
},
"plugins": {
"modbus": "protocol_modbus",
"canopen": "protocol_canopen"
},
"security": {
"tlsEnabled": false,
"certPath": "",
"keyPath": ""
}
}
```
| 字段 | 类型 | 默认值 | 说明 |
|------|------|--------|------|
| `opcua.port` | integer | 4840 | OPC UA 监听端口 |
| `opcua.applicationName` | string | SoftBusDaemon | OPC UA 应用名称 |
| `uplink.listenHost` | string | 127.0.0.1 | 上行 TCP 监听地址 |
| `uplink.listenPort` | integer | 9000 | 上行 TCP 监听端口 |
| `plugins.modbus` | string | protocol_modbus | Modbus 插件库名 |
| `plugins.canopen` | string | protocol_canopen | CANopen 插件库名 |
| `security.tlsEnabled` | boolean | false | TLS 开关Phase 1 未实现) |
| `security.certPath` | string | "" | 证书路径 |
| `security.keyPath` | string | "" | 私钥路径 |
### 5.2 softbus_model.json
对象模型与 OPC UA 地址空间定义:
```json
{
"site": "DemoSite",
"system": "DemoSystem",
"asset": "DemoAsset",
"devices": [
{
"stableDeviceKey": "SimModbusDevice",
"protocol": "modbus",
"points": [
{
"name": "Temperature",
"register": 40001,
"domain": "Process"
}
]
}
]
}
```
| 字段 | 类型 | 说明 |
|------|------|------|
| `site` | string | 站点名OPC UA Site 节点) |
| `system` | string | 系统名 |
| `asset` | string | 资产名 |
| `devices[].stableDeviceKey` | string | 设备稳定标识OPC UA Device 节点) |
| `devices[].protocol` | string | 协议标签 |
| `devices[].points[].name` | string | 测点名OPC UA Variable 节点) |
| `devices[].points[].register` | integer | 寄存器地址 |
| `devices[].points[].domain` | string | 数据域 |
### 5.3 ua_model.xml
Phase 2 静态 NodeSet2 XML 占位文件Phase 1 不使用。守护进程通过 `UaAddressSpaceBuilder` 从 JSON 动态构建地址空间。
## 6. 运行指南
### 6.1 双进程启动
```bash
# 终端 1启动守护进程
./build/bin/softbus_daemon \
--config config/daemon_profile.json \
--model config/softbus_model.json
# 终端 2启动边缘代理
./build/bin/softbus_edge --daemon 127.0.0.1:9000
```
### 6.2 自测模式
```bash
./build/bin/softbus_daemon --self-test
```
启动后自动向边缘发送一次写命令,验证 OPC UA Method → TCP 下行链路。
### 6.3 OPC UA 客户端验证
1. 使用 UaExpert 或其他 OPC UA 客户端
2. 连接 `opc.tcp://localhost:4840`
3. 浏览地址空间:
- `Objects → DemoSite → ... → Temperature`(读 Variable 值)
- `Objects → SoftBusControl → ExecuteCommand`(调用 Method
### 6.4 CLI 参数汇总
**softbus_daemon**
| 参数 | 默认值 |
|------|--------|
| `--config` | `config/daemon_profile.json` |
| `--model` | `config/softbus_model.json` |
| `--self-test` | 关闭 |
**softbus_edge**
| 参数 | 默认值 |
|------|--------|
| `--daemon` | `127.0.0.1:9000` |
| `--object-ref` | `DemoSite/DemoSystem/DemoAsset/SimModbusDevice/Temperature` |
## 7. 平台支持
| 平台 | 状态 | 备注 |
|------|------|------|
| Linux | 支持 | 主要开发与测试平台 |
| Windows | 支持 | 插件扩展名 `.dll`,路径解析适配 |
| macOS | 未测试 | 理论可编译 |
平台检测由 `cmake/SoftbusPlatform.cmake` 完成,定义 `SOFTBUS_PLATFORM_LINUX` / `SOFTBUS_PLATFORM_WINDOWS` 等宏。
## 8. 故障排查
| 现象 | 可能原因 | 处理 |
|------|----------|------|
| 守护进程启动失败plugin load | 插件 .so 不在搜索路径 | 确认 `config/lib/protocol_modbus.so``build/lib/libprotocol_modbus.so` 存在 |
| OPC UA 连接拒绝 | 端口被占用或 OPC UA 未启用 | 检查 4840 端口;确认 `SOFTBUS_WITH_OPCUA=ON` |
| Variable 值不更新 | 边缘未连接或 objectRef 不匹配 | 确认 edge 运行且 sourceId 与模型路径一致 |
| Method 调用失败 | JSON 格式错误或 objectRef 无效 | 检查入参 JSON 与五层路径 |
## 9. 项目目录结构
```
softbus_OPC/
├── CMakeLists.txt # 工作区根 CMake
├── cmake/ # CMake 模块
│ ├── SoftbusPackage.cmake
│ ├── SoftbusPlatform.cmake
│ └── SoftbusThirdParty.cmake
├── config/ # 运行时配置
│ ├── daemon_profile.json
│ ├── softbus_model.json
│ └── ua_model.xml
├── docs/ # 项目文档(本目录)
├── src/
│ ├── softbus_core/ # L0 内核
│ ├── softbus_api/ # L1 契约
│ ├── softbus_transport/ # L2 传输
│ ├── softbus_opcua/ # L2 OPC UA
│ ├── softbus_daemon/ # L3 守护进程
│ ├── softbus_edge/ # L3 边缘代理
│ └── plugins/ # L2 协议插件
│ ├── protocol_modbus/
│ └── protocol_canopen/
└── build/ # 构建输出gitignore
```