PDU encoding and decoding of SMS messages

Source: Internet
Author: User

There are three methods to send and receive SMS messages: block mode, text mode, and PDU mode. PDU mode is supported by all mobile phones and can use any character set. This is also the default encoding method for mobile phones.

Text and PDU (Protocol Data Unit) modes are commonly used to send short messages. Sending and receiving text messages in text mode is simple and easy to implement, but the biggest drawback is that you cannot send and receive Chinese text messages. PDU mode supports both Chinese text messages and English text messages. In PDU mode, messages can be sent and received in 7-bit, 8-bit, and ucs2 encoding formats. 7-bit encoding is used to send Common ASCII characters. It encodes a string of 7-bit characters (the highest bit is 0) into 8-bit data, each 8 characters can be "COMPRESSED" into 7; 8-bit encoding is usually used to send data messages, such as slices and ringtones; while ucs2 encoding is used to send Unicode characters. In these three encoding modes, the maximum capacity of the user information (TP-UD) segment of the PDU string (the maximum number of characters that can be sent for short messages) is 160, 140, and 70, respectively. Here, an English letter, a Chinese character, and a Data byte are regarded as a character.
The length of user information (TP-UDL) of a PDU string, meaning different in various encoding methods. 7-bit encoding refers to the number of characters of the original Short Message, rather than the number of bytes After encoding. 8-bit encoding is the number of bytes. Ucs2 encoding is also the number of bytes, which is twice the number of characters of the original short message. If a header exists in user information (TP-UD for basic parameters is 1), the length of user information (TP-UDHI) in all encoding modes) both are equal to the sum of the header length and the number of encoded bytes. If the compression algorithm recommended by GSM 03.42 is adopted (the High 3-bit TP-DCS is 001), the length is also the sum of the number of bytes after compression or the length of the header and the number of bytes after compression.

PDU is equivalent to a data packet, which consists of messages (SMS. As a data unit, it must contain the source/destination address, protected (valid) time, data format, protocol type, and body. The body length can be up to 140 bytes, they are all expressed in hexadecimal notation. The PDU structure varies depending on whether a short message is initiated by a mobile terminal or for a mobile terminal. Each message can be sent in 140 bytes. Since the longest data string in the system does not exceed 140 bytes, data can be sent in one message.
Generally, the PDU code consists of 13 items, namely a B c d e f g h I j k l m.
A: The length of the SMS center address, two hexadecimal digits (1 byte ).
B: The number type of the SMS center, which is a two-digit hexadecimal number.
C: the number of the short message center. The length of B + C is determined by the data in.
D: File Header byte, which is a two-digit hexadecimal number.
E: Information type, two-digit hexadecimal number.
F: the length of the called number, which is a two-digit hexadecimal number.
G: name of the called number type. It is a two-digit hexadecimal number. The value is the same as that of B.
H: The called number. The length is determined by the data in F.
I: Protocol identifier, two-digit hexadecimal number.
J: data encoding scheme, two-digit hexadecimal number.
K: validity period, two-digit hexadecimal number.
L: User Data Length, two-digit hexadecimal number.
M: The length of user data is determined by the data in L. The ucs2 encoding is used in J, which is a Unicode Character in Chinese and English.
PDU encoding protocol
Example 1 send: SMSC number is+ 8613800250500The recipient's number is13693092030The message content is"Hello!". The PDU string sent from the mobile phone can be
08 91 68 31 08 20 05 05 F0 11 00 0d 91 68 31 96 03 29 30 F0 00 00 00 06 C8 32 9B FD 0e 01
For detailed analysis, refer to the following specifications:
Description
08The length of SMSC address information is 8 8 bytes (including 91)
91SMSC address format (ton/NPI) number in international format (Add '+' in front ')
68 31 08 20 05 05 F0SMSC address 8613800250500, fill in 'F' into an even number
11Basic parameters (TP-MTI/VFP) sent, TP-VP in relative format
00Message Reference value (TP-MR) 0
0dA total of 13 decimal numbers of the target address (excluding 91 and 'F ')
91The destination address format (ton/NPI) is an international number (Add '+' to the Front ')
68 31 96 03 29 30 F0Target address (TP-DA) 8613693092030, fill 'F' to make up an even number
00Protocol identifier (TP-PID) is a common GSM type, point-to-point method
00User Information encoding (TP-DCS) 7-bit encoding
00Validity Period (TP-VP) 5 minutes
06User Information length (TP-UDL) actual length 6 bytes
C8 32 9B FD 0e 01User Information (TP-UD) "Hello !"
Example 2 receive: the SMSC number is+ 8613800250500The recipient's number is13693092030The message content is"Hello!". The PDU string received by the mobile phone can be
08 91 68 31 08 20 05 05 F0 84 0d 91 68 31 96 03 29 30 F0 00 08 30 30 21 80 63 54 80 06 4f 60 59 7d 00 21
For detailed analysis, refer to the following specifications:
Description
08The length of the address information is 8 bytes (including 91)
91SMSC address format (ton/NPI) number in international format (Add '+' in front ')
68 31 08 20 05 05 F0SMSC address 8613800250500, fill in 'F' into an even number
84Basic parameters (TP-MTI/MMS/RP) received, no more messages, reply address
0dThe number of reply addresses in 13 decimal digits (excluding 91 and 'F ')
91The reply address format (ton/NPI) uses an international number (Add '+' to the Front ')
68 31 96 03 29 30 F0Reply address (TP-RA) 8613693092030, fill 'F' to an even number
00Protocol identifier (TP-PID) is a common GSM type, point-to-point method
08User Information encoding (TP-DCS) ucs2 Encoding
30 30 21 80 63 54 80Timestamp (TP-SCTS) 2003-3-12 08:36:45 + 8 Time Zone
06User Information length (TP-UDL) actual length 6 bytes
4f 60 59 7d 00 21User Information (TP-UD) "Hello !"

Detailed analysis:
08916830000200505f011108d91683105151094f50008ff10008ff044f60597d
<1> SMS center address field: 0891
08: Address-lengt (address length). The length of the central address of the short message is 8 bytes, which is the length of (91) + (6830000200505f0), and 8 8 bytes.
91 URL type: 10010001 bit7: 1. Always 1 bits 6, 5, 4: type-of-number (number type): 001, representing the internation number. That is, add "+" before the number ". Note: For some special numbers, for example, when a mobile phone is connected to PHS, it cannot be set to 001, but to 000, which indicates that there is no "+" before the number ", otherwise, it cannot be received.
The following is an explanation of the number type of the gsm03.40 protocol:
0 0 0 unknown
0 0 1 international number
0 1 0 national number
0 1 1 Network specific number
1 0 0 subscriber number
1 0 1 alphanumeric (coded according to ts03.38 7-bit default alphabet)
1 1 0 abbreviated number
1 1 1 reserved for extension
Ll not interpret reserved values but will store them as received.
Bits 0000, 1111: Numbering-plan-identification (number identification), 0001-unknown, 0001-isdn/phone number (e.164/e.163),-reserved for extension; generally, the default value is, indicates the phone number type. The following is an explanation of the identification of the gsm03.40 Number:
Bits3 2 1 0
0 0 0 0 unknown
0 0 0 1 ISDN/telephone numbering plan (e.164/e.163)
0 0 1 1 Data Numbering Plan (x.121)
0 1 0 0 telex numbering plan
1 0 0 0 National Numbering Plan
1 0 0 1 private numbering plan
1 0 1 0 ERMES Numbering Plan (ETSI de/PS 3 01-3)
1 1 1 1 reserved for extension
All other values are reserved.
<2> SMS center number: 683104200805f0
The length of a byte is reversed to 8613800280500. If the length is an odd number, add "F. For example, if the number is: + 8613505165495, remove "+" and add "F" to the end to "8613505165495f". Then, replace the odd and even digits of the mobile phone number with "683105154254f5 ".
<3> firstoctet field: 1119
(1) 11 contains TP-MTI (2bit), TP-RD (1bit), TP-VPF (2bit), TP-RP (1bit), TP-UDHI (1bit), TP-SRR (1bit)
Binary representation: 0 0 0 10 0 01
TP-MTI: 01 TP-message-type-indicator (Message Type Indicator)

Bit1, 0: 00-read (deliver); 01-submit (submit)
Bit1, 0: 01 indicates SMS-SUBMIT type
The following is the explanation of gsm03.40:
Bit1 bit0 Message Type
0 0 SMS-DELIVER (in the direction SC to MS)
0 0 SMS-DELIVER report (in the direction MS to SC)
1 0 SMS-STATUS-REPORT (in the direction SC to MS)
1 0 SMS-COMMAND (in the direction MS to SC)
0 1 SMS-SUBMIT (in the direction MS to SC)
0 1 SMS-SUBMIT-REPORT (in the direction SC to MS)
1 1 reservedtp-rd: 0 TP-reject-Duplicates (whether to reject identical duplicate messages)

Bit2: 0-copy accepted; 1-copy denied
 
Bit2: 0 indicates that the message center receives messages that are not forwarded with the same TP-MR.
Bit 2:
0 instruct the SC to accept an SMS-SUBMIT for an SM still held in the SC which has the same TP-MR and the same TP-DA as a previusly submitted Sm from the same OA.
1 instruct the SC to reject an SMS-SUBMIT for an SM still held in the SC which has the same TP-MR and the same TP-DA as the previusly submitted Sm from the same OA. in this case an appropriate TP-FCS value will be returned in the SMS-SUBMIT-REPORT.
TP-VPF: 10 TP-validity-period-format (validity period format)

Bit4, 3: 00-not provided (not present); 01-reserved; 10-INTEGER (standard), which indicates that the relative format is used; 11-provide half of the 8-bit bytes (semi-octet represented)
The following is the explanation of gsm03.40:
Bit4 bit3
0 0 TP-VP field not present
1 0 TP-VP field present and integer represented (relative)
0 1 Reserved
1 1 TP-VP field present and semi-octet represented (absolute)
TP-SRR: 0 TP-status-report-Request
Bit5: 1: Report required; 0: report not required.
Bit 5:
0 a status report is not requested
1 A status report is requested
TP-UDHI: 0 TP-user-data-header-indicator (user data header identifier) bit6: 1: Header Information 0: header information not included, indicating this is an SMS message, no user data header, you need to set EMS (enhanced message service) messages. The image ringtones contain the header information. The text does not contain the header information.
Bit 6
0 The TP-UD field contains only the short message
1 The Beginning of the TP-UD field contains a header in addition to the short message
TP-RP: 0 TP-reply-path (reply path) bit7: 1: Set reply path, 0: No set reply path.
The following is the explanation of gsm03.40:
Bit 7:
0: TP-reply-path parameter is not set in this SMS-SUBMIT/deliver
1: TP-reply-path parameter is set in this SMS-SUBMIT/deliver
(2) Message Reference Value TP-MR (TP-message-reference): 19 if the "00" value is used, the phone sets the message reference value.
 
<4> peer number field: 0d913105152.164f5
0d: number of target address numbers. A total of 13 decimal numbers (excluding 91 and 'F ')
91: Address type, same as the SMS center number.
3105152.164f5: The target mobile phone number.

<5> upper-layer protocol identification TP-PID (TP-Protocol-identifier): 00
Generally set to 00, indicating general GSM, point-to-point <6> data encoding settings TP-DCS (TP-data-coding-Scheme): 08
Indicates the encoding method of the TP-UD. 08 represents the Unicode mode. Refer to the gsm03.38 protocol:
Bit 3, 2
00 default alphabet
01 8 bit
10 ucs2 (16bit) [10]
11 Reserved
<7> validity period TP-VP (TP-validity-period): FF
FF indicates the maximum value.
<8> TP-UDL (TP-user-data-length): 4
The actual length of user data. Note that the user length is defined differently under different codes.
<9> User Data: 4f60597d "hello" unicode encoding
You: 0x4f60; good: 0x597d
2: PDU string received by the mobile phone
0891683109200505f0040d91683105151094f5000850208151754500044f60597d
<1> text message center address field: 08916830000200505f0, that is, + 8613800250500
<2> firstoctet: 04 its binary code: 00000100
TP-MTI: 00
TP-MMS (TP-more-message-to-send): 1 The SMS center has no more messages to send
TP-SRI: 0
TP-UDHI: 0
TP-RP: 0
<3> Sender number: 0d91683105151094f5 + 8613505165495
<4> protocol identifier: 00 TP-DCS point-to-point
<5> encoding method: 08 TP-DCS unicode encoding
<6> SMS center time: 50208151754500
Byte reversal 05/02/18 15:57:45 the last 00 represents the time zone, where 0
<7> User Data Length: 4
<8> User Data: 4f60597d
Chinese "hello" unicode encoding

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.