Skip to content

Commit

Permalink
Using the whole common dialect, rather than just a few messages from it
Browse files Browse the repository at this point in the history
Added basic routing rules for COMMAND_LONG, COMMAND_ACK and COMMAND_INT

Fixed some liniting suggestions

Added additional print statement to allow route debbugging
  • Loading branch information
Kester-Broatch authored and aler9 committed Jun 18, 2023
1 parent fd06c65 commit 47644ad
Showing 1 changed file with 41 additions and 14 deletions.
55 changes: 41 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import (

"github.com/alecthomas/kong"
"github.com/bluenviron/gomavlib/v2"
"github.com/bluenviron/gomavlib/v2/pkg/dialect"
"github.com/bluenviron/gomavlib/v2/pkg/dialects/common"
"github.com/bluenviron/gomavlib/v2/pkg/message"
)

var version = "v0.0.0"
Expand Down Expand Up @@ -92,6 +90,7 @@ var cli struct {
Version bool `help:"print version."`
Quiet bool `short:"q" help:"suppress info messages."`
Print bool `help:"print routed frames."`
PrintRoutes bool `help:"print routes applied for messages with targets."`
PrintErrors bool
HbDisable bool `help:"disable heartbeats."`
HbVersion string `enum:"1,2" help:"set mavlink version of heartbeats." default:"1"`
Expand Down Expand Up @@ -185,17 +184,6 @@ func newProgram(args []string) (*program, error) {
econfs[i] = conf
}

// decode/encode only a minimal set of messages.
// other messages change too frequently and cannot be integrated into a static tool.
msgs := []message.Message{}
if !cli.HbDisable || !cli.StreamreqDisable {
msgs = append(msgs, &common.MessageHeartbeat{})
}
if !cli.StreamreqDisable {
msgs = append(msgs, &common.MessageRequestDataStream{})
}
dialect := &dialect.Dialect{3, msgs} //nolint:govet

ctx, ctxCancel := context.WithCancel(context.Background())

p := &program{
Expand All @@ -205,7 +193,7 @@ func newProgram(args []string) (*program, error) {

p.node, err = gomavlib.NewNode(gomavlib.NodeConf{
Endpoints: econfs,
Dialect: dialect,
Dialect: common.Dialect,
OutVersion: func() gomavlib.Version {
if cli.HbVersion == "2" {
return gomavlib.V2
Expand Down Expand Up @@ -309,6 +297,45 @@ func (p *program) run() {
}
}

routeMsg := false // Flag to mark a msg for routing
targetSystem := uint8(0)
targetComponent := uint8(0)
switch msg := evt.Message().(type) {
case *common.MessageCommandLong:
routeMsg = true
targetSystem = msg.TargetSystem
targetComponent = msg.TargetComponent
case *common.MessageCommandAck:
routeMsg = true
targetSystem = msg.TargetSystem
targetComponent = msg.TargetComponent
case *common.MessageCommandInt:
routeMsg = true
targetSystem = msg.TargetSystem
targetComponent = msg.TargetComponent
}

if routeMsg {
if targetSystem > 0 { // Route only if it's non-broadcast command
for remoteNode := range p.nodeHandler.remoteNodes { // Iterates through connected nodes
if remoteNode.SystemID == targetSystem {
if remoteNode.ComponentID == targetComponent ||
targetComponent < 1 { // Route if compid matches or is a broadcast
if remoteNode.Channel != evt.Channel { // Prevents Loops
if cli.PrintRoutes {
fmt.Println("Routing msg ", evt.Message().GetID(), " from ", evt.Channel, "--->", remoteNode.Channel)
}
p.node.WriteFrameTo(remoteNode.Channel, evt.Frame)
} else {
fmt.Println("Warning: channel ", remoteNode.Channel, " attempted to send to itself, discarding ")
}
}
}
}
continue
}
}

// route message to every other channel
p.node.WriteFrameExcept(evt.Channel, evt.Frame)

Expand Down

0 comments on commit 47644ad

Please sign in to comment.