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