-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgetcurrent.go
348 lines (317 loc) · 11.9 KB
/
getcurrent.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
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* 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 GetCurrentDataRequest struct {
MeBasePacket
AttributeMask uint16
}
func (omci *GetCurrentDataRequest) String() string {
return fmt.Sprintf("%v, Attribute Mask: %#x",
omci.MeBasePacket.String(), omci.AttributeMask)
}
// LayerType returns LayerTypeGetCurrentDataRequest
func (omci *GetCurrentDataRequest) LayerType() gopacket.LayerType {
return LayerTypeGetCurrentDataRequest
}
// CanDecode returns the set of layer types that this DecodingLayer can decode
func (omci *GetCurrentDataRequest) CanDecode() gopacket.LayerClass {
return LayerTypeGetCurrentDataRequest
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (omci *GetCurrentDataRequest) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypePayload
}
// DecodeFromBytes decodes the given bytes of a Get Current Data Request into this layer
func (omci *GetCurrentDataRequest) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error {
// Common ClassID/EntityID decode in msgBase
var offset int
if omci.Extended {
offset = 6
} else {
offset = 4
}
hdrSize := offset + 2
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.GetCurrentData) {
return me.NewProcessingError("managed entity does not support Get Current Data Message-Type")
}
// Note: G.988 specifies that an error code of (3) should result if more
// than one attribute is requested
omci.AttributeMask = binary.BigEndian.Uint16(data[offset:])
return nil
}
func decodeGetCurrentDataRequest(data []byte, p gopacket.PacketBuilder) error {
omci := &GetCurrentDataRequest{}
omci.MsgLayerType = LayerTypeGetCurrentDataRequest
return decodingLayerDecoder(omci, data, p)
}
func decodeGetCurrentDataRequestExtended(data []byte, p gopacket.PacketBuilder) error {
omci := &GetCurrentDataRequest{}
omci.MsgLayerType = LayerTypeGetCurrentDataRequest
omci.Extended = true
return decodingLayerDecoder(omci, data, p)
}
// SerializeTo provides serialization of an Get Current Data Request message
func (omci *GetCurrentDataRequest) 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.GetCurrentData) {
return me.NewProcessingError("managed entity does not support Get Current Data Message-Type")
}
bytes, err := b.AppendBytes(2)
if err != nil {
return err
}
binary.BigEndian.PutUint16(bytes, omci.AttributeMask)
return nil
}
type GetCurrentDataResponse struct {
MeBasePacket
Result me.Results
AttributeMask uint16
UnsupportedAttributeMask uint16
FailedAttributeMask uint16
Attributes me.AttributeValueMap
}
func (omci *GetCurrentDataResponse) String() string {
return fmt.Sprintf("%v, Result: %d (%v), Attribute Mask: %#x, Attributes: %v",
omci.MeBasePacket.String(), omci.Result, omci.Result, omci.AttributeMask, omci.Attributes)
}
// LayerType returns LayerTypeGetCurrentDataResponse
func (omci *GetCurrentDataResponse) LayerType() gopacket.LayerType {
return LayerTypeGetCurrentDataResponse
}
// CanDecode returns the set of layer types that this DecodingLayer can decode
func (omci *GetCurrentDataResponse) CanDecode() gopacket.LayerClass {
return LayerTypeGetCurrentDataResponse
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (omci *GetCurrentDataResponse) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypePayload
}
// DecodeFromBytes decodes the given bytes of a Get Current Data Response into this layer
func (omci *GetCurrentDataResponse) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error {
// Common ClassID/EntityID decode in msgBase
var offset, length int
if omci.Extended {
offset = 6
length = 7
} else {
offset = 4
length = 3
}
hdrSize := offset + length
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.GetCurrentData) {
return me.NewProcessingError("managed entity does not support Get Current Data Message-Type")
}
omci.Result = me.Results(data[offset])
omci.AttributeMask = binary.BigEndian.Uint16(data[offset+1:])
switch omci.Result {
case me.ProcessingError, me.NotSupported, me.UnknownEntity, me.UnknownInstance, me.DeviceBusy:
return nil // Done (do not try and decode attributes)
case me.AttributeFailure:
if omci.Extended {
omci.UnsupportedAttributeMask = binary.BigEndian.Uint16(data[offset+3:])
omci.FailedAttributeMask = binary.BigEndian.Uint16(data[offset+5:])
} else {
omci.UnsupportedAttributeMask = binary.BigEndian.Uint16(data[32:])
omci.FailedAttributeMask = binary.BigEndian.Uint16(data[34:])
}
}
// Attribute decode. Note that the ITU-T G.988 specification states that the
// Unsupported and Failed attribute masks are always present
// but only valid if the status code== 9. However some XGS
// ONUs (T&W and Alpha, perhaps more) will use these last 4
// octets for data if the status code == 0 in a baseline GET
// Response. So this behaviour is anticipated here as well
// and will be allowed in favor of greater interoperability.
omci.Attributes, err = meDefinition.DecodeAttributes(omci.AttributeMask, data[hdrSize:], p, byte(GetCurrentDataResponseType))
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
}
return errors.New("all Managed Entities have an EntityID attribute")
}
func decodeGetCurrentDataResponse(data []byte, p gopacket.PacketBuilder) error {
omci := &GetCurrentDataResponse{}
omci.MsgLayerType = LayerTypeGetCurrentDataResponse
return decodingLayerDecoder(omci, data, p)
}
func decodeGetCurrentDataResponseExtended(data []byte, p gopacket.PacketBuilder) error {
omci := &GetCurrentDataResponse{}
omci.MsgLayerType = LayerTypeGetCurrentDataResponse
omci.Extended = true
return decodingLayerDecoder(omci, data, p)
}
// SerializeTo provides serialization of an Get Current Data Message Type Response
func (omci *GetCurrentDataResponse) SerializeTo(b gopacket.SerializeBuffer, opts 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.GetCurrentData) {
return me.NewProcessingError("managed entity does not support the Get Current Data Message-Type")
}
var resultOffset, hdrSize int
if omci.Extended {
resultOffset = 2
hdrSize = resultOffset + 1 + 2 + 2 + 2 // length + result + masks
} else {
resultOffset = 0
hdrSize = resultOffset + 1 + 2 // length + result + attr-mask
}
bytes, err := b.AppendBytes(hdrSize)
if err != nil {
return err
}
bytes[resultOffset] = byte(omci.Result)
binary.BigEndian.PutUint16(bytes[resultOffset+1:], omci.AttributeMask)
// Validate all attributes support read
for attrName := range omci.Attributes {
var attr *me.AttributeDefinition
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:
if omci.Extended {
binary.BigEndian.PutUint16(bytes, 7) // Length
binary.BigEndian.PutUint32(bytes[resultOffset+3:], 0)
}
break
case me.Success, me.AttributeFailure:
var available int
if omci.Extended {
available = MaxExtendedLength - 10 - 3 - 4 - 4 // Less: header, result+mask, optional-masks mic
} else {
available = MaxBaselineLength - 8 - 3 - 4 - 8 // hdr, result+mask, optional-masks, trailer
}
// Serialize to temporary buffer if we may need to reset values due to
// recoverable truncation errors
attributeBuffer := gopacket.NewSerializeBuffer()
var failedMask uint16
err, failedMask = meDefinition.SerializeAttributes(omci.Attributes, omci.AttributeMask,
attributeBuffer, byte(GetCurrentDataResponseType), available, opts.FixLengths)
if err != nil {
return err
}
if failedMask != 0 {
// Not all attributes would fit
omci.FailedAttributeMask |= failedMask
omci.AttributeMask &= ^failedMask
omci.Result = me.AttributeFailure
// Adjust already recorded values
bytes[resultOffset] = byte(omci.Result)
binary.BigEndian.PutUint16(bytes[resultOffset+1:], omci.AttributeMask)
}
if omci.Extended {
// Set length and any failure masks
binary.BigEndian.PutUint16(bytes, uint16(len(attributeBuffer.Bytes())+7))
if omci.Result == me.AttributeFailure {
binary.BigEndian.PutUint16(bytes[resultOffset+3:], omci.UnsupportedAttributeMask)
binary.BigEndian.PutUint16(bytes[resultOffset+5:], omci.FailedAttributeMask)
} else {
binary.BigEndian.PutUint32(bytes[resultOffset+3:], 0)
}
}
// Copy over attributes to the original serialization buffer
var newSpace []byte
newSpace, err = b.AppendBytes(len(attributeBuffer.Bytes()))
if err != nil {
return err
}
copy(newSpace, attributeBuffer.Bytes())
if !omci.Extended {
// Calculate space left. Max - msgType header - OMCI trailer - spacedUsedSoFar
bytesLeft := MaxBaselineLength - 4 - 8 - len(b.Bytes())
var remainingBytes []byte
remainingBytes, err = b.AppendBytes(bytesLeft + 4)
if err != nil {
return me.NewMessageTruncatedError(err.Error())
}
copy(remainingBytes, lotsOfZeros[:])
if omci.Result == me.AttributeFailure {
binary.BigEndian.PutUint16(remainingBytes[bytesLeft-4:bytesLeft-2], omci.UnsupportedAttributeMask)
binary.BigEndian.PutUint16(remainingBytes[bytesLeft-2:bytesLeft], omci.FailedAttributeMask)
}
}
}
return nil
}