blob: 7692bbf1f18e19ae334c4271a4ed7d0424551281 [file] [log] [blame]
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -07001/*
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 */
17package omci
18
19import (
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
27type MeBasePacket struct {
28 EntityClass me.ClassID
29 EntityInstance uint16
30
31 gopacket.Layer
32 layers.BaseLayer
33 MsgLayerType gopacket.LayerType
34}
35
36func (msg *MeBasePacket) String() string {
37 return fmt.Sprintf("ClassID: %v, InstanceId: %d/%#x",
38 msg.EntityClass, msg.EntityInstance, msg.EntityInstance)
39}
40
41func (msg *MeBasePacket) CanDecode() gopacket.LayerClass {
42 return msg.MsgLayerType
43}
44
45// Layer Interface implementations
46func (msg *MeBasePacket) LayerType() gopacket.LayerType {
47 return msg.MsgLayerType
48}
49func (msg *MeBasePacket) LayerContents() []byte {
50 return msg.Contents
51}
52func (msg *MeBasePacket) LayerPayload() []byte {
53 return msg.Payload
54}
55
56// layerDecodingLayer Interface implementations
57func (msg *MeBasePacket) NextLayerType() gopacket.LayerType {
58 return gopacket.LayerTypeZero
59}
60func (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}
67func (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
78type layerDecodingLayer interface {
79 gopacket.Layer
80 DecodeFromBytes([]byte, gopacket.PacketBuilder) error
81 NextLayerType() gopacket.LayerType
82}
83
84func 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}