-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmpp.kt
More file actions
147 lines (112 loc) · 4.62 KB
/
Smpp.kt
File metadata and controls
147 lines (112 loc) · 4.62 KB
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
package com.example.testsmpp
import android.R.attr.password
import android.content.Context
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.cloudhopper.commons.charset.CharsetUtil
import com.cloudhopper.smpp.SmppBindType
import com.cloudhopper.smpp.SmppSession
import com.cloudhopper.smpp.SmppSessionConfiguration
import com.cloudhopper.smpp.impl.DefaultSmppClient
import com.cloudhopper.smpp.impl.DefaultSmppSessionHandler
import com.cloudhopper.smpp.pdu.DeliverSm
import com.cloudhopper.smpp.pdu.PduRequest
import com.cloudhopper.smpp.pdu.PduResponse
import com.cloudhopper.smpp.pdu.SubmitSm
import com.cloudhopper.smpp.type.Address
import com.cloudhopper.smpp.type.SmppBindException
import com.cloudhopper.smpp.type.SmppChannelException
import com.cloudhopper.smpp.type.SmppInvalidArgumentException
import com.cloudhopper.smpp.type.SmppTimeoutException
import com.cloudhopper.smpp.type.UnrecoverablePduException
import java.util.concurrent.TimeUnit
class Smpp(private val context: Context) : DefaultSmppSessionHandler() {
private var rSession: SmppSession? = null
fun sendSMS(number: String, text: String): Boolean {
try {
val client = DefaultSmppClient()
val session = client.bind(getSessionConfig(SmppBindType.TRANSCEIVER))
val sharedPref = context.getSharedPreferences(
"gateway_config",
AppCompatActivity.MODE_PRIVATE
)
val key = sharedPref.getString("key", "")
val enText = SecurityUtil.encryptText(text, key.toString())
val sm = createSubmitSm("9891211", "98$number", "$$$enText$$", "UCS-2")
println("Try to send message")
session.submit(sm, TimeUnit.SECONDS.toMillis(60))
Log.v("smpp", "hello")
println("Message sent")
println("Wait 10 seconds")
TimeUnit.SECONDS.sleep(10)
println("Destroy session")
session.close()
session.destroy()
println("Destroy client")
client.destroy()
println("Bye!")
return true
} catch (ex: SmppTimeoutException) {
Log.v("session", "SmppTimeoutException")
} catch (ex: SmppChannelException) {
Log.v("session", "SmppChannelException")
} catch (ex: SmppBindException) {
Log.v("session", "SmppBindException")
} catch (ex: UnrecoverablePduException) {
Log.v("session", "UnrecoverablePduException")
} catch (ex: InterruptedException) {
Log.v("session", "InterruptedException")
}
return false
}
private fun getSessionConfig(type: SmppBindType): SmppSessionConfiguration? {
val sessionConfig = SmppSessionConfiguration()
sessionConfig.type = type
val sharedPref = context.getSharedPreferences("gateway_config", Context.MODE_PRIVATE)
sessionConfig.host = sharedPref.getString("host", "")
sessionConfig.port = sharedPref.getInt("port", 0)
sessionConfig.systemId = sharedPref.getString("username", "")
sessionConfig.password = sharedPref.getString("password", "")
return sessionConfig
}
@Throws(SmppInvalidArgumentException::class)
fun createSubmitSm(src: String?, dst: String?, text: String?, charset: String?): SubmitSm? {
val sm = SubmitSm()
// For alpha numeric will use
// TON=5
// NPI=0
sm.sourceAddress = Address(5.toByte(), 0.toByte(), src)
// For national numbers will use
// TON=1
// NPI=1
sm.destAddress = Address(1.toByte(), 1.toByte(), dst)
// Set datacoding to UCS-2
sm.dataCoding = 8.toByte()
// Encode text
sm.shortMessage = CharsetUtil.encode(text, charset)
return sm
}
fun bindToSMSC() {
try {
val client = DefaultSmppClient()
rSession = client.bind(getSessionConfig(SmppBindType.RECEIVER))
println("Connected to SMSC...")
println("Ready to receive PDU...")
} catch (e: java.lang.Exception) {
e.printStackTrace()
}
}
override fun firePduRequestReceived(pduRequest: PduRequest<*>): PduResponse? {
val response = pduRequest.createResponse()
val sms = pduRequest as DeliverSm
if (sms.dataCoding.toInt() == 0) {
println("From: " + sms.sourceAddress.address)
println("To: " + sms.destAddress.address)
println("Content: " + String(sms.shortMessage))
}
return response
}
private fun waitForExitSignal() {
rSession?.unbind(0)
}
}