CdmaSMSDispatcher.java revision ea803b8e0cc613309d0ce096df9c13e9690243ff
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; 18 19 20import android.app.Activity; 21import android.app.PendingIntent; 22import android.app.PendingIntent.CanceledException; 23import android.content.ContentValues; 24import android.content.Intent; 25import android.content.SharedPreferences; 26import android.database.Cursor; 27import android.database.SQLException; 28import android.os.AsyncResult; 29import android.os.Message; 30import android.os.SystemProperties; 31import android.provider.Telephony; 32import android.provider.Telephony.Sms.Intents; 33import android.preference.PreferenceManager; 34import android.util.Config; 35import android.util.Log; 36import android.telephony.SmsManager; 37import android.telephony.SmsMessage.MessageClass; 38 39import com.android.internal.telephony.TelephonyProperties; 40import com.android.internal.telephony.CommandsInterface; 41import com.android.internal.telephony.SmsHeader; 42import com.android.internal.telephony.SmsMessageBase; 43import com.android.internal.telephony.SMSDispatcher; 44import com.android.internal.telephony.cdma.SmsMessage; 45import com.android.internal.telephony.cdma.sms.SmsEnvelope; 46import com.android.internal.telephony.cdma.sms.UserData; 47import com.android.internal.util.HexDump; 48 49import java.io.ByteArrayOutputStream; 50import java.util.ArrayList; 51import java.util.Arrays; 52import java.util.HashMap; 53import java.lang.Boolean; 54 55 56final class CdmaSMSDispatcher extends SMSDispatcher { 57 private static final String TAG = "CDMA"; 58 59 private CDMAPhone mCdmaPhone; 60 61 private byte[] mLastDispatchedSmsFingerprint; 62 private byte[] mLastAcknowledgedSmsFingerprint; 63 64 CdmaSMSDispatcher(CDMAPhone phone) { 65 super(phone); 66 mCdmaPhone = phone; 67 } 68 69 /** 70 * Called when a status report is received. This should correspond to 71 * a previously successful SEND. 72 * Is a special GSM function, should never be called in CDMA!! 73 * 74 * @param ar AsyncResult passed into the message handler. ar.result should 75 * be a String representing the status report PDU, as ASCII hex. 76 */ 77 protected void handleStatusReport(AsyncResult ar) { 78 Log.d(TAG, "handleStatusReport is a special GSM function, should never be called in CDMA!"); 79 } 80 81 private void handleCdmaStatusReport(SmsMessage sms) { 82 for (int i = 0, count = deliveryPendingList.size(); i < count; i++) { 83 SmsTracker tracker = deliveryPendingList.get(i); 84 if (tracker.mMessageRef == sms.messageRef) { 85 // Found it. Remove from list and broadcast. 86 deliveryPendingList.remove(i); 87 PendingIntent intent = tracker.mDeliveryIntent; 88 Intent fillIn = new Intent(); 89 fillIn.putExtra("pdu", sms.getPdu()); 90 try { 91 intent.send(mContext, Activity.RESULT_OK, fillIn); 92 } catch (CanceledException ex) {} 93 break; // Only expect to see one tracker matching this message. 94 } 95 } 96 } 97 98 /** {@inheritDoc} */ 99 protected int dispatchMessage(SmsMessageBase smsb) { 100 101 // If sms is null, means there was a parsing error. 102 if (smsb == null) { 103 return Intents.RESULT_SMS_GENERIC_ERROR; 104 } 105 106 String inEcm=SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE, "false"); 107 if (inEcm.equals("true")) { 108 return Activity.RESULT_OK; 109 } 110 111 // See if we have a network duplicate SMS. 112 SmsMessage sms = (SmsMessage) smsb; 113 mLastDispatchedSmsFingerprint = sms.getIncomingSmsFingerprint(); 114 if (mLastAcknowledgedSmsFingerprint != null && 115 Arrays.equals(mLastDispatchedSmsFingerprint, mLastAcknowledgedSmsFingerprint)) { 116 return Intents.RESULT_SMS_HANDLED; 117 } 118 // Decode BD stream and set sms variables. 119 sms.parseSms(); 120 int teleService = sms.getTeleService(); 121 boolean handled = false; 122 123 if ((SmsEnvelope.TELESERVICE_VMN == teleService) || 124 (SmsEnvelope.TELESERVICE_MWI == teleService)) { 125 // handling Voicemail 126 int voicemailCount = sms.getNumOfVoicemails(); 127 Log.d(TAG, "Voicemail count=" + voicemailCount); 128 // Store the voicemail count in preferences. 129 SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences( 130 ((CDMAPhone) mPhone).getContext()); 131 SharedPreferences.Editor editor = sp.edit(); 132 editor.putInt(CDMAPhone.VM_COUNT_CDMA, voicemailCount); 133 editor.commit(); 134 ((CDMAPhone) mPhone).updateMessageWaitingIndicator(voicemailCount); 135 handled = true; 136 } else if (((SmsEnvelope.TELESERVICE_WMT == teleService) || 137 (SmsEnvelope.TELESERVICE_WEMT == teleService)) && 138 sms.isStatusReportMessage()) { 139 handleCdmaStatusReport(sms); 140 handled = true; 141 } else if ((sms.getUserData() == null)) { 142 if (Config.LOGD) { 143 Log.d(TAG, "Received SMS without user data"); 144 } 145 handled = true; 146 } 147 148 if (handled) { 149 return Intents.RESULT_SMS_HANDLED; 150 } 151 152 if (!mStorageAvailable && (sms.getMessageClass() != MessageClass.CLASS_0)) { 153 // It's a storable message and there's no storage available. Bail. 154 // (See C.S0015-B v2.0 for a description of "Immediate Display" 155 // messages, which we represent as CLASS_0.) 156 return Intents.RESULT_SMS_OUT_OF_MEMORY; 157 } 158 159 if (SmsEnvelope.TELESERVICE_WAP == teleService) { 160 return processCdmaWapPdu(sms.getUserData(), sms.messageRef, 161 sms.getOriginatingAddress()); 162 } 163 164 // Reject (NAK) any messages with teleservice ids that have 165 // not yet been handled and also do not correspond to the two 166 // kinds that are processed below. 167 if ((SmsEnvelope.TELESERVICE_WMT != teleService) && 168 (SmsEnvelope.TELESERVICE_WEMT != teleService)) { 169 return Intents.RESULT_SMS_UNSUPPORTED; 170 } 171 172 /* 173 * TODO(cleanup): Why are we using a getter method for this 174 * (and for so many other sms fields)? Trivial getters and 175 * setters like this are direct violations of the style guide. 176 * If the purpose is to protect agaist writes (by not 177 * providing a setter) then any protection is illusory (and 178 * hence bad) for cases where the values are not primitives, 179 * such as this call for the header. Since this is an issue 180 * with the public API it cannot be changed easily, but maybe 181 * something can be done eventually. 182 */ 183 SmsHeader smsHeader = sms.getUserDataHeader(); 184 185 /* 186 * TODO(cleanup): Since both CDMA and GSM use the same header 187 * format, this dispatch processing is naturally identical, 188 * and code should probably not be replicated explicitly. 189 */ 190 191 // See if message is partial or port addressed. 192 if ((smsHeader == null) || (smsHeader.concatRef == null)) { 193 // Message is not partial (not part of concatenated sequence). 194 byte[][] pdus = new byte[1][]; 195 pdus[0] = sms.getPdu(); 196 197 if (smsHeader != null && smsHeader.portAddrs != null) { 198 if (smsHeader.portAddrs.destPort == SmsHeader.PORT_WAP_PUSH) { 199 // GSM-style WAP indication 200 return mWapPush.dispatchWapPdu(sms.getUserData()); 201 } else { 202 // The message was sent to a port, so concoct a URI for it. 203 dispatchPortAddressedPdus(pdus, smsHeader.portAddrs.destPort); 204 } 205 } else { 206 // Normal short and non-port-addressed message, dispatch it. 207 dispatchPdus(pdus); 208 } 209 return Activity.RESULT_OK; 210 } else { 211 // Process the message part. 212 return processMessagePart(sms, smsHeader.concatRef, smsHeader.portAddrs); 213 } 214 } 215 216 /** 217 * Processes inbound messages that are in the WAP-WDP PDU format. See 218 * wap-259-wdp-20010614-a section 6.5 for details on the WAP-WDP PDU format. 219 * WDP segments are gathered until a datagram completes and gets dispatched. 220 * 221 * @param pdu The WAP-WDP PDU segment 222 * @return a result code from {@link Telephony.Sms.Intents}, or 223 * {@link Activity#RESULT_OK} if the message has been broadcast 224 * to applications 225 */ 226 protected int processCdmaWapPdu(byte[] pdu, int referenceNumber, String address) { 227 int segment; 228 int totalSegments; 229 int index = 0; 230 int msgType; 231 232 int sourcePort = 0; 233 int destinationPort = 0; 234 235 msgType = pdu[index++]; 236 if (msgType != 0){ 237 Log.w(TAG, "Received a WAP SMS which is not WDP. Discard."); 238 return Intents.RESULT_SMS_HANDLED; 239 } 240 totalSegments = pdu[index++]; // >=1 241 segment = pdu[index++]; // >=0 242 243 // Only the first segment contains sourcePort and destination Port 244 if (segment == 0) { 245 //process WDP segment 246 sourcePort = (0xFF & pdu[index++]) << 8; 247 sourcePort |= 0xFF & pdu[index++]; 248 destinationPort = (0xFF & pdu[index++]) << 8; 249 destinationPort |= 0xFF & pdu[index++]; 250 } 251 252 // Lookup all other related parts 253 StringBuilder where = new StringBuilder("reference_number ="); 254 where.append(referenceNumber); 255 where.append(" AND address = ?"); 256 String[] whereArgs = new String[] {address}; 257 258 Log.i(TAG, "Received WAP PDU. Type = " + msgType + ", originator = " + address 259 + ", src-port = " + sourcePort + ", dst-port = " + destinationPort 260 + ", ID = " + referenceNumber + ", segment# = " + segment + "/" + totalSegments); 261 262 byte[][] pdus = null; 263 Cursor cursor = null; 264 try { 265 cursor = mResolver.query(mRawUri, RAW_PROJECTION, where.toString(), whereArgs, null); 266 int cursorCount = cursor.getCount(); 267 if (cursorCount != totalSegments - 1) { 268 // We don't have all the parts yet, store this one away 269 ContentValues values = new ContentValues(); 270 values.put("date", new Long(0)); 271 values.put("pdu", HexDump.toHexString(pdu, index, pdu.length - index)); 272 values.put("address", address); 273 values.put("reference_number", referenceNumber); 274 values.put("count", totalSegments); 275 values.put("sequence", segment); 276 values.put("destination_port", destinationPort); 277 278 mResolver.insert(mRawUri, values); 279 280 return Intents.RESULT_SMS_HANDLED; 281 } 282 283 // All the parts are in place, deal with them 284 int pduColumn = cursor.getColumnIndex("pdu"); 285 int sequenceColumn = cursor.getColumnIndex("sequence"); 286 287 pdus = new byte[totalSegments][]; 288 for (int i = 0; i < cursorCount; i++) { 289 cursor.moveToNext(); 290 int cursorSequence = (int)cursor.getLong(sequenceColumn); 291 // Read the destination port from the first segment 292 if (cursorSequence == 0) { 293 int destinationPortColumn = cursor.getColumnIndex("destination_port"); 294 destinationPort = (int)cursor.getLong(destinationPortColumn); 295 } 296 pdus[cursorSequence] = HexDump.hexStringToByteArray( 297 cursor.getString(pduColumn)); 298 } 299 // The last part will be added later 300 301 // Remove the parts from the database 302 mResolver.delete(mRawUri, where.toString(), whereArgs); 303 } catch (SQLException e) { 304 Log.e(TAG, "Can't access multipart SMS database", e); 305 return Intents.RESULT_SMS_GENERIC_ERROR; 306 } finally { 307 if (cursor != null) cursor.close(); 308 } 309 310 // Build up the data stream 311 ByteArrayOutputStream output = new ByteArrayOutputStream(); 312 for (int i = 0; i < totalSegments; i++) { 313 // reassemble the (WSP-)pdu 314 if (i == segment) { 315 // This one isn't in the DB, so add it 316 output.write(pdu, index, pdu.length - index); 317 } else { 318 output.write(pdus[i], 0, pdus[i].length); 319 } 320 } 321 322 byte[] datagram = output.toByteArray(); 323 // Dispatch the PDU to applications 324 switch (destinationPort) { 325 case SmsHeader.PORT_WAP_PUSH: 326 // Handle the PUSH 327 return mWapPush.dispatchWapPdu(datagram); 328 329 default:{ 330 pdus = new byte[1][]; 331 pdus[0] = datagram; 332 // The messages were sent to any other WAP port 333 dispatchPortAddressedPdus(pdus, destinationPort); 334 return Activity.RESULT_OK; 335 } 336 } 337 } 338 339 /** {@inheritDoc} */ 340 protected void sendData(String destAddr, String scAddr, int destPort, 341 byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent) { 342 SmsMessage.SubmitPdu pdu = SmsMessage.getSubmitPdu( 343 scAddr, destAddr, destPort, data, (deliveryIntent != null)); 344 sendSubmitPdu(pdu, sentIntent, deliveryIntent); 345 } 346 347 /** {@inheritDoc} */ 348 protected void sendText(String destAddr, String scAddr, String text, 349 PendingIntent sentIntent, PendingIntent deliveryIntent) { 350 SmsMessage.SubmitPdu pdu = SmsMessage.getSubmitPdu( 351 scAddr, destAddr, text, (deliveryIntent != null), null); 352 sendSubmitPdu(pdu, sentIntent, deliveryIntent); 353 } 354 355 /** {@inheritDoc} */ 356 protected void sendMultipartText(String destAddr, String scAddr, 357 ArrayList<String> parts, ArrayList<PendingIntent> sentIntents, 358 ArrayList<PendingIntent> deliveryIntents) { 359 360 /** 361 * TODO(cleanup): There is no real code difference between 362 * this and the GSM version, and hence it should be moved to 363 * the base class or consolidated somehow, provided calling 364 * the proper submitpdu stuff can be arranged. 365 */ 366 367 int refNumber = getNextConcatenatedRef() & 0x00FF; 368 369 for (int i = 0, msgCount = parts.size(); i < msgCount; i++) { 370 SmsHeader.ConcatRef concatRef = new SmsHeader.ConcatRef(); 371 concatRef.refNumber = refNumber; 372 concatRef.seqNumber = i + 1; // 1-based sequence 373 concatRef.msgCount = msgCount; 374 concatRef.isEightBits = true; 375 SmsHeader smsHeader = new SmsHeader(); 376 smsHeader.concatRef = concatRef; 377 378 PendingIntent sentIntent = null; 379 if (sentIntents != null && sentIntents.size() > i) { 380 sentIntent = sentIntents.get(i); 381 } 382 383 PendingIntent deliveryIntent = null; 384 if (deliveryIntents != null && deliveryIntents.size() > i) { 385 deliveryIntent = deliveryIntents.get(i); 386 } 387 388 UserData uData = new UserData(); 389 uData.payloadStr = parts.get(i); 390 uData.userDataHeader = smsHeader; 391 392 /* By setting the statusReportRequested bit only for the 393 * last message fragment, this will result in only one 394 * callback to the sender when that last fragment delivery 395 * has been acknowledged. */ 396 SmsMessage.SubmitPdu submitPdu = SmsMessage.getSubmitPdu(destAddr, 397 uData, (deliveryIntent != null) && (i == (msgCount - 1))); 398 399 sendSubmitPdu(submitPdu, sentIntent, deliveryIntent); 400 } 401 } 402 403 protected void sendSubmitPdu(SmsMessage.SubmitPdu pdu, 404 PendingIntent sentIntent, PendingIntent deliveryIntent) { 405 if (SystemProperties.getBoolean(TelephonyProperties.PROPERTY_INECM_MODE, false)) { 406 if (sentIntent != null) { 407 try { 408 sentIntent.send(SmsManager.RESULT_ERROR_NO_SERVICE); 409 } catch (CanceledException ex) {} 410 } 411 if (Config.LOGD) { 412 Log.d(TAG, "Block SMS in Emergency Callback mode"); 413 } 414 return; 415 } 416 sendRawPdu(pdu.encodedScAddress, pdu.encodedMessage, sentIntent, deliveryIntent); 417 } 418 419 /** {@inheritDoc} */ 420 protected void sendSms(SmsTracker tracker) { 421 HashMap map = tracker.mData; 422 423 byte smsc[] = (byte[]) map.get("smsc"); 424 byte pdu[] = (byte[]) map.get("pdu"); 425 426 Message reply = obtainMessage(EVENT_SEND_SMS_COMPLETE, tracker); 427 428 mCm.sendCdmaSms(pdu, reply); 429 } 430 431 /** {@inheritDoc} */ 432 protected void sendMultipartSms (SmsTracker tracker) { 433 Log.d(TAG, "TODO: CdmaSMSDispatcher.sendMultipartSms not implemented"); 434 } 435 436 /** {@inheritDoc} */ 437 protected void acknowledgeLastIncomingSms(boolean success, int result, Message response){ 438 // FIXME unit test leaves cm == null. this should change 439 440 String inEcm=SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE, "false"); 441 if (inEcm.equals("true")) { 442 return; 443 } 444 445 if (mCm != null) { 446 int causeCode = resultToCause(result); 447 mCm.acknowledgeLastIncomingCdmaSms(success, causeCode, response); 448 449 if (causeCode == 0) { 450 mLastAcknowledgedSmsFingerprint = mLastDispatchedSmsFingerprint; 451 } 452 mLastDispatchedSmsFingerprint = null; 453 } 454 } 455 456 /** {@inheritDoc} */ 457 protected void activateCellBroadcastSms(int activate, Message response) { 458 mCm.setCdmaBroadcastActivation((activate == 0), response); 459 } 460 461 /** {@inheritDoc} */ 462 protected void getCellBroadcastSmsConfig(Message response) { 463 mCm.getCdmaBroadcastConfig(response); 464 } 465 466 /** {@inheritDoc} */ 467 protected void setCellBroadcastConfig(int[] configValuesArray, Message response) { 468 mCm.setCdmaBroadcastConfig(configValuesArray, response); 469 } 470 471 private int resultToCause(int rc) { 472 switch (rc) { 473 case Activity.RESULT_OK: 474 case Intents.RESULT_SMS_HANDLED: 475 // Cause code is ignored on success. 476 return 0; 477 case Intents.RESULT_SMS_OUT_OF_MEMORY: 478 return CommandsInterface.CDMA_SMS_FAIL_CAUSE_RESOURCE_SHORTAGE; 479 case Intents.RESULT_SMS_UNSUPPORTED: 480 return CommandsInterface.CDMA_SMS_FAIL_CAUSE_INVALID_TELESERVICE_ID; 481 case Intents.RESULT_SMS_GENERIC_ERROR: 482 default: 483 return CommandsInterface.CDMA_SMS_FAIL_CAUSE_OTHER_TERMINAL_PROBLEM; 484 } 485 } 486} 487