forked from ulbios/bacnet
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathencoding.go
More file actions
168 lines (123 loc) · 4.69 KB
/
encoding.go
File metadata and controls
168 lines (123 loc) · 4.69 KB
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
package bacnet
import (
"github.com/jonalfarlinga/bacnet/plumbing"
"github.com/jonalfarlinga/bacnet/services"
)
const (
DEFAULT_ACCEPTED_SIZE = 1024
DEFAULT_SEGMENTATION_SUPPORT = 0x3 // No segmentation
)
func NewWhois() ([]byte, error) {
bvlc := plumbing.NewBVLC(plumbing.BVLCFuncBroadcast)
npdu := plumbing.NewNPDU(false, false, false, false)
u := services.NewUnconfirmedWhoIs(bvlc, npdu)
return u.MarshalBinary()
}
func NewIAm(instN uint32, vendorId uint16) ([]byte, error) {
bvlc := plumbing.NewBVLC(plumbing.BVLCFuncBroadcast)
npdu := plumbing.NewNPDU(false, true, false, false)
npdu.DNET = 0xFFFF
npdu.DLEN = 0
npdu.Hop = 0xFF
u := services.NewUnconfirmedIAm(bvlc, npdu)
u.APDU.Objects = services.IAmObjects(instN,
DEFAULT_ACCEPTED_SIZE, DEFAULT_SEGMENTATION_SUPPORT, vendorId)
u.SetLength()
return u.MarshalBinary()
}
func NewCACK(service uint8, objectType uint16, instN uint32, propertyId uint16, value float32) ([]byte, error) {
bvlc := plumbing.NewBVLC(plumbing.BVLCFuncUnicast)
npdu := plumbing.NewNPDU(false, false, false, false)
c := services.NewComplexACK(bvlc, npdu)
c.APDU.Service = service
c.APDU.InvokeID = 1
c.APDU.Objects = services.ComplexACKObjects(objectType, instN, propertyId, value)
c.SetLength()
return c.MarshalBinary()
}
func NewSACK(service uint8) ([]byte, error) {
bvlc := plumbing.NewBVLC(plumbing.BVLCFuncUnicast)
npdu := plumbing.NewNPDU(false, false, false, false)
s := services.NewSimpleACK(bvlc, npdu)
s.APDU.Service = service
s.APDU.InvokeID = 1
s.SetLength()
return s.MarshalBinary()
}
func NewError(service, errorClass, errorCode uint8) ([]byte, error) {
bvlc := plumbing.NewBVLC(plumbing.BVLCFuncUnicast)
npdu := plumbing.NewNPDU(false, false, false, false)
e := services.NewError(bvlc, npdu)
e.APDU.Service = service
e.APDU.InvokeID = 1
e.APDU.Objects = services.ErrorObjects(errorClass, errorCode)
e.SetLength()
return e.MarshalBinary()
}
func NewReadProperty(objectType uint16, instanceNumber uint32, propertyId uint16) ([]byte, error) {
bvlc := plumbing.NewBVLC(plumbing.BVLCFuncUnicast)
npdu := plumbing.NewNPDU(false, false, false, true)
c := services.NewConfirmedReadProperty(bvlc, npdu)
c.APDU.Service = services.ServiceConfirmedReadProperty
c.APDU.MaxSize = 5
c.APDU.InvokeID = 1
c.APDU.Objects = services.ConfirmedReadPropertyObjects(objectType, instanceNumber, propertyId)
c.SetLength()
return c.MarshalBinary()
}
func NewReadPropertyMultiple(objectType uint16, instanceNumber uint32, propertyIds []uint16) ([]byte, error) {
bvlc := plumbing.NewBVLC(plumbing.BVLCFuncUnicast)
npdu := plumbing.NewNPDU(false, false, false, true)
c := services.NewConfirmedReadProperty(bvlc, npdu)
c.APDU.Service = services.ServiceConfirmedReadPropMultiple
c.APDU.MaxSeg = 7
c.APDU.MaxSize = 5
c.APDU.InvokeID = 1
c.APDU.Flags = 2
c.APDU.Objects = services.ConfirmedReadPropertyMultipleObjects(objectType, instanceNumber, propertyIds)
c.SetLength()
return c.MarshalBinary()
}
func NewReadRange(objectType uint16, instanceNumber uint32, propertyId uint16, rangeStart uint16, length int32) ([]byte, error) {
bvlc := plumbing.NewBVLC(plumbing.BVLCFuncUnicast)
npdu := plumbing.NewNPDU(false, false, false, true)
c, _ := services.NewConfirmedReadRange(bvlc, npdu)
c.APDU.Service = services.ServiceConfirmedReadRange
c.APDU.MaxSeg = 7
c.APDU.MaxSize = 5
c.APDU.InvokeID = 1
c.APDU.Flags = 2
c.APDU.Objects = services.ConfirmedReadRangeObjects(objectType, instanceNumber, propertyId, rangeStart, length)
c.SetLength()
return c.MarshalBinary()
}
func NewSubscribeCOV(objectType uint16, instanceNumber uint32, processId uint, lifetime uint, expect, cancel bool) ([]byte, error) {
bvlc := plumbing.NewBVLC(plumbing.BVLCFuncUnicast)
npdu := plumbing.NewNPDU(false, false, false, true)
c, _ := services.NewConfirmedSubscribeCOV(bvlc, npdu)
c.APDU.Service = services.ServiceConfirmedSubscribeCOV
c.APDU.MaxSeg = 7
c.APDU.MaxSize = 5
c.APDU.InvokeID = 1
c.APDU.Flags = 2
if cancel {
c.APDU.Objects = services.CancelCOVOBjects(
processId, objectType, instanceNumber)
} else {
c.APDU.Objects = services.COVObjects(
processId, objectType, instanceNumber, expect, lifetime)
}
c.SetLength()
return c.MarshalBinary()
}
func NewWriteProperty(objectType uint16, instanceNumber uint32, propertyId uint16, data interface{}) ([]byte, error) {
bvlc := plumbing.NewBVLC(plumbing.BVLCFuncUnicast)
npdu := plumbing.NewNPDU(false, false, false, true)
c := services.NewConfirmedWriteProperty(bvlc, npdu)
c.APDU.Service = services.ServiceConfirmedWriteProperty
c.APDU.MaxSize = 5
c.APDU.InvokeID = 1
c.APDU.Objects = services.ConfirmedWritePropertyObjects(objectType, instanceNumber, propertyId, data)
c.SetLength()
return c.MarshalBinary()
}