Skip to content

Commit 4b42a29

Browse files
committed
Added Null monitor for testing
1 parent d35a3c9 commit 4b42a29

File tree

5 files changed

+106
-17
lines changed

5 files changed

+106
-17
lines changed

arduino/monitors/null.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// This file is part of arduino-cli.
2+
//
3+
// Copyright 2021 ARDUINO SA (http://www.arduino.cc/)
4+
//
5+
// This software is released under the GNU General Public License version 3,
6+
// which covers the main part of arduino-cli.
7+
// The terms of this license can be found at:
8+
// https://www.gnu.org/licenses/gpl-3.0.en.html
9+
//
10+
// You can be released from the requirements of the above licenses by purchasing
11+
// a commercial license. Buying such a license is mandatory if you want to
12+
// modify or otherwise use the software for commercial activities involving the
13+
// Arduino software without disclosing the source code of your own applications.
14+
// To purchase a commercial license, send an email to [email protected].
15+
16+
package monitors
17+
18+
import (
19+
"log"
20+
"time"
21+
)
22+
23+
// NullMonitor outputs zeros at a constant rate and discards anything sent
24+
type NullMonitor struct {
25+
started time.Time
26+
sent int
27+
bps float64
28+
}
29+
30+
// OpenNullMonitor creates a monitor that outputs the same character at a fixed
31+
// rate.
32+
func OpenNullMonitor(bytesPerSecondRate float64) *NullMonitor {
33+
log.Printf("Started streaming at %f\n", bytesPerSecondRate)
34+
return &NullMonitor{
35+
started: time.Now(),
36+
bps: bytesPerSecondRate,
37+
}
38+
}
39+
40+
// Close the connection
41+
func (mon *NullMonitor) Close() error {
42+
return nil
43+
}
44+
45+
// Read bytes from the port
46+
func (mon *NullMonitor) Read(bytes []byte) (int, error) {
47+
for {
48+
elapsed := time.Now().Sub(mon.started).Seconds()
49+
n := int(elapsed*mon.bps) - mon.sent
50+
if n == 0 {
51+
// Delay until the next char...
52+
time.Sleep(time.Millisecond)
53+
continue
54+
}
55+
if len(bytes) < n {
56+
n = len(bytes)
57+
}
58+
mon.sent += n
59+
for i := 0; i < n; i++ {
60+
bytes[i] = 0
61+
}
62+
return n, nil
63+
}
64+
}
65+
66+
// Write bytes to the port
67+
func (mon *NullMonitor) Write(bytes []byte) (int, error) {
68+
// Discard all chars
69+
return len(bytes), nil
70+
}

commands/daemon/monitor.go

+11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package daemon
1717

1818
import (
19+
"errors"
1920
"fmt"
2021
"io"
2122

@@ -61,6 +62,16 @@ func (s *MonitorService) StreamingOpen(stream rpc.Monitor_StreamingOpenServer) e
6162
if mon, err = monitors.OpenSerialMonitor(config.GetTarget(), int(baudRate)); err != nil {
6263
return err
6364
}
65+
66+
case rpc.MonitorConfig_NULL:
67+
if addCfg, ok := config.GetAdditionalConfig().AsMap()["OutputRate"]; !ok {
68+
mon = monitors.OpenNullMonitor(100.0) // 100 bytes per second as default
69+
} else if outputRate, ok := addCfg.(float64); !ok {
70+
return errors.New("OutputRate in Null monitor must be a float64")
71+
} else {
72+
// get the Monitor instance
73+
mon = monitors.OpenNullMonitor(outputRate)
74+
}
6475
}
6576

6677
// we'll use these channels to communicate with the goroutines

go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ golang.org/x/crypto v0.0.0-20200406173513-056763e48d71/go.mod h1:LzIPMQfyMNhhGPh
285285
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
286286
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
287287
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
288+
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 h1:XQyxROzUlZH+WIQwySDgnISgOivlhjIEwaQaJEJrrN0=
288289
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
289290
golang.org/x/net v0.0.0-20180406214816-61147c48b25b/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
290291
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=

rpc/monitor/monitor.pb.go

+20-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rpc/monitor/monitor.proto

+4-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ message StreamingOpenReq {
5050
// Tells the monitor which target to open and provides additional parameters
5151
// that might be needed to configure the target or the monitor itself.
5252
message MonitorConfig {
53-
enum TargetType { SERIAL = 0; }
53+
enum TargetType {
54+
SERIAL = 0;
55+
NULL = 99;
56+
}
5457

5558
// The target name.
5659
string target = 1;

0 commit comments

Comments
 (0)