summaryrefslogtreecommitdiffstats
path: root/codec/msg_codec.go
blob: 7ba87711b2d00c6ac70ddd94747bcaffed92d88b (plain)
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
// Copyright (c) 2017 Cisco and/or its affiliates.
//
// 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 codec

import (
	"bytes"
	"errors"
	"fmt"
	"reflect"

	"git.fd.io/govpp.git/api"
	"github.com/lunixbochs/struc"
)

// MsgCodec provides encoding and decoding functionality of `api.Message` structs into/from
// binary format as accepted by VPP.
type MsgCodec struct{}

// VppRequestHeader struct contains header fields implemented by all VPP requests.
type VppRequestHeader struct {
	VlMsgID     uint16
	ClientIndex uint32
	Context     uint32
}

// VppReplyHeader struct contains header fields implemented by all VPP replies.
type VppReplyHeader struct {
	VlMsgID uint16
	Context uint32
}

// VppEventHeader struct contains header fields implemented by all VPP events.
type VppEventHeader struct {
	VlMsgID uint16
	Context uint32
}

// VppOtherHeader struct contains header fields implemented by other VPP messages (not requests nor replies).
type VppOtherHeader struct {
	VlMsgID uint16
}

const (
	vppRequestHeaderSize = 10 // size of a VPP request header
	vppReplyHeaderSize   = 6  // size of a VPP reply header
	vppEventHeaderSize   = 6  // size of a VPP event header
	vppOtherHeaderSize   = 2  // size of the header of other VPP messages
)

// EncodeMsg encodes provided `Message` structure into its binary-encoded data representation.
func (*MsgCodec) EncodeMsg(msg api.Message, msgID uint16) ([]byte, error) {
	if msg == nil {
		return nil, errors.New("nil message passed in")
	}

	buf := new(bytes.Buffer)

	// encode message header
	var header interface{}
	if msg.GetMessageType() == api.RequestMessage {
		header = &VppRequestHeader{VlMsgID: msgID}
	} else if msg.GetMessageType() == api.ReplyMessage {
		header = &VppReplyHeader{VlMsgID: msgID}
	} else if msg.GetMessageType() == api.EventMessage {
		header = &VppEventHeader{VlMsgID: msgID}
	} else {
		header = &VppOtherHeader{VlMsgID: msgID}
	}
	err := struc.Pack(buf, header)
	if err != nil {
		return nil, fmt.Errorf("unable to encode message: header: %v, error %v", header, err)
	}

	// encode message content
	if reflect.Indirect(reflect.ValueOf(msg)).NumField() > 0 {
		err := struc.Pack(buf, msg)
		if err != nil {
			return nil, fmt.Errorf("unable to encode message: header %v, error %v", header, err)
		}
	}

	return buf.Bytes(), nil
}

// DecodeMsg decodes binary-encoded data of a message into provided `Message` structure.
func (*MsgCodec) DecodeMsg(data []byte, msg api.Message) error {
	if msg == nil {
		return errors.New("nil message passed in")
	}

	buf := bytes.NewReader(data)

	// check which header is expected
	var header interface{}
	if msg.GetMessageType() == api.RequestMessage {
		header = &VppRequestHeader{}
	} else if msg.GetMessageType() == api.ReplyMessage {
		header = &VppReplyHeader{}
	} else if msg.GetMessageType() == api.EventMessage {
		header = &VppEventHeader{}
	} else {
		header = &VppOtherHeader{}
	}

	// decode message header
	err := struc.Unpack(buf, header)
	if err != nil {
		return fmt.Errorf("unable to decode message: data %v, error %v", data, err)
	}

	// get rid of the message header
	if msg.GetMessageType() == api.RequestMessage {
		buf = bytes.NewReader(data[vppRequestHeaderSize:])
	} else if msg.GetMessageType() == api.ReplyMessage {
		buf = bytes.NewReader(data[vppReplyHeaderSize:])
	} else if msg.GetMessageType() == api.EventMessage {
		buf = bytes.NewReader(data[vppEventHeaderSize:])
	} else {
		buf = bytes.NewReader(data[vppOtherHeaderSize:])
	}

	// decode message content
	err = struc.Unpack(buf, msg)
	if err != nil {
		return fmt.Errorf("unable to decode message: data %v, error %v", data, err)
	}

	return nil
}