forked from nyotiemon/gosem
/
attributeDescriptorWithSelection.go
84 lines (70 loc) · 1.95 KB
/
attributeDescriptorWithSelection.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
package dlms
import (
"encoding/binary"
"fmt"
"github.com/Circutor/gosem/pkg/axdr"
)
type AttributeDescriptorWithSelection struct {
ClassID uint16
InstanceID Obis
AttributeID int8
AccessDescriptor *SelectiveAccessDescriptor
}
// CreateAttributeDescriptorWithSelection will create AttributeDescriptorWithSelection object
// SelectiveAccessDescriptor is allowed to be nil value therefore pointer
func CreateAttributeDescriptorWithSelection(c uint16, i string, a int8, sad *SelectiveAccessDescriptor) *AttributeDescriptorWithSelection {
ob := *CreateObis(i)
return &AttributeDescriptorWithSelection{ClassID: c, InstanceID: ob, AttributeID: a, AccessDescriptor: sad}
}
func (ad AttributeDescriptorWithSelection) Encode() (out []byte, err error) {
var output []byte
var c [2]byte
binary.BigEndian.PutUint16(c[:], ad.ClassID)
output = append(output, c[:]...)
output = append(output, ad.InstanceID.Bytes()...)
output = append(output, byte(ad.AttributeID))
if ad.AccessDescriptor == nil {
output = append(output, 0)
} else {
output = append(output, 1)
val, e := ad.AccessDescriptor.Encode()
if e != nil {
err = e
return
}
output = append(output, val...)
}
out = output
return
}
func DecodeAttributeDescriptorWithSelection(ori *[]byte) (out AttributeDescriptorWithSelection, err error) {
src := *ori
if len(src) < 11 {
err = fmt.Errorf("byte slice length must be at least 11 bytes")
return
}
_, out.ClassID, err = axdr.DecodeLongUnsigned(&src)
if err != nil {
return
}
out.InstanceID, err = DecodeObis(&src)
if err != nil {
return
}
out.AttributeID = int8(src[0])
haveAccDesc := src[1]
src = src[2:]
if haveAccDesc == 0x0 {
var nilAccDesc *SelectiveAccessDescriptor
out.AccessDescriptor = nilAccDesc
} else {
accDesc, errAcc := DecodeSelectiveAccessDescriptor(&src)
if errAcc != nil {
err = errAcc
return
}
out.AccessDescriptor = &accDesc
}
(*ori) = (*ori)[len((*ori))-len(src):]
return
}