forked from omec-project/pfcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.go
38 lines (29 loc) · 900 Bytes
/
encode.go
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
// Copyright 2019 free5GC.org
//
// SPDX-License-Identifier: Apache-2.0
//
package pfcp
import (
"fmt"
"github.com/free5gc/tlv"
)
func (m *Message) Marshal() ([]byte, error) {
var headerBytes []byte
var bodyBytes []byte
var err error
if bodyBytes, err = tlv.Marshal(m.Body); err != nil {
return nil, fmt.Errorf("pfcp: marshal message type %d body failed: %s", m.Header.MessageType, err)
}
if m.Header.S&1 != 0 {
// 8 (SEID) + 3 (Sequence Number) + 1 (Message Priority and Spare)
m.Header.MessageLength = 12
} else {
// 3 (Sequence Number) + 1 (Message Priority and Spare)
m.Header.MessageLength = 4
}
m.Header.MessageLength += uint16(len(bodyBytes))
if headerBytes, err = m.Header.MarshalBinary(); err != nil {
return nil, fmt.Errorf("pfcp: marshal message type %d header failed: %s", m.Header.MessageType, err)
}
return append(headerBytes, bodyBytes[:]...), nil
}