Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net) |
Andrea Campanella | 7167ebb | 2020-02-24 09:56:38 +0100 | [diff] [blame] | 3 | * Copyright 2020-present Open Networking Foundation |
| 4 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 5 | * 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 |
Andrea Campanella | 7167ebb | 2020-02-24 09:56:38 +0100 | [diff] [blame] | 8 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Andrea Campanella | 7167ebb | 2020-02-24 09:56:38 +0100 | [diff] [blame] | 10 | |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 11 | * 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. |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | package omci |
| 19 | |
| 20 | import ( |
| 21 | "encoding/binary" |
| 22 | "fmt" |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 23 | "github.com/google/gopacket" |
| 24 | "github.com/google/gopacket/layers" |
David K. Bainbridge | adf422d | 2021-04-09 16:06:41 +0000 | [diff] [blame] | 25 | me "github.com/opencord/omci-lib-go/generated" |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | type MeBasePacket struct { |
| 29 | EntityClass me.ClassID |
| 30 | EntityInstance uint16 |
| 31 | |
| 32 | gopacket.Layer |
| 33 | layers.BaseLayer |
| 34 | MsgLayerType gopacket.LayerType |
Chip Boling | 157c9b9 | 2021-04-21 09:58:36 -0500 | [diff] [blame] | 35 | Extended bool |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | func (msg *MeBasePacket) String() string { |
| 39 | return fmt.Sprintf("ClassID: %v, InstanceId: %d/%#x", |
| 40 | msg.EntityClass, msg.EntityInstance, msg.EntityInstance) |
| 41 | } |
| 42 | |
| 43 | // CanDecode returns the set of layer types that this DecodingLayer can decode |
| 44 | func (msg *MeBasePacket) CanDecode() gopacket.LayerClass { |
| 45 | return msg.MsgLayerType |
| 46 | } |
| 47 | |
| 48 | // LayerType returns MsgLayerType. It partially satisfies Layer and SerializableLayer |
| 49 | func (msg *MeBasePacket) LayerType() gopacket.LayerType { |
| 50 | return msg.MsgLayerType |
| 51 | } |
| 52 | |
| 53 | // LayerContents returns the bytes of the packet layer. |
| 54 | func (msg *MeBasePacket) LayerContents() []byte { |
| 55 | return msg.Contents |
| 56 | } |
| 57 | |
| 58 | // LayerPayload returns the bytes contained within the packet layer |
| 59 | func (msg *MeBasePacket) LayerPayload() []byte { |
| 60 | return msg.Payload |
| 61 | } |
| 62 | |
| 63 | // NextLayerType returns the layer type contained by this DecodingLayer |
| 64 | func (msg *MeBasePacket) NextLayerType() gopacket.LayerType { |
| 65 | return gopacket.LayerTypeZero |
| 66 | } |
| 67 | |
| 68 | // DecodeFromBytes decodes the given bytes into this layer |
Chip Boling | d8543b2 | 2021-03-08 08:34:26 -0600 | [diff] [blame] | 69 | func (msg *MeBasePacket) DecodeFromBytes(data []byte, p gopacket.PacketBuilder, contentSize int) error { |
Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 70 | if len(data) < contentSize { |
| 71 | p.SetTruncated() |
| 72 | layerType := msg.LayerType().String() |
| 73 | if msg.Extended { |
| 74 | layerType += " (extended)" |
| 75 | } |
| 76 | return fmt.Errorf("frame header too small. %v header length %v, %v required", |
| 77 | layerType, len(data), contentSize) |
| 78 | } |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 79 | msg.EntityClass = me.ClassID(binary.BigEndian.Uint16(data[0:])) |
| 80 | msg.EntityInstance = binary.BigEndian.Uint16(data[2:]) |
Chip Boling | d8543b2 | 2021-03-08 08:34:26 -0600 | [diff] [blame] | 81 | msg.BaseLayer = layers.BaseLayer{Contents: data[:contentSize], Payload: data[contentSize:]} |
Chip Boling | 6e27b35 | 2020-02-14 09:10:01 -0600 | [diff] [blame] | 82 | return nil |
| 83 | } |
| 84 | |
| 85 | // SerializeTo provides serialization of this message layer |
| 86 | func (msg *MeBasePacket) SerializeTo(b gopacket.SerializeBuffer) error { |
| 87 | // Add class ID and entity ID |
| 88 | bytes, err := b.PrependBytes(4) |
| 89 | if err != nil { |
| 90 | return err |
| 91 | } |
| 92 | binary.BigEndian.PutUint16(bytes, uint16(msg.EntityClass)) |
| 93 | binary.BigEndian.PutUint16(bytes[2:], msg.EntityInstance) |
| 94 | return nil |
| 95 | } |
| 96 | |
| 97 | type layerDecodingLayer interface { |
| 98 | gopacket.Layer |
| 99 | DecodeFromBytes([]byte, gopacket.PacketBuilder) error |
| 100 | NextLayerType() gopacket.LayerType |
| 101 | } |
| 102 | |
| 103 | func decodingLayerDecoder(d layerDecodingLayer, data []byte, p gopacket.PacketBuilder) error { |
| 104 | err := d.DecodeFromBytes(data, p) |
| 105 | if err != nil { |
| 106 | return err |
| 107 | } |
| 108 | p.AddLayer(d) |
| 109 | next := d.NextLayerType() |
| 110 | if next == gopacket.LayerTypeZero { |
| 111 | return nil |
| 112 | } |
| 113 | return p.NextDecoder(next) |
| 114 | } |