Click here to Skip to main content
Licence CPOL
First Posted 26 Nov 2006
Views 57,365
Downloads 314
Bookmarked 26 times

PDU lib under STL for SMS

By | 21 May 2007 | Article
This is a light weight lib for decoding and encoding the GSM SMS PDU format.

Introduction

The SMS message, as specified by the Etsi organization (documents GSM 03.40 and GSM 03.38), can be up to 160 characters long, where each character is 7 bits according to the 7-bit default alphabet. Eight-bit messages (max 140 characters) are usually not viewable by the phones as text messages; instead, they are used for data in, e.g., smart messaging (images and ringing tones) and OTA provisioning of WAP settings. 16-bit messages (max 70 characters) are used for Unicode (UCS2) text messages, viewable by most phones. A 16-bit text message of class 0 will, on some phones, appear as a Flash SMS (a.k.a. blinking SMS or alert SMS).

Please see dreamfabric for details.

This lib is used to encode/decode plain text to PDU format under 7-bit or UCS2 (yes, the EMS is not supported yet). This lib is easy to use, but not bug free, so please be patient :).

I have searched the internet, and there are some great libs for the PDU format under the .NET Framework which I can't use in my current job, so I wrote this lib for my own needs.

Source code

The source code is very simple, only two files; you can add them to your project or build a lib.

How to use...

There are two main function for the lib: Compose() and Fetch(). Using these two functions, you can compose a message or fetch data form a PDU string.

/**
 *    Compose a shot message
 *    @para pdudata pdu: [out] The pdu result
 *    @para string msg:    data need to be sent
 *    @para string phone:    the destination phone number
 *    @para string msc:    message center number, if null, use the preset number
 *    @para EEncodeMethod eMethod:    the encode method, default is UCS2
 *
 *    @return the data length without the SMSC part[you need it in AT+CMGS command]
 */
int Compose (pdudata& pdu, 
    std::tstring msg, std::tstring phone,
    std::tstring msc = _T(""),
    EEncodeMethod eMethod = EEncodeUCS2) throw(...);


/**
 *    Fetch data form a pdu format data
 *    only return the message part(USERDATA), 
      use other member function to fetch information
 *    @para pdudata pdu: the source data
 *
 *    @return The nomral message
 */
std::tstring Fetch(pdudata pdu) throw(...);

Compose an SMS message

To compose a message, you need do this:

CPDU pdu;
LPCTSTR szMsg = m_szEncodeContents.GetBuffer(m_szEncodeContents.GetLength());
pdu.SetMSCNumber(_T("13800210500"));    // Set the message center number here
try {

    CPDU::pdudata strPDU;
    // Compose the message
    int iLen = pdu.Compose(strPDU, szMsg, _T("13813878775"));
    WTL::CString csPDU;

    
    // Show the message to the output edit control
#ifdef _UNICODE
    MultiByteToWideChar(
        CP_ACP, 
        MB_PRECOMPOSED, 
        strPDU.c_str(), 
        -1,
        csPDU.GetBuffer(1024),
        1024
        );
#else
    csPDU = strPDU.c_str();
#endif // _UNICODE

    m_szDecodeContents = csPDU;
} catch (CPDUException e) {
    m_szDecodeContents = e.GetMessage().c_str();
}

Fetch data

When you need to fetch a message, you need do this:

CPDU pdu;
WTL::CString szMessage = pdu.Fetch(pdustring);

If there are no exceptions, then you can use these functions to fetch other data:

/**
 *    Fetch Data
 */
// The caller/sender number
std::tstring GetCaller()        { return m_szCaller; }
// The callee/recevier number
std::tstring GetCallee()        { return m_szCallee; }
// Message time stamp
std::tstring GetTimeStamp()        { return m_szTimeStamp; }

Notice

The tstl.h file is a TCHAR style header file for STL strings and streams.

Update

  • May 22 2007: Fixed a bug in the Decode7bit function, thanks to alexafros12345 :)

License

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

About the Author

Louis huang



China China

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalwhere i can find stl/stl.h header file Pinmemberkrshna.padave0:41 17 Apr '09  
GeneralRe: where i can find stl/stl.h header file PinmemberYoungho Kim23:24 29 Jan '10  
Generalsource code for PDU to Text Converter in java Pinmembersnehalpatil2718:45 1 Mar '09  
GeneralA bug about offset in parsing reply number in Fetch() PinmemberAllen.W18:36 5 Nov '07  
QuestionRe: A bug about offset in parsing reply number in Fetch() Pinmemberafriza22:39 4 Sep '08  
GeneralCPDU::Encode7bit Fix [modified] Pinmemberwaelahmed23:08 9 Oct '07  
GeneralRe: CPDU::Encode7bit Fix Pinmemberpatiqs23:41 11 Sep '08  
QuestionPossible to integrate with C# Pinmemberxpthinker7:07 24 Jul '07  
Questionhow to send picture in SMS? PinmemberShakeel Mumtaz5:46 10 Jun '07  
GeneralCPDU::Decode7bit - bug fix Pinmemberalexafros1234510:38 17 May '07  
GeneralRe: CPDU::Decode7bit - bug fix PinmemberLouis huang17:11 21 May '07  
GeneralNot working for me Pinmemberempty133713:58 29 Apr '07  
GeneralRe: Not working for me PinmemberLouis huang16:47 29 Apr '07  
GeneralRe: Not working for me Pinmemberempty133719:56 29 Apr '07  
GeneralRe: Not working for me PinmemberLouis huang23:10 29 Apr '07  
GeneralUse a webservice instead Pinmemberxaml.net2:30 13 Jan '07  
GeneralCopy from another site Pinmember_johan3:26 6 Dec '06  
GeneralRe: Copy from another site PinmemberLouis huang14:27 6 Dec '06  
QuestionQuestion about another method.... Pinmemberprcarp2:26 27 Nov '06  
AnswerRe: Question about another method.... PinmemberLouis huang2:40 27 Nov '06  
GeneralRe: Question about another method.... Pinmemberprcarp3:45 27 Nov '06  
GeneralRe: Question about another method.... PinmemberLouis huang4:17 27 Nov '06  
QuestionCan you tell me the use? PinmemberPriyank Bolia23:40 26 Nov '06  
AnswerRe: Can you tell me the use? PinmemberLouis huang2:21 27 Nov '06  
GeneralRe: Can you tell me the use? PinmemberPriyank Bolia19:48 28 Nov '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 21 May 2007
Article Copyright 2006 by Louis huang
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid