Home | History | Annotate | Download | only in sms
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.internal.telephony.cdma.sms;
     18 
     19 import android.util.SparseIntArray;
     20 
     21 import com.android.internal.telephony.SmsHeader;
     22 import com.android.internal.util.HexDump;
     23 
     24 public class UserData {
     25 
     26     /**
     27      * User data encoding types.
     28      * (See 3GPP2 C.R1001-F, v1.0, table 9.1-1)
     29      */
     30     public static final int ENCODING_OCTET                      = 0x00;
     31     public static final int ENCODING_IS91_EXTENDED_PROTOCOL     = 0x01;
     32     public static final int ENCODING_7BIT_ASCII                 = 0x02;
     33     public static final int ENCODING_IA5                        = 0x03;
     34     public static final int ENCODING_UNICODE_16                 = 0x04;
     35     public static final int ENCODING_SHIFT_JIS                  = 0x05;
     36     public static final int ENCODING_KOREAN                     = 0x06;
     37     public static final int ENCODING_LATIN_HEBREW               = 0x07;
     38     public static final int ENCODING_LATIN                      = 0x08;
     39     public static final int ENCODING_GSM_7BIT_ALPHABET          = 0x09;
     40     public static final int ENCODING_GSM_DCS                    = 0x0A;
     41 
     42     /**
     43      * IS-91 message types.
     44      * (See TIA/EIS/IS-91-A-ENGL 1999, table 3.7.1.1-3)
     45      */
     46     public static final int IS91_MSG_TYPE_VOICEMAIL_STATUS   = 0x82;
     47     public static final int IS91_MSG_TYPE_SHORT_MESSAGE_FULL = 0x83;
     48     public static final int IS91_MSG_TYPE_CLI                = 0x84;
     49     public static final int IS91_MSG_TYPE_SHORT_MESSAGE      = 0x85;
     50 
     51     /**
     52      * US ASCII character mapping table.
     53      *
     54      * This table contains only the printable ASCII characters, with a
     55      * 0x20 offset, meaning that the ASCII SPACE character is at index
     56      * 0, with the resulting code of 0x20.
     57      *
     58      * Note this mapping is also equivalent to that used by both the
     59      * IA5 and the IS-91 encodings.  For the former this is defined
     60      * using CCITT Rec. T.50 Tables 1 and 3.  For the latter IS 637 B,
     61      * Table 4.3.1.4.1-1 -- and note the encoding uses only 6 bits,
     62      * and hence only maps entries up to the '_' character.
     63      *
     64      */
     65     public static final char[] ASCII_MAP = {
     66         ' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
     67         '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
     68         '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
     69         'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
     70         '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
     71         'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~'};
     72 
     73     /**
     74      * Character to use when forced to encode otherwise unencodable
     75      * characters, meaning those not in the respective ASCII or GSM
     76      * 7-bit encoding tables.  Current choice is SPACE, which is 0x20
     77      * in both the GSM-7bit and ASCII-7bit encodings.
     78      */
     79     static final byte UNENCODABLE_7_BIT_CHAR = 0x20;
     80 
     81     /**
     82      * Only elements between these indices in the ASCII table are printable.
     83      */
     84     public static final int PRINTABLE_ASCII_MIN_INDEX = 0x20;
     85     public static final int ASCII_NL_INDEX = 0x0A;
     86     public static final int ASCII_CR_INDEX = 0x0D;
     87     public static final SparseIntArray charToAscii = new SparseIntArray();
     88     static {
     89         for (int i = 0; i < ASCII_MAP.length; i++) {
     90             charToAscii.put(ASCII_MAP[i], PRINTABLE_ASCII_MIN_INDEX + i);
     91         }
     92         charToAscii.put('\n', ASCII_NL_INDEX);
     93         charToAscii.put('\r', ASCII_CR_INDEX);
     94     }
     95 
     96     /*
     97      * TODO(cleanup): Move this very generic functionality somewhere
     98      * more general.
     99      */
    100     /**
    101      * Given a string generate a corresponding ASCII-encoded byte
    102      * array, but limited to printable characters.  If the input
    103      * contains unprintable characters, return null.
    104      */
    105     public static byte[] stringToAscii(String str) {
    106         int len = str.length();
    107         byte[] result = new byte[len];
    108         for (int i = 0; i < len; i++) {
    109             int charCode = charToAscii.get(str.charAt(i), -1);
    110             if (charCode == -1) return null;
    111             result[i] = (byte)charCode;
    112         }
    113         return result;
    114     }
    115 
    116     /**
    117      * Mapping for ASCII values less than 32 are flow control signals
    118      * and not used here.
    119      */
    120     public static final int ASCII_MAP_BASE_INDEX = 0x20;
    121     public static final int ASCII_MAP_MAX_INDEX = ASCII_MAP_BASE_INDEX + ASCII_MAP.length - 1;
    122 
    123     /**
    124      * Contains the data header of the user data
    125      */
    126     public SmsHeader userDataHeader;
    127 
    128     /**
    129      * Contains the data encoding type for the SMS message
    130      */
    131     public int msgEncoding;
    132     public boolean msgEncodingSet = false;
    133 
    134     public int msgType;
    135 
    136     /**
    137      * Number of invalid bits in the last byte of data.
    138      */
    139     public int paddingBits;
    140 
    141     public int numFields;
    142 
    143     /**
    144      * Contains the user data of a SMS message
    145      * (See 3GPP2 C.S0015-B, v2, 4.5.2)
    146      */
    147     public byte[] payload;
    148     public String payloadStr;
    149 
    150     @Override
    151     public String toString() {
    152         StringBuilder builder = new StringBuilder();
    153         builder.append("UserData ");
    154         builder.append("{ msgEncoding=" + (msgEncodingSet ? msgEncoding : "unset"));
    155         builder.append(", msgType=" + msgType);
    156         builder.append(", paddingBits=" + paddingBits);
    157         builder.append(", numFields=" + numFields);
    158         builder.append(", userDataHeader=" + userDataHeader);
    159         builder.append(", payload='" + HexDump.toHexString(payload) + "'");
    160         builder.append(", payloadStr='" + payloadStr + "'");
    161         builder.append(" }");
    162         return builder.toString();
    163     }
    164 
    165 }
    166