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