| 1 | # Arduino pluggable monitor reference implementation |
| 2 | |
| 3 | The `dummy-monitor` tool is a command line program that interacts via stdio. It accepts commands as plain ASCII strings terminated with LF `\n` and sends response as JSON. |
| 4 | The "communication ports" returned by the tool are fake, this monitor is intenteded only for educational and testing purposes. |
| 5 | |
| 6 | ## How to build |
| 7 | |
| 8 | Install a recent go environment and run `go build`. The executable `dummy-monitor` will be produced in your working directory. |
| 9 | |
| 10 | ## Usage |
| 11 | |
| 12 | After startup, the tool will just stay idle waiting for commands. The available commands are: HELLO, DESCRIBE, CONFIGURE, OPEN, CLOSE and QUIT. |
| 13 | |
| 14 | #### HELLO command |
| 15 | |
| 16 | The `HELLO` command is used to establish the pluggable monitor protocol between client and monitor. |
| 17 | The format of the command is: |
| 18 | |
| 19 | `HELLO <PROTOCOL_VERSION> "<USER_AGENT>"` |
| 20 | |
| 21 | for example: |
| 22 | |
| 23 | `HELLO 1 "Arduino IDE"` |
| 24 | |
| 25 | or: |
| 26 | |
| 27 | `HELLO 1 "arduino-cli"` |
| 28 | |
| 29 | in this case the protocol version requested by the client is `1` (at the moment of writing there were no other revisions of the protocol). |
| 30 | The response to the command is: |
| 31 | |
| 32 | ```json |
| 33 | { |
| 34 | "eventType": "hello", |
| 35 | "message": "OK", |
| 36 | "protocolVersion": 1 |
| 37 | } |
| 38 | ``` |
| 39 | |
| 40 | `protocolVersion` is the protocol version that the monitor is going to use in the remainder of the communication. |
| 41 | |
| 42 | #### DESCRIBE command |
| 43 | |
| 44 | The `DESCRIBE` command returns a description of the communication port. The description will have metadata about the port configuration, and which parameters are available: |
| 45 | |
| 46 | <!-- prettier-ignore --> |
| 47 | ```json |
| 48 | { |
| 49 | "eventType": "describe", |
| 50 | "message": "OK", |
| 51 | "port_description": { |
| 52 | "protocol": "test", |
| 53 | "configuration_parameters": { |
| 54 | "echo": { |
| 55 | "label": "echo", |
| 56 | "type": "enum", |
| 57 | "value": [ |
| 58 | "on", |
| 59 | "off" |
| 60 | ], |
| 61 | "selected": "on" |
| 62 | }, |
| 63 | "speed": { |
| 64 | "label": "Baudrate", |
| 65 | "type": "enum", |
| 66 | "value": [ |
| 67 | "9600", |
| 68 | "19200", |
| 69 | "38400", |
| 70 | "57600", |
| 71 | "115200" |
| 72 | ], |
| 73 | "selected": "9600" |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | ``` |
| 79 | |
| 80 | Each parameter has a unique name (`baudrate`, `parity`, etc...), a `type` (in this case only enum but more types will be added in the future), and the `selected` value for each parameter. |
| 81 | |
| 82 | The parameter name can not contain spaces, and the allowed characters in the name are alphanumerics, underscore `_`, dot `.`, and dash `-`. |
| 83 | |
| 84 | The `enum` types must have a list of possible `value`. |
| 85 | |
| 86 | The client/IDE may expose these configuration values to the user via a config file or a GUI, in this case the `label` field may be used for a user readable description of the parameter. |
| 87 | |
| 88 | #### CONFIGURE command |
| 89 | |
| 90 | The `CONFIGURE` command sets configuration parameters for the communication port. The parameters can be changed one at a time and the syntax is: |
| 91 | |
| 92 | `CONFIGURE <PARAMETER_NAME> <VALUE>` |
| 93 | |
| 94 | The response to the command is: |
| 95 | |
| 96 | ```JSON |
| 97 | { |
| 98 | "eventType": "configure", |
| 99 | "message": "OK" |
| 100 | } |
| 101 | ``` |
| 102 | |
| 103 | or if there is an error: |
| 104 | |
| 105 | ```JSON |
| 106 | { |
| 107 | "eventType": "configure", |
| 108 | "message": "invalid value for parameter speed: 123456", |
| 109 | "error": true |
| 110 | } |
| 111 | ``` |
| 112 | |
| 113 | The currently selected parameters may be obtained using the `DESCRIBE` command. |
| 114 | |
| 115 | #### OPEN command |
| 116 | |
| 117 | The `OPEN` command opens a communication with the board, the data exchanged with the board will be transferred to the Client/IDE via TCP/IP. |
| 118 | |
| 119 | The Client/IDE must first TCP-Listen to a randomly selected port and send it to the monitor tool as part of the OPEN command. The syntax of the OPEN command is: |
| 120 | |
| 121 | `OPEN <CLIENT_IP_ADDRESS> <BOARD_PORT>` |
| 122 | |
| 123 | For example, let's suppose that the Client/IDE wants to communicate with the serial port `/dev/ttyACM0` then the sequence of actions to perform will be the following: |
| 124 | |
| 125 | 1. the Client/IDE must first listen to a random TCP port (let's suppose it chose `32123`) |
| 126 | 1. the Client/IDE sends the command `OPEN 127.0.0.1:32123 /dev/ttyACM0` to the monitor tool |
| 127 | 1. the monitor tool opens `/dev/ttyACM0` |
| 128 | 1. the monitor tool connects via TCP/IP to `127.0.0.1:32123` and start streaming data back and forth |
| 129 | |
| 130 | The answer to the `OPEN` command is: |
| 131 | |
| 132 | ```JSON |
| 133 | { |
| 134 | "eventType": "open", |
| 135 | "message": "OK" |
| 136 | } |
| 137 | ``` |
| 138 | |
| 139 | If the monitor tool cannot communicate with the board, or if the tool can not connect back to the TCP port, or if any other error condition happens: |
| 140 | |
| 141 | ```JSON |
| 142 | { |
| 143 | "eventType": "open", |
| 144 | "error": true, |
| 145 | "message": "unknown port /dev/ttyACM23" |
| 146 | } |
| 147 | ``` |
| 148 | |
| 149 | The board port will be opened using the parameters previously set through the `CONFIGURE` command. |
| 150 | |
| 151 | Once the port is opened, it may be unexpectedly closed at any time due to hardware failure, or because the Client/IDE closes the TCP/IP connection. In this case an asynchronous `port_closed` message must be generated from the monitor tool: |
| 152 | |
| 153 | ```JSON |
| 154 | { |
| 155 | "eventType": "port_closed", |
| 156 | "message": "serial port disappeared!" |
| 157 | } |
| 158 | ``` |
| 159 | |
| 160 | or |
| 161 | |
| 162 | ```JSON |
| 163 | { |
| 164 | "eventType": "port_closed", |
| 165 | "message": "lost TCP/IP connection with the client!", |
| 166 | "error": true |
| 167 | } |
| 168 | ``` |
| 169 | |
| 170 | #### CLOSE command |
| 171 | |
| 172 | The `CLOSE` command will close the currently opened port and close the TCP/IP connection used to communicate with the Client/IDE. The answer to the command is: |
| 173 | |
| 174 | ```JSON |
| 175 | { |
| 176 | "eventType": "close", |
| 177 | "message": "ok" |
| 178 | } |
| 179 | ``` |
| 180 | |
| 181 | or in case of error |
| 182 | |
| 183 | ```JSON |
| 184 | { |
| 185 | "eventType": "close", |
| 186 | "message": "port already closed", |
| 187 | "error": true |
| 188 | } |
| 189 | ``` |
| 190 | |
| 191 | #### QUIT command |
| 192 | |
| 193 | The `QUIT` command terminates the monitor. The response to `QUIT` is: |
| 194 | |
| 195 | ```JSON |
| 196 | { |
| 197 | "eventType": "quit", |
| 198 | "message": "OK" |
| 199 | } |
| 200 | ``` |
| 201 | |
| 202 | after this output the monitor exits. This command is supposed to always succeed. |
| 203 | |
| 204 | #### Invalid commands |
| 205 | |
| 206 | If the client sends an invalid or malformed command, the monitor should answer with: |
| 207 | |
| 208 | ```JSON |
| 209 | { |
| 210 | "eventType": "command_error", |
| 211 | "message": "Command XXXX not supported", |
| 212 | "error": true |
| 213 | } |
| 214 | ``` |
| 215 | |
| 216 | ### Example of usage |
| 217 | |
| 218 | A possible transcript of the monitor usage: |
| 219 | |
| 220 | ``` |
| 221 | HELLO 1 "test" |
| 222 | { |
| 223 | "eventType": "hello", |
| 224 | "message": "OK", |
| 225 | "protocolVersion": 1 |
| 226 | } |
| 227 | DESCRIBE |
| 228 | { |
| 229 | "eventType": "describe", |
| 230 | "message": "OK", |
| 231 | "port_description": { |
| 232 | "protocol": "test", |
| 233 | "configuration_parameters": { |
| 234 | "echo": { |
| 235 | "label": "echo", |
| 236 | "type": "enum", |
| 237 | "value": [ |
| 238 | "on", |
| 239 | "off" |
| 240 | ], |
| 241 | "selected": "on" |
| 242 | }, |
| 243 | "speed": { |
| 244 | "label": "Baudrate", |
| 245 | "type": "enum", |
| 246 | "value": [ |
| 247 | "9600", |
| 248 | "19200", |
| 249 | "38400", |
| 250 | "57600", |
| 251 | "115200" |
| 252 | ], |
| 253 | "selected": "9600" |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | CONFIGURE speed 19200 |
| 259 | { |
| 260 | "eventType": "configure", |
| 261 | "message": "OK" |
| 262 | } |
| 263 | DESCRIBE |
| 264 | { |
| 265 | "eventType": "describe", |
| 266 | "message": "OK", |
| 267 | "port_description": { |
| 268 | "protocol": "test", |
| 269 | "configuration_parameters": { |
| 270 | "echo": { |
| 271 | "label": "echo", |
| 272 | "type": "enum", |
| 273 | "value": [ |
| 274 | "on", |
| 275 | "off" |
| 276 | ], |
| 277 | "selected": "on" |
| 278 | }, |
| 279 | "speed": { |
| 280 | "label": "Baudrate", |
| 281 | "type": "enum", |
| 282 | "value": [ |
| 283 | "9600", |
| 284 | "19200", |
| 285 | "38400", |
| 286 | "57600", |
| 287 | "115200" |
| 288 | ], |
| 289 | "selected": "19200" |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | OPEN 127.0.0.1:5678 "test" |
| 295 | { |
| 296 | "eventType": "open", |
| 297 | "message": "OK" |
| 298 | } |
| 299 | CLOSE |
| 300 | { |
| 301 | "eventType": "close", |
| 302 | "message": "OK" |
| 303 | } |
| 304 | QUIT |
| 305 | { |
| 306 | "eventType": "quit", |
| 307 | "message": "OK" |
| 308 | } |
| 309 | $ |
| 310 | ``` |
| 311 | |
| 312 | On another terminal tab to test it you can run `nc -l -p 5678` before running the `OPEN 127.0.0.1:5678 "test"` command. After that you can write messages in that terminal tab and see them being echoed. |
| 313 | |
| 314 | ## Security |
| 315 | |
| 316 | If you think you found a vulnerability or other security-related bug in this project, please read our |
| 317 | [security policy](https://github.com/arduino/pluggable-monitor-protocol-handler/security/policy) and report the bug to our Security Team 🛡️ |
| 318 | Thank you! |
| 319 | |
| 320 | e-mail contact: [email protected] |
| 321 | |
| 322 | ## License |
| 323 | |
| 324 | ```txt |
| 325 | SPDX-FileCopyrightText: Arduino s.r.l. and/or its affiliated companies |
| 326 | SPDX-License-Identifier: GPL-3.0-or-later |
| 327 | ``` |