'==================================================== ========= 'SMS Decoder' 2004-9-24'1.description 'this class decode a SMS or EMS PDU code to a ceertain'class. You can use it in your software to read smss AND 'EMSS. All of this is done undergsm 03.40. I Tested it'on my Siemens M55 and nokia 8xxx and it works well.'2.useage' if you know what type of pdu code, you can create a 'new instance of class like DIM s as SMS (myPDUCode) 'When instance is created, you read its public variable' to get what you want. 'When TP_DCS = 0, PDU code is coded from 7bit' charactor (see GSM 03.38), use shared function 'Deocde7Bit to decode it.' When TP_DCS = 8, PDU code is coded from Unicode'charactor (see GSM 03.38), use shared funtion'DecodeUnicode to decode it.'3.Bugs' So far in my tests I found none. '4.when you us it' you can freeely use it ing. t i 一 h 的 t................................. Ks'5.about me 'I am Writting a Program Which Can List folders and' Files in Siemens M55 Mobile Phone. It Can Also Read'and Send SMS, EMS. Some Documents Are Hard To Find on'Internet, But I Keep ON My Mind to Study IT and Finally'i Found IT IS Full of Interests. 'I Like Freedom, So'i Exchange My Ideas with All of'
The World. It is so happy thing you can use my classs! 'in the end, sorry for my poor English.'6.contact me' email: hesicong@mail.sc.cninfo.net 'QQ: 38288890' MSN: Hesicong @mail.sc.cninfo.net 'homepage: http://wwsicong2004.vip.myrice.com (Chinese)' Thanks for Using It! '---- By Hesicong'Last Edited 2004-9-24''Revision: '2004-10-29:' Fix bug in decode "@" Charactor. 'Add Functions in "Decode 7 Bit Code to English" RegionImports System.Text
Public MustInherit Class SMS 'Note all of following various with TP_ can be found in GSM 03.40 Public SCAddressLength As Byte' Service Center Address length Public SCAddressType As Byte 'Service Center Type [See GSM 03.40] Public SCAddressValue As String' Service Center nuber Public FirstOctet AS BYTE 'See GSM 03.40
Public tp_pid as byte public tp_dcs as byte public tp_udl as byte public tp_ud as string public text as string public type as smstype public userdata as String
Public enum smstype sms_received = 0 SMS_STATUS_REPORT = 2 SMS_SUBMIT = 1 EMS_RECEIVED = 64 'IT IS "reserved" on my phone ?? EMS_SUBMIT = 65 END ENUM
Public Mustoverride Sub getorignaldata (Byval Pducode As String)
'Get a byte from PDU String Shared function getByte (byref pducode as string) AS BYTE DIM R AS BYTE = VAL ("& H" MID (Pducode, 1, 2)) PDUCODE = MID (PDucode, 3) Return R End Function
'Get a string of certain length Shared Function GetString (ByRef PDUCode As String, ByVal Length As Integer) As String Dim r As String = Mid (PDUCode, 1, Length) PDUCode = Mid (PDUCode, Length 1) Return r End Function 'Get Date from SCTS Format Shared Function Getdate (Byref SCTS AS String) AS Date Dim Year, Month, Day, Hour, Minute, Second, Timezone As INTEGER
Year = VAL (SWAP (GetString (SCTS, 2))) 2000 MONTH = VAL (SWAP (GetString (SCTS, 2))) DAY = VAL (Swap (GetString (SCTS, 2))) Hour = VAL (SWAP GetString (SCTS, 2))) MINUTE = VAL (SWAP (GetString (SCTS, 2))) Second = Val (SWAP (GetString (SCTS, 2))) Timezone = Val (SWAP (GetString (SCTS, 2))))
Dim Result As New Date (Year, Month, Day, Hour, Minute, Second) Return Result End Function
'Swap Two Bit Shared Function Swap (Byref Twobitstr As String) AS String 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
'Get phone address Shared Function GetAddress (ByRef Address As String) As String Dim tmpChar As Char () = Address.ToCharArray Dim i As Integer, result As String For i = 0 To tmpChar.GetUpperBound (0) Step 2 result = Swap (TMPCHAR (I) TMPCHAR (I 1)) Next if INSTR (Result, "F") Then Result = MID (Result, 1, Result.Length - 1) Return Result End Function
Shared Function GetSMSType (ByVal PDUCode As String) As SMS.SMSType 'Get first october Dim FirstOctet As Byte Dim L As Integer = SMS.GetByte (PDUCode) SMS.GetByte (PDUCode) SMS.GetString (PDUCode, (L - 1) * 2) FirstOCTET = SMS.GETBYTE (PDucode) '[Chinese] gets the signature' [Chinese] gets the basic code last two bit whether there is header as a tag 'get base code. Use Last 2 Bit and WHether There's a header as Remark Dim t1 As Integer = FirstOctet And 3 '00000011 Dim t2 As Integer = FirstOctet And 64' 01000000 '[Chinese] special processing If t1 = 3 And t2 = 64 Then Return SMS.SMSType.EMS_SUBMIT Return t1 t2 End Function'Deoce a unicode string Shared Function DecodeUnicode (ByVal strUnicode As String) As String Dim Code As String Dim i, j As Integer Dim c () As String 'temp ReDim c (strUnicode.Length / 4)' 2 Byte a Unicode char
For J = 0 to strunicode.Length / 4 - 1 DIM D () As char = Strunicode.ToChararray (j * 4, 4) c (j) = "& H" & ctype (d, string) c (j) = chrw (VAL (c (j)))) Code = c (j) Next Return Code End Function
#Region " 'Deocde 7Bit to english"' 2004-10-29: Region added 'Fixed decode "@" charactor' I use a new method, it is easily to understand but look much longer than before Shared Function InvertHexString (ByVal HexString. As string 'for example:' 123456 '===>' 563412 Dim Result AS New StringBuilder Dim i as integer for i = hexstring.LENGTH - 2 TO 0 Step -2 Result.Append (HexString.Substring (i, 2)) Next Return Result.ToString End FunctionShared Function ByteToBinary (ByVal Dec As Byte) As String Dim Result As String Dim Temp As Byte = Dec Do Result = (Temp Mod 2) & Result If Temp = 1 Or Temp = 0 Then Exit Do Temp = Temp / 2 loop result = result.padleft (8, "0") RETURN RESULT END FUNCTION
Shared function binarytoint (byval binary as string) AS INTEGER DIM RESULT AS INTEGER DIM I AS INTEGER for i = 0 to binary.length - 1 results = result val (binary.substring (binary.length - i - 1, 1)) * 2 ^ i Next Return Result End Function
Shared Function Decode7Bit (ByVal str7BitCode As String) As String Dim Inv7BitCode As String = InvertHexString (str7BitCode) Dim Binary As String Dim Result As String Dim i As Integer For i = 0 To Inv7BitCode.Length - 1 Step 2 Binary = ByteToBinary (Val ("& H" & inv7bitcode.substring (i, 2))) Next Dim Temp as integer for i = 1 to binary.length / 7 TEMP = binarytoint (binary.tion (binary.length - i * 7, 7))) There is a problem: '"@" Charactor is decoded to binary "0000000", but it it it is a bug! Ifbe it is a bug! If Temp = 0 TEMP = 64 Result = Result ChrW (Temp) Next Return (Result) End Function # End RegionEnd ClassPublic Class SMS_RECEIVED Inherits SMS Public SrcAddressLength As Byte Public SrcAddressType As Byte Public SrcAddressValue As String Public TP_SCTS As Date
Sub New (ByVal PDUCode As String) Type = SMS.SMSType.SMS_RECEIVED GetOrignalData (PDUCode) End Sub Public Overrides Sub GetOrignalData (ByVal PDUCode As String) SCAddressLength = GetByte (PDUCode) SCAddressType = GetByte (PDUCode) SCAddressValue = GetAddress ((GetString ( Pducode, (ScaddressLength - 1) * 2))))))))))))))
SrcAddressLength = GetByte (PDUCode) SrcAddressType = GetByte (PDUCode) SrcAddressLength = SrcAddressLength Mod 2 SrcAddressValue = GetAddress ((GetString (PDUCode, SrcAddressLength)))
TP_PID = GetByte (PDUCode) TP_DCS = GetByte (PDUCode) TP_SCTS = GetDate (GetString (PDUCode, 14)) TP_UDL = GetByte (PDUCode) TP_UD = GetString (PDUCode, TP_UDL * 2) End SubEnd ClassPublic Class SMS_SUBMIT Inherits SMS Public TP_MR As Byte public DesAddressLength As Byte public DesAddressType As Byte public DesAddressValue As String public TP_VP As Byte Sub New (ByVal PDUCode As String) Type = SMS.SMSType.SMS_SUBMIT GetOrignalData (PDUCode) End Sub
Public Overrides Sub GetOrignalData (ByVal PDUCode As String) SCAddressLength = GetByte (PDUCode) SCAddressType = GetByte (PDUCode) SCAddressValue = GetAddress ((GetString (PDUCode, (SCAddressLength - 1) * 2))) FirstOctet = GetByte (PDUCode)
TP_MR = GetByte (PDucode)
DesaddressLength = getByte (pducode) desaddresstype = getByte (pducode) DesaddressLength = desaddressLength Mod2 DesaddressValue = GetDress
TP_PID = GetByte (PDucode) TP_DCS = GetByte (PDucode) TP_VP = GetByte (PDucode) TP_UDL = GetByte (PDucode) TP_UD = GetString (Pducode, TP_UDL * 2) End Subend Class
Public Class EMS_RECEIVED Inherits SMS_RECEIVED Public Structure InfoElem 'See document "How to create EMS" Public Identifier As Byte Public Length As Byte Public Data As String End Structure Public TP_UDHL As Byte
Public IE () AS InfoElem
Sub New (ByVal PDUCode As String) MyBase.New (PDUCode) End Sub Public Overrides Sub GetOrignalData (ByVal PDUCode As String) SCAddressLength = GetByte (PDUCode) SCAddressType = GetByte (PDUCode) SCAddressValue = GetAddress (GetString (PDUCode, (SCAddressLength - 1 ) * 2)) FirstOctet = GetByte (PDUCode) SrcAddressLength = GetByte (PDUCode) SrcAddressType = GetByte (PDUCode) SrcAddressLength = SrcAddressLength Mod 2 SrcAddressValue = GetAddress ((GetString (PDUCode, SrcAddressLength)))
TP_PID = GetByte (PDucode) TP_DCS = GetByte (PDucode) TP_SCTS = Getdate (GetString (PDucode, 14)) TP_UDL = GetByte (PDucode) TP_UDHL = GetByte (Pducode)
IE = Getie (getString (pducode, tp_udhl * 2))
TP_UD = GetString (Pducode, TP_UDL) End Sub
'Get Informat Elements Shared Function GetIE (ByVal IECode As String) As InfoElem () Dim tmp As String = IECode, t As Integer = 0 Dim result () As InfoElem Do Until IECode = "" ReDim Preserve result (t) With result ( T) .identifier = getByte (IECode) .length = getByte (IECode) .data = getString (IECode, .length * 2) end with T = 1 Loop Return Result End Function Class Class Class RESURN RESULT END FUNCTIONEND CLASS
Public class ems_submit inherits SMS_SUBMIT
Sub New (Byval Pducode As String) MyBase.new (PDucode) TYPE = SMS.SMSTYPE.EMS_SUBMIT END SUB
Public TP_UDHL AS BYTE
Public IE () AS EMS_RECEIVED.INFOELEM
Public Overrides Sub GetOrignalData (ByVal PDUCode As String) SCAddressLength = GetByte (PDUCode) SCAddressType = GetByte (PDUCode) SCAddressValue = GetAddress (GetString (PDUCode, (SCAddressLength - 1) * 2)) FirstOctet = GetByte (PDUCode) TP_MR = GetByte (PDUCode )
DesaddressLength = getByte (pducode) desaddresStype = getByte (pducode) DesaddressLength = desaddresslength mod 2 desaddressValue = GetDress (pducode, desaddresslength)
TP_PID = GetByte (PDucode) TP_DCS = GetByte (PDucode) TP_VP = GetByte (PDucode) TP_UDL = GetByte (PDucode)
TP_UDHL = GetByte (PDucode) IE = EMS_Received.Getie (GetString (pducode, tp_udhl * 2))
TP_UD = GetString (PDucode, TP_UDL * 2) End Subend Class
Public Class SMS_STATUS_REPORT Inherits SMS_RECEIVED Public TP_MR As Byte Public TP_DP As Date Public Status As Byte Sub New (ByVal PDUCode As String) MyBase.New (PDUCode) Type = SMS.SMSType.SMS_STATUS_REPORT End Sub Public Overrides Sub GetOrignalData (ByVal PDUCode As String) SCADDRESSLENGTH = GetByte (pducode) scaddresstype = getByte (pducode) scaddressValue = GetDress (pducode, (ScaddressLength - 1) * 2))))
FirstOCTET = GetByte (PDucode)
TP_MR = GetByte (PDucode)
SrcAddressLength = GetByte (PDUCode) SrcAddressType = GetByte (PDUCode) SrcAddressLength = SrcAddressLength Mod 2 SrcAddressValue = GetAddress (GetString (PDUCode, SrcAddressLength))
TP_SCTS = Getdate (PDucode, 14)) TP_DP = Getdate (GetString (PDucode, 14))
Status = getByte (PDucode)