-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgetnext.go
More file actions
305 lines (280 loc) · 9.55 KB
/
getnext.go
File metadata and controls
305 lines (280 loc) · 9.55 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
* 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"
"errors"
"fmt"
me "github.com/cboling/omci/v2/generated"
"github.com/google/gopacket"
)
type GetNextRequest struct {
MeBasePacket
AttributeMask uint16
SequenceNumber uint16
}
func (omci *GetNextRequest) String() string {
return fmt.Sprintf("%v, Attribute Mask: %#x, Sequence Number: %v",
omci.MeBasePacket.String(), omci.AttributeMask, omci.SequenceNumber)
}
// LayerType returns LayerTypeGetNextRequest
func (omci *GetNextRequest) LayerType() gopacket.LayerType {
return LayerTypeGetNextRequest
}
// CanDecode returns the set of layer types that this DecodingLayer can decode
func (omci *GetNextRequest) CanDecode() gopacket.LayerClass {
return LayerTypeGetNextRequest
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (omci *GetNextRequest) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypePayload
}
// DecodeFromBytes decodes the given bytes of a Get Next Request into this layer
func (omci *GetNextRequest) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error {
// Common ClassID/EntityID decode in msgBase
var hdrSize int
if omci.Extended {
//start here
hdrSize = 6 + 4
} else {
hdrSize = 4 + 4
}
err := omci.MeBasePacket.DecodeFromBytes(data, p, hdrSize)
if err != nil {
return err
}
meDefinition, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
me.ParamData{EntityID: omci.EntityInstance})
if omciErr.StatusCode() != me.Success {
return omciErr.GetError()
}
// ME needs to support GetNext
if !me.SupportsMsgType(meDefinition, me.GetNext) {
return me.NewProcessingError("managed entity does not support Get Next Message-Type")
}
// Note: G.988 specifies that an error code of (3) should result if more
// than one attribute is requested
// TODO: Return error. Have flag to optionally allow it to be encoded
// TODO: Check that the attribute is a table attribute. Issue warning or return error
omci.AttributeMask = binary.BigEndian.Uint16(data[hdrSize-4:])
omci.SequenceNumber = binary.BigEndian.Uint16(data[hdrSize-2:])
return nil
}
func decodeGetNextRequest(data []byte, p gopacket.PacketBuilder) error {
omci := &GetNextRequest{}
omci.MsgLayerType = LayerTypeGetNextRequest
return decodingLayerDecoder(omci, data, p)
}
func decodeGetNextRequestExtended(data []byte, p gopacket.PacketBuilder) error {
omci := &GetNextRequest{}
omci.MsgLayerType = LayerTypeGetNextRequest
omci.Extended = true
return decodingLayerDecoder(omci, data, p)
}
// SerializeTo provides serialization of an Get Next Message Type Request
func (omci *GetNextRequest) 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()
}
// ME needs to support GetNext
if !me.SupportsMsgType(meDefinition, me.GetNext) {
return me.NewProcessingError("managed entity does not support Get Next Message-Type")
}
maskOffset := 0
if omci.Extended {
maskOffset = 2
}
bytes, err := b.AppendBytes(4 + maskOffset)
if err != nil {
return err
}
if omci.Extended {
binary.BigEndian.PutUint16(bytes, uint16(4))
}
binary.BigEndian.PutUint16(bytes[maskOffset:], omci.AttributeMask)
binary.BigEndian.PutUint16(bytes[maskOffset+2:], omci.SequenceNumber)
return nil
}
type GetNextResponse struct {
MeBasePacket
Result me.Results
AttributeMask uint16
Attributes me.AttributeValueMap
}
// SerializeTo provides serialization of an Get Next Message Type Response
func (omci *GetNextResponse) String() string {
return fmt.Sprintf("%v, Result: %v, Attribute Mask: %#x, Attributes: %v",
omci.MeBasePacket.String(), omci.Result, omci.AttributeMask, omci.Attributes)
}
// LayerType returns LayerTypeGetNextResponse
func (omci *GetNextResponse) LayerType() gopacket.LayerType {
return LayerTypeGetNextResponse
}
// CanDecode returns the set of layer types that this DecodingLayer can decode
func (omci *GetNextResponse) CanDecode() gopacket.LayerClass {
return LayerTypeGetNextResponse
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (omci *GetNextResponse) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypePayload
}
// DecodeFromBytes decodes the given bytes of a Get Next Response into this layer
func (omci *GetNextResponse) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error {
// Common ClassID/EntityID decode in msgBase
var hdrSize int
if omci.Extended {
//start here
hdrSize = 6 + 3
} else {
hdrSize = 4 + 3
}
err := omci.MeBasePacket.DecodeFromBytes(data, p, hdrSize)
if err != nil {
return err
}
meDefinition, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
me.ParamData{EntityID: omci.EntityInstance})
if omciErr.StatusCode() != me.Success {
return omciErr.GetError()
}
// ME needs to support Set
if !me.SupportsMsgType(meDefinition, me.GetNext) {
return me.NewProcessingError("managed entity does not support Get Next Message-Type")
}
var offset int
if omci.Extended {
offset = 2
}
omci.Result = me.Results(data[4+offset])
if omci.Result > 6 {
msg := fmt.Sprintf("invalid get next results code: %v, must be 0..6", omci.Result)
return errors.New(msg)
}
omci.AttributeMask = binary.BigEndian.Uint16(data[4+offset+1:])
// Attribute decode
omci.Attributes, err = meDefinition.DecodeAttributes(omci.AttributeMask, data[4+offset+3:], p, byte(GetNextResponseType))
if err != nil {
return err
}
// Validate all attributes support read
for attrName := range omci.Attributes {
attr, err := me.GetAttributeDefinitionByName(meDefinition.GetAttributeDefinitions(), attrName)
if err != nil {
return err
}
if attr.Index != 0 && !me.SupportsAttributeAccess(*attr, me.Read) {
msg := fmt.Sprintf("attribute '%v' does not support read access", attrName)
return me.NewProcessingError(msg)
}
}
if eidDef, eidDefOK := meDefinition.GetAttributeDefinitions()[0]; eidDefOK {
omci.Attributes[eidDef.GetName()] = omci.EntityInstance
return nil
}
panic("All Managed Entities have an EntityID attribute")
}
func decodeGetNextResponse(data []byte, p gopacket.PacketBuilder) error {
omci := &GetNextResponse{}
omci.MsgLayerType = LayerTypeGetNextResponse
return decodingLayerDecoder(omci, data, p)
}
func decodeGetNextResponseExtended(data []byte, p gopacket.PacketBuilder) error {
omci := &GetNextResponse{}
omci.MsgLayerType = LayerTypeGetNextResponse
omci.Extended = true
return decodingLayerDecoder(omci, data, p)
}
// SerializeTo provides serialization of an Get Next Message Type Response
func (omci *GetNextResponse) 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()
}
// ME needs to support Get
if !me.SupportsMsgType(meDefinition, me.GetNext) {
return me.NewProcessingError("managed entity does not support the Get Next Message-Type")
}
var offset int
if omci.Extended {
offset = 2
}
bytes, err := b.AppendBytes(offset + 3)
if err != nil {
return err
}
bytes[offset] = byte(omci.Result)
if omci.Result > 6 {
msg := fmt.Sprintf("invalid get next results code: %v, must be 0..6", omci.Result)
return errors.New(msg)
}
binary.BigEndian.PutUint16(bytes[offset+1:], omci.AttributeMask)
// Validate all attributes support read
for attrName := range omci.Attributes {
attr, err := me.GetAttributeDefinitionByName(meDefinition.GetAttributeDefinitions(), attrName)
if err != nil {
return err
}
if attr.Index != 0 && !me.SupportsAttributeAccess(*attr, me.Read) {
msg := fmt.Sprintf("attribute '%v' does not support read access", attrName)
return me.NewProcessingError(msg)
}
}
// Attribute serialization
switch omci.Result {
default:
break
case me.Success:
// TODO: Only Baseline supported at this time
if omci.Extended {
bytesAvailable := MaxExtendedLength - 13 - 4
attributeBuffer := gopacket.NewSerializeBuffer()
err, _ = meDefinition.SerializeAttributes(omci.Attributes, omci.AttributeMask,
attributeBuffer, byte(GetNextResponseType), bytesAvailable, false)
if err != nil {
return err
}
binary.BigEndian.PutUint16(bytes, uint16(len(attributeBuffer.Bytes())+3))
var newSpace []byte
newSpace, err = b.AppendBytes(len(attributeBuffer.Bytes()))
if err != nil {
return err
}
copy(newSpace, attributeBuffer.Bytes())
} else {
bytesAvailable := MaxBaselineLength - 11 - 8
err, _ = meDefinition.SerializeAttributes(omci.Attributes, omci.AttributeMask, b,
byte(GetNextResponseType), bytesAvailable, false)
if err != nil {
return err
}
}
}
return nil
}