Matteo Scandolo | a6a3aee | 2019-11-26 13:30:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | */ |
| 17 | package omci |
| 18 | |
| 19 | import ( |
| 20 | "encoding/binary" |
| 21 | "fmt" |
| 22 | me "github.com/cboling/omci/generated" |
| 23 | "github.com/google/gopacket" |
| 24 | "github.com/google/gopacket/layers" |
| 25 | ) |
| 26 | |
| 27 | type MeBasePacket struct { |
| 28 | EntityClass me.ClassID |
| 29 | EntityInstance uint16 |
| 30 | |
| 31 | gopacket.Layer |
| 32 | layers.BaseLayer |
| 33 | MsgLayerType gopacket.LayerType |
| 34 | } |
| 35 | |
| 36 | func (msg *MeBasePacket) String() string { |
| 37 | return fmt.Sprintf("ClassID: %v, InstanceId: %d/%#x", |
| 38 | msg.EntityClass, msg.EntityInstance, msg.EntityInstance) |
| 39 | } |
| 40 | |
| 41 | func (msg *MeBasePacket) CanDecode() gopacket.LayerClass { |
| 42 | return msg.MsgLayerType |
| 43 | } |
| 44 | |
| 45 | // Layer Interface implementations |
| 46 | func (msg *MeBasePacket) LayerType() gopacket.LayerType { |
| 47 | return msg.MsgLayerType |
| 48 | } |
| 49 | func (msg *MeBasePacket) LayerContents() []byte { |
| 50 | return msg.Contents |
| 51 | } |
| 52 | func (msg *MeBasePacket) LayerPayload() []byte { |
| 53 | return msg.Payload |
| 54 | } |
| 55 | |
| 56 | // layerDecodingLayer Interface implementations |
| 57 | func (msg *MeBasePacket) NextLayerType() gopacket.LayerType { |
| 58 | return gopacket.LayerTypeZero |
| 59 | } |
| 60 | func (msg *MeBasePacket) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error { |
| 61 | // Note: Base OMCI frame already checked for frame with at least 10 octets |
| 62 | msg.EntityClass = me.ClassID(binary.BigEndian.Uint16(data[0:])) |
| 63 | msg.EntityInstance = binary.BigEndian.Uint16(data[2:]) |
| 64 | msg.BaseLayer = layers.BaseLayer{Contents: data[:4], Payload: data[4:]} |
| 65 | return nil |
| 66 | } |
| 67 | func (msg *MeBasePacket) SerializeTo(b gopacket.SerializeBuffer) error { |
| 68 | // Add class ID and entity ID |
| 69 | bytes, err := b.PrependBytes(4) |
| 70 | if err != nil { |
| 71 | return err |
| 72 | } |
| 73 | binary.BigEndian.PutUint16(bytes, uint16(msg.EntityClass)) |
| 74 | binary.BigEndian.PutUint16(bytes[2:], msg.EntityInstance) |
| 75 | return nil |
| 76 | } |
| 77 | |
| 78 | type layerDecodingLayer interface { |
| 79 | gopacket.Layer |
| 80 | DecodeFromBytes([]byte, gopacket.PacketBuilder) error |
| 81 | NextLayerType() gopacket.LayerType |
| 82 | } |
| 83 | |
| 84 | func decodingLayerDecoder(d layerDecodingLayer, data []byte, p gopacket.PacketBuilder) error { |
| 85 | err := d.DecodeFromBytes(data, p) |
| 86 | if err != nil { |
| 87 | return err |
| 88 | } |
| 89 | p.AddLayer(d) |
| 90 | next := d.NextLayerType() |
| 91 | if next == gopacket.LayerTypeZero { |
| 92 | return nil |
| 93 | } |
| 94 | return p.NextDecoder(next) |
| 95 | } |