-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpdu_header.go
86 lines (77 loc) · 2.38 KB
/
pdu_header.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
package smpp
import (
"encoding/binary"
"encoding/hex"
"fmt"
)
type Header struct {
CommandLength int
CommandId string
CommandStatus string
SequenceNumber int
}
func parseHeader(bytes []byte) (header Header, err error) {
length, err := verifyLength(bytes)
if err != nil {
return
}
commandId, err := extractCommandID(bytes)
if err != nil {
return
}
commandStatus, err := extractCommandStatus(bytes)
if err != nil {
return
}
sequenceNumber := extractSequenceNumber(bytes)
header = Header{
CommandLength: length,
CommandId: commandId,
CommandStatus: commandStatus,
SequenceNumber: sequenceNumber,
}
return
}
func extractSequenceNumber(bytes []byte) (sequenceNumber int) {
sequenceNumber = int(binary.BigEndian.Uint32(bytes[12:16]))
return
}
func extractCommandStatus(bytes []byte) (string, error) {
commandStatus := hex.EncodeToString(bytes[8:12])
if value, ok := commandStatusByHex[commandStatus]; ok {
return value["name"], nil
}
return commandStatus, fmt.Errorf("unknown command status %s", commandStatus)
}
func extractCommandID(bytes []byte) (string, error) {
commandId := hex.EncodeToString(bytes[4:8])
if value, ok := commandIdByHex[commandId]; ok {
return value["name"], nil
}
return "", fmt.Errorf("unknown command_id %s", commandId)
}
func verifyLength(fixture []byte) (int, error) {
if len(fixture) > 3 {
pdu_length := int(binary.BigEndian.Uint32(fixture[0:4]))
if len(fixture) < pdu_length {
return 0, fmt.Errorf("invalid PDU Length for pdu : %v", hex.EncodeToString(fixture))
}
return pdu_length, nil
}
return 0, fmt.Errorf("invalid length parameter")
}
func encodeHeader(obj PDU, bodyBytes []byte) (headerBytes []byte, err error) {
headerBytes = []byte{}
commandIdBytes, _ := hex.DecodeString(commandIdByName[obj.Header.CommandId]["hex"])
headerBytes = append(headerBytes, commandIdBytes...)
commandStatusBytes, _ := hex.DecodeString(commandStatusByName[obj.Header.CommandStatus]["hex"])
headerBytes = append(headerBytes, commandStatusBytes...)
sequence_number_buffer := make([]byte, 4)
binary.BigEndian.PutUint32(sequence_number_buffer, uint32(obj.Header.SequenceNumber))
headerBytes = append(headerBytes, sequence_number_buffer...)
length := len(bodyBytes) + 16
lengthBytes := make([]byte, 4)
binary.BigEndian.PutUint32(lengthBytes, uint32(length))
headerBytes = append(lengthBytes, headerBytes...)
return headerBytes, err
}