From 109a6e945af80e502868fafd8caad66c6b971cd6 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 11 Nov 2025 17:47:13 +0100 Subject: [PATCH 1/3] Added a start callback flag to the router --- main.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/main.go b/main.go index 9cba6de..efb08e9 100644 --- a/main.go +++ b/main.go @@ -25,6 +25,7 @@ import ( "net" "os" "os/signal" + "strings" "sync" "syscall" "time" @@ -34,6 +35,7 @@ import ( "github.com/arduino/arduino-router/msgpackrouter" "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" @@ -51,6 +53,7 @@ type Config struct { SerialPortAddr string SerialBaudRate int MonitorPortAddr string + StartCallback string } func main() { @@ -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) } @@ -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() @@ -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} @@ -252,6 +261,7 @@ func startRouter(cfg Config) error { <-routerExit } }() + started.Wait() } // Wait for incoming connections on all listeners @@ -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) From 7f566b5b0910ed1fddcc8f18147dd8f7d4e4d32f Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 11 Nov 2025 17:50:13 +0100 Subject: [PATCH 2/3] Execute gpio-set internally --- .../arduino-router/etc/systemd/system/arduino-router.service | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/debian/arduino-router/etc/systemd/system/arduino-router.service b/debian/arduino-router/etc/systemd/system/arduino-router.service index 5a4aed5..1b7e303 100644 --- a/debian/arduino-router/etc/systemd/system/arduino-router.service +++ b/debian/arduino-router/etc/systemd/system/arduino-router.service @@ -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 From eb8a1c1b7dc1ec408323f864f5b980898eab6ba8 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Tue, 11 Nov 2025 18:02:36 +0100 Subject: [PATCH 3/3] Please linter --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index efb08e9..b6c5c0b 100644 --- a/main.go +++ b/main.go @@ -35,8 +35,8 @@ import ( "github.com/arduino/arduino-router/msgpackrouter" "github.com/arduino/arduino-router/msgpackrpc" networkapi "github.com/arduino/arduino-router/network-api" - "github.com/arduino/go-paths-helper" + "github.com/arduino/go-paths-helper" "github.com/spf13/cobra" "go.bug.st/f" "go.bug.st/serial"