/
deliver_sm.go
217 lines (206 loc) · 5.7 KB
/
deliver_sm.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
package pdu
import (
"fmt"
"io/ioutil"
"time"
smpptime "github.com/ajankovic/smpp/time"
)
// DeliverSm contains mandatory fields for delivering short message.
// There is no need to set SmLength it will be automatically set when
// encoding pdu to binary representation.
type DeliverSm struct {
ServiceType string
SourceAddrTon int
SourceAddrNpi int
SourceAddr string
DestAddrTon int
DestAddrNpi int
DestinationAddr string
EsmClass EsmClass
ProtocolID int
PriorityFlag int
ScheduleDeliveryTime time.Time
ValidityPeriod time.Time
RegisteredDelivery RegisteredDelivery
ReplaceIfPresentFlag int
DataCoding int
SmDefaultMsgID int
ShortMessage string
Options *Options
}
// CommandID implements pdu.PDU interface.
func (p DeliverSm) CommandID() CommandID {
return DeliverSmID
}
// Response creates new DeliverSmResp.
func (p DeliverSm) Response(msgID string) *DeliverSmResp {
return &DeliverSmResp{
MessageID: msgID,
}
}
// MarshalBinary implements encoding.BinaryMarshaler interface.
func (p DeliverSm) MarshalBinary() ([]byte, error) {
out := append(
[]byte(p.ServiceType),
0,
byte(p.SourceAddrTon),
byte(p.SourceAddrNpi),
)
out = append(out, append([]byte(p.SourceAddr), 0)...)
out = append(out, byte(p.DestAddrTon), byte(p.DestAddrNpi))
out = append(out, append([]byte(p.DestinationAddr), 0)...)
out = append(out, p.EsmClass.Byte(), byte(p.ProtocolID), byte(p.PriorityFlag))
tm, err := writeTime(smpptime.Absolute, p.ScheduleDeliveryTime)
if err != nil {
return nil, err
}
out = append(out, tm...)
tm, err = writeTime(smpptime.Absolute, p.ValidityPeriod)
if err != nil {
return nil, err
}
out = append(out, tm...)
l := len(p.ShortMessage)
out = append(out, p.RegisteredDelivery.Byte(), byte(p.ReplaceIfPresentFlag), byte(p.DataCoding), byte(p.SmDefaultMsgID), byte(l))
if l > 0 {
out = append(out, []byte(p.ShortMessage)...)
}
if p.Options == nil {
return out, nil
}
opts, err := p.Options.MarshalBinary()
if err != nil {
return nil, err
}
return append(out, opts...), nil
}
// UnmarshalBinary implements encoding.BinaryUnmarshaler interface.
func (p *DeliverSm) UnmarshalBinary(body []byte) error {
if len(body) < 25 {
return fmt.Errorf("smpp/pdu: deliver_sm body too short: %d", len(body))
}
buf := newBuffer(body)
res, err := buf.ReadCString(6)
if err != nil {
return fmt.Errorf("smpp/pdu: decoding service_type %s", err)
}
p.ServiceType = string(res)
b, err := buf.ReadByte()
if err != nil {
return fmt.Errorf("smpp/pdu: decoding source_addr_ton %s", err)
}
p.SourceAddrTon = int(b)
b, err = buf.ReadByte()
if err != nil {
return fmt.Errorf("smpp/pdu: decoding source_addr_npi %s", err)
}
p.SourceAddrNpi = int(b)
res, err = buf.ReadCString(21)
if err != nil {
return fmt.Errorf("smpp/pdu: decoding source_addr %s", err)
}
p.SourceAddr = string(res)
b, err = buf.ReadByte()
if err != nil {
return fmt.Errorf("smpp/pdu: decoding dest_addr_ton %s", err)
}
p.DestAddrTon = int(b)
b, err = buf.ReadByte()
if err != nil {
return fmt.Errorf("smpp/pdu: decoding dest_addr_npi %s", err)
}
p.DestAddrNpi = int(b)
res, err = buf.ReadCString(21)
if err != nil {
return fmt.Errorf("smpp/pdu: decoding dest_addr %s", err)
}
p.DestinationAddr = string(res)
b, err = buf.ReadByte()
if err != nil {
return fmt.Errorf("smpp/pdu: decoding esm_class %s", err)
}
p.EsmClass = ParseEsmClass(b)
b, err = buf.ReadByte()
if err != nil {
return fmt.Errorf("smpp/pdu: decoding protocol_id %s", err)
}
p.ProtocolID = int(b)
b, err = buf.ReadByte()
if err != nil {
return fmt.Errorf("smpp/pdu: decoding priority_flag %s", err)
}
p.PriorityFlag = int(b)
res, err = buf.ReadCString(17)
if err != nil {
return fmt.Errorf("smpp/pdu: decoding schedule_delivery_time %s", err)
}
t, err := smpptime.Parse(res)
if err != nil {
return fmt.Errorf("smpp/pdu: decoding schedule_delivery_time %s", err)
}
p.ScheduleDeliveryTime = t
res, err = buf.ReadCString(17)
if err != nil {
return fmt.Errorf("smpp/pdu: decoding validity_period %s", err)
}
t, err = smpptime.Parse(res)
if err != nil {
return fmt.Errorf("smpp/pdu: decoding validity_period %s", err)
}
p.ValidityPeriod = t
b, err = buf.ReadByte()
if err != nil {
return fmt.Errorf("smpp/pdu: decoding registered_delivery %s", err)
}
p.RegisteredDelivery = ParseRegisteredDelivery(b)
b, err = buf.ReadByte()
if err != nil {
return fmt.Errorf("smpp/pdu: decoding replace_if_present_flag %s", err)
}
p.ReplaceIfPresentFlag = int(b)
b, err = buf.ReadByte()
if err != nil {
return fmt.Errorf("smpp/pdu: decoding data_coding %s", err)
}
p.DataCoding = int(b)
b, err = buf.ReadByte()
if err != nil {
return fmt.Errorf("smpp/pdu: decoding sm_default_msg_id %s", err)
}
p.SmDefaultMsgID = int(b)
sm, err := buf.ReadString(254)
if err != nil {
return fmt.Errorf("smpp/pdu: decoding short_message %s", err)
}
p.ShortMessage = string(sm)
if buf.Len() == 0 {
return nil
}
if p.Options == nil {
p.Options = NewOptions()
}
opts, err := ioutil.ReadAll(buf)
if err != nil {
return err
}
if err := p.Options.UnmarshalBinary(opts); err != nil {
return err
}
return nil
}
// DeliverSmResp contains mandatory fields for deliver_sm response.
type DeliverSmResp struct {
MessageID string
}
// CommandID implements pdu.PDU interface.
func (p DeliverSmResp) CommandID() CommandID {
return DeliverSmRespID
}
// MarshalBinary implements encoding.BinaryMarshaler interface.
func (p DeliverSmResp) MarshalBinary() ([]byte, error) {
return []byte{0}, nil
}
// UnmarshalBinary implements encoding.BinaryUnmarshaler interface.
func (p *DeliverSmResp) UnmarshalBinary(body []byte) error {
return nil
}