Click here to Skip to main content
15,905,782 members
Articles / Programming Languages / C#

Library for Decode/Encode SMS PDU

Rate me:
Please Sign up or sign in to vote.
4.74/5 (49 votes)
18 Oct 2012CPOL 762.1K   26.6K   128   192
Library for decoding and encoding mobile short messages to/from SMS protocol data unit

Last Updates

You can find some community fixes mentioned on comments section on Project Home at Google Code[^].

Introduction

The smspdulib library is intended for more transparent interaction with SMS PDU. This thought over hierarchy of classes will allow you to create new classes for other types of short messages using the realized base functionality.

At present, the following types of short messages are supported: status report, SMS (with long (large) SMS parts support).

Hierarchy of Classes

Classes hierarchy

Enums

Sample Code

This example shows you how to decode PDU.

C#
SMSType smsType = SMSBase.GetSMSType(pduSource);
switch (smsType) {
    case SMSType.SMS:
        SMS sms = new SMS();
        SMS.Fetch(sms, ref pduSource);
        //Use instance of SMS class here
        break;
    case SMSType.StatusReport:
        SMSStatusReport statusReport = new SMSStatusReport();
        SMSStatusReport.Fetch(statusReport, ref pduSource);
        //Use instance of SMSStatusReport class here
        break;
}

This example show you how to encode SMS to PDU.

C#
SMS sms = new SMS();
sms.Direction = SMSDirection.Submited;
sms.PhoneNumber = "+74951234567";
sms.ValidityPeriod = new TimeSpan(4, 0, 0, 0);
sms.Message = "Me message here!";
string pduSource = sms.Compose(SMS.SMSEncoding.UCS2);

Useful Links

Updated

  • Updated Decode7bit method: Fixed some bugs (see comments)
  • PopDate method now doesn't recognize time zone because some phones send strange stuff

Please help me find more detailed specification of PDU format and TP-SCTS format.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Longer messages Pin
Treca3-Mar-07 6:19
Treca3-Mar-07 6:19 
GeneralRe: Longer messages Pin
Eduard Gomolyako3-Mar-07 9:23
Eduard Gomolyako3-Mar-07 9:23 
GeneralRe: Longer messages Pin
Treca3-Mar-07 11:23
Treca3-Mar-07 11:23 
GeneralRe: Longer messages Pin
Eduard Gomolyako3-Mar-07 12:25
Eduard Gomolyako3-Mar-07 12:25 
GeneralRe: Longer messages Pin
zaven12-Mar-07 8:32
zaven12-Mar-07 8:32 
GeneralBUG!!!!! can u answer it really helps me... Pin
Umairarain14-Feb-07 2:21
Umairarain14-Feb-07 2:21 
GeneralRe: BUG!!!!! can u answer it really helps me... Pin
Eduard Gomolyako19-Feb-07 5:07
Eduard Gomolyako19-Feb-07 5:07 
GeneralBUG: 7bit Long SMS Pin
Ladislav Soukup16-Jan-07 3:39
Ladislav Soukup16-Jan-07 3:39 
Hello,

You have one bug (?) in decoding 7bit multi part messages.

PDU:
07 91 246030500200 64 0C 91 247013015183 00 00 70106151516440 A0 050003140301886FB1BC0...

What is wrong?
Header parsing is OK...
So we have only this left:

050003140301886FB1BC0C9287DD6F96FA0DD2AFEBF3B2FB3...

This is message WITH part id and part number (050003140301). You will correctly parse this informations out and you will strip this part. AND THIS IS THE PROBLEM. You can't do that for 7bit encoding. If You do this, You won't be able to decode the text.

public static void Fetch(SMS sms, ref string source)<br />
        {<br />
            string source_7bit = "";<br />
<br />
            SMSBase.Fetch(sms, ref source);<br />
<br />
            if (sms._direction == SMSDirection.Submited)<br />
                sms._messageReference = PopByte(ref source);<br />
<br />
            sms._phoneNumber = PopPhoneNumber(ref source);<br />
            sms._protocolIdentifier = PopByte(ref source);<br />
            sms._dataCodingScheme = PopByte(ref source);<br />
<br />
            if (sms._direction == SMSDirection.Submited)<br />
                sms._validityPeriod = PopByte(ref source);<br />
<br />
            if (sms._direction == SMSDirection.Received)<br />
                sms._serviceCenterTimeStamp = PopDate(ref source);<br />
<br />
            sms._userData = source;<br />
<br />
            if (source == string.Empty)<br />
                return;<br />
<br />
            int userDataLength = PopByte(ref source);<br />
<br />
            if (userDataLength == 0)<br />
                return;<br />
<br />
            if (sms._userDataStartsWithHeader)<br />
            {<br />
                source_7bit = source;<br />
                byte userDataHeaderLength = PopByte(ref source);<br />
<br />
                sms._userDataHeader = PopBytes(ref source, userDataHeaderLength);<br />
<br />
                userDataLength -= userDataHeaderLength + 1;<br />
            }<br />
<br />
            if (userDataLength == 0)<br />
                return;<br />
<br />
            switch ((SMSEncoding)sms._dataCodingScheme & SMSEncoding.ReservedMask)<br />
            {<br />
                case SMSEncoding._7bit:<br />
                    if (source_7bit == "")<br />
                    {<br />
                        sms._message = Decode7bit(source, userDataLength);<br />
                    }<br />
                    else<br />
                    {<br />
                        string tmp_string = Decode7bit(source_7bit, userDataLength + 6);<br />
                        sms._message = tmp_string.Substring(7);<br />
                    }<br />
                    break;<br />
                case SMSEncoding._8bit:<br />
                    sms._message = Decode8bit(source, userDataLength);<br />
                    break;<br />
                case SMSEncoding.UCS2:<br />
                    sms._message = DecodeUCS2(source, userDataLength);<br />
                    break;<br />
            }<br />
        }


What I do... When Multi part sms is detected, i store pdu source before partId and partNumber parsing (string source_7bit). Now when 7bit decoding is called, I "hacked" the input params, so I can send my "source_7bit" string and i added 6 to userDataLength (not yet fully tested, but this will prevent lost of last few chars). When data is parsed, I just strip first 7 chars (part id and part number).

This is just quick fix, so there can be problems and this can be fixed more complex. But helped us in parsing multi part sms with 7bit encoding.

Please try to look at this

Ladislav Soukup
AnswerRe: BUG: 7bit Long SMS Pin
Eduard Gomolyako19-Feb-07 5:10
Eduard Gomolyako19-Feb-07 5:10 
GeneralUsing a Webservice Pin
xaml.net13-Jan-07 2:04
xaml.net13-Jan-07 2:04 
GeneralWhats wrong with this message Pin
nmrdk15-Dec-06 8:44
nmrdk15-Dec-06 8:44 
GeneralRe: Whats wrong with this message Pin
Eduard Gomolyako15-Dec-06 9:05
Eduard Gomolyako15-Dec-06 9:05 
GeneralRe: Whats wrong with this message Pin
frada20-Dec-06 21:32
frada20-Dec-06 21:32 
GeneralExtremely beautiful! Pin
eliran15-Nov-06 8:04
eliran15-Nov-06 8:04 
Questioncan we use this code to compose flash sms and vcard? Pin
Cman-Lobo28-Oct-06 4:02
Cman-Lobo28-Oct-06 4:02 
AnswerRe: can we use this code to compose flash sms and vcard? Pin
Eduard Gomolyako28-Oct-06 10:41
Eduard Gomolyako28-Oct-06 10:41 
AnswerRe: can we use this code to compose flash sms and vcard? Pin
Eduard Gomolyako28-Oct-06 10:46
Eduard Gomolyako28-Oct-06 10:46 
QuestionWhich SMS server are you using? Pin
Rajib Bahar23-Oct-06 3:11
Rajib Bahar23-Oct-06 3:11 
AnswerRe: Which SMS server are you using? Pin
Eduard Gomolyako23-Oct-06 3:23
Eduard Gomolyako23-Oct-06 3:23 
GeneralRe: Which SMS server are you using? Pin
Rajib Bahar23-Oct-06 5:06
Rajib Bahar23-Oct-06 5:06 
GeneralRe: Which SMS server are you using? Pin
Eduard Gomolyako23-Oct-06 5:13
Eduard Gomolyako23-Oct-06 5:13 
GeneralRe: Which SMS server are you using? Pin
Rajib Bahar23-Oct-06 5:15
Rajib Bahar23-Oct-06 5:15 
AnswerRe: Which SMS server are you using? Pin
Eduard Gomolyako23-Oct-06 5:30
Eduard Gomolyako23-Oct-06 5:30 
GeneralThanks! Pin
Ladislav Soukup17-Oct-06 6:48
Ladislav Soukup17-Oct-06 6:48 
GeneralRe: Thanks! Pin
Eduard Gomolyako19-Oct-06 5:25
Eduard Gomolyako19-Oct-06 5:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.