-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnpdu.go
197 lines (161 loc) · 4.67 KB
/
npdu.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package encoding
import (
"github.com/NubeDev/bacnet/btypes"
"github.com/NubeDev/bacnet/btypes/ndpu"
)
//https://github.com/bacnet-stack/bacnet-stack/blob/master/src/bacnet/npdu.c#L391
// NPDU encodes the network layer control message
func (e *Encoder) NPDU(n *btypes.NPDU) {
e.write(n.Version)
// Prepare metadata into the second byte
meta := NPDUMetadata(0)
meta.SetNetworkLayerMessage(n.IsNetworkLayerMessage)
meta.SetExpectingReply(n.ExpectingReply)
meta.SetPriority(n.Priority)
// Check to see if we have a net address. If so set destination true
if n.Destination != nil {
if n.Destination.Net != 0 {
meta.SetDestination(true)
}
}
// Repeat for source
if n.Source != nil {
if n.Source.Net != 0 {
meta.SetSource(true)
}
}
e.write(meta)
if meta.HasDestination() {
e.write(n.Destination.Net)
// Address
e.write(n.Destination.Len)
e.write(n.Destination.Adr)
}
if meta.HasSource() {
e.write(n.Source.Net)
// Address
e.write(n.Source.Len)
e.write(n.Source.Adr)
}
// Hop count is after source
if meta.HasDestination() {
e.write(n.HopCount)
}
if meta.IsNetworkLayerMessage() {
e.write(n.NetworkLayerMessageType)
// If the network value is above 0x80, then it should have a vendor id
if n.NetworkLayerMessageType >= 0x80 {
e.write(n.VendorId)
}
}
}
func (d *Decoder) Address(a *btypes.Address) {
d.decode(&a.Net) //decode the network address
d.decode(&a.Len)
// Make space for address
a.Adr = make([]uint8, a.Len) //decode the device hardware mac addr
d.decode(a.Adr)
}
type RouterToNetworkList struct {
Source []btypes.Address
}
// NPDU encodes the network layer control message
func (d *Decoder) NPDU(n *btypes.NPDU) (addr []btypes.Address, err error) {
d.decode(&n.Version)
// Prepare metadata into the second byte
meta := NPDUMetadata(0)
d.decode(&meta)
n.ExpectingReply = meta.ExpectingReply()
n.IsNetworkLayerMessage = meta.IsNetworkLayerMessage()
n.Priority = meta.Priority()
if meta.HasDestination() {
n.Destination = &btypes.Address{}
d.Address(n.Destination)
}
if meta.HasSource() {
n.Source = &btypes.Address{}
d.Address(n.Source)
}
if meta.HasDestination() {
d.decode(&n.HopCount)
} else {
n.HopCount = 0
}
if meta.IsNetworkLayerMessage() {
d.decode(&n.NetworkLayerMessageType)
if n.NetworkLayerMessageType > 0x80 {
d.decode(&n.VendorId)
}
if n.NetworkLayerMessageType == ndpu.NetworkIs { //used for decoding a bacnet network number on a What-Is-Network-Number 0x12
n.Source = &btypes.Address{}
d.decode(&n.Source.Net)
}
if n.NetworkLayerMessageType == ndpu.IamRouterToNetwork { //used for decoding a bacnet network number on a What-Is-Network-Number 0x12
n.Source = &btypes.Address{}
var nets []btypes.Address
d.decode(&n.Source.Net) //decode the first network
nets = append(nets, *n.Source)
size := d.len()
for i := d.len(); i <= size; i++ {
d.decode(&n.Source.Net)
for _, adr := range nets {
if adr.Net != n.Source.Net { //make sure that a network is only added once
nets = append(nets, *n.Source)
}
}
}
addr = nets
}
}
return addr, d.Error()
}
// NPDUMetadata includes additional metadata about npdu message
type NPDUMetadata byte
const maskNetworkLayerMessage = 1 << 7
const maskDestination = 1 << 5
const maskSource = 1 << 3
const maskExpectingReply = 1 << 2
// General setter for the info bits using the mask
func (meta *NPDUMetadata) setInfoMask(b bool, mask byte) {
*meta = NPDUMetadata(setInfoMask(byte(*meta), b, mask))
}
// CheckMask uses mask to check bit position
func (meta *NPDUMetadata) checkMask(mask byte) bool {
return (*meta & NPDUMetadata(mask)) > 0
}
// IsNetworkLayerMessage returns true if it is a network layer message
func (n *NPDUMetadata) IsNetworkLayerMessage() bool {
return n.checkMask(maskNetworkLayerMessage)
}
func (n *NPDUMetadata) SetNetworkLayerMessage(b bool) {
n.setInfoMask(b, maskNetworkLayerMessage)
}
// Priority returns priority
func (n *NPDUMetadata) Priority() btypes.NPDUPriority {
// Encoded in bit 0 and 1
return btypes.NPDUPriority(byte(*n) & 3)
}
// SetPriority for NPDU
func (n *NPDUMetadata) SetPriority(p btypes.NPDUPriority) {
// Clear the first two bits
//*n &= (0xF - 3)
*n |= NPDUMetadata(p)
}
func (n *NPDUMetadata) HasDestination() bool {
return n.checkMask(maskDestination)
}
func (n *NPDUMetadata) SetDestination(b bool) {
n.setInfoMask(b, maskDestination)
}
func (n *NPDUMetadata) HasSource() bool {
return n.checkMask(maskSource)
}
func (n *NPDUMetadata) SetSource(b bool) {
n.setInfoMask(b, maskSource)
}
func (n *NPDUMetadata) ExpectingReply() bool {
return n.checkMask(maskExpectingReply)
}
func (n *NPDUMetadata) SetExpectingReply(b bool) {
n.setInfoMask(b, maskExpectingReply)
}