SmsMessage.java revision 1260f1c6c909f2940989b72afe1b91fd83845eaa
1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.android.internal.telephony.gsm;
19
20import android.telephony.PhoneNumberUtils;
21import android.text.format.Time;
22import android.telephony.Rlog;
23
24import com.android.internal.telephony.EncodeException;
25import com.android.internal.telephony.GsmAlphabet;
26import com.android.internal.telephony.GsmAlphabet.TextEncodingDetails;
27import com.android.internal.telephony.uicc.IccUtils;
28import com.android.internal.telephony.SmsHeader;
29import com.android.internal.telephony.SmsMessageBase;
30
31import java.io.ByteArrayOutputStream;
32import java.io.UnsupportedEncodingException;
33import java.text.ParseException;
34
35import static com.android.internal.telephony.SmsConstants.MessageClass;
36import static com.android.internal.telephony.SmsConstants.ENCODING_UNKNOWN;
37import static com.android.internal.telephony.SmsConstants.ENCODING_7BIT;
38import static com.android.internal.telephony.SmsConstants.ENCODING_8BIT;
39import static com.android.internal.telephony.SmsConstants.ENCODING_16BIT;
40import static com.android.internal.telephony.SmsConstants.ENCODING_KSC5601;
41import static com.android.internal.telephony.SmsConstants.MAX_USER_DATA_SEPTETS;
42import static com.android.internal.telephony.SmsConstants.MAX_USER_DATA_BYTES;
43import static com.android.internal.telephony.SmsConstants.MAX_USER_DATA_BYTES_WITH_HEADER;
44
45/**
46 * A Short Message Service message.
47 *
48 */
49public class SmsMessage extends SmsMessageBase {
50    static final String LOG_TAG = "SmsMessage";
51    private static final boolean VDBG = false;
52
53    private MessageClass messageClass;
54
55    /**
56     * TP-Message-Type-Indicator
57     * 9.2.3
58     */
59    private int mMti;
60
61    /** TP-Protocol-Identifier (TP-PID) */
62    private int mProtocolIdentifier;
63
64    // TP-Data-Coding-Scheme
65    // see TS 23.038
66    private int mDataCodingScheme;
67
68    // TP-Reply-Path
69    // e.g. 23.040 9.2.2.1
70    private boolean mReplyPathPresent = false;
71
72    /** The address of the receiver. */
73    private GsmSmsAddress mRecipientAddress;
74
75    /**
76     *  TP-Status - status of a previously submitted SMS.
77     *  This field applies to SMS-STATUS-REPORT messages.  0 indicates success;
78     *  see TS 23.040, 9.2.3.15 for description of other possible values.
79     */
80    private int mStatus;
81
82    /**
83     *  TP-Status - status of a previously submitted SMS.
84     *  This field is true iff the message is a SMS-STATUS-REPORT message.
85     */
86    private boolean mIsStatusReportMessage = false;
87
88    public static class SubmitPdu extends SubmitPduBase {
89    }
90
91    /**
92     * Create an SmsMessage from a raw PDU.
93     */
94    public static SmsMessage createFromPdu(byte[] pdu) {
95        try {
96            SmsMessage msg = new SmsMessage();
97            msg.parsePdu(pdu);
98            return msg;
99        } catch (RuntimeException ex) {
100            Rlog.e(LOG_TAG, "SMS PDU parsing failed: ", ex);
101            return null;
102        } catch (OutOfMemoryError e) {
103            Rlog.e(LOG_TAG, "SMS PDU parsing failed with out of memory: ", e);
104            return null;
105        }
106    }
107
108    /**
109     * 3GPP TS 23.040 9.2.3.9 specifies that Type Zero messages are indicated
110     * by TP_PID field set to value 0x40
111     */
112    public boolean isTypeZero() {
113        return (mProtocolIdentifier == 0x40);
114    }
115
116    /**
117     * TS 27.005 3.4.1 lines[0] and lines[1] are the two lines read from the
118     * +CMT unsolicited response (PDU mode, of course)
119     *  +CMT: [&lt;alpha>],<length><CR><LF><pdu>
120     *
121     * Only public for debugging
122     *
123     * {@hide}
124     */
125    public static SmsMessage newFromCMT(String[] lines) {
126        try {
127            SmsMessage msg = new SmsMessage();
128            msg.parsePdu(IccUtils.hexStringToBytes(lines[1]));
129            return msg;
130        } catch (RuntimeException ex) {
131            Rlog.e(LOG_TAG, "SMS PDU parsing failed: ", ex);
132            return null;
133        }
134    }
135
136    /** @hide */
137    public static SmsMessage newFromCDS(String line) {
138        try {
139            SmsMessage msg = new SmsMessage();
140            msg.parsePdu(IccUtils.hexStringToBytes(line));
141            return msg;
142        } catch (RuntimeException ex) {
143            Rlog.e(LOG_TAG, "CDS SMS PDU parsing failed: ", ex);
144            return null;
145        }
146    }
147
148    /**
149     * Create an SmsMessage from an SMS EF record.
150     *
151     * @param index Index of SMS record. This should be index in ArrayList
152     *              returned by SmsManager.getAllMessagesFromSim + 1.
153     * @param data Record data.
154     * @return An SmsMessage representing the record.
155     *
156     * @hide
157     */
158    public static SmsMessage createFromEfRecord(int index, byte[] data) {
159        try {
160            SmsMessage msg = new SmsMessage();
161
162            msg.mIndexOnIcc = index;
163
164            // First byte is status: RECEIVED_READ, RECEIVED_UNREAD, STORED_SENT,
165            // or STORED_UNSENT
166            // See TS 51.011 10.5.3
167            if ((data[0] & 1) == 0) {
168                Rlog.w(LOG_TAG,
169                        "SMS parsing failed: Trying to parse a free record");
170                return null;
171            } else {
172                msg.mStatusOnIcc = data[0] & 0x07;
173            }
174
175            int size = data.length - 1;
176
177            // Note: Data may include trailing FF's.  That's OK; message
178            // should still parse correctly.
179            byte[] pdu = new byte[size];
180            System.arraycopy(data, 1, pdu, 0, size);
181            msg.parsePdu(pdu);
182            return msg;
183        } catch (RuntimeException ex) {
184            Rlog.e(LOG_TAG, "SMS PDU parsing failed: ", ex);
185            return null;
186        }
187    }
188
189    /**
190     * Get the TP-Layer-Length for the given SMS-SUBMIT PDU Basically, the
191     * length in bytes (not hex chars) less the SMSC header
192     */
193    public static int getTPLayerLengthForPDU(String pdu) {
194        int len = pdu.length() / 2;
195        int smscLen = Integer.parseInt(pdu.substring(0, 2), 16);
196
197        return len - smscLen - 1;
198    }
199
200    /**
201     * Get an SMS-SUBMIT PDU for a destination address and a message
202     *
203     * @param scAddress Service Centre address.  Null means use default.
204     * @return a <code>SubmitPdu</code> containing the encoded SC
205     *         address, if applicable, and the encoded message.
206     *         Returns null on encode error.
207     * @hide
208     */
209    public static SubmitPdu getSubmitPdu(String scAddress,
210            String destinationAddress, String message,
211            boolean statusReportRequested, byte[] header) {
212        return getSubmitPdu(scAddress, destinationAddress, message, statusReportRequested, header,
213                ENCODING_UNKNOWN, 0, 0);
214    }
215
216
217    /**
218     * Get an SMS-SUBMIT PDU for a destination address and a message using the
219     * specified encoding.
220     *
221     * @param scAddress Service Centre address.  Null means use default.
222     * @param encoding Encoding defined by constants in
223     *        com.android.internal.telephony.SmsConstants.ENCODING_*
224     * @param languageTable
225     * @param languageShiftTable
226     * @return a <code>SubmitPdu</code> containing the encoded SC
227     *         address, if applicable, and the encoded message.
228     *         Returns null on encode error.
229     * @hide
230     */
231    public static SubmitPdu getSubmitPdu(String scAddress,
232            String destinationAddress, String message,
233            boolean statusReportRequested, byte[] header, int encoding,
234            int languageTable, int languageShiftTable) {
235
236        // Perform null parameter checks.
237        if (message == null || destinationAddress == null) {
238            return null;
239        }
240
241        if (encoding == ENCODING_UNKNOWN) {
242            // Find the best encoding to use
243            TextEncodingDetails ted = calculateLength(message, false);
244            encoding = ted.codeUnitSize;
245            languageTable = ted.languageTable;
246            languageShiftTable = ted.languageShiftTable;
247
248            if (encoding == ENCODING_7BIT &&
249                    (languageTable != 0 || languageShiftTable != 0)) {
250                if (header != null) {
251                    SmsHeader smsHeader = SmsHeader.fromByteArray(header);
252                    if (smsHeader.languageTable != languageTable
253                            || smsHeader.languageShiftTable != languageShiftTable) {
254                        Rlog.w(LOG_TAG, "Updating language table in SMS header: "
255                                + smsHeader.languageTable + " -> " + languageTable + ", "
256                                + smsHeader.languageShiftTable + " -> " + languageShiftTable);
257                        smsHeader.languageTable = languageTable;
258                        smsHeader.languageShiftTable = languageShiftTable;
259                        header = SmsHeader.toByteArray(smsHeader);
260                    }
261                } else {
262                    SmsHeader smsHeader = new SmsHeader();
263                    smsHeader.languageTable = languageTable;
264                    smsHeader.languageShiftTable = languageShiftTable;
265                    header = SmsHeader.toByteArray(smsHeader);
266                }
267            }
268        }
269
270        SubmitPdu ret = new SubmitPdu();
271        // MTI = SMS-SUBMIT, UDHI = header != null
272        byte mtiByte = (byte)(0x01 | (header != null ? 0x40 : 0x00));
273        ByteArrayOutputStream bo = getSubmitPduHead(
274                scAddress, destinationAddress, mtiByte,
275                statusReportRequested, ret);
276
277        // User Data (and length)
278        byte[] userData;
279        try {
280            if (encoding == ENCODING_7BIT) {
281                userData = GsmAlphabet.stringToGsm7BitPackedWithHeader(message, header,
282                        languageTable, languageShiftTable);
283            } else { //assume UCS-2
284                try {
285                    userData = encodeUCS2(message, header);
286                } catch(UnsupportedEncodingException uex) {
287                    Rlog.e(LOG_TAG,
288                            "Implausible UnsupportedEncodingException ",
289                            uex);
290                    return null;
291                }
292            }
293        } catch (EncodeException ex) {
294            // Encoding to the 7-bit alphabet failed. Let's see if we can
295            // send it as a UCS-2 encoded message
296            try {
297                userData = encodeUCS2(message, header);
298                encoding = ENCODING_16BIT;
299            } catch(UnsupportedEncodingException uex) {
300                Rlog.e(LOG_TAG,
301                        "Implausible UnsupportedEncodingException ",
302                        uex);
303                return null;
304            }
305        }
306
307        if (encoding == ENCODING_7BIT) {
308            if ((0xff & userData[0]) > MAX_USER_DATA_SEPTETS) {
309                // Message too long
310                Rlog.e(LOG_TAG, "Message too long (" + (0xff & userData[0]) + " septets)");
311                return null;
312            }
313            // TP-Data-Coding-Scheme
314            // Default encoding, uncompressed
315            // To test writing messages to the SIM card, change this value 0x00
316            // to 0x12, which means "bits 1 and 0 contain message class, and the
317            // class is 2". Note that this takes effect for the sender. In other
318            // words, messages sent by the phone with this change will end up on
319            // the receiver's SIM card. You can then send messages to yourself
320            // (on a phone with this change) and they'll end up on the SIM card.
321            bo.write(0x00);
322        } else { // assume UCS-2
323            if ((0xff & userData[0]) > MAX_USER_DATA_BYTES) {
324                // Message too long
325                Rlog.e(LOG_TAG, "Message too long (" + (0xff & userData[0]) + " bytes)");
326                return null;
327            }
328            // TP-Data-Coding-Scheme
329            // UCS-2 encoding, uncompressed
330            bo.write(0x08);
331        }
332
333        // (no TP-Validity-Period)
334        bo.write(userData, 0, userData.length);
335        ret.encodedMessage = bo.toByteArray();
336        return ret;
337    }
338
339    /**
340     * Packs header and UCS-2 encoded message. Includes TP-UDL & TP-UDHL if necessary
341     *
342     * @return encoded message as UCS2
343     * @throws UnsupportedEncodingException
344     */
345    private static byte[] encodeUCS2(String message, byte[] header)
346        throws UnsupportedEncodingException {
347        byte[] userData, textPart;
348        textPart = message.getBytes("utf-16be");
349
350        if (header != null) {
351            // Need 1 byte for UDHL
352            userData = new byte[header.length + textPart.length + 1];
353
354            userData[0] = (byte)header.length;
355            System.arraycopy(header, 0, userData, 1, header.length);
356            System.arraycopy(textPart, 0, userData, header.length + 1, textPart.length);
357        }
358        else {
359            userData = textPart;
360        }
361        byte[] ret = new byte[userData.length+1];
362        ret[0] = (byte) (userData.length & 0xff );
363        System.arraycopy(userData, 0, ret, 1, userData.length);
364        return ret;
365    }
366
367    /**
368     * Get an SMS-SUBMIT PDU for a destination address and a message
369     *
370     * @param scAddress Service Centre address.  Null means use default.
371     * @return a <code>SubmitPdu</code> containing the encoded SC
372     *         address, if applicable, and the encoded message.
373     *         Returns null on encode error.
374     */
375    public static SubmitPdu getSubmitPdu(String scAddress,
376            String destinationAddress, String message,
377            boolean statusReportRequested) {
378
379        return getSubmitPdu(scAddress, destinationAddress, message, statusReportRequested, null);
380    }
381
382    /**
383     * Get an SMS-SUBMIT PDU for a data message to a destination address &amp; port
384     *
385     * @param scAddress Service Centre address. null == use default
386     * @param destinationAddress the address of the destination for the message
387     * @param destinationPort the port to deliver the message to at the
388     *        destination
389     * @param data the data for the message
390     * @return a <code>SubmitPdu</code> containing the encoded SC
391     *         address, if applicable, and the encoded message.
392     *         Returns null on encode error.
393     */
394    public static SubmitPdu getSubmitPdu(String scAddress,
395            String destinationAddress, int destinationPort, byte[] data,
396            boolean statusReportRequested) {
397
398        SmsHeader.PortAddrs portAddrs = new SmsHeader.PortAddrs();
399        portAddrs.destPort = destinationPort;
400        portAddrs.origPort = 0;
401        portAddrs.areEightBits = false;
402
403        SmsHeader smsHeader = new SmsHeader();
404        smsHeader.portAddrs = portAddrs;
405
406        byte[] smsHeaderData = SmsHeader.toByteArray(smsHeader);
407
408        if ((data.length + smsHeaderData.length + 1) > MAX_USER_DATA_BYTES) {
409            Rlog.e(LOG_TAG, "SMS data message may only contain "
410                    + (MAX_USER_DATA_BYTES - smsHeaderData.length - 1) + " bytes");
411            return null;
412        }
413
414        SubmitPdu ret = new SubmitPdu();
415        ByteArrayOutputStream bo = getSubmitPduHead(
416                scAddress, destinationAddress, (byte) 0x41, // MTI = SMS-SUBMIT,
417                                                            // TP-UDHI = true
418                statusReportRequested, ret);
419
420        // TP-Data-Coding-Scheme
421        // No class, 8 bit data
422        bo.write(0x04);
423
424        // (no TP-Validity-Period)
425
426        // Total size
427        bo.write(data.length + smsHeaderData.length + 1);
428
429        // User data header
430        bo.write(smsHeaderData.length);
431        bo.write(smsHeaderData, 0, smsHeaderData.length);
432
433        // User data
434        bo.write(data, 0, data.length);
435
436        ret.encodedMessage = bo.toByteArray();
437        return ret;
438    }
439
440    /**
441     * Create the beginning of a SUBMIT PDU.  This is the part of the
442     * SUBMIT PDU that is common to the two versions of {@link #getSubmitPdu},
443     * one of which takes a byte array and the other of which takes a
444     * <code>String</code>.
445     *
446     * @param scAddress Service Centre address. null == use default
447     * @param destinationAddress the address of the destination for the message
448     * @param mtiByte
449     * @param ret <code>SubmitPdu</code> containing the encoded SC
450     *        address, if applicable, and the encoded message
451     */
452    private static ByteArrayOutputStream getSubmitPduHead(
453            String scAddress, String destinationAddress, byte mtiByte,
454            boolean statusReportRequested, SubmitPdu ret) {
455        ByteArrayOutputStream bo = new ByteArrayOutputStream(
456                MAX_USER_DATA_BYTES + 40);
457
458        // SMSC address with length octet, or 0
459        if (scAddress == null) {
460            ret.encodedScAddress = null;
461        } else {
462            ret.encodedScAddress = PhoneNumberUtils.networkPortionToCalledPartyBCDWithLength(
463                    scAddress);
464        }
465
466        // TP-Message-Type-Indicator (and friends)
467        if (statusReportRequested) {
468            // Set TP-Status-Report-Request bit.
469            mtiByte |= 0x20;
470            if (VDBG) Rlog.d(LOG_TAG, "SMS status report requested");
471        }
472        bo.write(mtiByte);
473
474        // space for TP-Message-Reference
475        bo.write(0);
476
477        byte[] daBytes;
478
479        daBytes = PhoneNumberUtils.networkPortionToCalledPartyBCD(destinationAddress);
480
481        // destination address length in BCD digits, ignoring TON byte and pad
482        // TODO Should be better.
483        bo.write((daBytes.length - 1) * 2
484                - ((daBytes[daBytes.length - 1] & 0xf0) == 0xf0 ? 1 : 0));
485
486        // destination address
487        bo.write(daBytes, 0, daBytes.length);
488
489        // TP-Protocol-Identifier
490        bo.write(0);
491        return bo;
492    }
493
494    private static class PduParser {
495        byte mPdu[];
496        int mCur;
497        SmsHeader mUserDataHeader;
498        byte[] mUserData;
499        int mUserDataSeptetPadding;
500
501        PduParser(byte[] pdu) {
502            mPdu = pdu;
503            mCur = 0;
504            mUserDataSeptetPadding = 0;
505        }
506
507        /**
508         * Parse and return the SC address prepended to SMS messages coming via
509         * the TS 27.005 / AT interface.  Returns null on invalid address
510         */
511        String getSCAddress() {
512            int len;
513            String ret;
514
515            // length of SC Address
516            len = getByte();
517
518            if (len == 0) {
519                // no SC address
520                ret = null;
521            } else {
522                // SC address
523                try {
524                    ret = PhoneNumberUtils
525                            .calledPartyBCDToString(mPdu, mCur, len);
526                } catch (RuntimeException tr) {
527                    Rlog.d(LOG_TAG, "invalid SC address: ", tr);
528                    ret = null;
529                }
530            }
531
532            mCur += len;
533
534            return ret;
535        }
536
537        /**
538         * returns non-sign-extended byte value
539         */
540        int getByte() {
541            return mPdu[mCur++] & 0xff;
542        }
543
544        /**
545         * Any address except the SC address (eg, originating address) See TS
546         * 23.040 9.1.2.5
547         */
548        GsmSmsAddress getAddress() {
549            GsmSmsAddress ret;
550
551            // "The Address-Length field is an integer representation of
552            // the number field, i.e. excludes any semi-octet containing only
553            // fill bits."
554            // The TOA field is not included as part of this
555            int addressLength = mPdu[mCur] & 0xff;
556            int lengthBytes = 2 + (addressLength + 1) / 2;
557
558            try {
559                ret = new GsmSmsAddress(mPdu, mCur, lengthBytes);
560            } catch (ParseException e) {
561                Rlog.e(LOG_TAG, e.getMessage());
562                ret = null;
563            }
564
565            mCur += lengthBytes;
566
567            return ret;
568        }
569
570        /**
571         * Parses an SC timestamp and returns a currentTimeMillis()-style
572         * timestamp
573         */
574
575        long getSCTimestampMillis() {
576            // TP-Service-Centre-Time-Stamp
577            int year = IccUtils.gsmBcdByteToInt(mPdu[mCur++]);
578            int month = IccUtils.gsmBcdByteToInt(mPdu[mCur++]);
579            int day = IccUtils.gsmBcdByteToInt(mPdu[mCur++]);
580            int hour = IccUtils.gsmBcdByteToInt(mPdu[mCur++]);
581            int minute = IccUtils.gsmBcdByteToInt(mPdu[mCur++]);
582            int second = IccUtils.gsmBcdByteToInt(mPdu[mCur++]);
583
584            // For the timezone, the most significant bit of the
585            // least significant nibble is the sign byte
586            // (meaning the max range of this field is 79 quarter-hours,
587            // which is more than enough)
588
589            byte tzByte = mPdu[mCur++];
590
591            // Mask out sign bit.
592            int timezoneOffset = IccUtils.gsmBcdByteToInt((byte) (tzByte & (~0x08)));
593
594            timezoneOffset = ((tzByte & 0x08) == 0) ? timezoneOffset : -timezoneOffset;
595
596            Time time = new Time(Time.TIMEZONE_UTC);
597
598            // It's 2006.  Should I really support years < 2000?
599            time.year = year >= 90 ? year + 1900 : year + 2000;
600            time.month = month - 1;
601            time.monthDay = day;
602            time.hour = hour;
603            time.minute = minute;
604            time.second = second;
605
606            // Timezone offset is in quarter hours.
607            return time.toMillis(true) - (timezoneOffset * 15 * 60 * 1000);
608        }
609
610        /**
611         * Pulls the user data out of the PDU, and separates the payload from
612         * the header if there is one.
613         *
614         * @param hasUserDataHeader true if there is a user data header
615         * @param dataInSeptets true if the data payload is in septets instead
616         *  of octets
617         * @return the number of septets or octets in the user data payload
618         */
619        int constructUserData(boolean hasUserDataHeader, boolean dataInSeptets) {
620            int offset = mCur;
621            int userDataLength = mPdu[offset++] & 0xff;
622            int headerSeptets = 0;
623            int userDataHeaderLength = 0;
624
625            if (hasUserDataHeader) {
626                userDataHeaderLength = mPdu[offset++] & 0xff;
627
628                byte[] udh = new byte[userDataHeaderLength];
629                System.arraycopy(mPdu, offset, udh, 0, userDataHeaderLength);
630                mUserDataHeader = SmsHeader.fromByteArray(udh);
631                offset += userDataHeaderLength;
632
633                int headerBits = (userDataHeaderLength + 1) * 8;
634                headerSeptets = headerBits / 7;
635                headerSeptets += (headerBits % 7) > 0 ? 1 : 0;
636                mUserDataSeptetPadding = (headerSeptets * 7) - headerBits;
637            }
638
639            int bufferLen;
640            if (dataInSeptets) {
641                /*
642                 * Here we just create the user data length to be the remainder of
643                 * the pdu minus the user data header, since userDataLength means
644                 * the number of uncompressed septets.
645                 */
646                bufferLen = mPdu.length - offset;
647            } else {
648                /*
649                 * userDataLength is the count of octets, so just subtract the
650                 * user data header.
651                 */
652                bufferLen = userDataLength - (hasUserDataHeader ? (userDataHeaderLength + 1) : 0);
653                if (bufferLen < 0) {
654                    bufferLen = 0;
655                }
656            }
657
658            mUserData = new byte[bufferLen];
659            System.arraycopy(mPdu, offset, mUserData, 0, mUserData.length);
660            mCur = offset;
661
662            if (dataInSeptets) {
663                // Return the number of septets
664                int count = userDataLength - headerSeptets;
665                // If count < 0, return 0 (means UDL was probably incorrect)
666                return count < 0 ? 0 : count;
667            } else {
668                // Return the number of octets
669                return mUserData.length;
670            }
671        }
672
673        /**
674         * Returns the user data payload, not including the headers
675         *
676         * @return the user data payload, not including the headers
677         */
678        byte[] getUserData() {
679            return mUserData;
680        }
681
682        /**
683         * Returns an object representing the user data headers
684         *
685         * {@hide}
686         */
687        SmsHeader getUserDataHeader() {
688            return mUserDataHeader;
689        }
690
691        /**
692         * Interprets the user data payload as packed GSM 7bit characters, and
693         * decodes them into a String.
694         *
695         * @param septetCount the number of septets in the user data payload
696         * @return a String with the decoded characters
697         */
698        String getUserDataGSM7Bit(int septetCount, int languageTable,
699                int languageShiftTable) {
700            String ret;
701
702            ret = GsmAlphabet.gsm7BitPackedToString(mPdu, mCur, septetCount,
703                    mUserDataSeptetPadding, languageTable, languageShiftTable);
704
705            mCur += (septetCount * 7) / 8;
706
707            return ret;
708        }
709
710        /**
711         * Interprets the user data payload as UCS2 characters, and
712         * decodes them into a String.
713         *
714         * @param byteCount the number of bytes in the user data payload
715         * @return a String with the decoded characters
716         */
717        String getUserDataUCS2(int byteCount) {
718            String ret;
719
720            try {
721                ret = new String(mPdu, mCur, byteCount, "utf-16");
722            } catch (UnsupportedEncodingException ex) {
723                ret = "";
724                Rlog.e(LOG_TAG, "implausible UnsupportedEncodingException", ex);
725            }
726
727            mCur += byteCount;
728            return ret;
729        }
730
731        /**
732         * Interprets the user data payload as KSC-5601 characters, and
733         * decodes them into a String.
734         *
735         * @param byteCount the number of bytes in the user data payload
736         * @return a String with the decoded characters
737         */
738        String getUserDataKSC5601(int byteCount) {
739            String ret;
740
741            try {
742                ret = new String(mPdu, mCur, byteCount, "KSC5601");
743            } catch (UnsupportedEncodingException ex) {
744                ret = "";
745                Rlog.e(LOG_TAG, "implausible UnsupportedEncodingException", ex);
746            }
747
748            mCur += byteCount;
749            return ret;
750        }
751
752        boolean moreDataPresent() {
753            return (mPdu.length > mCur);
754        }
755    }
756
757    /**
758     * Calculate the number of septets needed to encode the message.
759     *
760     * @param msgBody the message to encode
761     * @param use7bitOnly ignore (but still count) illegal characters if true
762     * @return TextEncodingDetails
763     */
764    public static TextEncodingDetails calculateLength(CharSequence msgBody,
765            boolean use7bitOnly) {
766        TextEncodingDetails ted = GsmAlphabet.countGsmSeptets(msgBody, use7bitOnly);
767        if (ted == null) {
768            ted = new TextEncodingDetails();
769            int octets = msgBody.length() * 2;
770            ted.codeUnitCount = msgBody.length();
771            if (octets > MAX_USER_DATA_BYTES) {
772                ted.msgCount = (octets + (MAX_USER_DATA_BYTES_WITH_HEADER - 1)) /
773                        MAX_USER_DATA_BYTES_WITH_HEADER;
774                ted.codeUnitsRemaining = ((ted.msgCount *
775                        MAX_USER_DATA_BYTES_WITH_HEADER) - octets) / 2;
776            } else {
777                ted.msgCount = 1;
778                ted.codeUnitsRemaining = (MAX_USER_DATA_BYTES - octets)/2;
779            }
780            ted.codeUnitSize = ENCODING_16BIT;
781        }
782        return ted;
783    }
784
785    /** {@inheritDoc} */
786    @Override
787    public int getProtocolIdentifier() {
788        return mProtocolIdentifier;
789    }
790
791    /**
792     * Returns the TP-Data-Coding-Scheme byte, for acknowledgement of SMS-PP download messages.
793     * @return the TP-DCS field of the SMS header
794     */
795    int getDataCodingScheme() {
796        return mDataCodingScheme;
797    }
798
799    /** {@inheritDoc} */
800    @Override
801    public boolean isReplace() {
802        return (mProtocolIdentifier & 0xc0) == 0x40
803                && (mProtocolIdentifier & 0x3f) > 0
804                && (mProtocolIdentifier & 0x3f) < 8;
805    }
806
807    /** {@inheritDoc} */
808    @Override
809    public boolean isCphsMwiMessage() {
810        return ((GsmSmsAddress) mOriginatingAddress).isCphsVoiceMessageClear()
811                || ((GsmSmsAddress) mOriginatingAddress).isCphsVoiceMessageSet();
812    }
813
814    /** {@inheritDoc} */
815    @Override
816    public boolean isMWIClearMessage() {
817        if (mIsMwi && !mMwiSense) {
818            return true;
819        }
820
821        return mOriginatingAddress != null
822                && ((GsmSmsAddress) mOriginatingAddress).isCphsVoiceMessageClear();
823    }
824
825    /** {@inheritDoc} */
826    @Override
827    public boolean isMWISetMessage() {
828        if (mIsMwi && mMwiSense) {
829            return true;
830        }
831
832        return mOriginatingAddress != null
833                && ((GsmSmsAddress) mOriginatingAddress).isCphsVoiceMessageSet();
834    }
835
836    /** {@inheritDoc} */
837    @Override
838    public boolean isMwiDontStore() {
839        if (mIsMwi && mMwiDontStore) {
840            return true;
841        }
842
843        if (isCphsMwiMessage()) {
844            // See CPHS 4.2 Section B.4.2.1
845            // If the user data is a single space char, do not store
846            // the message. Otherwise, store and display as usual
847            if (" ".equals(getMessageBody())) {
848                return true;
849            }
850        }
851
852        return false;
853    }
854
855    /** {@inheritDoc} */
856    @Override
857    public int getStatus() {
858        return mStatus;
859    }
860
861    /** {@inheritDoc} */
862    @Override
863    public boolean isStatusReportMessage() {
864        return mIsStatusReportMessage;
865    }
866
867    /** {@inheritDoc} */
868    @Override
869    public boolean isReplyPathPresent() {
870        return mReplyPathPresent;
871    }
872
873    /**
874     * TS 27.005 3.1, &lt;pdu&gt; definition "In the case of SMS: 3GPP TS 24.011 [6]
875     * SC address followed by 3GPP TS 23.040 [3] TPDU in hexadecimal format:
876     * ME/TA converts each octet of TP data unit into two IRA character long
877     * hex number (e.g. octet with integer value 42 is presented to TE as two
878     * characters 2A (IRA 50 and 65))" ...in the case of cell broadcast,
879     * something else...
880     */
881    private void parsePdu(byte[] pdu) {
882        mPdu = pdu;
883        // Rlog.d(LOG_TAG, "raw sms message:");
884        // Rlog.d(LOG_TAG, s);
885
886        PduParser p = new PduParser(pdu);
887
888        mScAddress = p.getSCAddress();
889
890        if (mScAddress != null) {
891            if (VDBG) Rlog.d(LOG_TAG, "SMS SC address: " + mScAddress);
892        }
893
894        // TODO(mkf) support reply path, user data header indicator
895
896        // TP-Message-Type-Indicator
897        // 9.2.3
898        int firstByte = p.getByte();
899
900        mMti = firstByte & 0x3;
901        switch (mMti) {
902        // TP-Message-Type-Indicator
903        // 9.2.3
904        case 0:
905        case 3: //GSM 03.40 9.2.3.1: MTI == 3 is Reserved.
906                //This should be processed in the same way as MTI == 0 (Deliver)
907            parseSmsDeliver(p, firstByte);
908            break;
909        case 1:
910            parseSmsSubmit(p, firstByte);
911            break;
912        case 2:
913            parseSmsStatusReport(p, firstByte);
914            break;
915        default:
916            // TODO(mkf) the rest of these
917            throw new RuntimeException("Unsupported message type");
918        }
919    }
920
921    /**
922     * Parses a SMS-STATUS-REPORT message.
923     *
924     * @param p A PduParser, cued past the first byte.
925     * @param firstByte The first byte of the PDU, which contains MTI, etc.
926     */
927    private void parseSmsStatusReport(PduParser p, int firstByte) {
928        mIsStatusReportMessage = true;
929
930        // TP-Message-Reference
931        mMessageRef = p.getByte();
932        // TP-Recipient-Address
933        mRecipientAddress = p.getAddress();
934        // TP-Service-Centre-Time-Stamp
935        mScTimeMillis = p.getSCTimestampMillis();
936        p.getSCTimestampMillis();
937        // TP-Status
938        mStatus = p.getByte();
939
940        // The following are optional fields that may or may not be present.
941        if (p.moreDataPresent()) {
942            // TP-Parameter-Indicator
943            int extraParams = p.getByte();
944            int moreExtraParams = extraParams;
945            while ((moreExtraParams & 0x80) != 0) {
946                // We only know how to parse a few extra parameters, all
947                // indicated in the first TP-PI octet, so skip over any
948                // additional TP-PI octets.
949                moreExtraParams = p.getByte();
950            }
951            // As per 3GPP 23.040 section 9.2.3.27 TP-Parameter-Indicator,
952            // only process the byte if the reserved bits (bits3 to 6) are zero.
953            if ((extraParams & 0x78) == 0) {
954                // TP-Protocol-Identifier
955                if ((extraParams & 0x01) != 0) {
956                    mProtocolIdentifier = p.getByte();
957                }
958                // TP-Data-Coding-Scheme
959                if ((extraParams & 0x02) != 0) {
960                    mDataCodingScheme = p.getByte();
961                }
962                // TP-User-Data-Length (implies existence of TP-User-Data)
963                if ((extraParams & 0x04) != 0) {
964                    boolean hasUserDataHeader = (firstByte & 0x40) == 0x40;
965                    parseUserData(p, hasUserDataHeader);
966                }
967            }
968        }
969    }
970
971    private void parseSmsDeliver(PduParser p, int firstByte) {
972        mReplyPathPresent = (firstByte & 0x80) == 0x80;
973
974        mOriginatingAddress = p.getAddress();
975
976        if (mOriginatingAddress != null) {
977            if (VDBG) Rlog.v(LOG_TAG, "SMS originating address: "
978                    + mOriginatingAddress.address);
979        }
980
981        // TP-Protocol-Identifier (TP-PID)
982        // TS 23.040 9.2.3.9
983        mProtocolIdentifier = p.getByte();
984
985        // TP-Data-Coding-Scheme
986        // see TS 23.038
987        mDataCodingScheme = p.getByte();
988
989        if (VDBG) {
990            Rlog.v(LOG_TAG, "SMS TP-PID:" + mProtocolIdentifier
991                    + " data coding scheme: " + mDataCodingScheme);
992        }
993
994        mScTimeMillis = p.getSCTimestampMillis();
995
996        if (VDBG) Rlog.d(LOG_TAG, "SMS SC timestamp: " + mScTimeMillis);
997
998        boolean hasUserDataHeader = (firstByte & 0x40) == 0x40;
999
1000        parseUserData(p, hasUserDataHeader);
1001    }
1002
1003    /**
1004     * Parses a SMS-SUBMIT message.
1005     *
1006     * @param p A PduParser, cued past the first byte.
1007     * @param firstByte The first byte of the PDU, which contains MTI, etc.
1008     */
1009    private void parseSmsSubmit(PduParser p, int firstByte) {
1010        mReplyPathPresent = (firstByte & 0x80) == 0x80;
1011
1012        // TP-MR (TP-Message Reference)
1013        mMessageRef = p.getByte();
1014
1015        mRecipientAddress = p.getAddress();
1016
1017        if (mRecipientAddress != null) {
1018            if (VDBG) Rlog.v(LOG_TAG, "SMS recipient address: " + mRecipientAddress.address);
1019        }
1020
1021        // TP-Protocol-Identifier (TP-PID)
1022        // TS 23.040 9.2.3.9
1023        mProtocolIdentifier = p.getByte();
1024
1025        // TP-Data-Coding-Scheme
1026        // see TS 23.038
1027        mDataCodingScheme = p.getByte();
1028
1029        if (VDBG) {
1030            Rlog.v(LOG_TAG, "SMS TP-PID:" + mProtocolIdentifier
1031                    + " data coding scheme: " + mDataCodingScheme);
1032        }
1033
1034        // TP-Validity-Period-Format
1035        int validityPeriodLength = 0;
1036        int validityPeriodFormat = ((firstByte>>3) & 0x3);
1037        if (0x0 == validityPeriodFormat) /* 00, TP-VP field not present*/
1038        {
1039            validityPeriodLength = 0;
1040        }
1041        else if (0x2 == validityPeriodFormat) /* 10, TP-VP: relative format*/
1042        {
1043            validityPeriodLength = 1;
1044        }
1045        else /* other case, 11 or 01, TP-VP: absolute or enhanced format*/
1046        {
1047            validityPeriodLength = 7;
1048        }
1049
1050        // TP-Validity-Period is not used on phone, so just ignore it for now.
1051        while (validityPeriodLength-- > 0)
1052        {
1053            p.getByte();
1054        }
1055
1056        boolean hasUserDataHeader = (firstByte & 0x40) == 0x40;
1057
1058        parseUserData(p, hasUserDataHeader);
1059    }
1060
1061    /**
1062     * Parses the User Data of an SMS.
1063     *
1064     * @param p The current PduParser.
1065     * @param hasUserDataHeader Indicates whether a header is present in the
1066     *                          User Data.
1067     */
1068    private void parseUserData(PduParser p, boolean hasUserDataHeader) {
1069        boolean hasMessageClass = false;
1070        boolean userDataCompressed = false;
1071
1072        int encodingType = ENCODING_UNKNOWN;
1073
1074        // Look up the data encoding scheme
1075        if ((mDataCodingScheme & 0x80) == 0) {
1076            userDataCompressed = (0 != (mDataCodingScheme & 0x20));
1077            hasMessageClass = (0 != (mDataCodingScheme & 0x10));
1078
1079            if (userDataCompressed) {
1080                Rlog.w(LOG_TAG, "4 - Unsupported SMS data coding scheme "
1081                        + "(compression) " + (mDataCodingScheme & 0xff));
1082            } else {
1083                switch ((mDataCodingScheme >> 2) & 0x3) {
1084                case 0: // GSM 7 bit default alphabet
1085                    encodingType = ENCODING_7BIT;
1086                    break;
1087
1088                case 2: // UCS 2 (16bit)
1089                    encodingType = ENCODING_16BIT;
1090                    break;
1091
1092                case 1: // 8 bit data
1093                case 3: // reserved
1094                    Rlog.w(LOG_TAG, "1 - Unsupported SMS data coding scheme "
1095                            + (mDataCodingScheme & 0xff));
1096                    encodingType = ENCODING_8BIT;
1097                    break;
1098                }
1099            }
1100        } else if ((mDataCodingScheme & 0xf0) == 0xf0) {
1101            hasMessageClass = true;
1102            userDataCompressed = false;
1103
1104            if (0 == (mDataCodingScheme & 0x04)) {
1105                // GSM 7 bit default alphabet
1106                encodingType = ENCODING_7BIT;
1107            } else {
1108                // 8 bit data
1109                encodingType = ENCODING_8BIT;
1110            }
1111        } else if ((mDataCodingScheme & 0xF0) == 0xC0
1112                || (mDataCodingScheme & 0xF0) == 0xD0
1113                || (mDataCodingScheme & 0xF0) == 0xE0) {
1114            // 3GPP TS 23.038 V7.0.0 (2006-03) section 4
1115
1116            // 0xC0 == 7 bit, don't store
1117            // 0xD0 == 7 bit, store
1118            // 0xE0 == UCS-2, store
1119
1120            if ((mDataCodingScheme & 0xF0) == 0xE0) {
1121                encodingType = ENCODING_16BIT;
1122            } else {
1123                encodingType = ENCODING_7BIT;
1124            }
1125
1126            userDataCompressed = false;
1127            boolean active = ((mDataCodingScheme & 0x08) == 0x08);
1128
1129            // bit 0x04 reserved
1130
1131            if ((mDataCodingScheme & 0x03) == 0x00) {
1132                mIsMwi = true;
1133                mMwiSense = active;
1134                mMwiDontStore = ((mDataCodingScheme & 0xF0) == 0xC0);
1135            } else {
1136                mIsMwi = false;
1137
1138                Rlog.w(LOG_TAG, "MWI for fax, email, or other "
1139                        + (mDataCodingScheme & 0xff));
1140            }
1141        } else if ((mDataCodingScheme & 0xC0) == 0x80) {
1142            // 3GPP TS 23.038 V7.0.0 (2006-03) section 4
1143            // 0x80..0xBF == Reserved coding groups
1144            if (mDataCodingScheme == 0x84) {
1145                // This value used for KSC5601 by carriers in Korea.
1146                encodingType = ENCODING_KSC5601;
1147            } else {
1148                Rlog.w(LOG_TAG, "5 - Unsupported SMS data coding scheme "
1149                        + (mDataCodingScheme & 0xff));
1150            }
1151        } else {
1152            Rlog.w(LOG_TAG, "3 - Unsupported SMS data coding scheme "
1153                    + (mDataCodingScheme & 0xff));
1154        }
1155
1156        // set both the user data and the user data header.
1157        int count = p.constructUserData(hasUserDataHeader,
1158                encodingType == ENCODING_7BIT);
1159        this.mUserData = p.getUserData();
1160        this.mUserDataHeader = p.getUserDataHeader();
1161
1162        switch (encodingType) {
1163        case ENCODING_UNKNOWN:
1164        case ENCODING_8BIT:
1165            mMessageBody = null;
1166            break;
1167
1168        case ENCODING_7BIT:
1169            mMessageBody = p.getUserDataGSM7Bit(count,
1170                    hasUserDataHeader ? mUserDataHeader.languageTable : 0,
1171                    hasUserDataHeader ? mUserDataHeader.languageShiftTable : 0);
1172            break;
1173
1174        case ENCODING_16BIT:
1175            mMessageBody = p.getUserDataUCS2(count);
1176            break;
1177
1178        case ENCODING_KSC5601:
1179            mMessageBody = p.getUserDataKSC5601(count);
1180            break;
1181        }
1182
1183        if (VDBG) Rlog.v(LOG_TAG, "SMS message body (raw): '" + mMessageBody + "'");
1184
1185        if (mMessageBody != null) {
1186            parseMessageBody();
1187        }
1188
1189        if (!hasMessageClass) {
1190            messageClass = MessageClass.UNKNOWN;
1191        } else {
1192            switch (mDataCodingScheme & 0x3) {
1193            case 0:
1194                messageClass = MessageClass.CLASS_0;
1195                break;
1196            case 1:
1197                messageClass = MessageClass.CLASS_1;
1198                break;
1199            case 2:
1200                messageClass = MessageClass.CLASS_2;
1201                break;
1202            case 3:
1203                messageClass = MessageClass.CLASS_3;
1204                break;
1205            }
1206        }
1207    }
1208
1209    /**
1210     * {@inheritDoc}
1211     */
1212    @Override
1213    public MessageClass getMessageClass() {
1214        return messageClass;
1215    }
1216
1217    /**
1218     * Returns true if this is a (U)SIM data download type SM.
1219     * See 3GPP TS 31.111 section 9.1 and TS 23.040 section 9.2.3.9.
1220     *
1221     * @return true if this is a USIM data download message; false otherwise
1222     */
1223    boolean isUsimDataDownload() {
1224        return messageClass == MessageClass.CLASS_2 &&
1225                (mProtocolIdentifier == 0x7f || mProtocolIdentifier == 0x7c);
1226    }
1227}
1228