forked from jsimonetti/go-artnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
154 lines (136 loc) · 3.73 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package artnet
import (
"fmt"
"net"
"github.com/jsimonetti/go-artnet/packet"
"github.com/jsimonetti/go-artnet/packet/code"
)
// Address contains a universe address
type Address struct {
Net uint8 // 0-128
SubUni uint8
}
// String returns a string representation of Address
func (a Address) String() string {
return fmt.Sprintf("%d:%d.%d", a.Net, a.SubUni>>4, a.SubUni&0x0f)
}
// Integer returns the integer representation of Address
func (a Address) Integer() int {
return int(uint16(a.Net)<<8 | uint16(a.SubUni))
}
// InputPort contains information for an input port
type InputPort struct {
Address Address
Type code.PortType
Status code.GoodInput
}
// OutputPort contains information for an input port
type OutputPort struct {
Address Address
Type code.PortType
Status code.GoodOutput
}
// NodeConfig is a representation of a single node.
type NodeConfig struct {
OEM uint16
Version uint16
BiosVersion uint8
Manufacturer string
Type code.StyleCode
Name string
Description string
Ethernet net.HardwareAddr
IP net.IP
BindIP net.IP
BindIndex uint8
Port uint16
Report []code.NodeReportCode
Status1 code.Status1
Status2 code.Status2
BaseAddress Address
InputPorts []InputPort
OutputPorts []OutputPort
}
// ArtPollReplyFromConfig will return a ArtPollReplyPacket from the NodeConfig
// TODO: make this a more complete packet by adding the other NodeConfig fields
func ArtPollReplyFromConfig(c NodeConfig) *packet.ArtPollReplyPacket {
p := &packet.ArtPollReplyPacket{
OpCode: code.OpPollReply,
Port: packet.ArtNetPort,
Oem: c.OEM,
VersionInfo: c.Version,
UBEAVersion: c.BiosVersion,
Style: c.Type,
BindIndex: c.BindIndex,
Status1: c.Status1,
Status2: c.Status2,
NetSwitch: c.BaseAddress.Net,
SubSwitch: c.BaseAddress.SubUni,
}
copy(p.IPAddress[0:4], c.IP.To4())
copy(p.ESTAmanufacturer[0:2], c.Manufacturer)
copy(p.ShortName[0:18], c.Name)
copy(p.LongName[0:64], c.Description)
copy(p.NodeReport[0:64], c.Report)
copy(p.BindIP[0:4], c.BindIP.To4())
copy(p.Macaddress[0:6], c.Ethernet)
return p
}
// ConfigFromArtPollReply will return a Config from the information in the ArtPollReplyPacket
func ConfigFromArtPollReply(p packet.ArtPollReplyPacket) NodeConfig {
nodeConfig := NodeConfig{
OEM: p.Oem,
Version: p.VersionInfo,
BiosVersion: p.UBEAVersion,
Manufacturer: decodeString(p.ESTAmanufacturer[:]),
Type: p.Style,
Name: decodeString(p.ShortName[:]),
Description: decodeString(p.LongName[:]),
Report: p.NodeReport[:],
Ethernet: p.Macaddress[:],
IP: p.IPAddress[:],
BindIP: p.BindIP[:],
BindIndex: p.BindIndex,
Port: p.Port,
Status1: p.Status1,
Status2: p.Status2,
BaseAddress: Address{
Net: p.NetSwitch,
SubUni: p.SubSwitch,
},
}
for i := 0; i < int(p.NumPorts) && i < 4; i++ {
if p.PortTypes[i].Output() {
nodeConfig.OutputPorts = append(nodeConfig.OutputPorts, OutputPort{
Address: Address{
Net: nodeConfig.BaseAddress.Net,
SubUni: nodeConfig.BaseAddress.SubUni | p.SwOut[i],
},
Type: p.PortTypes[i],
Status: p.GoodOutput[i],
})
}
if p.PortTypes[i].Input() {
nodeConfig.InputPorts = append(nodeConfig.InputPorts, InputPort{
Address: Address{
Net: nodeConfig.BaseAddress.Net,
SubUni: nodeConfig.BaseAddress.SubUni | p.SwIn[i],
},
Type: p.PortTypes[i],
Status: p.GoodInput[i],
})
}
}
return nodeConfig
}
// decodeString will take a byte slice and create an ASCII string
// the ASCII strings are 0 terminated
func decodeString(b []byte) (str string) {
for _, c := range b {
if c == 0 {
return
}
str += string(c)
}
return
}