forked from Philio/GoSMPP
-
Notifications
You must be signed in to change notification settings - Fork 4
/
smpp_pdu.go
857 lines (806 loc) · 20 KB
/
smpp_pdu.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
// GoSMPP - An SMPP library for Go
// Copyright 2010 Phil Bayfield
// This software is licensed under a Creative Commons Attribution-Share Alike 2.0 UK: England & Wales License
// Further information on this license can be found here: http://creativecommons.org/licenses/by-sa/2.0/uk/
package smpp
import (
"bufio"
"errors"
"fmt"
"reflect"
)
// PDU interface which all PDU types should implement
type PDU interface {
// Read the PDU from the buffer
read(r *bufio.Reader) (err error)
// Write the PDU to the buffer
write(w *bufio.Writer) (err error)
// Set the packet header
setHeader(hdr *PDUHeader)
// Get the packet header
GetHeader() *PDUHeader
// Get the struct
GetStruct() interface{}
}
// Common PDU functions & fields
type PDUCommon struct {
Header *PDUHeader
Optional OptParams
OptionalLen uint32
}
// Set header
func (pdu *PDUCommon) setHeader(hdr *PDUHeader) {
pdu.Header = hdr
}
// Get header
func (pdu *PDUCommon) GetHeader() *PDUHeader {
return pdu.Header
}
// Get Struct
func (pdu *PDUCommon) GetStruct() interface{} {
return *pdu
}
// Write Optional Params
func (pdu *PDUCommon) writeOptional(w *bufio.Writer) (err error) {
if len(pdu.Optional) > 0 {
for key, val := range pdu.Optional {
op := new(pduOptParam)
op.tag = uint16(key)
op.value = val
v := reflect.ValueOf(val)
switch t := v; t.Kind() {
case reflect.String:
op.length = uint16(len(val.(string)))
case reflect.Bool:
op.length = 1
case reflect.Uint8:
op.length = 1
case reflect.Uint16:
op.length = 2
case reflect.Uint32:
op.length = 4
case reflect.Uint64:
op.length = 8
}
err = op.write(w)
if err != nil {
return
}
}
}
return
}
// Bind PDU
type PDUBind struct {
PDUCommon
SystemId string
Password string
SystemType string
IfVersion uint8
AddrTon SMPPTypeOfNumber
AddrNpi SMPPNumericPlanIndicator
AddressRange string
}
// Read Bind PDU
func (pdu *PDUBind) read(r *bufio.Reader) (err error) {
// Read system id (null terminated string or null)
line, err := r.ReadBytes(0x00)
if err != nil {
err = errors.New("Bind: Error reading system id")
return
}
if len(line) > 1 {
pdu.SystemId = string(line[0 : len(line)-1])
}
// Read Password (null terminated string or null)
line, err = r.ReadBytes(0x00)
if err != nil {
err = errors.New("Bind: Error reading Password")
return
}
if len(line) > 1 {
pdu.Password = string(line[0 : len(line)-1])
}
// Read system type
line, err = r.ReadBytes(0x00)
if err != nil {
err = errors.New("Bind: Error reading system type")
return
}
if len(line) > 1 {
pdu.SystemType = string(line[0 : len(line)-1])
}
// Read interface version
c, err := r.ReadByte()
if err != nil {
err = errors.New("Bind: Error reading interface version")
return
}
pdu.IfVersion = uint8(c)
// Read TON
c, err = r.ReadByte()
if err != nil {
err = errors.New("Bind: Error reading default type of number")
return
}
pdu.AddrTon = SMPPTypeOfNumber(c)
// Read NPI
c, err = r.ReadByte()
if err != nil {
err = errors.New("Bind: Error reading default number plan indicator")
return
}
pdu.AddrNpi = SMPPNumericPlanIndicator(c)
// Read Address range
line, err = r.ReadBytes(0x00)
if err != nil {
err = errors.New("Bind Response: Error reading system type")
return
}
if len(line) > 1 {
pdu.AddressRange = string(line[0 : len(line)-1])
}
return
}
// Write Bind PDU
func (pdu *PDUBind) write(w *bufio.Writer) (err error) {
// Write Header
err = pdu.Header.write(w)
if err != nil {
err = errors.New("Bind: Error writing Header")
return
}
// Create byte array the size of the PDU
p := make([]byte, pdu.Header.CmdLength-pdu.OptionalLen-16)
pos := 0
// Copy system id
if len(pdu.SystemId) > 0 {
copy(p[pos:len(pdu.SystemId)], []byte(pdu.SystemId))
pos += len(pdu.SystemId)
}
pos++ // Null terminator
// Copy Password
if len(pdu.Password) > 0 {
copy(p[pos:pos+len(pdu.Password)], []byte(pdu.Password))
pos += len(pdu.Password)
}
pos++ // Null terminator
// Copy system type
if len(pdu.SystemType) > 0 {
copy(p[pos:pos+len(pdu.SystemType)], []byte(pdu.SystemType))
pos += len(pdu.SystemType)
}
pos++ // Null terminator
// Add interface version
p[pos] = byte(pdu.IfVersion)
pos++
// Add TON
p[pos] = byte(pdu.AddrTon)
pos++
// Add NPI
p[pos] = byte(pdu.AddrNpi)
pos++
// Copy Address range
if len(pdu.AddressRange) > 0 {
copy(p[pos:pos+len(pdu.AddressRange)], []byte(pdu.AddressRange))
pos += len(pdu.AddressRange)
}
// Write to buffer
_, err = w.Write(p)
if err != nil {
err = errors.New("Bind: Error writing to buffer")
return
}
// Flush write buffer
err = w.Flush()
if err != nil {
err = errors.New("Bind: Error flushing write buffer")
}
return
}
// Get Struct
func (pdu *PDUBind) GetStruct() interface{} {
return *pdu
}
// Bind Response PDU
type PDUBindResp struct {
PDUCommon
SystemId string
}
// Read Bind Response PDU
func (pdu *PDUBindResp) read(r *bufio.Reader) (err error) {
// Read system id (null terminated string or null)
line, err := r.ReadBytes(0x00)
if err != nil {
err = errors.New("Bind Response: Error reading system id")
return
}
if len(line) > 1 {
pdu.SystemId = string(line[0 : len(line)-1])
}
// Read Optional param
if pdu.Header.CmdLength > uint32(len(pdu.SystemId))+17 {
op := new(pduOptParam)
err = op.read(r)
if err != nil {
err = errors.New("Bind Response: Error reading Optional param")
return
}
pdu.Optional = OptParams{SMPPOptionalParamTag(op.tag): op.value}
}
return
}
// Write Bind Response PDU
func (pdu *PDUBindResp) write(w *bufio.Writer) (err error) {
// Write Header
err = pdu.Header.write(w)
if err != nil {
err = errors.New("Bind Response: Error writing Header")
return
}
// Create byte array the size of the PDU
p := make([]byte, pdu.Header.CmdLength-pdu.OptionalLen-16)
pos := 0
// Copy system id
if len(pdu.SystemId) > 0 {
copy(p[pos:len(pdu.SystemId)], []byte(pdu.SystemId))
pos += len(pdu.SystemId)
}
pos++ // Null terminator
// Write to buffer
_, err = w.Write(p)
if err != nil {
err = errors.New("Bind Response: Error writing to buffer")
return
}
// Flush write buffer
err = w.Flush()
if err != nil {
err = errors.New("Bind Response: Error flushing write buffer")
}
// Optional params
err = pdu.writeOptional(w)
if err != nil {
err = errors.New("Bind Response: Error writing optional params")
}
return
}
// Get Struct
func (pdu *PDUBindResp) GetStruct() interface{} {
return *pdu
}
// Unbind PDU
type PDUUnbind struct {
PDUCommon
}
// Read Unbind PDU
func (pdu *PDUUnbind) read(r *bufio.Reader) (err error) {
return
}
// Write Unbind PDU
func (pdu *PDUUnbind) write(w *bufio.Writer) (err error) {
// Write Header
err = pdu.Header.write(w)
if err != nil {
err = errors.New("Unbind: Error writing Header")
}
return
}
// Get Struct
func (pdu *PDUUnbind) GetStruct() interface{} {
return *pdu
}
// Unbind Response PDU
type PDUUnbindResp struct {
PDUCommon
}
// Read Unbind Response PDU
func (pdu *PDUUnbindResp) read(r *bufio.Reader) (err error) {
return
}
// Write Unbind Response PDU
func (pdu *PDUUnbindResp) write(w *bufio.Writer) (err error) {
// Write Header
err = pdu.Header.write(w)
if err != nil {
err = errors.New("Unbind Response: Error writing Header")
}
return
}
// Get Struct
func (pdu *PDUUnbindResp) GetStruct() interface{} {
return *pdu
}
// Submit SM PDU
type PDUSubmitSM struct {
PDUCommon
ServiceType string
SourceAddrTon SMPPTypeOfNumber
SourceAddrNpi SMPPNumericPlanIndicator
SourceAddr string
DestAddrTon SMPPTypeOfNumber
DestAddrNpi SMPPNumericPlanIndicator
DestAddr string
EsmClass SMPPEsmClassESME
ProtocolId uint8
PriorityFlag SMPPPriority
SchedDelTime string
ValidityPeriod string
RegDelivery SMPPDelivery
ReplaceFlag uint8
DataCoding SMPPDataCoding
SmDefaultMsgId uint8
SmLength uint8
ShortMessage string
}
// Read SubmitSM PDU
func (pdu *PDUSubmitSM) read(r *bufio.Reader) (err error) {
return
}
// Write SubmitSM PDU
func (pdu *PDUSubmitSM) write(w *bufio.Writer) (err error) {
// Write Header
err = pdu.Header.write(w)
if err != nil {
err = errors.New("SubmitSM: Error writing Header")
return
}
// Create byte array the size of the PDU
p := make([]byte, pdu.Header.CmdLength-pdu.OptionalLen-16)
pos := 0
// Copy service type
if len(pdu.ServiceType) > 0 {
copy(p[pos:len(pdu.ServiceType)], []byte(pdu.ServiceType))
pos += len(pdu.ServiceType)
}
pos++ // Null terminator
// Source TON
p[pos] = byte(pdu.SourceAddrTon)
pos++
// Source NPI
p[pos] = byte(pdu.SourceAddrNpi)
pos++
// Source Address
if len(pdu.SourceAddr) > 0 {
copy(p[pos:pos+len(pdu.SourceAddr)], []byte(pdu.SourceAddr))
pos += len(pdu.SourceAddr)
}
pos++ // Null terminator
// Destination TON
p[pos] = byte(pdu.DestAddrTon)
pos++
// Destination NPI
p[pos] = byte(pdu.DestAddrNpi)
pos++
// Destination Address
if len(pdu.DestAddr) > 0 {
copy(p[pos:pos+len(pdu.DestAddr)], []byte(pdu.DestAddr))
pos += len(pdu.DestAddr)
}
pos++ // Null terminator
// ESM Class
p[pos] = byte(pdu.EsmClass)
pos++
// Protocol Id
p[pos] = byte(pdu.ProtocolId)
pos++
// Priority Flag
p[pos] = byte(pdu.PriorityFlag)
pos++
// Sheduled Delivery Time
if len(pdu.SchedDelTime) > 0 {
copy(p[pos:pos+len(pdu.SchedDelTime)], []byte(pdu.SchedDelTime))
pos += len(pdu.SchedDelTime)
}
pos++ // Null terminator
// Validity Period
if len(pdu.ValidityPeriod) > 0 {
copy(p[pos:pos+len(pdu.ValidityPeriod)], []byte(pdu.ValidityPeriod))
pos += len(pdu.ValidityPeriod)
}
pos++ // Null terminator
// Registered Delivery
p[pos] = byte(pdu.RegDelivery)
pos++
// Replace Flag
p[pos] = byte(pdu.ReplaceFlag)
pos++
// Data Coding
p[pos] = byte(pdu.DataCoding)
pos++
// Default Msg Id
p[pos] = byte(pdu.SmDefaultMsgId)
pos++
// Msg Length
p[pos] = byte(pdu.SmLength)
pos++
// Message
if len(pdu.ShortMessage) > 0 {
copy(p[pos:pos+len(pdu.ShortMessage)], []byte(pdu.ShortMessage))
pos += len(pdu.ShortMessage)
}
// Write to buffer
_, err = w.Write(p)
if err != nil {
err = errors.New("SubmitSM: Error writing to buffer")
return
}
// Flush write buffer
err = w.Flush()
if err != nil {
err = errors.New("SubmitSM: Error flushing write buffer")
return
}
// Optional params
err = pdu.writeOptional(w)
if err != nil {
err = errors.New("SubmitSM: Error writing optional params")
}
return
}
// Get Struct
func (pdu *PDUSubmitSM) GetStruct() interface{} {
return *pdu
}
// SubmitSM Response PDU
type PDUSubmitSMResp struct {
PDUCommon
MessageId string
}
// Read SubmitSM Response PDU
func (pdu *PDUSubmitSMResp) read(r *bufio.Reader) (err error) {
// Read message id (null terminated string or null)
line, err := r.ReadBytes(0x00)
if err != nil {
err = errors.New("SubmitSM Response: Error reading message id")
return
}
if len(line) > 1 {
pdu.MessageId = string(line[0 : len(line)-1])
}
return
}
// Write SubmitSM Response PDU
func (pdu *PDUSubmitSMResp) write(r *bufio.Writer) (err error) {
return
}
// Get Struct
func (pdu *PDUSubmitSMResp) GetStruct() interface{} {
return *pdu
}
// SubmitMulti PDU
type PDUSubmitMulti struct {
PDUCommon
ServiceType string
SourceAddrTon SMPPTypeOfNumber
SourceAddrNpi SMPPNumericPlanIndicator
SourceAddr string
NumOfDests uint8
DestAddrTon SMPPTypeOfNumber
DestAddrNpi SMPPNumericPlanIndicator
DestAddrs []string
DestLists []string
EsmClass SMPPEsmClassESME
ProtocolId uint8
PriorityFlag SMPPPriority
SchedDelTime string
ValidityPeriod string
RegDelivery SMPPDelivery
ReplaceFlag uint8
DataCoding SMPPDataCoding
SmDefaultMsgId uint8
SmLength uint8
ShortMessage string
}
// Read SubmitMulti PDU
func (pdu *PDUSubmitMulti) read(r *bufio.Reader) (err error) {
return
}
// Write SubmitMulti PDU
func (pdu *PDUSubmitMulti) write(w *bufio.Writer) (err error) {
// Write Header
err = pdu.Header.write(w)
if err != nil {
err = errors.New("SubmitMulti: Error writing Header")
return
}
// Create byte array the size of the PDU
p := make([]byte, pdu.Header.CmdLength-pdu.OptionalLen-16)
pos := 0
// Copy service type
if len(pdu.ServiceType) > 0 {
copy(p[pos:len(pdu.ServiceType)], []byte(pdu.ServiceType))
pos += len(pdu.ServiceType)
}
pos++ // Null terminator
// Source TON
p[pos] = byte(pdu.SourceAddrTon)
pos++
// Source NPI
p[pos] = byte(pdu.SourceAddrNpi)
pos++
// Source Address
if len(pdu.SourceAddr) > 0 {
copy(p[pos:pos+len(pdu.SourceAddr)], []byte(pdu.SourceAddr))
pos += len(pdu.SourceAddr)
}
pos++ // Null terminator
// Number of destinations
p[pos] = byte(pdu.NumOfDests)
pos++
// Send destination numbers
for _, destNum := range pdu.DestAddrs {
// Number indicator
p[pos] = byte(0x01)
pos++
// Destination TON
p[pos] = byte(pdu.DestAddrTon)
pos++
// Destination NPI
p[pos] = byte(pdu.DestAddrNpi)
pos++
// Copy number
copy(p[pos:pos+len(destNum)], []byte(destNum))
pos += len(destNum) + 1
}
// Send destination lists
for _, destList := range pdu.DestLists {
// List indicator
p[pos] = byte(0x02)
pos++
// Copy list name
copy(p[pos:pos+len(destList)], []byte(destList))
pos += len(destList) + 1
}
// ESM Class
p[pos] = byte(pdu.EsmClass)
pos++
// Protocol Id
p[pos] = byte(pdu.ProtocolId)
pos++
// Priority Flag
p[pos] = byte(pdu.PriorityFlag)
pos++
// Sheduled Delivery Time
if len(pdu.SchedDelTime) > 0 {
copy(p[pos:pos+len(pdu.SchedDelTime)], []byte(pdu.SchedDelTime))
pos += len(pdu.SchedDelTime)
}
pos++ // Null terminator
// Validity Period
if len(pdu.ValidityPeriod) > 0 {
copy(p[pos:pos+len(pdu.ValidityPeriod)], []byte(pdu.ValidityPeriod))
pos += len(pdu.ValidityPeriod)
}
pos++ // Null terminator
// Registered Delivery
p[pos] = byte(pdu.RegDelivery)
pos++
// Replace Flag
p[pos] = byte(pdu.ReplaceFlag)
pos++
// Data Coding
p[pos] = byte(pdu.DataCoding)
pos++
// Default Msg Id
p[pos] = byte(pdu.SmDefaultMsgId)
pos++
// Msg Length
p[pos] = byte(pdu.SmLength)
pos++
// Message
if len(pdu.ShortMessage) > 0 {
copy(p[pos:pos+len(pdu.ShortMessage)], []byte(pdu.ShortMessage))
pos += len(pdu.ShortMessage)
}
// Write to buffer
_, err = w.Write(p)
if err != nil {
err = errors.New("SubmitMulti: Error writing to buffer")
return
}
// Flush write buffer
err = w.Flush()
if err != nil {
err = errors.New("SubmitMulti: Error flushing write buffer")
return
}
// Optional params
err = pdu.writeOptional(w)
if err != nil {
err = errors.New("SubmitMulti: Error writing optional params")
}
fmt.Printf("SubmitMulti Header: %#v\n", pdu.Header)
fmt.Printf("SubmitMulti p: %#v\nLength p: %d\n", p, len(p))
return
}
// Get Struct
func (pdu *PDUSubmitMulti) GetStruct() interface{} {
return *pdu
}
// SubmitMulti Response PDU
type PDUSubmitMultiResp struct {
PDUCommon
MessageId string
NumUnsuccess uint8
Unsuccess []string
ErrorCodes []uint32
}
// Read SubmitMulti Response PDU
func (pdu *PDUSubmitMultiResp) read(r *bufio.Reader) (err error) {
// Read message id (null terminated string or null)
line, err := r.ReadBytes(0x00)
if err != nil {
err = errors.New("SubmitMulti Response: Error reading message id")
return
}
if len(line) > 1 {
pdu.MessageId = string(line[0 : len(line)-1])
}
// Read unsuccessful destinations
c, err := r.ReadByte()
if err != nil {
err = errors.New("SubmitMulti Response: Error reading number of unsuccessful destinations")
return
}
pdu.NumUnsuccess = uint8(c)
// Read Unsuccessful destination numbers
pdu.Unsuccess = make([]string, pdu.NumUnsuccess)
pdu.ErrorCodes = make([]uint32, pdu.NumUnsuccess)
for i := uint8(0); i < pdu.NumUnsuccess; i++ {
// Discard Ton/Npi
p := make([]byte, 2)
_, err = r.Read(p)
if err != nil {
err = errors.New("SubmitMulti Response: Error reading TON/NPI")
return
}
// Read Destination
line, err = r.ReadBytes(0x00)
if err != nil {
err = errors.New("SubmitMulti Response: Error reading destination")
return
}
if len(line) > 1 {
pdu.Unsuccess[i] = string(line[0 : len(line)-1])
}
// Read Error code
p = make([]byte, 4)
_, err = r.Read(p)
if err != nil {
err = errors.New("SubmitMulti Response: Error reading error code")
return
}
pdu.ErrorCodes[i] = uint32(unpackUint(p))
}
return
}
// Write SubmitMulti Response PDU
func (pdu *PDUSubmitMultiResp) write(r *bufio.Writer) (err error) {
return
}
// Get Struct
func (pdu *PDUSubmitMultiResp) GetStruct() interface{} {
return *pdu
}
// PDU Header
type PDUHeader struct {
CmdLength uint32
CmdId SMPPCommand
CmdStatus SMPPCommandStatus
Sequence uint32
}
// Read PDU Header
func (hdr *PDUHeader) read(r *bufio.Reader) (err error) {
// Read all 16 Header bytes
p := make([]byte, 16)
_, err = r.Read(p)
if err != nil {
return
}
// Convert bytes into Header vars
hdr.CmdLength = uint32(unpackUint(p[0:4]))
hdr.CmdId = SMPPCommand(unpackUint(p[4:8]))
hdr.CmdStatus = SMPPCommandStatus(unpackUint(p[8:12]))
hdr.Sequence = uint32(unpackUint(p[12:16]))
return
}
// Write PDU Header
func (hdr *PDUHeader) write(w *bufio.Writer) (err error) {
// Convert Header into byte array
p := make([]byte, 16)
copy(p[0:4], packUint(uint64(hdr.CmdLength), 4))
copy(p[4:8], packUint(uint64(hdr.CmdId), 4))
copy(p[8:12], packUint(uint64(hdr.CmdStatus), 4))
copy(p[12:16], packUint(uint64(hdr.Sequence), 4))
// Write Header
_, err = w.Write(p)
if err != nil {
return
}
// Flush write buffer
err = w.Flush()
return
}
// Optional paramater
type pduOptParam struct {
tag uint16
length uint16
value interface{}
}
// Read Optional param
func (op *pduOptParam) read(r *bufio.Reader) (err error) {
// Read first 4 descripter bytes
p := make([]byte, 4)
_, err = r.Read(p)
if err != nil {
return
}
op.tag = uint16(unpackUint(p[0:2]))
op.length = uint16(unpackUint(p[2:4]))
// Read value data
if op.length > 0 {
vp := make([]byte, op.length)
_, err = r.Read(vp)
if err != nil {
return
}
// Determine data type of value
switch op.tag {
case TAG_ADDITIONAL_STATUS_INFO_TEXT, TAG_RECEIPTED_MESSAGE_ID, TAG_SOURCE_SUBADDRESS, TAG_DEST_SUBADDRESS, TAG_NETWORK_ERROR_CODE, TAG_MESSAGE_PAYLOAD, TAG_CALLBACK_NUM, TAG_CALLBACK_NUM_ATAG, TAG_ITS_SESSION_INFO:
op.value = string(vp)
case TAG_DEST_ADDR_SUBUNIT, TAG_SOURCE_ADDR_SUBUNIT, TAG_DEST_NETWORK_TYPE, TAG_SOURCE_NETWORK_TYPE, TAG_DEST_BEARER_TYPE, TAG_SOURCE_BEARER_TYPE, TAG_SOURCE_TELEMATICS_ID, TAG_PAYLOAD_TYPE, TAG_MS_MSG_WAIT_FACILITIES, TAG_PRIVACY_INDICATOR, TAG_USER_RESPONSE_CODE, TAG_LANGUAGE_INDICATOR, TAG_SAR_TOTAL_SEGMENTS, TAG_SAR_SEGMENT_SEQNUM, TAG_SC_INTERFACE_VERSION, TAG_DISPLAY_TIME, TAG_MS_VALIDITY, TAG_DPF_RESULT, TAG_SET_DPF, TAG_MS_AVAILABILITY_STATUS, TAG_DELIVERY_FAILURE_REASON, TAG_MORE_MESSAGES_TO_SEND, TAG_MESSAGE_STATE, TAG_CALLBACK_NUM_PRES_IND, TAG_NUMBER_OF_MESSAGES, TAG_SMS_SIGNAL, TAG_ITS_REPLY_TYPE, TAG_USSD_SERVICE_OP:
op.value = uint8(vp[0])
case TAG_DEST_TELEMATICS_ID, TAG_USER_MESSAGE_REFERENCE, TAG_SOURCE_PORT, TAG_DESTINATION_PORT, TAG_SAR_MSG_REF_NUM:
op.value = uint16(unpackUint(vp))
case TAG_QOS_TIME_TO_LIVE:
op.value = uint32(unpackUint(vp))
}
} else {
op.value = nil
}
return
}
// Write Optional param
func (op *pduOptParam) write(w *bufio.Writer) (err error) {
// Create byte array
p := make([]byte, 4+op.length)
copy(p[0:2], packUint(uint64(op.tag), 2))
copy(p[2:4], packUint(uint64(op.length), 2))
// Determine data type of value
v := reflect.ValueOf(op.value)
switch t := v; t.Kind() {
case reflect.String:
copy(p[4:op.length], []byte(op.value.(string)))
case reflect.Uint8:
p[4] = byte(op.value.(uint8))
case reflect.Uint16:
copy(p[4:6], packUint(uint64(op.value.(uint16)), 2))
case reflect.Uint32:
copy(p[4:8], packUint(uint64(op.value.(uint32)), 4))
}
// Write to buffer
_, err = w.Write(p)
if err != nil {
return
}
// Flush write buffer
err = w.Flush()
return
}
// Unpack uint from l bytes (big endian)
func unpackUint(p []byte) (n uint64) {
l := uint8(len(p))
for i := uint8(0); i < l; i++ {
n |= uint64(p[i]) << ((l - i - 1) * 8)
}
return
}
// Pack uint into l bytes (big endian)
func packUint(n uint64, l uint8) (p []byte) {
p = make([]byte, l)
for i := uint8(0); i < l; i++ {
p[i] = byte(n >> ((l - i - 1) * 8))
}
return
}