Skip to content

Commit

Permalink
Partial implementation of the port info for ArtPollReply (#37)
Browse files Browse the repository at this point in the history
* go.mod updated

Signed-off-by: 72nd <msg@frg72.com>

* go.mod fixed

Signed-off-by: 72nd <msg@frg72.com>

* revert all changes since creation of branch

Signed-off-by: 72nd <msg@frg72.com>

* validate Node input and output port count

Signed-off-by: 72nd <msg@frg72.com>

* announce number of ports in art poll reply

Signed-off-by: 72nd <msg@frg72.com>

* node config validation now checks if in-/outputs with the same index have the same type

Signed-off-by: 72nd <msg@frg72.com>

* announce port types in art poll reply

Signed-off-by: 72nd <msg@frg72.com>

* fixed validation bug

Signed-off-by: 72nd <msg@frg72.com>

---------

Signed-off-by: 72nd <msg@frg72.com>
Co-authored-by: Jeroen Simonetti <jsimonetti@users.noreply.github.com>
  • Loading branch information
72nd and jsimonetti committed Feb 1, 2024
1 parent d52b119 commit e4f1b1b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
60 changes: 60 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ func ArtPollReplyFromConfig(c NodeConfig) *packet.ArtPollReplyPacket {
Status2: c.Status2,
NetSwitch: c.BaseAddress.Net,
SubSwitch: c.BaseAddress.SubUni,
NumPorts: c.NumberOfPorts(),
PortTypes: c.PortTypes(),
}

copy(p.IPAddress[0:4], c.IP.To4())
Expand All @@ -91,6 +93,64 @@ func ArtPollReplyFromConfig(c NodeConfig) *packet.ArtPollReplyPacket {
return p
}

// NumberOfPorts returns the count of node ports. This method assumes that
// NodeConfig is validated.
func (c NodeConfig) NumberOfPorts() uint16 {
if len(c.InputPorts) > len(c.OutputPorts) {
return uint16(len(c.InputPorts))
}
return uint16(len(c.OutputPorts))
}

// PortType merges the InputPorts and OutputPorts config into a single PortTypes
// definition. This method assumes that NodeConfig is validated.
func (c NodeConfig) PortTypes() [4]code.PortType {
rsl := [4]code.PortType{}
for i := 0; i < 4; i++ {
tmp := code.PortType(0)
if len(c.InputPorts) > i {
tmp = tmp.WithInput(true)
rsl[i] = tmp.WithType(c.InputPorts[i].Type.Type())
}
if len(c.OutputPorts) > i {
tmp = tmp.WithOutput(true)
rsl[i] = tmp.WithType(c.OutputPorts[i].Type.Type())
}
}
return rsl
}

// validate will check the config and return an error if something is not valid.
// The main objective of this method is to check if the in- and output-ports configured
// by the user can be announced on the Art-Net network. It checks:
//
// - At max 4 in- and/or outputs are supported per node.
// - If a port supports in- and output at the same time the protocol type has to be
// the same for the input port of the same index as the output port.
func (c NodeConfig) validate() error {
if len(c.InputPorts) > 4 {
return fmt.Errorf("validation error: more than 4 input ports configured (%d) for the node, this isn't supported by the library", len(c.InputPorts))
}
if len(c.OutputPorts) > 4 {
return fmt.Errorf("validation error: more than 4 output ports configured (%d) for the node, this isn't supported by the library", len(c.InputPorts))
}
for i := 0; i < 4; i++ {
if len(c.InputPorts) <= i || len(c.OutputPorts) <= i {
continue
}
if c.InputPorts[i].Type.Type() != c.OutputPorts[i].Type.Type() {
return fmt.Errorf(
"validation error: the type (%s) of input port %d has a different type (%s) than output port %d, input and output ports with the same index must have the same type",
c.InputPorts[i].Type.Type(),
i+1,
c.OutputPorts[i].Type.Type(),
i+1,
)
}
}
return nil
}

// ConfigFromArtPollReply will return a Config from the information in the ArtPollReplyPacket
func ConfigFromArtPollReply(p packet.ArtPollReplyPacket) NodeConfig {
nodeConfig := NodeConfig{
Expand Down
3 changes: 3 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ func (n *Node) isShutdown() bool {

// Start will start the controller
func (n *Node) Start() error {
if err := n.Config.validate(); err != nil {
return err
}
n.log.With(Fields{"ip": n.Config.IP.String(), "type": n.Config.Type.String()}).Debug("node started")

n.sendCh = make(chan netPayload, 10)
Expand Down

0 comments on commit e4f1b1b

Please sign in to comment.