forked from linxGnu/gosmpp
/
ShortMessage.go
311 lines (260 loc) · 8.32 KB
/
ShortMessage.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
package pdu
import (
"sync/atomic"
"github.com/JorgenPo/gosmpp/data"
"github.com/JorgenPo/gosmpp/errors"
)
var (
ref = uint32(0)
)
// ShortMessage message.
type ShortMessage struct {
SmDefaultMsgID byte
message string
enc data.Encoding
udHeader UDH
messageData []byte
withoutDataCoding bool // purpose of ReplaceSM usage
}
// NewShortMessage returns new ShortMessage.
func NewShortMessage(message string) (s ShortMessage, err error) {
err = s.SetMessageWithEncoding(message, data.GSM7BIT)
return
}
// NewShortMessageWithEncoding returns new ShortMessage with predefined encoding.
func NewShortMessageWithEncoding(message string, enc data.Encoding) (s ShortMessage, err error) {
err = s.SetMessageWithEncoding(message, enc)
return
}
// NewBinaryShortMessage returns new ShortMessage.
func NewBinaryShortMessage(messageData []byte) (s ShortMessage, err error) {
err = s.SetMessageDataWithEncoding(messageData, data.BINARY8BIT2)
return
}
// NewBinaryShortMessageWithEncoding returns new ShortMessage with predefined encoding.
func NewBinaryShortMessageWithEncoding(messageData []byte, enc data.Encoding) (s ShortMessage, err error) {
err = s.SetMessageDataWithEncoding(messageData, enc)
return
}
// NewLongMessage returns long message splitted into multiple short message
func NewLongMessage(message string) (s []*ShortMessage, err error) {
return NewLongMessageWithEncoding(message, data.GSM7BIT)
}
// NewLongMessageWithEncoding returns long message splitted into multiple short message with encoding of choice
func NewLongMessageWithEncoding(message string, enc data.Encoding) (s []*ShortMessage, err error) {
sm := &ShortMessage{
message: message,
enc: enc,
}
return sm.split()
}
// SetMessageWithEncoding sets message with encoding.
func (c *ShortMessage) SetMessageWithEncoding(message string, enc data.Encoding) (err error) {
if c.messageData, err = enc.Encode(message); err == nil {
if len(c.messageData) > data.SM_MSG_LEN {
err = errors.ErrShortMessageLengthTooLarge
} else {
c.message = message
c.enc = enc
}
}
return
}
// SetLongMessageWithEnc sets ShortMessage with message longer than 256 bytes
// callers are expected to call Split() after this
func (c *ShortMessage) SetLongMessageWithEnc(message string, enc data.Encoding) (err error) {
c.message = message
c.enc = enc
return
}
// UDH gets user data header for short message
func (c *ShortMessage) UDH() UDH {
return c.udHeader
}
// SetUDH sets user data header for short message
// also appends udh to the beginning of messageData
func (c *ShortMessage) SetUDH(udh UDH) {
c.udHeader = udh
}
// SetMessageDataWithEncoding sets underlying raw data which is used for pdu marshalling.
func (c *ShortMessage) SetMessageDataWithEncoding(d []byte, enc data.Encoding) (err error) {
if len(d) > data.SM_MSG_LEN {
err = errors.ErrShortMessageLengthTooLarge
} else {
c.messageData = d
c.enc = enc
}
return
}
// GetMessageData returns underlying binary message.
func (c *ShortMessage) GetMessageData() (d []byte, err error) {
return c.messageData, nil
}
// GetMessage returns underlying message.
func (c *ShortMessage) GetMessage() (st string, err error) {
enc := c.enc
if enc == nil {
enc = data.GSM7BIT
}
st, err = c.GetMessageWithEncoding(enc)
return
}
// GetMessageWithEncoding returns (decoded) underlying message.
func (c *ShortMessage) GetMessageWithEncoding(enc data.Encoding) (st string, err error) {
if len(c.messageData) > 0 {
st, err = enc.Decode(c.messageData)
}
return
}
// split one short message and split into multiple short message, with UDH
// according to 33GP TS 23.040 section 9.2.3.24.1
//
// NOTE: split() will return array of length 1 if data length is still within the limit
// The encoding interface can implement the data.Splitter interface for ad-hoc splitting rule
func (c *ShortMessage) split() (multiSM []*ShortMessage, err error) {
var encoding data.Encoding
if c.enc == nil {
encoding = data.GSM7BIT
} else {
encoding = c.enc
}
// check if encoding implements data.Splitter
splitter, ok := encoding.(data.Splitter)
// check if encoding implements data.Splitter or split is necessary
if !ok || !splitter.ShouldSplit(c.message, data.SM_GSM_MSG_LEN) {
err = c.SetMessageWithEncoding(c.message, c.enc)
multiSM = []*ShortMessage{c}
return
}
// reserve 6 bytes for concat message UDH
segments, err := splitter.EncodeSplit(c.message, data.SM_GSM_MSG_LEN-6)
if err != nil {
return nil, err
}
// prealloc result
multiSM = make([]*ShortMessage, 0, len(segments))
// all segments will have the same ref id
ref := getRefNum()
// construct SM(s)
for i, seg := range segments {
// create new SM, encode data
multiSM = append(multiSM, &ShortMessage{
enc: c.enc,
// message: we don't really care
messageData: seg,
withoutDataCoding: c.withoutDataCoding,
udHeader: UDH{NewIEConcatMessage(uint8(len(segments)), uint8(i+1), uint8(ref))},
})
}
return
}
// Marshal implements PDU interface.
func (c *ShortMessage) Marshal(b *ByteBuffer) {
var (
udhBin []byte
n = byte(len(c.messageData))
)
// Prepend UDH to message data if there are any
if c.udHeader != nil && c.udHeader.UDHL() > 0 {
udhBin, _ = c.udHeader.MarshalBinary()
}
b.Grow(int(n) + 3)
var coding byte
if c.enc == nil {
coding = data.GSM7BITCoding
} else {
coding = c.enc.DataCoding()
}
// data_coding
if !c.withoutDataCoding {
_ = b.WriteByte(coding)
}
// sm_default_msg_id
_ = b.WriteByte(c.SmDefaultMsgID)
// sm_length
if udhBin != nil {
_ = b.WriteByte(byte(int(n) + len(udhBin)))
b.Write(udhBin)
} else {
_ = b.WriteByte(n)
}
// short_message
_, _ = b.Write(c.messageData[:n])
}
// Unmarshal implements PDU interface.
func (c *ShortMessage) Unmarshal(b *ByteBuffer, udhi bool) (err error) {
var dataCoding, n byte
if !c.withoutDataCoding {
if dataCoding, err = b.ReadByte(); err != nil {
return
}
}
if c.SmDefaultMsgID, err = b.ReadByte(); err != nil {
return
}
if n, err = b.ReadByte(); err != nil {
return
}
if c.messageData, err = b.ReadN(int(n)); err != nil {
return
}
c.enc = data.FromDataCoding(dataCoding)
// If short message length is non zero, short message contains User-Data Header
// Else UDH should be in TLV field MessagePayload
if udhi && n > 0 {
udh := UDH{}
_, err = udh.UnmarshalBinary(c.messageData)
if err != nil {
return
}
c.udHeader = udh
f := c.udHeader.UDHL()
if f > len(c.messageData) {
err = errors.ErrUDHTooLong
return
}
c.messageData = c.messageData[f:]
}
return
}
// Encoding returns message encoding.
func (c *ShortMessage) Encoding() data.Encoding {
return c.enc
}
// returns an atomically incrementing number each time it's called
func getRefNum() uint32 {
return atomic.AddUint32(&ref, 1)
}
// NOTE:
// When coding splitting function, I have 4 choices of abstraction
// 1. Split the message before encode
// 2. Split the message after encoded
// 3. Split the message DURING encoding (before bit packing)
// 4. Encode, unpack, split
//
// Disadvantages:
// 1. The only way to really know if each segment will fit into 134 octet limit is
// to do some kind of simulated encoding, where you calculate the total octet
// by iterating through each character one by one.
// Too cumbersome
//
// 2. When breaking string at octet position 134, I have to detemeine which
// character is it ( by doing some kind of decoding)
// a. If the character code point does not fit in the octet
// boundary, it has to be carried-over to the next segment.
// The remaining bits after extracting the carry-over
// has to be filled with zero.
// b. If it is an escape character, then I have to backtrack
// even further since escape chars are not allowed to be splitted
// in the middle.
// Since the second bytes of escape chars can be confused with
// normal chars, I must always lookback 2 character ( repeat step a for at least 2 septet )
// c. After extracting the carry-on
// -> Option 2 is very hard when bit packing is already applied
//
// 3. Options 3 require extending Encoding interface,
// The not good point is not being able to utilize the encoder's Transform() method
// The good point is you don't have to do bit packing twice
// 4. Terrible option
// All this headaches really only apply to variable length encoding.
// When using fixed length encoding, you can really split the source message BEFORE encodes.