|
| 1 | +# RPCD AT Daemon 用户指南 |
| 2 | + |
| 3 | +## 为什么要使用这个服务 |
| 4 | +1. **队列机制**:该服务实现了一个命令队列,可以较大程度确保同一时间只有一个AT指令运行,避免模组端的ATD出现并发错误 |
| 5 | +2. **多种调用方式**:该服务可以借助 ubus 在路由端被其他服务调用,借助 rpcd 被 HTTP 服务调用,同时适合模组管理和 WebUI 开发 |
| 6 | +3. **稳定性更好**:相比模组自带的 ATD 服务,该服务在不同厂商的模组上表现更加稳定可靠 |
| 7 | + |
| 8 | +## 使用方法 |
| 9 | + |
| 10 | +### 1. 添加 rpcd 权限配置 |
| 11 | +为 at-daemon 添加 rpcd 权限(为了方便演示,下面添加的是免认证服务,生产环境请根据安全需求配置适当的认证) |
| 12 | +```bash |
| 13 | +echo << EOF > /usr/share/rpcd/acl.d/unauthenticated.json |
| 14 | +{ |
| 15 | + "unauthenticated": { |
| 16 | + "description": "Access controls for unauthenticated requests", |
| 17 | + "read": { |
| 18 | + "ubus": { |
| 19 | + "session": [ |
| 20 | + "access", |
| 21 | + "login" |
| 22 | + ], |
| 23 | + "at-daemon" : ["list","open","sendat","close"] |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | +EOF |
| 29 | +``` |
| 30 | + |
| 31 | +### 2. 通过 HTTP 接口发送 AT 指令 |
| 32 | +使用 curl 访问 rpcd 发送指令: |
| 33 | + |
| 34 | +```bash |
| 35 | +curl -s -X POST -H "Content-Type: application/json" -d ' |
| 36 | +{ |
| 37 | + "jsonrpc":"2.0", |
| 38 | + "id":1, |
| 39 | + "method":"call", |
| 40 | + "params":["00000000000000000000000000000000","at-daemon","sendat",{"at_port":"/dev/ttyUSB0","at_cmd":"at+cgmm"}] |
| 41 | +} |
| 42 | +' http://192.168.1.1/ubus |
| 43 | +``` |
| 44 | + |
| 45 | +**响应示例:** |
| 46 | +```json |
| 47 | +{ |
| 48 | + "jsonrpc": "2.0", |
| 49 | + "id": 1, |
| 50 | + "result": [ |
| 51 | + 0, |
| 52 | + { |
| 53 | + "port": "/dev/ttyUSB0", |
| 54 | + "command": "at+cgmm", |
| 55 | + "is_raw": 0, |
| 56 | + "sendonly": 0, |
| 57 | + "timeout": 5, |
| 58 | + "end_flag": "default", |
| 59 | + "end_flags_used": [ |
| 60 | + "OK", |
| 61 | + "ERROR", |
| 62 | + "+CMS ERROR:", |
| 63 | + "+CME ERROR:", |
| 64 | + "NO CARRIER" |
| 65 | + ], |
| 66 | + "status": "success", |
| 67 | + "response": "\r\nMH5000-82M\r\n\r\nOK\r\n", |
| 68 | + "response_length": 20, |
| 69 | + "end_flag_matched": "OK", |
| 70 | + "response_time_ms": 79 |
| 71 | + } |
| 72 | + ] |
| 73 | + } |
| 74 | +``` |
| 75 | + |
| 76 | +### 3. 通过 ubus 命令发送 AT 指令 |
| 77 | +也可以直接在路由器上使用 ubus 命令: |
| 78 | + |
| 79 | +```bash |
| 80 | +ubus call at-daemon sendat '{"at_port":"/dev/ttyUSB0","at_cmd":"at+cgmm"}' |
| 81 | +``` |
| 82 | + |
| 83 | +## API 参数详解 |
| 84 | +```bash |
| 85 | +ubus -v list at-daemon |
| 86 | +'at-daemon' @de5d6d53 |
| 87 | + "open":{"at_port":"String","baudrate":"Integer","databits":"Integer","parity":"Integer","stopbits":"Integer","timeout":"Integer"} |
| 88 | + "sendat":{"at_port":"String","timeout":"Integer","end_flag":"String","at_cmd":"String","raw_at_content":"String","sendonly":"Boolean"} |
| 89 | + "list":{} |
| 90 | + "close":{"at_port":"String"} |
| 91 | +``` |
| 92 | + |
| 93 | +### 方法说明 |
| 94 | + |
| 95 | +#### open - 打开串口 |
| 96 | +打开指定的串口设备(通常使用 `sendat` 时会自动调用此方法) |
| 97 | + |
| 98 | +**参数:** |
| 99 | +- `at_port` (String, 必需): 串口设备路径,如 `/dev/ttyUSB0` |
| 100 | +- `baudrate` (Integer, 可选): 波特率,默认 115200 |
| 101 | +- `databits` (Integer, 可选): 数据位,默认 8 |
| 102 | +- `parity` (Integer, 可选): 校验位,0=无校验,1=奇校验,2=偶校验 |
| 103 | +- `stopbits` (Integer, 可选): 停止位,1 或 2 |
| 104 | +- `timeout` (Integer, 可选): 超时时间(秒),默认 5 |
| 105 | + |
| 106 | +#### sendat - 发送 AT 指令 |
| 107 | +发送 AT 指令到指定串口并接收响应 |
| 108 | + |
| 109 | +**参数:** |
| 110 | +- `at_port` (String, 必需): 串口设备路径 |
| 111 | +- `at_cmd` (String, 可选): AT 指令内容,如 `at+cgmm` |
| 112 | +- `raw_at_content` (String, 可选): 16进制格式的原始内容,与 `at_cmd` 二选一 |
| 113 | +- `timeout` (Integer, 可选): 超时时间(秒),默认 5 |
| 114 | +- `sendonly` (Boolean, 可选): 是否只发送不接收响应,默认 false |
| 115 | +- `end_flag` (String, 可选): 自定义结束符,响应包含该字符时截断 |
| 116 | + |
| 117 | +**默认结束符:** `OK`、`ERROR`、`+CMS ERROR:`、`+CME ERROR:`、`NO CARRIER` |
| 118 | + |
| 119 | +#### list - 列出串口 |
| 120 | +列出所有已打开的串口连接 |
| 121 | + |
| 122 | +**参数:** 无 |
| 123 | + |
| 124 | +#### close - 关闭串口 |
| 125 | +关闭指定的串口连接 |
| 126 | + |
| 127 | +**参数:** |
| 128 | +- `at_port` (String, 必需): 要关闭的串口设备路径 |
| 129 | + |
| 130 | +## 重要注意事项 |
| 131 | + |
| 132 | +### 1. 与 QModem 共享串口 |
| 133 | +如果要与 QModem 共享串口,需要在 QModem 配置中启用 ubus at 模式,否则 ubus at 启动后会独占串口缓冲区,导致 QModem 无法获取信息。 |
| 134 | + |
| 135 | +**配置方法:** |
| 136 | +```bash |
| 137 | +uci set qmodem.1_1_2.use_ubus='1' |
| 138 | +uci commit qmodem |
| 139 | +``` |
| 140 | + |
| 141 | +### 2. 响应终止符 |
| 142 | +在项目中调用 ubus-at-daemon 时,请确保 AT 命令返回包含以下终止符之一: |
| 143 | +- `OK` |
| 144 | +- `ERROR` |
| 145 | +- `+CMS ERROR:` |
| 146 | +- `+CME ERROR:` |
| 147 | +- `NO CARRIER` |
| 148 | + |
| 149 | +或者手动通过 `end_flag` 参数指定终止符。否则请求会等待超时才返回,且 `status` 会返回错误。 |
| 150 | + |
| 151 | +### 3. 安全建议 |
| 152 | +生产环境中不建议使用免认证的 rpcd 配置,请根据实际安全需求配置适当的认证机制。 |
| 153 | + |
| 154 | +## 使用示例 |
| 155 | + |
| 156 | +### 查询模组型号 |
| 157 | +```bash |
| 158 | +ubus call at-daemon sendat '{"at_port":"/dev/ttyUSB0","at_cmd":"at+cgmm"}' |
| 159 | +``` |
| 160 | + |
| 161 | +### 查询信号强度 |
| 162 | +```bash |
| 163 | +ubus call at-daemon sendat '{"at_port":"/dev/ttyUSB0","at_cmd":"at+csq"}' |
| 164 | +``` |
| 165 | + |
| 166 | +### 仅发送指令不等待响应 |
| 167 | +```bash |
| 168 | +ubus call at-daemon sendat '{"at_port":"/dev/ttyUSB0","at_cmd":"at+cfun=1,1","sendonly":true}' |
| 169 | +``` |
| 170 | + |
| 171 | +### 自定义超时时间 |
| 172 | +```bash |
| 173 | +ubus call at-daemon sendat '{"at_port":"/dev/ttyUSB0","at_cmd":"at+cgmm","timeout":10}' |
| 174 | +``` |
0 commit comments