Move DataConnection related classes to their own package.
[android/platform/frameworks/opt/telephony.git] / src / java / com / android / internal / telephony / RIL.java
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 static com.android.internal.telephony.RILConstants.*;
20 import static android.telephony.TelephonyManager.NETWORK_TYPE_UNKNOWN;
21 import static android.telephony.TelephonyManager.NETWORK_TYPE_EDGE;
22 import static android.telephony.TelephonyManager.NETWORK_TYPE_GPRS;
23 import static android.telephony.TelephonyManager.NETWORK_TYPE_UMTS;
24 import static android.telephony.TelephonyManager.NETWORK_TYPE_HSDPA;
25 import static android.telephony.TelephonyManager.NETWORK_TYPE_HSUPA;
26 import static android.telephony.TelephonyManager.NETWORK_TYPE_HSPA;
27
28 import android.content.BroadcastReceiver;
29 import android.content.Context;
30 import android.content.Intent;
31 import android.content.IntentFilter;
32 import android.net.ConnectivityManager;
33 import android.net.LocalSocket;
34 import android.net.LocalSocketAddress;
35 import android.os.AsyncResult;
36 import android.os.Handler;
37 import android.os.HandlerThread;
38 import android.os.Looper;
39 import android.os.Message;
40 import android.os.Parcel;
41 import android.os.PowerManager;
42 import android.os.SystemProperties;
43 import android.os.PowerManager.WakeLock;
44 import android.telephony.NeighboringCellInfo;
45 import android.telephony.PhoneNumberUtils;
46 import android.telephony.Rlog;
47 import android.telephony.SignalStrength;
48 import android.telephony.SmsManager;
49 import android.telephony.SmsMessage;
50 import android.text.TextUtils;
51
52 import com.android.internal.telephony.gsm.SmsBroadcastConfigInfo;
53 import com.android.internal.telephony.gsm.SuppServiceNotification;
54 import com.android.internal.telephony.uicc.IccCardApplicationStatus;
55 import com.android.internal.telephony.uicc.IccCardStatus;
56 import com.android.internal.telephony.uicc.IccIoResult;
57 import com.android.internal.telephony.uicc.IccRefreshResponse;
58 import com.android.internal.telephony.uicc.IccUtils;
59 import com.android.internal.telephony.cdma.CdmaCallWaitingNotification;
60 import com.android.internal.telephony.cdma.CdmaInformationRecords;
61 import com.android.internal.telephony.cdma.CdmaSmsBroadcastConfigInfo;
62 import com.android.internal.telephony.dataconnection.DataConnection;
63
64 import java.io.ByteArrayInputStream;
65 import java.io.DataInputStream;
66 import java.io.FileDescriptor;
67 import java.io.IOException;
68 import java.io.InputStream;
69 import java.io.PrintWriter;
70 import java.util.ArrayList;
71 import java.util.Collections;
72 import java.util.concurrent.atomic.AtomicBoolean;
73
74 /**
75  * {@hide}
76  */
77 class RILRequest {
78     static final String LOG_TAG = "RILJ";
79
80     //***** Class Variables
81     static int sNextSerial = 0;
82     static Object sSerialMonitor = new Object();
83     private static Object sPoolSync = new Object();
84     private static RILRequest sPool = null;
85     private static int sPoolSize = 0;
86     private static final int MAX_POOL_SIZE = 4;
87
88     //***** Instance Variables
89     int mSerial;
90     int mRequest;
91     Message mResult;
92     Parcel mp;
93     RILRequest mNext;
94
95     /**
96      * Retrieves a new RILRequest instance from the pool.
97      *
98      * @param request RIL_REQUEST_*
99      * @param result sent when operation completes
100      * @return a RILRequest instance from the pool.
101      */
102     static RILRequest obtain(int request, Message result) {
103         RILRequest rr = null;
104
105         synchronized(sPoolSync) {
106             if (sPool != null) {
107                 rr = sPool;
108                 sPool = rr.mNext;
109                 rr.mNext = null;
110                 sPoolSize--;
111             }
112         }
113
114         if (rr == null) {
115             rr = new RILRequest();
116         }
117
118         synchronized(sSerialMonitor) {
119             rr.mSerial = sNextSerial++;
120         }
121         rr.mRequest = request;
122         rr.mResult = result;
123         rr.mp = Parcel.obtain();
124
125         if (result != null && result.getTarget() == null) {
126             throw new NullPointerException("Message target must not be null");
127         }
128
129         // first elements in any RIL Parcel
130         rr.mp.writeInt(request);
131         rr.mp.writeInt(rr.mSerial);
132
133         return rr;
134     }
135
136     /**
137      * Returns a RILRequest instance to the pool.
138      *
139      * Note: This should only be called once per use.
140      */
141     void release() {
142         synchronized (sPoolSync) {
143             if (sPoolSize < MAX_POOL_SIZE) {
144                 this.mNext = sPool;
145                 sPool = this;
146                 sPoolSize++;
147                 mResult = null;
148             }
149         }
150     }
151
152     private RILRequest() {
153     }
154
155     static void
156     resetSerial() {
157         synchronized(sSerialMonitor) {
158             sNextSerial = 0;
159         }
160     }
161
162     String
163     serialString() {
164         //Cheesy way to do %04d
165         StringBuilder sb = new StringBuilder(8);
166         String sn;
167
168         sn = Integer.toString(mSerial);
169
170         //sb.append("J[");
171         sb.append('[');
172         for (int i = 0, s = sn.length() ; i < 4 - s; i++) {
173             sb.append('0');
174         }
175
176         sb.append(sn);
177         sb.append(']');
178         return sb.toString();
179     }
180
181     void
182     onError(int error, Object ret) {
183         CommandException ex;
184
185         ex = CommandException.fromRilErrno(error);
186
187         if (RIL.RILJ_LOGD) Rlog.d(LOG_TAG, serialString() + "< "
188             + RIL.requestToString(mRequest)
189             + " error: " + ex);
190
191         if (mResult != null) {
192             AsyncResult.forMessage(mResult, ret, ex);
193             mResult.sendToTarget();
194         }
195
196         if (mp != null) {
197             mp.recycle();
198             mp = null;
199         }
200     }
201 }
202
203
204 /**
205  * RIL implementation of the CommandsInterface.
206  *
207  * {@hide}
208  */
209 public final class RIL extends BaseCommands implements CommandsInterface {
210     static final String RILJ_LOG_TAG = "RILJ";
211     static final boolean RILJ_LOGD = true;
212     static final boolean RILJ_LOGV = false; // STOP SHIP if true
213
214     /**
215      * Wake lock timeout should be longer than the longest timeout in
216      * the vendor ril.
217      */
218     private static final int DEFAULT_WAKE_LOCK_TIMEOUT = 60000;
219
220     //***** Instance Variables
221
222     LocalSocket mSocket;
223     HandlerThread mSenderThread;
224     RILSender mSender;
225     Thread mReceiverThread;
226     RILReceiver mReceiver;
227     WakeLock mWakeLock;
228     int mWakeLockTimeout;
229     // The number of requests pending to be sent out, it increases before calling
230     // EVENT_SEND and decreases while handling EVENT_SEND. It gets cleared while
231     // WAKE_LOCK_TIMEOUT occurs.
232     int mRequestMessagesPending;
233     // The number of requests sent out but waiting for response. It increases while
234     // sending request and decreases while handling response. It should match
235     // mRequestList.size() unless there are requests no replied while
236     // WAKE_LOCK_TIMEOUT occurs.
237     int mRequestMessagesWaiting;
238
239     //I'd rather this be LinkedList or something
240     ArrayList<RILRequest> mRequestList = new ArrayList<RILRequest>();
241
242     Object     mLastNITZTimeInfo;
243
244     // When we are testing emergency calls
245     AtomicBoolean mTestingEmergencyCall = new AtomicBoolean(false);
246
247     //***** Events
248
249     static final int EVENT_SEND                 = 1;
250     static final int EVENT_WAKE_LOCK_TIMEOUT    = 2;
251
252     //***** Constants
253
254     // match with constant in ril.cpp
255     static final int RIL_MAX_COMMAND_BYTES = (8 * 1024);
256     static final int RESPONSE_SOLICITED = 0;
257     static final int RESPONSE_UNSOLICITED = 1;
258
259     static final String SOCKET_NAME_RIL = "rild";
260
261     static final int SOCKET_OPEN_RETRY_MILLIS = 4 * 1000;
262
263     // The number of the required config values for broadcast SMS stored in the C struct
264     // RIL_CDMA_BroadcastServiceInfo
265     private static final int CDMA_BSI_NO_OF_INTS_STRUCT = 3;
266
267     private static final int CDMA_BROADCAST_SMS_NO_OF_SERVICE_CATEGORIES = 31;
268
269     BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
270         @Override
271         public void onReceive(Context context, Intent intent) {
272             if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
273                 sendScreenState(true);
274             } else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
275                 sendScreenState(false);
276             } else {
277                 Rlog.w(RILJ_LOG_TAG, "RIL received unexpected Intent: " + intent.getAction());
278             }
279         }
280     };
281
282     class RILSender extends Handler implements Runnable {
283         public RILSender(Looper looper) {
284             super(looper);
285         }
286
287         // Only allocated once
288         byte[] dataLength = new byte[4];
289
290         //***** Runnable implementation
291         public void
292         run() {
293             //setup if needed
294         }
295
296
297         //***** Handler implementation
298         @Override public void
299         handleMessage(Message msg) {
300             RILRequest rr = (RILRequest)(msg.obj);
301             RILRequest req = null;
302
303             switch (msg.what) {
304                 case EVENT_SEND:
305                     /**
306                      * mRequestMessagePending++ already happened for every
307                      * EVENT_SEND, thus we must make sure
308                      * mRequestMessagePending-- happens once and only once
309                      */
310                     boolean alreadySubtracted = false;
311                     try {
312                         LocalSocket s;
313
314                         s = mSocket;
315
316                         if (s == null) {
317                             rr.onError(RADIO_NOT_AVAILABLE, null);
318                             rr.release();
319                             if (mRequestMessagesPending > 0)
320                                 mRequestMessagesPending--;
321                             alreadySubtracted = true;
322                             return;
323                         }
324
325                         synchronized (mRequestList) {
326                             mRequestList.add(rr);
327                             mRequestMessagesWaiting++;
328                         }
329
330                         if (mRequestMessagesPending > 0)
331                             mRequestMessagesPending--;
332                         alreadySubtracted = true;
333
334                         byte[] data;
335
336                         data = rr.mp.marshall();
337                         rr.mp.recycle();
338                         rr.mp = null;
339
340                         if (data.length > RIL_MAX_COMMAND_BYTES) {
341                             throw new RuntimeException(
342                                     "Parcel larger than max bytes allowed! "
343                                                           + data.length);
344                         }
345
346                         // parcel length in big endian
347                         dataLength[0] = dataLength[1] = 0;
348                         dataLength[2] = (byte)((data.length >> 8) & 0xff);
349                         dataLength[3] = (byte)((data.length) & 0xff);
350
351                         //Rlog.v(RILJ_LOG_TAG, "writing packet: " + data.length + " bytes");
352
353                         s.getOutputStream().write(dataLength);
354                         s.getOutputStream().write(data);
355                     } catch (IOException ex) {
356                         Rlog.e(RILJ_LOG_TAG, "IOException", ex);
357                         req = findAndRemoveRequestFromList(rr.mSerial);
358                         // make sure this request has not already been handled,
359                         // eg, if RILReceiver cleared the list.
360                         if (req != null || !alreadySubtracted) {
361                             rr.onError(RADIO_NOT_AVAILABLE, null);
362                             rr.release();
363                         }
364                     } catch (RuntimeException exc) {
365                         Rlog.e(RILJ_LOG_TAG, "Uncaught exception ", exc);
366                         req = findAndRemoveRequestFromList(rr.mSerial);
367                         // make sure this request has not already been handled,
368                         // eg, if RILReceiver cleared the list.
369                         if (req != null || !alreadySubtracted) {
370                             rr.onError(GENERIC_FAILURE, null);
371                             rr.release();
372                         }
373                     } finally {
374                         // Note: We are "Done" only if there are no outstanding
375                         // requests or replies. Thus this code path will only release
376                         // the wake lock on errors.
377                         releaseWakeLockIfDone();
378                     }
379
380                     if (!alreadySubtracted && mRequestMessagesPending > 0) {
381                         mRequestMessagesPending--;
382                     }
383
384                     break;
385
386                 case EVENT_WAKE_LOCK_TIMEOUT:
387                     // Haven't heard back from the last request.  Assume we're
388                     // not getting a response and  release the wake lock.
389                     synchronized (mWakeLock) {
390                         if (mWakeLock.isHeld()) {
391                             // The timer of WAKE_LOCK_TIMEOUT is reset with each
392                             // new send request. So when WAKE_LOCK_TIMEOUT occurs
393                             // all requests in mRequestList already waited at
394                             // least DEFAULT_WAKE_LOCK_TIMEOUT but no response.
395                             // Reset mRequestMessagesWaiting to enable
396                             // releaseWakeLockIfDone().
397                             //
398                             // Note: Keep mRequestList so that delayed response
399                             // can still be handled when response finally comes.
400                             if (mRequestMessagesWaiting != 0) {
401                                 Rlog.d(RILJ_LOG_TAG, "NOTE: mReqWaiting is NOT 0 but"
402                                         + mRequestMessagesWaiting + " at TIMEOUT, reset!"
403                                         + " There still msg waitng for response");
404
405                                 mRequestMessagesWaiting = 0;
406
407                                 if (RILJ_LOGD) {
408                                     synchronized (mRequestList) {
409                                         int count = mRequestList.size();
410                                         Rlog.d(RILJ_LOG_TAG, "WAKE_LOCK_TIMEOUT " +
411                                                 " mRequestList=" + count);
412
413                                         for (int i = 0; i < count; i++) {
414                                             rr = mRequestList.get(i);
415                                             Rlog.d(RILJ_LOG_TAG, i + ": [" + rr.mSerial + "] "
416                                                     + requestToString(rr.mRequest));
417                                         }
418                                     }
419                                 }
420                             }
421                             // mRequestMessagesPending shows how many
422                             // requests are waiting to be sent (and before
423                             // to be added in request list) since star the
424                             // WAKE_LOCK_TIMEOUT timer. Since WAKE_LOCK_TIMEOUT
425                             // is the expected time to get response, all requests
426                             // should already sent out (i.e.
427                             // mRequestMessagesPending is 0 )while TIMEOUT occurs.
428                             if (mRequestMessagesPending != 0) {
429                                 Rlog.e(RILJ_LOG_TAG, "ERROR: mReqPending is NOT 0 but"
430                                         + mRequestMessagesPending + " at TIMEOUT, reset!");
431                                 mRequestMessagesPending = 0;
432
433                             }
434                             mWakeLock.release();
435                         }
436                     }
437                     break;
438             }
439         }
440     }
441
442     /**
443      * Reads in a single RIL message off the wire. A RIL message consists
444      * of a 4-byte little-endian length and a subsequent series of bytes.
445      * The final message (length header omitted) is read into
446      * <code>buffer</code> and the length of the final message (less header)
447      * is returned. A return value of -1 indicates end-of-stream.
448      *
449      * @param is non-null; Stream to read from
450      * @param buffer Buffer to fill in. Must be as large as maximum
451      * message size, or an ArrayOutOfBounds exception will be thrown.
452      * @return Length of message less header, or -1 on end of stream.
453      * @throws IOException
454      */
455     private static int readRilMessage(InputStream is, byte[] buffer)
456             throws IOException {
457         int countRead;
458         int offset;
459         int remaining;
460         int messageLength;
461
462         // First, read in the length of the message
463         offset = 0;
464         remaining = 4;
465         do {
466             countRead = is.read(buffer, offset, remaining);
467
468             if (countRead < 0 ) {
469                 Rlog.e(RILJ_LOG_TAG, "Hit EOS reading message length");
470                 return -1;
471             }
472
473             offset += countRead;
474             remaining -= countRead;
475         } while (remaining > 0);
476
477         messageLength = ((buffer[0] & 0xff) << 24)
478                 | ((buffer[1] & 0xff) << 16)
479                 | ((buffer[2] & 0xff) << 8)
480                 | (buffer[3] & 0xff);
481
482         // Then, re-use the buffer and read in the message itself
483         offset = 0;
484         remaining = messageLength;
485         do {
486             countRead = is.read(buffer, offset, remaining);
487
488             if (countRead < 0 ) {
489                 Rlog.e(RILJ_LOG_TAG, "Hit EOS reading message.  messageLength=" + messageLength
490                         + " remaining=" + remaining);
491                 return -1;
492             }
493
494             offset += countRead;
495             remaining -= countRead;
496         } while (remaining > 0);
497
498         return messageLength;
499     }
500
501     class RILReceiver implements Runnable {
502         byte[] buffer;
503
504         RILReceiver() {
505             buffer = new byte[RIL_MAX_COMMAND_BYTES];
506         }
507
508         public void
509         run() {
510             int retryCount = 0;
511
512             try {for (;;) {
513                 LocalSocket s = null;
514                 LocalSocketAddress l;
515
516                 try {
517                     s = new LocalSocket();
518                     l = new LocalSocketAddress(SOCKET_NAME_RIL,
519                             LocalSocketAddress.Namespace.RESERVED);
520                     s.connect(l);
521                 } catch (IOException ex){
522                     try {
523                         if (s != null) {
524                             s.close();
525                         }
526                     } catch (IOException ex2) {
527                         //ignore failure to close after failure to connect
528                     }
529
530                     // don't print an error message after the the first time
531                     // or after the 8th time
532
533                     if (retryCount == 8) {
534                         Rlog.e (RILJ_LOG_TAG,
535                             "Couldn't find '" + SOCKET_NAME_RIL
536                             + "' socket after " + retryCount
537                             + " times, continuing to retry silently");
538                     } else if (retryCount > 0 && retryCount < 8) {
539                         Rlog.i (RILJ_LOG_TAG,
540                             "Couldn't find '" + SOCKET_NAME_RIL
541                             + "' socket; retrying after timeout");
542                     }
543
544                     try {
545                         Thread.sleep(SOCKET_OPEN_RETRY_MILLIS);
546                     } catch (InterruptedException er) {
547                     }
548
549                     retryCount++;
550                     continue;
551                 }
552
553                 retryCount = 0;
554
555                 mSocket = s;
556                 Rlog.i(RILJ_LOG_TAG, "Connected to '" + SOCKET_NAME_RIL + "' socket");
557
558                 int length = 0;
559                 try {
560                     InputStream is = mSocket.getInputStream();
561
562                     for (;;) {
563                         Parcel p;
564
565                         length = readRilMessage(is, buffer);
566
567                         if (length < 0) {
568                             // End-of-stream reached
569                             break;
570                         }
571
572                         p = Parcel.obtain();
573                         p.unmarshall(buffer, 0, length);
574                         p.setDataPosition(0);
575
576                         //Rlog.v(RILJ_LOG_TAG, "Read packet: " + length + " bytes");
577
578                         processResponse(p);
579                         p.recycle();
580                     }
581                 } catch (java.io.IOException ex) {
582                     Rlog.i(RILJ_LOG_TAG, "'" + SOCKET_NAME_RIL + "' socket closed",
583                           ex);
584                 } catch (Throwable tr) {
585                     Rlog.e(RILJ_LOG_TAG, "Uncaught exception read length=" + length +
586                         "Exception:" + tr.toString());
587                 }
588
589                 Rlog.i(RILJ_LOG_TAG, "Disconnected from '" + SOCKET_NAME_RIL
590                       + "' socket");
591
592                 setRadioState (RadioState.RADIO_UNAVAILABLE);
593
594                 try {
595                     mSocket.close();
596                 } catch (IOException ex) {
597                 }
598
599                 mSocket = null;
600                 RILRequest.resetSerial();
601
602                 // Clear request list on close
603                 clearRequestList(RADIO_NOT_AVAILABLE, false);
604             }} catch (Throwable tr) {
605                 Rlog.e(RILJ_LOG_TAG,"Uncaught exception", tr);
606             }
607
608             /* We're disconnected so we don't know the ril version */
609             notifyRegistrantsRilConnectionChanged(-1);
610         }
611     }
612
613
614
615     //***** Constructors
616
617     public RIL(Context context, int preferredNetworkType, int cdmaSubscription) {
618         super(context);
619         if (RILJ_LOGD) {
620             riljLog("RIL(context, preferredNetworkType=" + preferredNetworkType +
621                     " cdmaSubscription=" + cdmaSubscription + ")");
622         }
623         mCdmaSubscription  = cdmaSubscription;
624         mPreferredNetworkType = preferredNetworkType;
625         mPhoneType = RILConstants.NO_PHONE;
626
627         PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
628         mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, RILJ_LOG_TAG);
629         mWakeLock.setReferenceCounted(false);
630         mWakeLockTimeout = SystemProperties.getInt(TelephonyProperties.PROPERTY_WAKE_LOCK_TIMEOUT,
631                 DEFAULT_WAKE_LOCK_TIMEOUT);
632         mRequestMessagesPending = 0;
633         mRequestMessagesWaiting = 0;
634
635         mSenderThread = new HandlerThread("RILSender");
636         mSenderThread.start();
637
638         Looper looper = mSenderThread.getLooper();
639         mSender = new RILSender(looper);
640
641         ConnectivityManager cm = (ConnectivityManager)context.getSystemService(
642                 Context.CONNECTIVITY_SERVICE);
643         if (cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE) == false) {
644             riljLog("Not starting RILReceiver: wifi-only");
645         } if (SystemProperties.getBoolean("ro.radio.noril", false)) {
646             riljLog("Not starting RILReceiver: basebandless target");
647         } else {
648             riljLog("Starting RILReceiver");
649             mReceiver = new RILReceiver();
650             mReceiverThread = new Thread(mReceiver, "RILReceiver");
651             mReceiverThread.start();
652
653             IntentFilter filter = new IntentFilter();
654             filter.addAction(Intent.ACTION_SCREEN_ON);
655             filter.addAction(Intent.ACTION_SCREEN_OFF);
656             context.registerReceiver(mIntentReceiver, filter);
657         }
658     }
659
660     //***** CommandsInterface implementation
661
662     public void getVoiceRadioTechnology(Message result) {
663         RILRequest rr = RILRequest.obtain(RIL_REQUEST_VOICE_RADIO_TECH, result);
664
665         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
666
667         send(rr);
668     }
669
670
671     @Override public void
672     setOnNITZTime(Handler h, int what, Object obj) {
673         super.setOnNITZTime(h, what, obj);
674
675         // Send the last NITZ time if we have it
676         if (mLastNITZTimeInfo != null) {
677             mNITZTimeRegistrant
678                 .notifyRegistrant(
679                     new AsyncResult (null, mLastNITZTimeInfo, null));
680             mLastNITZTimeInfo = null;
681         }
682     }
683
684     public void
685     getIccCardStatus(Message result) {
686         //Note: This RIL request has not been renamed to ICC,
687         //       but this request is also valid for SIM and RUIM
688         RILRequest rr = RILRequest.obtain(RIL_REQUEST_GET_SIM_STATUS, result);
689
690         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
691
692         send(rr);
693     }
694
695     @Override public void
696     supplyIccPin(String pin, Message result) {
697         supplyIccPinForApp(pin, null, result);
698     }
699
700     @Override public void
701     supplyIccPinForApp(String pin, String aid, Message result) {
702         //Note: This RIL request has not been renamed to ICC,
703         //       but this request is also valid for SIM and RUIM
704         RILRequest rr = RILRequest.obtain(RIL_REQUEST_ENTER_SIM_PIN, result);
705
706         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
707
708         rr.mp.writeInt(2);
709         rr.mp.writeString(pin);
710         rr.mp.writeString(aid);
711
712         send(rr);
713     }
714
715     @Override public void
716     supplyIccPuk(String puk, String newPin, Message result) {
717         supplyIccPukForApp(puk, newPin, null, result);
718     }
719
720     @Override public void
721     supplyIccPukForApp(String puk, String newPin, String aid, Message result) {
722         //Note: This RIL request has not been renamed to ICC,
723         //       but this request is also valid for SIM and RUIM
724         RILRequest rr = RILRequest.obtain(RIL_REQUEST_ENTER_SIM_PUK, result);
725
726         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
727
728         rr.mp.writeInt(3);
729         rr.mp.writeString(puk);
730         rr.mp.writeString(newPin);
731         rr.mp.writeString(aid);
732
733         send(rr);
734     }
735
736     @Override public void
737     supplyIccPin2(String pin, Message result) {
738         supplyIccPin2ForApp(pin, null, result);
739     }
740
741     @Override public void
742     supplyIccPin2ForApp(String pin, String aid, Message result) {
743         //Note: This RIL request has not been renamed to ICC,
744         //       but this request is also valid for SIM and RUIM
745         RILRequest rr = RILRequest.obtain(RIL_REQUEST_ENTER_SIM_PIN2, result);
746
747         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
748
749         rr.mp.writeInt(2);
750         rr.mp.writeString(pin);
751         rr.mp.writeString(aid);
752
753         send(rr);
754     }
755
756     @Override public void
757     supplyIccPuk2(String puk2, String newPin2, Message result) {
758         supplyIccPuk2ForApp(puk2, newPin2, null, result);
759     }
760
761     @Override public void
762     supplyIccPuk2ForApp(String puk, String newPin2, String aid, Message result) {
763         //Note: This RIL request has not been renamed to ICC,
764         //       but this request is also valid for SIM and RUIM
765         RILRequest rr = RILRequest.obtain(RIL_REQUEST_ENTER_SIM_PUK2, result);
766
767         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
768
769         rr.mp.writeInt(3);
770         rr.mp.writeString(puk);
771         rr.mp.writeString(newPin2);
772         rr.mp.writeString(aid);
773
774         send(rr);
775     }
776
777     @Override public void
778     changeIccPin(String oldPin, String newPin, Message result) {
779         changeIccPinForApp(oldPin, newPin, null, result);
780     }
781
782     @Override public void
783     changeIccPinForApp(String oldPin, String newPin, String aid, Message result) {
784         //Note: This RIL request has not been renamed to ICC,
785         //       but this request is also valid for SIM and RUIM
786         RILRequest rr = RILRequest.obtain(RIL_REQUEST_CHANGE_SIM_PIN, result);
787
788         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
789
790         rr.mp.writeInt(3);
791         rr.mp.writeString(oldPin);
792         rr.mp.writeString(newPin);
793         rr.mp.writeString(aid);
794
795         send(rr);
796     }
797
798     @Override public void
799     changeIccPin2(String oldPin2, String newPin2, Message result) {
800         changeIccPin2ForApp(oldPin2, newPin2, null, result);
801     }
802
803     @Override public void
804     changeIccPin2ForApp(String oldPin2, String newPin2, String aid, Message result) {
805         //Note: This RIL request has not been renamed to ICC,
806         //       but this request is also valid for SIM and RUIM
807         RILRequest rr = RILRequest.obtain(RIL_REQUEST_CHANGE_SIM_PIN2, result);
808
809         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
810
811         rr.mp.writeInt(3);
812         rr.mp.writeString(oldPin2);
813         rr.mp.writeString(newPin2);
814         rr.mp.writeString(aid);
815
816         send(rr);
817     }
818
819     public void
820     changeBarringPassword(String facility, String oldPwd, String newPwd, Message result) {
821         RILRequest rr = RILRequest.obtain(RIL_REQUEST_CHANGE_BARRING_PASSWORD, result);
822
823         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
824
825         rr.mp.writeInt(3);
826         rr.mp.writeString(facility);
827         rr.mp.writeString(oldPwd);
828         rr.mp.writeString(newPwd);
829
830         send(rr);
831     }
832
833     public void
834     supplyNetworkDepersonalization(String netpin, Message result) {
835         RILRequest rr = RILRequest.obtain(RIL_REQUEST_ENTER_NETWORK_DEPERSONALIZATION, result);
836
837         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
838
839         rr.mp.writeInt(1);
840         rr.mp.writeString(netpin);
841
842         send(rr);
843     }
844
845     public void
846     getCurrentCalls (Message result) {
847         RILRequest rr = RILRequest.obtain(RIL_REQUEST_GET_CURRENT_CALLS, result);
848
849         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
850
851         send(rr);
852     }
853
854     @Deprecated public void
855     getPDPContextList(Message result) {
856         getDataCallList(result);
857     }
858
859     public void
860     getDataCallList(Message result) {
861         RILRequest rr = RILRequest.obtain(RIL_REQUEST_DATA_CALL_LIST, result);
862
863         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
864
865         send(rr);
866     }
867
868     public void
869     dial (String address, int clirMode, Message result) {
870         dial(address, clirMode, null, result);
871     }
872
873     public void
874     dial(String address, int clirMode, UUSInfo uusInfo, Message result) {
875         RILRequest rr = RILRequest.obtain(RIL_REQUEST_DIAL, result);
876
877         rr.mp.writeString(address);
878         rr.mp.writeInt(clirMode);
879         rr.mp.writeInt(0); // UUS information is absent
880
881         if (uusInfo == null) {
882             rr.mp.writeInt(0); // UUS information is absent
883         } else {
884             rr.mp.writeInt(1); // UUS information is present
885             rr.mp.writeInt(uusInfo.getType());
886             rr.mp.writeInt(uusInfo.getDcs());
887             rr.mp.writeByteArray(uusInfo.getUserData());
888         }
889
890         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
891
892         send(rr);
893     }
894
895     public void
896     getIMSI(Message result) {
897         getIMSIForApp(null, result);
898     }
899
900     public void
901     getIMSIForApp(String aid, Message result) {
902         RILRequest rr = RILRequest.obtain(RIL_REQUEST_GET_IMSI, result);
903
904         rr.mp.writeInt(1);
905         rr.mp.writeString(aid);
906
907         if (RILJ_LOGD) riljLog(rr.serialString() +
908                               "> getIMSI: " + requestToString(rr.mRequest)
909                               + " aid: " + aid);
910
911         send(rr);
912     }
913
914     public void
915     getIMEI(Message result) {
916         RILRequest rr = RILRequest.obtain(RIL_REQUEST_GET_IMEI, result);
917
918         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
919
920         send(rr);
921     }
922
923     public void
924     getIMEISV(Message result) {
925         RILRequest rr = RILRequest.obtain(RIL_REQUEST_GET_IMEISV, result);
926
927         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
928
929         send(rr);
930     }
931
932
933     public void
934     hangupConnection (int gsmIndex, Message result) {
935         if (RILJ_LOGD) riljLog("hangupConnection: gsmIndex=" + gsmIndex);
936
937         RILRequest rr = RILRequest.obtain(RIL_REQUEST_HANGUP, result);
938
939         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest) + " " +
940                 gsmIndex);
941
942         rr.mp.writeInt(1);
943         rr.mp.writeInt(gsmIndex);
944
945         send(rr);
946     }
947
948     public void
949     hangupWaitingOrBackground (Message result) {
950         RILRequest rr = RILRequest.obtain(RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND,
951                                         result);
952
953         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
954
955         send(rr);
956     }
957
958     public void
959     hangupForegroundResumeBackground (Message result) {
960         RILRequest rr
961                 = RILRequest.obtain(
962                         RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND,
963                                         result);
964         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
965
966         send(rr);
967     }
968
969     public void
970     switchWaitingOrHoldingAndActive (Message result) {
971         RILRequest rr
972                 = RILRequest.obtain(
973                         RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE,
974                                         result);
975         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
976
977         send(rr);
978     }
979
980     public void
981     conference (Message result) {
982         RILRequest rr
983                 = RILRequest.obtain(RIL_REQUEST_CONFERENCE, result);
984
985         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
986
987         send(rr);
988     }
989
990
991     public void setPreferredVoicePrivacy(boolean enable, Message result) {
992         RILRequest rr = RILRequest.obtain(RIL_REQUEST_CDMA_SET_PREFERRED_VOICE_PRIVACY_MODE,
993                 result);
994
995         rr.mp.writeInt(1);
996         rr.mp.writeInt(enable ? 1:0);
997
998         send(rr);
999     }
1000
1001     public void getPreferredVoicePrivacy(Message result) {
1002         RILRequest rr = RILRequest.obtain(RIL_REQUEST_CDMA_QUERY_PREFERRED_VOICE_PRIVACY_MODE,
1003                 result);
1004         send(rr);
1005     }
1006
1007     public void
1008     separateConnection (int gsmIndex, Message result) {
1009         RILRequest rr
1010                 = RILRequest.obtain(RIL_REQUEST_SEPARATE_CONNECTION, result);
1011
1012         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
1013                             + " " + gsmIndex);
1014
1015         rr.mp.writeInt(1);
1016         rr.mp.writeInt(gsmIndex);
1017
1018         send(rr);
1019     }
1020
1021     public void
1022     acceptCall (Message result) {
1023         RILRequest rr
1024                 = RILRequest.obtain(RIL_REQUEST_ANSWER, result);
1025
1026         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1027
1028         send(rr);
1029     }
1030
1031     public void
1032     rejectCall (Message result) {
1033         RILRequest rr
1034                 = RILRequest.obtain(RIL_REQUEST_UDUB, result);
1035
1036         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1037
1038         send(rr);
1039     }
1040
1041     public void
1042     explicitCallTransfer (Message result) {
1043         RILRequest rr
1044                 = RILRequest.obtain(RIL_REQUEST_EXPLICIT_CALL_TRANSFER, result);
1045
1046         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1047
1048         send(rr);
1049     }
1050
1051     public void
1052     getLastCallFailCause (Message result) {
1053         RILRequest rr
1054                 = RILRequest.obtain(RIL_REQUEST_LAST_CALL_FAIL_CAUSE, result);
1055
1056         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1057
1058         send(rr);
1059     }
1060
1061     /**
1062      * @deprecated
1063      */
1064     public void
1065     getLastPdpFailCause (Message result) {
1066         getLastDataCallFailCause (result);
1067     }
1068
1069     /**
1070      * The preferred new alternative to getLastPdpFailCause
1071      */
1072     public void
1073     getLastDataCallFailCause (Message result) {
1074         RILRequest rr
1075                 = RILRequest.obtain(RIL_REQUEST_LAST_DATA_CALL_FAIL_CAUSE, result);
1076
1077         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1078
1079         send(rr);
1080     }
1081
1082     public void
1083     setMute (boolean enableMute, Message response) {
1084         RILRequest rr
1085                 = RILRequest.obtain(RIL_REQUEST_SET_MUTE, response);
1086
1087         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
1088                             + " " + enableMute);
1089
1090         rr.mp.writeInt(1);
1091         rr.mp.writeInt(enableMute ? 1 : 0);
1092
1093         send(rr);
1094     }
1095
1096     public void
1097     getMute (Message response) {
1098         RILRequest rr
1099                 = RILRequest.obtain(RIL_REQUEST_GET_MUTE, response);
1100
1101         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1102
1103         send(rr);
1104     }
1105
1106     public void
1107     getSignalStrength (Message result) {
1108         RILRequest rr
1109                 = RILRequest.obtain(RIL_REQUEST_SIGNAL_STRENGTH, result);
1110
1111         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1112
1113         send(rr);
1114     }
1115
1116     public void
1117     getVoiceRegistrationState (Message result) {
1118         RILRequest rr
1119                 = RILRequest.obtain(RIL_REQUEST_VOICE_REGISTRATION_STATE, result);
1120
1121         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1122
1123         send(rr);
1124     }
1125
1126     public void
1127     getDataRegistrationState (Message result) {
1128         RILRequest rr
1129                 = RILRequest.obtain(RIL_REQUEST_DATA_REGISTRATION_STATE, result);
1130
1131         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1132
1133         send(rr);
1134     }
1135
1136     public void
1137     getOperator(Message result) {
1138         RILRequest rr
1139                 = RILRequest.obtain(RIL_REQUEST_OPERATOR, result);
1140
1141         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1142
1143         send(rr);
1144     }
1145
1146     public void
1147     sendDtmf(char c, Message result) {
1148         RILRequest rr
1149                 = RILRequest.obtain(RIL_REQUEST_DTMF, result);
1150
1151         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1152
1153         rr.mp.writeString(Character.toString(c));
1154
1155         send(rr);
1156     }
1157
1158     public void
1159     startDtmf(char c, Message result) {
1160         RILRequest rr
1161                 = RILRequest.obtain(RIL_REQUEST_DTMF_START, result);
1162
1163         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1164
1165         rr.mp.writeString(Character.toString(c));
1166
1167         send(rr);
1168     }
1169
1170     public void
1171     stopDtmf(Message result) {
1172         RILRequest rr
1173                 = RILRequest.obtain(RIL_REQUEST_DTMF_STOP, result);
1174
1175         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1176
1177         send(rr);
1178     }
1179
1180     public void
1181     sendBurstDtmf(String dtmfString, int on, int off, Message result) {
1182         RILRequest rr = RILRequest.obtain(RIL_REQUEST_CDMA_BURST_DTMF, result);
1183
1184         rr.mp.writeInt(3);
1185         rr.mp.writeString(dtmfString);
1186         rr.mp.writeString(Integer.toString(on));
1187         rr.mp.writeString(Integer.toString(off));
1188
1189         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
1190                 + " : " + dtmfString);
1191
1192         send(rr);
1193     }
1194
1195     public void
1196     sendSMS (String smscPDU, String pdu, Message result) {
1197         RILRequest rr
1198                 = RILRequest.obtain(RIL_REQUEST_SEND_SMS, result);
1199
1200         rr.mp.writeInt(2);
1201         rr.mp.writeString(smscPDU);
1202         rr.mp.writeString(pdu);
1203
1204         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1205
1206         send(rr);
1207     }
1208
1209     public void
1210     sendCdmaSms(byte[] pdu, Message result) {
1211         int address_nbr_of_digits;
1212         int subaddr_nbr_of_digits;
1213         int bearerDataLength;
1214         ByteArrayInputStream bais = new ByteArrayInputStream(pdu);
1215         DataInputStream dis = new DataInputStream(bais);
1216
1217         RILRequest rr
1218                 = RILRequest.obtain(RIL_REQUEST_CDMA_SEND_SMS, result);
1219
1220         try {
1221             rr.mp.writeInt(dis.readInt()); //teleServiceId
1222             rr.mp.writeByte((byte) dis.readInt()); //servicePresent
1223             rr.mp.writeInt(dis.readInt()); //serviceCategory
1224             rr.mp.writeInt(dis.read()); //address_digit_mode
1225             rr.mp.writeInt(dis.read()); //address_nbr_mode
1226             rr.mp.writeInt(dis.read()); //address_ton
1227             rr.mp.writeInt(dis.read()); //address_nbr_plan
1228             address_nbr_of_digits = (byte) dis.read();
1229             rr.mp.writeByte((byte) address_nbr_of_digits);
1230             for(int i=0; i < address_nbr_of_digits; i++){
1231                 rr.mp.writeByte(dis.readByte()); // address_orig_bytes[i]
1232             }
1233             rr.mp.writeInt(dis.read()); //subaddressType
1234             rr.mp.writeByte((byte) dis.read()); //subaddr_odd
1235             subaddr_nbr_of_digits = (byte) dis.read();
1236             rr.mp.writeByte((byte) subaddr_nbr_of_digits);
1237             for(int i=0; i < subaddr_nbr_of_digits; i++){
1238                 rr.mp.writeByte(dis.readByte()); //subaddr_orig_bytes[i]
1239             }
1240
1241             bearerDataLength = dis.read();
1242             rr.mp.writeInt(bearerDataLength);
1243             for(int i=0; i < bearerDataLength; i++){
1244                 rr.mp.writeByte(dis.readByte()); //bearerData[i]
1245             }
1246         }catch (IOException ex){
1247             if (RILJ_LOGD) riljLog("sendSmsCdma: conversion from input stream to object failed: "
1248                     + ex);
1249         }
1250
1251         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1252
1253         send(rr);
1254     }
1255
1256     public void deleteSmsOnSim(int index, Message response) {
1257         RILRequest rr = RILRequest.obtain(RIL_REQUEST_DELETE_SMS_ON_SIM,
1258                 response);
1259
1260         rr.mp.writeInt(1);
1261         rr.mp.writeInt(index);
1262
1263         if (false) {
1264             if (RILJ_LOGD) riljLog(rr.serialString() + "> "
1265                     + requestToString(rr.mRequest)
1266                     + " " + index);
1267         }
1268
1269         send(rr);
1270     }
1271
1272     public void deleteSmsOnRuim(int index, Message response) {
1273         RILRequest rr = RILRequest.obtain(RIL_REQUEST_CDMA_DELETE_SMS_ON_RUIM,
1274                 response);
1275
1276         rr.mp.writeInt(1);
1277         rr.mp.writeInt(index);
1278
1279         if (false) {
1280             if (RILJ_LOGD) riljLog(rr.serialString() + "> "
1281                     + requestToString(rr.mRequest)
1282                     + " " + index);
1283         }
1284
1285         send(rr);
1286     }
1287
1288     public void writeSmsToSim(int status, String smsc, String pdu, Message response) {
1289         status = translateStatus(status);
1290
1291         RILRequest rr = RILRequest.obtain(RIL_REQUEST_WRITE_SMS_TO_SIM,
1292                 response);
1293
1294         rr.mp.writeInt(status);
1295         rr.mp.writeString(pdu);
1296         rr.mp.writeString(smsc);
1297
1298         if (false) {
1299             if (RILJ_LOGD) riljLog(rr.serialString() + "> "
1300                     + requestToString(rr.mRequest)
1301                     + " " + status);
1302         }
1303
1304         send(rr);
1305     }
1306
1307     public void writeSmsToRuim(int status, String pdu, Message response) {
1308         status = translateStatus(status);
1309
1310         RILRequest rr = RILRequest.obtain(RIL_REQUEST_CDMA_WRITE_SMS_TO_RUIM,
1311                 response);
1312
1313         rr.mp.writeInt(status);
1314         rr.mp.writeString(pdu);
1315
1316         if (false) {
1317             if (RILJ_LOGD) riljLog(rr.serialString() + "> "
1318                     + requestToString(rr.mRequest)
1319                     + " " + status);
1320         }
1321
1322         send(rr);
1323     }
1324
1325     /**
1326      *  Translates EF_SMS status bits to a status value compatible with
1327      *  SMS AT commands.  See TS 27.005 3.1.
1328      */
1329     private int translateStatus(int status) {
1330         switch(status & 0x7) {
1331             case SmsManager.STATUS_ON_ICC_READ:
1332                 return 1;
1333             case SmsManager.STATUS_ON_ICC_UNREAD:
1334                 return 0;
1335             case SmsManager.STATUS_ON_ICC_SENT:
1336                 return 3;
1337             case SmsManager.STATUS_ON_ICC_UNSENT:
1338                 return 2;
1339         }
1340
1341         // Default to READ.
1342         return 1;
1343     }
1344
1345     public void
1346     setupDataCall(String radioTechnology, String profile, String apn,
1347             String user, String password, String authType, String protocol,
1348             Message result) {
1349         RILRequest rr
1350                 = RILRequest.obtain(RIL_REQUEST_SETUP_DATA_CALL, result);
1351
1352         rr.mp.writeInt(7);
1353
1354         rr.mp.writeString(radioTechnology);
1355         rr.mp.writeString(profile);
1356         rr.mp.writeString(apn);
1357         rr.mp.writeString(user);
1358         rr.mp.writeString(password);
1359         rr.mp.writeString(authType);
1360         rr.mp.writeString(protocol);
1361
1362         if (RILJ_LOGD) riljLog(rr.serialString() + "> "
1363                 + requestToString(rr.mRequest) + " " + radioTechnology + " "
1364                 + profile + " " + apn + " " + user + " "
1365                 + password + " " + authType + " " + protocol);
1366
1367         send(rr);
1368     }
1369
1370     public void
1371     deactivateDataCall(int cid, int reason, Message result) {
1372         RILRequest rr
1373                 = RILRequest.obtain(RIL_REQUEST_DEACTIVATE_DATA_CALL, result);
1374
1375         rr.mp.writeInt(2);
1376         rr.mp.writeString(Integer.toString(cid));
1377         rr.mp.writeString(Integer.toString(reason));
1378
1379         if (RILJ_LOGD) riljLog(rr.serialString() + "> " +
1380                 requestToString(rr.mRequest) + " " + cid + " " + reason);
1381
1382         send(rr);
1383     }
1384
1385     public void
1386     setRadioPower(boolean on, Message result) {
1387         RILRequest rr = RILRequest.obtain(RIL_REQUEST_RADIO_POWER, result);
1388
1389         rr.mp.writeInt(1);
1390         rr.mp.writeInt(on ? 1 : 0);
1391
1392         if (RILJ_LOGD) {
1393             riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
1394                     + (on ? " on" : " off"));
1395         }
1396
1397         send(rr);
1398     }
1399
1400     public void
1401     setSuppServiceNotifications(boolean enable, Message result) {
1402         RILRequest rr
1403                 = RILRequest.obtain(RIL_REQUEST_SET_SUPP_SVC_NOTIFICATION, result);
1404
1405         rr.mp.writeInt(1);
1406         rr.mp.writeInt(enable ? 1 : 0);
1407
1408         if (RILJ_LOGD) riljLog(rr.serialString() + "> "
1409                 + requestToString(rr.mRequest));
1410
1411         send(rr);
1412     }
1413
1414     public void
1415     acknowledgeLastIncomingGsmSms(boolean success, int cause, Message result) {
1416         RILRequest rr
1417                 = RILRequest.obtain(RIL_REQUEST_SMS_ACKNOWLEDGE, result);
1418
1419         rr.mp.writeInt(2);
1420         rr.mp.writeInt(success ? 1 : 0);
1421         rr.mp.writeInt(cause);
1422
1423         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
1424                 + " " + success + " " + cause);
1425
1426         send(rr);
1427     }
1428
1429     public void
1430     acknowledgeLastIncomingCdmaSms(boolean success, int cause, Message result) {
1431         RILRequest rr
1432                 = RILRequest.obtain(RIL_REQUEST_CDMA_SMS_ACKNOWLEDGE, result);
1433
1434         rr.mp.writeInt(success ? 0 : 1); //RIL_CDMA_SMS_ErrorClass
1435         // cause code according to X.S004-550E
1436         rr.mp.writeInt(cause);
1437
1438         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
1439                 + " " + success + " " + cause);
1440
1441         send(rr);
1442     }
1443
1444     public void
1445     acknowledgeIncomingGsmSmsWithPdu(boolean success, String ackPdu, Message result) {
1446         RILRequest rr
1447                 = RILRequest.obtain(RIL_REQUEST_ACKNOWLEDGE_INCOMING_GSM_SMS_WITH_PDU, result);
1448
1449         rr.mp.writeInt(2);
1450         rr.mp.writeString(success ? "1" : "0");
1451         rr.mp.writeString(ackPdu);
1452
1453         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
1454                 + ' ' + success + " [" + ackPdu + ']');
1455
1456         send(rr);
1457     }
1458
1459     public void
1460     iccIO (int command, int fileid, String path, int p1, int p2, int p3,
1461             String data, String pin2, Message result) {
1462         iccIOForApp(command, fileid, path, p1, p2, p3, data, pin2, null, result);
1463     }
1464     public void
1465     iccIOForApp (int command, int fileid, String path, int p1, int p2, int p3,
1466             String data, String pin2, String aid, Message result) {
1467         //Note: This RIL request has not been renamed to ICC,
1468         //       but this request is also valid for SIM and RUIM
1469         RILRequest rr
1470                 = RILRequest.obtain(RIL_REQUEST_SIM_IO, result);
1471
1472         rr.mp.writeInt(command);
1473         rr.mp.writeInt(fileid);
1474         rr.mp.writeString(path);
1475         rr.mp.writeInt(p1);
1476         rr.mp.writeInt(p2);
1477         rr.mp.writeInt(p3);
1478         rr.mp.writeString(data);
1479         rr.mp.writeString(pin2);
1480         rr.mp.writeString(aid);
1481
1482         if (RILJ_LOGD) riljLog(rr.serialString() + "> iccIO: "
1483                 + requestToString(rr.mRequest)
1484                 + " 0x" + Integer.toHexString(command)
1485                 + " 0x" + Integer.toHexString(fileid) + " "
1486                 + " path: " + path + ","
1487                 + p1 + "," + p2 + "," + p3
1488                 + " aid: " + aid);
1489
1490         send(rr);
1491     }
1492
1493     public void
1494     getCLIR(Message result) {
1495         RILRequest rr
1496                 = RILRequest.obtain(RIL_REQUEST_GET_CLIR, result);
1497
1498         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1499
1500         send(rr);
1501     }
1502
1503     public void
1504     setCLIR(int clirMode, Message result) {
1505         RILRequest rr
1506                 = RILRequest.obtain(RIL_REQUEST_SET_CLIR, result);
1507
1508         // count ints
1509         rr.mp.writeInt(1);
1510
1511         rr.mp.writeInt(clirMode);
1512
1513         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
1514                     + " " + clirMode);
1515
1516         send(rr);
1517     }
1518
1519     public void
1520     queryCallWaiting(int serviceClass, Message response) {
1521         RILRequest rr
1522                 = RILRequest.obtain(RIL_REQUEST_QUERY_CALL_WAITING, response);
1523
1524         rr.mp.writeInt(1);
1525         rr.mp.writeInt(serviceClass);
1526
1527         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
1528                     + " " + serviceClass);
1529
1530         send(rr);
1531     }
1532
1533     public void
1534     setCallWaiting(boolean enable, int serviceClass, Message response) {
1535         RILRequest rr
1536                 = RILRequest.obtain(RIL_REQUEST_SET_CALL_WAITING, response);
1537
1538         rr.mp.writeInt(2);
1539         rr.mp.writeInt(enable ? 1 : 0);
1540         rr.mp.writeInt(serviceClass);
1541
1542         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
1543                 + " " + enable + ", " + serviceClass);
1544
1545         send(rr);
1546     }
1547
1548     public void
1549     setNetworkSelectionModeAutomatic(Message response) {
1550         RILRequest rr
1551                 = RILRequest.obtain(RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC,
1552                                     response);
1553
1554         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1555
1556         send(rr);
1557     }
1558
1559     public void
1560     setNetworkSelectionModeManual(String operatorNumeric, Message response) {
1561         RILRequest rr
1562                 = RILRequest.obtain(RIL_REQUEST_SET_NETWORK_SELECTION_MANUAL,
1563                                     response);
1564
1565         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
1566                     + " " + operatorNumeric);
1567
1568         rr.mp.writeString(operatorNumeric);
1569
1570         send(rr);
1571     }
1572
1573     public void
1574     getNetworkSelectionMode(Message response) {
1575         RILRequest rr
1576                 = RILRequest.obtain(RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE,
1577                                     response);
1578
1579         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1580
1581         send(rr);
1582     }
1583
1584     public void
1585     getAvailableNetworks(Message response) {
1586         RILRequest rr
1587                 = RILRequest.obtain(RIL_REQUEST_QUERY_AVAILABLE_NETWORKS,
1588                                     response);
1589
1590         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1591
1592         send(rr);
1593     }
1594
1595     public void
1596     setCallForward(int action, int cfReason, int serviceClass,
1597                 String number, int timeSeconds, Message response) {
1598         RILRequest rr
1599                 = RILRequest.obtain(RIL_REQUEST_SET_CALL_FORWARD, response);
1600
1601         rr.mp.writeInt(action);
1602         rr.mp.writeInt(cfReason);
1603         rr.mp.writeInt(serviceClass);
1604         rr.mp.writeInt(PhoneNumberUtils.toaFromString(number));
1605         rr.mp.writeString(number);
1606         rr.mp.writeInt (timeSeconds);
1607
1608         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
1609                     + " " + action + " " + cfReason + " " + serviceClass
1610                     + timeSeconds);
1611
1612         send(rr);
1613     }
1614
1615     public void
1616     queryCallForwardStatus(int cfReason, int serviceClass,
1617                 String number, Message response) {
1618         RILRequest rr
1619             = RILRequest.obtain(RIL_REQUEST_QUERY_CALL_FORWARD_STATUS, response);
1620
1621         rr.mp.writeInt(2); // 2 is for query action, not in used anyway
1622         rr.mp.writeInt(cfReason);
1623         rr.mp.writeInt(serviceClass);
1624         rr.mp.writeInt(PhoneNumberUtils.toaFromString(number));
1625         rr.mp.writeString(number);
1626         rr.mp.writeInt (0);
1627
1628         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
1629                 + " " + cfReason + " " + serviceClass);
1630
1631         send(rr);
1632     }
1633
1634     public void
1635     queryCLIP(Message response) {
1636         RILRequest rr
1637             = RILRequest.obtain(RIL_REQUEST_QUERY_CLIP, response);
1638
1639         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1640
1641         send(rr);
1642     }
1643
1644
1645     public void
1646     getBasebandVersion (Message response) {
1647         RILRequest rr
1648                 = RILRequest.obtain(RIL_REQUEST_BASEBAND_VERSION, response);
1649
1650         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1651
1652         send(rr);
1653     }
1654
1655     @Override
1656     public void
1657     queryFacilityLock(String facility, String password, int serviceClass,
1658                             Message response) {
1659         queryFacilityLockForApp(facility, password, serviceClass, null, response);
1660     }
1661
1662     @Override
1663     public void
1664     queryFacilityLockForApp(String facility, String password, int serviceClass, String appId,
1665                             Message response) {
1666         RILRequest rr = RILRequest.obtain(RIL_REQUEST_QUERY_FACILITY_LOCK, response);
1667
1668         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
1669                                                  + " [" + facility + " " + serviceClass
1670                                                  + " " + appId + "]");
1671
1672         // count strings
1673         rr.mp.writeInt(4);
1674
1675         rr.mp.writeString(facility);
1676         rr.mp.writeString(password);
1677
1678         rr.mp.writeString(Integer.toString(serviceClass));
1679         rr.mp.writeString(appId);
1680
1681         send(rr);
1682     }
1683
1684     @Override
1685     public void
1686     setFacilityLock (String facility, boolean lockState, String password,
1687                         int serviceClass, Message response) {
1688         setFacilityLockForApp(facility, lockState, password, serviceClass, null, response);
1689     }
1690
1691     @Override
1692     public void
1693     setFacilityLockForApp(String facility, boolean lockState, String password,
1694                         int serviceClass, String appId, Message response) {
1695         String lockString;
1696          RILRequest rr
1697                 = RILRequest.obtain(RIL_REQUEST_SET_FACILITY_LOCK, response);
1698
1699         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
1700                                                         + " [" + facility + " " + lockState
1701                                                         + " " + serviceClass + " " + appId + "]");
1702
1703         // count strings
1704         rr.mp.writeInt(5);
1705
1706         rr.mp.writeString(facility);
1707         lockString = (lockState)?"1":"0";
1708         rr.mp.writeString(lockString);
1709         rr.mp.writeString(password);
1710         rr.mp.writeString(Integer.toString(serviceClass));
1711         rr.mp.writeString(appId);
1712
1713         send(rr);
1714
1715     }
1716
1717     public void
1718     sendUSSD (String ussdString, Message response) {
1719         RILRequest rr
1720                 = RILRequest.obtain(RIL_REQUEST_SEND_USSD, response);
1721
1722         if (RILJ_LOGD) {
1723             String logUssdString = "*******";
1724             if (RILJ_LOGV) logUssdString = ussdString;
1725             riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
1726                                    + " " + logUssdString);
1727         }
1728
1729         rr.mp.writeString(ussdString);
1730
1731         send(rr);
1732     }
1733
1734     // inherited javadoc suffices
1735     public void cancelPendingUssd (Message response) {
1736         RILRequest rr
1737                 = RILRequest.obtain(RIL_REQUEST_CANCEL_USSD, response);
1738
1739         if (RILJ_LOGD) riljLog(rr.serialString()
1740                 + "> " + requestToString(rr.mRequest));
1741
1742         send(rr);
1743     }
1744
1745
1746     public void resetRadio(Message result) {
1747         RILRequest rr
1748                 = RILRequest.obtain(RIL_REQUEST_RESET_RADIO, result);
1749
1750         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1751
1752         send(rr);
1753     }
1754
1755     public void invokeOemRilRequestRaw(byte[] data, Message response) {
1756         RILRequest rr
1757                 = RILRequest.obtain(RIL_REQUEST_OEM_HOOK_RAW, response);
1758
1759         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
1760                + "[" + IccUtils.bytesToHexString(data) + "]");
1761
1762         rr.mp.writeByteArray(data);
1763
1764         send(rr);
1765
1766     }
1767
1768     public void invokeOemRilRequestStrings(String[] strings, Message response) {
1769         RILRequest rr
1770                 = RILRequest.obtain(RIL_REQUEST_OEM_HOOK_STRINGS, response);
1771
1772         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1773
1774         rr.mp.writeStringArray(strings);
1775
1776         send(rr);
1777     }
1778
1779      /**
1780      * Assign a specified band for RF configuration.
1781      *
1782      * @param bandMode one of BM_*_BAND
1783      * @param response is callback message
1784      */
1785     public void setBandMode (int bandMode, Message response) {
1786         RILRequest rr
1787                 = RILRequest.obtain(RIL_REQUEST_SET_BAND_MODE, response);
1788
1789         rr.mp.writeInt(1);
1790         rr.mp.writeInt(bandMode);
1791
1792         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
1793                  + " " + bandMode);
1794
1795         send(rr);
1796      }
1797
1798     /**
1799      * Query the list of band mode supported by RF.
1800      *
1801      * @param response is callback message
1802      *        ((AsyncResult)response.obj).result  is an int[] with every
1803      *        element representing one avialable BM_*_BAND
1804      */
1805     public void queryAvailableBandMode (Message response) {
1806         RILRequest rr
1807                 = RILRequest.obtain(RIL_REQUEST_QUERY_AVAILABLE_BAND_MODE,
1808                 response);
1809
1810         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1811
1812         send(rr);
1813     }
1814
1815     /**
1816      * {@inheritDoc}
1817      */
1818     public void sendTerminalResponse(String contents, Message response) {
1819         RILRequest rr = RILRequest.obtain(
1820                 RILConstants.RIL_REQUEST_STK_SEND_TERMINAL_RESPONSE, response);
1821
1822         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1823
1824         rr.mp.writeString(contents);
1825         send(rr);
1826     }
1827
1828     /**
1829      * {@inheritDoc}
1830      */
1831     public void sendEnvelope(String contents, Message response) {
1832         RILRequest rr = RILRequest.obtain(
1833                 RILConstants.RIL_REQUEST_STK_SEND_ENVELOPE_COMMAND, response);
1834
1835         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1836
1837         rr.mp.writeString(contents);
1838         send(rr);
1839     }
1840
1841     /**
1842      * {@inheritDoc}
1843      */
1844     public void sendEnvelopeWithStatus(String contents, Message response) {
1845         RILRequest rr = RILRequest.obtain(
1846                 RILConstants.RIL_REQUEST_STK_SEND_ENVELOPE_WITH_STATUS, response);
1847
1848         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
1849                 + '[' + contents + ']');
1850
1851         rr.mp.writeString(contents);
1852         send(rr);
1853     }
1854
1855     /**
1856      * {@inheritDoc}
1857      */
1858     public void handleCallSetupRequestFromSim(
1859             boolean accept, Message response) {
1860
1861         RILRequest rr = RILRequest.obtain(
1862             RILConstants.RIL_REQUEST_STK_HANDLE_CALL_SETUP_REQUESTED_FROM_SIM,
1863             response);
1864
1865         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1866
1867         int[] param = new int[1];
1868         param[0] = accept ? 1 : 0;
1869         rr.mp.writeIntArray(param);
1870         send(rr);
1871     }
1872
1873     /**
1874      * {@inheritDoc}
1875      */
1876     @Override
1877     public void setCurrentPreferredNetworkType() {
1878         if (RILJ_LOGD) riljLog("setCurrentPreferredNetworkType: " + mSetPreferredNetworkType);
1879         setPreferredNetworkType(mSetPreferredNetworkType, null);
1880     }
1881     private int mSetPreferredNetworkType;
1882
1883     /**
1884      * {@inheritDoc}
1885      */
1886     public void setPreferredNetworkType(int networkType , Message response) {
1887         RILRequest rr = RILRequest.obtain(
1888                 RILConstants.RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE, response);
1889
1890         rr.mp.writeInt(1);
1891         rr.mp.writeInt(networkType);
1892
1893         mSetPreferredNetworkType = networkType;
1894         mPreferredNetworkType = networkType;
1895
1896         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
1897                 + " : " + networkType);
1898
1899         send(rr);
1900     }
1901
1902     /**
1903      * {@inheritDoc}
1904      */
1905     public void getPreferredNetworkType(Message response) {
1906         RILRequest rr = RILRequest.obtain(
1907                 RILConstants.RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE, response);
1908
1909         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1910
1911         send(rr);
1912     }
1913
1914     /**
1915      * {@inheritDoc}
1916      */
1917     public void getNeighboringCids(Message response) {
1918         RILRequest rr = RILRequest.obtain(
1919                 RILConstants.RIL_REQUEST_GET_NEIGHBORING_CELL_IDS, response);
1920
1921         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1922
1923         send(rr);
1924     }
1925
1926     /**
1927      * {@inheritDoc}
1928      */
1929     public void setLocationUpdates(boolean enable, Message response) {
1930         RILRequest rr = RILRequest.obtain(RIL_REQUEST_SET_LOCATION_UPDATES, response);
1931         rr.mp.writeInt(1);
1932         rr.mp.writeInt(enable ? 1 : 0);
1933
1934         if (RILJ_LOGD) riljLog(rr.serialString() + "> "
1935                 + requestToString(rr.mRequest) + ": " + enable);
1936
1937         send(rr);
1938     }
1939
1940     /**
1941      * {@inheritDoc}
1942      */
1943     public void getSmscAddress(Message result) {
1944         RILRequest rr = RILRequest.obtain(RIL_REQUEST_GET_SMSC_ADDRESS, result);
1945
1946         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1947
1948         send(rr);
1949     }
1950
1951     /**
1952      * {@inheritDoc}
1953      */
1954     public void setSmscAddress(String address, Message result) {
1955         RILRequest rr = RILRequest.obtain(RIL_REQUEST_SET_SMSC_ADDRESS, result);
1956
1957         rr.mp.writeString(address);
1958
1959         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
1960                 + " : " + address);
1961
1962         send(rr);
1963     }
1964
1965     /**
1966      * {@inheritDoc}
1967      */
1968     public void reportSmsMemoryStatus(boolean available, Message result) {
1969         RILRequest rr = RILRequest.obtain(RIL_REQUEST_REPORT_SMS_MEMORY_STATUS, result);
1970         rr.mp.writeInt(1);
1971         rr.mp.writeInt(available ? 1 : 0);
1972
1973         if (RILJ_LOGD) riljLog(rr.serialString() + "> "
1974                 + requestToString(rr.mRequest) + ": " + available);
1975
1976         send(rr);
1977     }
1978
1979     /**
1980      * {@inheritDoc}
1981      */
1982     public void reportStkServiceIsRunning(Message result) {
1983         RILRequest rr = RILRequest.obtain(RIL_REQUEST_REPORT_STK_SERVICE_IS_RUNNING, result);
1984
1985         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1986
1987         send(rr);
1988     }
1989
1990     /**
1991      * {@inheritDoc}
1992      */
1993     public void getGsmBroadcastConfig(Message response) {
1994         RILRequest rr = RILRequest.obtain(RIL_REQUEST_GSM_GET_BROADCAST_CONFIG, response);
1995
1996         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
1997
1998         send(rr);
1999     }
2000
2001     /**
2002      * {@inheritDoc}
2003      */
2004     public void setGsmBroadcastConfig(SmsBroadcastConfigInfo[] config, Message response) {
2005         RILRequest rr = RILRequest.obtain(RIL_REQUEST_GSM_SET_BROADCAST_CONFIG, response);
2006
2007         int numOfConfig = config.length;
2008         rr.mp.writeInt(numOfConfig);
2009
2010         for(int i = 0; i < numOfConfig; i++) {
2011             rr.mp.writeInt(config[i].getFromServiceId());
2012             rr.mp.writeInt(config[i].getToServiceId());
2013             rr.mp.writeInt(config[i].getFromCodeScheme());
2014             rr.mp.writeInt(config[i].getToCodeScheme());
2015             rr.mp.writeInt(config[i].isSelected() ? 1 : 0);
2016         }
2017
2018         if (RILJ_LOGD) {
2019             riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
2020                     + " with " + numOfConfig + " configs : ");
2021             for (int i = 0; i < numOfConfig; i++) {
2022                 riljLog(config[i].toString());
2023             }
2024         }
2025
2026         send(rr);
2027     }
2028
2029     /**
2030      * {@inheritDoc}
2031      */
2032     public void setGsmBroadcastActivation(boolean activate, Message response) {
2033         RILRequest rr = RILRequest.obtain(RIL_REQUEST_GSM_BROADCAST_ACTIVATION, response);
2034
2035         rr.mp.writeInt(1);
2036         rr.mp.writeInt(activate ? 0 : 1);
2037
2038         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
2039
2040         send(rr);
2041     }
2042
2043     //***** Private Methods
2044
2045     private void sendScreenState(boolean on) {
2046         RILRequest rr = RILRequest.obtain(RIL_REQUEST_SCREEN_STATE, null);
2047         rr.mp.writeInt(1);
2048         rr.mp.writeInt(on ? 1 : 0);
2049
2050         if (RILJ_LOGD) riljLog(rr.serialString()
2051                 + "> " + requestToString(rr.mRequest) + ": " + on);
2052
2053         send(rr);
2054     }
2055
2056     protected void
2057     onRadioAvailable() {
2058         // In case screen state was lost (due to process crash),
2059         // this ensures that the RIL knows the correct screen state.
2060
2061         PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);
2062         sendScreenState(pm.isScreenOn());
2063    }
2064
2065     private RadioState getRadioStateFromInt(int stateInt) {
2066         RadioState state;
2067
2068         /* RIL_RadioState ril.h */
2069         switch(stateInt) {
2070             case 0: state = RadioState.RADIO_OFF; break;
2071             case 1: state = RadioState.RADIO_UNAVAILABLE; break;
2072             case 10: state = RadioState.RADIO_ON; break;
2073
2074             default:
2075                 throw new RuntimeException(
2076                             "Unrecognized RIL_RadioState: " + stateInt);
2077         }
2078         return state;
2079     }
2080
2081     private void switchToRadioState(RadioState newState) {
2082         setRadioState(newState);
2083     }
2084
2085     /**
2086      * Holds a PARTIAL_WAKE_LOCK whenever
2087      * a) There is outstanding RIL request sent to RIL deamon and no replied
2088      * b) There is a request pending to be sent out.
2089      *
2090      * There is a WAKE_LOCK_TIMEOUT to release the lock, though it shouldn't
2091      * happen often.
2092      */
2093
2094     private void
2095     acquireWakeLock() {
2096         synchronized (mWakeLock) {
2097             mWakeLock.acquire();
2098             mRequestMessagesPending++;
2099
2100             mSender.removeMessages(EVENT_WAKE_LOCK_TIMEOUT);
2101             Message msg = mSender.obtainMessage(EVENT_WAKE_LOCK_TIMEOUT);
2102             mSender.sendMessageDelayed(msg, mWakeLockTimeout);
2103         }
2104     }
2105
2106     private void
2107     releaseWakeLockIfDone() {
2108         synchronized (mWakeLock) {
2109             if (mWakeLock.isHeld() &&
2110                 (mRequestMessagesPending == 0) &&
2111                 (mRequestMessagesWaiting == 0)) {
2112                 mSender.removeMessages(EVENT_WAKE_LOCK_TIMEOUT);
2113                 mWakeLock.release();
2114             }
2115         }
2116     }
2117
2118     private void
2119     send(RILRequest rr) {
2120         Message msg;
2121
2122         if (mSocket == null) {
2123             rr.onError(RADIO_NOT_AVAILABLE, null);
2124             rr.release();
2125             return;
2126         }
2127
2128         msg = mSender.obtainMessage(EVENT_SEND, rr);
2129
2130         acquireWakeLock();
2131
2132         msg.sendToTarget();
2133     }
2134
2135     private void
2136     processResponse (Parcel p) {
2137         int type;
2138
2139         type = p.readInt();
2140
2141         if (type == RESPONSE_UNSOLICITED) {
2142             processUnsolicited (p);
2143         } else if (type == RESPONSE_SOLICITED) {
2144             processSolicited (p);
2145         }
2146
2147         releaseWakeLockIfDone();
2148     }
2149
2150     /**
2151      * Release each request in mRequestList then clear the list
2152      * @param error is the RIL_Errno sent back
2153      * @param loggable true means to print all requests in mRequestList
2154      */
2155     private void clearRequestList(int error, boolean loggable) {
2156         RILRequest rr;
2157         synchronized (mRequestList) {
2158             int count = mRequestList.size();
2159             if (RILJ_LOGD && loggable) {
2160                 Rlog.d(RILJ_LOG_TAG, "WAKE_LOCK_TIMEOUT " +
2161                         " mReqPending=" + mRequestMessagesPending +
2162                         " mRequestList=" + count);
2163             }
2164
2165             for (int i = 0; i < count ; i++) {
2166                 rr = mRequestList.get(i);
2167                 if (RILJ_LOGD && loggable) {
2168                     Rlog.d(RILJ_LOG_TAG, i + ": [" + rr.mSerial + "] " +
2169                             requestToString(rr.mRequest));
2170                 }
2171                 rr.onError(error, null);
2172                 rr.release();
2173             }
2174             mRequestList.clear();
2175             mRequestMessagesWaiting = 0;
2176         }
2177     }
2178
2179     private RILRequest findAndRemoveRequestFromList(int serial) {
2180         synchronized (mRequestList) {
2181             for (int i = 0, s = mRequestList.size() ; i < s ; i++) {
2182                 RILRequest rr = mRequestList.get(i);
2183
2184                 if (rr.mSerial == serial) {
2185                     mRequestList.remove(i);
2186                     if (mRequestMessagesWaiting > 0)
2187                         mRequestMessagesWaiting--;
2188                     return rr;
2189                 }
2190             }
2191         }
2192
2193         return null;
2194     }
2195
2196     private void
2197     processSolicited (Parcel p) {
2198         int serial, error;
2199         boolean found = false;
2200
2201         serial = p.readInt();
2202         error = p.readInt();
2203
2204         RILRequest rr;
2205
2206         rr = findAndRemoveRequestFromList(serial);
2207
2208         if (rr == null) {
2209             Rlog.w(RILJ_LOG_TAG, "Unexpected solicited response! sn: "
2210                             + serial + " error: " + error);
2211             return;
2212         }
2213
2214         Object ret = null;
2215
2216         if (error == 0 || p.dataAvail() > 0) {
2217             // either command succeeds or command fails but with data payload
2218             try {switch (rr.mRequest) {
2219             /*
2220  cat libs/telephony/ril_commands.h \
2221  | egrep "^ *{RIL_" \
2222  | sed -re 's/\{([^,]+),[^,]+,([^}]+).+/case \1: ret = \2(p); break;/'
2223              */
2224             case RIL_REQUEST_GET_SIM_STATUS: ret =  responseIccCardStatus(p); break;
2225             case RIL_REQUEST_ENTER_SIM_PIN: ret =  responseInts(p); break;
2226             case RIL_REQUEST_ENTER_SIM_PUK: ret =  responseInts(p); break;
2227             case RIL_REQUEST_ENTER_SIM_PIN2: ret =  responseInts(p); break;
2228             case RIL_REQUEST_ENTER_SIM_PUK2: ret =  responseInts(p); break;
2229             case RIL_REQUEST_CHANGE_SIM_PIN: ret =  responseInts(p); break;
2230             case RIL_REQUEST_CHANGE_SIM_PIN2: ret =  responseInts(p); break;
2231             case RIL_REQUEST_ENTER_NETWORK_DEPERSONALIZATION: ret =  responseInts(p); break;
2232             case RIL_REQUEST_GET_CURRENT_CALLS: ret =  responseCallList(p); break;
2233             case RIL_REQUEST_DIAL: ret =  responseVoid(p); break;
2234             case RIL_REQUEST_GET_IMSI: ret =  responseString(p); break;
2235             case RIL_REQUEST_HANGUP: ret =  responseVoid(p); break;
2236             case RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND: ret =  responseVoid(p); break;
2237             case RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND: {
2238                 if (mTestingEmergencyCall.getAndSet(false)) {
2239                     if (mEmergencyCallbackModeRegistrant != null) {
2240                         riljLog("testing emergency call, notify ECM Registrants");
2241                         mEmergencyCallbackModeRegistrant.notifyRegistrant();
2242                     }
2243                 }
2244                 ret =  responseVoid(p);
2245                 break;
2246             }
2247             case RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE: ret =  responseVoid(p); break;
2248             case RIL_REQUEST_CONFERENCE: ret =  responseVoid(p); break;
2249             case RIL_REQUEST_UDUB: ret =  responseVoid(p); break;
2250             case RIL_REQUEST_LAST_CALL_FAIL_CAUSE: ret =  responseInts(p); break;
2251             case RIL_REQUEST_SIGNAL_STRENGTH: ret =  responseSignalStrength(p); break;
2252             case RIL_REQUEST_VOICE_REGISTRATION_STATE: ret =  responseStrings(p); break;
2253             case RIL_REQUEST_DATA_REGISTRATION_STATE: ret =  responseStrings(p); break;
2254             case RIL_REQUEST_OPERATOR: ret =  responseStrings(p); break;
2255             case RIL_REQUEST_RADIO_POWER: ret =  responseVoid(p); break;
2256             case RIL_REQUEST_DTMF: ret =  responseVoid(p); break;
2257             case RIL_REQUEST_SEND_SMS: ret =  responseSMS(p); break;
2258             case RIL_REQUEST_SEND_SMS_EXPECT_MORE: ret =  responseSMS(p); break;
2259             case RIL_REQUEST_SETUP_DATA_CALL: ret =  responseSetupDataCall(p); break;
2260             case RIL_REQUEST_SIM_IO: ret =  responseICC_IO(p); break;
2261             case RIL_REQUEST_SEND_USSD: ret =  responseVoid(p); break;
2262             case RIL_REQUEST_CANCEL_USSD: ret =  responseVoid(p); break;
2263             case RIL_REQUEST_GET_CLIR: ret =  responseInts(p); break;
2264             case RIL_REQUEST_SET_CLIR: ret =  responseVoid(p); break;
2265             case RIL_REQUEST_QUERY_CALL_FORWARD_STATUS: ret =  responseCallForward(p); break;
2266             case RIL_REQUEST_SET_CALL_FORWARD: ret =  responseVoid(p); break;
2267             case RIL_REQUEST_QUERY_CALL_WAITING: ret =  responseInts(p); break;
2268             case RIL_REQUEST_SET_CALL_WAITING: ret =  responseVoid(p); break;
2269             case RIL_REQUEST_SMS_ACKNOWLEDGE: ret =  responseVoid(p); break;
2270             case RIL_REQUEST_GET_IMEI: ret =  responseString(p); break;
2271             case RIL_REQUEST_GET_IMEISV: ret =  responseString(p); break;
2272             case RIL_REQUEST_ANSWER: ret =  responseVoid(p); break;
2273             case RIL_REQUEST_DEACTIVATE_DATA_CALL: ret =  responseVoid(p); break;
2274             case RIL_REQUEST_QUERY_FACILITY_LOCK: ret =  responseInts(p); break;
2275             case RIL_REQUEST_SET_FACILITY_LOCK: ret =  responseInts(p); break;
2276             case RIL_REQUEST_CHANGE_BARRING_PASSWORD: ret =  responseVoid(p); break;
2277             case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE: ret =  responseInts(p); break;
2278             case RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC: ret =  responseVoid(p); break;
2279             case RIL_REQUEST_SET_NETWORK_SELECTION_MANUAL: ret =  responseVoid(p); break;
2280             case RIL_REQUEST_QUERY_AVAILABLE_NETWORKS : ret =  responseOperatorInfos(p); break;
2281             case RIL_REQUEST_DTMF_START: ret =  responseVoid(p); break;
2282             case RIL_REQUEST_DTMF_STOP: ret =  responseVoid(p); break;
2283             case RIL_REQUEST_BASEBAND_VERSION: ret =  responseString(p); break;
2284             case RIL_REQUEST_SEPARATE_CONNECTION: ret =  responseVoid(p); break;
2285             case RIL_REQUEST_SET_MUTE: ret =  responseVoid(p); break;
2286             case RIL_REQUEST_GET_MUTE: ret =  responseInts(p); break;
2287             case RIL_REQUEST_QUERY_CLIP: ret =  responseInts(p); break;
2288             case RIL_REQUEST_LAST_DATA_CALL_FAIL_CAUSE: ret =  responseInts(p); break;
2289             case RIL_REQUEST_DATA_CALL_LIST: ret =  responseDataCallList(p); break;
2290             case RIL_REQUEST_RESET_RADIO: ret =  responseVoid(p); break;
2291             case RIL_REQUEST_OEM_HOOK_RAW: ret =  responseRaw(p); break;
2292             case RIL_REQUEST_OEM_HOOK_STRINGS: ret =  responseStrings(p); break;
2293             case RIL_REQUEST_SCREEN_STATE: ret =  responseVoid(p); break;
2294             case RIL_REQUEST_SET_SUPP_SVC_NOTIFICATION: ret =  responseVoid(p); break;
2295             case RIL_REQUEST_WRITE_SMS_TO_SIM: ret =  responseInts(p); break;
2296             case RIL_REQUEST_DELETE_SMS_ON_SIM: ret =  responseVoid(p); break;
2297             case RIL_REQUEST_SET_BAND_MODE: ret =  responseVoid(p); break;
2298             case RIL_REQUEST_QUERY_AVAILABLE_BAND_MODE: ret =  responseInts(p); break;
2299             case RIL_REQUEST_STK_GET_PROFILE: ret =  responseString(p); break;
2300             case RIL_REQUEST_STK_SET_PROFILE: ret =  responseVoid(p); break;
2301             case RIL_REQUEST_STK_SEND_ENVELOPE_COMMAND: ret =  responseString(p); break;
2302             case RIL_REQUEST_STK_SEND_TERMINAL_RESPONSE: ret =  responseVoid(p); break;
2303             case RIL_REQUEST_STK_HANDLE_CALL_SETUP_REQUESTED_FROM_SIM: ret =  responseInts(p); break;
2304             case RIL_REQUEST_EXPLICIT_CALL_TRANSFER: ret =  responseVoid(p); break;
2305             case RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE: ret =  responseVoid(p); break;
2306             case RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE: ret =  responseGetPreferredNetworkType(p); break;
2307             case RIL_REQUEST_GET_NEIGHBORING_CELL_IDS: ret = responseCellList(p); break;
2308             case RIL_REQUEST_SET_LOCATION_UPDATES: ret =  responseVoid(p); break;
2309             case RIL_REQUEST_CDMA_SET_SUBSCRIPTION_SOURCE: ret =  responseVoid(p); break;
2310             case RIL_REQUEST_CDMA_SET_ROAMING_PREFERENCE: ret =  responseVoid(p); break;
2311             case RIL_REQUEST_CDMA_QUERY_ROAMING_PREFERENCE: ret =  responseInts(p); break;
2312             case RIL_REQUEST_SET_TTY_MODE: ret =  responseVoid(p); break;
2313             case RIL_REQUEST_QUERY_TTY_MODE: ret =  responseInts(p); break;
2314             case RIL_REQUEST_CDMA_SET_PREFERRED_VOICE_PRIVACY_MODE: ret =  responseVoid(p); break;
2315             case RIL_REQUEST_CDMA_QUERY_PREFERRED_VOICE_PRIVACY_MODE: ret =  responseInts(p); break;
2316             case RIL_REQUEST_CDMA_FLASH: ret =  responseVoid(p); break;
2317             case RIL_REQUEST_CDMA_BURST_DTMF: ret =  responseVoid(p); break;
2318             case RIL_REQUEST_CDMA_SEND_SMS: ret =  responseSMS(p); break;
2319             case RIL_REQUEST_CDMA_SMS_ACKNOWLEDGE: ret =  responseVoid(p); break;
2320             case RIL_REQUEST_GSM_GET_BROADCAST_CONFIG: ret =  responseGmsBroadcastConfig(p); break;
2321             case RIL_REQUEST_GSM_SET_BROADCAST_CONFIG: ret =  responseVoid(p); break;
2322             case RIL_REQUEST_GSM_BROADCAST_ACTIVATION: ret =  responseVoid(p); break;
2323             case RIL_REQUEST_CDMA_GET_BROADCAST_CONFIG: ret =  responseCdmaBroadcastConfig(p); break;
2324             case RIL_REQUEST_CDMA_SET_BROADCAST_CONFIG: ret =  responseVoid(p); break;
2325             case RIL_REQUEST_CDMA_BROADCAST_ACTIVATION: ret =  responseVoid(p); break;
2326             case RIL_REQUEST_CDMA_VALIDATE_AND_WRITE_AKEY: ret =  responseVoid(p); break;
2327             case RIL_REQUEST_CDMA_SUBSCRIPTION: ret =  responseStrings(p); break;
2328             case RIL_REQUEST_CDMA_WRITE_SMS_TO_RUIM: ret =  responseInts(p); break;
2329             case RIL_REQUEST_CDMA_DELETE_SMS_ON_RUIM: ret =  responseVoid(p); break;
2330             case RIL_REQUEST_DEVICE_IDENTITY: ret =  responseStrings(p); break;
2331             case RIL_REQUEST_GET_SMSC_ADDRESS: ret = responseString(p); break;
2332             case RIL_REQUEST_SET_SMSC_ADDRESS: ret = responseVoid(p); break;
2333             case RIL_REQUEST_EXIT_EMERGENCY_CALLBACK_MODE: ret = responseVoid(p); break;
2334             case RIL_REQUEST_REPORT_SMS_MEMORY_STATUS: ret = responseVoid(p); break;
2335             case RIL_REQUEST_REPORT_STK_SERVICE_IS_RUNNING: ret = responseVoid(p); break;
2336             case RIL_REQUEST_CDMA_GET_SUBSCRIPTION_SOURCE: ret =  responseInts(p); break;
2337             case RIL_REQUEST_ISIM_AUTHENTICATION: ret =  responseString(p); break;
2338             case RIL_REQUEST_ACKNOWLEDGE_INCOMING_GSM_SMS_WITH_PDU: ret = responseVoid(p); break;
2339             case RIL_REQUEST_STK_SEND_ENVELOPE_WITH_STATUS: ret = responseICC_IO(p); break;
2340             case RIL_REQUEST_VOICE_RADIO_TECH: ret = responseInts(p); break;
2341             default:
2342                 throw new RuntimeException("Unrecognized solicited response: " + rr.mRequest);
2343             //break;
2344             }} catch (Throwable tr) {
2345                 // Exceptions here usually mean invalid RIL responses
2346
2347                 Rlog.w(RILJ_LOG_TAG, rr.serialString() + "< "
2348                         + requestToString(rr.mRequest)
2349                         + " exception, possible invalid RIL response", tr);
2350
2351                 if (rr.mResult != null) {
2352                     AsyncResult.forMessage(rr.mResult, null, tr);
2353                     rr.mResult.sendToTarget();
2354                 }
2355                 rr.release();
2356                 return;
2357             }
2358         }
2359
2360         // Here and below fake RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED, see b/7255789.
2361         // This is needed otherwise we don't automatically transition to the main lock
2362         // screen when the pin or puk is entered incorrectly.
2363         switch (rr.mRequest) {
2364             case RIL_REQUEST_ENTER_SIM_PUK:
2365             case RIL_REQUEST_ENTER_SIM_PUK2:
2366                 if (mIccStatusChangedRegistrants != null) {
2367                     if (RILJ_LOGD) {
2368                         riljLog("ON enter sim puk fakeSimStatusChanged: reg count="
2369                                 + mIccStatusChangedRegistrants.size());
2370                     }
2371                     mIccStatusChangedRegistrants.notifyRegistrants();
2372                 }
2373                 break;
2374         }
2375
2376         if (error != 0) {
2377             switch (rr.mRequest) {
2378                 case RIL_REQUEST_ENTER_SIM_PIN:
2379                 case RIL_REQUEST_ENTER_SIM_PIN2:
2380                 case RIL_REQUEST_CHANGE_SIM_PIN:
2381                 case RIL_REQUEST_CHANGE_SIM_PIN2:
2382                 case RIL_REQUEST_SET_FACILITY_LOCK:
2383                     if (mIccStatusChangedRegistrants != null) {
2384                         if (RILJ_LOGD) {
2385                             riljLog("ON some errors fakeSimStatusChanged: reg count="
2386                                     + mIccStatusChangedRegistrants.size());
2387                         }
2388                         mIccStatusChangedRegistrants.notifyRegistrants();
2389                     }
2390                     break;
2391             }
2392
2393             rr.onError(error, ret);
2394             rr.release();
2395             return;
2396         }
2397
2398         if (RILJ_LOGD) riljLog(rr.serialString() + "< " + requestToString(rr.mRequest)
2399             + " " + retToString(rr.mRequest, ret));
2400
2401         if (rr.mResult != null) {
2402             AsyncResult.forMessage(rr.mResult, ret, null);
2403             rr.mResult.sendToTarget();
2404         }
2405
2406         rr.release();
2407     }
2408
2409     private String
2410     retToString(int req, Object ret) {
2411         if (ret == null) return "";
2412         switch (req) {
2413             // Don't log these return values, for privacy's sake.
2414             case RIL_REQUEST_GET_IMSI:
2415             case RIL_REQUEST_GET_IMEI:
2416             case RIL_REQUEST_GET_IMEISV:
2417                 if (!RILJ_LOGV) {
2418                     // If not versbose logging just return and don't display IMSI and IMEI, IMEISV
2419                     return "";
2420                 }
2421         }
2422
2423         StringBuilder sb;
2424         String s;
2425         int length;
2426         if (ret instanceof int[]){
2427             int[] intArray = (int[]) ret;
2428             length = intArray.length;
2429             sb = new StringBuilder("{");
2430             if (length > 0) {
2431                 int i = 0;
2432                 sb.append(intArray[i++]);
2433                 while ( i < length) {
2434                     sb.append(", ").append(intArray[i++]);
2435                 }
2436             }
2437             sb.append("}");
2438             s = sb.toString();
2439         } else if (ret instanceof String[]) {
2440             String[] strings = (String[]) ret;
2441             length = strings.length;
2442             sb = new StringBuilder("{");
2443             if (length > 0) {
2444                 int i = 0;
2445                 sb.append(strings[i++]);
2446                 while ( i < length) {
2447                     sb.append(", ").append(strings[i++]);
2448                 }
2449             }
2450             sb.append("}");
2451             s = sb.toString();
2452         }else if (req == RIL_REQUEST_GET_CURRENT_CALLS) {
2453             ArrayList<DriverCall> calls = (ArrayList<DriverCall>) ret;
2454             sb = new StringBuilder(" ");
2455             for (DriverCall dc : calls) {
2456                 sb.append("[").append(dc).append("] ");
2457             }
2458             s = sb.toString();
2459         } else if (req == RIL_REQUEST_GET_NEIGHBORING_CELL_IDS) {
2460             ArrayList<NeighboringCellInfo> cells;
2461             cells = (ArrayList<NeighboringCellInfo>) ret;
2462             sb = new StringBuilder(" ");
2463             for (NeighboringCellInfo cell : cells) {
2464                 sb.append(cell).append(" ");
2465             }
2466             s = sb.toString();
2467         } else {
2468             s = ret.toString();
2469         }
2470         return s;
2471     }
2472
2473     private void
2474     processUnsolicited (Parcel p) {
2475         int response;
2476         Object ret;
2477
2478         response = p.readInt();
2479
2480         try {switch(response) {
2481 /*
2482  cat libs/telephony/ril_unsol_commands.h \
2483  | egrep "^ *{RIL_" \
2484  | sed -re 's/\{([^,]+),[^,]+,([^}]+).+/case \1: \2(rr, p); break;/'
2485 */
2486
2487             case RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED: ret =  responseVoid(p); break;
2488             case RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED: ret =  responseVoid(p); break;
2489             case RIL_UNSOL_RESPONSE_VOICE_NETWORK_STATE_CHANGED: ret =  responseVoid(p); break;
2490             case RIL_UNSOL_RESPONSE_NEW_SMS: ret =  responseString(p); break;
2491             case RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT: ret =  responseString(p); break;
2492             case RIL_UNSOL_RESPONSE_NEW_SMS_ON_SIM: ret =  responseInts(p); break;
2493             case RIL_UNSOL_ON_USSD: ret =  responseStrings(p); break;
2494             case RIL_UNSOL_NITZ_TIME_RECEIVED: ret =  responseString(p); break;
2495             case RIL_UNSOL_SIGNAL_STRENGTH: ret = responseSignalStrength(p); break;
2496             case RIL_UNSOL_DATA_CALL_LIST_CHANGED: ret = responseDataCallList(p);break;
2497             case RIL_UNSOL_SUPP_SVC_NOTIFICATION: ret = responseSuppServiceNotification(p); break;
2498             case RIL_UNSOL_STK_SESSION_END: ret = responseVoid(p); break;
2499             case RIL_UNSOL_STK_PROACTIVE_COMMAND: ret = responseString(p); break;
2500             case RIL_UNSOL_STK_EVENT_NOTIFY: ret = responseString(p); break;
2501             case RIL_UNSOL_STK_CALL_SETUP: ret = responseInts(p); break;
2502             case RIL_UNSOL_SIM_SMS_STORAGE_FULL: ret =  responseVoid(p); break;
2503             case RIL_UNSOL_SIM_REFRESH: ret =  responseSimRefresh(p); break;
2504             case RIL_UNSOL_CALL_RING: ret =  responseCallRing(p); break;
2505             case RIL_UNSOL_RESTRICTED_STATE_CHANGED: ret = responseInts(p); break;
2506             case RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED:  ret =  responseVoid(p); break;
2507             case RIL_UNSOL_RESPONSE_CDMA_NEW_SMS:  ret =  responseCdmaSms(p); break;
2508             case RIL_UNSOL_RESPONSE_NEW_BROADCAST_SMS:  ret =  responseRaw(p); break;
2509             case RIL_UNSOL_CDMA_RUIM_SMS_STORAGE_FULL:  ret =  responseVoid(p); break;
2510             case RIL_UNSOL_ENTER_EMERGENCY_CALLBACK_MODE: ret = responseVoid(p); break;
2511             case RIL_UNSOL_CDMA_CALL_WAITING: ret = responseCdmaCallWaiting(p); break;
2512             case RIL_UNSOL_CDMA_OTA_PROVISION_STATUS: ret = responseInts(p); break;
2513             case RIL_UNSOL_CDMA_INFO_REC: ret = responseCdmaInformationRecord(p); break;
2514             case RIL_UNSOL_OEM_HOOK_RAW: ret = responseRaw(p); break;
2515             case RIL_UNSOL_RINGBACK_TONE: ret = responseInts(p); break;
2516             case RIL_UNSOL_RESEND_INCALL_MUTE: ret = responseVoid(p); break;
2517             case RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED: ret = responseInts(p); break;
2518             case RIL_UNSOl_CDMA_PRL_CHANGED: ret = responseInts(p); break;
2519             case RIL_UNSOL_EXIT_EMERGENCY_CALLBACK_MODE: ret = responseVoid(p); break;
2520             case RIL_UNSOL_RIL_CONNECTED: ret = responseInts(p); break;
2521             case RIL_UNSOL_VOICE_RADIO_TECH_CHANGED: ret =  responseInts(p); break;
2522
2523             default:
2524                 throw new RuntimeException("Unrecognized unsol response: " + response);
2525             //break; (implied)
2526         }} catch (Throwable tr) {
2527             Rlog.e(RILJ_LOG_TAG, "Exception processing unsol response: " + response +
2528                 "Exception:" + tr.toString());
2529             return;
2530         }
2531
2532         switch(response) {
2533             case RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED:
2534                 /* has bonus radio state int */
2535                 RadioState newState = getRadioStateFromInt(p.readInt());
2536                 if (RILJ_LOGD) unsljLogMore(response, newState.toString());
2537
2538                 switchToRadioState(newState);
2539             break;
2540             case RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED:
2541                 if (RILJ_LOGD) unsljLog(response);
2542
2543                 mCallStateRegistrants
2544                     .notifyRegistrants(new AsyncResult(null, null, null));
2545             break;
2546             case RIL_UNSOL_RESPONSE_VOICE_NETWORK_STATE_CHANGED:
2547                 if (RILJ_LOGD) unsljLog(response);
2548
2549                 mVoiceNetworkStateRegistrants
2550                     .notifyRegistrants(new AsyncResult(null, null, null));
2551             break;
2552             case RIL_UNSOL_RESPONSE_NEW_SMS: {
2553                 if (RILJ_LOGD) unsljLog(response);
2554
2555                 // FIXME this should move up a layer
2556                 String a[] = new String[2];
2557
2558                 a[1] = (String)ret;
2559
2560                 SmsMessage sms;
2561
2562                 sms = SmsMessage.newFromCMT(a);
2563                 if (mGsmSmsRegistrant != null) {
2564                     mGsmSmsRegistrant
2565                         .notifyRegistrant(new AsyncResult(null, sms, null));
2566                 }
2567             break;
2568             }
2569             case RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT:
2570                 if (RILJ_LOGD) unsljLogRet(response, ret);
2571
2572                 if (mSmsStatusRegistrant != null) {
2573                     mSmsStatusRegistrant.notifyRegistrant(
2574                             new AsyncResult(null, ret, null));
2575                 }
2576             break;
2577             case RIL_UNSOL_RESPONSE_NEW_SMS_ON_SIM:
2578                 if (RILJ_LOGD) unsljLogRet(response, ret);
2579
2580                 int[] smsIndex = (int[])ret;
2581
2582                 if(smsIndex.length == 1) {
2583                     if (mSmsOnSimRegistrant != null) {
2584                         mSmsOnSimRegistrant.
2585                                 notifyRegistrant(new AsyncResult(null, smsIndex, null));
2586                     }
2587                 } else {
2588                     if (RILJ_LOGD) riljLog(" NEW_SMS_ON_SIM ERROR with wrong length "
2589                             + smsIndex.length);
2590                 }
2591             break;
2592             case RIL_UNSOL_ON_USSD:
2593                 String[] resp = (String[])ret;
2594
2595                 if (resp.length < 2) {
2596                     resp = new String[2];
2597                     resp[0] = ((String[])ret)[0];
2598                     resp[1] = null;
2599                 }
2600                 if (RILJ_LOGD) unsljLogMore(response, resp[0]);
2601                 if (mUSSDRegistrant != null) {
2602                     mUSSDRegistrant.notifyRegistrant(
2603                         new AsyncResult (null, resp, null));
2604                 }
2605             break;
2606             case RIL_UNSOL_NITZ_TIME_RECEIVED:
2607                 if (RILJ_LOGD) unsljLogRet(response, ret);
2608
2609                 // has bonus long containing milliseconds since boot that the NITZ
2610                 // time was received
2611                 long nitzReceiveTime = p.readLong();
2612
2613                 Object[] result = new Object[2];
2614
2615                 result[0] = ret;
2616                 result[1] = Long.valueOf(nitzReceiveTime);
2617
2618                 boolean ignoreNitz = SystemProperties.getBoolean(
2619                         TelephonyProperties.PROPERTY_IGNORE_NITZ, false);
2620
2621                 if (ignoreNitz) {
2622                     if (RILJ_LOGD) riljLog("ignoring UNSOL_NITZ_TIME_RECEIVED");
2623                 } else {
2624                     if (mNITZTimeRegistrant != null) {
2625
2626                         mNITZTimeRegistrant
2627                             .notifyRegistrant(new AsyncResult (null, result, null));
2628                     } else {
2629                         // in case NITZ time registrant isnt registered yet
2630                         mLastNITZTimeInfo = result;
2631                     }
2632                 }
2633             break;
2634
2635             case RIL_UNSOL_SIGNAL_STRENGTH:
2636                 // Note this is set to "verbose" because it happens
2637                 // frequently
2638                 if (RILJ_LOGV) unsljLogvRet(response, ret);
2639
2640                 if (mSignalStrengthRegistrant != null) {
2641                     mSignalStrengthRegistrant.notifyRegistrant(
2642                                         new AsyncResult (null, ret, null));
2643                 }
2644             break;
2645             case RIL_UNSOL_DATA_CALL_LIST_CHANGED:
2646                 if (RILJ_LOGD) unsljLogRet(response, ret);
2647
2648                 mDataNetworkStateRegistrants.notifyRegistrants(new AsyncResult(null, ret, null));
2649             break;
2650
2651             case RIL_UNSOL_SUPP_SVC_NOTIFICATION:
2652                 if (RILJ_LOGD) unsljLogRet(response, ret);
2653
2654                 if (mSsnRegistrant != null) {
2655                     mSsnRegistrant.notifyRegistrant(
2656                                         new AsyncResult (null, ret, null));
2657                 }
2658                 break;
2659
2660             case RIL_UNSOL_STK_SESSION_END:
2661                 if (RILJ_LOGD) unsljLog(response);
2662
2663                 if (mCatSessionEndRegistrant != null) {
2664                     mCatSessionEndRegistrant.notifyRegistrant(
2665                                         new AsyncResult (null, ret, null));
2666                 }
2667                 break;
2668
2669             case RIL_UNSOL_STK_PROACTIVE_COMMAND:
2670                 if (RILJ_LOGD) unsljLogRet(response, ret);
2671
2672                 if (mCatProCmdRegistrant != null) {
2673                     mCatProCmdRegistrant.notifyRegistrant(
2674                                         new AsyncResult (null, ret, null));
2675                 }
2676                 break;
2677
2678             case RIL_UNSOL_STK_EVENT_NOTIFY:
2679                 if (RILJ_LOGD) unsljLogRet(response, ret);
2680
2681                 if (mCatEventRegistrant != null) {
2682                     mCatEventRegistrant.notifyRegistrant(
2683                                         new AsyncResult (null, ret, null));
2684                 }
2685                 break;
2686
2687             case RIL_UNSOL_STK_CALL_SETUP:
2688                 if (RILJ_LOGD) unsljLogRet(response, ret);
2689
2690                 if (mCatCallSetUpRegistrant != null) {
2691                     mCatCallSetUpRegistrant.notifyRegistrant(
2692                                         new AsyncResult (null, ret, null));
2693                 }
2694                 break;
2695
2696             case RIL_UNSOL_SIM_SMS_STORAGE_FULL:
2697                 if (RILJ_LOGD) unsljLog(response);
2698
2699                 if (mIccSmsFullRegistrant != null) {
2700                     mIccSmsFullRegistrant.notifyRegistrant();
2701                 }
2702                 break;
2703
2704             case RIL_UNSOL_SIM_REFRESH:
2705                 if (RILJ_LOGD) unsljLogRet(response, ret);
2706
2707                 if (mIccRefreshRegistrants != null) {
2708                     mIccRefreshRegistrants.notifyRegistrants(
2709                             new AsyncResult (null, ret, null));
2710                 }
2711                 break;
2712
2713             case RIL_UNSOL_CALL_RING:
2714                 if (RILJ_LOGD) unsljLogRet(response, ret);
2715
2716                 if (mRingRegistrant != null) {
2717                     mRingRegistrant.notifyRegistrant(
2718                             new AsyncResult (null, ret, null));
2719                 }
2720                 break;
2721
2722             case RIL_UNSOL_RESTRICTED_STATE_CHANGED:
2723                 if (RILJ_LOGD) unsljLogvRet(response, ret);
2724                 if (mRestrictedStateRegistrant != null) {
2725                     mRestrictedStateRegistrant.notifyRegistrant(
2726                                         new AsyncResult (null, ret, null));
2727                 }
2728                 break;
2729
2730             case RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED:
2731                 if (RILJ_LOGD) unsljLog(response);
2732
2733                 if (mIccStatusChangedRegistrants != null) {
2734                     mIccStatusChangedRegistrants.notifyRegistrants();
2735                 }
2736                 break;
2737
2738             case RIL_UNSOL_RESPONSE_CDMA_NEW_SMS:
2739                 if (RILJ_LOGD) unsljLog(response);
2740
2741                 SmsMessage sms = (SmsMessage) ret;
2742
2743                 if (mCdmaSmsRegistrant != null) {
2744                     mCdmaSmsRegistrant
2745                         .notifyRegistrant(new AsyncResult(null, sms, null));
2746                 }
2747                 break;
2748
2749             case RIL_UNSOL_RESPONSE_NEW_BROADCAST_SMS:
2750                 if (RILJ_LOGD) unsljLog(response);
2751
2752                 if (mGsmBroadcastSmsRegistrant != null) {
2753                     mGsmBroadcastSmsRegistrant
2754                         .notifyRegistrant(new AsyncResult(null, ret, null));
2755                 }
2756                 break;
2757
2758             case RIL_UNSOL_CDMA_RUIM_SMS_STORAGE_FULL:
2759                 if (RILJ_LOGD) unsljLog(response);
2760
2761                 if (mIccSmsFullRegistrant != null) {
2762                     mIccSmsFullRegistrant.notifyRegistrant();
2763                 }
2764                 break;
2765
2766             case RIL_UNSOL_ENTER_EMERGENCY_CALLBACK_MODE:
2767                 if (RILJ_LOGD) unsljLog(response);
2768
2769                 if (mEmergencyCallbackModeRegistrant != null) {
2770                     mEmergencyCallbackModeRegistrant.notifyRegistrant();
2771                 }
2772                 break;
2773
2774             case RIL_UNSOL_CDMA_CALL_WAITING:
2775                 if (RILJ_LOGD) unsljLogRet(response, ret);
2776
2777                 if (mCallWaitingInfoRegistrants != null) {
2778                     mCallWaitingInfoRegistrants.notifyRegistrants(
2779                                         new AsyncResult (null, ret, null));
2780                 }
2781                 break;
2782
2783             case RIL_UNSOL_CDMA_OTA_PROVISION_STATUS:
2784                 if (RILJ_LOGD) unsljLogRet(response, ret);
2785
2786                 if (mOtaProvisionRegistrants != null) {
2787                     mOtaProvisionRegistrants.notifyRegistrants(
2788                                         new AsyncResult (null, ret, null));
2789                 }
2790                 break;
2791
2792             case RIL_UNSOL_CDMA_INFO_REC:
2793                 ArrayList<CdmaInformationRecords> listInfoRecs;
2794
2795                 try {
2796                     listInfoRecs = (ArrayList<CdmaInformationRecords>)ret;
2797                 } catch (ClassCastException e) {
2798                     Rlog.e(RILJ_LOG_TAG, "Unexpected exception casting to listInfoRecs", e);
2799                     break;
2800                 }
2801
2802                 for (CdmaInformationRecords rec : listInfoRecs) {
2803                     if (RILJ_LOGD) unsljLogRet(response, rec);
2804                     notifyRegistrantsCdmaInfoRec(rec);
2805                 }
2806                 break;
2807
2808             case RIL_UNSOL_OEM_HOOK_RAW:
2809                 if (RILJ_LOGD) unsljLogvRet(response, IccUtils.bytesToHexString((byte[])ret));
2810                 if (mUnsolOemHookRawRegistrant != null) {
2811                     mUnsolOemHookRawRegistrant.notifyRegistrant(new AsyncResult(null, ret, null));
2812                 }
2813                 break;
2814
2815             case RIL_UNSOL_RINGBACK_TONE:
2816                 if (RILJ_LOGD) unsljLogvRet(response, ret);
2817                 if (mRingbackToneRegistrants != null) {
2818                     boolean playtone = (((int[])ret)[0] == 1);
2819                     mRingbackToneRegistrants.notifyRegistrants(
2820                                         new AsyncResult (null, playtone, null));
2821                 }
2822                 break;
2823
2824             case RIL_UNSOL_RESEND_INCALL_MUTE:
2825                 if (RILJ_LOGD) unsljLogRet(response, ret);
2826
2827                 if (mResendIncallMuteRegistrants != null) {
2828                     mResendIncallMuteRegistrants.notifyRegistrants(
2829                                         new AsyncResult (null, ret, null));
2830                 }
2831                 break;
2832
2833             case RIL_UNSOL_VOICE_RADIO_TECH_CHANGED:
2834                 if (RILJ_LOGD) unsljLogRet(response, ret);
2835
2836                 if (mVoiceRadioTechChangedRegistrants != null) {
2837                     mVoiceRadioTechChangedRegistrants.notifyRegistrants(
2838                             new AsyncResult(null, ret, null));
2839                 }
2840                 break;
2841
2842             case RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED:
2843                 if (RILJ_LOGD) unsljLogRet(response, ret);
2844
2845                 if (mCdmaSubscriptionChangedRegistrants != null) {
2846                     mCdmaSubscriptionChangedRegistrants.notifyRegistrants(
2847                                         new AsyncResult (null, ret, null));
2848                 }
2849                 break;
2850
2851             case RIL_UNSOl_CDMA_PRL_CHANGED:
2852                 if (RILJ_LOGD) unsljLogRet(response, ret);
2853
2854                 if (mCdmaPrlChangedRegistrants != null) {
2855                     mCdmaPrlChangedRegistrants.notifyRegistrants(
2856                                         new AsyncResult (null, ret, null));
2857                 }
2858                 break;
2859
2860             case RIL_UNSOL_EXIT_EMERGENCY_CALLBACK_MODE:
2861                 if (RILJ_LOGD) unsljLogRet(response, ret);
2862
2863                 if (mExitEmergencyCallbackModeRegistrants != null) {
2864                     mExitEmergencyCallbackModeRegistrants.notifyRegistrants(
2865                                         new AsyncResult (null, null, null));
2866                 }
2867                 break;
2868
2869             case RIL_UNSOL_RIL_CONNECTED: {
2870                 if (RILJ_LOGD) unsljLogRet(response, ret);
2871
2872                 // Initial conditions
2873                 setRadioPower(false, null);
2874                 setPreferredNetworkType(mPreferredNetworkType, null);
2875                 setCdmaSubscriptionSource(mCdmaSubscription, null);
2876                 notifyRegistrantsRilConnectionChanged(((int[])ret)[0]);
2877                 break;
2878             }
2879         }
2880     }
2881
2882     /**
2883      * Notifiy all registrants that the ril has connected or disconnected.
2884      *
2885      * @param rilVer is the version of the ril or -1 if disconnected.
2886      */
2887     private void notifyRegistrantsRilConnectionChanged(int rilVer) {
2888         mRilVersion = rilVer;
2889         if (mRilConnectedRegistrants != null) {
2890             mRilConnectedRegistrants.notifyRegistrants(
2891                                 new AsyncResult (null, new Integer(rilVer), null));
2892         }
2893     }
2894
2895     private Object
2896     responseInts(Parcel p) {
2897         int numInts;
2898         int response[];
2899
2900         numInts = p.readInt();
2901
2902         response = new int[numInts];
2903
2904         for (int i = 0 ; i < numInts ; i++) {
2905             response[i] = p.readInt();
2906         }
2907
2908         return response;
2909     }
2910
2911
2912     private Object
2913     responseVoid(Parcel p) {
2914         return null;
2915     }
2916
2917     private Object
2918     responseCallForward(Parcel p) {
2919         int numInfos;
2920         CallForwardInfo infos[];
2921
2922         numInfos = p.readInt();
2923
2924         infos = new CallForwardInfo[numInfos];
2925
2926         for (int i = 0 ; i < numInfos ; i++) {
2927             infos[i] = new CallForwardInfo();
2928
2929             infos[i].status = p.readInt();
2930             infos[i].reason = p.readInt();
2931             infos[i].serviceClass = p.readInt();
2932             infos[i].toa = p.readInt();
2933             infos[i].number = p.readString();
2934             infos[i].timeSeconds = p.readInt();
2935         }
2936
2937         return infos;
2938     }
2939
2940     private Object
2941     responseSuppServiceNotification(Parcel p) {
2942         SuppServiceNotification notification = new SuppServiceNotification();
2943
2944         notification.notificationType = p.readInt();
2945         notification.code = p.readInt();
2946         notification.index = p.readInt();
2947         notification.type = p.readInt();
2948         notification.number = p.readString();
2949
2950         return notification;
2951     }
2952
2953     private Object
2954     responseCdmaSms(Parcel p) {
2955         SmsMessage sms;
2956         sms = SmsMessage.newFromParcel(p);
2957
2958         return sms;
2959     }
2960
2961     private Object
2962     responseString(Parcel p) {
2963         String response;
2964
2965         response = p.readString();
2966
2967         return response;
2968     }
2969
2970     private Object
2971     responseStrings(Parcel p) {
2972         int num;
2973         String response[];
2974
2975         response = p.readStringArray();
2976
2977         if (false) {
2978             num = p.readInt();
2979
2980             response = new String[num];
2981             for (int i = 0; i < num; i++) {
2982                 response[i] = p.readString();
2983             }
2984         }
2985
2986         return response;
2987     }
2988
2989     private Object
2990     responseRaw(Parcel p) {
2991         int num;
2992         byte response[];
2993
2994         response = p.createByteArray();
2995
2996         return response;
2997     }
2998
2999     private Object
3000     responseSMS(Parcel p) {
3001         int messageRef, errorCode;
3002         String ackPDU;
3003
3004         messageRef = p.readInt();
3005         ackPDU = p.readString();
3006         errorCode = p.readInt();
3007
3008         SmsResponse response = new SmsResponse(messageRef, ackPDU, errorCode);
3009
3010         return response;
3011     }
3012
3013
3014     private Object
3015     responseICC_IO(Parcel p) {
3016         int sw1, sw2;
3017         byte data[] = null;
3018         Message ret;
3019
3020         sw1 = p.readInt();
3021         sw2 = p.readInt();
3022
3023         String s = p.readString();
3024
3025         if (RILJ_LOGV) riljLog("< iccIO: "
3026                 + " 0x" + Integer.toHexString(sw1)
3027                 + " 0x" + Integer.toHexString(sw2) + " "
3028                 + s);
3029
3030         return new IccIoResult(sw1, sw2, s);
3031     }
3032
3033     private Object
3034     responseIccCardStatus(Parcel p) {
3035         IccCardApplicationStatus appStatus;
3036
3037         IccCardStatus cardStatus = new IccCardStatus();
3038         cardStatus.setCardState(p.readInt());
3039         cardStatus.setUniversalPinState(p.readInt());
3040         cardStatus.mGsmUmtsSubscriptionAppIndex = p.readInt();
3041         cardStatus.mCdmaSubscriptionAppIndex = p.readInt();
3042         cardStatus.mImsSubscriptionAppIndex = p.readInt();
3043         int numApplications = p.readInt();
3044
3045         // limit to maximum allowed applications
3046         if (numApplications > IccCardStatus.CARD_MAX_APPS) {
3047             numApplications = IccCardStatus.CARD_MAX_APPS;
3048         }
3049         cardStatus.mApplications = new IccCardApplicationStatus[numApplications];
3050         for (int i = 0 ; i < numApplications ; i++) {
3051             appStatus = new IccCardApplicationStatus();
3052             appStatus.app_type       = appStatus.AppTypeFromRILInt(p.readInt());
3053             appStatus.app_state      = appStatus.AppStateFromRILInt(p.readInt());
3054             appStatus.perso_substate = appStatus.PersoSubstateFromRILInt(p.readInt());
3055             appStatus.aid            = p.readString();
3056             appStatus.app_label      = p.readString();
3057             appStatus.pin1_replaced  = p.readInt();
3058             appStatus.pin1           = appStatus.PinStateFromRILInt(p.readInt());
3059             appStatus.pin2           = appStatus.PinStateFromRILInt(p.readInt());
3060             cardStatus.mApplications[i] = appStatus;
3061         }
3062         return cardStatus;
3063     }
3064
3065     private Object
3066     responseSimRefresh(Parcel p) {
3067         IccRefreshResponse response = new IccRefreshResponse();
3068
3069         response.refreshResult = p.readInt();
3070         response.efId   = p.readInt();
3071         response.aid = p.readString();
3072         return response;
3073     }
3074
3075     private Object
3076     responseCallList(Parcel p) {
3077         int num;
3078         int voiceSettings;
3079         ArrayList<DriverCall> response;
3080         DriverCall dc;
3081
3082         num = p.readInt();
3083         response = new ArrayList<DriverCall>(num);
3084
3085         if (RILJ_LOGV) {
3086             riljLog("responseCallList: num=" + num +
3087                     " mEmergencyCallbackModeRegistrant=" + mEmergencyCallbackModeRegistrant +
3088                     " mTestingEmergencyCall=" + mTestingEmergencyCall.get());
3089         }
3090         for (int i = 0 ; i < num ; i++) {
3091             dc = new DriverCall();
3092
3093             dc.state = DriverCall.stateFromCLCC(p.readInt());
3094             dc.index = p.readInt();
3095             dc.TOA = p.readInt();
3096             dc.isMpty = (0 != p.readInt());
3097             dc.isMT = (0 != p.readInt());
3098             dc.als = p.readInt();
3099             voiceSettings = p.readInt();
3100             dc.isVoice = (0 == voiceSettings) ? false : true;
3101             dc.isVoicePrivacy = (0 != p.readInt());
3102             dc.number = p.readString();
3103             int np = p.readInt();
3104             dc.numberPresentation = DriverCall.presentationFromCLIP(np);
3105             dc.name = p.readString();
3106             dc.namePresentation = p.readInt();
3107             int uusInfoPresent = p.readInt();
3108             if (uusInfoPresent == 1) {
3109                 dc.uusInfo = new UUSInfo();
3110                 dc.uusInfo.setType(p.readInt());
3111                 dc.uusInfo.setDcs(p.readInt());
3112                 byte[] userData = p.createByteArray();
3113                 dc.uusInfo.setUserData(userData);
3114                 riljLogv(String.format("Incoming UUS : type=%d, dcs=%d, length=%d",
3115                                 dc.uusInfo.getType(), dc.uusInfo.getDcs(),
3116                                 dc.uusInfo.getUserData().length));
3117                 riljLogv("Incoming UUS : data (string)="
3118                         + new String(dc.uusInfo.getUserData()));
3119                 riljLogv("Incoming UUS : data (hex): "
3120                         + IccUtils.bytesToHexString(dc.uusInfo.getUserData()));
3121             } else {
3122                 riljLogv("Incoming UUS : NOT present!");
3123             }
3124
3125             // Make sure there's a leading + on addresses with a TOA of 145
3126             dc.number = PhoneNumberUtils.stringFromStringAndTOA(dc.number, dc.TOA);
3127
3128             response.add(dc);
3129
3130             if (dc.isVoicePrivacy) {
3131                 mVoicePrivacyOnRegistrants.notifyRegistrants();
3132                 riljLog("InCall VoicePrivacy is enabled");
3133             } else {
3134                 mVoicePrivacyOffRegistrants.notifyRegistrants();
3135                 riljLog("InCall VoicePrivacy is disabled");
3136             }
3137         }
3138
3139         Collections.sort(response);
3140
3141         if ((num == 0) && mTestingEmergencyCall.getAndSet(false)) {
3142             if (mEmergencyCallbackModeRegistrant != null) {
3143                 riljLog("responseCallList: call ended, testing emergency call," +
3144                             " notify ECM Registrants");
3145                 mEmergencyCallbackModeRegistrant.notifyRegistrant();
3146             }
3147         }
3148
3149         return response;
3150     }
3151
3152     private DataCallState getDataCallState(Parcel p, int version) {
3153         DataCallState dataCall = new DataCallState();
3154
3155         dataCall.version = version;
3156         if (version < 5) {
3157             dataCall.cid = p.readInt();
3158             dataCall.active = p.readInt();
3159             dataCall.type = p.readString();
3160             String addresses = p.readString();
3161             if (!TextUtils.isEmpty(addresses)) {
3162                 dataCall.addresses = addresses.split(" ");
3163             }
3164         } else {
3165             dataCall.status = p.readInt();
3166             dataCall.suggestedRetryTime = p.readInt();
3167             dataCall.cid = p.readInt();
3168             dataCall.active = p.readInt();
3169             dataCall.type = p.readString();
3170             dataCall.ifname = p.readString();
3171             if ((dataCall.status == DataConnection.FailCause.NONE.getErrorCode()) &&
3172                     TextUtils.isEmpty(dataCall.ifname)) {
3173               throw new RuntimeException("getDataCallState, no ifname");
3174             }
3175             String addresses = p.readString();
3176             if (!TextUtils.isEmpty(addresses)) {
3177                 dataCall.addresses = addresses.split(" ");
3178             }
3179             String dnses = p.readString();
3180             if (!TextUtils.isEmpty(dnses)) {
3181                 dataCall.dnses = dnses.split(" ");
3182             }
3183             String gateways = p.readString();
3184             if (!TextUtils.isEmpty(gateways)) {
3185                 dataCall.gateways = gateways.split(" ");
3186             }
3187         }
3188         return dataCall;
3189     }
3190
3191     private Object
3192     responseDataCallList(Parcel p) {
3193         ArrayList<DataCallState> response;
3194
3195         int ver = p.readInt();
3196         int num = p.readInt();
3197         riljLog("responseDataCallList ver=" + ver + " num=" + num);
3198
3199         response = new ArrayList<DataCallState>(num);
3200         for (int i = 0; i < num; i++) {
3201             response.add(getDataCallState(p, ver));
3202         }
3203
3204         return response;
3205     }
3206
3207     private Object
3208     responseSetupDataCall(Parcel p) {
3209         int ver = p.readInt();
3210         int num = p.readInt();
3211         if (RILJ_LOGV) riljLog("responseSetupDataCall ver=" + ver + " num=" + num);
3212
3213         DataCallState dataCall;
3214
3215         if (ver < 5) {
3216             dataCall = new DataCallState();
3217             dataCall.version = ver;
3218             dataCall.cid = Integer.parseInt(p.readString());
3219             dataCall.ifname = p.readString();
3220             if (TextUtils.isEmpty(dataCall.ifname)) {
3221                 throw new RuntimeException(
3222                         "RIL_REQUEST_SETUP_DATA_CALL response, no ifname");
3223             }
3224             String addresses = p.readString();
3225             if (!TextUtils.isEmpty(addresses)) {
3226               dataCall.addresses = addresses.split(" ");
3227             }
3228             if (num >= 4) {
3229                 String dnses = p.readString();
3230                 if (RILJ_LOGD) riljLog("responseSetupDataCall got dnses=" + dnses);
3231                 if (!TextUtils.isEmpty(dnses)) {
3232                     dataCall.dnses = dnses.split(" ");
3233                 }
3234             }
3235             if (num >= 5) {
3236                 String gateways = p.readString();
3237                 if (RILJ_LOGD) riljLog("responseSetupDataCall got gateways=" + gateways);
3238                 if (!TextUtils.isEmpty(gateways)) {
3239                     dataCall.gateways = gateways.split(" ");
3240                 }
3241             }
3242         } else {
3243             if (num != 1) {
3244                 throw new RuntimeException(
3245                         "RIL_REQUEST_SETUP_DATA_CALL response expecting 1 RIL_Data_Call_response_v5"
3246                         + " got " + num);
3247             }
3248             dataCall = getDataCallState(p, ver);
3249         }
3250
3251         return dataCall;
3252     }
3253
3254     private Object
3255     responseOperatorInfos(Parcel p) {
3256         String strings[] = (String [])responseStrings(p);
3257         ArrayList<OperatorInfo> ret;
3258
3259         if (strings.length % 4 != 0) {
3260             throw new RuntimeException(
3261                 "RIL_REQUEST_QUERY_AVAILABLE_NETWORKS: invalid response. Got "
3262                 + strings.length + " strings, expected multible of 4");
3263         }
3264
3265         ret = new ArrayList<OperatorInfo>(strings.length / 4);
3266
3267         for (int i = 0 ; i < strings.length ; i += 4) {
3268             ret.add (
3269                 new OperatorInfo(
3270                     strings[i+0],
3271                     strings[i+1],
3272                     strings[i+2],
3273                     strings[i+3]));
3274         }
3275
3276         return ret;
3277     }
3278
3279     private Object
3280     responseCellList(Parcel p) {
3281        int num, rssi;
3282        String location;
3283        ArrayList<NeighboringCellInfo> response;
3284        NeighboringCellInfo cell;
3285
3286        num = p.readInt();
3287        response = new ArrayList<NeighboringCellInfo>();
3288
3289        // Determine the radio access type
3290        String radioString = SystemProperties.get(
3291                TelephonyProperties.PROPERTY_DATA_NETWORK_TYPE, "unknown");
3292        int radioType;
3293        if (radioString.equals("GPRS")) {
3294            radioType = NETWORK_TYPE_GPRS;
3295        } else if (radioString.equals("EDGE")) {
3296            radioType = NETWORK_TYPE_EDGE;
3297        } else if (radioString.equals("UMTS")) {
3298            radioType = NETWORK_TYPE_UMTS;
3299        } else if (radioString.equals("HSDPA")) {
3300            radioType = NETWORK_TYPE_HSDPA;
3301        } else if (radioString.equals("HSUPA")) {
3302            radioType = NETWORK_TYPE_HSUPA;
3303        } else if (radioString.equals("HSPA")) {
3304            radioType = NETWORK_TYPE_HSPA;
3305        } else {
3306            radioType = NETWORK_TYPE_UNKNOWN;
3307        }
3308
3309        // Interpret the location based on radio access type
3310        if (radioType != NETWORK_TYPE_UNKNOWN) {
3311            for (int i = 0 ; i < num ; i++) {
3312                rssi = p.readInt();
3313                location = p.readString();
3314                cell = new NeighboringCellInfo(rssi, location, radioType);
3315                response.add(cell);
3316            }
3317        }
3318        return response;
3319     }
3320
3321     private Object responseGetPreferredNetworkType(Parcel p) {
3322        int [] response = (int[]) responseInts(p);
3323
3324        if (response.length >= 1) {
3325            // Since this is the response for getPreferredNetworkType
3326            // we'll assume that it should be the value we want the
3327            // vendor ril to take if we reestablish a connection to it.
3328            mPreferredNetworkType = response[0];
3329        }
3330        return response;
3331     }
3332
3333     private Object responseGmsBroadcastConfig(Parcel p) {
3334         int num;
3335         ArrayList<SmsBroadcastConfigInfo> response;
3336         SmsBroadcastConfigInfo info;
3337
3338         num = p.readInt();
3339         response = new ArrayList<SmsBroadcastConfigInfo>(num);
3340
3341         for (int i = 0; i < num; i++) {
3342             int fromId = p.readInt();
3343             int toId = p.readInt();
3344             int fromScheme = p.readInt();
3345             int toScheme = p.readInt();
3346             boolean selected = (p.readInt() == 1);
3347
3348             info = new SmsBroadcastConfigInfo(fromId, toId, fromScheme,
3349                     toScheme, selected);
3350             response.add(info);
3351         }
3352         return response;
3353     }
3354
3355     private Object
3356     responseCdmaBroadcastConfig(Parcel p) {
3357         int numServiceCategories;
3358         int response[];
3359
3360         numServiceCategories = p.readInt();
3361
3362         if (numServiceCategories == 0) {
3363             // TODO: The logic of providing default values should
3364             // not be done by this transport layer. And needs to
3365             // be done by the vendor ril or application logic.
3366             int numInts;
3367             numInts = CDMA_BROADCAST_SMS_NO_OF_SERVICE_CATEGORIES * CDMA_BSI_NO_OF_INTS_STRUCT + 1;
3368             response = new int[numInts];
3369
3370             // Faking a default record for all possible records.
3371             response[0] = CDMA_BROADCAST_SMS_NO_OF_SERVICE_CATEGORIES;
3372
3373             // Loop over CDMA_BROADCAST_SMS_NO_OF_SERVICE_CATEGORIES set 'english' as
3374             // default language and selection status to false for all.
3375             for (int i = 1; i < numInts; i += CDMA_BSI_NO_OF_INTS_STRUCT ) {
3376                 response[i + 0] = i / CDMA_BSI_NO_OF_INTS_STRUCT;
3377                 response[i + 1] = 1;
3378                 response[i + 2] = 0;
3379             }
3380         } else {
3381             int numInts;
3382             numInts = (numServiceCategories * CDMA_BSI_NO_OF_INTS_STRUCT) + 1;
3383             response = new int[numInts];
3384
3385             response[0] = numServiceCategories;
3386             for (int i = 1 ; i < numInts; i++) {
3387                  response[i] = p.readInt();
3388              }
3389         }
3390
3391         return response;
3392     }
3393
3394     private Object
3395     responseSignalStrength(Parcel p) {
3396         SignalStrength signalStrength = new SignalStrength(p);
3397         return signalStrength;
3398     }
3399
3400     private ArrayList<CdmaInformationRecords>
3401     responseCdmaInformationRecord(Parcel p) {
3402         int numberOfInfoRecs;
3403         ArrayList<CdmaInformationRecords> response;
3404
3405         /**
3406          * Loop through all of the information records unmarshalling them
3407          * and converting them to Java Objects.
3408          */
3409         numberOfInfoRecs = p.readInt();
3410         response = new ArrayList<CdmaInformationRecords>(numberOfInfoRecs);
3411
3412         for (int i = 0; i < numberOfInfoRecs; i++) {
3413             CdmaInformationRecords InfoRec = new CdmaInformationRecords(p);
3414             response.add(InfoRec);
3415         }
3416
3417         return response;
3418     }
3419
3420     private Object
3421     responseCdmaCallWaiting(Parcel p) {
3422         CdmaCallWaitingNotification notification = new CdmaCallWaitingNotification();
3423
3424         notification.number = p.readString();
3425         notification.numberPresentation = notification.presentationFromCLIP(p.readInt());
3426         notification.name = p.readString();
3427         notification.namePresentation = notification.numberPresentation;
3428         notification.isPresent = p.readInt();
3429         notification.signalType = p.readInt();
3430         notification.alertPitch = p.readInt();
3431         notification.signal = p.readInt();
3432         notification.numberType = p.readInt();
3433         notification.numberPlan = p.readInt();
3434
3435         return notification;
3436     }
3437
3438     private Object
3439     responseCallRing(Parcel p){
3440         char response[] = new char[4];
3441
3442         response[0] = (char) p.readInt();    // isPresent
3443         response[1] = (char) p.readInt();    // signalType
3444         response[2] = (char) p.readInt();    // alertPitch
3445         response[3] = (char) p.readInt();    // signal
3446
3447         return response;
3448     }
3449
3450     private void
3451     notifyRegistrantsCdmaInfoRec(CdmaInformationRecords infoRec) {
3452         int response = RIL_UNSOL_CDMA_INFO_REC;
3453         if (infoRec.record instanceof CdmaInformationRecords.CdmaDisplayInfoRec) {
3454             if (mDisplayInfoRegistrants != null) {
3455                 if (RILJ_LOGD) unsljLogRet(response, infoRec.record);
3456                 mDisplayInfoRegistrants.notifyRegistrants(
3457                         new AsyncResult (null, infoRec.record, null));
3458             }
3459         } else if (infoRec.record instanceof CdmaInformationRecords.CdmaSignalInfoRec) {
3460             if (mSignalInfoRegistrants != null) {
3461                 if (RILJ_LOGD) unsljLogRet(response, infoRec.record);
3462                 mSignalInfoRegistrants.notifyRegistrants(
3463                         new AsyncResult (null, infoRec.record, null));
3464             }
3465         } else if (infoRec.record instanceof CdmaInformationRecords.CdmaNumberInfoRec) {
3466             if (mNumberInfoRegistrants != null) {
3467                 if (RILJ_LOGD) unsljLogRet(response, infoRec.record);
3468                 mNumberInfoRegistrants.notifyRegistrants(
3469                         new AsyncResult (null, infoRec.record, null));
3470             }
3471         } else if (infoRec.record instanceof CdmaInformationRecords.CdmaRedirectingNumberInfoRec) {
3472             if (mRedirNumInfoRegistrants != null) {
3473                 if (RILJ_LOGD) unsljLogRet(response, infoRec.record);
3474                 mRedirNumInfoRegistrants.notifyRegistrants(
3475                         new AsyncResult (null, infoRec.record, null));
3476             }
3477         } else if (infoRec.record instanceof CdmaInformationRecords.CdmaLineControlInfoRec) {
3478             if (mLineControlInfoRegistrants != null) {
3479                 if (RILJ_LOGD) unsljLogRet(response, infoRec.record);
3480                 mLineControlInfoRegistrants.notifyRegistrants(
3481                         new AsyncResult (null, infoRec.record, null));
3482             }
3483         } else if (infoRec.record instanceof CdmaInformationRecords.CdmaT53ClirInfoRec) {
3484             if (mT53ClirInfoRegistrants != null) {
3485                 if (RILJ_LOGD) unsljLogRet(response, infoRec.record);
3486                 mT53ClirInfoRegistrants.notifyRegistrants(
3487                         new AsyncResult (null, infoRec.record, null));
3488             }
3489         } else if (infoRec.record instanceof CdmaInformationRecords.CdmaT53AudioControlInfoRec) {
3490             if (mT53AudCntrlInfoRegistrants != null) {
3491                if (RILJ_LOGD) unsljLogRet(response, infoRec.record);
3492                mT53AudCntrlInfoRegistrants.notifyRegistrants(
3493                        new AsyncResult (null, infoRec.record, null));
3494             }
3495         }
3496     }
3497
3498     static String
3499     requestToString(int request) {
3500 /*
3501  cat libs/telephony/ril_commands.h \
3502  | egrep "^ *{RIL_" \
3503  | sed -re 's/\{RIL_([^,]+),[^,]+,([^}]+).+/case RIL_\1: return "\1";/'
3504 */
3505         switch(request) {
3506             case RIL_REQUEST_GET_SIM_STATUS: return "GET_SIM_STATUS";
3507             case RIL_REQUEST_ENTER_SIM_PIN: return "ENTER_SIM_PIN";
3508             case RIL_REQUEST_ENTER_SIM_PUK: return "ENTER_SIM_PUK";
3509             case RIL_REQUEST_ENTER_SIM_PIN2: return "ENTER_SIM_PIN2";
3510             case RIL_REQUEST_ENTER_SIM_PUK2: return "ENTER_SIM_PUK2";
3511             case RIL_REQUEST_CHANGE_SIM_PIN: return "CHANGE_SIM_PIN";
3512             case RIL_REQUEST_CHANGE_SIM_PIN2: return "CHANGE_SIM_PIN2";
3513             case RIL_REQUEST_ENTER_NETWORK_DEPERSONALIZATION: return "ENTER_NETWORK_DEPERSONALIZATION";
3514             case RIL_REQUEST_GET_CURRENT_CALLS: return "GET_CURRENT_CALLS";
3515             case RIL_REQUEST_DIAL: return "DIAL";
3516             case RIL_REQUEST_GET_IMSI: return "GET_IMSI";
3517             case RIL_REQUEST_HANGUP: return "HANGUP";
3518             case RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND: return "HANGUP_WAITING_OR_BACKGROUND";
3519             case RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND: return "HANGUP_FOREGROUND_RESUME_BACKGROUND";
3520             case RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE: return "REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE";
3521             case RIL_REQUEST_CONFERENCE: return "CONFERENCE";
3522             case RIL_REQUEST_UDUB: return "UDUB";
3523             case RIL_REQUEST_LAST_CALL_FAIL_CAUSE: return "LAST_CALL_FAIL_CAUSE";
3524             case RIL_REQUEST_SIGNAL_STRENGTH: return "SIGNAL_STRENGTH";
3525             case RIL_REQUEST_VOICE_REGISTRATION_STATE: return "VOICE_REGISTRATION_STATE";
3526             case RIL_REQUEST_DATA_REGISTRATION_STATE: return "DATA_REGISTRATION_STATE";
3527             case RIL_REQUEST_OPERATOR: return "OPERATOR";
3528             case RIL_REQUEST_RADIO_POWER: return "RADIO_POWER";
3529             case RIL_REQUEST_DTMF: return "DTMF";
3530             case RIL_REQUEST_SEND_SMS: return "SEND_SMS";
3531             case RIL_REQUEST_SEND_SMS_EXPECT_MORE: return "SEND_SMS_EXPECT_MORE";
3532             case RIL_REQUEST_SETUP_DATA_CALL: return "SETUP_DATA_CALL";
3533             case RIL_REQUEST_SIM_IO: return "SIM_IO";
3534             case RIL_REQUEST_SEND_USSD: return "SEND_USSD";
3535             case RIL_REQUEST_CANCEL_USSD: return "CANCEL_USSD";
3536             case RIL_REQUEST_GET_CLIR: return "GET_CLIR";
3537             case RIL_REQUEST_SET_CLIR: return "SET_CLIR";
3538             case RIL_REQUEST_QUERY_CALL_FORWARD_STATUS: return "QUERY_CALL_FORWARD_STATUS";
3539             case RIL_REQUEST_SET_CALL_FORWARD: return "SET_CALL_FORWARD";
3540             case RIL_REQUEST_QUERY_CALL_WAITING: return "QUERY_CALL_WAITING";
3541             case RIL_REQUEST_SET_CALL_WAITING: return "SET_CALL_WAITING";
3542             case RIL_REQUEST_SMS_ACKNOWLEDGE: return "SMS_ACKNOWLEDGE";
3543             case RIL_REQUEST_GET_IMEI: return "GET_IMEI";
3544             case RIL_REQUEST_GET_IMEISV: return "GET_IMEISV";
3545             case RIL_REQUEST_ANSWER: return "ANSWER";
3546             case RIL_REQUEST_DEACTIVATE_DATA_CALL: return "DEACTIVATE_DATA_CALL";
3547             case RIL_REQUEST_QUERY_FACILITY_LOCK: return "QUERY_FACILITY_LOCK";
3548             case RIL_REQUEST_SET_FACILITY_LOCK: return "SET_FACILITY_LOCK";
3549             case RIL_REQUEST_CHANGE_BARRING_PASSWORD: return "CHANGE_BARRING_PASSWORD";
3550             case RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE: return "QUERY_NETWORK_SELECTION_MODE";
3551             case RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC: return "SET_NETWORK_SELECTION_AUTOMATIC";
3552             case RIL_REQUEST_SET_NETWORK_SELECTION_MANUAL: return "SET_NETWORK_SELECTION_MANUAL";
3553             case RIL_REQUEST_QUERY_AVAILABLE_NETWORKS : return "QUERY_AVAILABLE_NETWORKS ";
3554             case RIL_REQUEST_DTMF_START: return "DTMF_START";
3555             case RIL_REQUEST_DTMF_STOP: return "DTMF_STOP";
3556             case RIL_REQUEST_BASEBAND_VERSION: return "BASEBAND_VERSION";
3557             case RIL_REQUEST_SEPARATE_CONNECTION: return "SEPARATE_CONNECTION";
3558             case RIL_REQUEST_SET_MUTE: return "SET_MUTE";
3559             case RIL_REQUEST_GET_MUTE: return "GET_MUTE";
3560             case RIL_REQUEST_QUERY_CLIP: return "QUERY_CLIP";
3561             case RIL_REQUEST_LAST_DATA_CALL_FAIL_CAUSE: return "LAST_DATA_CALL_FAIL_CAUSE";
3562             case RIL_REQUEST_DATA_CALL_LIST: return "DATA_CALL_LIST";
3563             case RIL_REQUEST_RESET_RADIO: return "RESET_RADIO";
3564             case RIL_REQUEST_OEM_HOOK_RAW: return "OEM_HOOK_RAW";
3565             case RIL_REQUEST_OEM_HOOK_STRINGS: return "OEM_HOOK_STRINGS";
3566             case RIL_REQUEST_SCREEN_STATE: return "SCREEN_STATE";
3567             case RIL_REQUEST_SET_SUPP_SVC_NOTIFICATION: return "SET_SUPP_SVC_NOTIFICATION";
3568             case RIL_REQUEST_WRITE_SMS_TO_SIM: return "WRITE_SMS_TO_SIM";
3569             case RIL_REQUEST_DELETE_SMS_ON_SIM: return "DELETE_SMS_ON_SIM";
3570             case RIL_REQUEST_SET_BAND_MODE: return "SET_BAND_MODE";
3571             case RIL_REQUEST_QUERY_AVAILABLE_BAND_MODE: return "QUERY_AVAILABLE_BAND_MODE";
3572             case RIL_REQUEST_STK_GET_PROFILE: return "REQUEST_STK_GET_PROFILE";
3573             case RIL_REQUEST_STK_SET_PROFILE: return "REQUEST_STK_SET_PROFILE";
3574             case RIL_REQUEST_STK_SEND_ENVELOPE_COMMAND: return "REQUEST_STK_SEND_ENVELOPE_COMMAND";
3575             case RIL_REQUEST_STK_SEND_TERMINAL_RESPONSE: return "REQUEST_STK_SEND_TERMINAL_RESPONSE";
3576             case RIL_REQUEST_STK_HANDLE_CALL_SETUP_REQUESTED_FROM_SIM: return "REQUEST_STK_HANDLE_CALL_SETUP_REQUESTED_FROM_SIM";
3577             case RIL_REQUEST_EXPLICIT_CALL_TRANSFER: return "REQUEST_EXPLICIT_CALL_TRANSFER";
3578             case RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE: return "REQUEST_SET_PREFERRED_NETWORK_TYPE";
3579             case RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE: return "REQUEST_GET_PREFERRED_NETWORK_TYPE";
3580             case RIL_REQUEST_GET_NEIGHBORING_CELL_IDS: return "REQUEST_GET_NEIGHBORING_CELL_IDS";
3581             case RIL_REQUEST_SET_LOCATION_UPDATES: return "REQUEST_SET_LOCATION_UPDATES";
3582             case RIL_REQUEST_CDMA_SET_SUBSCRIPTION_SOURCE: return "RIL_REQUEST_CDMA_SET_SUBSCRIPTION_SOURCE";
3583             case RIL_REQUEST_CDMA_SET_ROAMING_PREFERENCE: return "RIL_REQUEST_CDMA_SET_ROAMING_PREFERENCE";
3584             case RIL_REQUEST_CDMA_QUERY_ROAMING_PREFERENCE: return "RIL_REQUEST_CDMA_QUERY_ROAMING_PREFERENCE";
3585             case RIL_REQUEST_SET_TTY_MODE: return "RIL_REQUEST_SET_TTY_MODE";
3586             case RIL_REQUEST_QUERY_TTY_MODE: return "RIL_REQUEST_QUERY_TTY_MODE";
3587             case RIL_REQUEST_CDMA_SET_PREFERRED_VOICE_PRIVACY_MODE: return "RIL_REQUEST_CDMA_SET_PREFERRED_VOICE_PRIVACY_MODE";
3588             case RIL_REQUEST_CDMA_QUERY_PREFERRED_VOICE_PRIVACY_MODE: return "RIL_REQUEST_CDMA_QUERY_PREFERRED_VOICE_PRIVACY_MODE";
3589             case RIL_REQUEST_CDMA_FLASH: return "RIL_REQUEST_CDMA_FLASH";
3590             case RIL_REQUEST_CDMA_BURST_DTMF: return "RIL_REQUEST_CDMA_BURST_DTMF";
3591             case RIL_REQUEST_CDMA_SEND_SMS: return "RIL_REQUEST_CDMA_SEND_SMS";
3592             case RIL_REQUEST_CDMA_SMS_ACKNOWLEDGE: return "RIL_REQUEST_CDMA_SMS_ACKNOWLEDGE";
3593             case RIL_REQUEST_GSM_GET_BROADCAST_CONFIG: return "RIL_REQUEST_GSM_GET_BROADCAST_CONFIG";
3594             case RIL_REQUEST_GSM_SET_BROADCAST_CONFIG: return "RIL_REQUEST_GSM_SET_BROADCAST_CONFIG";
3595             case RIL_REQUEST_CDMA_GET_BROADCAST_CONFIG: return "RIL_REQUEST_CDMA_GET_BROADCAST_CONFIG";
3596             case RIL_REQUEST_CDMA_SET_BROADCAST_CONFIG: return "RIL_REQUEST_CDMA_SET_BROADCAST_CONFIG";
3597             case RIL_REQUEST_GSM_BROADCAST_ACTIVATION: return "RIL_REQUEST_GSM_BROADCAST_ACTIVATION";
3598             case RIL_REQUEST_CDMA_VALIDATE_AND_WRITE_AKEY: return "RIL_REQUEST_CDMA_VALIDATE_AND_WRITE_AKEY";
3599             case RIL_REQUEST_CDMA_BROADCAST_ACTIVATION: return "RIL_REQUEST_CDMA_BROADCAST_ACTIVATION";
3600             case RIL_REQUEST_CDMA_SUBSCRIPTION: return "RIL_REQUEST_CDMA_SUBSCRIPTION";
3601             case RIL_REQUEST_CDMA_WRITE_SMS_TO_RUIM: return "RIL_REQUEST_CDMA_WRITE_SMS_TO_RUIM";
3602             case RIL_REQUEST_CDMA_DELETE_SMS_ON_RUIM: return "RIL_REQUEST_CDMA_DELETE_SMS_ON_RUIM";
3603             case RIL_REQUEST_DEVICE_IDENTITY: return "RIL_REQUEST_DEVICE_IDENTITY";
3604             case RIL_REQUEST_GET_SMSC_ADDRESS: return "RIL_REQUEST_GET_SMSC_ADDRESS";
3605             case RIL_REQUEST_SET_SMSC_ADDRESS: return "RIL_REQUEST_SET_SMSC_ADDRESS";
3606             case RIL_REQUEST_EXIT_EMERGENCY_CALLBACK_MODE: return "REQUEST_EXIT_EMERGENCY_CALLBACK_MODE";
3607             case RIL_REQUEST_REPORT_SMS_MEMORY_STATUS: return "RIL_REQUEST_REPORT_SMS_MEMORY_STATUS";
3608             case RIL_REQUEST_REPORT_STK_SERVICE_IS_RUNNING: return "RIL_REQUEST_REPORT_STK_SERVICE_IS_RUNNING";
3609             case RIL_REQUEST_CDMA_GET_SUBSCRIPTION_SOURCE: return "RIL_REQUEST_CDMA_GET_SUBSCRIPTION_SOURCE";
3610             case RIL_REQUEST_ISIM_AUTHENTICATION: return "RIL_REQUEST_ISIM_AUTHENTICATION";
3611             case RIL_REQUEST_ACKNOWLEDGE_INCOMING_GSM_SMS_WITH_PDU: return "RIL_REQUEST_ACKNOWLEDGE_INCOMING_GSM_SMS_WITH_PDU";
3612             case RIL_REQUEST_STK_SEND_ENVELOPE_WITH_STATUS: return "RIL_REQUEST_STK_SEND_ENVELOPE_WITH_STATUS";
3613             case RIL_REQUEST_VOICE_RADIO_TECH: return "RIL_REQUEST_VOICE_RADIO_TECH";
3614             default: return "<unknown request>";
3615         }
3616     }
3617
3618     static String
3619     responseToString(int request)
3620     {
3621 /*
3622  cat libs/telephony/ril_unsol_commands.h \
3623  | egrep "^ *{RIL_" \
3624  | sed -re 's/\{RIL_([^,]+),[^,]+,([^}]+).+/case RIL_\1: return "\1";/'
3625 */
3626         switch(request) {
3627             case RIL_UNSOL_RESPONSE_RADIO_STATE_CHANGED: return "UNSOL_RESPONSE_RADIO_STATE_CHANGED";
3628             case RIL_UNSOL_RESPONSE_CALL_STATE_CHANGED: return "UNSOL_RESPONSE_CALL_STATE_CHANGED";
3629             case RIL_UNSOL_RESPONSE_VOICE_NETWORK_STATE_CHANGED: return "UNSOL_RESPONSE_VOICE_NETWORK_STATE_CHANGED";
3630             case RIL_UNSOL_RESPONSE_NEW_SMS: return "UNSOL_RESPONSE_NEW_SMS";
3631             case RIL_UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT: return "UNSOL_RESPONSE_NEW_SMS_STATUS_REPORT";
3632             case RIL_UNSOL_RESPONSE_NEW_SMS_ON_SIM: return "UNSOL_RESPONSE_NEW_SMS_ON_SIM";
3633             case RIL_UNSOL_ON_USSD: return "UNSOL_ON_USSD";
3634             case RIL_UNSOL_ON_USSD_REQUEST: return "UNSOL_ON_USSD_REQUEST";
3635             case RIL_UNSOL_NITZ_TIME_RECEIVED: return "UNSOL_NITZ_TIME_RECEIVED";
3636             case RIL_UNSOL_SIGNAL_STRENGTH: return "UNSOL_SIGNAL_STRENGTH";
3637             case RIL_UNSOL_DATA_CALL_LIST_CHANGED: return "UNSOL_DATA_CALL_LIST_CHANGED";
3638             case RIL_UNSOL_SUPP_SVC_NOTIFICATION: return "UNSOL_SUPP_SVC_NOTIFICATION";
3639             case RIL_UNSOL_STK_SESSION_END: return "UNSOL_STK_SESSION_END";
3640             case RIL_UNSOL_STK_PROACTIVE_COMMAND: return "UNSOL_STK_PROACTIVE_COMMAND";
3641             case RIL_UNSOL_STK_EVENT_NOTIFY: return "UNSOL_STK_EVENT_NOTIFY";
3642             case RIL_UNSOL_STK_CALL_SETUP: return "UNSOL_STK_CALL_SETUP";
3643             case RIL_UNSOL_SIM_SMS_STORAGE_FULL: return "UNSOL_SIM_SMS_STORAGE_FULL";
3644             case RIL_UNSOL_SIM_REFRESH: return "UNSOL_SIM_REFRESH";
3645             case RIL_UNSOL_CALL_RING: return "UNSOL_CALL_RING";
3646             case RIL_UNSOL_RESPONSE_SIM_STATUS_CHANGED: return "UNSOL_RESPONSE_SIM_STATUS_CHANGED";
3647             case RIL_UNSOL_RESPONSE_CDMA_NEW_SMS: return "UNSOL_RESPONSE_CDMA_NEW_SMS";
3648             case RIL_UNSOL_RESPONSE_NEW_BROADCAST_SMS: return "UNSOL_RESPONSE_NEW_BROADCAST_SMS";
3649             case RIL_UNSOL_CDMA_RUIM_SMS_STORAGE_FULL: return "UNSOL_CDMA_RUIM_SMS_STORAGE_FULL";
3650             case RIL_UNSOL_RESTRICTED_STATE_CHANGED: return "UNSOL_RESTRICTED_STATE_CHANGED";
3651             case RIL_UNSOL_ENTER_EMERGENCY_CALLBACK_MODE: return "UNSOL_ENTER_EMERGENCY_CALLBACK_MODE";
3652             case RIL_UNSOL_CDMA_CALL_WAITING: return "UNSOL_CDMA_CALL_WAITING";
3653             case RIL_UNSOL_CDMA_OTA_PROVISION_STATUS: return "UNSOL_CDMA_OTA_PROVISION_STATUS";
3654             case RIL_UNSOL_CDMA_INFO_REC: return "UNSOL_CDMA_INFO_REC";
3655             case RIL_UNSOL_OEM_HOOK_RAW: return "UNSOL_OEM_HOOK_RAW";
3656             case RIL_UNSOL_RINGBACK_TONE: return "UNSOL_RINGBACK_TONE";
3657             case RIL_UNSOL_RESEND_INCALL_MUTE: return "UNSOL_RESEND_INCALL_MUTE";
3658             case RIL_UNSOL_CDMA_SUBSCRIPTION_SOURCE_CHANGED: return "CDMA_SUBSCRIPTION_SOURCE_CHANGED";
3659             case RIL_UNSOl_CDMA_PRL_CHANGED: return "UNSOL_CDMA_PRL_CHANGED";
3660             case RIL_UNSOL_EXIT_EMERGENCY_CALLBACK_MODE: return "UNSOL_EXIT_EMERGENCY_CALLBACK_MODE";
3661             case RIL_UNSOL_RIL_CONNECTED: return "UNSOL_RIL_CONNECTED";
3662             case RIL_UNSOL_VOICE_RADIO_TECH_CHANGED: return "UNSOL_VOICE_RADIO_TECH_CHANGED";
3663             default: return "<unknown reponse>";
3664         }
3665     }
3666
3667     private void riljLog(String msg) {
3668         Rlog.d(RILJ_LOG_TAG, msg);
3669     }
3670
3671     private void riljLogv(String msg) {
3672         Rlog.v(RILJ_LOG_TAG, msg);
3673     }
3674
3675     private void unsljLog(int response) {
3676         riljLog("[UNSL]< " + responseToString(response));
3677     }
3678
3679     private void unsljLogMore(int response, String more) {
3680         riljLog("[UNSL]< " + responseToString(response) + " " + more);
3681     }
3682
3683     private void unsljLogRet(int response, Object ret) {
3684         riljLog("[UNSL]< " + responseToString(response) + " " + retToString(response, ret));
3685     }
3686
3687     private void unsljLogvRet(int response, Object ret) {
3688         riljLogv("[UNSL]< " + responseToString(response) + " " + retToString(response, ret));
3689     }
3690
3691
3692     // ***** Methods for CDMA support
3693     public void
3694     getDeviceIdentity(Message response) {
3695         RILRequest rr = RILRequest.obtain(RIL_REQUEST_DEVICE_IDENTITY, response);
3696
3697         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
3698
3699         send(rr);
3700     }
3701
3702     public void
3703     getCDMASubscription(Message response) {
3704         RILRequest rr = RILRequest.obtain(RIL_REQUEST_CDMA_SUBSCRIPTION, response);
3705
3706         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
3707
3708         send(rr);
3709     }
3710
3711     @Override
3712     public void setPhoneType(int phoneType) { // Called by CDMAPhone and GSMPhone constructor
3713         if (RILJ_LOGD) riljLog("setPhoneType=" + phoneType + " old value=" + mPhoneType);
3714         mPhoneType = phoneType;
3715     }
3716
3717     /**
3718      * {@inheritDoc}
3719      */
3720     public void queryCdmaRoamingPreference(Message response) {
3721         RILRequest rr = RILRequest.obtain(
3722                 RILConstants.RIL_REQUEST_CDMA_QUERY_ROAMING_PREFERENCE, response);
3723
3724         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
3725
3726         send(rr);
3727     }
3728
3729     /**
3730      * {@inheritDoc}
3731      */
3732     public void setCdmaRoamingPreference(int cdmaRoamingType, Message response) {
3733         RILRequest rr = RILRequest.obtain(
3734                 RILConstants.RIL_REQUEST_CDMA_SET_ROAMING_PREFERENCE, response);
3735
3736         rr.mp.writeInt(1);
3737         rr.mp.writeInt(cdmaRoamingType);
3738
3739         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
3740                 + " : " + cdmaRoamingType);
3741
3742         send(rr);
3743     }
3744
3745     /**
3746      * {@inheritDoc}
3747      */
3748     public void setCdmaSubscriptionSource(int cdmaSubscription , Message response) {
3749         RILRequest rr = RILRequest.obtain(
3750                 RILConstants.RIL_REQUEST_CDMA_SET_SUBSCRIPTION_SOURCE, response);
3751
3752         rr.mp.writeInt(1);
3753         rr.mp.writeInt(cdmaSubscription);
3754
3755         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
3756                 + " : " + cdmaSubscription);
3757
3758         send(rr);
3759     }
3760
3761     /**
3762      * {@inheritDoc}
3763      */
3764     @Override
3765     public void getCdmaSubscriptionSource(Message response) {
3766         RILRequest rr = RILRequest.obtain(
3767                 RILConstants.RIL_REQUEST_CDMA_GET_SUBSCRIPTION_SOURCE, response);
3768
3769         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
3770
3771         send(rr);
3772     }
3773
3774     /**
3775      * {@inheritDoc}
3776      */
3777     public void queryTTYMode(Message response) {
3778         RILRequest rr = RILRequest.obtain(
3779                 RILConstants.RIL_REQUEST_QUERY_TTY_MODE, response);
3780
3781         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
3782
3783         send(rr);
3784     }
3785
3786     /**
3787      * {@inheritDoc}
3788      */
3789     public void setTTYMode(int ttyMode, Message response) {
3790         RILRequest rr = RILRequest.obtain(
3791                 RILConstants.RIL_REQUEST_SET_TTY_MODE, response);
3792
3793         rr.mp.writeInt(1);
3794         rr.mp.writeInt(ttyMode);
3795
3796         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
3797                 + " : " + ttyMode);
3798
3799         send(rr);
3800     }
3801
3802     /**
3803      * {@inheritDoc}
3804      */
3805     public void
3806     sendCDMAFeatureCode(String FeatureCode, Message response) {
3807         RILRequest rr = RILRequest.obtain(RIL_REQUEST_CDMA_FLASH, response);
3808
3809         rr.mp.writeString(FeatureCode);
3810
3811         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
3812                 + " : " + FeatureCode);
3813
3814         send(rr);
3815     }
3816
3817     public void getCdmaBroadcastConfig(Message response) {
3818         RILRequest rr = RILRequest.obtain(RIL_REQUEST_CDMA_GET_BROADCAST_CONFIG, response);
3819
3820         send(rr);
3821     }
3822
3823     public void setCdmaBroadcastConfig(CdmaSmsBroadcastConfigInfo[] configs, Message response) {
3824         RILRequest rr = RILRequest.obtain(RIL_REQUEST_CDMA_SET_BROADCAST_CONFIG, response);
3825
3826         // Convert to 1 service category per config (the way RIL takes is)
3827         ArrayList<CdmaSmsBroadcastConfigInfo> processedConfigs =
3828             new ArrayList<CdmaSmsBroadcastConfigInfo>();
3829         for (CdmaSmsBroadcastConfigInfo config : configs) {
3830             for (int i = config.getFromServiceCategory(); i <= config.getToServiceCategory(); i++) {
3831                 processedConfigs.add(new CdmaSmsBroadcastConfigInfo(i,
3832                         i,
3833                         config.getLanguage(),
3834                         config.isSelected()));
3835             }
3836         }
3837
3838         CdmaSmsBroadcastConfigInfo[] rilConfigs = processedConfigs.toArray(configs);
3839         rr.mp.writeInt(rilConfigs.length);
3840         for(int i = 0; i < rilConfigs.length; i++) {
3841             rr.mp.writeInt(rilConfigs[i].getFromServiceCategory());
3842             rr.mp.writeInt(rilConfigs[i].getLanguage());
3843             rr.mp.writeInt(rilConfigs[i].isSelected() ? 1 : 0);
3844         }
3845
3846         if (RILJ_LOGD) {
3847             riljLog(rr.serialString() + "> " + requestToString(rr.mRequest)
3848                     + " with " + rilConfigs.length + " configs : ");
3849             for (int i = 0; i < rilConfigs.length; i++) {
3850                 riljLog(rilConfigs[i].toString());
3851             }
3852         }
3853
3854         send(rr);
3855     }
3856
3857     public void setCdmaBroadcastActivation(boolean activate, Message response) {
3858         RILRequest rr = RILRequest.obtain(RIL_REQUEST_CDMA_BROADCAST_ACTIVATION, response);
3859
3860         rr.mp.writeInt(1);
3861         rr.mp.writeInt(activate ? 0 :1);
3862
3863         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
3864
3865         send(rr);
3866     }
3867
3868     /**
3869      * {@inheritDoc}
3870      */
3871     public void exitEmergencyCallbackMode(Message response) {
3872         RILRequest rr = RILRequest.obtain(RIL_REQUEST_EXIT_EMERGENCY_CALLBACK_MODE, response);
3873
3874         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
3875
3876         send(rr);
3877     }
3878
3879     public void requestIsimAuthentication(String nonce, Message response) {
3880         RILRequest rr = RILRequest.obtain(RIL_REQUEST_ISIM_AUTHENTICATION, response);
3881
3882         rr.mp.writeString(nonce);
3883
3884         if (RILJ_LOGD) riljLog(rr.serialString() + "> " + requestToString(rr.mRequest));
3885
3886         send(rr);
3887     }
3888
3889     /* (non-Javadoc)
3890      * @see com.android.internal.telephony.BaseCommands#testingEmergencyCall()
3891      */
3892     @Override
3893     public void testingEmergencyCall() {
3894         if (RILJ_LOGD) riljLog("testingEmergencyCall");
3895         mTestingEmergencyCall.set(true);
3896     }
3897
3898     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
3899         pw.println("RIL: " + this);
3900         pw.println(" mSocket=" + mSocket);
3901         pw.println(" mSenderThread=" + mSenderThread);
3902         pw.println(" mSender=" + mSender);
3903         pw.println(" mReceiverThread=" + mReceiverThread);
3904         pw.println(" mReceiver=" + mReceiver);
3905         pw.println(" mWakeLock=" + mWakeLock);
3906         pw.println(" mWakeLockTimeout=" + mWakeLockTimeout);
3907         synchronized (mRequestList) {
3908           pw.println(" mRequestMessagesPending=" + mRequestMessagesPending);
3909           pw.println(" mRequestMessagesWaiting=" + mRequestMessagesWaiting);
3910             int count = mRequestList.size();
3911             pw.println(" mRequestList count=" + count);
3912             for (int i = 0; i < count; i++) {
3913                 RILRequest rr = mRequestList.get(i);
3914                 pw.println("  [" + rr.mSerial + "] " + requestToString(rr.mRequest));
3915             }
3916         }
3917         pw.println(" mLastNITZTimeInfo=" + mLastNITZTimeInfo);
3918         pw.println(" mTestingEmergencyCall=" + mTestingEmergencyCall.get());
3919     }
3920 }