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