'####################################
'SMS / EMS ENCODER
'WRITE BY HESICONG
'Last Edited:
2005/2/20
'Until Now no bugfact.
'Contact:
'Email:
'Hesicong@mail.sc.cninfo.net
'OR
'
38288890@qq.com
'HomePage:
'Http://dream-world.nease.net
'THANKS for Using My Code
'####################################
Namespace SMS
Namespace Encoder
Public Class SMS
#Region "enums"
Public Enum enum_tp_vpf
RELATIVE_FORMAT = 16 'b4 = 1 b3 = 0
END ENUM
Public enum_tp_sri
REQUEST_SMS_REPORT = 32
NO_SMS_REPORT = 0
END ENUM
Public enum enum_tp_dcs
DEFAULTALPHABET = 0
UCS2 = 8
END ENUM
Public enum_tp_valid_period
OneHour = 11 '0 to 143: (TP-VP 1) * 5min
Threehours = 29
SixhouRS = 71
TwelveHours = 143
Oneday = 167
ONEWEEK = 196
Maximum = 255
END ENUM
#End region
#Region "Private Data"
Protected sc_number as string 'Note The Plus!
Protected tp_mti as byte = 1
Protected tp_rd as byte = 0
Protected tp_vpf as byte = 16
Protected tp_udhi as byte
Protected tp_srr as byte
Protected tp_mr as integer
Protected tp_da as string
Protected tp_pid as byte
Protected tp_dcs as byte
Protected tp_vp as byte
Protected tp_udl as integer
Protected tp_UD AS STRING
#End region
#Region "Properties"
Public property serviceCenterNumber () AS STRING
Get
Return SC_Number
END GET
Set (byval value as string)
'Convert An ServiceCenterNumber to PDU Code
IF INSTR (Value, " ") THEN
SC_NUMBER = "91"
Else
SC_NUMBER = "81"
END IF
Value = MID (Value, 2)
Dim I as integer (value.length mod 2) = 1 THEN
Value = "f"
END IF
For i = 1 to value.Length Step 2
SC_NUMBER = SWAP (MID (Value, I, 2))
NEXT
SC_NUMBER = BYTETOHEX ((SC_NUMBER.LENGTH - 2) / 2 1) SC_NUMBER
End set
End Property
Public property TP_STATUS_REPORT_REQUEST () AS ENUM_TP_SRI
Get
Return TP_SRR
END GET
Set (ByVal Value As Enum_TP_SRI)
TP_SRR = Value
End set
End Property
Public property TP_MESSAGE_REMENCE () AS INTEGER
Get
Return TP_MR
END GET
Set (ByVal Value As Integer)
TP_MR = Value
End set
End Property
Public property TP_DESTINATION_ADDRESS () AS STRING
Get
Return TP_DA
END GET
Set (byval value as string)
TP_DA = "" "
IF INSTR (Value, " ") THEN
TP_DA = "91"
Else
TP_DA = "81"
END IF
Value = value.Replace (" ", "")
TP_DA = Format (Value.Length, "X2") TP_DA
DIM I as integer
IF (Value.Length Mod 2) = 1 THEN
Value = "f"
END IF
For i = 1 to value.Length Step 2
TP_DA = SWAP (MID (Value, I, 2))
NEXT
End set
End Property
Public property TP_DATA_CODING_SCHEME () AS enum_tp_dcs
Get
Return TP_DCS
END GET
Set (ByVal Value As Enum_TP_DCS)
TP_DCS = Value
End set
End Property
Public property TP_VALIDITY_PERIOD () AS Enum_TP_VALID_PERIOD
Get
Return TP_VP
END GET
Set (Byval Value As Enum_TP_VALID_PERIOD)
TP_VP = Value
End set
End Property
Public overridable property tp_user_data () AS STRING
Get
Return TP_UD
END GET
Set (byval value as string)
SELECT CASE TP_DCS
Case is = enum_tp_dcs.defaultalphabet
TP_UDL = VALUE.LENGTH
TP_UD = Encode7bit (Value)
Case is = enum_tp_dcs.ucs2tp_udl = value.length * 2
TP_UD = EncodeuCS2 (Value)
Case Else
TP_UD = Value
End SELECT
End set
End Property
#End region
#Region "functions"
Public Shared Function CheckForencoding (Byval Content As String) AS SMS.Enum_tp_dcs
DIM I as integer
For i = 1 to content.Length
IF ASC (MID (Content, I, 1)) <0 THEN
Return SMS.Enum_tp_dcs.ucs2
END IF
NEXT
Return SMS.Enum_tp_dcs.defaultalphabet
END FUNCTION
Public overridable function getsmspducode () AS STRING
Dim Pducode As String
'Check User Data Length
IF tp_dcs = enum_tp_dcs.defaultalphabet then
If TP_UD.LENGTH> 280 THROW New Exception ("User Data I to SMS")
END IF
IF tp_dcs = enum_tp_dcs.us2 then
If TP_UD.LENGTH> 280 THROW New Exception ("User Data I to SMS")
END IF
'Make Pducode
PDucode = SC_NUMBER
PDucode = firstoctet ()
PDucode = Format (TP_MR, "X2")
PDucode = TP_DA
PDucode = Format (TP_PID, "X2")
PDucode = Format (TP_DCS, "X2")
PDucode = Format (TP_VP, "X2")
PDucode = Format (TP_UDL, "X2")
PDucode = TP_UD
Return PDucode
END FUNCTION
Public overridable function firstector () AS STRING
Return ByTohex (TP_MTI TP_VPF TP_SRR TP_UDHI)
END FUNCTION
Shared function bytetohex (byval abyte as byte) AS STRING
Dim Result As String
Result = format (abyte, "x2")
Return RESULT
END FUNCTION
#Region "Enocode7bit"
Shared function encode7bit (byval content as string) AS String
'Prepare
Dim CharRay As Char () = Content.toChararray
DIM C as char
Dim t as string
For Each C in Chararrayt = ChartO7BITS (C) T
NEXT
'Add "0"
DIM I as integer
IF (T.Length Mod 8) <> 0 THEN
For i = 1 to 8 - (t.length mod 8)
T = "0" T
NEXT
END IF
'Split Into 8bits
Dim Result As String
For i = t.length - 8 TO 0 Step -8
Result = Result BitStohex (MID (T, I 1, 8)))
NEXT
Return RESULT
END FUNCTION
Shared Function BitStohex (Byval Bits As String) AS String
'Convert 8bits to Hex String
DIM I, V AS INTEGER
For i = 0 to bits.Length - 1
V = V VAL (MID (BITS, I 1, 1) * 2 ^ (7 - i)
NEXT
Dim Result As String
Result = Format (v, "x2")
Return RESULT
END FUNCTION
Shared Function CharTo7Bits (Byval C As Char) AS String
IF c = "@" Then return "0000000"
Dim Result As String
DIM I as integer
For i = 0 TO 6
IF (ASC (C) and 2 ^ i)> 0 THEN
Result = "1" result
Else
Result = "0" Result
END IF
NEXT
Return RESULT
END FUNCTION
#End region
Shared function encodeucs2 (byval content as string) AS String
DIM I, J, V AS INTEGER
Dim Result, t as string
For i = 1 to content.Length
v = ASCW (MID (Content, I, 4))
T = format (v, "x4")
Result = T
NEXT
Return RESULT
END FUNCTION
Shared function swap (byref twobitstr as string) AS String
'Swap Two Bit Like "EF" to "fe"
DIM C () as char = twobitstr.tochararray
Dim t as char
T = C (0)
c (0) = c (1)
c (1) = t
Return (C (0) C (1)). TOSTRING
END FUNCTION
#End region
END CLASS
Public Class ConcatenatedShortMessage
Inherits SMS
Private TotalMessages as in
Public function getemspducode () as string ()
SELECT CASE TP_DCSCASE ENUM_TP_DCS.UCS2
TotalMessages = (TP_UD.LENGTH / 4) / 66 ((TP_UD.LENGTH / 4 MOD 66) = 0)
Case enum_tp_dcs.defaultalphabet
TotalMessages = (TP_UD.LENGTH / 266) - ((TP_UD.LENGTH MOD 266) = 0)
End SELECT
Dim Result (TotalMessages) AS STRING
DIM TMPTP_UD AS STRING
DIM I as integer
TP_UDHI = 2 ^ 6
DIM REFERENCE AS INTEGER = RND (1) * 65536 '16bit Reference Number' See 3GPP Document
For i = 0 to TotalMessages
SELECT CASE TP_DCS
Case enum_tp_dcs.ucs2
TMPTP_UD = MID (TP_UD, I * 66 * 4 1, 66 * 4)
'When TP_UDL IS ODD, THE MAX Length of An Unicode String in PDU Code IS 66 Charactor.see [3GPP TS 23.040 V6.5.0 (2004-09] 9.2.3.24.1
Case enum_tp_dcs.defaultalphabet
TMPTP_UD = MID (TP_UD, I * 133 * 2 1, 133 * 2)
End SELECT
Result (i) = sc_number
Result (i) = firstock ()
Result (i) = format (tp_mr, "x2")
'Next Segement TP_MR Must Be Increas
'TP_MR = 1
Result (i) = TP_DA
Result (i) = format (TP_PID, "X2")
Result (i) = format (tp_dcs, "x2")
Result (i) = format (tp_vp, "x2")
IF tp_dcs = enum_tp_dcs.us2 then
TP_UDL = TMPTP_UD.LENGTH / 2 6 1 '6: IE
END IF
IF tp_dcs = enum_tp_dcs.defaultalphabet then
TP_UDL = FIX ((TMPTP_UD.LENGTH 7 * 2) * 4/7) '6: Length of IE
'###############################################
'' Still Problem Here:
'' When The Charcter IS Several Times of 7 of the last message, tp_udl will not correct!
'' to Correct this Problem I Write Some Code Below. That's May Not Perfect Solution.
'###############################################
'If i = TotalMessages and ((TMPTP_UD.LENGTH MOD 14) = 0) Then' tp_udl - = 1
'End if
END IF
Result (i) = Format (TP_UDL, "X2")
Result (i) = "060804" 'TP_UDHL AND SOME OF Concatenated Message
Result (i) = Format (Reference, "X4")
Result (i) = Format (TotalMessages 1, "X2")
Result (i) = Format (i 1, "x2")
Result (i) = TMPTP_UD
NEXT
Return RESULT
END FUNCTION
Public overrides function firstector () AS STRING
Return ByTohex (TP_MTI TP_VPF TP_SRR TP_UDHI)
END FUNCTION
END CLASS
End Namespace
End Namespace