/
rpdu.go
264 lines (238 loc) · 4.53 KB
/
rpdu.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package sms
import (
"bytes"
"io"
)
// RPDU represents a SMS RP PDU
type RPDU interface {
CPDU
MarshalRP() []byte
}
// UnmarshalerRP is the interface implemented by types
// that can unmarshal a RPDU
type UnmarshalerRP interface {
UnmarshalRP([]byte) error
}
// UnmarshalRPMO parse byte data to TPDU as SC.
func UnmarshalRPMO(b []byte) (RPDU, error) {
return unmarshalRPMO(b, cpData{})
}
func unmarshalRPMO(b []byte, c cpData) (RPDU, error) {
if len(b) == 0 {
return nil, io.EOF
}
switch b[0] & 0x07 {
case 0x00:
var rp rpData
var e error
if b, e = rp.unmarshal(true, b); e != nil {
return nil, e
}
rp.cpData = c
switch b[0] & 0x03 {
case 0x01:
var tp Submit
e = tp.UnmarshalTP(b)
tp.rpData = rp
return tp, e
case 0x02:
var tp Command
e = tp.UnmarshalTP(b)
tp.rpData = rp
return tp, e
}
case 0x02:
var rp RpAck
var e error
if b, e = rp.unmarshalRP(true, b); e != nil {
return nil, e
}
rp.cpData = c
if b == nil {
return RpAckMO(rp), nil
}
var tp DeliverReport
e = tp.UnmarshalTP(b)
tp.cpData = rp.cpData
tp.RMR = rp.RMR
return tp, e
case 0x04:
var rp RpError
var e error
if b, e = rp.unmarshalRP(true, b); e != nil {
return nil, e
}
rp.cpData = c
if b == nil {
return RpErrorMO(rp), nil
}
var tp DeliverReport
e = tp.UnmarshalTP(b)
tp.cpData = rp.cpData
tp.RMR = rp.RMR
tp.CS = rp.CS
tp.DIAG = rp.DIAG
return tp, e
case 0x06:
rp, e := UnmarshalMemoryAvailable(b)
rp.cpData = c
return rp, e
}
return nil, UnknownMessageTypeError{Actual: b[0]}
}
// UnmarshalRPMT parse byte data to TPDU as MS.
func UnmarshalRPMT(b []byte) (RPDU, error) {
return unmarshalRPMT(b, cpData{})
}
func unmarshalRPMT(b []byte, c cpData) (RPDU, error) {
if len(b) == 0 {
return nil, io.EOF
}
switch b[0] & 0x07 {
case 0x01:
var rp rpData
var e error
if b, e = rp.unmarshal(false, b); e != nil {
return nil, e
}
rp.cpData = c
switch b[0] & 0x03 {
case 0x00:
var tp Deliver
e = tp.UnmarshalTP(b)
tp.rpData = rp
return tp, e
case 0x02:
var tp StatusReport
e = tp.UnmarshalTP(b)
tp.rpData = rp
return tp, e
}
case 0x03:
var rp RpAck
var e error
if b, e = rp.unmarshalRP(false, b); e != nil {
return nil, e
}
rp.cpData = c
if b == nil {
return RpAckMT(rp), nil
}
var tp SubmitReport
e = tp.UnmarshalTP(b)
tp.cpData = rp.cpData
tp.RMR = rp.RMR
return tp, e
case 0x05:
var rp RpError
var e error
if b, e = rp.unmarshalRP(false, b); e != nil {
return nil, e
}
rp.cpData = c
if b == nil {
return RpErrorMT(rp), nil
}
var tp SubmitReport
e = tp.UnmarshalTP(b)
tp.cpData = rp.cpData
tp.RMR = rp.RMR
tp.CS = rp.CS
tp.DIAG = rp.DIAG
return tp, e
}
return nil, UnknownMessageTypeError{Actual: b[0]}
}
func unmarshalRpHeader(mti byte, b []byte) (byte, error) {
if len(b) < 2 {
return 0, io.EOF
}
if b[0] != mti {
return 0, UnexpectedMessageTypeError{
Expected: mti, Actual: b[0]}
}
return b[1], nil
}
type rpData struct {
cpData
RMR byte `json:"rmr"` // M / Message Reference for RP
SCA Address `json:"sca"` // M / SC Address
}
func (d rpData) marshal(mo bool, tp []byte) []byte {
w := new(bytes.Buffer)
if mo {
w.WriteByte(0) // MTI
w.WriteByte(d.RMR)
w.WriteByte(0) // OA
_, a := d.SCA.Marshal()
w.WriteByte(byte(len(a)))
w.Write(a)
} else {
w.WriteByte(1) // MTI
w.WriteByte(d.RMR)
_, a := d.SCA.Marshal()
w.WriteByte(byte(len(a)))
w.Write(a)
w.WriteByte(0) // DA
}
w.WriteByte(byte(len(tp)))
w.Write(tp)
return w.Bytes()
}
func (d *rpData) unmarshal(mo bool, b []byte) (tp []byte, e error) {
if mo {
d.RMR, e = unmarshalRpHeader(0, b)
} else {
d.RMR, e = unmarshalRpHeader(1, b)
}
if e != nil {
return
}
r := bytes.NewReader(b[2:])
var tmp byte
if mo {
if tmp, e = r.ReadByte(); e != nil {
return
} else if tmp != 0 {
e = ErrInvalidLength
return
}
}
if d.SCA, e = readRPAddr(r); e != nil {
return
}
if !mo {
if tmp, e = r.ReadByte(); e != nil {
return
} else if tmp != 0 {
e = ErrInvalidLength
return
}
}
if tmp, e = r.ReadByte(); e != nil {
return
}
tp = make([]byte, int(tmp))
var n int
if n, e = r.Read(tp); e != nil {
return
}
if n != len(tp) {
e = io.EOF
} else if r.Len() != 0 {
e = ErrExtraData
}
return
}
/*
type rpAResult interface {
marshalRP(bool, []byte) []byte
}
/*
type rpAnswer struct {
cpData
RMR byte `json:"rmr"` // M / Message Reference
CS byte `json:"cs"` // M / Cause
DIAG *byte `json:"diag,omitempty"` // O / Diagnostics
}
*/