-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathavc.go
160 lines (147 loc) · 5.4 KB
/
avc.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
/*
* Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package omci
import (
"encoding/binary"
"fmt"
me "github.com/cboling/omci/v2/generated"
"github.com/google/gopacket"
)
type AttributeValueChangeMsg struct {
MeBasePacket
AttributeMask uint16
Attributes me.AttributeValueMap
}
func (omci *AttributeValueChangeMsg) String() string {
return fmt.Sprintf("%v, Mask: %#x, attributes: %v",
omci.MeBasePacket.String(), omci.AttributeMask, omci.Attributes)
}
// LayerType returns LayerTypeAttributeValueChange
func (omci *AttributeValueChangeMsg) LayerType() gopacket.LayerType {
return LayerTypeAttributeValueChange
}
// CanDecode returns the set of layer types that this DecodingLayer can decode
func (omci *AttributeValueChangeMsg) CanDecode() gopacket.LayerClass {
return LayerTypeAttributeValueChange
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (omci *AttributeValueChangeMsg) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypePayload
}
// DecodeFromBytes decodes the given bytes of an Attribute Value Change notification into this layer
func (omci *AttributeValueChangeMsg) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error {
// Common ClassID/EntityID decode in msgBase
err := omci.MeBasePacket.DecodeFromBytes(data, p, 4+2)
if err != nil {
return err
}
meDefinition, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
me.ParamData{EntityID: omci.EntityInstance})
if omciErr.StatusCode() != me.Success {
return omciErr.GetError()
}
// TODO: Support for encoding AVC into message type support not yet supported
//if !me.SupportsMsgType(meDefinition, me.AlarmNotification) {
// return me.NewProcessingError("managed entity does not support Alarm Notification Message-Type")
//}
maskOffset := 4
if omci.Extended {
maskOffset = 6
}
omci.AttributeMask = binary.BigEndian.Uint16(data[maskOffset:])
// Attribute decode
omci.Attributes, err = meDefinition.DecodeAttributes(omci.AttributeMask, data[maskOffset+2:],
p, byte(AttributeValueChangeType))
// TODO: Add support for attributes that can have an AVC associated with them and then add a check here
// Validate all attributes support AVC
//for attrName := range omci.attributes {
// attr, err := me.GetAttributeDefinitionByName(meDefinition.GetAttributeDefinitions(), attrName)
// if err != nil {
// return err
// }
// if attr.Index != 0 && !me.SupportsAttributeAVC(attr) {
// msg := fmt.Sprintf("attribute '%v' does not support AVC notifications", attrName)
// return me.NewProcessingError(msg)
// }
//}
return err
}
func decodeAttributeValueChange(data []byte, p gopacket.PacketBuilder) error {
omci := &AttributeValueChangeMsg{}
omci.MsgLayerType = LayerTypeAttributeValueChange
return decodingLayerDecoder(omci, data, p)
}
func decodeAttributeValueChangeExtended(data []byte, p gopacket.PacketBuilder) error {
omci := &AttributeValueChangeMsg{}
omci.MsgLayerType = LayerTypeAttributeValueChange
omci.Extended = true
return decodingLayerDecoder(omci, data, p)
}
// SerializeTo provides serialization of an Attribute Value Change Notification message
func (omci *AttributeValueChangeMsg) SerializeTo(b gopacket.SerializeBuffer, _ gopacket.SerializeOptions) error {
// Basic (common) OMCI Header is 8 octets, 10
err := omci.MeBasePacket.SerializeTo(b)
if err != nil {
return err
}
meDefinition, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
me.ParamData{EntityID: omci.EntityInstance})
if omciErr.StatusCode() != me.Success {
return omciErr.GetError()
}
// TODO: Add support for attributes that can have an AVC associated with them and then add a check here
// Validate all attributes support AVC
//for attrName := range omci.attributes {
// attr, err := me.GetAttributeDefinitionByName(meDefinition.GetAttributeDefinitions(), attrName)
// if err != nil {
// return err
// }
// if attr.Index != 0 && !me.SupportsAttributeAVC(attr) {
// msg := fmt.Sprintf("attribute '%v' does not support AVC notifications", attrName)
// return me.NewProcessingError(msg)
// }
//}
var maskOffset int
var bytesAvailable int
if omci.Extended {
maskOffset = 2
bytesAvailable = MaxExtendedLength - 12 - 4
} else {
maskOffset = 0
bytesAvailable = MaxBaselineLength - 10 - 8
}
bytes, err := b.AppendBytes(maskOffset + 2)
if err != nil {
return err
}
binary.BigEndian.PutUint16(bytes[maskOffset:], omci.AttributeMask)
// Attribute serialization
attributeBuffer := gopacket.NewSerializeBuffer()
if err, _ = meDefinition.SerializeAttributes(omci.Attributes, omci.AttributeMask,
attributeBuffer, byte(GetResponseType), bytesAvailable, false); err != nil {
return err
}
if omci.Extended {
binary.BigEndian.PutUint16(bytes, uint16(len(attributeBuffer.Bytes())+2))
}
bytes, err = b.AppendBytes(len(attributeBuffer.Bytes()))
if err != nil {
return err
}
copy(bytes, attributeBuffer.Bytes())
return nil
}