forked from free5gc/pfcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PacketRate.go
100 lines (83 loc) · 2.56 KB
/
PacketRate.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
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
package pfcpType
import (
"bytes"
"encoding/binary"
"errors"
)
// PacketRateTimeUnit represents the thime unit of packet rate
type PacketRateTimeUnit uint8
const (
PacketRateTimeUnitMinute = iota
PacketRateTimeUnit6Minutes
PacketRateTimeUnitHours
PacketRateTimeUnitDay
PacketRateTimeUnitWeek
)
type PacketRate struct {
ULPR bool
DLPR bool
ULTimeUnit PacketRateTimeUnit
MaximumUL uint16
DLTimeUnit PacketRateTimeUnit
MaximumDL uint16
}
func (p *PacketRate) MarshalBinary() ([]byte, error) {
buf := &bytes.Buffer{}
if err := buf.WriteByte(btou(p.ULPR)<<0 + btou(p.DLPR)<<1); err != nil {
return nil, errors.New("marshal ULPR & DLPR fail: " + err.Error())
}
if p.ULPR {
if err := buf.WriteByte(uint8(p.ULTimeUnit)); err != nil {
return nil, errors.New("marshal UL Time Unit fail: " + err.Error())
}
if err := binary.Write(buf, binary.BigEndian, p.MaximumUL); err != nil {
return nil, errors.New("marshal Maximum Uplink fail: " + err.Error())
}
}
if p.DLPR {
if err := buf.WriteByte(uint8(p.DLTimeUnit)); err != nil {
return nil, errors.New("marshal DL Time Unit fail: " + err.Error())
}
if err := binary.Write(buf, binary.BigEndian, p.MaximumDL); err != nil {
return nil, errors.New("marshal Maximum Downlink fail: " + err.Error())
}
}
return buf.Bytes(), nil
}
func (p *PacketRate) UnmarshalBinary(data []byte) error {
buf := bytes.NewBuffer(data)
var tmpByte byte
// octet 1
if err := binary.Read(buf, binary.BigEndian, &tmpByte); err != nil {
return errors.New("Packet Rate flag read fail: " + err.Error())
}
p.ULPR = utob(tmpByte & (1 << 0))
p.DLPR = utob(tmpByte & (1 << 1))
if p.ULPR {
if err := binary.Read(buf, binary.BigEndian, &tmpByte); err != nil {
return errors.New("Packet Rate UL Time Unit read fail: " + err.Error())
}
if tmpByte > 4 {
return errors.New("Packet Rate UL Time Unit should be 0 to 4")
} else {
p.ULTimeUnit = PacketRateTimeUnit(tmpByte)
}
if err := binary.Read(buf, binary.BigEndian, &p.MaximumUL); err != nil {
return errors.New("Maximum Uplink Packet Rate read fail: " + err.Error())
}
}
if p.DLPR {
if err := binary.Read(buf, binary.BigEndian, &tmpByte); err != nil {
return errors.New("Packet Rate DL Time Unit read fail: " + err.Error())
}
if tmpByte > 4 {
return errors.New("Packet Rate DL Time Unit should be 0 to 4")
} else {
p.DLTimeUnit = PacketRateTimeUnit(tmpByte)
}
if err := binary.Read(buf, binary.BigEndian, &p.MaximumDL); err != nil {
return errors.New("Maximum Downlink Packet Rate read fail: " + err.Error())
}
}
return nil
}