SMS, EMS PDU Code Decoder SMS PDU Format Decoder

xiaoxiao2021-03-06  57

'==================================================== =========

'SMS,

EMS

Decoder

'2005-2-20

'1.description

'This class decode a SMS or EMS PDU Code to a Certain

'Class. You can use it in your software to read smss and

'EMSS. All of this is done undergam 03.40. I TESTED IT

'On My Siemens M55 and Nokia 8xxx and It Works 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)

'Hen 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 use it

'You Can Freely Use it or modify it in your program,

'But when you find bus or improved it please publish it

'or send one copy to me. Thanks

'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 ALL OF

'The world. it is so happy thing you can use my class!' in the end, sorry for my poor english.

'6.Contact ME

'Email: hesicong@mail.sc.cninfo.net

'QQ: 38288890

'HomePage: http://dream-world.nese.net (Chinese)

'Thanks for Using IT!

'---- By Hesicong

'

'Revision:

'2004-10-29:

'Fix bug in decode "@" Charactor.

'Add Functions in "Decode 7 Bit Code TO ENGLISH" Region

'2005-2-20:

'Final Version Released. Fixed Minor Bugs.

Imports System.Text

Namespace SMS

Namespace Decoder

Public Mustinherit Class Smsbase

'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 ceertling

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 smsbase.smstype

'GET First October

DIM FIRSTOCTAT AS BYTE

DIM L As Integer = SMSBase.GetByte (PDucode)

SMSBase.GetByte (PDucode)

SMSBase.getstring (Pducode, (L - 1) * 2)

FirstOCTET = SMSBASE.GETBYTE (PDucode)

'[Chinese] acquisition signature

'[Chinese] gets the basic code last two Bit have a 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 '0000000011dim T2 as integer = firstoctet and 64' 01000000

'[Chinese] special treatment

IF T1 = 3 and T2 = 64 Ten Return SMSBase.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) 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 FUNCTION

Shared 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

Result = Result Val (binary.substring (binary.length - i - 1, 1)) * 2 ^ inext

Return RESULT

END FUNCTION

Shared function decode7bit (byval str7bitcode as string, byval charcount as integer) 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 charcount

Temp = binarytoint (binary.substring (binary.length - i * 7, 7))

'There is a problem:

'"@" charactor is decoded to binary "0000000", But ITS ASCII Code IS 64 !!

'Don't know what to do with it, maybe it is a bug!

IF TEMP = 0 THEN TEMP = 64

Result = Result Chrw (TEMP)

NEXT

Return (Result)

END FUNCTION

#End region

END CLASS

Public Class SMS_Received

Inherits SMSBase

Public SrcaddressLength as Byte

Public srcaddressType as Byte

Public SrcaddressValue as string

Public TP_SCTS AS Date

Sub New (Byval Pducode As String)

TYPE = SMSBASE.SMSTYPE.SMS_RECEIVED

GetorignalData (PDucode)

End Sub

Public overrides sub getorignaldata (Byval Pducode As String)

ScaddressLength = GetByte (PDucode)

SCADDRESSTYPE = GetByte (PDucode)

ScaddressValue = Getdress ((GetString (pducode, (ScaddressLength - 1) * 2))))))))))

FirstOCTET = GetByte (PDucode)

SRCADDRESSLENGTH = GetByte (PDucode)

SRCADDRESSTYPE = GetByte (pducode)

SRCADDRESSLENGTH = SRCADDRESSLENGTH MOD 2

SrcaddressValue = GetDress ((GetString (pducode, srcaddressLength))))

TP_PID = GetByte (PDucode)

TP_DCS = GetByte (PDucode)

TP_SCTS = GetDate (pducode, 14)) TP_UDL = GetByte (PDucode)

TP_UD = GetString (Pducode, TP_UDL * 2)

End Sub

END CLASS

Public Class SMS_SUBMIT

Inherits SMSBase

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 = SMSBASE.SMSTYPE.SMS_SUBMIT

GetorignalData (PDucode)

End Sub

Public overrides sub getorignaldata (Byval Pducode As String)

ScaddressLength = GetByte (PDucode)

SCADDRESSTYPE = GetByte (PDucode)

ScaddressValue = Getdress ((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_UD = GetString (Pducode, TP_UDL * 2)

End Sub

END 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 = GetDress (pducode, (ScaddressLength - 1) * 2)))

FirstOCTET = GetByte (PDucode)

SRCADDRESSLENGTH = GetByte (PDucode)

SRCADDRESSTYPE = GetByte (PDucode) srcaddressLength = SRCADDRESSLENGTH MOD 2

SrcaddressValue = GetDress ((GetString (pducode, srcaddressLength))))

TP_PID = GetByte (PDucode)

TP_DCS = GetByte (PDucode)

TP_SCTS = Getdate (Pducode, 14))

TP_UDL = GetByte (PDucode)

TP_UDHL = GetByte (PDucode)

IE = Getie (getString (pducode, tp_udhl * 2))

TP_UD = GetString (Pducode, TP_UDL * 2)

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

END CLASS

Public Class EMS_SUBMIT

Inherits SMS_SUBMIT

Sub New (Byval Pducode As String)

Mybase.new (pducode)

TYPE = SMSBASE.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 = GetDress (pducode, (ScaddressLength - 1) * 2)))

FirstOCTET = GetByte (PDucode)

TP_MR = GetByte (PDucode)

DesaddressLength = GetByte (PDucode)

DesaddressType = getByte (pducode)

DesaddressLength = desaddressLength Mod 2

DesaddressValue = GetDress (GetString (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 (pducode, tp_udhl * 2)) TP_UD = GetString (Pducode, TP_UDL * 2)

End Sub

END CLASS

Public Class SMS_STATUS_REPORT

Inherits SMS_Received

Public TP_MR AS BYTE

Public TP_DP As Date

Public Status as enumstatus

Public Enum Enumstatus

SUCCESS = 0

Notsend = 96

Noresponsefromsme = 98

END ENUM

Sub New (Byval Pducode As String)

Mybase.new (pducode)

TYPE = SMSBASE.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 = Getdress (GetString (pducode, srcaddressLength)

TP_SCTS = Getdate (Pducode, 14))

TP_DP = Getdate (pducode, 14))

Status = getByte (PDucode)

'Status Report Do Not Have Content So I Set It A Zero Length String

TP_UD = ""

End Sub

END CLASS

Public Class Pdudecoder

Public structure baseinfo

Public SourceNumber As String

Public DestinationNumber As String

Public ReceivedDate as date

Public text as string

Public Type as sms.decoder.smsbase.smstype

Public Emstotolpiece As Integer

Public EMSCURRENTPIECE AS INTEGER

Public StatusFromReport As SMS_STATUS_REPORT.ENUMSTATUS

Public DestinationReceiveddate

End structure

Sub New (Byval Pducode As String)

Decode (pducode)

End Sub

Public Shared Function Decode (Byval Pducode As String) AS BaseInfo

Dim Result As Baseinfo

Try

DIM S as Object

DIM T as smsbase.smstype = smsbase.getsmstype (pducode) Result.type = t

SELECT CASE T

Case smsbase.smstype.ems_received

S = new EMS_RECEIVED (PDucode)

Result.Sourcenumber = S.SRCADDRESSVALUE

If s.srcaddresstype = & h91 Then Result.SourceNumber = " " Result.SourCenumber

Result.ReceiveDDate = S.TP_SCTS

IF not (S.IE is nothing) THEN

Dim data as string = S.IE (0) .data

Result.emstotolpiece = CINT (MID (DATA, 5, 2))

Result.emscurrentpiece = CINT (MID (Data, 7, 2)))

END IF

Case smsbase.smstype.sms_received

s = new SMS_Received (PDucode)

Result.Sourcenumber = S.SRCADDRESSVALUE

If s.srcaddresstype = & h91 Then Result.SourceNumber = " " Result.SourCenumber

Result.ReceiveDDate = S.TP_SCTS

Case SMSBase.SMSTYPE.EMS_SUBMIT

s = new EMS_SUBMIT (PDucode)

Result.destinationNumber = s.deesaddressValue

If s.deesaddresstype = & h91 Ten Result.destinationNumber = " " Result.DestinationNumber

IF not (S.IE is nothing) THEN

Dim data as string = S.IE (0) .data

Result.emstotolpiece = CINT (MID (DATA, 5, 2))

Result.emscurrentpiece = CINT (MID (Data, 7, 2)))

END IF

Case SMSBase.SMSTYPE.SMS_SUBMIT

s = new SMS_SUBMIT (PDucode)

Result.destinationNumber = s.deesaddressValue

If s.deesaddresstype = & h91 Ten Result.destinationNumber = " " Result.DestinationNumber

Case SMSBase.SMSTYPE.SMS_STATUS_REPORT

s = new SMS_STATUS_REPORT (PDucode)

Result.Sourcenumber = S.SRCADDRESSVALUE

If s.srcaddresstype = & h91 Then Result.SourceNumber = " " Result.SourCenumber

Result.ReceiveDDate = S.TP_SCTS

Result.DestinationReceivedDate = S.TP_DP () Result.statusFromReport = S.Status

Case Else

STOP

End SELECT

'####################

'Correct When S is SMS Type, No TP_UDL IS FOUND.

'NOTE: Only Ems Has The TP_UDHL AND TP_UDH SEE 3GPP TS 23.040 V6.5.0 (2004-09)

'####################

If S.TP_DCS = 0 THEN

IF T = SMSBase.SMSTYPE.SMS_RECEIVED OR T = SMSBASE.SMSTYPE.SMS_STATUS_REPORT OR T = SMSBASE.SMSTYPE.SMS_SUBMIT THEN

'##############################

'add a parameter

'#####################

Result.text = s.Decode7bit (S.tp_UD, S.TP_UDL)

END IF

IF T = SMSBASE.SMSTYPE.EMS_RECEIVED OR T = SMSBASE.SMSTYPE.EMS_SUBMIT THEN

Result.text = s.Decode7bit (S.TP_UD, S.TP_UDL - 8 * (1 S.TP_UDHL) / 7)

END IF

Else

Result.text = S.DecodeUnicode (S.tp_UD)

END IF

Catch Err as Exception

Result.text = "PDU decoding error:" & pducode

END TRY

Return RESULT

END FUNCTION

END CLASS

End Namespace

End Namespace

转载请注明原文地址:https://www.9cbs.com/read-113526.html

New Post(0)