blob: 41f7a1f8c4666281612ff7c2339547411f833041 [file] [log] [blame]
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -07001/*
2 * Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net)
Matteo Scandolof9d43412021-01-12 11:11:34 -08003 * Copyright 2020-present Open Networking Foundation
4
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -07005 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
Matteo Scandolof9d43412021-01-12 11:11:34 -08008
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -07009 * http://www.apache.org/licenses/LICENSE-2.0
Matteo Scandolof9d43412021-01-12 11:11:34 -080010
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070011 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070016 */
Matteo Scandolof9d43412021-01-12 11:11:34 -080017
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070018package omci
19
20import (
21 "encoding/binary"
22 "fmt"
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070023 "github.com/google/gopacket"
24 "github.com/google/gopacket/layers"
Girish Gowdra161d27a2021-05-05 12:01:44 -070025 me "github.com/opencord/omci-lib-go/generated"
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070026)
27
28type MeBasePacket struct {
29 EntityClass me.ClassID
30 EntityInstance uint16
31
32 gopacket.Layer
33 layers.BaseLayer
34 MsgLayerType gopacket.LayerType
Girish Gowdra161d27a2021-05-05 12:01:44 -070035 Extended bool
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070036}
37
38func (msg *MeBasePacket) String() string {
39 return fmt.Sprintf("ClassID: %v, InstanceId: %d/%#x",
40 msg.EntityClass, msg.EntityInstance, msg.EntityInstance)
41}
42
Matteo Scandolof9d43412021-01-12 11:11:34 -080043// CanDecode returns the set of layer types that this DecodingLayer can decode
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070044func (msg *MeBasePacket) CanDecode() gopacket.LayerClass {
45 return msg.MsgLayerType
46}
47
Matteo Scandolof9d43412021-01-12 11:11:34 -080048// LayerType returns MsgLayerType. It partially satisfies Layer and SerializableLayer
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070049func (msg *MeBasePacket) LayerType() gopacket.LayerType {
50 return msg.MsgLayerType
51}
Matteo Scandolof9d43412021-01-12 11:11:34 -080052
53// LayerContents returns the bytes of the packet layer.
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070054func (msg *MeBasePacket) LayerContents() []byte {
55 return msg.Contents
56}
Matteo Scandolof9d43412021-01-12 11:11:34 -080057
58// LayerPayload returns the bytes contained within the packet layer
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070059func (msg *MeBasePacket) LayerPayload() []byte {
60 return msg.Payload
61}
62
Matteo Scandolof9d43412021-01-12 11:11:34 -080063// NextLayerType returns the layer type contained by this DecodingLayer
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070064func (msg *MeBasePacket) NextLayerType() gopacket.LayerType {
65 return gopacket.LayerTypeZero
66}
Matteo Scandolof9d43412021-01-12 11:11:34 -080067
68// DecodeFromBytes decodes the given bytes into this layer
Matteo Scandolocedde462021-03-09 17:37:16 -080069func (msg *MeBasePacket) DecodeFromBytes(data []byte, p gopacket.PacketBuilder, contentSize int) error {
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070070 // Note: Base OMCI frame already checked for frame with at least 10 octets
71 msg.EntityClass = me.ClassID(binary.BigEndian.Uint16(data[0:]))
72 msg.EntityInstance = binary.BigEndian.Uint16(data[2:])
Matteo Scandolocedde462021-03-09 17:37:16 -080073 msg.BaseLayer = layers.BaseLayer{Contents: data[:contentSize], Payload: data[contentSize:]}
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070074 return nil
75}
Matteo Scandolof9d43412021-01-12 11:11:34 -080076
77// SerializeTo provides serialization of this message layer
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -070078func (msg *MeBasePacket) SerializeTo(b gopacket.SerializeBuffer) error {
79 // Add class ID and entity ID
80 bytes, err := b.PrependBytes(4)
81 if err != nil {
82 return err
83 }
84 binary.BigEndian.PutUint16(bytes, uint16(msg.EntityClass))
85 binary.BigEndian.PutUint16(bytes[2:], msg.EntityInstance)
86 return nil
87}
88
89type layerDecodingLayer interface {
90 gopacket.Layer
91 DecodeFromBytes([]byte, gopacket.PacketBuilder) error
92 NextLayerType() gopacket.LayerType
93}
94
95func decodingLayerDecoder(d layerDecodingLayer, data []byte, p gopacket.PacketBuilder) error {
96 err := d.DecodeFromBytes(data, p)
97 if err != nil {
98 return err
99 }
100 p.AddLayer(d)
101 next := d.NextLayerType()
102 if next == gopacket.LayerTypeZero {
103 return nil
104 }
105 return p.NextDecoder(next)
106}