forked from free5gc/pfcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MBR.go
62 lines (46 loc) · 1.42 KB
/
MBR.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package pfcpType
import (
"bytes"
"encoding/binary"
"fmt"
"math/bits"
)
type MBR struct {
ULMBR uint64 // 40-bit data
DLMBR uint64 // 40-bit data
}
func (m *MBR) MarshalBinary() (data []byte, err error) {
buf := &bytes.Buffer{}
if bits.Len64(m.ULMBR) > 40 {
return nil, fmt.Errorf("UL MBR shall not be greater than 40 bits binary integer")
}
if bits.Len64(m.DLMBR) > 40 {
return nil, fmt.Errorf("DL MBR shall not be greater than 40 bits binary integer")
}
gbrBytes := make([]byte, 8)
binary.BigEndian.PutUint64(gbrBytes, m.ULMBR)
if err := binary.Write(buf, binary.BigEndian, gbrBytes[3:]); err != nil {
return nil, fmt.Errorf("write ULMBR fail: %s", err)
}
binary.BigEndian.PutUint64(gbrBytes, m.DLMBR)
if err := binary.Write(buf, binary.BigEndian, gbrBytes[3:]); err != nil {
return nil, fmt.Errorf("write DLMBR fail: %s", err)
}
return buf.Bytes(), nil
}
func (m *MBR) UnmarshalBinary(data []byte) error {
buf := bytes.NewBuffer(data)
MBRBytes := make([]byte, 5)
uint64Byte := make([]byte, 8)
if err := binary.Read(buf, binary.BigEndian, MBRBytes); err != nil {
return fmt.Errorf("read UL MBR fail: %s", err)
}
copy(uint64Byte[3:], MBRBytes)
m.ULMBR = binary.BigEndian.Uint64(uint64Byte)
if err := binary.Read(buf, binary.BigEndian, MBRBytes); err != nil {
return fmt.Errorf("read DL MBR fail: %s", err)
}
copy(uint64Byte[3:], MBRBytes)
m.DLMBR = binary.BigEndian.Uint64(uint64Byte)
return nil
}