Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ Requires=
[Service]
# Put the micro in a ready state.
ExecStartPre=-/usr/bin/gpioset -c /dev/gpiochip1 -t0 37=0
ExecStart=/usr/bin/arduino-router --unix-port /var/run/arduino-router.sock --serial-port /dev/ttyHS1 --serial-baudrate 115200
# End the boot animation after the router is started.
ExecStartPost=/usr/bin/gpioset -c /dev/gpiochip1 -t0 70=1
# Launch router and end the boot animation after the router is started (--after-start).
ExecStart=/usr/bin/arduino-router --unix-port /var/run/arduino-router.sock --serial-port /dev/ttyHS1 --serial-baudrate 115200 --after-start "/usr/bin/gpioset -c /dev/gpiochip1 -t0 70=1"
StandardOutput=journal
StandardError=journal
Restart=always
Expand Down
24 changes: 24 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"net"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
Expand All @@ -35,6 +36,7 @@ import (
"github.com/arduino/arduino-router/msgpackrpc"
networkapi "github.com/arduino/arduino-router/network-api"

"github.com/arduino/go-paths-helper"
"github.com/spf13/cobra"
"go.bug.st/f"
"go.bug.st/serial"
Expand All @@ -51,6 +53,7 @@ type Config struct {
SerialPortAddr string
SerialBaudRate int
MonitorPortAddr string
StartCallback string
}

func main() {
Expand Down Expand Up @@ -86,6 +89,7 @@ func main() {
cmd.Flags().StringVarP(&cfg.SerialPortAddr, "serial-port", "p", "", "Serial port address")
cmd.Flags().IntVarP(&cfg.SerialBaudRate, "serial-baudrate", "b", 115200, "Serial port baud rate")
cmd.Flags().StringVarP(&cfg.MonitorPortAddr, "monitor-port", "m", "127.0.0.1:7500", "Listening port for MCU monitor proxy")
cmd.Flags().StringVar(&cfg.StartCallback, "after-start", "", "Command to execute when the router has successfully completed startup")
if err := cmd.Execute(); err != nil {
slog.Error("Error executing command.", "error", err)
}
Expand Down Expand Up @@ -214,6 +218,10 @@ func startRouter(cfg Config) error {
return true, nil
})
f.Assert(err == nil, "Failed to register $/serial/close method")

var started sync.WaitGroup
started.Add(1)
initialized := sync.OnceFunc(started.Done)
go func() {
for {
serialOpened.L.Lock()
Expand All @@ -236,6 +244,7 @@ func startRouter(cfg Config) error {
time.Sleep(5 * time.Second)
continue
}
initialized()
slog.Info("Opened serial connection", "serial", cfg.SerialPortAddr)
wr := &MsgpackDebugStream{Name: cfg.SerialPortAddr, Upstream: serialPort}

Expand All @@ -252,6 +261,7 @@ func startRouter(cfg Config) error {
<-routerExit
}
}()
started.Wait()
}

// Wait for incoming connections on all listeners
Expand All @@ -270,6 +280,20 @@ func startRouter(cfg Config) error {
}()
}

// Execute start callback if specified
if cfg.StartCallback != "" {
slog.Info("Executing start callback", "cmd", cfg.StartCallback)
cb, err := paths.NewProcess(nil, strings.Split(cfg.StartCallback, " ")...)
if err != nil {
slog.Error("Failed to start callback process", "err", err)
return err
}
if err := cb.Run(); err != nil {
slog.Error("Start callback process failed", "err", err)
return err
}
}

// Sleep forever until interrupted
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, os.Interrupt, syscall.SIGTERM)
Expand Down