-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtpdu.go
98 lines (87 loc) · 1.71 KB
/
tpdu.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
package sms
import (
"bytes"
"io"
"time"
)
var (
msgRef chan byte
// Indent for String() output for each TPDU
Indent = " | "
)
func init() {
msgRef = make(chan byte, 1)
msgRef <- byte(time.Now().Nanosecond())
}
// NextMsgReference make Message Reference ID
func NextMsgReference() byte {
ret := <-msgRef
msgRef <- ret + 1
return ret
}
// TPDU represents a SMS TP PDU
type TPDU interface {
RPDU
MarshalTP() []byte
}
// UnmarshalerTP is the interface implemented by types
// that can unmarshal a TPDU
type UnmarshalerTP interface {
UnmarshalTP([]byte) error
}
// UnmarshalTPMO parse byte data to TPDU as SC.
func UnmarshalTPMO(b []byte) (TPDU, error) {
if len(b) == 0 {
return nil, io.EOF
}
switch b[0] & 0x03 {
case 0x00:
return UnmarshalDeliverReport(b)
case 0x01:
return UnmarshalSubmit(b)
case 0x02:
return UnmarshalCommand(b)
}
return nil, UnknownMessageTypeError{Actual: b[0] & 0x03}
}
// UnmarshalTPMT parse byte data to TPDU as MS.
func UnmarshalTPMT(b []byte) (t TPDU, e error) {
if len(b) == 0 {
return nil, io.EOF
}
switch b[0] & 0x03 {
case 0x00:
return UnmarshalDeliver(b)
case 0x01:
return UnmarshalSubmitReport(b)
case 0x02:
return UnmarshalStatusReport(b)
}
return nil, UnknownMessageTypeError{Actual: b[0] & 0x03}
}
func read7Bytes(r *bytes.Reader) ([7]byte, error) {
if r.Len() < 7 {
return [7]byte{}, io.EOF
}
b := make([]byte, 7)
r.Read(b)
return [7]byte{
b[0], b[1], b[2], b[3], b[4], b[5], b[6]}, nil
}
func int2SemiOctet(i int) (b byte) {
b = byte(i % 10)
b = (b << 4) | byte((i/10)%10)
return
}
func semiOctet2Int(b byte) (i int) {
i = int(b & 0x0f)
if i > 0x09 {
i = 0
}
j := int((b & 0xf0) >> 4)
if j > 0x09 {
j = 0
}
i = (i * 10) + j
return
}