1 /* 2 * Copyright (C) 2006 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; 18 19 import com.android.internal.telephony.SmsConstants; 20 import com.android.internal.util.HexDump; 21 22 import java.io.ByteArrayInputStream; 23 import java.io.ByteArrayOutputStream; 24 25 import java.util.ArrayList; 26 27 /** 28 * SMS user data header, as specified in TS 23.040 9.2.3.24. 29 */ 30 public class SmsHeader { 31 32 // TODO(cleanup): this data structure is generally referred to as 33 // the 'user data header' or UDH, and so the class name should 34 // change to reflect this... 35 36 /** SMS user data header information element identifiers. 37 * (see TS 23.040 9.2.3.24) 38 */ 39 public static final int ELT_ID_CONCATENATED_8_BIT_REFERENCE = 0x00; 40 public static final int ELT_ID_SPECIAL_SMS_MESSAGE_INDICATION = 0x01; 41 public static final int ELT_ID_APPLICATION_PORT_ADDRESSING_8_BIT = 0x04; 42 public static final int ELT_ID_APPLICATION_PORT_ADDRESSING_16_BIT = 0x05; 43 public static final int ELT_ID_SMSC_CONTROL_PARAMS = 0x06; 44 public static final int ELT_ID_UDH_SOURCE_INDICATION = 0x07; 45 public static final int ELT_ID_CONCATENATED_16_BIT_REFERENCE = 0x08; 46 public static final int ELT_ID_WIRELESS_CTRL_MSG_PROTOCOL = 0x09; 47 public static final int ELT_ID_TEXT_FORMATTING = 0x0A; 48 public static final int ELT_ID_PREDEFINED_SOUND = 0x0B; 49 public static final int ELT_ID_USER_DEFINED_SOUND = 0x0C; 50 public static final int ELT_ID_PREDEFINED_ANIMATION = 0x0D; 51 public static final int ELT_ID_LARGE_ANIMATION = 0x0E; 52 public static final int ELT_ID_SMALL_ANIMATION = 0x0F; 53 public static final int ELT_ID_LARGE_PICTURE = 0x10; 54 public static final int ELT_ID_SMALL_PICTURE = 0x11; 55 public static final int ELT_ID_VARIABLE_PICTURE = 0x12; 56 public static final int ELT_ID_USER_PROMPT_INDICATOR = 0x13; 57 public static final int ELT_ID_EXTENDED_OBJECT = 0x14; 58 public static final int ELT_ID_REUSED_EXTENDED_OBJECT = 0x15; 59 public static final int ELT_ID_COMPRESSION_CONTROL = 0x16; 60 public static final int ELT_ID_OBJECT_DISTR_INDICATOR = 0x17; 61 public static final int ELT_ID_STANDARD_WVG_OBJECT = 0x18; 62 public static final int ELT_ID_CHARACTER_SIZE_WVG_OBJECT = 0x19; 63 public static final int ELT_ID_EXTENDED_OBJECT_DATA_REQUEST_CMD = 0x1A; 64 public static final int ELT_ID_RFC_822_EMAIL_HEADER = 0x20; 65 public static final int ELT_ID_HYPERLINK_FORMAT_ELEMENT = 0x21; 66 public static final int ELT_ID_REPLY_ADDRESS_ELEMENT = 0x22; 67 public static final int ELT_ID_ENHANCED_VOICE_MAIL_INFORMATION = 0x23; 68 public static final int ELT_ID_NATIONAL_LANGUAGE_SINGLE_SHIFT = 0x24; 69 public static final int ELT_ID_NATIONAL_LANGUAGE_LOCKING_SHIFT = 0x25; 70 71 public static final int PORT_WAP_PUSH = 2948; 72 public static final int PORT_WAP_WSP = 9200; 73 74 public static class PortAddrs { 75 public int destPort; 76 public int origPort; 77 public boolean areEightBits; 78 } 79 80 public static class ConcatRef { 81 public int refNumber; 82 public int seqNumber; 83 public int msgCount; 84 public boolean isEightBits; 85 } 86 87 /** 88 * A header element that is not explicitly parsed, meaning not 89 * PortAddrs or ConcatRef. 90 */ 91 public static class MiscElt { 92 public int id; 93 public byte[] data; 94 } 95 96 public PortAddrs portAddrs; 97 public ConcatRef concatRef; 98 public ArrayList<MiscElt> miscEltList = new ArrayList<MiscElt>(); 99 100 /** 7 bit national language locking shift table, or 0 for GSM default 7 bit alphabet. */ 101 public int languageTable; 102 103 /** 7 bit national language single shift table, or 0 for GSM default 7 bit extension table. */ 104 public int languageShiftTable; 105 106 public SmsHeader() {} 107 108 /** 109 * Create structured SmsHeader object from serialized byte array representation. 110 * (see TS 23.040 9.2.3.24) 111 * @param data is user data header bytes 112 * @return SmsHeader object 113 */ 114 public static SmsHeader fromByteArray(byte[] data) { 115 ByteArrayInputStream inStream = new ByteArrayInputStream(data); 116 SmsHeader smsHeader = new SmsHeader(); 117 while (inStream.available() > 0) { 118 /** 119 * NOTE: as defined in the spec, ConcatRef and PortAddr 120 * fields should not reoccur, but if they do the last 121 * occurrence is to be used. Also, for ConcatRef 122 * elements, if the count is zero, sequence is zero, or 123 * sequence is larger than count, the entire element is to 124 * be ignored. 125 */ 126 int id = inStream.read(); 127 int length = inStream.read(); 128 ConcatRef concatRef; 129 PortAddrs portAddrs; 130 switch (id) { 131 case ELT_ID_CONCATENATED_8_BIT_REFERENCE: 132 concatRef = new ConcatRef(); 133 concatRef.refNumber = inStream.read(); 134 concatRef.msgCount = inStream.read(); 135 concatRef.seqNumber = inStream.read(); 136 concatRef.isEightBits = true; 137 if (concatRef.msgCount != 0 && concatRef.seqNumber != 0 && 138 concatRef.seqNumber <= concatRef.msgCount) { 139 smsHeader.concatRef = concatRef; 140 } 141 break; 142 case ELT_ID_CONCATENATED_16_BIT_REFERENCE: 143 concatRef = new ConcatRef(); 144 concatRef.refNumber = (inStream.read() << 8) | inStream.read(); 145 concatRef.msgCount = inStream.read(); 146 concatRef.seqNumber = inStream.read(); 147 concatRef.isEightBits = false; 148 if (concatRef.msgCount != 0 && concatRef.seqNumber != 0 && 149 concatRef.seqNumber <= concatRef.msgCount) { 150 smsHeader.concatRef = concatRef; 151 } 152 break; 153 case ELT_ID_APPLICATION_PORT_ADDRESSING_8_BIT: 154 portAddrs = new PortAddrs(); 155 portAddrs.destPort = inStream.read(); 156 portAddrs.origPort = inStream.read(); 157 portAddrs.areEightBits = true; 158 smsHeader.portAddrs = portAddrs; 159 break; 160 case ELT_ID_APPLICATION_PORT_ADDRESSING_16_BIT: 161 portAddrs = new PortAddrs(); 162 portAddrs.destPort = (inStream.read() << 8) | inStream.read(); 163 portAddrs.origPort = (inStream.read() << 8) | inStream.read(); 164 portAddrs.areEightBits = false; 165 smsHeader.portAddrs = portAddrs; 166 break; 167 case ELT_ID_NATIONAL_LANGUAGE_SINGLE_SHIFT: 168 smsHeader.languageShiftTable = inStream.read(); 169 break; 170 case ELT_ID_NATIONAL_LANGUAGE_LOCKING_SHIFT: 171 smsHeader.languageTable = inStream.read(); 172 break; 173 default: 174 MiscElt miscElt = new MiscElt(); 175 miscElt.id = id; 176 miscElt.data = new byte[length]; 177 inStream.read(miscElt.data, 0, length); 178 smsHeader.miscEltList.add(miscElt); 179 } 180 } 181 return smsHeader; 182 } 183 184 /** 185 * Create serialized byte array representation from structured SmsHeader object. 186 * (see TS 23.040 9.2.3.24) 187 * @return Byte array representing the SmsHeader 188 */ 189 public static byte[] toByteArray(SmsHeader smsHeader) { 190 if ((smsHeader.portAddrs == null) && 191 (smsHeader.concatRef == null) && 192 (smsHeader.miscEltList.isEmpty()) && 193 (smsHeader.languageShiftTable == 0) && 194 (smsHeader.languageTable == 0)) { 195 return null; 196 } 197 198 ByteArrayOutputStream outStream = 199 new ByteArrayOutputStream(SmsConstants.MAX_USER_DATA_BYTES); 200 ConcatRef concatRef = smsHeader.concatRef; 201 if (concatRef != null) { 202 if (concatRef.isEightBits) { 203 outStream.write(ELT_ID_CONCATENATED_8_BIT_REFERENCE); 204 outStream.write(3); 205 outStream.write(concatRef.refNumber); 206 } else { 207 outStream.write(ELT_ID_CONCATENATED_16_BIT_REFERENCE); 208 outStream.write(4); 209 outStream.write(concatRef.refNumber >>> 8); 210 outStream.write(concatRef.refNumber & 0x00FF); 211 } 212 outStream.write(concatRef.msgCount); 213 outStream.write(concatRef.seqNumber); 214 } 215 PortAddrs portAddrs = smsHeader.portAddrs; 216 if (portAddrs != null) { 217 if (portAddrs.areEightBits) { 218 outStream.write(ELT_ID_APPLICATION_PORT_ADDRESSING_8_BIT); 219 outStream.write(2); 220 outStream.write(portAddrs.destPort); 221 outStream.write(portAddrs.origPort); 222 } else { 223 outStream.write(ELT_ID_APPLICATION_PORT_ADDRESSING_16_BIT); 224 outStream.write(4); 225 outStream.write(portAddrs.destPort >>> 8); 226 outStream.write(portAddrs.destPort & 0x00FF); 227 outStream.write(portAddrs.origPort >>> 8); 228 outStream.write(portAddrs.origPort & 0x00FF); 229 } 230 } 231 if (smsHeader.languageShiftTable != 0) { 232 outStream.write(ELT_ID_NATIONAL_LANGUAGE_SINGLE_SHIFT); 233 outStream.write(1); 234 outStream.write(smsHeader.languageShiftTable); 235 } 236 if (smsHeader.languageTable != 0) { 237 outStream.write(ELT_ID_NATIONAL_LANGUAGE_LOCKING_SHIFT); 238 outStream.write(1); 239 outStream.write(smsHeader.languageTable); 240 } 241 for (MiscElt miscElt : smsHeader.miscEltList) { 242 outStream.write(miscElt.id); 243 outStream.write(miscElt.data.length); 244 outStream.write(miscElt.data, 0, miscElt.data.length); 245 } 246 return outStream.toByteArray(); 247 } 248 249 @Override 250 public String toString() { 251 StringBuilder builder = new StringBuilder(); 252 builder.append("UserDataHeader "); 253 builder.append("{ ConcatRef "); 254 if (concatRef == null) { 255 builder.append("unset"); 256 } else { 257 builder.append("{ refNumber=" + concatRef.refNumber); 258 builder.append(", msgCount=" + concatRef.msgCount); 259 builder.append(", seqNumber=" + concatRef.seqNumber); 260 builder.append(", isEightBits=" + concatRef.isEightBits); 261 builder.append(" }"); 262 } 263 builder.append(", PortAddrs "); 264 if (portAddrs == null) { 265 builder.append("unset"); 266 } else { 267 builder.append("{ destPort=" + portAddrs.destPort); 268 builder.append(", origPort=" + portAddrs.origPort); 269 builder.append(", areEightBits=" + portAddrs.areEightBits); 270 builder.append(" }"); 271 } 272 if (languageShiftTable != 0) { 273 builder.append(", languageShiftTable=" + languageShiftTable); 274 } 275 if (languageTable != 0) { 276 builder.append(", languageTable=" + languageTable); 277 } 278 for (MiscElt miscElt : miscEltList) { 279 builder.append(", MiscElt "); 280 builder.append("{ id=" + miscElt.id); 281 builder.append(", length=" + miscElt.data.length); 282 builder.append(", data=" + HexDump.toHexString(miscElt.data)); 283 builder.append(" }"); 284 } 285 builder.append(" }"); 286 return builder.toString(); 287 } 288 289 } 290