|
| 1 | +package RpcClientZeroTest |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/arduino/go-paths-helper" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + "github.com/vmihailenco/msgpack/v5" |
| 15 | + "go.bug.st/serial" |
| 16 | +) |
| 17 | + |
| 18 | +func TestBasicComm(t *testing.T) { |
| 19 | + // Get the upload port to upload the sketch |
| 20 | + fqbn, _, uploadPort := getFQBNAndPorts(t) |
| 21 | + { |
| 22 | + // Upload the sketch |
| 23 | + cli, err := paths.NewProcess(nil, "arduino-cli", "compile", "--fqbn", fqbn, "--library", "../..", "-u", "-p", uploadPort) |
| 24 | + require.NoError(t, err) |
| 25 | + cli.RedirectStderrTo(os.Stderr) |
| 26 | + cli.RedirectStdoutTo(os.Stdout) |
| 27 | + require.NoError(t, cli.Run()) |
| 28 | + } |
| 29 | + |
| 30 | + // Get the rpc and debug ports |
| 31 | + fqbn2, rpcPort, debugPort := getFQBNAndPorts(t) |
| 32 | + require.Equal(t, fqbn, fqbn2, "FQBN mismatch between upload and run ports: %s != %s", fqbn, fqbn2) |
| 33 | + |
| 34 | + // Connect to the RPC serial port |
| 35 | + _rpcSer, err := serial.Open(rpcPort, &serial.Mode{BaudRate: 115200}) |
| 36 | + rpcSer := &DebugStream{upstream: _rpcSer, portname: rpcPort} |
| 37 | + require.NoError(t, err) |
| 38 | + t.Cleanup(func() { rpcSer.Close() }) |
| 39 | + in := msgpack.NewDecoder(rpcSer) |
| 40 | + out := msgpack.NewEncoder(rpcSer) |
| 41 | + out.UseCompactInts(true) |
| 42 | + |
| 43 | + // Connect to the Debug serial port |
| 44 | + debugSer, err := serial.Open(debugPort, &serial.Mode{BaudRate: 115200}) |
| 45 | + require.NoError(t, err) |
| 46 | + t.Cleanup(func() { debugSer.Close() }) |
| 47 | + expectDebug := func(exp string) { |
| 48 | + buff := make([]byte, len(exp)) |
| 49 | + read := 0 |
| 50 | + for read < len(exp) { |
| 51 | + n, err := debugSer.Read(buff[read:]) |
| 52 | + read += n |
| 53 | + require.NoError(t, err) |
| 54 | + } |
| 55 | + require.Equal(t, exp, string(buff)) |
| 56 | + } |
| 57 | + |
| 58 | + // Timeout fallback: close the connection after 10 seconds, if the test do not go through |
| 59 | + go func() { |
| 60 | + time.Sleep(10 * time.Second) |
| 61 | + rpcSer.Close() |
| 62 | + debugSer.Close() |
| 63 | + }() |
| 64 | + |
| 65 | + // RPC: Receive an RPC call to the "mult" method with 2 arguments |
| 66 | + // and send back the result |
| 67 | + t.Run("RPCClientCallFloatArgs", func(t *testing.T) { |
| 68 | + arr, err := in.DecodeSlice() |
| 69 | + require.NoError(t, err) |
| 70 | + require.Equal(t, []any{int8(0), int8(1), "mult", []any{2.0, 3.0}}, arr) |
| 71 | + err = out.Encode([]any{1, 1, nil, 6.0}) |
| 72 | + require.NoError(t, err) |
| 73 | + expectDebug("mult(2.0, 3.0)\r\n") |
| 74 | + expectDebug("-> 6.00\r\n") |
| 75 | + }) |
| 76 | + |
| 77 | + // RPC: Receive an RPC call to the "mult" method with 1 argument (wrong number of arguments) |
| 78 | + // and send back an error with [int, string] format |
| 79 | + t.Run("RPCClientCallFloatArgsError", func(t *testing.T) { |
| 80 | + arr, err := in.DecodeSlice() |
| 81 | + require.NoError(t, err) |
| 82 | + require.Equal(t, []any{int8(0), int8(2), "mult", []any{2.0}}, arr) |
| 83 | + err = out.Encode([]any{1, 2, []any{1, "missing parameter"}, nil}) |
| 84 | + require.NoError(t, err) |
| 85 | + expectDebug("mult(2.0)\r\n") |
| 86 | + expectDebug("-> error\r\n") |
| 87 | + }) |
| 88 | + |
| 89 | + // RPC: Receive an RPC call to the "or" method with 1 or 2 arguments |
| 90 | + // and send back the result |
| 91 | + t.Run("RPCClientCallBoolArgs", func(t *testing.T) { |
| 92 | + arr, err := in.DecodeSlice() |
| 93 | + require.NoError(t, err) |
| 94 | + require.Equal(t, []any{int8(0), int8(3), "or", []any{true, false}}, arr) |
| 95 | + err = out.Encode([]any{1, 3, nil, true}) |
| 96 | + require.NoError(t, err) |
| 97 | + expectDebug("or(true, false)\r\n") |
| 98 | + expectDebug("-> true\r\n") |
| 99 | + |
| 100 | + arr, err = in.DecodeSlice() |
| 101 | + require.NoError(t, err) |
| 102 | + require.Equal(t, []any{int8(0), int8(4), "or", []any{false}}, arr) |
| 103 | + err = out.Encode([]any{1, 4, nil, false}) |
| 104 | + require.NoError(t, err) |
| 105 | + expectDebug("or(false)\r\n") |
| 106 | + expectDebug("-> false\r\n") |
| 107 | + }) |
| 108 | + |
| 109 | + // RPC: Receive an RPC call to the "mult" method with 1 argument (wrong number of arguments) |
| 110 | + // and send back a custom error without [int, string] format |
| 111 | + // t.Run("RPCClientCallFloatArgsErrorCustom", func(t *testing.T) { |
| 112 | + // arr, err := in.DecodeSlice() |
| 113 | + // require.NoError(t, err) |
| 114 | + // require.Equal(t, []any{int8(0), int8(3), "mult", []any{2.0}}, arr) |
| 115 | + // err = out.Encode([]any{1, 3, "missing parameter", nil}) |
| 116 | + // require.NoError(t, err) |
| 117 | + // expectDebug("mult(2.0)\r\n") |
| 118 | + // expectDebug("-> error\r\n") |
| 119 | + // }) |
| 120 | +} |
| 121 | + |
| 122 | +func getFQBNAndPorts(t *testing.T) (fqbn string, rpcPort string, uploadPort string) { |
| 123 | + cli, err := paths.NewProcess(nil, "arduino-cli", "board", "list", "--json") |
| 124 | + require.NoError(t, err) |
| 125 | + out, _, err := cli.RunAndCaptureOutput(t.Context()) |
| 126 | + require.NoError(t, err) |
| 127 | + var cliResult struct { |
| 128 | + DetectedPorts []struct { |
| 129 | + MatchingBoards []struct { |
| 130 | + Fqbn string `json:"fqbn"` |
| 131 | + } `json:"matching_boards"` |
| 132 | + Port struct { |
| 133 | + Address string `json:"address"` |
| 134 | + } `json:"port"` |
| 135 | + } `json:"detected_ports"` |
| 136 | + } |
| 137 | + require.NoError(t, json.Unmarshal(out, &cliResult)) |
| 138 | + checkFQBN := func(boardFQBN string) { |
| 139 | + if fqbn != boardFQBN { |
| 140 | + fqbn = boardFQBN |
| 141 | + uploadPort = "" |
| 142 | + rpcPort = "" |
| 143 | + } |
| 144 | + } |
| 145 | + for _, port := range cliResult.DetectedPorts { |
| 146 | + for _, board := range port.MatchingBoards { |
| 147 | + if board.Fqbn == "arduino:samd:arduino_zero_edbg" { |
| 148 | + checkFQBN("arduino:samd:arduino_zero_native") |
| 149 | + rpcPort = port.Port.Address |
| 150 | + } |
| 151 | + if board.Fqbn == "arduino:samd:arduino_zero_native" { |
| 152 | + checkFQBN(board.Fqbn) |
| 153 | + uploadPort = port.Port.Address |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + require.NotEmpty(t, uploadPort, "Upload port not found") |
| 158 | + require.NotEmpty(t, rpcPort, "Debug port not found") |
| 159 | + return fqbn, rpcPort, uploadPort |
| 160 | +} |
| 161 | + |
| 162 | +// DebugStream is a wrapper around io.ReadWriteCloser that logs the data |
| 163 | +// read and written to the stream in hex format. |
| 164 | +// It is used to debug the communication with the Arduino board. |
| 165 | +type DebugStream struct { |
| 166 | + upstream io.ReadWriteCloser |
| 167 | + portname string |
| 168 | +} |
| 169 | + |
| 170 | +func (d *DebugStream) Read(p []byte) (n int, err error) { |
| 171 | + n, err = d.upstream.Read(p) |
| 172 | + if err != nil { |
| 173 | + fmt.Printf("%s READ ERROR: %v\n", d.portname, err) |
| 174 | + } else { |
| 175 | + fmt.Printf("%s READ << %s\n", d.portname, hex.EncodeToString(p[:n])) |
| 176 | + } |
| 177 | + return n, err |
| 178 | +} |
| 179 | + |
| 180 | +func (d *DebugStream) Write(p []byte) (n int, err error) { |
| 181 | + n, err = d.upstream.Write(p) |
| 182 | + if err != nil { |
| 183 | + fmt.Printf("%s WRITE ERROR: %v\n", d.portname, err) |
| 184 | + } else { |
| 185 | + fmt.Printf("%s WRITE >> %s\n", d.portname, hex.EncodeToString(p[:n])) |
| 186 | + } |
| 187 | + return n, err |
| 188 | +} |
| 189 | + |
| 190 | +func (d *DebugStream) Close() error { |
| 191 | + err := d.upstream.Close() |
| 192 | + fmt.Printf("%s CLOSE", d.portname) |
| 193 | + if err != nil { |
| 194 | + fmt.Printf(" (ERROR: %v)", err) |
| 195 | + } |
| 196 | + fmt.Println() |
| 197 | + return err |
| 198 | +} |
0 commit comments