# softbus_daemon API 接口表 本文档汇总 `src/api` 当前对外接口,分为: - D-Bus 直接方法 - `executeCommand` JSON 命令接口 - HTTP REST 接口 ## 1) D-Bus 服务基础信息 | 项目 | 值 | |---|---| | Service | `com.softbus.Daemon` | | ObjectPath | `/com/softbus/Daemon` | | Interface | `com.softbus.Daemon` | | 适配类 | `IpcDBus` | ## 2) D-Bus 方法表(IpcDBus) | 方法 | 入参 | 返回 | 说明 | |---|---|---|---| | `isRunning()` | 无 | `bool` | 核心服务是否初始化完成 | | `ping()` | 无 | `QString` | 健康探针,固定返回 `pong` | | `getVersion()` | 无 | `QString` | 进程版本号 | | `getStatus()` | 无 | `QString(JSON)` | 返回服务状态(pid/role/running/timestamp 等) | | `getPid()` | 无 | `qint64` | 返回守护进程 PID | | `shutdown()` | 无 | `void` | 关闭 CoreService 并退出进程 | | `reloadConfig()` | 无 | `bool` | 预留,当前为占位实现 | | `executeCommand(requestJson)` | `QString(JSON)` | `QString(JSON)` | 统一 JSON 命令入口 | ## 3) executeCommand 协议 ### 3.1 请求格式 ```json { "id": "req-001", "action": "add_filter", "params": {} } ``` ### 3.2 响应格式 ```json { "id": "req-001", "code": 0, "message": "ok", "data": {} } ``` ## 4) action 路由表(CommandDispatcher) | action | params 关键字段 | 成功 code | 失败 code | 说明 | |---|---|---:|---:|---| | `add_filter` | `id`, `endpointHash?`, `deviceId?`, `stableKey?`, `condition?` | `0` | `4001` | 添加过滤规则 | | `remove_filter` | `id` | `0` | `4041` | 删除过滤规则 | | `add_transform_rule` | `id`, `endpointHash?`, `deviceId?`, `stableKey?`, `scale?`, `offset?`, `unit?` | `0` | `4002` | 添加转换规则 | | `remove_transform_rule` | `id` | `0` | `4042` | 删除转换规则 | | `add_mapping_rule` | `compositeKey`, `domPath`, `scale?`, `offset?`, `unit?` | `0` | `4004` | 新增/更新映射规则 | | `remove_mapping_rule` | `compositeKey` | `0` | `4043` | 删除映射规则 | | `list_mapping_rules` | 无 | `0` | - | 返回映射规则列表 | | `discovery_start` | `ttlMs?` | `0` | - | 开启嗅探模式 | | `discovery_stop` | 无 | `0` | - | 关闭嗅探模式 | | `discovery_clear` | 无 | `0` | - | 清空发现池 | | `list_rules` | 无 | `0` | - | 返回规则计数状态 | | `get_status` | 无 | `0` | - | 返回规则计数 + discovery/mapping 状态 | | 其他未知 action | - | - | `4040` | 未知动作 | ## 5) HTTP REST 路由表(RestApiRoutes) ### 5.1 通用路由 | 方法 | 路径 | 说明 | |---|---|---| | `GET` | `/health` | 健康检查,返回 `{"ok": true}` | | `GET` | `/v1/status` | 透传 `get_status` 的状态响应 | | `POST` | `/v1/command` | 统一命令入口(请求体为 executeCommand JSON) | ### 5.2 Discovery 路由 | 方法 | 路径 | 请求参数 | 说明 | |---|---|---|---| | `POST` | `/api/v1/discovery/start` | body: `ttlMs?` | 开启嗅探;可设置自动关闭 TTL | | `POST` | `/api/v1/discovery/stop` | 无 | 关闭嗅探 | | `GET` | `/api/v1/discovery/status` | 无 | 获取发现池运行状态 | | `POST` | `/api/v1/discovery/clear` | 无 | 清空发现池样本 | | `GET` | `/api/v1/discovery/samples` | query: `offset?`, `limit?`, `endpointHash?`, `protocol?` | 拉取样本(按更新时间倒序,且过滤已映射 key) | ### 5.3 Mapping 路由 | 方法 | 路径 | 请求参数 | 说明 | |---|---|---|---| | `POST` | `/api/v1/mapping/bind` | body: `compositeKey`, `domPath`, `scale?`, `offset?`, `unit?` | 绑定或更新映射规则 | | `POST` | `/api/v1/mapping/unbind` | body: `compositeKey` | 删除映射规则 | | `GET` | `/api/v1/mapping/list` | 无 | 列出当前映射规则 | ## 6) 通用错误码约定(当前实现) | code | message | 说明 | |---:|---|---| | `0` | `ok` | 成功 | | `4000` | `invalid_json` | 请求 JSON 无法解析或非对象 | | `4001` | `invalid_filter_rule` | 过滤规则参数非法 | | `4002` | `invalid_transform_rule` | 转换规则参数非法 | | `4003` | `invalid_action` | action 为空或分发器不可用 | | `4004` | `invalid_mapping_rule` | 映射规则参数非法 | | `4040` | `unknown_action` | action 未注册 | | `4041` | `filter_not_found` | 删除过滤规则时未找到 | | `4042` | `transform_rule_not_found` | 删除转换规则时未找到 | | `4043` | `mapping_rule_not_found` | 删除映射规则时未找到 | ## 7) 维护建议 - 新增 action 时,同时更新: - `CommandDispatcher::dispatch(...)` - 本文档第 4 节 action 表 - 新增 REST 路由时,同时更新: - `RestApiRoutes.cpp` - 本文档第 5 节路由表 - 若扩展返回结构,保持 `code/message/data` 三元组不变,避免客户端兼容性问题。