UserData.java revision 4980bf4aff8d49ac4e05444a6ef40ea1536f1afb
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
17package com.android.internal.telephony.cdma.sms;
18
19import android.util.SparseIntArray;
20
21import com.android.internal.telephony.SmsHeader;
22import com.android.internal.util.HexDump;
23
24public 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     * User data message type encoding types.
44     * (See 3GPP2 C.S0015-B, 4.5.2 and 3GPP 23.038, Section 4)
45     */
46    public static final int ENCODING_GSM_DCS_7BIT               = 0x00;
47    public static final int ENCODING_GSM_DCS_8BIT               = 0x01;
48    public static final int ENCODING_GSM_DCS_16BIT              = 0x02;
49
50    /**
51     * IS-91 message types.
52     * (See TIA/EIS/IS-91-A-ENGL 1999, table 3.7.1.1-3)
53     */
54    public static final int IS91_MSG_TYPE_VOICEMAIL_STATUS   = 0x82;
55    public static final int IS91_MSG_TYPE_SHORT_MESSAGE_FULL = 0x83;
56    public static final int IS91_MSG_TYPE_CLI                = 0x84;
57    public static final int IS91_MSG_TYPE_SHORT_MESSAGE      = 0x85;
58
59    /**
60     * US ASCII character mapping table.
61     *
62     * This table contains only the printable ASCII characters, with a
63     * 0x20 offset, meaning that the ASCII SPACE character is at index
64     * 0, with the resulting code of 0x20.
65     *
66     * Note this mapping is also equivalent to that used by both the
67     * IA5 and the IS-91 encodings.  For the former this is defined
68     * using CCITT Rec. T.50 Tables 1 and 3.  For the latter IS 637 B,
69     * Table 4.3.1.4.1-1 -- and note the encoding uses only 6 bits,
70     * and hence only maps entries up to the '_' character.
71     *
72     */
73    public static final char[] ASCII_MAP = {
74        ' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
75        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?',
76        '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
77        'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', '\\', ']', '^', '_',
78        '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
79        'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~'};
80
81    /**
82     * Character to use when forced to encode otherwise unencodable
83     * characters, meaning those not in the respective ASCII or GSM
84     * 7-bit encoding tables.  Current choice is SPACE, which is 0x20
85     * in both the GSM-7bit and ASCII-7bit encodings.
86     */
87    static final byte UNENCODABLE_7_BIT_CHAR = 0x20;
88
89    /**
90     * Only elements between these indices in the ASCII table are printable.
91     */
92    public static final int PRINTABLE_ASCII_MIN_INDEX = 0x20;
93    public static final int ASCII_NL_INDEX = 0x0A;
94    public static final int ASCII_CR_INDEX = 0x0D;
95    public static final SparseIntArray charToAscii = new SparseIntArray();
96    static {
97        for (int i = 0; i < ASCII_MAP.length; i++) {
98            charToAscii.put(ASCII_MAP[i], PRINTABLE_ASCII_MIN_INDEX + i);
99        }
100        charToAscii.put('\n', ASCII_NL_INDEX);
101        charToAscii.put('\r', ASCII_CR_INDEX);
102    }
103
104    /*
105     * TODO(cleanup): Move this very generic functionality somewhere
106     * more general.
107     */
108    /**
109     * Given a string generate a corresponding ASCII-encoded byte
110     * array, but limited to printable characters.  If the input
111     * contains unprintable characters, return null.
112     */
113    public static byte[] stringToAscii(String str) {
114        int len = str.length();
115        byte[] result = new byte[len];
116        for (int i = 0; i < len; i++) {
117            int charCode = charToAscii.get(str.charAt(i), -1);
118            if (charCode == -1) return null;
119            result[i] = (byte)charCode;
120        }
121        return result;
122    }
123
124    /**
125     * Mapping for ASCII values less than 32 are flow control signals
126     * and not used here.
127     */
128    public static final int ASCII_MAP_BASE_INDEX = 0x20;
129    public static final int ASCII_MAP_MAX_INDEX = ASCII_MAP_BASE_INDEX + ASCII_MAP.length - 1;
130
131    /**
132     * Contains the data header of the user data
133     */
134    public SmsHeader userDataHeader;
135
136    /**
137     * Contains the data encoding type for the SMS message
138     */
139    public int msgEncoding;
140    public boolean msgEncodingSet = false;
141
142    public int msgType;
143
144    /**
145     * Number of invalid bits in the last byte of data.
146     */
147    public int paddingBits;
148
149    public int numFields;
150
151    /**
152     * Contains the user data of a SMS message
153     * (See 3GPP2 C.S0015-B, v2, 4.5.2)
154     */
155    public byte[] payload;
156    public String payloadStr;
157
158    @Override
159    public String toString() {
160        StringBuilder builder = new StringBuilder();
161        builder.append("UserData ");
162        builder.append("{ msgEncoding=" + (msgEncodingSet ? msgEncoding : "unset"));
163        builder.append(", msgType=" + msgType);
164        builder.append(", paddingBits=" + paddingBits);
165        builder.append(", numFields=" + numFields);
166        builder.append(", userDataHeader=" + userDataHeader);
167        builder.append(", payload='" + HexDump.toHexString(payload) + "'");
168        builder.append(", payloadStr='" + payloadStr + "'");
169        builder.append(" }");
170        return builder.toString();
171    }
172
173}
174