OSDN Git Service

Handle empty apn string in setInitialAttachApn HIDL API.
[android-x86/hardware-ril.git] / libril / ril_service.cpp
1 /*
2  * Copyright (c) 2016 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 #define LOG_TAG "RILC"
18
19 #include <android/hardware/radio/1.0/IRadio.h>
20 #include <android/hardware/radio/deprecated/1.0/IOemHook.h>
21
22 #include <hwbinder/IPCThreadState.h>
23 #include <hwbinder/ProcessState.h>
24 #include <ril_service.h>
25 #include <hidl/HidlTransportSupport.h>
26 #include <utils/SystemClock.h>
27 #include <inttypes.h>
28
29 #define INVALID_HEX_CHAR 16
30
31 // Enable verbose logging
32 #define VDBG 0
33
34 using namespace android::hardware::radio::V1_0;
35 using namespace android::hardware::radio::deprecated::V1_0;
36 using ::android::hardware::configureRpcThreadpool;
37 using ::android::hardware::joinRpcThreadpool;
38 using ::android::hardware::Return;
39 using ::android::hardware::hidl_string;
40 using ::android::hardware::hidl_vec;
41 using ::android::hardware::hidl_array;
42 using ::android::hardware::Void;
43 using android::CommandInfo;
44 using android::RequestInfo;
45 using android::requestToString;
46 using android::sp;
47
48 #define BOOL_TO_INT(x) (x ? 1 : 0)
49 #define ATOI_NULL_HANDLED(x) (x ? atoi(x) : -1)
50 #define ATOI_NULL_HANDLED_DEF(x, defaultVal) (x ? atoi(x) : defaultVal)
51
52 #if defined(ANDROID_MULTI_SIM)
53 #define CALL_ONREQUEST(a, b, c, d, e) \
54         s_vendorFunctions->onRequest((a), (b), (c), (d), ((RIL_SOCKET_ID)(e)))
55 #define CALL_ONSTATEREQUEST(a) s_vendorFunctions->onStateRequest((RIL_SOCKET_ID)(a))
56 #else
57 #define CALL_ONREQUEST(a, b, c, d, e) s_vendorFunctions->onRequest((a), (b), (c), (d))
58 #define CALL_ONSTATEREQUEST(a) s_vendorFunctions->onStateRequest()
59 #endif
60
61 RIL_RadioFunctions *s_vendorFunctions = NULL;
62 static CommandInfo *s_commands;
63
64 struct RadioImpl;
65 struct OemHookImpl;
66
67 #if (SIM_COUNT >= 2)
68 sp<RadioImpl> radioService[SIM_COUNT];
69 sp<OemHookImpl> oemHookService[SIM_COUNT];
70 // counter used for synchronization. It is incremented every time response callbacks are updated.
71 volatile int32_t mCounterRadio[SIM_COUNT];
72 volatile int32_t mCounterOemHook[SIM_COUNT];
73 #else
74 sp<RadioImpl> radioService[1];
75 sp<OemHookImpl> oemHookService[1];
76 // counter used for synchronization. It is incremented every time response callbacks are updated.
77 volatile int32_t mCounterRadio[1];
78 volatile int32_t mCounterOemHook[1];
79 #endif
80
81 static pthread_rwlock_t radioServiceRwlock = PTHREAD_RWLOCK_INITIALIZER;
82
83 #if (SIM_COUNT >= 2)
84 static pthread_rwlock_t radioServiceRwlock2 = PTHREAD_RWLOCK_INITIALIZER;
85 #if (SIM_COUNT >= 3)
86 static pthread_rwlock_t radioServiceRwlock3 = PTHREAD_RWLOCK_INITIALIZER;
87 #if (SIM_COUNT >= 4)
88 static pthread_rwlock_t radioServiceRwlock4 = PTHREAD_RWLOCK_INITIALIZER;
89 #endif
90 #endif
91 #endif
92
93 void convertRilHardwareConfigListToHal(void *response, size_t responseLen,
94         hidl_vec<HardwareConfig>& records);
95
96 void convertRilRadioCapabilityToHal(void *response, size_t responseLen, RadioCapability& rc);
97
98 void convertRilLceDataInfoToHal(void *response, size_t responseLen, LceDataInfo& lce);
99
100 void convertRilSignalStrengthToHal(void *response, size_t responseLen,
101         SignalStrength& signalStrength);
102
103 void convertRilDataCallToHal(RIL_Data_Call_Response_v11 *dcResponse,
104         SetupDataCallResult& dcResult);
105
106 void convertRilDataCallListToHal(void *response, size_t responseLen,
107         hidl_vec<SetupDataCallResult>& dcResultList);
108
109 void convertRilCellInfoListToHal(void *response, size_t responseLen, hidl_vec<CellInfo>& records);
110
111 struct RadioImpl : public IRadio {
112     int32_t mSlotId;
113     sp<IRadioResponse> mRadioResponse;
114     sp<IRadioIndication> mRadioIndication;
115
116     Return<void> setResponseFunctions(
117             const ::android::sp<IRadioResponse>& radioResponse,
118             const ::android::sp<IRadioIndication>& radioIndication);
119
120     Return<void> getIccCardStatus(int32_t serial);
121
122     Return<void> supplyIccPinForApp(int32_t serial, const hidl_string& pin,
123             const hidl_string& aid);
124
125     Return<void> supplyIccPukForApp(int32_t serial, const hidl_string& puk,
126             const hidl_string& pin, const hidl_string& aid);
127
128     Return<void> supplyIccPin2ForApp(int32_t serial,
129             const hidl_string& pin2,
130             const hidl_string& aid);
131
132     Return<void> supplyIccPuk2ForApp(int32_t serial, const hidl_string& puk2,
133             const hidl_string& pin2, const hidl_string& aid);
134
135     Return<void> changeIccPinForApp(int32_t serial, const hidl_string& oldPin,
136             const hidl_string& newPin, const hidl_string& aid);
137
138     Return<void> changeIccPin2ForApp(int32_t serial, const hidl_string& oldPin2,
139             const hidl_string& newPin2, const hidl_string& aid);
140
141     Return<void> supplyNetworkDepersonalization(int32_t serial, const hidl_string& netPin);
142
143     Return<void> getCurrentCalls(int32_t serial);
144
145     Return<void> dial(int32_t serial, const Dial& dialInfo);
146
147     Return<void> getImsiForApp(int32_t serial,
148             const ::android::hardware::hidl_string& aid);
149
150     Return<void> hangup(int32_t serial, int32_t gsmIndex);
151
152     Return<void> hangupWaitingOrBackground(int32_t serial);
153
154     Return<void> hangupForegroundResumeBackground(int32_t serial);
155
156     Return<void> switchWaitingOrHoldingAndActive(int32_t serial);
157
158     Return<void> conference(int32_t serial);
159
160     Return<void> rejectCall(int32_t serial);
161
162     Return<void> getLastCallFailCause(int32_t serial);
163
164     Return<void> getSignalStrength(int32_t serial);
165
166     Return<void> getVoiceRegistrationState(int32_t serial);
167
168     Return<void> getDataRegistrationState(int32_t serial);
169
170     Return<void> getOperator(int32_t serial);
171
172     Return<void> setRadioPower(int32_t serial, bool on);
173
174     Return<void> sendDtmf(int32_t serial,
175             const ::android::hardware::hidl_string& s);
176
177     Return<void> sendSms(int32_t serial, const GsmSmsMessage& message);
178
179     Return<void> sendSMSExpectMore(int32_t serial, const GsmSmsMessage& message);
180
181     Return<void> setupDataCall(int32_t serial,
182             RadioTechnology radioTechnology,
183             const DataProfileInfo& profileInfo,
184             bool modemCognitive,
185             bool roamingAllowed,
186             bool isRoaming);
187
188     Return<void> iccIOForApp(int32_t serial,
189             const IccIo& iccIo);
190
191     Return<void> sendUssd(int32_t serial,
192             const ::android::hardware::hidl_string& ussd);
193
194     Return<void> cancelPendingUssd(int32_t serial);
195
196     Return<void> getClir(int32_t serial);
197
198     Return<void> setClir(int32_t serial, int32_t status);
199
200     Return<void> getCallForwardStatus(int32_t serial,
201             const CallForwardInfo& callInfo);
202
203     Return<void> setCallForward(int32_t serial,
204             const CallForwardInfo& callInfo);
205
206     Return<void> getCallWaiting(int32_t serial, int32_t serviceClass);
207
208     Return<void> setCallWaiting(int32_t serial, bool enable, int32_t serviceClass);
209
210     Return<void> acknowledgeLastIncomingGsmSms(int32_t serial,
211             bool success, SmsAcknowledgeFailCause cause);
212
213     Return<void> acceptCall(int32_t serial);
214
215     Return<void> deactivateDataCall(int32_t serial,
216             int32_t cid, bool reasonRadioShutDown);
217
218     Return<void> getFacilityLockForApp(int32_t serial,
219             const ::android::hardware::hidl_string& facility,
220             const ::android::hardware::hidl_string& password,
221             int32_t serviceClass,
222             const ::android::hardware::hidl_string& appId);
223
224     Return<void> setFacilityLockForApp(int32_t serial,
225             const ::android::hardware::hidl_string& facility,
226             bool lockState,
227             const ::android::hardware::hidl_string& password,
228             int32_t serviceClass,
229             const ::android::hardware::hidl_string& appId);
230
231     Return<void> setBarringPassword(int32_t serial,
232             const ::android::hardware::hidl_string& facility,
233             const ::android::hardware::hidl_string& oldPassword,
234             const ::android::hardware::hidl_string& newPassword);
235
236     Return<void> getNetworkSelectionMode(int32_t serial);
237
238     Return<void> setNetworkSelectionModeAutomatic(int32_t serial);
239
240     Return<void> setNetworkSelectionModeManual(int32_t serial,
241             const ::android::hardware::hidl_string& operatorNumeric);
242
243     Return<void> getAvailableNetworks(int32_t serial);
244
245     Return<void> startDtmf(int32_t serial,
246             const ::android::hardware::hidl_string& s);
247
248     Return<void> stopDtmf(int32_t serial);
249
250     Return<void> getBasebandVersion(int32_t serial);
251
252     Return<void> separateConnection(int32_t serial, int32_t gsmIndex);
253
254     Return<void> setMute(int32_t serial, bool enable);
255
256     Return<void> getMute(int32_t serial);
257
258     Return<void> getClip(int32_t serial);
259
260     Return<void> getDataCallList(int32_t serial);
261
262     Return<void> setSuppServiceNotifications(int32_t serial, bool enable);
263
264     Return<void> writeSmsToSim(int32_t serial,
265             const SmsWriteArgs& smsWriteArgs);
266
267     Return<void> deleteSmsOnSim(int32_t serial, int32_t index);
268
269     Return<void> setBandMode(int32_t serial, RadioBandMode mode);
270
271     Return<void> getAvailableBandModes(int32_t serial);
272
273     Return<void> sendEnvelope(int32_t serial,
274             const ::android::hardware::hidl_string& command);
275
276     Return<void> sendTerminalResponseToSim(int32_t serial,
277             const ::android::hardware::hidl_string& commandResponse);
278
279     Return<void> handleStkCallSetupRequestFromSim(int32_t serial, bool accept);
280
281     Return<void> explicitCallTransfer(int32_t serial);
282
283     Return<void> setPreferredNetworkType(int32_t serial, PreferredNetworkType nwType);
284
285     Return<void> getPreferredNetworkType(int32_t serial);
286
287     Return<void> getNeighboringCids(int32_t serial);
288
289     Return<void> setLocationUpdates(int32_t serial, bool enable);
290
291     Return<void> setCdmaSubscriptionSource(int32_t serial,
292             CdmaSubscriptionSource cdmaSub);
293
294     Return<void> setCdmaRoamingPreference(int32_t serial, CdmaRoamingType type);
295
296     Return<void> getCdmaRoamingPreference(int32_t serial);
297
298     Return<void> setTTYMode(int32_t serial, TtyMode mode);
299
300     Return<void> getTTYMode(int32_t serial);
301
302     Return<void> setPreferredVoicePrivacy(int32_t serial, bool enable);
303
304     Return<void> getPreferredVoicePrivacy(int32_t serial);
305
306     Return<void> sendCDMAFeatureCode(int32_t serial,
307             const ::android::hardware::hidl_string& featureCode);
308
309     Return<void> sendBurstDtmf(int32_t serial,
310             const ::android::hardware::hidl_string& dtmf,
311             int32_t on,
312             int32_t off);
313
314     Return<void> sendCdmaSms(int32_t serial, const CdmaSmsMessage& sms);
315
316     Return<void> acknowledgeLastIncomingCdmaSms(int32_t serial,
317             const CdmaSmsAck& smsAck);
318
319     Return<void> getGsmBroadcastConfig(int32_t serial);
320
321     Return<void> setGsmBroadcastConfig(int32_t serial,
322             const hidl_vec<GsmBroadcastSmsConfigInfo>& configInfo);
323
324     Return<void> setGsmBroadcastActivation(int32_t serial, bool activate);
325
326     Return<void> getCdmaBroadcastConfig(int32_t serial);
327
328     Return<void> setCdmaBroadcastConfig(int32_t serial,
329             const hidl_vec<CdmaBroadcastSmsConfigInfo>& configInfo);
330
331     Return<void> setCdmaBroadcastActivation(int32_t serial, bool activate);
332
333     Return<void> getCDMASubscription(int32_t serial);
334
335     Return<void> writeSmsToRuim(int32_t serial, const CdmaSmsWriteArgs& cdmaSms);
336
337     Return<void> deleteSmsOnRuim(int32_t serial, int32_t index);
338
339     Return<void> getDeviceIdentity(int32_t serial);
340
341     Return<void> exitEmergencyCallbackMode(int32_t serial);
342
343     Return<void> getSmscAddress(int32_t serial);
344
345     Return<void> setSmscAddress(int32_t serial,
346             const ::android::hardware::hidl_string& smsc);
347
348     Return<void> reportSmsMemoryStatus(int32_t serial, bool available);
349
350     Return<void> reportStkServiceIsRunning(int32_t serial);
351
352     Return<void> getCdmaSubscriptionSource(int32_t serial);
353
354     Return<void> requestIsimAuthentication(int32_t serial,
355             const ::android::hardware::hidl_string& challenge);
356
357     Return<void> acknowledgeIncomingGsmSmsWithPdu(int32_t serial,
358             bool success,
359             const ::android::hardware::hidl_string& ackPdu);
360
361     Return<void> sendEnvelopeWithStatus(int32_t serial,
362             const ::android::hardware::hidl_string& contents);
363
364     Return<void> getVoiceRadioTechnology(int32_t serial);
365
366     Return<void> getCellInfoList(int32_t serial);
367
368     Return<void> setCellInfoListRate(int32_t serial, int32_t rate);
369
370     Return<void> setInitialAttachApn(int32_t serial, const DataProfileInfo& dataProfileInfo,
371             bool modemCognitive, bool isRoaming);
372
373     Return<void> getImsRegistrationState(int32_t serial);
374
375     Return<void> sendImsSms(int32_t serial, const ImsSmsMessage& message);
376
377     Return<void> iccTransmitApduBasicChannel(int32_t serial, const SimApdu& message);
378
379     Return<void> iccOpenLogicalChannel(int32_t serial,
380             const ::android::hardware::hidl_string& aid, int32_t p2);
381
382     Return<void> iccCloseLogicalChannel(int32_t serial, int32_t channelId);
383
384     Return<void> iccTransmitApduLogicalChannel(int32_t serial, const SimApdu& message);
385
386     Return<void> nvReadItem(int32_t serial, NvItem itemId);
387
388     Return<void> nvWriteItem(int32_t serial, const NvWriteItem& item);
389
390     Return<void> nvWriteCdmaPrl(int32_t serial,
391             const ::android::hardware::hidl_vec<uint8_t>& prl);
392
393     Return<void> nvResetConfig(int32_t serial, ResetNvType resetType);
394
395     Return<void> setUiccSubscription(int32_t serial, const SelectUiccSub& uiccSub);
396
397     Return<void> setDataAllowed(int32_t serial, bool allow);
398
399     Return<void> getHardwareConfig(int32_t serial);
400
401     Return<void> requestIccSimAuthentication(int32_t serial,
402             int32_t authContext,
403             const ::android::hardware::hidl_string& authData,
404             const ::android::hardware::hidl_string& aid);
405
406     Return<void> setDataProfile(int32_t serial,
407             const ::android::hardware::hidl_vec<DataProfileInfo>& profiles, bool isRoaming);
408
409     Return<void> requestShutdown(int32_t serial);
410
411     Return<void> getRadioCapability(int32_t serial);
412
413     Return<void> setRadioCapability(int32_t serial, const RadioCapability& rc);
414
415     Return<void> startLceService(int32_t serial, int32_t reportInterval, bool pullMode);
416
417     Return<void> stopLceService(int32_t serial);
418
419     Return<void> pullLceData(int32_t serial);
420
421     Return<void> getModemActivityInfo(int32_t serial);
422
423     Return<void> setAllowedCarriers(int32_t serial,
424             bool allAllowed,
425             const CarrierRestrictions& carriers);
426
427     Return<void> getAllowedCarriers(int32_t serial);
428
429     Return<void> sendDeviceState(int32_t serial, DeviceStateType deviceStateType, bool state);
430
431     Return<void> setIndicationFilter(int32_t serial, int32_t indicationFilter);
432
433     Return<void> setSimCardPower(int32_t serial, bool powerUp);
434
435     Return<void> responseAcknowledgement();
436
437     void checkReturnStatus(Return<void>& ret);
438 };
439
440 struct OemHookImpl : public IOemHook {
441     int32_t mSlotId;
442     sp<IOemHookResponse> mOemHookResponse;
443     sp<IOemHookIndication> mOemHookIndication;
444
445     Return<void> setResponseFunctions(
446             const ::android::sp<IOemHookResponse>& oemHookResponse,
447             const ::android::sp<IOemHookIndication>& oemHookIndication);
448
449     Return<void> sendRequestRaw(int32_t serial,
450             const ::android::hardware::hidl_vec<uint8_t>& data);
451
452     Return<void> sendRequestStrings(int32_t serial,
453             const ::android::hardware::hidl_vec<::android::hardware::hidl_string>& data);
454 };
455
456 void memsetAndFreeStrings(int numPointers, ...) {
457     va_list ap;
458     va_start(ap, numPointers);
459     for (int i = 0; i < numPointers; i++) {
460         char *ptr = va_arg(ap, char *);
461         if (ptr) {
462 #ifdef MEMSET_FREED
463             // TODO: Should pass in the maximum length of the string
464             memsetString(ptr);
465 #endif
466             free(ptr);
467         }
468     }
469     va_end(ap);
470 }
471
472 void sendErrorResponse(RequestInfo *pRI, RIL_Errno err) {
473     pRI->pCI->responseFunction((int) pRI->socket_id,
474             (int) RadioResponseType::SOLICITED, pRI->token, err, NULL, 0);
475 }
476
477 /**
478  * Copies over src to dest. If memory allocation fails, responseFunction() is called for the
479  * request with error RIL_E_NO_MEMORY.
480  * Returns true on success, and false on failure.
481  */
482 bool copyHidlStringToRil(char **dest, const hidl_string &src, RequestInfo *pRI) {
483     size_t len = src.size();
484     if (len == 0) {
485         *dest = NULL;
486         return true;
487     }
488     *dest = (char *) calloc(len + 1, sizeof(char));
489     if (*dest == NULL) {
490         RLOGE("Memory allocation failed for request %s", requestToString(pRI->pCI->requestNumber));
491         sendErrorResponse(pRI, RIL_E_NO_MEMORY);
492         return false;
493     }
494     strncpy(*dest, src.c_str(), len + 1);
495     return true;
496 }
497
498 hidl_string convertCharPtrToHidlString(const char *ptr) {
499     hidl_string ret;
500     if (ptr != NULL) {
501         // TODO: replace this with strnlen
502         ret.setToExternal(ptr, strlen(ptr));
503     }
504     return ret;
505 }
506
507 bool dispatchVoid(int serial, int slotId, int request) {
508     RequestInfo *pRI = android::addRequestToList(serial, slotId, request);
509     if (pRI == NULL) {
510         return false;
511     }
512     CALL_ONREQUEST(request, NULL, 0, pRI, slotId);
513     return true;
514 }
515
516 bool dispatchString(int serial, int slotId, int request, const char * str) {
517     RequestInfo *pRI = android::addRequestToList(serial, slotId, request);
518     if (pRI == NULL) {
519         return false;
520     }
521
522     char *pString;
523     if (!copyHidlStringToRil(&pString, str, pRI)) {
524         return false;
525     }
526
527     CALL_ONREQUEST(request, pString, sizeof(char *), pRI, slotId);
528
529     memsetAndFreeStrings(1, pString);
530     return true;
531 }
532
533 bool dispatchStrings(int serial, int slotId, int request, int countStrings, ...) {
534     RequestInfo *pRI = android::addRequestToList(serial, slotId, request);
535     if (pRI == NULL) {
536         return false;
537     }
538
539     char **pStrings;
540     pStrings = (char **)calloc(countStrings, sizeof(char *));
541     if (pStrings == NULL) {
542         RLOGE("Memory allocation failed for request %s", requestToString(request));
543         sendErrorResponse(pRI, RIL_E_NO_MEMORY);
544         return false;
545     }
546     va_list ap;
547     va_start(ap, countStrings);
548     for (int i = 0; i < countStrings; i++) {
549         const char* str = va_arg(ap, const char *);
550         if (!copyHidlStringToRil(&pStrings[i], hidl_string(str), pRI)) {
551             va_end(ap);
552             for (int j = 0; j < i; j++) {
553                 memsetAndFreeStrings(1, pStrings[j]);
554             }
555             free(pStrings);
556             return false;
557         }
558     }
559     va_end(ap);
560
561     CALL_ONREQUEST(request, pStrings, countStrings * sizeof(char *), pRI, slotId);
562
563     if (pStrings != NULL) {
564         for (int i = 0 ; i < countStrings ; i++) {
565             memsetAndFreeStrings(1, pStrings[i]);
566         }
567
568 #ifdef MEMSET_FREED
569         memset(pStrings, 0, countStrings * sizeof(char *));
570 #endif
571         free(pStrings);
572     }
573     return true;
574 }
575
576 bool dispatchStrings(int serial, int slotId, int request, const hidl_vec<hidl_string>& data) {
577     RequestInfo *pRI = android::addRequestToList(serial, slotId, request);
578     if (pRI == NULL) {
579         return false;
580     }
581
582     int countStrings = data.size();
583     char **pStrings;
584     pStrings = (char **)calloc(countStrings, sizeof(char *));
585     if (pStrings == NULL) {
586         RLOGE("Memory allocation failed for request %s", requestToString(request));
587         sendErrorResponse(pRI, RIL_E_NO_MEMORY);
588         return false;
589     }
590
591     for (int i = 0; i < countStrings; i++) {
592         if (!copyHidlStringToRil(&pStrings[i], data[i], pRI)) {
593             for (int j = 0; j < i; j++) {
594                 memsetAndFreeStrings(1, pStrings[j]);
595             }
596             free(pStrings);
597             return false;
598         }
599     }
600
601     CALL_ONREQUEST(request, pStrings, countStrings * sizeof(char *), pRI, slotId);
602
603     if (pStrings != NULL) {
604         for (int i = 0 ; i < countStrings ; i++) {
605             memsetAndFreeStrings(1, pStrings[i]);
606         }
607
608 #ifdef MEMSET_FREED
609         memset(pStrings, 0, countStrings * sizeof(char *));
610 #endif
611         free(pStrings);
612     }
613     return true;
614 }
615
616 bool dispatchInts(int serial, int slotId, int request, int countInts, ...) {
617     RequestInfo *pRI = android::addRequestToList(serial, slotId, request);
618     if (pRI == NULL) {
619         return false;
620     }
621
622     int *pInts = (int *)calloc(countInts, sizeof(int));
623
624     if (pInts == NULL) {
625         RLOGE("Memory allocation failed for request %s", requestToString(request));
626         sendErrorResponse(pRI, RIL_E_NO_MEMORY);
627         return false;
628     }
629     va_list ap;
630     va_start(ap, countInts);
631     for (int i = 0; i < countInts; i++) {
632         pInts[i] = va_arg(ap, int);
633     }
634     va_end(ap);
635
636     CALL_ONREQUEST(request, pInts, countInts * sizeof(int), pRI, slotId);
637
638     if (pInts != NULL) {
639 #ifdef MEMSET_FREED
640         memset(pInts, 0, countInts * sizeof(int));
641 #endif
642         free(pInts);
643     }
644     return true;
645 }
646
647 bool dispatchCallForwardStatus(int serial, int slotId, int request,
648                               const CallForwardInfo& callInfo) {
649     RequestInfo *pRI = android::addRequestToList(serial, slotId, request);
650     if (pRI == NULL) {
651         return false;
652     }
653
654     RIL_CallForwardInfo cf;
655     cf.status = (int) callInfo.status;
656     cf.reason = callInfo.reason;
657     cf.serviceClass = callInfo.serviceClass;
658     cf.toa = callInfo.toa;
659     cf.timeSeconds = callInfo.timeSeconds;
660
661     if (!copyHidlStringToRil(&cf.number, callInfo.number, pRI)) {
662         return false;
663     }
664
665     CALL_ONREQUEST(request, &cf, sizeof(cf), pRI, slotId);
666
667     memsetAndFreeStrings(1, cf.number);
668
669     return true;
670 }
671
672 bool dispatchRaw(int serial, int slotId, int request, const hidl_vec<uint8_t>& rawBytes) {
673     RequestInfo *pRI = android::addRequestToList(serial, slotId, request);
674     if (pRI == NULL) {
675         return false;
676     }
677
678     const uint8_t *uData = rawBytes.data();
679
680     CALL_ONREQUEST(request, (void *) uData, rawBytes.size(), pRI, slotId);
681
682     return true;
683 }
684
685 bool dispatchIccApdu(int serial, int slotId, int request, const SimApdu& message) {
686     RequestInfo *pRI = android::addRequestToList(serial, slotId, request);
687     if (pRI == NULL) {
688         return false;
689     }
690
691     RIL_SIM_APDU apdu = {};
692
693     apdu.sessionid = message.sessionId;
694     apdu.cla = message.cla;
695     apdu.instruction = message.instruction;
696     apdu.p1 = message.p1;
697     apdu.p2 = message.p2;
698     apdu.p3 = message.p3;
699
700     if (!copyHidlStringToRil(&apdu.data, message.data, pRI)) {
701         return false;
702     }
703
704     CALL_ONREQUEST(request, &apdu, sizeof(apdu), pRI, slotId);
705
706     memsetAndFreeStrings(1, apdu.data);
707
708     return true;
709 }
710
711 void checkReturnStatus(int32_t slotId, Return<void>& ret, bool isRadioService) {
712     if (ret.isOk() == false) {
713         RLOGE("checkReturnStatus: unable to call response/indication callback");
714         // Remote process hosting the callbacks must be dead. Reset the callback objects;
715         // there's no other recovery to be done here. When the client process is back up, it will
716         // call setResponseFunctions()
717
718         // Caller should already hold rdlock, release that first
719         // note the current counter to avoid overwriting updates made by another thread before
720         // write lock is acquired.
721         int counter = isRadioService ? mCounterRadio[slotId] : mCounterOemHook[slotId];
722         pthread_rwlock_t *radioServiceRwlockPtr = radio::getRadioServiceRwlock(slotId);
723         int ret = pthread_rwlock_unlock(radioServiceRwlockPtr);
724         assert(ret == 0);
725
726         // acquire wrlock
727         ret = pthread_rwlock_wrlock(radioServiceRwlockPtr);
728         assert(ret == 0);
729
730         // make sure the counter value has not changed
731         if (counter == (isRadioService ? mCounterRadio[slotId] : mCounterOemHook[slotId])) {
732             if (isRadioService) {
733                 radioService[slotId]->mRadioResponse = NULL;
734                 radioService[slotId]->mRadioIndication = NULL;
735             } else {
736                 oemHookService[slotId]->mOemHookResponse = NULL;
737                 oemHookService[slotId]->mOemHookIndication = NULL;
738             }
739             isRadioService ? mCounterRadio[slotId]++ : mCounterOemHook[slotId]++;
740         } else {
741             RLOGE("checkReturnStatus: not resetting responseFunctions as they likely "
742                     "got updated on another thread");
743         }
744
745         // release wrlock
746         ret = pthread_rwlock_unlock(radioServiceRwlockPtr);
747         assert(ret == 0);
748
749         // Reacquire rdlock
750         ret = pthread_rwlock_rdlock(radioServiceRwlockPtr);
751         assert(ret == 0);
752     }
753 }
754
755 void RadioImpl::checkReturnStatus(Return<void>& ret) {
756     ::checkReturnStatus(mSlotId, ret, true);
757 }
758
759 Return<void> RadioImpl::setResponseFunctions(
760         const ::android::sp<IRadioResponse>& radioResponseParam,
761         const ::android::sp<IRadioIndication>& radioIndicationParam) {
762     RLOGD("setResponseFunctions");
763
764     pthread_rwlock_t *radioServiceRwlockPtr = radio::getRadioServiceRwlock(mSlotId);
765     int ret = pthread_rwlock_wrlock(radioServiceRwlockPtr);
766     assert(ret == 0);
767
768     mRadioResponse = radioResponseParam;
769     mRadioIndication = radioIndicationParam;
770     mCounterRadio[mSlotId]++;
771
772     ret = pthread_rwlock_unlock(radioServiceRwlockPtr);
773     assert(ret == 0);
774
775     // client is connected. Send initial indications.
776     android::onNewCommandConnect((RIL_SOCKET_ID) mSlotId);
777
778     return Void();
779 }
780
781 Return<void> RadioImpl::getIccCardStatus(int32_t serial) {
782 #if VDBG
783     RLOGD("getIccCardStatus: serial %d", serial);
784 #endif
785     dispatchVoid(serial, mSlotId, RIL_REQUEST_GET_SIM_STATUS);
786     return Void();
787 }
788
789 Return<void> RadioImpl::supplyIccPinForApp(int32_t serial, const hidl_string& pin,
790         const hidl_string& aid) {
791 #if VDBG
792     RLOGD("supplyIccPinForApp: serial %d", serial);
793 #endif
794     dispatchStrings(serial, mSlotId, RIL_REQUEST_ENTER_SIM_PIN,
795             2, pin.c_str(), aid.c_str());
796     return Void();
797 }
798
799 Return<void> RadioImpl::supplyIccPukForApp(int32_t serial, const hidl_string& puk,
800                                            const hidl_string& pin, const hidl_string& aid) {
801 #if VDBG
802     RLOGD("supplyIccPukForApp: serial %d", serial);
803 #endif
804     dispatchStrings(serial, mSlotId, RIL_REQUEST_ENTER_SIM_PUK,
805             3, puk.c_str(), pin.c_str(), aid.c_str());
806     return Void();
807 }
808
809 Return<void> RadioImpl::supplyIccPin2ForApp(int32_t serial, const hidl_string& pin2,
810                                             const hidl_string& aid) {
811 #if VDBG
812     RLOGD("supplyIccPin2ForApp: serial %d", serial);
813 #endif
814     dispatchStrings(serial, mSlotId, RIL_REQUEST_ENTER_SIM_PIN2,
815             2, pin2.c_str(), aid.c_str());
816     return Void();
817 }
818
819 Return<void> RadioImpl::supplyIccPuk2ForApp(int32_t serial, const hidl_string& puk2,
820                                             const hidl_string& pin2, const hidl_string& aid) {
821 #if VDBG
822     RLOGD("supplyIccPuk2ForApp: serial %d", serial);
823 #endif
824     dispatchStrings(serial, mSlotId, RIL_REQUEST_ENTER_SIM_PUK2,
825             3, puk2.c_str(), pin2.c_str(), aid.c_str());
826     return Void();
827 }
828
829 Return<void> RadioImpl::changeIccPinForApp(int32_t serial, const hidl_string& oldPin,
830                                            const hidl_string& newPin, const hidl_string& aid) {
831 #if VDBG
832     RLOGD("changeIccPinForApp: serial %d", serial);
833 #endif
834     dispatchStrings(serial, mSlotId, RIL_REQUEST_CHANGE_SIM_PIN,
835             3, oldPin.c_str(), newPin.c_str(), aid.c_str());
836     return Void();
837 }
838
839 Return<void> RadioImpl::changeIccPin2ForApp(int32_t serial, const hidl_string& oldPin2,
840                                             const hidl_string& newPin2, const hidl_string& aid) {
841 #if VDBG
842     RLOGD("changeIccPin2ForApp: serial %d", serial);
843 #endif
844     dispatchStrings(serial, mSlotId, RIL_REQUEST_CHANGE_SIM_PIN2,
845             3, oldPin2.c_str(), newPin2.c_str(), aid.c_str());
846     return Void();
847 }
848
849 Return<void> RadioImpl::supplyNetworkDepersonalization(int32_t serial,
850                                                        const hidl_string& netPin) {
851 #if VDBG
852     RLOGD("supplyNetworkDepersonalization: serial %d", serial);
853 #endif
854     dispatchStrings(serial, mSlotId, RIL_REQUEST_ENTER_NETWORK_DEPERSONALIZATION,
855             1, netPin.c_str());
856     return Void();
857 }
858
859 Return<void> RadioImpl::getCurrentCalls(int32_t serial) {
860 #if VDBG
861     RLOGD("getCurrentCalls: serial %d", serial);
862 #endif
863     dispatchVoid(serial, mSlotId, RIL_REQUEST_GET_CURRENT_CALLS);
864     return Void();
865 }
866
867 Return<void> RadioImpl::dial(int32_t serial, const Dial& dialInfo) {
868 #if VDBG
869     RLOGD("dial: serial %d", serial);
870 #endif
871     RequestInfo *pRI = android::addRequestToList(serial, mSlotId, RIL_REQUEST_DIAL);
872     if (pRI == NULL) {
873         return Void();
874     }
875     RIL_Dial dial = {};
876     RIL_UUS_Info uusInfo = {};
877     int32_t sizeOfDial = sizeof(dial);
878
879     if (!copyHidlStringToRil(&dial.address, dialInfo.address, pRI)) {
880         return Void();
881     }
882     dial.clir = (int) dialInfo.clir;
883
884     if (dialInfo.uusInfo.size() != 0) {
885         uusInfo.uusType = (RIL_UUS_Type) dialInfo.uusInfo[0].uusType;
886         uusInfo.uusDcs = (RIL_UUS_DCS) dialInfo.uusInfo[0].uusDcs;
887
888         if (dialInfo.uusInfo[0].uusData.size() == 0) {
889             uusInfo.uusData = NULL;
890             uusInfo.uusLength = 0;
891         } else {
892             if (!copyHidlStringToRil(&uusInfo.uusData, dialInfo.uusInfo[0].uusData, pRI)) {
893                 memsetAndFreeStrings(1, dial.address);
894                 return Void();
895             }
896             uusInfo.uusLength = dialInfo.uusInfo[0].uusData.size();
897         }
898
899         dial.uusInfo = &uusInfo;
900     }
901
902     CALL_ONREQUEST(RIL_REQUEST_DIAL, &dial, sizeOfDial, pRI, mSlotId);
903
904     memsetAndFreeStrings(2, dial.address, uusInfo.uusData);
905
906     return Void();
907 }
908
909 Return<void> RadioImpl::getImsiForApp(int32_t serial, const hidl_string& aid) {
910 #if VDBG
911     RLOGD("getImsiForApp: serial %d", serial);
912 #endif
913     dispatchStrings(serial, mSlotId, RIL_REQUEST_GET_IMSI,
914             1, aid.c_str());
915     return Void();
916 }
917
918 Return<void> RadioImpl::hangup(int32_t serial, int32_t gsmIndex) {
919 #if VDBG
920     RLOGD("hangup: serial %d", serial);
921 #endif
922     dispatchInts(serial, mSlotId, RIL_REQUEST_HANGUP, 1, gsmIndex);
923     return Void();
924 }
925
926 Return<void> RadioImpl::hangupWaitingOrBackground(int32_t serial) {
927 #if VDBG
928     RLOGD("hangupWaitingOrBackground: serial %d", serial);
929 #endif
930     dispatchVoid(serial, mSlotId, RIL_REQUEST_HANGUP_WAITING_OR_BACKGROUND);
931     return Void();
932 }
933
934 Return<void> RadioImpl::hangupForegroundResumeBackground(int32_t serial) {
935 #if VDBG
936     RLOGD("hangupForegroundResumeBackground: serial %d", serial);
937 #endif
938     dispatchVoid(serial, mSlotId, RIL_REQUEST_HANGUP_FOREGROUND_RESUME_BACKGROUND);
939     return Void();
940 }
941
942 Return<void> RadioImpl::switchWaitingOrHoldingAndActive(int32_t serial) {
943 #if VDBG
944     RLOGD("switchWaitingOrHoldingAndActive: serial %d", serial);
945 #endif
946     dispatchVoid(serial, mSlotId, RIL_REQUEST_SWITCH_WAITING_OR_HOLDING_AND_ACTIVE);
947     return Void();
948 }
949
950 Return<void> RadioImpl::conference(int32_t serial) {
951 #if VDBG
952     RLOGD("conference: serial %d", serial);
953 #endif
954     dispatchVoid(serial, mSlotId, RIL_REQUEST_CONFERENCE);
955     return Void();
956 }
957
958 Return<void> RadioImpl::rejectCall(int32_t serial) {
959 #if VDBG
960     RLOGD("rejectCall: serial %d", serial);
961 #endif
962     dispatchVoid(serial, mSlotId, RIL_REQUEST_UDUB);
963     return Void();
964 }
965
966 Return<void> RadioImpl::getLastCallFailCause(int32_t serial) {
967 #if VDBG
968     RLOGD("getLastCallFailCause: serial %d", serial);
969 #endif
970     dispatchVoid(serial, mSlotId, RIL_REQUEST_LAST_CALL_FAIL_CAUSE);
971     return Void();
972 }
973
974 Return<void> RadioImpl::getSignalStrength(int32_t serial) {
975 #if VDBG
976     RLOGD("getSignalStrength: serial %d", serial);
977 #endif
978     dispatchVoid(serial, mSlotId, RIL_REQUEST_SIGNAL_STRENGTH);
979     return Void();
980 }
981
982 Return<void> RadioImpl::getVoiceRegistrationState(int32_t serial) {
983 #if VDBG
984     RLOGD("getVoiceRegistrationState: serial %d", serial);
985 #endif
986     dispatchVoid(serial, mSlotId, RIL_REQUEST_VOICE_REGISTRATION_STATE);
987     return Void();
988 }
989
990 Return<void> RadioImpl::getDataRegistrationState(int32_t serial) {
991 #if VDBG
992     RLOGD("getDataRegistrationState: serial %d", serial);
993 #endif
994     dispatchVoid(serial, mSlotId, RIL_REQUEST_DATA_REGISTRATION_STATE);
995     return Void();
996 }
997
998 Return<void> RadioImpl::getOperator(int32_t serial) {
999 #if VDBG
1000     RLOGD("getOperator: serial %d", serial);
1001 #endif
1002     dispatchVoid(serial, mSlotId, RIL_REQUEST_OPERATOR);
1003     return Void();
1004 }
1005
1006 Return<void> RadioImpl::setRadioPower(int32_t serial, bool on) {
1007     RLOGD("setRadioPower: serial %d on %d", serial, on);
1008     dispatchInts(serial, mSlotId, RIL_REQUEST_RADIO_POWER, 1, BOOL_TO_INT(on));
1009     return Void();
1010 }
1011
1012 Return<void> RadioImpl::sendDtmf(int32_t serial, const hidl_string& s) {
1013 #if VDBG
1014     RLOGD("sendDtmf: serial %d", serial);
1015 #endif
1016     dispatchString(serial, mSlotId, RIL_REQUEST_DTMF, s.c_str());
1017     return Void();
1018 }
1019
1020 Return<void> RadioImpl::sendSms(int32_t serial, const GsmSmsMessage& message) {
1021 #if VDBG
1022     RLOGD("sendSms: serial %d", serial);
1023 #endif
1024     dispatchStrings(serial, mSlotId, RIL_REQUEST_SEND_SMS,
1025             2, message.smscPdu.c_str(), message.pdu.c_str());
1026     return Void();
1027 }
1028
1029 Return<void> RadioImpl::sendSMSExpectMore(int32_t serial, const GsmSmsMessage& message) {
1030 #if VDBG
1031     RLOGD("sendSMSExpectMore: serial %d", serial);
1032 #endif
1033     dispatchStrings(serial, mSlotId, RIL_REQUEST_SEND_SMS_EXPECT_MORE,
1034             2, message.smscPdu.c_str(), message.pdu.c_str());
1035     return Void();
1036 }
1037
1038 static bool convertMvnoTypeToString(MvnoType type, char *&str) {
1039     switch (type) {
1040         case MvnoType::IMSI:
1041             str = (char *)"imsi";
1042             return true;
1043         case MvnoType::GID:
1044             str = (char *)"gid";
1045             return true;
1046         case MvnoType::SPN:
1047             str = (char *)"spn";
1048             return true;
1049         case MvnoType::NONE:
1050             str = (char *)"";
1051             return true;
1052     }
1053     return false;
1054 }
1055
1056 Return<void> RadioImpl::setupDataCall(int32_t serial, RadioTechnology radioTechnology,
1057                                       const DataProfileInfo& dataProfileInfo, bool modemCognitive,
1058                                       bool roamingAllowed, bool isRoaming) {
1059
1060 #if VDBG
1061     RLOGD("setupDataCall: serial %d", serial);
1062 #endif
1063
1064     if (s_vendorFunctions->version >= 4 && s_vendorFunctions->version <= 14) {
1065         const hidl_string &protocol =
1066                 (isRoaming ? dataProfileInfo.roamingProtocol : dataProfileInfo.protocol);
1067         dispatchStrings(serial, mSlotId, RIL_REQUEST_SETUP_DATA_CALL, 7,
1068             std::to_string((int) radioTechnology + 2).c_str(),
1069             std::to_string((int) dataProfileInfo.profileId).c_str(),
1070             dataProfileInfo.apn.c_str(),
1071             dataProfileInfo.user.c_str(),
1072             dataProfileInfo.password.c_str(),
1073             std::to_string((int) dataProfileInfo.authType).c_str(),
1074             protocol.c_str());
1075     } else if (s_vendorFunctions->version >= 15) {
1076         char *mvnoTypeStr = NULL;
1077         if (!convertMvnoTypeToString(dataProfileInfo.mvnoType, mvnoTypeStr)) {
1078             RequestInfo *pRI = android::addRequestToList(serial, mSlotId,
1079                     RIL_REQUEST_SETUP_DATA_CALL);
1080             if (pRI != NULL) {
1081                 sendErrorResponse(pRI, RIL_E_INVALID_ARGUMENTS);
1082             }
1083             return Void();
1084         }
1085         dispatchStrings(serial, mSlotId, RIL_REQUEST_SETUP_DATA_CALL, 15,
1086             std::to_string((int) radioTechnology + 2).c_str(),
1087             std::to_string((int) dataProfileInfo.profileId).c_str(),
1088             dataProfileInfo.apn.c_str(),
1089             dataProfileInfo.user.c_str(),
1090             dataProfileInfo.password.c_str(),
1091             std::to_string((int) dataProfileInfo.authType).c_str(),
1092             dataProfileInfo.protocol.c_str(),
1093             dataProfileInfo.roamingProtocol.c_str(),
1094             std::to_string(dataProfileInfo.supportedApnTypesBitmap).c_str(),
1095             std::to_string(dataProfileInfo.bearerBitmap).c_str(),
1096             modemCognitive ? "1" : "0",
1097             std::to_string(dataProfileInfo.mtu).c_str(),
1098             mvnoTypeStr,
1099             dataProfileInfo.mvnoMatchData.c_str(),
1100             roamingAllowed ? "1" : "0");
1101     } else {
1102         RLOGE("Unsupported RIL version %d, min version expected 4", s_vendorFunctions->version);
1103         RequestInfo *pRI = android::addRequestToList(serial, mSlotId,
1104                 RIL_REQUEST_SETUP_DATA_CALL);
1105         if (pRI != NULL) {
1106             sendErrorResponse(pRI, RIL_E_REQUEST_NOT_SUPPORTED);
1107         }
1108     }
1109     return Void();
1110 }
1111
1112 Return<void> RadioImpl::iccIOForApp(int32_t serial, const IccIo& iccIo) {
1113 #if VDBG
1114     RLOGD("iccIOForApp: serial %d", serial);
1115 #endif
1116     RequestInfo *pRI = android::addRequestToList(serial, mSlotId, RIL_REQUEST_SIM_IO);
1117     if (pRI == NULL) {
1118         return Void();
1119     }
1120
1121     RIL_SIM_IO_v6 rilIccIo = {};
1122     rilIccIo.command = iccIo.command;
1123     rilIccIo.fileid = iccIo.fileId;
1124     if (!copyHidlStringToRil(&rilIccIo.path, iccIo.path, pRI)) {
1125         return Void();
1126     }
1127
1128     rilIccIo.p1 = iccIo.p1;
1129     rilIccIo.p2 = iccIo.p2;
1130     rilIccIo.p3 = iccIo.p3;
1131
1132     if (!copyHidlStringToRil(&rilIccIo.data, iccIo.data, pRI)) {
1133         memsetAndFreeStrings(1, rilIccIo.path);
1134         return Void();
1135     }
1136
1137     if (!copyHidlStringToRil(&rilIccIo.pin2, iccIo.pin2, pRI)) {
1138         memsetAndFreeStrings(2, rilIccIo.path, rilIccIo.data);
1139         return Void();
1140     }
1141
1142     if (!copyHidlStringToRil(&rilIccIo.aidPtr, iccIo.aid, pRI)) {
1143         memsetAndFreeStrings(3, rilIccIo.path, rilIccIo.data, rilIccIo.pin2);
1144         return Void();
1145     }
1146
1147     CALL_ONREQUEST(RIL_REQUEST_SIM_IO, &rilIccIo, sizeof(rilIccIo), pRI, mSlotId);
1148
1149     memsetAndFreeStrings(4, rilIccIo.path, rilIccIo.data, rilIccIo.pin2, rilIccIo.aidPtr);
1150
1151     return Void();
1152 }
1153
1154 Return<void> RadioImpl::sendUssd(int32_t serial, const hidl_string& ussd) {
1155 #if VDBG
1156     RLOGD("sendUssd: serial %d", serial);
1157 #endif
1158     dispatchString(serial, mSlotId, RIL_REQUEST_SEND_USSD, ussd.c_str());
1159     return Void();
1160 }
1161
1162 Return<void> RadioImpl::cancelPendingUssd(int32_t serial) {
1163 #if VDBG
1164     RLOGD("cancelPendingUssd: serial %d", serial);
1165 #endif
1166     dispatchVoid(serial, mSlotId, RIL_REQUEST_CANCEL_USSD);
1167     return Void();
1168 }
1169
1170 Return<void> RadioImpl::getClir(int32_t serial) {
1171 #if VDBG
1172     RLOGD("getClir: serial %d", serial);
1173 #endif
1174     dispatchVoid(serial, mSlotId, RIL_REQUEST_GET_CLIR);
1175     return Void();
1176 }
1177
1178 Return<void> RadioImpl::setClir(int32_t serial, int32_t status) {
1179 #if VDBG
1180     RLOGD("setClir: serial %d", serial);
1181 #endif
1182     dispatchInts(serial, mSlotId, RIL_REQUEST_SET_CLIR, 1, status);
1183     return Void();
1184 }
1185
1186 Return<void> RadioImpl::getCallForwardStatus(int32_t serial, const CallForwardInfo& callInfo) {
1187 #if VDBG
1188     RLOGD("getCallForwardStatus: serial %d", serial);
1189 #endif
1190     dispatchCallForwardStatus(serial, mSlotId, RIL_REQUEST_QUERY_CALL_FORWARD_STATUS,
1191             callInfo);
1192     return Void();
1193 }
1194
1195 Return<void> RadioImpl::setCallForward(int32_t serial, const CallForwardInfo& callInfo) {
1196 #if VDBG
1197     RLOGD("setCallForward: serial %d", serial);
1198 #endif
1199     dispatchCallForwardStatus(serial, mSlotId, RIL_REQUEST_SET_CALL_FORWARD,
1200             callInfo);
1201     return Void();
1202 }
1203
1204 Return<void> RadioImpl::getCallWaiting(int32_t serial, int32_t serviceClass) {
1205 #if VDBG
1206     RLOGD("getCallWaiting: serial %d", serial);
1207 #endif
1208     dispatchInts(serial, mSlotId, RIL_REQUEST_QUERY_CALL_WAITING, 1, serviceClass);
1209     return Void();
1210 }
1211
1212 Return<void> RadioImpl::setCallWaiting(int32_t serial, bool enable, int32_t serviceClass) {
1213 #if VDBG
1214     RLOGD("setCallWaiting: serial %d", serial);
1215 #endif
1216     dispatchInts(serial, mSlotId, RIL_REQUEST_SET_CALL_WAITING, 2, BOOL_TO_INT(enable),
1217             serviceClass);
1218     return Void();
1219 }
1220
1221 Return<void> RadioImpl::acknowledgeLastIncomingGsmSms(int32_t serial,
1222                                                       bool success, SmsAcknowledgeFailCause cause) {
1223 #if VDBG
1224     RLOGD("acknowledgeLastIncomingGsmSms: serial %d", serial);
1225 #endif
1226     dispatchInts(serial, mSlotId, RIL_REQUEST_SMS_ACKNOWLEDGE, 2, BOOL_TO_INT(success),
1227             cause);
1228     return Void();
1229 }
1230
1231 Return<void> RadioImpl::acceptCall(int32_t serial) {
1232 #if VDBG
1233     RLOGD("acceptCall: serial %d", serial);
1234 #endif
1235     dispatchVoid(serial, mSlotId, RIL_REQUEST_ANSWER);
1236     return Void();
1237 }
1238
1239 Return<void> RadioImpl::deactivateDataCall(int32_t serial,
1240                                            int32_t cid, bool reasonRadioShutDown) {
1241 #if VDBG
1242     RLOGD("deactivateDataCall: serial %d", serial);
1243 #endif
1244     dispatchStrings(serial, mSlotId, RIL_REQUEST_DEACTIVATE_DATA_CALL,
1245             2, (std::to_string(cid)).c_str(), reasonRadioShutDown ? "1" : "0");
1246     return Void();
1247 }
1248
1249 Return<void> RadioImpl::getFacilityLockForApp(int32_t serial, const hidl_string& facility,
1250                                               const hidl_string& password, int32_t serviceClass,
1251                                               const hidl_string& appId) {
1252 #if VDBG
1253     RLOGD("getFacilityLockForApp: serial %d", serial);
1254 #endif
1255     dispatchStrings(serial, mSlotId, RIL_REQUEST_QUERY_FACILITY_LOCK,
1256             4, facility.c_str(), password.c_str(),
1257             (std::to_string(serviceClass)).c_str(), appId.c_str());
1258     return Void();
1259 }
1260
1261 Return<void> RadioImpl::setFacilityLockForApp(int32_t serial, const hidl_string& facility,
1262                                               bool lockState, const hidl_string& password,
1263                                               int32_t serviceClass, const hidl_string& appId) {
1264 #if VDBG
1265     RLOGD("setFacilityLockForApp: serial %d", serial);
1266 #endif
1267     dispatchStrings(serial, mSlotId, RIL_REQUEST_SET_FACILITY_LOCK,
1268             5, facility.c_str(), lockState ? "1" : "0", password.c_str(),
1269             (std::to_string(serviceClass)).c_str(), appId.c_str() );
1270     return Void();
1271 }
1272
1273 Return<void> RadioImpl::setBarringPassword(int32_t serial, const hidl_string& facility,
1274                                            const hidl_string& oldPassword,
1275                                            const hidl_string& newPassword) {
1276 #if VDBG
1277     RLOGD("setBarringPassword: serial %d", serial);
1278 #endif
1279     dispatchStrings(serial, mSlotId, RIL_REQUEST_CHANGE_BARRING_PASSWORD,
1280             3, facility.c_str(), oldPassword.c_str(), newPassword.c_str());
1281     return Void();
1282 }
1283
1284 Return<void> RadioImpl::getNetworkSelectionMode(int32_t serial) {
1285 #if VDBG
1286     RLOGD("getNetworkSelectionMode: serial %d", serial);
1287 #endif
1288     dispatchVoid(serial, mSlotId, RIL_REQUEST_QUERY_NETWORK_SELECTION_MODE);
1289     return Void();
1290 }
1291
1292 Return<void> RadioImpl::setNetworkSelectionModeAutomatic(int32_t serial) {
1293 #if VDBG
1294     RLOGD("setNetworkSelectionModeAutomatic: serial %d", serial);
1295 #endif
1296     dispatchVoid(serial, mSlotId, RIL_REQUEST_SET_NETWORK_SELECTION_AUTOMATIC);
1297     return Void();
1298 }
1299
1300 Return<void> RadioImpl::setNetworkSelectionModeManual(int32_t serial,
1301                                                       const hidl_string& operatorNumeric) {
1302 #if VDBG
1303     RLOGD("setNetworkSelectionModeManual: serial %d", serial);
1304 #endif
1305     dispatchString(serial, mSlotId, RIL_REQUEST_SET_NETWORK_SELECTION_MANUAL,
1306             operatorNumeric.c_str());
1307     return Void();
1308 }
1309
1310 Return<void> RadioImpl::getAvailableNetworks(int32_t serial) {
1311 #if VDBG
1312     RLOGD("getAvailableNetworks: serial %d", serial);
1313 #endif
1314     dispatchVoid(serial, mSlotId, RIL_REQUEST_QUERY_AVAILABLE_NETWORKS);
1315     return Void();
1316 }
1317
1318 Return<void> RadioImpl::startDtmf(int32_t serial, const hidl_string& s) {
1319 #if VDBG
1320     RLOGD("startDtmf: serial %d", serial);
1321 #endif
1322     dispatchString(serial, mSlotId, RIL_REQUEST_DTMF_START,
1323             s.c_str());
1324     return Void();
1325 }
1326
1327 Return<void> RadioImpl::stopDtmf(int32_t serial) {
1328 #if VDBG
1329     RLOGD("stopDtmf: serial %d", serial);
1330 #endif
1331     dispatchVoid(serial, mSlotId, RIL_REQUEST_DTMF_STOP);
1332     return Void();
1333 }
1334
1335 Return<void> RadioImpl::getBasebandVersion(int32_t serial) {
1336 #if VDBG
1337     RLOGD("getBasebandVersion: serial %d", serial);
1338 #endif
1339     dispatchVoid(serial, mSlotId, RIL_REQUEST_BASEBAND_VERSION);
1340     return Void();
1341 }
1342
1343 Return<void> RadioImpl::separateConnection(int32_t serial, int32_t gsmIndex) {
1344 #if VDBG
1345     RLOGD("separateConnection: serial %d", serial);
1346 #endif
1347     dispatchInts(serial, mSlotId, RIL_REQUEST_SEPARATE_CONNECTION, 1, gsmIndex);
1348     return Void();
1349 }
1350
1351 Return<void> RadioImpl::setMute(int32_t serial, bool enable) {
1352 #if VDBG
1353     RLOGD("setMute: serial %d", serial);
1354 #endif
1355     dispatchInts(serial, mSlotId, RIL_REQUEST_SET_MUTE, 1, BOOL_TO_INT(enable));
1356     return Void();
1357 }
1358
1359 Return<void> RadioImpl::getMute(int32_t serial) {
1360 #if VDBG
1361     RLOGD("getMute: serial %d", serial);
1362 #endif
1363     dispatchVoid(serial, mSlotId, RIL_REQUEST_GET_MUTE);
1364     return Void();
1365 }
1366
1367 Return<void> RadioImpl::getClip(int32_t serial) {
1368 #if VDBG
1369     RLOGD("getClip: serial %d", serial);
1370 #endif
1371     dispatchVoid(serial, mSlotId, RIL_REQUEST_QUERY_CLIP);
1372     return Void();
1373 }
1374
1375 Return<void> RadioImpl::getDataCallList(int32_t serial) {
1376 #if VDBG
1377     RLOGD("getDataCallList: serial %d", serial);
1378 #endif
1379     dispatchVoid(serial, mSlotId, RIL_REQUEST_DATA_CALL_LIST);
1380     return Void();
1381 }
1382
1383 Return<void> RadioImpl::setSuppServiceNotifications(int32_t serial, bool enable) {
1384 #if VDBG
1385     RLOGD("setSuppServiceNotifications: serial %d", serial);
1386 #endif
1387     dispatchInts(serial, mSlotId, RIL_REQUEST_SET_SUPP_SVC_NOTIFICATION, 1,
1388             BOOL_TO_INT(enable));
1389     return Void();
1390 }
1391
1392 Return<void> RadioImpl::writeSmsToSim(int32_t serial, const SmsWriteArgs& smsWriteArgs) {
1393 #if VDBG
1394     RLOGD("writeSmsToSim: serial %d", serial);
1395 #endif
1396     RequestInfo *pRI = android::addRequestToList(serial, mSlotId, RIL_REQUEST_WRITE_SMS_TO_SIM);
1397     if (pRI == NULL) {
1398         return Void();
1399     }
1400
1401     RIL_SMS_WriteArgs args;
1402     args.status = (int) smsWriteArgs.status;
1403
1404     int len;
1405     if (!copyHidlStringToRil(&args.pdu, smsWriteArgs.pdu, pRI)) {
1406         return Void();
1407     }
1408
1409     if (!copyHidlStringToRil(&args.smsc, smsWriteArgs.smsc, pRI)) {
1410         memsetAndFreeStrings(1, args.pdu);
1411         return Void();
1412     }
1413
1414     CALL_ONREQUEST(RIL_REQUEST_WRITE_SMS_TO_SIM, &args, sizeof(args), pRI, mSlotId);
1415
1416     memsetAndFreeStrings(2, args.smsc, args.pdu);
1417
1418     return Void();
1419 }
1420
1421 Return<void> RadioImpl::deleteSmsOnSim(int32_t serial, int32_t index) {
1422 #if VDBG
1423     RLOGD("deleteSmsOnSim: serial %d", serial);
1424 #endif
1425     dispatchInts(serial, mSlotId, RIL_REQUEST_DELETE_SMS_ON_SIM, 1, index);
1426     return Void();
1427 }
1428
1429 Return<void> RadioImpl::setBandMode(int32_t serial, RadioBandMode mode) {
1430 #if VDBG
1431     RLOGD("setBandMode: serial %d", serial);
1432 #endif
1433     dispatchInts(serial, mSlotId, RIL_REQUEST_SET_BAND_MODE, 1, mode);
1434     return Void();
1435 }
1436
1437 Return<void> RadioImpl::getAvailableBandModes(int32_t serial) {
1438 #if VDBG
1439     RLOGD("getAvailableBandModes: serial %d", serial);
1440 #endif
1441     dispatchVoid(serial, mSlotId, RIL_REQUEST_QUERY_AVAILABLE_BAND_MODE);
1442     return Void();
1443 }
1444
1445 Return<void> RadioImpl::sendEnvelope(int32_t serial, const hidl_string& command) {
1446 #if VDBG
1447     RLOGD("sendEnvelope: serial %d", serial);
1448 #endif
1449     dispatchString(serial, mSlotId, RIL_REQUEST_STK_SEND_ENVELOPE_COMMAND,
1450             command.c_str());
1451     return Void();
1452 }
1453
1454 Return<void> RadioImpl::sendTerminalResponseToSim(int32_t serial,
1455                                                   const hidl_string& commandResponse) {
1456 #if VDBG
1457     RLOGD("sendTerminalResponseToSim: serial %d", serial);
1458 #endif
1459     dispatchString(serial, mSlotId, RIL_REQUEST_STK_SEND_TERMINAL_RESPONSE,
1460             commandResponse.c_str());
1461     return Void();
1462 }
1463
1464 Return<void> RadioImpl::handleStkCallSetupRequestFromSim(int32_t serial, bool accept) {
1465 #if VDBG
1466     RLOGD("handleStkCallSetupRequestFromSim: serial %d", serial);
1467 #endif
1468     dispatchInts(serial, mSlotId, RIL_REQUEST_STK_HANDLE_CALL_SETUP_REQUESTED_FROM_SIM,
1469             1, BOOL_TO_INT(accept));
1470     return Void();
1471 }
1472
1473 Return<void> RadioImpl::explicitCallTransfer(int32_t serial) {
1474 #if VDBG
1475     RLOGD("explicitCallTransfer: serial %d", serial);
1476 #endif
1477     dispatchVoid(serial, mSlotId, RIL_REQUEST_EXPLICIT_CALL_TRANSFER);
1478     return Void();
1479 }
1480
1481 Return<void> RadioImpl::setPreferredNetworkType(int32_t serial, PreferredNetworkType nwType) {
1482 #if VDBG
1483     RLOGD("setPreferredNetworkType: serial %d", serial);
1484 #endif
1485     dispatchInts(serial, mSlotId, RIL_REQUEST_SET_PREFERRED_NETWORK_TYPE, 1, nwType);
1486     return Void();
1487 }
1488
1489 Return<void> RadioImpl::getPreferredNetworkType(int32_t serial) {
1490 #if VDBG
1491     RLOGD("getPreferredNetworkType: serial %d", serial);
1492 #endif
1493     dispatchVoid(serial, mSlotId, RIL_REQUEST_GET_PREFERRED_NETWORK_TYPE);
1494     return Void();
1495 }
1496
1497 Return<void> RadioImpl::getNeighboringCids(int32_t serial) {
1498 #if VDBG
1499     RLOGD("getNeighboringCids: serial %d", serial);
1500 #endif
1501     dispatchVoid(serial, mSlotId, RIL_REQUEST_GET_NEIGHBORING_CELL_IDS);
1502     return Void();
1503 }
1504
1505 Return<void> RadioImpl::setLocationUpdates(int32_t serial, bool enable) {
1506 #if VDBG
1507     RLOGD("setLocationUpdates: serial %d", serial);
1508 #endif
1509     dispatchInts(serial, mSlotId, RIL_REQUEST_SET_LOCATION_UPDATES, 1, BOOL_TO_INT(enable));
1510     return Void();
1511 }
1512
1513 Return<void> RadioImpl::setCdmaSubscriptionSource(int32_t serial, CdmaSubscriptionSource cdmaSub) {
1514 #if VDBG
1515     RLOGD("setCdmaSubscriptionSource: serial %d", serial);
1516 #endif
1517     dispatchInts(serial, mSlotId, RIL_REQUEST_CDMA_SET_SUBSCRIPTION_SOURCE, 1, cdmaSub);
1518     return Void();
1519 }
1520
1521 Return<void> RadioImpl::setCdmaRoamingPreference(int32_t serial, CdmaRoamingType type) {
1522 #if VDBG
1523     RLOGD("setCdmaRoamingPreference: serial %d", serial);
1524 #endif
1525     dispatchInts(serial, mSlotId, RIL_REQUEST_CDMA_SET_ROAMING_PREFERENCE, 1, type);
1526     return Void();
1527 }
1528
1529 Return<void> RadioImpl::getCdmaRoamingPreference(int32_t serial) {
1530 #if VDBG
1531     RLOGD("getCdmaRoamingPreference: serial %d", serial);
1532 #endif
1533     dispatchVoid(serial, mSlotId, RIL_REQUEST_CDMA_QUERY_ROAMING_PREFERENCE);
1534     return Void();
1535 }
1536
1537 Return<void> RadioImpl::setTTYMode(int32_t serial, TtyMode mode) {
1538 #if VDBG
1539     RLOGD("setTTYMode: serial %d", serial);
1540 #endif
1541     dispatchInts(serial, mSlotId, RIL_REQUEST_SET_TTY_MODE, 1, mode);
1542     return Void();
1543 }
1544
1545 Return<void> RadioImpl::getTTYMode(int32_t serial) {
1546 #if VDBG
1547     RLOGD("getTTYMode: serial %d", serial);
1548 #endif
1549     dispatchVoid(serial, mSlotId, RIL_REQUEST_QUERY_TTY_MODE);
1550     return Void();
1551 }
1552
1553 Return<void> RadioImpl::setPreferredVoicePrivacy(int32_t serial, bool enable) {
1554 #if VDBG
1555     RLOGD("setPreferredVoicePrivacy: serial %d", serial);
1556 #endif
1557     dispatchInts(serial, mSlotId, RIL_REQUEST_CDMA_SET_PREFERRED_VOICE_PRIVACY_MODE,
1558             1, BOOL_TO_INT(enable));
1559     return Void();
1560 }
1561
1562 Return<void> RadioImpl::getPreferredVoicePrivacy(int32_t serial) {
1563 #if VDBG
1564     RLOGD("getPreferredVoicePrivacy: serial %d", serial);
1565 #endif
1566     dispatchVoid(serial, mSlotId, RIL_REQUEST_CDMA_QUERY_PREFERRED_VOICE_PRIVACY_MODE);
1567     return Void();
1568 }
1569
1570 Return<void> RadioImpl::sendCDMAFeatureCode(int32_t serial, const hidl_string& featureCode) {
1571 #if VDBG
1572     RLOGD("sendCDMAFeatureCode: serial %d", serial);
1573 #endif
1574     dispatchString(serial, mSlotId, RIL_REQUEST_CDMA_FLASH,
1575             featureCode.c_str());
1576     return Void();
1577 }
1578
1579 Return<void> RadioImpl::sendBurstDtmf(int32_t serial, const hidl_string& dtmf, int32_t on,
1580                                       int32_t off) {
1581 #if VDBG
1582     RLOGD("sendBurstDtmf: serial %d", serial);
1583 #endif
1584     dispatchStrings(serial, mSlotId, RIL_REQUEST_CDMA_BURST_DTMF,
1585             3, dtmf.c_str(), (std::to_string(on)).c_str(),
1586             (std::to_string(off)).c_str());
1587     return Void();
1588 }
1589
1590 void constructCdmaSms(RIL_CDMA_SMS_Message &rcsm, const CdmaSmsMessage& sms) {
1591     rcsm.uTeleserviceID = sms.teleserviceId;
1592     rcsm.bIsServicePresent = BOOL_TO_INT(sms.isServicePresent);
1593     rcsm.uServicecategory = sms.serviceCategory;
1594     rcsm.sAddress.digit_mode = (RIL_CDMA_SMS_DigitMode) sms.address.digitMode;
1595     rcsm.sAddress.number_mode = (RIL_CDMA_SMS_NumberMode) sms.address.numberMode;
1596     rcsm.sAddress.number_type = (RIL_CDMA_SMS_NumberType) sms.address.numberType;
1597     rcsm.sAddress.number_plan = (RIL_CDMA_SMS_NumberPlan) sms.address.numberPlan;
1598
1599     rcsm.sAddress.number_of_digits = sms.address.digits.size();
1600     int digitLimit= MIN((rcsm.sAddress.number_of_digits), RIL_CDMA_SMS_ADDRESS_MAX);
1601     for (int i = 0; i < digitLimit; i++) {
1602         rcsm.sAddress.digits[i] = sms.address.digits[i];
1603     }
1604
1605     rcsm.sSubAddress.subaddressType = (RIL_CDMA_SMS_SubaddressType) sms.subAddress.subaddressType;
1606     rcsm.sSubAddress.odd = BOOL_TO_INT(sms.subAddress.odd);
1607
1608     rcsm.sSubAddress.number_of_digits = sms.subAddress.digits.size();
1609     digitLimit= MIN((rcsm.sSubAddress.number_of_digits), RIL_CDMA_SMS_SUBADDRESS_MAX);
1610     for (int i = 0; i < digitLimit; i++) {
1611         rcsm.sSubAddress.digits[i] = sms.subAddress.digits[i];
1612     }
1613
1614     rcsm.uBearerDataLen = sms.bearerData.size();
1615     digitLimit= MIN((rcsm.uBearerDataLen), RIL_CDMA_SMS_BEARER_DATA_MAX);
1616     for (int i = 0; i < digitLimit; i++) {
1617         rcsm.aBearerData[i] = sms.bearerData[i];
1618     }
1619 }
1620
1621 Return<void> RadioImpl::sendCdmaSms(int32_t serial, const CdmaSmsMessage& sms) {
1622 #if VDBG
1623     RLOGD("sendCdmaSms: serial %d", serial);
1624 #endif
1625     RequestInfo *pRI = android::addRequestToList(serial, mSlotId, RIL_REQUEST_CDMA_SEND_SMS);
1626     if (pRI == NULL) {
1627         return Void();
1628     }
1629
1630     RIL_CDMA_SMS_Message rcsm = {};
1631     constructCdmaSms(rcsm, sms);
1632
1633     CALL_ONREQUEST(pRI->pCI->requestNumber, &rcsm, sizeof(rcsm), pRI, mSlotId);
1634     return Void();
1635 }
1636
1637 Return<void> RadioImpl::acknowledgeLastIncomingCdmaSms(int32_t serial, const CdmaSmsAck& smsAck) {
1638 #if VDBG
1639     RLOGD("acknowledgeLastIncomingCdmaSms: serial %d", serial);
1640 #endif
1641     RequestInfo *pRI = android::addRequestToList(serial, mSlotId, RIL_REQUEST_CDMA_SMS_ACKNOWLEDGE);
1642     if (pRI == NULL) {
1643         return Void();
1644     }
1645
1646     RIL_CDMA_SMS_Ack rcsa = {};
1647
1648     rcsa.uErrorClass = (RIL_CDMA_SMS_ErrorClass) smsAck.errorClass;
1649     rcsa.uSMSCauseCode = smsAck.smsCauseCode;
1650
1651     CALL_ONREQUEST(pRI->pCI->requestNumber, &rcsa, sizeof(rcsa), pRI, mSlotId);
1652     return Void();
1653 }
1654
1655 Return<void> RadioImpl::getGsmBroadcastConfig(int32_t serial) {
1656 #if VDBG
1657     RLOGD("getGsmBroadcastConfig: serial %d", serial);
1658 #endif
1659     dispatchVoid(serial, mSlotId, RIL_REQUEST_GSM_GET_BROADCAST_SMS_CONFIG);
1660     return Void();
1661 }
1662
1663 Return<void> RadioImpl::setGsmBroadcastConfig(int32_t serial,
1664                                               const hidl_vec<GsmBroadcastSmsConfigInfo>&
1665                                               configInfo) {
1666 #if VDBG
1667     RLOGD("setGsmBroadcastConfig: serial %d", serial);
1668 #endif
1669     RequestInfo *pRI = android::addRequestToList(serial, mSlotId,
1670             RIL_REQUEST_GSM_SET_BROADCAST_SMS_CONFIG);
1671     if (pRI == NULL) {
1672         return Void();
1673     }
1674
1675     int num = configInfo.size();
1676     RIL_GSM_BroadcastSmsConfigInfo gsmBci[num];
1677     RIL_GSM_BroadcastSmsConfigInfo *gsmBciPtrs[num];
1678
1679     for (int i = 0 ; i < num ; i++ ) {
1680         gsmBciPtrs[i] = &gsmBci[i];
1681         gsmBci[i].fromServiceId = configInfo[i].fromServiceId;
1682         gsmBci[i].toServiceId = configInfo[i].toServiceId;
1683         gsmBci[i].fromCodeScheme = configInfo[i].fromCodeScheme;
1684         gsmBci[i].toCodeScheme = configInfo[i].toCodeScheme;
1685         gsmBci[i].selected = BOOL_TO_INT(configInfo[i].selected);
1686     }
1687
1688     CALL_ONREQUEST(pRI->pCI->requestNumber, gsmBciPtrs,
1689             num * sizeof(RIL_GSM_BroadcastSmsConfigInfo *), pRI, mSlotId);
1690     return Void();
1691 }
1692
1693 Return<void> RadioImpl::setGsmBroadcastActivation(int32_t serial, bool activate) {
1694 #if VDBG
1695     RLOGD("setGsmBroadcastActivation: serial %d", serial);
1696 #endif
1697     dispatchInts(serial, mSlotId, RIL_REQUEST_GSM_SMS_BROADCAST_ACTIVATION,
1698             1, BOOL_TO_INT(!activate));
1699     return Void();
1700 }
1701
1702 Return<void> RadioImpl::getCdmaBroadcastConfig(int32_t serial) {
1703 #if VDBG
1704     RLOGD("getCdmaBroadcastConfig: serial %d", serial);
1705 #endif
1706     dispatchVoid(serial, mSlotId, RIL_REQUEST_CDMA_GET_BROADCAST_SMS_CONFIG);
1707     return Void();
1708 }
1709
1710 Return<void> RadioImpl::setCdmaBroadcastConfig(int32_t serial,
1711                                                const hidl_vec<CdmaBroadcastSmsConfigInfo>&
1712                                                configInfo) {
1713 #if VDBG
1714     RLOGD("setCdmaBroadcastConfig: serial %d", serial);
1715 #endif
1716     RequestInfo *pRI = android::addRequestToList(serial, mSlotId,
1717             RIL_REQUEST_CDMA_SET_BROADCAST_SMS_CONFIG);
1718     if (pRI == NULL) {
1719         return Void();
1720     }
1721
1722     int num = configInfo.size();
1723     RIL_CDMA_BroadcastSmsConfigInfo cdmaBci[num];
1724     RIL_CDMA_BroadcastSmsConfigInfo *cdmaBciPtrs[num];
1725
1726     for (int i = 0 ; i < num ; i++ ) {
1727         cdmaBciPtrs[i] = &cdmaBci[i];
1728         cdmaBci[i].service_category = configInfo[i].serviceCategory;
1729         cdmaBci[i].language = configInfo[i].language;
1730         cdmaBci[i].selected = BOOL_TO_INT(configInfo[i].selected);
1731     }
1732
1733     CALL_ONREQUEST(pRI->pCI->requestNumber, cdmaBciPtrs,
1734             num * sizeof(RIL_CDMA_BroadcastSmsConfigInfo *), pRI, mSlotId);
1735     return Void();
1736 }
1737
1738 Return<void> RadioImpl::setCdmaBroadcastActivation(int32_t serial, bool activate) {
1739 #if VDBG
1740     RLOGD("setCdmaBroadcastActivation: serial %d", serial);
1741 #endif
1742     dispatchInts(serial, mSlotId, RIL_REQUEST_CDMA_SMS_BROADCAST_ACTIVATION,
1743             1, BOOL_TO_INT(!activate));
1744     return Void();
1745 }
1746
1747 Return<void> RadioImpl::getCDMASubscription(int32_t serial) {
1748 #if VDBG
1749     RLOGD("getCDMASubscription: serial %d", serial);
1750 #endif
1751     dispatchVoid(serial, mSlotId, RIL_REQUEST_CDMA_SUBSCRIPTION);
1752     return Void();
1753 }
1754
1755 Return<void> RadioImpl::writeSmsToRuim(int32_t serial, const CdmaSmsWriteArgs& cdmaSms) {
1756 #if VDBG
1757     RLOGD("writeSmsToRuim: serial %d", serial);
1758 #endif
1759     RequestInfo *pRI = android::addRequestToList(serial, mSlotId,
1760             RIL_REQUEST_CDMA_WRITE_SMS_TO_RUIM);
1761     if (pRI == NULL) {
1762         return Void();
1763     }
1764
1765     RIL_CDMA_SMS_WriteArgs rcsw = {};
1766     rcsw.status = (int) cdmaSms.status;
1767     constructCdmaSms(rcsw.message, cdmaSms.message);
1768
1769     CALL_ONREQUEST(pRI->pCI->requestNumber, &rcsw, sizeof(rcsw), pRI, mSlotId);
1770     return Void();
1771 }
1772
1773 Return<void> RadioImpl::deleteSmsOnRuim(int32_t serial, int32_t index) {
1774 #if VDBG
1775     RLOGD("deleteSmsOnRuim: serial %d", serial);
1776 #endif
1777     dispatchInts(serial, mSlotId, RIL_REQUEST_CDMA_DELETE_SMS_ON_RUIM, 1, index);
1778     return Void();
1779 }
1780
1781 Return<void> RadioImpl::getDeviceIdentity(int32_t serial) {
1782 #if VDBG
1783     RLOGD("getDeviceIdentity: serial %d", serial);
1784 #endif
1785     dispatchVoid(serial, mSlotId, RIL_REQUEST_DEVICE_IDENTITY);
1786     return Void();
1787 }
1788
1789 Return<void> RadioImpl::exitEmergencyCallbackMode(int32_t serial) {
1790 #if VDBG
1791     RLOGD("exitEmergencyCallbackMode: serial %d", serial);
1792 #endif
1793     dispatchVoid(serial, mSlotId, RIL_REQUEST_EXIT_EMERGENCY_CALLBACK_MODE);
1794     return Void();
1795 }
1796
1797 Return<void> RadioImpl::getSmscAddress(int32_t serial) {
1798 #if VDBG
1799     RLOGD("getSmscAddress: serial %d", serial);
1800 #endif
1801     dispatchVoid(serial, mSlotId, RIL_REQUEST_GET_SMSC_ADDRESS);
1802     return Void();
1803 }
1804
1805 Return<void> RadioImpl::setSmscAddress(int32_t serial, const hidl_string& smsc) {
1806 #if VDBG
1807     RLOGD("setSmscAddress: serial %d", serial);
1808 #endif
1809     dispatchString(serial, mSlotId, RIL_REQUEST_SET_SMSC_ADDRESS,
1810             smsc.c_str());
1811     return Void();
1812 }
1813
1814 Return<void> RadioImpl::reportSmsMemoryStatus(int32_t serial, bool available) {
1815 #if VDBG
1816     RLOGD("reportSmsMemoryStatus: serial %d", serial);
1817 #endif
1818     dispatchInts(serial, mSlotId, RIL_REQUEST_REPORT_SMS_MEMORY_STATUS, 1,
1819             BOOL_TO_INT(available));
1820     return Void();
1821 }
1822
1823 Return<void> RadioImpl::reportStkServiceIsRunning(int32_t serial) {
1824 #if VDBG
1825     RLOGD("reportStkServiceIsRunning: serial %d", serial);
1826 #endif
1827     dispatchVoid(serial, mSlotId, RIL_REQUEST_REPORT_STK_SERVICE_IS_RUNNING);
1828     return Void();
1829 }
1830
1831 Return<void> RadioImpl::getCdmaSubscriptionSource(int32_t serial) {
1832 #if VDBG
1833     RLOGD("getCdmaSubscriptionSource: serial %d", serial);
1834 #endif
1835     dispatchVoid(serial, mSlotId, RIL_REQUEST_CDMA_GET_SUBSCRIPTION_SOURCE);
1836     return Void();
1837 }
1838
1839 Return<void> RadioImpl::requestIsimAuthentication(int32_t serial, const hidl_string& challenge) {
1840 #if VDBG
1841     RLOGD("requestIsimAuthentication: serial %d", serial);
1842 #endif
1843     dispatchString(serial, mSlotId, RIL_REQUEST_ISIM_AUTHENTICATION,
1844             challenge.c_str());
1845     return Void();
1846 }
1847
1848 Return<void> RadioImpl::acknowledgeIncomingGsmSmsWithPdu(int32_t serial, bool success,
1849                                                          const hidl_string& ackPdu) {
1850 #if VDBG
1851     RLOGD("acknowledgeIncomingGsmSmsWithPdu: serial %d", serial);
1852 #endif
1853     dispatchStrings(serial, mSlotId, RIL_REQUEST_ACKNOWLEDGE_INCOMING_GSM_SMS_WITH_PDU,
1854             2, success ? "1" : "0", ackPdu.c_str());
1855     return Void();
1856 }
1857
1858 Return<void> RadioImpl::sendEnvelopeWithStatus(int32_t serial, const hidl_string& contents) {
1859 #if VDBG
1860     RLOGD("sendEnvelopeWithStatus: serial %d", serial);
1861 #endif
1862     dispatchString(serial, mSlotId, RIL_REQUEST_STK_SEND_ENVELOPE_WITH_STATUS,
1863             contents.c_str());
1864     return Void();
1865 }
1866
1867 Return<void> RadioImpl::getVoiceRadioTechnology(int32_t serial) {
1868 #if VDBG
1869     RLOGD("getVoiceRadioTechnology: serial %d", serial);
1870 #endif
1871     dispatchVoid(serial, mSlotId, RIL_REQUEST_VOICE_RADIO_TECH);
1872     return Void();
1873 }
1874
1875 Return<void> RadioImpl::getCellInfoList(int32_t serial) {
1876 #if VDBG
1877     RLOGD("getCellInfoList: serial %d", serial);
1878 #endif
1879     dispatchVoid(serial, mSlotId, RIL_REQUEST_GET_CELL_INFO_LIST);
1880     return Void();
1881 }
1882
1883 Return<void> RadioImpl::setCellInfoListRate(int32_t serial, int32_t rate) {
1884 #if VDBG
1885     RLOGD("setCellInfoListRate: serial %d", serial);
1886 #endif
1887     dispatchInts(serial, mSlotId, RIL_REQUEST_SET_UNSOL_CELL_INFO_LIST_RATE, 1, rate);
1888     return Void();
1889 }
1890
1891 Return<void> RadioImpl::setInitialAttachApn(int32_t serial, const DataProfileInfo& dataProfileInfo,
1892                                             bool modemCognitive, bool isRoaming) {
1893 #if VDBG
1894     RLOGD("setInitialAttachApn: serial %d", serial);
1895 #endif
1896     RequestInfo *pRI = android::addRequestToList(serial, mSlotId,
1897             RIL_REQUEST_SET_INITIAL_ATTACH_APN);
1898     if (pRI == NULL) {
1899         return Void();
1900     }
1901
1902     if (s_vendorFunctions->version <= 14) {
1903         RIL_InitialAttachApn iaa = {};
1904
1905         if (dataProfileInfo.apn.size() == 0) {
1906             iaa.apn = (char *) calloc(1, sizeof(char));
1907             if (iaa.apn == NULL) {
1908                 RLOGE("Memory allocation failed for request %s",
1909                         requestToString(pRI->pCI->requestNumber));
1910                 sendErrorResponse(pRI, RIL_E_NO_MEMORY);
1911                 return Void();
1912             }
1913             iaa.apn[0] = '\0';
1914         } else {
1915             if (!copyHidlStringToRil(&iaa.apn, dataProfileInfo.apn, pRI)) {
1916                 return Void();
1917             }
1918         }
1919
1920         const hidl_string &protocol =
1921                 (isRoaming ? dataProfileInfo.roamingProtocol : dataProfileInfo.protocol);
1922
1923         if (!copyHidlStringToRil(&iaa.protocol, protocol, pRI)) {
1924             memsetAndFreeStrings(1, iaa.apn);
1925             return Void();
1926         }
1927         iaa.authtype = (int) dataProfileInfo.authType;
1928         if (!copyHidlStringToRil(&iaa.username, dataProfileInfo.user, pRI)) {
1929             memsetAndFreeStrings(2, iaa.apn, iaa.protocol);
1930             return Void();
1931         }
1932         if (!copyHidlStringToRil(&iaa.password, dataProfileInfo.password, pRI)) {
1933             memsetAndFreeStrings(3, iaa.apn, iaa.protocol, iaa.username);
1934             return Void();
1935         }
1936
1937         CALL_ONREQUEST(RIL_REQUEST_SET_INITIAL_ATTACH_APN, &iaa, sizeof(iaa), pRI, mSlotId);
1938
1939         memsetAndFreeStrings(4, iaa.apn, iaa.protocol, iaa.username, iaa.password);
1940     } else {
1941         RIL_InitialAttachApn_v15 iaa = {};
1942
1943         if (dataProfileInfo.apn.size() == 0) {
1944             iaa.apn = (char *) calloc(1, sizeof(char));
1945             if (iaa.apn == NULL) {
1946                 RLOGE("Memory allocation failed for request %s",
1947                         requestToString(pRI->pCI->requestNumber));
1948                 sendErrorResponse(pRI, RIL_E_NO_MEMORY);
1949                 return Void();
1950             }
1951             iaa.apn[0] = '\0';
1952         } else {
1953             if (!copyHidlStringToRil(&iaa.apn, dataProfileInfo.apn, pRI)) {
1954                 return Void();
1955             }
1956         }
1957
1958         if (!copyHidlStringToRil(&iaa.protocol, dataProfileInfo.protocol, pRI)) {
1959             memsetAndFreeStrings(1, iaa.apn);
1960             return Void();
1961         }
1962         if (!copyHidlStringToRil(&iaa.roamingProtocol, dataProfileInfo.roamingProtocol, pRI)) {
1963             memsetAndFreeStrings(2, iaa.apn, iaa.protocol);
1964             return Void();
1965         }
1966         iaa.authtype = (int) dataProfileInfo.authType;
1967         if (!copyHidlStringToRil(&iaa.username, dataProfileInfo.user, pRI)) {
1968             memsetAndFreeStrings(3, iaa.apn, iaa.protocol, iaa.roamingProtocol);
1969             return Void();
1970         }
1971         if (!copyHidlStringToRil(&iaa.password, dataProfileInfo.password, pRI)) {
1972             memsetAndFreeStrings(4, iaa.apn, iaa.protocol, iaa.roamingProtocol, iaa.username);
1973             return Void();
1974         }
1975         iaa.supportedTypesBitmask = dataProfileInfo.supportedApnTypesBitmap;
1976         iaa.bearerBitmask = dataProfileInfo.bearerBitmap;
1977         iaa.modemCognitive = BOOL_TO_INT(modemCognitive);
1978         iaa.mtu = dataProfileInfo.mtu;
1979
1980         if (!convertMvnoTypeToString(dataProfileInfo.mvnoType, iaa.mvnoType)) {
1981             sendErrorResponse(pRI, RIL_E_INVALID_ARGUMENTS);
1982             memsetAndFreeStrings(5, iaa.apn, iaa.protocol, iaa.roamingProtocol, iaa.username,
1983                     iaa.password);
1984             return Void();
1985         }
1986
1987         if (!copyHidlStringToRil(&iaa.mvnoMatchData, dataProfileInfo.mvnoMatchData, pRI)) {
1988             memsetAndFreeStrings(5, iaa.apn, iaa.protocol, iaa.roamingProtocol, iaa.username,
1989                     iaa.password);
1990             return Void();
1991         }
1992
1993         CALL_ONREQUEST(RIL_REQUEST_SET_INITIAL_ATTACH_APN, &iaa, sizeof(iaa), pRI, mSlotId);
1994
1995         memsetAndFreeStrings(6, iaa.apn, iaa.protocol, iaa.roamingProtocol, iaa.username,
1996                 iaa.password, iaa.mvnoMatchData);
1997     }
1998
1999     return Void();
2000 }
2001
2002 Return<void> RadioImpl::getImsRegistrationState(int32_t serial) {
2003 #if VDBG
2004     RLOGD("getImsRegistrationState: serial %d", serial);
2005 #endif
2006     dispatchVoid(serial, mSlotId, RIL_REQUEST_IMS_REGISTRATION_STATE);
2007     return Void();
2008 }
2009
2010 bool dispatchImsGsmSms(const ImsSmsMessage& message, RequestInfo *pRI) {
2011     RIL_IMS_SMS_Message rism = {};
2012     char **pStrings;
2013     int countStrings = 2;
2014     int dataLen = sizeof(char *) * countStrings;
2015
2016     rism.tech = RADIO_TECH_3GPP;
2017     rism.retry = BOOL_TO_INT(message.retry);
2018     rism.messageRef = message.messageRef;
2019
2020     if (message.gsmMessage.size() != 1) {
2021         RLOGE("dispatchImsGsmSms: Invalid len %s", requestToString(pRI->pCI->requestNumber));
2022         sendErrorResponse(pRI, RIL_E_INVALID_ARGUMENTS);
2023         return false;
2024     }
2025
2026     pStrings = (char **)calloc(countStrings, sizeof(char *));
2027     if (pStrings == NULL) {
2028         RLOGE("dispatchImsGsmSms: Memory allocation failed for request %s",
2029                 requestToString(pRI->pCI->requestNumber));
2030         sendErrorResponse(pRI, RIL_E_NO_MEMORY);
2031         return false;
2032     }
2033
2034     if (!copyHidlStringToRil(&pStrings[0], message.gsmMessage[0].smscPdu, pRI)) {
2035 #ifdef MEMSET_FREED
2036         memset(pStrings, 0, datalen);
2037 #endif
2038         free(pStrings);
2039         return false;
2040     }
2041
2042     if (!copyHidlStringToRil(&pStrings[1], message.gsmMessage[0].pdu, pRI)) {
2043         memsetAndFreeStrings(1, pStrings[0]);
2044 #ifdef MEMSET_FREED
2045         memset(pStrings, 0, datalen);
2046 #endif
2047         free(pStrings);
2048         return false;
2049     }
2050
2051     rism.message.gsmMessage = pStrings;
2052     CALL_ONREQUEST(pRI->pCI->requestNumber, &rism, sizeof(RIL_RadioTechnologyFamily) +
2053             sizeof(uint8_t) + sizeof(int32_t) + dataLen, pRI, pRI->socket_id);
2054
2055     for (int i = 0 ; i < countStrings ; i++) {
2056         memsetAndFreeStrings(1, pStrings[i]);
2057     }
2058
2059 #ifdef MEMSET_FREED
2060     memset(pStrings, 0, datalen);
2061 #endif
2062     free(pStrings);
2063
2064     return true;
2065 }
2066
2067 bool dispatchImsCdmaSms(const ImsSmsMessage& message, RequestInfo *pRI) {
2068     RIL_IMS_SMS_Message rism = {};
2069     RIL_CDMA_SMS_Message rcsm = {};
2070
2071     if (message.cdmaMessage.size() != 1) {
2072         RLOGE("dispatchImsCdmaSms: Invalid len %s", requestToString(pRI->pCI->requestNumber));
2073         sendErrorResponse(pRI, RIL_E_INVALID_ARGUMENTS);
2074         return false;
2075     }
2076
2077     rism.tech = RADIO_TECH_3GPP2;
2078     rism.retry = BOOL_TO_INT(message.retry);
2079     rism.messageRef = message.messageRef;
2080     rism.message.cdmaMessage = &rcsm;
2081
2082     constructCdmaSms(rcsm, message.cdmaMessage[0]);
2083
2084     CALL_ONREQUEST(pRI->pCI->requestNumber, &rism, sizeof(RIL_RadioTechnologyFamily) +
2085             sizeof(uint8_t) + sizeof(int32_t) + sizeof(rcsm), pRI, pRI->socket_id);
2086
2087     return true;
2088 }
2089
2090 Return<void> RadioImpl::sendImsSms(int32_t serial, const ImsSmsMessage& message) {
2091 #if VDBG
2092     RLOGD("sendImsSms: serial %d", serial);
2093 #endif
2094     RequestInfo *pRI = android::addRequestToList(serial, mSlotId, RIL_REQUEST_IMS_SEND_SMS);
2095     if (pRI == NULL) {
2096         return Void();
2097     }
2098
2099     RIL_RadioTechnologyFamily format = (RIL_RadioTechnologyFamily) message.tech;
2100
2101     if (RADIO_TECH_3GPP == format) {
2102         dispatchImsGsmSms(message, pRI);
2103     } else if (RADIO_TECH_3GPP2 == format) {
2104         dispatchImsCdmaSms(message, pRI);
2105     } else {
2106         RLOGE("sendImsSms: Invalid radio tech %s",
2107                 requestToString(pRI->pCI->requestNumber));
2108         sendErrorResponse(pRI, RIL_E_INVALID_ARGUMENTS);
2109     }
2110     return Void();
2111 }
2112
2113 Return<void> RadioImpl::iccTransmitApduBasicChannel(int32_t serial, const SimApdu& message) {
2114 #if VDBG
2115     RLOGD("iccTransmitApduBasicChannel: serial %d", serial);
2116 #endif
2117     dispatchIccApdu(serial, mSlotId, RIL_REQUEST_SIM_TRANSMIT_APDU_BASIC, message);
2118     return Void();
2119 }
2120
2121 Return<void> RadioImpl::iccOpenLogicalChannel(int32_t serial, const hidl_string& aid, int32_t p2) {
2122 #if VDBG
2123     RLOGD("iccOpenLogicalChannel: serial %d", serial);
2124 #endif
2125     if (s_vendorFunctions->version < 15) {
2126         dispatchString(serial, mSlotId, RIL_REQUEST_SIM_OPEN_CHANNEL, aid.c_str());
2127     } else {
2128         RequestInfo *pRI = android::addRequestToList(serial, mSlotId, RIL_REQUEST_SIM_OPEN_CHANNEL);
2129         if (pRI == NULL) {
2130             return Void();
2131         }
2132
2133         RIL_OpenChannelParams params = {};
2134
2135         params.p2 = p2;
2136
2137         if (!copyHidlStringToRil(&params.aidPtr, aid, pRI)) {
2138             return Void();
2139         }
2140
2141         CALL_ONREQUEST(pRI->pCI->requestNumber, &params, sizeof(params), pRI, mSlotId);
2142
2143         memsetAndFreeStrings(1, params.aidPtr);
2144     }
2145     return Void();
2146 }
2147
2148 Return<void> RadioImpl::iccCloseLogicalChannel(int32_t serial, int32_t channelId) {
2149 #if VDBG
2150     RLOGD("iccCloseLogicalChannel: serial %d", serial);
2151 #endif
2152     dispatchInts(serial, mSlotId, RIL_REQUEST_SIM_CLOSE_CHANNEL, 1, channelId);
2153     return Void();
2154 }
2155
2156 Return<void> RadioImpl::iccTransmitApduLogicalChannel(int32_t serial, const SimApdu& message) {
2157 #if VDBG
2158     RLOGD("iccTransmitApduLogicalChannel: serial %d", serial);
2159 #endif
2160     dispatchIccApdu(serial, mSlotId, RIL_REQUEST_SIM_TRANSMIT_APDU_CHANNEL, message);
2161     return Void();
2162 }
2163
2164 Return<void> RadioImpl::nvReadItem(int32_t serial, NvItem itemId) {
2165 #if VDBG
2166     RLOGD("nvReadItem: serial %d", serial);
2167 #endif
2168     RequestInfo *pRI = android::addRequestToList(serial, mSlotId, RIL_REQUEST_NV_READ_ITEM);
2169     if (pRI == NULL) {
2170         return Void();
2171     }
2172
2173     RIL_NV_ReadItem nvri = {};
2174     nvri.itemID = (RIL_NV_Item) itemId;
2175
2176     CALL_ONREQUEST(pRI->pCI->requestNumber, &nvri, sizeof(nvri), pRI, mSlotId);
2177     return Void();
2178 }
2179
2180 Return<void> RadioImpl::nvWriteItem(int32_t serial, const NvWriteItem& item) {
2181 #if VDBG
2182     RLOGD("nvWriteItem: serial %d", serial);
2183 #endif
2184     RequestInfo *pRI = android::addRequestToList(serial, mSlotId, RIL_REQUEST_NV_WRITE_ITEM);
2185     if (pRI == NULL) {
2186         return Void();
2187     }
2188
2189     RIL_NV_WriteItem nvwi = {};
2190
2191     nvwi.itemID = (RIL_NV_Item) item.itemId;
2192
2193     if (!copyHidlStringToRil(&nvwi.value, item.value, pRI)) {
2194         return Void();
2195     }
2196
2197     CALL_ONREQUEST(pRI->pCI->requestNumber, &nvwi, sizeof(nvwi), pRI, mSlotId);
2198
2199     memsetAndFreeStrings(1, nvwi.value);
2200     return Void();
2201 }
2202
2203 Return<void> RadioImpl::nvWriteCdmaPrl(int32_t serial, const hidl_vec<uint8_t>& prl) {
2204 #if VDBG
2205     RLOGD("nvWriteCdmaPrl: serial %d", serial);
2206 #endif
2207     dispatchRaw(serial, mSlotId, RIL_REQUEST_NV_WRITE_CDMA_PRL, prl);
2208     return Void();
2209 }
2210
2211 Return<void> RadioImpl::nvResetConfig(int32_t serial, ResetNvType resetType) {
2212     int rilResetType = -1;
2213 #if VDBG
2214     RLOGD("nvResetConfig: serial %d", serial);
2215 #endif
2216     /* Convert ResetNvType to RIL.h values
2217      * RIL_REQUEST_NV_RESET_CONFIG
2218      * 1 - reload all NV items
2219      * 2 - erase NV reset (SCRTN)
2220      * 3 - factory reset (RTN)
2221      */
2222     switch(resetType) {
2223       case ResetNvType::RELOAD:
2224         rilResetType = 1;
2225         break;
2226       case ResetNvType::ERASE:
2227         rilResetType = 2;
2228         break;
2229       case ResetNvType::FACTORY_RESET:
2230         rilResetType = 3;
2231         break;
2232     }
2233     dispatchInts(serial, mSlotId, RIL_REQUEST_NV_RESET_CONFIG, 1, rilResetType);
2234     return Void();
2235 }
2236
2237 Return<void> RadioImpl::setUiccSubscription(int32_t serial, const SelectUiccSub& uiccSub) {
2238 #if VDBG
2239     RLOGD("setUiccSubscription: serial %d", serial);
2240 #endif
2241     RequestInfo *pRI = android::addRequestToList(serial, mSlotId,
2242             RIL_REQUEST_SET_UICC_SUBSCRIPTION);
2243     if (pRI == NULL) {
2244         return Void();
2245     }
2246
2247     RIL_SelectUiccSub rilUiccSub = {};
2248
2249     rilUiccSub.slot = uiccSub.slot;
2250     rilUiccSub.app_index = uiccSub.appIndex;
2251     rilUiccSub.sub_type = (RIL_SubscriptionType) uiccSub.subType;
2252     rilUiccSub.act_status = (RIL_UiccSubActStatus) uiccSub.actStatus;
2253
2254     CALL_ONREQUEST(pRI->pCI->requestNumber, &rilUiccSub, sizeof(rilUiccSub), pRI, mSlotId);
2255     return Void();
2256 }
2257
2258 Return<void> RadioImpl::setDataAllowed(int32_t serial, bool allow) {
2259 #if VDBG
2260     RLOGD("setDataAllowed: serial %d", serial);
2261 #endif
2262     dispatchInts(serial, mSlotId, RIL_REQUEST_ALLOW_DATA, 1, BOOL_TO_INT(allow));
2263     return Void();
2264 }
2265
2266 Return<void> RadioImpl::getHardwareConfig(int32_t serial) {
2267 #if VDBG
2268     RLOGD("getHardwareConfig: serial %d", serial);
2269 #endif
2270     dispatchVoid(serial, mSlotId, RIL_REQUEST_GET_HARDWARE_CONFIG);
2271     return Void();
2272 }
2273
2274 Return<void> RadioImpl::requestIccSimAuthentication(int32_t serial, int32_t authContext,
2275         const hidl_string& authData, const hidl_string& aid) {
2276 #if VDBG
2277     RLOGD("requestIccSimAuthentication: serial %d", serial);
2278 #endif
2279     RequestInfo *pRI = android::addRequestToList(serial, mSlotId, RIL_REQUEST_SIM_AUTHENTICATION);
2280     if (pRI == NULL) {
2281         return Void();
2282     }
2283
2284     RIL_SimAuthentication pf = {};
2285
2286     pf.authContext = authContext;
2287
2288     int len;
2289     if (!copyHidlStringToRil(&pf.authData, authData, pRI)) {
2290         return Void();
2291     }
2292
2293     if (!copyHidlStringToRil(&pf.aid, aid, pRI)) {
2294         memsetAndFreeStrings(1, pf.authData);
2295         return Void();
2296     }
2297
2298     CALL_ONREQUEST(pRI->pCI->requestNumber, &pf, sizeof(pf), pRI, mSlotId);
2299
2300     memsetAndFreeStrings(2, pf.authData, pf.aid);
2301     return Void();
2302 }
2303
2304 /**
2305  * @param numProfiles number of data profile
2306  * @param dataProfiles the pointer to the actual data profiles. The acceptable type is
2307           RIL_DataProfileInfo or RIL_DataProfileInfo_v15.
2308  * @param dataProfilePtrs the pointer to the pointers that point to each data profile structure
2309  * @param numfields number of string-type member in the data profile structure
2310  * @param ... the variadic parameters are pointers to each string-type member
2311  **/
2312 template <typename T>
2313 void freeSetDataProfileData(int numProfiles, T *dataProfiles, T **dataProfilePtrs,
2314                             int numfields, ...) {
2315     va_list args;
2316     va_start(args, numfields);
2317
2318     // Iterate through each string-type field that need to be free.
2319     for (int i = 0; i < numfields; i++) {
2320         // Iterate through each data profile and free that specific string-type field.
2321         // The type 'char *T::*' is a type of pointer to a 'char *' member inside T structure.
2322         char *T::*ptr = va_arg(args, char *T::*);
2323         for (int j = 0; j < numProfiles; j++) {
2324             memsetAndFreeStrings(1, dataProfiles[j].*ptr);
2325         }
2326     }
2327
2328     va_end(args);
2329
2330 #ifdef MEMSET_FREED
2331     memset(dataProfiles, 0, numProfiles * sizeof(T));
2332     memset(dataProfilePtrs, 0, numProfiles * sizeof(T *));
2333 #endif
2334     free(dataProfiles);
2335     free(dataProfilePtrs);
2336 }
2337
2338 Return<void> RadioImpl::setDataProfile(int32_t serial, const hidl_vec<DataProfileInfo>& profiles,
2339                                        bool isRoaming) {
2340 #if VDBG
2341     RLOGD("setDataProfile: serial %d", serial);
2342 #endif
2343     RequestInfo *pRI = android::addRequestToList(serial, mSlotId, RIL_REQUEST_SET_DATA_PROFILE);
2344     if (pRI == NULL) {
2345         return Void();
2346     }
2347
2348     size_t num = profiles.size();
2349     bool success = false;
2350
2351     if (s_vendorFunctions->version <= 14) {
2352
2353         RIL_DataProfileInfo *dataProfiles =
2354             (RIL_DataProfileInfo *) calloc(num, sizeof(RIL_DataProfileInfo));
2355
2356         if (dataProfiles == NULL) {
2357             RLOGE("Memory allocation failed for request %s",
2358                     requestToString(pRI->pCI->requestNumber));
2359             sendErrorResponse(pRI, RIL_E_NO_MEMORY);
2360             return Void();
2361         }
2362
2363         RIL_DataProfileInfo **dataProfilePtrs =
2364             (RIL_DataProfileInfo **) calloc(num, sizeof(RIL_DataProfileInfo *));
2365         if (dataProfilePtrs == NULL) {
2366             RLOGE("Memory allocation failed for request %s",
2367                     requestToString(pRI->pCI->requestNumber));
2368             free(dataProfiles);
2369             sendErrorResponse(pRI, RIL_E_NO_MEMORY);
2370             return Void();
2371         }
2372
2373         for (size_t i = 0; i < num; i++) {
2374             dataProfilePtrs[i] = &dataProfiles[i];
2375
2376             success = copyHidlStringToRil(&dataProfiles[i].apn, profiles[i].apn, pRI);
2377
2378             const hidl_string &protocol =
2379                     (isRoaming ? profiles[i].roamingProtocol : profiles[i].protocol);
2380
2381             if (success && !copyHidlStringToRil(&dataProfiles[i].protocol, protocol, pRI)) {
2382                 success = false;
2383             }
2384
2385             if (success && !copyHidlStringToRil(&dataProfiles[i].user, profiles[i].user, pRI)) {
2386                 success = false;
2387             }
2388             if (success && !copyHidlStringToRil(&dataProfiles[i].password, profiles[i].password,
2389                     pRI)) {
2390                 success = false;
2391             }
2392
2393             if (!success) {
2394                 freeSetDataProfileData(num, dataProfiles, dataProfilePtrs, 4,
2395                     &RIL_DataProfileInfo::apn, &RIL_DataProfileInfo::protocol,
2396                     &RIL_DataProfileInfo::user, &RIL_DataProfileInfo::password);
2397                 return Void();
2398             }
2399
2400             dataProfiles[i].profileId = (RIL_DataProfile) profiles[i].profileId;
2401             dataProfiles[i].authType = (int) profiles[i].authType;
2402             dataProfiles[i].type = (int) profiles[i].type;
2403             dataProfiles[i].maxConnsTime = profiles[i].maxConnsTime;
2404             dataProfiles[i].maxConns = profiles[i].maxConns;
2405             dataProfiles[i].waitTime = profiles[i].waitTime;
2406             dataProfiles[i].enabled = BOOL_TO_INT(profiles[i].enabled);
2407         }
2408
2409         CALL_ONREQUEST(RIL_REQUEST_SET_DATA_PROFILE, dataProfilePtrs,
2410                 num * sizeof(RIL_DataProfileInfo *), pRI, mSlotId);
2411
2412         freeSetDataProfileData(num, dataProfiles, dataProfilePtrs, 4,
2413                 &RIL_DataProfileInfo::apn, &RIL_DataProfileInfo::protocol,
2414                 &RIL_DataProfileInfo::user, &RIL_DataProfileInfo::password);
2415     } else {
2416         RIL_DataProfileInfo_v15 *dataProfiles =
2417             (RIL_DataProfileInfo_v15 *) calloc(num, sizeof(RIL_DataProfileInfo_v15));
2418
2419         if (dataProfiles == NULL) {
2420             RLOGE("Memory allocation failed for request %s",
2421                     requestToString(pRI->pCI->requestNumber));
2422             sendErrorResponse(pRI, RIL_E_NO_MEMORY);
2423             return Void();
2424         }
2425
2426         RIL_DataProfileInfo_v15 **dataProfilePtrs =
2427             (RIL_DataProfileInfo_v15 **) calloc(num, sizeof(RIL_DataProfileInfo_v15 *));
2428         if (dataProfilePtrs == NULL) {
2429             RLOGE("Memory allocation failed for request %s",
2430                     requestToString(pRI->pCI->requestNumber));
2431             free(dataProfiles);
2432             sendErrorResponse(pRI, RIL_E_NO_MEMORY);
2433             return Void();
2434         }
2435
2436         for (size_t i = 0; i < num; i++) {
2437             dataProfilePtrs[i] = &dataProfiles[i];
2438
2439             success = copyHidlStringToRil(&dataProfiles[i].apn, profiles[i].apn, pRI);
2440             if (success && !copyHidlStringToRil(&dataProfiles[i].protocol, profiles[i].protocol,
2441                     pRI)) {
2442                 success = false;
2443             }
2444             if (success && !copyHidlStringToRil(&dataProfiles[i].roamingProtocol,
2445                     profiles[i].roamingProtocol, pRI)) {
2446                 success = false;
2447             }
2448             if (success && !copyHidlStringToRil(&dataProfiles[i].user, profiles[i].user, pRI)) {
2449                 success = false;
2450             }
2451             if (success && !copyHidlStringToRil(&dataProfiles[i].password, profiles[i].password,
2452                     pRI)) {
2453                 success = false;
2454             }
2455
2456             if (success && !copyHidlStringToRil(&dataProfiles[i].mvnoMatchData,
2457                     profiles[i].mvnoMatchData, pRI)) {
2458                 success = false;
2459             }
2460
2461             if (success && !convertMvnoTypeToString(profiles[i].mvnoType,
2462                     dataProfiles[i].mvnoType)) {
2463                 sendErrorResponse(pRI, RIL_E_INVALID_ARGUMENTS);
2464                 success = false;
2465             }
2466
2467             if (!success) {
2468                 freeSetDataProfileData(num, dataProfiles, dataProfilePtrs, 6,
2469                     &RIL_DataProfileInfo_v15::apn, &RIL_DataProfileInfo_v15::protocol,
2470                     &RIL_DataProfileInfo_v15::roamingProtocol, &RIL_DataProfileInfo_v15::user,
2471                     &RIL_DataProfileInfo_v15::password, &RIL_DataProfileInfo_v15::mvnoMatchData);
2472                 return Void();
2473             }
2474
2475             dataProfiles[i].profileId = (RIL_DataProfile) profiles[i].profileId;
2476             dataProfiles[i].authType = (int) profiles[i].authType;
2477             dataProfiles[i].type = (int) profiles[i].type;
2478             dataProfiles[i].maxConnsTime = profiles[i].maxConnsTime;
2479             dataProfiles[i].maxConns = profiles[i].maxConns;
2480             dataProfiles[i].waitTime = profiles[i].waitTime;
2481             dataProfiles[i].enabled = BOOL_TO_INT(profiles[i].enabled);
2482             dataProfiles[i].supportedTypesBitmask = profiles[i].supportedApnTypesBitmap;
2483             dataProfiles[i].bearerBitmask = profiles[i].bearerBitmap;
2484             dataProfiles[i].mtu = profiles[i].mtu;
2485         }
2486
2487         CALL_ONREQUEST(RIL_REQUEST_SET_DATA_PROFILE, dataProfilePtrs,
2488                 num * sizeof(RIL_DataProfileInfo_v15 *), pRI, mSlotId);
2489
2490         freeSetDataProfileData(num, dataProfiles, dataProfilePtrs, 6,
2491                 &RIL_DataProfileInfo_v15::apn, &RIL_DataProfileInfo_v15::protocol,
2492                 &RIL_DataProfileInfo_v15::roamingProtocol, &RIL_DataProfileInfo_v15::user,
2493                 &RIL_DataProfileInfo_v15::password, &RIL_DataProfileInfo_v15::mvnoMatchData);
2494     }
2495
2496     return Void();
2497 }
2498
2499 Return<void> RadioImpl::requestShutdown(int32_t serial) {
2500 #if VDBG
2501     RLOGD("requestShutdown: serial %d", serial);
2502 #endif
2503     dispatchVoid(serial, mSlotId, RIL_REQUEST_SHUTDOWN);
2504     return Void();
2505 }
2506
2507 Return<void> RadioImpl::getRadioCapability(int32_t serial) {
2508 #if VDBG
2509     RLOGD("getRadioCapability: serial %d", serial);
2510 #endif
2511     dispatchVoid(serial, mSlotId, RIL_REQUEST_GET_RADIO_CAPABILITY);
2512     return Void();
2513 }
2514
2515 Return<void> RadioImpl::setRadioCapability(int32_t serial, const RadioCapability& rc) {
2516 #if VDBG
2517     RLOGD("setRadioCapability: serial %d", serial);
2518 #endif
2519     RequestInfo *pRI = android::addRequestToList(serial, mSlotId, RIL_REQUEST_SET_RADIO_CAPABILITY);
2520     if (pRI == NULL) {
2521         return Void();
2522     }
2523
2524     RIL_RadioCapability rilRc = {};
2525
2526     // TODO : set rilRc.version using HIDL version ?
2527     rilRc.session = rc.session;
2528     rilRc.phase = (int) rc.phase;
2529     rilRc.rat = (int) rc.raf;
2530     rilRc.status = (int) rc.status;
2531     strncpy(rilRc.logicalModemUuid, rc.logicalModemUuid.c_str(), MAX_UUID_LENGTH);
2532
2533     CALL_ONREQUEST(pRI->pCI->requestNumber, &rilRc, sizeof(rilRc), pRI, mSlotId);
2534
2535     return Void();
2536 }
2537
2538 Return<void> RadioImpl::startLceService(int32_t serial, int32_t reportInterval, bool pullMode) {
2539 #if VDBG
2540     RLOGD("startLceService: serial %d", serial);
2541 #endif
2542     dispatchInts(serial, mSlotId, RIL_REQUEST_START_LCE, 2, reportInterval,
2543             BOOL_TO_INT(pullMode));
2544     return Void();
2545 }
2546
2547 Return<void> RadioImpl::stopLceService(int32_t serial) {
2548 #if VDBG
2549     RLOGD("stopLceService: serial %d", serial);
2550 #endif
2551     dispatchVoid(serial, mSlotId, RIL_REQUEST_STOP_LCE);
2552     return Void();
2553 }
2554
2555 Return<void> RadioImpl::pullLceData(int32_t serial) {
2556 #if VDBG
2557     RLOGD("pullLceData: serial %d", serial);
2558 #endif
2559     dispatchVoid(serial, mSlotId, RIL_REQUEST_PULL_LCEDATA);
2560     return Void();
2561 }
2562
2563 Return<void> RadioImpl::getModemActivityInfo(int32_t serial) {
2564 #if VDBG
2565     RLOGD("getModemActivityInfo: serial %d", serial);
2566 #endif
2567     dispatchVoid(serial, mSlotId, RIL_REQUEST_GET_ACTIVITY_INFO);
2568     return Void();
2569 }
2570
2571 Return<void> RadioImpl::setAllowedCarriers(int32_t serial, bool allAllowed,
2572                                            const CarrierRestrictions& carriers) {
2573 #if VDBG
2574     RLOGD("setAllowedCarriers: serial %d", serial);
2575 #endif
2576     RequestInfo *pRI = android::addRequestToList(serial, mSlotId,
2577             RIL_REQUEST_SET_CARRIER_RESTRICTIONS);
2578     if (pRI == NULL) {
2579         return Void();
2580     }
2581
2582     RIL_CarrierRestrictions cr = {};
2583     RIL_Carrier *allowedCarriers = NULL;
2584     RIL_Carrier *excludedCarriers = NULL;
2585
2586     cr.len_allowed_carriers = carriers.allowedCarriers.size();
2587     allowedCarriers = (RIL_Carrier *)calloc(cr.len_allowed_carriers, sizeof(RIL_Carrier));
2588     if (allowedCarriers == NULL) {
2589         RLOGE("setAllowedCarriers: Memory allocation failed for request %s",
2590                 requestToString(pRI->pCI->requestNumber));
2591         sendErrorResponse(pRI, RIL_E_NO_MEMORY);
2592         return Void();
2593     }
2594     cr.allowed_carriers = allowedCarriers;
2595
2596     cr.len_excluded_carriers = carriers.excludedCarriers.size();
2597     excludedCarriers = (RIL_Carrier *)calloc(cr.len_excluded_carriers, sizeof(RIL_Carrier));
2598     if (excludedCarriers == NULL) {
2599         RLOGE("setAllowedCarriers: Memory allocation failed for request %s",
2600                 requestToString(pRI->pCI->requestNumber));
2601         sendErrorResponse(pRI, RIL_E_NO_MEMORY);
2602 #ifdef MEMSET_FREED
2603         memset(allowedCarriers, 0, cr.len_allowed_carriers * sizeof(RIL_Carrier));
2604 #endif
2605         free(allowedCarriers);
2606         return Void();
2607     }
2608     cr.excluded_carriers = excludedCarriers;
2609
2610     for (int i = 0; i < cr.len_allowed_carriers; i++) {
2611         allowedCarriers[i].mcc = carriers.allowedCarriers[i].mcc.c_str();
2612         allowedCarriers[i].mnc = carriers.allowedCarriers[i].mnc.c_str();
2613         allowedCarriers[i].match_type = (RIL_CarrierMatchType) carriers.allowedCarriers[i].matchType;
2614         allowedCarriers[i].match_data = carriers.allowedCarriers[i].matchData.c_str();
2615     }
2616
2617     for (int i = 0; i < cr.len_excluded_carriers; i++) {
2618         excludedCarriers[i].mcc = carriers.excludedCarriers[i].mcc.c_str();
2619         excludedCarriers[i].mnc = carriers.excludedCarriers[i].mnc.c_str();
2620         excludedCarriers[i].match_type =
2621                 (RIL_CarrierMatchType) carriers.excludedCarriers[i].matchType;
2622         excludedCarriers[i].match_data = carriers.excludedCarriers[i].matchData.c_str();
2623     }
2624
2625     CALL_ONREQUEST(pRI->pCI->requestNumber, &cr, sizeof(RIL_CarrierRestrictions), pRI, mSlotId);
2626
2627 #ifdef MEMSET_FREED
2628     memset(allowedCarriers, 0, cr.len_allowed_carriers * sizeof(RIL_Carrier));
2629     memset(excludedCarriers, 0, cr.len_excluded_carriers * sizeof(RIL_Carrier));
2630 #endif
2631     free(allowedCarriers);
2632     free(excludedCarriers);
2633     return Void();
2634 }
2635
2636 Return<void> RadioImpl::getAllowedCarriers(int32_t serial) {
2637 #if VDBG
2638     RLOGD("getAllowedCarriers: serial %d", serial);
2639 #endif
2640     dispatchVoid(serial, mSlotId, RIL_REQUEST_GET_CARRIER_RESTRICTIONS);
2641     return Void();
2642 }
2643
2644 Return<void> RadioImpl::sendDeviceState(int32_t serial, DeviceStateType deviceStateType,
2645                                         bool state) {
2646 #if VDBG
2647     RLOGD("sendDeviceState: serial %d", serial);
2648 #endif
2649     if (s_vendorFunctions->version < 15) {
2650         if (deviceStateType ==  DeviceStateType::LOW_DATA_EXPECTED) {
2651             RLOGD("sendDeviceState: calling screen state %d", BOOL_TO_INT(!state));
2652             dispatchInts(serial, mSlotId, RIL_REQUEST_SCREEN_STATE, 1, BOOL_TO_INT(!state));
2653         } else {
2654             RequestInfo *pRI = android::addRequestToList(serial, mSlotId,
2655                     RIL_REQUEST_SEND_DEVICE_STATE);
2656             sendErrorResponse(pRI, RIL_E_REQUEST_NOT_SUPPORTED);
2657         }
2658         return Void();
2659     }
2660     dispatchInts(serial, mSlotId, RIL_REQUEST_SEND_DEVICE_STATE, 2, (int) deviceStateType,
2661             BOOL_TO_INT(state));
2662     return Void();
2663 }
2664
2665 Return<void> RadioImpl::setIndicationFilter(int32_t serial, int32_t indicationFilter) {
2666 #if VDBG
2667     RLOGD("setIndicationFilter: serial %d", serial);
2668 #endif
2669     if (s_vendorFunctions->version < 15) {
2670         RequestInfo *pRI = android::addRequestToList(serial, mSlotId,
2671                 RIL_REQUEST_SET_UNSOLICITED_RESPONSE_FILTER);
2672         sendErrorResponse(pRI, RIL_E_REQUEST_NOT_SUPPORTED);
2673         return Void();
2674     }
2675     dispatchInts(serial, mSlotId, RIL_REQUEST_SET_UNSOLICITED_RESPONSE_FILTER, 1, indicationFilter);
2676     return Void();
2677 }
2678
2679 Return<void> RadioImpl::setSimCardPower(int32_t serial, bool powerUp) {
2680 #if VDBG
2681     RLOGD("setSimCardPower: serial %d", serial);
2682 #endif
2683     dispatchInts(serial, mSlotId, RIL_REQUEST_SET_SIM_CARD_POWER, 1, BOOL_TO_INT(powerUp));
2684     return Void();
2685 }
2686
2687 Return<void> RadioImpl::responseAcknowledgement() {
2688     android::releaseWakeLock();
2689     return Void();
2690 }
2691
2692 Return<void> OemHookImpl::setResponseFunctions(
2693         const ::android::sp<IOemHookResponse>& oemHookResponseParam,
2694         const ::android::sp<IOemHookIndication>& oemHookIndicationParam) {
2695 #if VDBG
2696     RLOGD("OemHookImpl::setResponseFunctions");
2697 #endif
2698
2699     pthread_rwlock_t *radioServiceRwlockPtr = radio::getRadioServiceRwlock(mSlotId);
2700     int ret = pthread_rwlock_wrlock(radioServiceRwlockPtr);
2701     assert(ret == 0);
2702
2703     mOemHookResponse = oemHookResponseParam;
2704     mOemHookIndication = oemHookIndicationParam;
2705     mCounterOemHook[mSlotId]++;
2706
2707     ret = pthread_rwlock_unlock(radioServiceRwlockPtr);
2708     assert(ret == 0);
2709
2710     return Void();
2711 }
2712
2713 Return<void> OemHookImpl::sendRequestRaw(int32_t serial, const hidl_vec<uint8_t>& data) {
2714 #if VDBG
2715     RLOGD("OemHookImpl::sendRequestRaw: serial %d", serial);
2716 #endif
2717     dispatchRaw(serial, mSlotId, RIL_REQUEST_OEM_HOOK_RAW, data);
2718     return Void();
2719 }
2720
2721 Return<void> OemHookImpl::sendRequestStrings(int32_t serial,
2722         const hidl_vec<hidl_string>& data) {
2723 #if VDBG
2724     RLOGD("OemHookImpl::sendRequestStrings: serial %d", serial);
2725 #endif
2726     dispatchStrings(serial, mSlotId, RIL_REQUEST_OEM_HOOK_STRINGS, data);
2727     return Void();
2728 }
2729
2730 /***************************************************************************************************
2731  * RESPONSE FUNCTIONS
2732  * Functions above are used for requests going from framework to vendor code. The ones below are
2733  * responses for those requests coming back from the vendor code.
2734  **************************************************************************************************/
2735
2736 void radio::acknowledgeRequest(int slotId, int serial) {
2737     if (radioService[slotId]->mRadioResponse != NULL) {
2738         Return<void> retStatus = radioService[slotId]->mRadioResponse->acknowledgeRequest(serial);
2739         radioService[slotId]->checkReturnStatus(retStatus);
2740     } else {
2741         RLOGE("acknowledgeRequest: radioService[%d]->mRadioResponse == NULL", slotId);
2742     }
2743 }
2744
2745 void populateResponseInfo(RadioResponseInfo& responseInfo, int serial, int responseType,
2746                          RIL_Errno e) {
2747     responseInfo.serial = serial;
2748     switch (responseType) {
2749         case RESPONSE_SOLICITED:
2750             responseInfo.type = RadioResponseType::SOLICITED;
2751             break;
2752         case RESPONSE_SOLICITED_ACK_EXP:
2753             responseInfo.type = RadioResponseType::SOLICITED_ACK_EXP;
2754             break;
2755     }
2756     responseInfo.error = (RadioError) e;
2757 }
2758
2759 int responseIntOrEmpty(RadioResponseInfo& responseInfo, int serial, int responseType, RIL_Errno e,
2760                void *response, size_t responseLen) {
2761     populateResponseInfo(responseInfo, serial, responseType, e);
2762     int ret = -1;
2763
2764     if (response == NULL && responseLen == 0) {
2765         // Earlier RILs did not send a response for some cases although the interface
2766         // expected an integer as response. Do not return error if response is empty. Instead
2767         // Return -1 in those cases to maintain backward compatibility.
2768     } else if (response == NULL || responseLen != sizeof(int)) {
2769         RLOGE("responseIntOrEmpty: Invalid response");
2770         if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
2771     } else {
2772         int *p_int = (int *) response;
2773         ret = p_int[0];
2774     }
2775     return ret;
2776 }
2777
2778 int responseInt(RadioResponseInfo& responseInfo, int serial, int responseType, RIL_Errno e,
2779                void *response, size_t responseLen) {
2780     populateResponseInfo(responseInfo, serial, responseType, e);
2781     int ret = -1;
2782
2783     if (response == NULL || responseLen != sizeof(int)) {
2784         RLOGE("responseInt: Invalid response");
2785         if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
2786     } else {
2787         int *p_int = (int *) response;
2788         ret = p_int[0];
2789     }
2790     return ret;
2791 }
2792
2793 int radio::getIccCardStatusResponse(int slotId,
2794                                    int responseType, int serial, RIL_Errno e,
2795                                    void *response, size_t responseLen) {
2796     if (radioService[slotId]->mRadioResponse != NULL) {
2797         RadioResponseInfo responseInfo = {};
2798         populateResponseInfo(responseInfo, serial, responseType, e);
2799         CardStatus cardStatus = {};
2800         if (response == NULL || responseLen != sizeof(RIL_CardStatus_v6)) {
2801             RLOGE("getIccCardStatusResponse: Invalid response");
2802             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
2803         } else {
2804             RIL_CardStatus_v6 *p_cur = ((RIL_CardStatus_v6 *) response);
2805             cardStatus.cardState = (CardState) p_cur->card_state;
2806             cardStatus.universalPinState = (PinState) p_cur->universal_pin_state;
2807             cardStatus.gsmUmtsSubscriptionAppIndex = p_cur->gsm_umts_subscription_app_index;
2808             cardStatus.cdmaSubscriptionAppIndex = p_cur->cdma_subscription_app_index;
2809             cardStatus.imsSubscriptionAppIndex = p_cur->ims_subscription_app_index;
2810
2811             RIL_AppStatus *rilAppStatus = p_cur->applications;
2812             cardStatus.applications.resize(p_cur->num_applications);
2813             AppStatus *appStatus = cardStatus.applications.data();
2814 #if VDBG
2815             RLOGD("getIccCardStatusResponse: num_applications %d", p_cur->num_applications);
2816 #endif
2817             for (int i = 0; i < p_cur->num_applications; i++) {
2818                 appStatus[i].appType = (AppType) rilAppStatus[i].app_type;
2819                 appStatus[i].appState = (AppState) rilAppStatus[i].app_state;
2820                 appStatus[i].persoSubstate = (PersoSubstate) rilAppStatus[i].perso_substate;
2821                 appStatus[i].aidPtr = convertCharPtrToHidlString(rilAppStatus[i].aid_ptr);
2822                 appStatus[i].appLabelPtr = convertCharPtrToHidlString(
2823                         rilAppStatus[i].app_label_ptr);
2824                 appStatus[i].pin1Replaced = rilAppStatus[i].pin1_replaced;
2825                 appStatus[i].pin1 = (PinState) rilAppStatus[i].pin1;
2826                 appStatus[i].pin2 = (PinState) rilAppStatus[i].pin2;
2827             }
2828         }
2829
2830         Return<void> retStatus = radioService[slotId]->mRadioResponse->
2831                 getIccCardStatusResponse(responseInfo, cardStatus);
2832         radioService[slotId]->checkReturnStatus(retStatus);
2833     } else {
2834         RLOGE("getIccCardStatusResponse: radioService[%d]->mRadioResponse == NULL", slotId);
2835     }
2836
2837     return 0;
2838 }
2839
2840 int radio::supplyIccPinForAppResponse(int slotId,
2841                                      int responseType, int serial, RIL_Errno e,
2842                                      void *response, size_t responseLen) {
2843 #if VDBG
2844     RLOGD("supplyIccPinForAppResponse: serial %d", serial);
2845 #endif
2846
2847     if (radioService[slotId]->mRadioResponse != NULL) {
2848         RadioResponseInfo responseInfo = {};
2849         int ret = responseIntOrEmpty(responseInfo, serial, responseType, e, response, responseLen);
2850         Return<void> retStatus = radioService[slotId]->mRadioResponse->
2851                 supplyIccPinForAppResponse(responseInfo, ret);
2852         RLOGE("supplyIccPinForAppResponse: amit ret %d", ret);
2853         radioService[slotId]->checkReturnStatus(retStatus);
2854     } else {
2855         RLOGE("supplyIccPinForAppResponse: radioService[%d]->mRadioResponse == NULL",
2856                 slotId);
2857     }
2858
2859     return 0;
2860 }
2861
2862 int radio::supplyIccPukForAppResponse(int slotId,
2863                                      int responseType, int serial, RIL_Errno e,
2864                                      void *response, size_t responseLen) {
2865 #if VDBG
2866     RLOGD("supplyIccPukForAppResponse: serial %d", serial);
2867 #endif
2868
2869     if (radioService[slotId]->mRadioResponse != NULL) {
2870         RadioResponseInfo responseInfo = {};
2871         int ret = responseIntOrEmpty(responseInfo, serial, responseType, e, response, responseLen);
2872         Return<void> retStatus = radioService[slotId]->mRadioResponse->supplyIccPukForAppResponse(
2873                 responseInfo, ret);
2874         radioService[slotId]->checkReturnStatus(retStatus);
2875     } else {
2876         RLOGE("supplyIccPukForAppResponse: radioService[%d]->mRadioResponse == NULL",
2877                 slotId);
2878     }
2879
2880     return 0;
2881 }
2882
2883 int radio::supplyIccPin2ForAppResponse(int slotId,
2884                                       int responseType, int serial, RIL_Errno e,
2885                                       void *response, size_t responseLen) {
2886 #if VDBG
2887     RLOGD("supplyIccPin2ForAppResponse: serial %d", serial);
2888 #endif
2889
2890     if (radioService[slotId]->mRadioResponse != NULL) {
2891         RadioResponseInfo responseInfo = {};
2892         int ret = responseIntOrEmpty(responseInfo, serial, responseType, e, response, responseLen);
2893         Return<void> retStatus = radioService[slotId]->mRadioResponse->
2894                 supplyIccPin2ForAppResponse(responseInfo, ret);
2895         radioService[slotId]->checkReturnStatus(retStatus);
2896     } else {
2897         RLOGE("supplyIccPin2ForAppResponse: radioService[%d]->mRadioResponse == NULL",
2898                 slotId);
2899     }
2900
2901     return 0;
2902 }
2903
2904 int radio::supplyIccPuk2ForAppResponse(int slotId,
2905                                       int responseType, int serial, RIL_Errno e,
2906                                       void *response, size_t responseLen) {
2907 #if VDBG
2908     RLOGD("supplyIccPuk2ForAppResponse: serial %d", serial);
2909 #endif
2910
2911     if (radioService[slotId]->mRadioResponse != NULL) {
2912         RadioResponseInfo responseInfo = {};
2913         int ret = responseIntOrEmpty(responseInfo, serial, responseType, e, response, responseLen);
2914         Return<void> retStatus = radioService[slotId]->mRadioResponse->
2915                 supplyIccPuk2ForAppResponse(responseInfo, ret);
2916         radioService[slotId]->checkReturnStatus(retStatus);
2917     } else {
2918         RLOGE("supplyIccPuk2ForAppResponse: radioService[%d]->mRadioResponse == NULL",
2919                 slotId);
2920     }
2921
2922     return 0;
2923 }
2924
2925 int radio::changeIccPinForAppResponse(int slotId,
2926                                      int responseType, int serial, RIL_Errno e,
2927                                      void *response, size_t responseLen) {
2928 #if VDBG
2929     RLOGD("changeIccPinForAppResponse: serial %d", serial);
2930 #endif
2931
2932     if (radioService[slotId]->mRadioResponse != NULL) {
2933         RadioResponseInfo responseInfo = {};
2934         int ret = responseIntOrEmpty(responseInfo, serial, responseType, e, response, responseLen);
2935         Return<void> retStatus = radioService[slotId]->mRadioResponse->
2936                 changeIccPinForAppResponse(responseInfo, ret);
2937         radioService[slotId]->checkReturnStatus(retStatus);
2938     } else {
2939         RLOGE("changeIccPinForAppResponse: radioService[%d]->mRadioResponse == NULL",
2940                 slotId);
2941     }
2942
2943     return 0;
2944 }
2945
2946 int radio::changeIccPin2ForAppResponse(int slotId,
2947                                       int responseType, int serial, RIL_Errno e,
2948                                       void *response, size_t responseLen) {
2949 #if VDBG
2950     RLOGD("changeIccPin2ForAppResponse: serial %d", serial);
2951 #endif
2952
2953     if (radioService[slotId]->mRadioResponse != NULL) {
2954         RadioResponseInfo responseInfo = {};
2955         int ret = responseIntOrEmpty(responseInfo, serial, responseType, e, response, responseLen);
2956         Return<void> retStatus = radioService[slotId]->mRadioResponse->
2957                 changeIccPin2ForAppResponse(responseInfo, ret);
2958         radioService[slotId]->checkReturnStatus(retStatus);
2959     } else {
2960         RLOGE("changeIccPin2ForAppResponse: radioService[%d]->mRadioResponse == NULL",
2961                 slotId);
2962     }
2963
2964     return 0;
2965 }
2966
2967 int radio::supplyNetworkDepersonalizationResponse(int slotId,
2968                                                  int responseType, int serial, RIL_Errno e,
2969                                                  void *response, size_t responseLen) {
2970 #if VDBG
2971     RLOGD("supplyNetworkDepersonalizationResponse: serial %d", serial);
2972 #endif
2973
2974     if (radioService[slotId]->mRadioResponse != NULL) {
2975         RadioResponseInfo responseInfo = {};
2976         int ret = responseIntOrEmpty(responseInfo, serial, responseType, e, response, responseLen);
2977         Return<void> retStatus = radioService[slotId]->mRadioResponse->
2978                 supplyNetworkDepersonalizationResponse(responseInfo, ret);
2979         radioService[slotId]->checkReturnStatus(retStatus);
2980     } else {
2981         RLOGE("supplyNetworkDepersonalizationResponse: radioService[%d]->mRadioResponse == "
2982                 "NULL", slotId);
2983     }
2984
2985     return 0;
2986 }
2987
2988 int radio::getCurrentCallsResponse(int slotId,
2989                                   int responseType, int serial, RIL_Errno e,
2990                                   void *response, size_t responseLen) {
2991 #if VDBG
2992     RLOGD("getCurrentCallsResponse: serial %d", serial);
2993 #endif
2994
2995     if (radioService[slotId]->mRadioResponse != NULL) {
2996         RadioResponseInfo responseInfo = {};
2997         populateResponseInfo(responseInfo, serial, responseType, e);
2998
2999         hidl_vec<Call> calls;
3000         if ((response == NULL && responseLen != 0)
3001                 || (responseLen % sizeof(RIL_Call *)) != 0) {
3002             RLOGE("getCurrentCallsResponse: Invalid response");
3003             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3004         } else {
3005             int num = responseLen / sizeof(RIL_Call *);
3006             calls.resize(num);
3007
3008             for (int i = 0 ; i < num ; i++) {
3009                 RIL_Call *p_cur = ((RIL_Call **) response)[i];
3010                 /* each call info */
3011                 calls[i].state = (CallState) p_cur->state;
3012                 calls[i].index = p_cur->index;
3013                 calls[i].toa = p_cur->toa;
3014                 calls[i].isMpty = p_cur->isMpty;
3015                 calls[i].isMT = p_cur->isMT;
3016                 calls[i].als = p_cur->als;
3017                 calls[i].isVoice = p_cur->isVoice;
3018                 calls[i].isVoicePrivacy = p_cur->isVoicePrivacy;
3019                 calls[i].number = convertCharPtrToHidlString(p_cur->number);
3020                 calls[i].numberPresentation = (CallPresentation) p_cur->numberPresentation;
3021                 calls[i].name = convertCharPtrToHidlString(p_cur->name);
3022                 calls[i].namePresentation = (CallPresentation) p_cur->namePresentation;
3023                 if (p_cur->uusInfo != NULL && p_cur->uusInfo->uusData != NULL) {
3024                     RIL_UUS_Info *uusInfo = p_cur->uusInfo;
3025                     calls[i].uusInfo[0].uusType = (UusType) uusInfo->uusType;
3026                     calls[i].uusInfo[0].uusDcs = (UusDcs) uusInfo->uusDcs;
3027                     // convert uusInfo->uusData to a null-terminated string
3028                     char *nullTermStr = strndup(uusInfo->uusData, uusInfo->uusLength);
3029                     calls[i].uusInfo[0].uusData = nullTermStr;
3030                     free(nullTermStr);
3031                 }
3032             }
3033         }
3034
3035         Return<void> retStatus = radioService[slotId]->mRadioResponse->
3036                 getCurrentCallsResponse(responseInfo, calls);
3037         radioService[slotId]->checkReturnStatus(retStatus);
3038     } else {
3039         RLOGE("getCurrentCallsResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3040     }
3041
3042     return 0;
3043 }
3044
3045 int radio::dialResponse(int slotId,
3046                        int responseType, int serial, RIL_Errno e, void *response,
3047                        size_t responseLen) {
3048 #if VDBG
3049     RLOGD("dialResponse: serial %d", serial);
3050 #endif
3051
3052     if (radioService[slotId]->mRadioResponse != NULL) {
3053         RadioResponseInfo responseInfo = {};
3054         populateResponseInfo(responseInfo, serial, responseType, e);
3055         Return<void> retStatus = radioService[slotId]->mRadioResponse->dialResponse(responseInfo);
3056         radioService[slotId]->checkReturnStatus(retStatus);
3057     } else {
3058         RLOGE("dialResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3059     }
3060
3061     return 0;
3062 }
3063
3064 int radio::getIMSIForAppResponse(int slotId,
3065                                 int responseType, int serial, RIL_Errno e, void *response,
3066                                 size_t responseLen) {
3067 #if VDBG
3068     RLOGD("getIMSIForAppResponse: serial %d", serial);
3069 #endif
3070
3071     if (radioService[slotId]->mRadioResponse != NULL) {
3072         RadioResponseInfo responseInfo = {};
3073         populateResponseInfo(responseInfo, serial, responseType, e);
3074         Return<void> retStatus = radioService[slotId]->mRadioResponse->getIMSIForAppResponse(
3075                 responseInfo, convertCharPtrToHidlString((char *) response));
3076         radioService[slotId]->checkReturnStatus(retStatus);
3077     } else {
3078         RLOGE("getIMSIForAppResponse: radioService[%d]->mRadioResponse == NULL",
3079                 slotId);
3080     }
3081
3082     return 0;
3083 }
3084
3085 int radio::hangupConnectionResponse(int slotId,
3086                                    int responseType, int serial, RIL_Errno e,
3087                                    void *response, size_t responseLen) {
3088 #if VDBG
3089     RLOGD("hangupConnectionResponse: serial %d", serial);
3090 #endif
3091
3092     if (radioService[slotId]->mRadioResponse != NULL) {
3093         RadioResponseInfo responseInfo = {};
3094         populateResponseInfo(responseInfo, serial, responseType, e);
3095         Return<void> retStatus = radioService[slotId]->mRadioResponse->hangupConnectionResponse(
3096                 responseInfo);
3097         radioService[slotId]->checkReturnStatus(retStatus);
3098     } else {
3099         RLOGE("hangupConnectionResponse: radioService[%d]->mRadioResponse == NULL",
3100                 slotId);
3101     }
3102
3103     return 0;
3104 }
3105
3106 int radio::hangupWaitingOrBackgroundResponse(int slotId,
3107                                             int responseType, int serial, RIL_Errno e,
3108                                             void *response, size_t responseLen) {
3109 #if VDBG
3110     RLOGD("hangupWaitingOrBackgroundResponse: serial %d", serial);
3111 #endif
3112
3113     if (radioService[slotId]->mRadioResponse != NULL) {
3114         RadioResponseInfo responseInfo = {};
3115         populateResponseInfo(responseInfo, serial, responseType, e);
3116         Return<void> retStatus =
3117                 radioService[slotId]->mRadioResponse->hangupWaitingOrBackgroundResponse(
3118                 responseInfo);
3119         radioService[slotId]->checkReturnStatus(retStatus);
3120     } else {
3121         RLOGE("hangupWaitingOrBackgroundResponse: radioService[%d]->mRadioResponse == NULL",
3122                 slotId);
3123     }
3124
3125     return 0;
3126 }
3127
3128 int radio::hangupForegroundResumeBackgroundResponse(int slotId, int responseType, int serial,
3129                                                     RIL_Errno e, void *response,
3130                                                     size_t responseLen) {
3131 #if VDBG
3132     RLOGD("hangupWaitingOrBackgroundResponse: serial %d", serial);
3133 #endif
3134
3135     if (radioService[slotId]->mRadioResponse != NULL) {
3136         RadioResponseInfo responseInfo = {};
3137         populateResponseInfo(responseInfo, serial, responseType, e);
3138         Return<void> retStatus =
3139                 radioService[slotId]->mRadioResponse->hangupWaitingOrBackgroundResponse(
3140                 responseInfo);
3141         radioService[slotId]->checkReturnStatus(retStatus);
3142     } else {
3143         RLOGE("hangupWaitingOrBackgroundResponse: radioService[%d]->mRadioResponse == NULL",
3144                 slotId);
3145     }
3146
3147     return 0;
3148 }
3149
3150 int radio::switchWaitingOrHoldingAndActiveResponse(int slotId, int responseType, int serial,
3151                                                    RIL_Errno e, void *response,
3152                                                    size_t responseLen) {
3153 #if VDBG
3154     RLOGD("switchWaitingOrHoldingAndActiveResponse: serial %d", serial);
3155 #endif
3156
3157     if (radioService[slotId]->mRadioResponse != NULL) {
3158         RadioResponseInfo responseInfo = {};
3159         populateResponseInfo(responseInfo, serial, responseType, e);
3160         Return<void> retStatus =
3161                 radioService[slotId]->mRadioResponse->switchWaitingOrHoldingAndActiveResponse(
3162                 responseInfo);
3163         radioService[slotId]->checkReturnStatus(retStatus);
3164     } else {
3165         RLOGE("switchWaitingOrHoldingAndActiveResponse: radioService[%d]->mRadioResponse "
3166                 "== NULL", slotId);
3167     }
3168
3169     return 0;
3170 }
3171
3172 int radio::conferenceResponse(int slotId, int responseType,
3173                              int serial, RIL_Errno e, void *response, size_t responseLen) {
3174 #if VDBG
3175     RLOGD("conferenceResponse: serial %d", serial);
3176 #endif
3177
3178     if (radioService[slotId]->mRadioResponse != NULL) {
3179         RadioResponseInfo responseInfo = {};
3180         populateResponseInfo(responseInfo, serial, responseType, e);
3181         Return<void> retStatus = radioService[slotId]->mRadioResponse->conferenceResponse(
3182                 responseInfo);
3183         radioService[slotId]->checkReturnStatus(retStatus);
3184     } else {
3185         RLOGE("conferenceResponse: radioService[%d]->mRadioResponse == NULL",
3186                 slotId);
3187     }
3188
3189     return 0;
3190 }
3191
3192 int radio::rejectCallResponse(int slotId, int responseType,
3193                              int serial, RIL_Errno e, void *response, size_t responseLen) {
3194 #if VDBG
3195     RLOGD("rejectCallResponse: serial %d", serial);
3196 #endif
3197
3198     if (radioService[slotId]->mRadioResponse != NULL) {
3199         RadioResponseInfo responseInfo = {};
3200         populateResponseInfo(responseInfo, serial, responseType, e);
3201         Return<void> retStatus = radioService[slotId]->mRadioResponse->rejectCallResponse(
3202                 responseInfo);
3203         radioService[slotId]->checkReturnStatus(retStatus);
3204     } else {
3205         RLOGE("rejectCallResponse: radioService[%d]->mRadioResponse == NULL",
3206                 slotId);
3207     }
3208
3209     return 0;
3210 }
3211
3212 int radio::getLastCallFailCauseResponse(int slotId,
3213                                        int responseType, int serial, RIL_Errno e, void *response,
3214                                        size_t responseLen) {
3215 #if VDBG
3216     RLOGD("getLastCallFailCauseResponse: serial %d", serial);
3217 #endif
3218
3219     if (radioService[slotId]->mRadioResponse != NULL) {
3220         RadioResponseInfo responseInfo = {};
3221         populateResponseInfo(responseInfo, serial, responseType, e);
3222
3223         LastCallFailCauseInfo info = {};
3224         info.vendorCause = hidl_string();
3225         if (response == NULL) {
3226             RLOGE("getCurrentCallsResponse Invalid response: NULL");
3227             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3228         } else if (responseLen == sizeof(int)) {
3229             int *pInt = (int *) response;
3230             info.causeCode = (LastCallFailCause) pInt[0];
3231         } else if (responseLen == sizeof(RIL_LastCallFailCauseInfo))  {
3232             RIL_LastCallFailCauseInfo *pFailCauseInfo = (RIL_LastCallFailCauseInfo *) response;
3233             info.causeCode = (LastCallFailCause) pFailCauseInfo->cause_code;
3234             info.vendorCause = convertCharPtrToHidlString(pFailCauseInfo->vendor_cause);
3235         } else {
3236             RLOGE("getCurrentCallsResponse Invalid response: NULL");
3237             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3238         }
3239
3240         Return<void> retStatus = radioService[slotId]->mRadioResponse->getLastCallFailCauseResponse(
3241                 responseInfo, info);
3242         radioService[slotId]->checkReturnStatus(retStatus);
3243     } else {
3244         RLOGE("getLastCallFailCauseResponse: radioService[%d]->mRadioResponse == NULL",
3245                 slotId);
3246     }
3247
3248     return 0;
3249 }
3250
3251 int radio::getSignalStrengthResponse(int slotId,
3252                                      int responseType, int serial, RIL_Errno e,
3253                                      void *response, size_t responseLen) {
3254 #if VDBG
3255     RLOGD("getSignalStrengthResponse: serial %d", serial);
3256 #endif
3257
3258     if (radioService[slotId]->mRadioResponse != NULL) {
3259         RadioResponseInfo responseInfo = {};
3260         populateResponseInfo(responseInfo, serial, responseType, e);
3261         SignalStrength signalStrength = {};
3262         if (response == NULL || responseLen != sizeof(RIL_SignalStrength_v10)) {
3263             RLOGE("getSignalStrengthResponse: Invalid response");
3264             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3265         } else {
3266             convertRilSignalStrengthToHal(response, responseLen, signalStrength);
3267         }
3268
3269         Return<void> retStatus = radioService[slotId]->mRadioResponse->getSignalStrengthResponse(
3270                 responseInfo, signalStrength);
3271         radioService[slotId]->checkReturnStatus(retStatus);
3272     } else {
3273         RLOGE("getSignalStrengthResponse: radioService[%d]->mRadioResponse == NULL",
3274                 slotId);
3275     }
3276
3277     return 0;
3278 }
3279
3280 RIL_CellInfoType getCellInfoTypeRadioTechnology(char *rat) {
3281     if (rat == NULL) {
3282         return RIL_CELL_INFO_TYPE_NONE;
3283     }
3284
3285     int radioTech = atoi(rat);
3286
3287     switch(radioTech) {
3288
3289         case RADIO_TECH_GPRS:
3290         case RADIO_TECH_EDGE:
3291         case RADIO_TECH_GSM: {
3292             return RIL_CELL_INFO_TYPE_GSM;
3293         }
3294
3295         case RADIO_TECH_UMTS:
3296         case RADIO_TECH_HSDPA:
3297         case RADIO_TECH_HSUPA:
3298         case RADIO_TECH_HSPA:
3299         case RADIO_TECH_HSPAP: {
3300             return RIL_CELL_INFO_TYPE_WCDMA;
3301         }
3302
3303         case RADIO_TECH_IS95A:
3304         case RADIO_TECH_IS95B:
3305         case RADIO_TECH_1xRTT:
3306         case RADIO_TECH_EVDO_0:
3307         case RADIO_TECH_EVDO_A:
3308         case RADIO_TECH_EVDO_B:
3309         case RADIO_TECH_EHRPD: {
3310             return RIL_CELL_INFO_TYPE_CDMA;
3311         }
3312
3313         case RADIO_TECH_LTE:
3314         case RADIO_TECH_LTE_CA: {
3315             return RIL_CELL_INFO_TYPE_LTE;
3316         }
3317
3318         case RADIO_TECH_TD_SCDMA: {
3319             return RIL_CELL_INFO_TYPE_TD_SCDMA;
3320         }
3321
3322         default: {
3323             break;
3324         }
3325     }
3326
3327     return RIL_CELL_INFO_TYPE_NONE;
3328
3329 }
3330
3331 void fillCellIdentityResponse(CellIdentity &cellIdentity, RIL_CellIdentity_v16 &rilCellIdentity) {
3332
3333     cellIdentity.cellIdentityGsm.resize(0);
3334     cellIdentity.cellIdentityWcdma.resize(0);
3335     cellIdentity.cellIdentityCdma.resize(0);
3336     cellIdentity.cellIdentityTdscdma.resize(0);
3337     cellIdentity.cellIdentityLte.resize(0);
3338     cellIdentity.cellInfoType = (CellInfoType)rilCellIdentity.cellInfoType;
3339     switch(rilCellIdentity.cellInfoType) {
3340
3341         case RIL_CELL_INFO_TYPE_GSM: {
3342             cellIdentity.cellIdentityGsm.resize(1);
3343             cellIdentity.cellIdentityGsm[0].mcc =
3344                     std::to_string(rilCellIdentity.cellIdentityGsm.mcc);
3345             cellIdentity.cellIdentityGsm[0].mnc =
3346                     std::to_string(rilCellIdentity.cellIdentityGsm.mnc);
3347             cellIdentity.cellIdentityGsm[0].lac = rilCellIdentity.cellIdentityGsm.lac;
3348             cellIdentity.cellIdentityGsm[0].cid = rilCellIdentity.cellIdentityGsm.cid;
3349             cellIdentity.cellIdentityGsm[0].arfcn = rilCellIdentity.cellIdentityGsm.arfcn;
3350             cellIdentity.cellIdentityGsm[0].bsic = rilCellIdentity.cellIdentityGsm.bsic;
3351             break;
3352         }
3353
3354         case RIL_CELL_INFO_TYPE_WCDMA: {
3355             cellIdentity.cellIdentityWcdma.resize(1);
3356             cellIdentity.cellIdentityWcdma[0].mcc =
3357                     std::to_string(rilCellIdentity.cellIdentityWcdma.mcc);
3358             cellIdentity.cellIdentityWcdma[0].mnc =
3359                     std::to_string(rilCellIdentity.cellIdentityWcdma.mnc);
3360             cellIdentity.cellIdentityWcdma[0].lac = rilCellIdentity.cellIdentityWcdma.lac;
3361             cellIdentity.cellIdentityWcdma[0].cid = rilCellIdentity.cellIdentityWcdma.cid;
3362             cellIdentity.cellIdentityWcdma[0].psc = rilCellIdentity.cellIdentityWcdma.psc;
3363             cellIdentity.cellIdentityWcdma[0].uarfcn = rilCellIdentity.cellIdentityWcdma.uarfcn;
3364             break;
3365         }
3366
3367         case RIL_CELL_INFO_TYPE_CDMA: {
3368             cellIdentity.cellIdentityCdma.resize(1);
3369             cellIdentity.cellIdentityCdma[0].networkId = rilCellIdentity.cellIdentityCdma.networkId;
3370             cellIdentity.cellIdentityCdma[0].systemId = rilCellIdentity.cellIdentityCdma.systemId;
3371             cellIdentity.cellIdentityCdma[0].baseStationId =
3372                     rilCellIdentity.cellIdentityCdma.basestationId;
3373             cellIdentity.cellIdentityCdma[0].longitude = rilCellIdentity.cellIdentityCdma.longitude;
3374             cellIdentity.cellIdentityCdma[0].latitude = rilCellIdentity.cellIdentityCdma.latitude;
3375             break;
3376         }
3377
3378         case RIL_CELL_INFO_TYPE_LTE: {
3379             cellIdentity.cellIdentityLte.resize(1);
3380             cellIdentity.cellIdentityLte[0].mcc =
3381                     std::to_string(rilCellIdentity.cellIdentityLte.mcc);
3382             cellIdentity.cellIdentityLte[0].mnc =
3383                     std::to_string(rilCellIdentity.cellIdentityLte.mnc);
3384             cellIdentity.cellIdentityLte[0].ci = rilCellIdentity.cellIdentityLte.ci;
3385             cellIdentity.cellIdentityLte[0].pci = rilCellIdentity.cellIdentityLte.pci;
3386             cellIdentity.cellIdentityLte[0].tac = rilCellIdentity.cellIdentityLte.tac;
3387             cellIdentity.cellIdentityLte[0].earfcn = rilCellIdentity.cellIdentityLte.earfcn;
3388             break;
3389         }
3390
3391         case RIL_CELL_INFO_TYPE_TD_SCDMA: {
3392             cellIdentity.cellIdentityTdscdma.resize(1);
3393             cellIdentity.cellIdentityTdscdma[0].mcc =
3394                     std::to_string(rilCellIdentity.cellIdentityTdscdma.mcc);
3395             cellIdentity.cellIdentityTdscdma[0].mnc =
3396                     std::to_string(rilCellIdentity.cellIdentityTdscdma.mnc);
3397             cellIdentity.cellIdentityTdscdma[0].lac = rilCellIdentity.cellIdentityTdscdma.lac;
3398             cellIdentity.cellIdentityTdscdma[0].cid = rilCellIdentity.cellIdentityTdscdma.cid;
3399             cellIdentity.cellIdentityTdscdma[0].cpid = rilCellIdentity.cellIdentityTdscdma.cpid;
3400             break;
3401         }
3402
3403         default: {
3404             break;
3405         }
3406     }
3407 }
3408
3409 int convertResponseStringEntryToInt(char **response, int index, int numStrings) {
3410     if ((response != NULL) &&  (numStrings > index) && (response[index] != NULL)) {
3411         return atoi(response[index]);
3412     }
3413
3414     return -1;
3415 }
3416
3417 int convertResponseHexStringEntryToInt(char **response, int index, int numStrings) {
3418     const int hexBase = 16;
3419     if ((response != NULL) &&  (numStrings > index) && (response[index] != NULL)) {
3420         return strtol(response[index], NULL, hexBase);
3421     }
3422
3423     return -1;
3424 }
3425
3426 /* Fill Cell Identity info from Voice Registration State Response.
3427  * This fucntion is applicable only for RIL Version < 15.
3428  * Response is a  "char **".
3429  * First and Second entries are in hex string format
3430  * and rest are integers represented in ascii format. */
3431 void fillCellIdentityFromVoiceRegStateResponseString(CellIdentity &cellIdentity,
3432         int numStrings, char** response) {
3433
3434     RIL_CellIdentity_v16 rilCellIdentity;
3435     memset(&rilCellIdentity, -1, sizeof(RIL_CellIdentity_v16));
3436
3437     rilCellIdentity.cellInfoType = getCellInfoTypeRadioTechnology(response[3]);
3438     switch(rilCellIdentity.cellInfoType) {
3439
3440         case RIL_CELL_INFO_TYPE_GSM: {
3441             /* valid LAC are hexstrings in the range 0x0000 - 0xffff */
3442             rilCellIdentity.cellIdentityGsm.lac =
3443                     convertResponseHexStringEntryToInt(response, 1, numStrings);
3444
3445             /* valid CID are hexstrings in the range 0x00000000 - 0xffffffff */
3446             rilCellIdentity.cellIdentityGsm.cid =
3447                     convertResponseHexStringEntryToInt(response, 2, numStrings);
3448             break;
3449         }
3450
3451         case RIL_CELL_INFO_TYPE_WCDMA: {
3452             /* valid LAC are hexstrings in the range 0x0000 - 0xffff */
3453             rilCellIdentity.cellIdentityWcdma.lac =
3454                     convertResponseHexStringEntryToInt(response, 1, numStrings);
3455
3456             /* valid CID are hexstrings in the range 0x00000000 - 0xffffffff */
3457             rilCellIdentity.cellIdentityWcdma.cid =
3458                     convertResponseHexStringEntryToInt(response, 2, numStrings);
3459             rilCellIdentity.cellIdentityWcdma.psc =
3460                     convertResponseStringEntryToInt(response, 14, numStrings);
3461             break;
3462         }
3463
3464         case RIL_CELL_INFO_TYPE_TD_SCDMA:{
3465             /* valid LAC are hexstrings in the range 0x0000 - 0xffff */
3466             rilCellIdentity.cellIdentityTdscdma.lac =
3467                     convertResponseHexStringEntryToInt(response, 1, numStrings);
3468
3469             /* valid CID are hexstrings in the range 0x00000000 - 0xffffffff */
3470             rilCellIdentity.cellIdentityTdscdma.cid =
3471                     convertResponseHexStringEntryToInt(response, 2, numStrings);
3472             break;
3473         }
3474
3475         case RIL_CELL_INFO_TYPE_CDMA:{
3476             rilCellIdentity.cellIdentityCdma.basestationId =
3477                     convertResponseStringEntryToInt(response, 4, numStrings);
3478             rilCellIdentity.cellIdentityCdma.longitude =
3479                     convertResponseStringEntryToInt(response, 5, numStrings);
3480             rilCellIdentity.cellIdentityCdma.latitude =
3481                     convertResponseStringEntryToInt(response, 6, numStrings);
3482             rilCellIdentity.cellIdentityCdma.systemId =
3483                     convertResponseStringEntryToInt(response, 8, numStrings);
3484             rilCellIdentity.cellIdentityCdma.networkId =
3485                     convertResponseStringEntryToInt(response, 9, numStrings);
3486             break;
3487         }
3488
3489         case RIL_CELL_INFO_TYPE_LTE:{
3490             /* valid TAC are hexstrings in the range 0x0000 - 0xffff */
3491             rilCellIdentity.cellIdentityLte.tac =
3492                     convertResponseHexStringEntryToInt(response, 1, numStrings);
3493
3494             /* valid CID are hexstrings in the range 0x00000000 - 0xffffffff */
3495             rilCellIdentity.cellIdentityLte.ci =
3496                     convertResponseHexStringEntryToInt(response, 2, numStrings);
3497             break;
3498         }
3499
3500         default: {
3501             break;
3502         }
3503     }
3504
3505     fillCellIdentityResponse(cellIdentity, rilCellIdentity);
3506 }
3507
3508 /* Fill Cell Identity info from Data Registration State Response.
3509  * This fucntion is applicable only for RIL Version < 15.
3510  * Response is a  "char **".
3511  * First and Second entries are in hex string format
3512  * and rest are integers represented in ascii format. */
3513 void fillCellIdentityFromDataRegStateResponseString(CellIdentity &cellIdentity,
3514         int numStrings, char** response) {
3515
3516     RIL_CellIdentity_v16 rilCellIdentity;
3517     memset(&rilCellIdentity, -1, sizeof(RIL_CellIdentity_v16));
3518
3519     rilCellIdentity.cellInfoType = getCellInfoTypeRadioTechnology(response[3]);
3520     switch(rilCellIdentity.cellInfoType) {
3521         case RIL_CELL_INFO_TYPE_GSM: {
3522             /* valid LAC are hexstrings in the range 0x0000 - 0xffff */
3523             rilCellIdentity.cellIdentityGsm.lac =
3524                     convertResponseHexStringEntryToInt(response, 1, numStrings);
3525
3526             /* valid CID are hexstrings in the range 0x00000000 - 0xffffffff */
3527             rilCellIdentity.cellIdentityGsm.cid =
3528                     convertResponseHexStringEntryToInt(response, 2, numStrings);
3529             break;
3530         }
3531         case RIL_CELL_INFO_TYPE_WCDMA: {
3532             /* valid LAC are hexstrings in the range 0x0000 - 0xffff */
3533             rilCellIdentity.cellIdentityWcdma.lac =
3534                     convertResponseHexStringEntryToInt(response, 1, numStrings);
3535
3536             /* valid CID are hexstrings in the range 0x00000000 - 0xffffffff */
3537             rilCellIdentity.cellIdentityWcdma.cid =
3538                     convertResponseHexStringEntryToInt(response, 2, numStrings);
3539             break;
3540         }
3541         case RIL_CELL_INFO_TYPE_TD_SCDMA:{
3542             /* valid LAC are hexstrings in the range 0x0000 - 0xffff */
3543             rilCellIdentity.cellIdentityTdscdma.lac =
3544                     convertResponseHexStringEntryToInt(response, 1, numStrings);
3545
3546             /* valid CID are hexstrings in the range 0x00000000 - 0xffffffff */
3547             rilCellIdentity.cellIdentityTdscdma.cid =
3548                     convertResponseHexStringEntryToInt(response, 2, numStrings);
3549             break;
3550         }
3551         case RIL_CELL_INFO_TYPE_LTE: {
3552             rilCellIdentity.cellIdentityLte.tac =
3553                     convertResponseStringEntryToInt(response, 6, numStrings);
3554             rilCellIdentity.cellIdentityLte.pci =
3555                     convertResponseStringEntryToInt(response, 7, numStrings);
3556             rilCellIdentity.cellIdentityLte.ci =
3557                     convertResponseStringEntryToInt(response, 8, numStrings);
3558             break;
3559         }
3560         default: {
3561             break;
3562         }
3563     }
3564
3565     fillCellIdentityResponse(cellIdentity, rilCellIdentity);
3566 }
3567
3568 int radio::getVoiceRegistrationStateResponse(int slotId,
3569                                             int responseType, int serial, RIL_Errno e,
3570                                             void *response, size_t responseLen) {
3571 #if VDBG
3572     RLOGD("getVoiceRegistrationStateResponse: serial %d", serial);
3573 #endif
3574
3575     if (radioService[slotId]->mRadioResponse != NULL) {
3576         RadioResponseInfo responseInfo = {};
3577         populateResponseInfo(responseInfo, serial, responseType, e);
3578
3579         VoiceRegStateResult voiceRegResponse = {};
3580         int numStrings = responseLen / sizeof(char *);
3581         if (response == NULL) {
3582                RLOGE("getVoiceRegistrationStateResponse Invalid response: NULL");
3583                if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3584         } else if (s_vendorFunctions->version <= 14) {
3585             if (numStrings != 15) {
3586                 RLOGE("getVoiceRegistrationStateResponse Invalid response: NULL");
3587                 if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3588             } else {
3589                 char **resp = (char **) response;
3590                 voiceRegResponse.regState = (RegState) ATOI_NULL_HANDLED_DEF(resp[0], 4);
3591                 voiceRegResponse.rat = ATOI_NULL_HANDLED(resp[3]);
3592                 voiceRegResponse.cssSupported = ATOI_NULL_HANDLED_DEF(resp[7], 0);
3593                 voiceRegResponse.roamingIndicator = ATOI_NULL_HANDLED(resp[10]);
3594                 voiceRegResponse.systemIsInPrl = ATOI_NULL_HANDLED_DEF(resp[11], 0);
3595                 voiceRegResponse.defaultRoamingIndicator = ATOI_NULL_HANDLED_DEF(resp[12], 0);
3596                 voiceRegResponse.reasonForDenial = ATOI_NULL_HANDLED_DEF(resp[13], 0);
3597                 fillCellIdentityFromVoiceRegStateResponseString(voiceRegResponse.cellIdentity,
3598                         numStrings, resp);
3599             }
3600         } else {
3601             RIL_VoiceRegistrationStateResponse *voiceRegState =
3602                     (RIL_VoiceRegistrationStateResponse *)response;
3603
3604             if (responseLen != sizeof(RIL_VoiceRegistrationStateResponse)) {
3605                 RLOGE("getVoiceRegistrationStateResponse Invalid response: NULL");
3606                 if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3607             } else {
3608                 voiceRegResponse.regState = (RegState) voiceRegState->regState;
3609                 voiceRegResponse.rat = voiceRegState->rat;;
3610                 voiceRegResponse.cssSupported = voiceRegState->cssSupported;
3611                 voiceRegResponse.roamingIndicator = voiceRegState->roamingIndicator;
3612                 voiceRegResponse.systemIsInPrl = voiceRegState->systemIsInPrl;
3613                 voiceRegResponse.defaultRoamingIndicator = voiceRegState->defaultRoamingIndicator;
3614                 voiceRegResponse.reasonForDenial = voiceRegState->reasonForDenial;
3615                 fillCellIdentityResponse(voiceRegResponse.cellIdentity,
3616                         voiceRegState->cellIdentity);
3617             }
3618         }
3619
3620         Return<void> retStatus =
3621                 radioService[slotId]->mRadioResponse->getVoiceRegistrationStateResponse(
3622                 responseInfo, voiceRegResponse);
3623         radioService[slotId]->checkReturnStatus(retStatus);
3624     } else {
3625         RLOGE("getVoiceRegistrationStateResponse: radioService[%d]->mRadioResponse == NULL",
3626                 slotId);
3627     }
3628
3629     return 0;
3630 }
3631
3632 int radio::getDataRegistrationStateResponse(int slotId,
3633                                            int responseType, int serial, RIL_Errno e,
3634                                            void *response, size_t responseLen) {
3635 #if VDBG
3636     RLOGD("getDataRegistrationStateResponse: serial %d", serial);
3637 #endif
3638
3639     if (radioService[slotId]->mRadioResponse != NULL) {
3640         RadioResponseInfo responseInfo = {};
3641         populateResponseInfo(responseInfo, serial, responseType, e);
3642         DataRegStateResult dataRegResponse = {};
3643         if (response == NULL) {
3644             RLOGE("getDataRegistrationStateResponse Invalid response: NULL");
3645             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3646         } else if (s_vendorFunctions->version <= 14) {
3647             int numStrings = responseLen / sizeof(char *);
3648             if ((numStrings != 6) && (numStrings != 11)) {
3649                 RLOGE("getDataRegistrationStateResponse Invalid response: NULL");
3650                 if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3651             } else {
3652                 char **resp = (char **) response;
3653                 dataRegResponse.regState = (RegState) ATOI_NULL_HANDLED_DEF(resp[0], 4);
3654                 dataRegResponse.rat =  ATOI_NULL_HANDLED_DEF(resp[3], 0);
3655                 dataRegResponse.reasonDataDenied =  ATOI_NULL_HANDLED(resp[4]);
3656                 dataRegResponse.maxDataCalls =  ATOI_NULL_HANDLED_DEF(resp[5], 1);
3657                 fillCellIdentityFromDataRegStateResponseString(dataRegResponse.cellIdentity,
3658                         numStrings, resp);
3659             }
3660         } else {
3661             RIL_DataRegistrationStateResponse *dataRegState =
3662                     (RIL_DataRegistrationStateResponse *)response;
3663
3664             if (responseLen != sizeof(RIL_DataRegistrationStateResponse)) {
3665                 RLOGE("getDataRegistrationStateResponse Invalid response: NULL");
3666                 if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3667             } else {
3668                 dataRegResponse.regState = (RegState) dataRegState->regState;
3669                 dataRegResponse.rat = dataRegState->rat;;
3670                 dataRegResponse.reasonDataDenied = dataRegState->reasonDataDenied;
3671                 dataRegResponse.maxDataCalls = dataRegState->maxDataCalls;
3672                 fillCellIdentityResponse(dataRegResponse.cellIdentity, dataRegState->cellIdentity);
3673             }
3674         }
3675
3676         Return<void> retStatus =
3677                 radioService[slotId]->mRadioResponse->getDataRegistrationStateResponse(responseInfo,
3678                 dataRegResponse);
3679         radioService[slotId]->checkReturnStatus(retStatus);
3680     } else {
3681         RLOGE("getDataRegistrationStateResponse: radioService[%d]->mRadioResponse == NULL",
3682                 slotId);
3683     }
3684
3685     return 0;
3686 }
3687
3688 int radio::getOperatorResponse(int slotId,
3689                               int responseType, int serial, RIL_Errno e, void *response,
3690                               size_t responseLen) {
3691 #if VDBG
3692     RLOGD("getOperatorResponse: serial %d", serial);
3693 #endif
3694
3695     if (radioService[slotId]->mRadioResponse != NULL) {
3696         RadioResponseInfo responseInfo = {};
3697         populateResponseInfo(responseInfo, serial, responseType, e);
3698         hidl_string longName;
3699         hidl_string shortName;
3700         hidl_string numeric;
3701         int numStrings = responseLen / sizeof(char *);
3702         if (response == NULL || numStrings != 3) {
3703             RLOGE("getOperatorResponse Invalid response: NULL");
3704             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3705
3706         } else {
3707             char **resp = (char **) response;
3708             longName = convertCharPtrToHidlString(resp[0]);
3709             shortName = convertCharPtrToHidlString(resp[1]);
3710             numeric = convertCharPtrToHidlString(resp[2]);
3711         }
3712         Return<void> retStatus = radioService[slotId]->mRadioResponse->getOperatorResponse(
3713                 responseInfo, longName, shortName, numeric);
3714         radioService[slotId]->checkReturnStatus(retStatus);
3715     } else {
3716         RLOGE("getOperatorResponse: radioService[%d]->mRadioResponse == NULL",
3717                 slotId);
3718     }
3719
3720     return 0;
3721 }
3722
3723 int radio::setRadioPowerResponse(int slotId,
3724                                 int responseType, int serial, RIL_Errno e, void *response,
3725                                 size_t responseLen) {
3726     RLOGD("setRadioPowerResponse: serial %d", serial);
3727
3728     if (radioService[slotId]->mRadioResponse != NULL) {
3729         RadioResponseInfo responseInfo = {};
3730         populateResponseInfo(responseInfo, serial, responseType, e);
3731         Return<void> retStatus = radioService[slotId]->mRadioResponse->setRadioPowerResponse(
3732                 responseInfo);
3733         radioService[slotId]->checkReturnStatus(retStatus);
3734     } else {
3735         RLOGE("setRadioPowerResponse: radioService[%d]->mRadioResponse == NULL",
3736                 slotId);
3737     }
3738
3739     return 0;
3740 }
3741
3742 int radio::sendDtmfResponse(int slotId,
3743                            int responseType, int serial, RIL_Errno e, void *response,
3744                            size_t responseLen) {
3745 #if VDBG
3746     RLOGD("sendDtmfResponse: serial %d", serial);
3747 #endif
3748
3749     if (radioService[slotId]->mRadioResponse != NULL) {
3750         RadioResponseInfo responseInfo = {};
3751         populateResponseInfo(responseInfo, serial, responseType, e);
3752         Return<void> retStatus = radioService[slotId]->mRadioResponse->sendDtmfResponse(
3753                 responseInfo);
3754         radioService[slotId]->checkReturnStatus(retStatus);
3755     } else {
3756         RLOGE("sendDtmfResponse: radioService[%d]->mRadioResponse == NULL",
3757                 slotId);
3758     }
3759
3760     return 0;
3761 }
3762
3763 SendSmsResult makeSendSmsResult(RadioResponseInfo& responseInfo, int serial, int responseType,
3764                                 RIL_Errno e, void *response, size_t responseLen) {
3765     populateResponseInfo(responseInfo, serial, responseType, e);
3766     SendSmsResult result = {};
3767
3768     if (response == NULL || responseLen != sizeof(RIL_SMS_Response)) {
3769         RLOGE("Invalid response: NULL");
3770         if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3771         result.ackPDU = hidl_string();
3772     } else {
3773         RIL_SMS_Response *resp = (RIL_SMS_Response *) response;
3774         result.messageRef = resp->messageRef;
3775         result.ackPDU = convertCharPtrToHidlString(resp->ackPDU);
3776         result.errorCode = resp->errorCode;
3777     }
3778     return result;
3779 }
3780
3781 int radio::sendSmsResponse(int slotId,
3782                           int responseType, int serial, RIL_Errno e, void *response,
3783                           size_t responseLen) {
3784 #if VDBG
3785     RLOGD("sendSmsResponse: serial %d", serial);
3786 #endif
3787
3788     if (radioService[slotId]->mRadioResponse != NULL) {
3789         RadioResponseInfo responseInfo = {};
3790         SendSmsResult result = makeSendSmsResult(responseInfo, serial, responseType, e, response,
3791                 responseLen);
3792
3793         Return<void> retStatus = radioService[slotId]->mRadioResponse->sendSmsResponse(responseInfo,
3794                 result);
3795         radioService[slotId]->checkReturnStatus(retStatus);
3796     } else {
3797         RLOGE("sendSmsResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3798     }
3799
3800     return 0;
3801 }
3802
3803 int radio::sendSMSExpectMoreResponse(int slotId,
3804                                     int responseType, int serial, RIL_Errno e, void *response,
3805                                     size_t responseLen) {
3806 #if VDBG
3807     RLOGD("sendSMSExpectMoreResponse: serial %d", serial);
3808 #endif
3809
3810     if (radioService[slotId]->mRadioResponse != NULL) {
3811         RadioResponseInfo responseInfo = {};
3812         SendSmsResult result = makeSendSmsResult(responseInfo, serial, responseType, e, response,
3813                 responseLen);
3814
3815         Return<void> retStatus = radioService[slotId]->mRadioResponse->sendSMSExpectMoreResponse(
3816                 responseInfo, result);
3817         radioService[slotId]->checkReturnStatus(retStatus);
3818     } else {
3819         RLOGE("sendSMSExpectMoreResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3820     }
3821
3822     return 0;
3823 }
3824
3825 int radio::setupDataCallResponse(int slotId,
3826                                  int responseType, int serial, RIL_Errno e, void *response,
3827                                  size_t responseLen) {
3828 #if VDBG
3829     RLOGD("setupDataCallResponse: serial %d", serial);
3830 #endif
3831
3832     if (radioService[slotId]->mRadioResponse != NULL) {
3833         RadioResponseInfo responseInfo = {};
3834         populateResponseInfo(responseInfo, serial, responseType, e);
3835
3836         SetupDataCallResult result = {};
3837         if (response == NULL || (responseLen % sizeof(RIL_Data_Call_Response_v11)) != 0) {
3838             if (response != NULL) {
3839                 RLOGE("setupDataCallResponse: Invalid response");
3840                 if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3841             }
3842             result.status = DataCallFailCause::ERROR_UNSPECIFIED;
3843             result.type = hidl_string();
3844             result.ifname = hidl_string();
3845             result.addresses = hidl_string();
3846             result.dnses = hidl_string();
3847             result.gateways = hidl_string();
3848             result.pcscf = hidl_string();
3849         } else {
3850             convertRilDataCallToHal((RIL_Data_Call_Response_v11 *) response, result);
3851         }
3852
3853         Return<void> retStatus = radioService[slotId]->mRadioResponse->setupDataCallResponse(
3854                 responseInfo, result);
3855         radioService[slotId]->checkReturnStatus(retStatus);
3856     } else {
3857         RLOGE("setupDataCallResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3858     }
3859
3860     return 0;
3861 }
3862
3863 IccIoResult responseIccIo(RadioResponseInfo& responseInfo, int serial, int responseType,
3864                            RIL_Errno e, void *response, size_t responseLen) {
3865     populateResponseInfo(responseInfo, serial, responseType, e);
3866     IccIoResult result = {};
3867
3868     if (response == NULL || responseLen != sizeof(RIL_SIM_IO_Response)) {
3869         RLOGE("Invalid response: NULL");
3870         if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3871         result.simResponse = hidl_string();
3872     } else {
3873         RIL_SIM_IO_Response *resp = (RIL_SIM_IO_Response *) response;
3874         result.sw1 = resp->sw1;
3875         result.sw2 = resp->sw2;
3876         result.simResponse = convertCharPtrToHidlString(resp->simResponse);
3877     }
3878     return result;
3879 }
3880
3881 int radio::iccIOForAppResponse(int slotId,
3882                       int responseType, int serial, RIL_Errno e, void *response,
3883                       size_t responseLen) {
3884 #if VDBG
3885     RLOGD("iccIOForAppResponse: serial %d", serial);
3886 #endif
3887
3888     if (radioService[slotId]->mRadioResponse != NULL) {
3889         RadioResponseInfo responseInfo = {};
3890         IccIoResult result = responseIccIo(responseInfo, serial, responseType, e, response,
3891                 responseLen);
3892
3893         Return<void> retStatus = radioService[slotId]->mRadioResponse->iccIOForAppResponse(
3894                 responseInfo, result);
3895         radioService[slotId]->checkReturnStatus(retStatus);
3896     } else {
3897         RLOGE("iccIOForAppResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3898     }
3899
3900     return 0;
3901 }
3902
3903 int radio::sendUssdResponse(int slotId,
3904                            int responseType, int serial, RIL_Errno e, void *response,
3905                            size_t responseLen) {
3906 #if VDBG
3907     RLOGD("sendUssdResponse: serial %d", serial);
3908 #endif
3909
3910     if (radioService[slotId]->mRadioResponse != NULL) {
3911         RadioResponseInfo responseInfo = {};
3912         populateResponseInfo(responseInfo, serial, responseType, e);
3913         Return<void> retStatus = radioService[slotId]->mRadioResponse->sendUssdResponse(
3914                 responseInfo);
3915         radioService[slotId]->checkReturnStatus(retStatus);
3916     } else {
3917         RLOGE("sendUssdResponse: radioService[%d]->mRadioResponse == NULL",
3918                 slotId);
3919     }
3920
3921     return 0;
3922 }
3923
3924 int radio::cancelPendingUssdResponse(int slotId,
3925                                     int responseType, int serial, RIL_Errno e, void *response,
3926                                     size_t responseLen) {
3927 #if VDBG
3928     RLOGD("cancelPendingUssdResponse: serial %d", serial);
3929 #endif
3930
3931     if (radioService[slotId]->mRadioResponse != NULL) {
3932         RadioResponseInfo responseInfo = {};
3933         populateResponseInfo(responseInfo, serial, responseType, e);
3934         Return<void> retStatus = radioService[slotId]->mRadioResponse->cancelPendingUssdResponse(
3935                 responseInfo);
3936         radioService[slotId]->checkReturnStatus(retStatus);
3937     } else {
3938         RLOGE("cancelPendingUssdResponse: radioService[%d]->mRadioResponse == NULL",
3939                 slotId);
3940     }
3941
3942     return 0;
3943 }
3944
3945 int radio::getClirResponse(int slotId,
3946                               int responseType, int serial, RIL_Errno e, void *response,
3947                               size_t responseLen) {
3948 #if VDBG
3949     RLOGD("getClirResponse: serial %d", serial);
3950 #endif
3951
3952     if (radioService[slotId]->mRadioResponse != NULL) {
3953         RadioResponseInfo responseInfo = {};
3954         populateResponseInfo(responseInfo, serial, responseType, e);
3955         int n = -1, m = -1;
3956         int numInts = responseLen / sizeof(int);
3957         if (response == NULL || numInts != 2) {
3958             RLOGE("getClirResponse Invalid response: NULL");
3959             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3960         } else {
3961             int *pInt = (int *) response;
3962             n = pInt[0];
3963             m = pInt[1];
3964         }
3965         Return<void> retStatus = radioService[slotId]->mRadioResponse->getClirResponse(responseInfo,
3966                 n, m);
3967         radioService[slotId]->checkReturnStatus(retStatus);
3968     } else {
3969         RLOGE("getClirResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3970     }
3971
3972     return 0;
3973 }
3974
3975 int radio::setClirResponse(int slotId,
3976                           int responseType, int serial, RIL_Errno e, void *response,
3977                           size_t responseLen) {
3978 #if VDBG
3979     RLOGD("setClirResponse: serial %d", serial);
3980 #endif
3981
3982     if (radioService[slotId]->mRadioResponse != NULL) {
3983         RadioResponseInfo responseInfo = {};
3984         populateResponseInfo(responseInfo, serial, responseType, e);
3985         Return<void> retStatus = radioService[slotId]->mRadioResponse->setClirResponse(
3986                 responseInfo);
3987         radioService[slotId]->checkReturnStatus(retStatus);
3988     } else {
3989         RLOGE("setClirResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3990     }
3991
3992     return 0;
3993 }
3994
3995 int radio::getCallForwardStatusResponse(int slotId,
3996                                        int responseType, int serial, RIL_Errno e,
3997                                        void *response, size_t responseLen) {
3998 #if VDBG
3999     RLOGD("getCallForwardStatusResponse: serial %d", serial);
4000 #endif
4001
4002     if (radioService[slotId]->mRadioResponse != NULL) {
4003         RadioResponseInfo responseInfo = {};
4004         populateResponseInfo(responseInfo, serial, responseType, e);
4005         hidl_vec<CallForwardInfo> callForwardInfos;
4006
4007         if ((response == NULL && responseLen != 0)
4008                 || responseLen % sizeof(RIL_CallForwardInfo *) != 0) {
4009             RLOGE("getCallForwardStatusResponse Invalid response: NULL");
4010             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4011         } else {
4012             int num = responseLen / sizeof(RIL_CallForwardInfo *);
4013             callForwardInfos.resize(num);
4014             for (int i = 0 ; i < num; i++) {
4015                 RIL_CallForwardInfo *resp = ((RIL_CallForwardInfo **) response)[i];
4016                 callForwardInfos[i].status = (CallForwardInfoStatus) resp->status;
4017                 callForwardInfos[i].reason = resp->reason;
4018                 callForwardInfos[i].serviceClass = resp->serviceClass;
4019                 callForwardInfos[i].toa = resp->toa;
4020                 callForwardInfos[i].number = convertCharPtrToHidlString(resp->number);
4021                 callForwardInfos[i].timeSeconds = resp->timeSeconds;
4022             }
4023         }
4024
4025         Return<void> retStatus = radioService[slotId]->mRadioResponse->getCallForwardStatusResponse(
4026                 responseInfo, callForwardInfos);
4027         radioService[slotId]->checkReturnStatus(retStatus);
4028     } else {
4029         RLOGE("getCallForwardStatusResponse: radioService[%d]->mRadioResponse == NULL",
4030                 slotId);
4031     }
4032
4033     return 0;
4034 }
4035
4036 int radio::setCallForwardResponse(int slotId,
4037                                  int responseType, int serial, RIL_Errno e, void *response,
4038                                  size_t responseLen) {
4039 #if VDBG
4040     RLOGD("setCallForwardResponse: serial %d", serial);
4041 #endif
4042
4043     if (radioService[slotId]->mRadioResponse != NULL) {
4044         RadioResponseInfo responseInfo = {};
4045         populateResponseInfo(responseInfo, serial, responseType, e);
4046         Return<void> retStatus = radioService[slotId]->mRadioResponse->setCallForwardResponse(
4047                 responseInfo);
4048         radioService[slotId]->checkReturnStatus(retStatus);
4049     } else {
4050         RLOGE("setCallForwardResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4051     }
4052
4053     return 0;
4054 }
4055
4056 int radio::getCallWaitingResponse(int slotId,
4057                                  int responseType, int serial, RIL_Errno e, void *response,
4058                                  size_t responseLen) {
4059 #if VDBG
4060     RLOGD("getCallWaitingResponse: serial %d", serial);
4061 #endif
4062
4063     if (radioService[slotId]->mRadioResponse != NULL) {
4064         RadioResponseInfo responseInfo = {};
4065         populateResponseInfo(responseInfo, serial, responseType, e);
4066         bool enable = false;
4067         int serviceClass = -1;
4068         int numInts = responseLen / sizeof(int);
4069         if (response == NULL || numInts != 2) {
4070             RLOGE("getCallWaitingResponse Invalid response: NULL");
4071             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4072         } else {
4073             int *pInt = (int *) response;
4074             enable = pInt[0] == 1 ? true : false;
4075             serviceClass = pInt[1];
4076         }
4077         Return<void> retStatus = radioService[slotId]->mRadioResponse->getCallWaitingResponse(
4078                 responseInfo, enable, serviceClass);
4079         radioService[slotId]->checkReturnStatus(retStatus);
4080     } else {
4081         RLOGE("getCallWaitingResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4082     }
4083
4084     return 0;
4085 }
4086
4087 int radio::setCallWaitingResponse(int slotId,
4088                                  int responseType, int serial, RIL_Errno e, void *response,
4089                                  size_t responseLen) {
4090 #if VDBG
4091     RLOGD("setCallWaitingResponse: serial %d", serial);
4092 #endif
4093
4094     if (radioService[slotId]->mRadioResponse != NULL) {
4095         RadioResponseInfo responseInfo = {};
4096         populateResponseInfo(responseInfo, serial, responseType, e);
4097         Return<void> retStatus = radioService[slotId]->mRadioResponse->setCallWaitingResponse(
4098                 responseInfo);
4099         radioService[slotId]->checkReturnStatus(retStatus);
4100     } else {
4101         RLOGE("setCallWaitingResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4102     }
4103
4104     return 0;
4105 }
4106
4107 int radio::acknowledgeLastIncomingGsmSmsResponse(int slotId,
4108                                                 int responseType, int serial, RIL_Errno e,
4109                                                 void *response, size_t responseLen) {
4110 #if VDBG
4111     RLOGD("acknowledgeLastIncomingGsmSmsResponse: serial %d", serial);
4112 #endif
4113
4114     if (radioService[slotId]->mRadioResponse != NULL) {
4115         RadioResponseInfo responseInfo = {};
4116         populateResponseInfo(responseInfo, serial, responseType, e);
4117         Return<void> retStatus =
4118                 radioService[slotId]->mRadioResponse->acknowledgeLastIncomingGsmSmsResponse(
4119                 responseInfo);
4120         radioService[slotId]->checkReturnStatus(retStatus);
4121     } else {
4122         RLOGE("acknowledgeLastIncomingGsmSmsResponse: radioService[%d]->mRadioResponse "
4123                 "== NULL", slotId);
4124     }
4125
4126     return 0;
4127 }
4128
4129 int radio::acceptCallResponse(int slotId,
4130                              int responseType, int serial, RIL_Errno e,
4131                              void *response, size_t responseLen) {
4132 #if VDBG
4133     RLOGD("acceptCallResponse: serial %d", serial);
4134 #endif
4135
4136     if (radioService[slotId]->mRadioResponse != NULL) {
4137         RadioResponseInfo responseInfo = {};
4138         populateResponseInfo(responseInfo, serial, responseType, e);
4139         Return<void> retStatus = radioService[slotId]->mRadioResponse->acceptCallResponse(
4140                 responseInfo);
4141         radioService[slotId]->checkReturnStatus(retStatus);
4142     } else {
4143         RLOGE("acceptCallResponse: radioService[%d]->mRadioResponse == NULL",
4144                 slotId);
4145     }
4146
4147     return 0;
4148 }
4149
4150 int radio::deactivateDataCallResponse(int slotId,
4151                                                 int responseType, int serial, RIL_Errno e,
4152                                                 void *response, size_t responseLen) {
4153 #if VDBG
4154     RLOGD("deactivateDataCallResponse: serial %d", serial);
4155 #endif
4156
4157     if (radioService[slotId]->mRadioResponse != NULL) {
4158         RadioResponseInfo responseInfo = {};
4159         populateResponseInfo(responseInfo, serial, responseType, e);
4160         Return<void> retStatus = radioService[slotId]->mRadioResponse->deactivateDataCallResponse(
4161                 responseInfo);
4162         radioService[slotId]->checkReturnStatus(retStatus);
4163     } else {
4164         RLOGE("deactivateDataCallResponse: radioService[%d]->mRadioResponse == NULL",
4165                 slotId);
4166     }
4167
4168     return 0;
4169 }
4170
4171 int radio::getFacilityLockForAppResponse(int slotId,
4172                                         int responseType, int serial, RIL_Errno e,
4173                                         void *response, size_t responseLen) {
4174 #if VDBG
4175     RLOGD("getFacilityLockForAppResponse: serial %d", serial);
4176 #endif
4177
4178     if (radioService[slotId]->mRadioResponse != NULL) {
4179         RadioResponseInfo responseInfo = {};
4180         int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
4181         Return<void> retStatus = radioService[slotId]->mRadioResponse->
4182                 getFacilityLockForAppResponse(responseInfo, ret);
4183         radioService[slotId]->checkReturnStatus(retStatus);
4184     } else {
4185         RLOGE("getFacilityLockForAppResponse: radioService[%d]->mRadioResponse == NULL",
4186                 slotId);
4187     }
4188
4189     return 0;
4190 }
4191
4192 int radio::setFacilityLockForAppResponse(int slotId,
4193                                       int responseType, int serial, RIL_Errno e,
4194                                       void *response, size_t responseLen) {
4195 #if VDBG
4196     RLOGD("setFacilityLockForAppResponse: serial %d", serial);
4197 #endif
4198
4199     if (radioService[slotId]->mRadioResponse != NULL) {
4200         RadioResponseInfo responseInfo = {};
4201         int ret = responseIntOrEmpty(responseInfo, serial, responseType, e, response, responseLen);
4202         Return<void> retStatus
4203                 = radioService[slotId]->mRadioResponse->setFacilityLockForAppResponse(responseInfo,
4204                 ret);
4205         radioService[slotId]->checkReturnStatus(retStatus);
4206     } else {
4207         RLOGE("setFacilityLockForAppResponse: radioService[%d]->mRadioResponse == NULL",
4208                 slotId);
4209     }
4210
4211     return 0;
4212 }
4213
4214 int radio::setBarringPasswordResponse(int slotId,
4215                              int responseType, int serial, RIL_Errno e,
4216                              void *response, size_t responseLen) {
4217 #if VDBG
4218     RLOGD("acceptCallResponse: serial %d", serial);
4219 #endif
4220
4221     if (radioService[slotId]->mRadioResponse != NULL) {
4222         RadioResponseInfo responseInfo = {};
4223         populateResponseInfo(responseInfo, serial, responseType, e);
4224         Return<void> retStatus
4225                 = radioService[slotId]->mRadioResponse->setBarringPasswordResponse(responseInfo);
4226         radioService[slotId]->checkReturnStatus(retStatus);
4227     } else {
4228         RLOGE("setBarringPasswordResponse: radioService[%d]->mRadioResponse == NULL",
4229                 slotId);
4230     }
4231
4232     return 0;
4233 }
4234
4235 int radio::getNetworkSelectionModeResponse(int slotId,
4236                                           int responseType, int serial, RIL_Errno e, void *response,
4237                                           size_t responseLen) {
4238 #if VDBG
4239     RLOGD("getNetworkSelectionModeResponse: serial %d", serial);
4240 #endif
4241
4242     if (radioService[slotId]->mRadioResponse != NULL) {
4243         RadioResponseInfo responseInfo = {};
4244         populateResponseInfo(responseInfo, serial, responseType, e);
4245         bool manual = false;
4246         int serviceClass;
4247         if (response == NULL || responseLen != sizeof(int)) {
4248             RLOGE("getNetworkSelectionModeResponse Invalid response: NULL");
4249             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4250         } else {
4251             int *pInt = (int *) response;
4252             manual = pInt[0] == 1 ? true : false;
4253         }
4254         Return<void> retStatus
4255                 = radioService[slotId]->mRadioResponse->getNetworkSelectionModeResponse(
4256                 responseInfo,
4257                 manual);
4258         radioService[slotId]->checkReturnStatus(retStatus);
4259     } else {
4260         RLOGE("getNetworkSelectionModeResponse: radioService[%d]->mRadioResponse == NULL",
4261                 slotId);
4262     }
4263
4264     return 0;
4265 }
4266
4267 int radio::setNetworkSelectionModeAutomaticResponse(int slotId, int responseType, int serial,
4268                                                     RIL_Errno e, void *response,
4269                                                     size_t responseLen) {
4270 #if VDBG
4271     RLOGD("setNetworkSelectionModeAutomaticResponse: serial %d", serial);
4272 #endif
4273
4274     if (radioService[slotId]->mRadioResponse != NULL) {
4275         RadioResponseInfo responseInfo = {};
4276         populateResponseInfo(responseInfo, serial, responseType, e);
4277         Return<void> retStatus
4278                 = radioService[slotId]->mRadioResponse->setNetworkSelectionModeAutomaticResponse(
4279                 responseInfo);
4280         radioService[slotId]->checkReturnStatus(retStatus);
4281     } else {
4282         RLOGE("setNetworkSelectionModeAutomaticResponse: radioService[%d]->mRadioResponse "
4283                 "== NULL", slotId);
4284     }
4285
4286     return 0;
4287 }
4288
4289 int radio::setNetworkSelectionModeManualResponse(int slotId,
4290                              int responseType, int serial, RIL_Errno e,
4291                              void *response, size_t responseLen) {
4292 #if VDBG
4293     RLOGD("setNetworkSelectionModeManualResponse: serial %d", serial);
4294 #endif
4295
4296     if (radioService[slotId]->mRadioResponse != NULL) {
4297         RadioResponseInfo responseInfo = {};
4298         populateResponseInfo(responseInfo, serial, responseType, e);
4299         Return<void> retStatus
4300                 = radioService[slotId]->mRadioResponse->setNetworkSelectionModeManualResponse(
4301                 responseInfo);
4302         radioService[slotId]->checkReturnStatus(retStatus);
4303     } else {
4304         RLOGE("acceptCallResponse: radioService[%d]->setNetworkSelectionModeManualResponse "
4305                 "== NULL", slotId);
4306     }
4307
4308     return 0;
4309 }
4310
4311 int convertOperatorStatusToInt(const char *str) {
4312     if (strncmp("unknown", str, 9) == 0) {
4313         return (int) OperatorStatus::UNKNOWN;
4314     } else if (strncmp("available", str, 9) == 0) {
4315         return (int) OperatorStatus::AVAILABLE;
4316     } else if (strncmp("current", str, 9) == 0) {
4317         return (int) OperatorStatus::CURRENT;
4318     } else if (strncmp("forbidden", str, 9) == 0) {
4319         return (int) OperatorStatus::FORBIDDEN;
4320     } else {
4321         return -1;
4322     }
4323 }
4324
4325 int radio::getAvailableNetworksResponse(int slotId,
4326                               int responseType, int serial, RIL_Errno e, void *response,
4327                               size_t responseLen) {
4328 #if VDBG
4329     RLOGD("getAvailableNetworksResponse: serial %d", serial);
4330 #endif
4331
4332     if (radioService[slotId]->mRadioResponse != NULL) {
4333         RadioResponseInfo responseInfo = {};
4334         populateResponseInfo(responseInfo, serial, responseType, e);
4335         hidl_vec<OperatorInfo> networks;
4336         if ((response == NULL && responseLen != 0)
4337                 || responseLen % (4 * sizeof(char *))!= 0) {
4338             RLOGE("getAvailableNetworksResponse Invalid response: NULL");
4339             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4340         } else {
4341             char **resp = (char **) response;
4342             int numStrings = responseLen / sizeof(char *);
4343             networks.resize(numStrings/4);
4344             for (int i = 0, j = 0; i < numStrings; i = i + 4, j++) {
4345                 networks[j].alphaLong = convertCharPtrToHidlString(resp[i]);
4346                 networks[j].alphaShort = convertCharPtrToHidlString(resp[i + 1]);
4347                 networks[j].operatorNumeric = convertCharPtrToHidlString(resp[i + 2]);
4348                 int status = convertOperatorStatusToInt(resp[i + 3]);
4349                 if (status == -1) {
4350                     if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4351                 } else {
4352                     networks[j].status = (OperatorStatus) status;
4353                 }
4354             }
4355         }
4356         Return<void> retStatus
4357                 = radioService[slotId]->mRadioResponse->getAvailableNetworksResponse(responseInfo,
4358                 networks);
4359         radioService[slotId]->checkReturnStatus(retStatus);
4360     } else {
4361         RLOGE("getAvailableNetworksResponse: radioService[%d]->mRadioResponse == NULL",
4362                 slotId);
4363     }
4364
4365     return 0;
4366 }
4367
4368 int radio::startDtmfResponse(int slotId,
4369                             int responseType, int serial, RIL_Errno e,
4370                             void *response, size_t responseLen) {
4371 #if VDBG
4372     RLOGD("startDtmfResponse: serial %d", serial);
4373 #endif
4374
4375     if (radioService[slotId]->mRadioResponse != NULL) {
4376         RadioResponseInfo responseInfo = {};
4377         populateResponseInfo(responseInfo, serial, responseType, e);
4378         Return<void> retStatus
4379                 = radioService[slotId]->mRadioResponse->startDtmfResponse(responseInfo);
4380         radioService[slotId]->checkReturnStatus(retStatus);
4381     } else {
4382         RLOGE("startDtmfResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4383     }
4384
4385     return 0;
4386 }
4387
4388 int radio::stopDtmfResponse(int slotId,
4389                            int responseType, int serial, RIL_Errno e,
4390                            void *response, size_t responseLen) {
4391 #if VDBG
4392     RLOGD("stopDtmfResponse: serial %d", serial);
4393 #endif
4394
4395     if (radioService[slotId]->mRadioResponse != NULL) {
4396         RadioResponseInfo responseInfo = {};
4397         populateResponseInfo(responseInfo, serial, responseType, e);
4398         Return<void> retStatus
4399                 = radioService[slotId]->mRadioResponse->stopDtmfResponse(responseInfo);
4400         radioService[slotId]->checkReturnStatus(retStatus);
4401     } else {
4402         RLOGE("stopDtmfResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4403     }
4404
4405     return 0;
4406 }
4407
4408 int radio::getBasebandVersionResponse(int slotId,
4409                                      int responseType, int serial, RIL_Errno e,
4410                                      void *response, size_t responseLen) {
4411 #if VDBG
4412     RLOGD("getBasebandVersionResponse: serial %d", serial);
4413 #endif
4414
4415     if (radioService[slotId]->mRadioResponse != NULL) {
4416         RadioResponseInfo responseInfo = {};
4417         populateResponseInfo(responseInfo, serial, responseType, e);
4418         Return<void> retStatus
4419                 = radioService[slotId]->mRadioResponse->getBasebandVersionResponse(responseInfo,
4420                 convertCharPtrToHidlString((char *) response));
4421         radioService[slotId]->checkReturnStatus(retStatus);
4422     } else {
4423         RLOGE("getBasebandVersionResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4424     }
4425
4426     return 0;
4427 }
4428
4429 int radio::separateConnectionResponse(int slotId,
4430                                      int responseType, int serial, RIL_Errno e,
4431                                      void *response, size_t responseLen) {
4432 #if VDBG
4433     RLOGD("separateConnectionResponse: serial %d", serial);
4434 #endif
4435
4436     if (radioService[slotId]->mRadioResponse != NULL) {
4437         RadioResponseInfo responseInfo = {};
4438         populateResponseInfo(responseInfo, serial, responseType, e);
4439         Return<void> retStatus
4440                 = radioService[slotId]->mRadioResponse->separateConnectionResponse(responseInfo);
4441         radioService[slotId]->checkReturnStatus(retStatus);
4442     } else {
4443         RLOGE("separateConnectionResponse: radioService[%d]->mRadioResponse == NULL",
4444                 slotId);
4445     }
4446
4447     return 0;
4448 }
4449
4450 int radio::setMuteResponse(int slotId,
4451                           int responseType, int serial, RIL_Errno e,
4452                           void *response, size_t responseLen) {
4453 #if VDBG
4454     RLOGD("setMuteResponse: serial %d", serial);
4455 #endif
4456
4457     if (radioService[slotId]->mRadioResponse != NULL) {
4458         RadioResponseInfo responseInfo = {};
4459         populateResponseInfo(responseInfo, serial, responseType, e);
4460         Return<void> retStatus
4461                 = radioService[slotId]->mRadioResponse->setMuteResponse(responseInfo);
4462         radioService[slotId]->checkReturnStatus(retStatus);
4463     } else {
4464         RLOGE("setMuteResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4465     }
4466
4467     return 0;
4468 }
4469
4470 int radio::getMuteResponse(int slotId,
4471                           int responseType, int serial, RIL_Errno e, void *response,
4472                           size_t responseLen) {
4473 #if VDBG
4474     RLOGD("getMuteResponse: serial %d", serial);
4475 #endif
4476
4477     if (radioService[slotId]->mRadioResponse != NULL) {
4478         RadioResponseInfo responseInfo = {};
4479         populateResponseInfo(responseInfo, serial, responseType, e);
4480         bool enable = false;
4481         int serviceClass;
4482         if (response == NULL || responseLen != sizeof(int)) {
4483             RLOGE("getMuteResponse Invalid response: NULL");
4484             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4485         } else {
4486             int *pInt = (int *) response;
4487             enable = pInt[0] == 1 ? true : false;
4488         }
4489         Return<void> retStatus = radioService[slotId]->mRadioResponse->getMuteResponse(responseInfo,
4490                 enable);
4491         radioService[slotId]->checkReturnStatus(retStatus);
4492     } else {
4493         RLOGE("getMuteResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4494     }
4495
4496     return 0;
4497 }
4498
4499 int radio::getClipResponse(int slotId,
4500                           int responseType, int serial, RIL_Errno e,
4501                           void *response, size_t responseLen) {
4502 #if VDBG
4503     RLOGD("getClipResponse: serial %d", serial);
4504 #endif
4505
4506     if (radioService[slotId]->mRadioResponse != NULL) {
4507         RadioResponseInfo responseInfo = {};
4508         int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
4509         Return<void> retStatus = radioService[slotId]->mRadioResponse->getClipResponse(responseInfo,
4510                 (ClipStatus) ret);
4511         radioService[slotId]->checkReturnStatus(retStatus);
4512     } else {
4513         RLOGE("getClipResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4514     }
4515
4516     return 0;
4517 }
4518
4519 int radio::getDataCallListResponse(int slotId,
4520                                    int responseType, int serial, RIL_Errno e,
4521                                    void *response, size_t responseLen) {
4522 #if VDBG
4523     RLOGD("getDataCallListResponse: serial %d", serial);
4524 #endif
4525
4526     if (radioService[slotId]->mRadioResponse != NULL) {
4527         RadioResponseInfo responseInfo = {};
4528         populateResponseInfo(responseInfo, serial, responseType, e);
4529
4530         hidl_vec<SetupDataCallResult> ret;
4531         if ((response == NULL && responseLen != 0)
4532                 || responseLen % sizeof(RIL_Data_Call_Response_v11) != 0) {
4533             RLOGE("getDataCallListResponse: invalid response");
4534             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4535         } else {
4536             convertRilDataCallListToHal(response, responseLen, ret);
4537         }
4538
4539         Return<void> retStatus = radioService[slotId]->mRadioResponse->getDataCallListResponse(
4540                 responseInfo, ret);
4541         radioService[slotId]->checkReturnStatus(retStatus);
4542     } else {
4543         RLOGE("getDataCallListResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4544     }
4545
4546     return 0;
4547 }
4548
4549 int radio::setSuppServiceNotificationsResponse(int slotId,
4550                                               int responseType, int serial, RIL_Errno e,
4551                                               void *response, size_t responseLen) {
4552 #if VDBG
4553     RLOGD("setSuppServiceNotificationsResponse: serial %d", serial);
4554 #endif
4555
4556     if (radioService[slotId]->mRadioResponse != NULL) {
4557         RadioResponseInfo responseInfo = {};
4558         populateResponseInfo(responseInfo, serial, responseType, e);
4559         Return<void> retStatus
4560                 = radioService[slotId]->mRadioResponse->setSuppServiceNotificationsResponse(
4561                 responseInfo);
4562         radioService[slotId]->checkReturnStatus(retStatus);
4563     } else {
4564         RLOGE("setSuppServiceNotificationsResponse: radioService[%d]->mRadioResponse "
4565                 "== NULL", slotId);
4566     }
4567
4568     return 0;
4569 }
4570
4571 int radio::deleteSmsOnSimResponse(int slotId,
4572                                  int responseType, int serial, RIL_Errno e,
4573                                  void *response, size_t responseLen) {
4574 #if VDBG
4575     RLOGD("deleteSmsOnSimResponse: serial %d", serial);
4576 #endif
4577
4578     if (radioService[slotId]->mRadioResponse != NULL) {
4579         RadioResponseInfo responseInfo = {};
4580         populateResponseInfo(responseInfo, serial, responseType, e);
4581         Return<void> retStatus
4582                 = radioService[slotId]->mRadioResponse->deleteSmsOnSimResponse(responseInfo);
4583         radioService[slotId]->checkReturnStatus(retStatus);
4584     } else {
4585         RLOGE("deleteSmsOnSimResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4586     }
4587
4588     return 0;
4589 }
4590
4591 int radio::setBandModeResponse(int slotId,
4592                               int responseType, int serial, RIL_Errno e,
4593                               void *response, size_t responseLen) {
4594 #if VDBG
4595     RLOGD("setBandModeResponse: serial %d", serial);
4596 #endif
4597
4598     if (radioService[slotId]->mRadioResponse != NULL) {
4599         RadioResponseInfo responseInfo = {};
4600         populateResponseInfo(responseInfo, serial, responseType, e);
4601         Return<void> retStatus
4602                 = radioService[slotId]->mRadioResponse->setBandModeResponse(responseInfo);
4603         radioService[slotId]->checkReturnStatus(retStatus);
4604     } else {
4605         RLOGE("setBandModeResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4606     }
4607
4608     return 0;
4609 }
4610
4611 int radio::writeSmsToSimResponse(int slotId,
4612                                 int responseType, int serial, RIL_Errno e,
4613                                 void *response, size_t responseLen) {
4614 #if VDBG
4615     RLOGD("writeSmsToSimResponse: serial %d", serial);
4616 #endif
4617
4618     if (radioService[slotId]->mRadioResponse != NULL) {
4619         RadioResponseInfo responseInfo = {};
4620         int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
4621         Return<void> retStatus
4622                 = radioService[slotId]->mRadioResponse->writeSmsToSimResponse(responseInfo, ret);
4623         radioService[slotId]->checkReturnStatus(retStatus);
4624     } else {
4625         RLOGE("writeSmsToSimResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4626     }
4627
4628     return 0;
4629 }
4630
4631 int radio::getAvailableBandModesResponse(int slotId,
4632                                         int responseType, int serial, RIL_Errno e, void *response,
4633                                         size_t responseLen) {
4634 #if VDBG
4635     RLOGD("getAvailableBandModesResponse: serial %d", serial);
4636 #endif
4637
4638     if (radioService[slotId]->mRadioResponse != NULL) {
4639         RadioResponseInfo responseInfo = {};
4640         populateResponseInfo(responseInfo, serial, responseType, e);
4641         hidl_vec<RadioBandMode> modes;
4642         if ((response == NULL && responseLen != 0)|| responseLen % sizeof(int) != 0) {
4643             RLOGE("getAvailableBandModesResponse Invalid response: NULL");
4644             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4645         } else {
4646             int *pInt = (int *) response;
4647             int numInts = responseLen / sizeof(int);
4648             modes.resize(numInts);
4649             for (int i = 0; i < numInts; i++) {
4650                 modes[i] = (RadioBandMode) pInt[i];
4651             }
4652         }
4653         Return<void> retStatus
4654                 = radioService[slotId]->mRadioResponse->getAvailableBandModesResponse(responseInfo,
4655                 modes);
4656         radioService[slotId]->checkReturnStatus(retStatus);
4657     } else {
4658         RLOGE("getAvailableBandModesResponse: radioService[%d]->mRadioResponse == NULL",
4659                 slotId);
4660     }
4661
4662     return 0;
4663 }
4664
4665 int radio::sendEnvelopeResponse(int slotId,
4666                                int responseType, int serial, RIL_Errno e,
4667                                void *response, size_t responseLen) {
4668 #if VDBG
4669     RLOGD("sendEnvelopeResponse: serial %d", serial);
4670 #endif
4671
4672     if (radioService[slotId]->mRadioResponse != NULL) {
4673         RadioResponseInfo responseInfo = {};
4674         populateResponseInfo(responseInfo, serial, responseType, e);
4675         Return<void> retStatus
4676                 = radioService[slotId]->mRadioResponse->sendEnvelopeResponse(responseInfo,
4677                 convertCharPtrToHidlString((char *) response));
4678         radioService[slotId]->checkReturnStatus(retStatus);
4679     } else {
4680         RLOGE("sendEnvelopeResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4681     }
4682
4683     return 0;
4684 }
4685
4686 int radio::sendTerminalResponseToSimResponse(int slotId,
4687                                             int responseType, int serial, RIL_Errno e,
4688                                             void *response, size_t responseLen) {
4689 #if VDBG
4690     RLOGD("sendTerminalResponseToSimResponse: serial %d", serial);
4691 #endif
4692
4693     if (radioService[slotId]->mRadioResponse != NULL) {
4694         RadioResponseInfo responseInfo = {};
4695         populateResponseInfo(responseInfo, serial, responseType, e);
4696         Return<void> retStatus
4697                 = radioService[slotId]->mRadioResponse->sendTerminalResponseToSimResponse(
4698                 responseInfo);
4699         radioService[slotId]->checkReturnStatus(retStatus);
4700     } else {
4701         RLOGE("sendTerminalResponseToSimResponse: radioService[%d]->mRadioResponse == NULL",
4702                 slotId);
4703     }
4704
4705     return 0;
4706 }
4707
4708 int radio::handleStkCallSetupRequestFromSimResponse(int slotId,
4709                                                    int responseType, int serial,
4710                                                    RIL_Errno e, void *response,
4711                                                    size_t responseLen) {
4712 #if VDBG
4713     RLOGD("handleStkCallSetupRequestFromSimResponse: serial %d", serial);
4714 #endif
4715
4716     if (radioService[slotId]->mRadioResponse != NULL) {
4717         RadioResponseInfo responseInfo = {};
4718         populateResponseInfo(responseInfo, serial, responseType, e);
4719         Return<void> retStatus
4720                 = radioService[slotId]->mRadioResponse->handleStkCallSetupRequestFromSimResponse(
4721                 responseInfo);
4722         radioService[slotId]->checkReturnStatus(retStatus);
4723     } else {
4724         RLOGE("handleStkCallSetupRequestFromSimResponse: radioService[%d]->mRadioResponse "
4725                 "== NULL", slotId);
4726     }
4727
4728     return 0;
4729 }
4730
4731 int radio::explicitCallTransferResponse(int slotId,
4732                                        int responseType, int serial, RIL_Errno e,
4733                                        void *response, size_t responseLen) {
4734 #if VDBG
4735     RLOGD("explicitCallTransferResponse: serial %d", serial);
4736 #endif
4737
4738     if (radioService[slotId]->mRadioResponse != NULL) {
4739         RadioResponseInfo responseInfo = {};
4740         populateResponseInfo(responseInfo, serial, responseType, e);
4741         Return<void> retStatus
4742                 = radioService[slotId]->mRadioResponse->explicitCallTransferResponse(responseInfo);
4743         radioService[slotId]->checkReturnStatus(retStatus);
4744     } else {
4745         RLOGE("explicitCallTransferResponse: radioService[%d]->mRadioResponse == NULL",
4746                 slotId);
4747     }
4748
4749     return 0;
4750 }
4751
4752 int radio::setPreferredNetworkTypeResponse(int slotId,
4753                                  int responseType, int serial, RIL_Errno e,
4754                                  void *response, size_t responseLen) {
4755 #if VDBG
4756     RLOGD("setPreferredNetworkTypeResponse: serial %d", serial);
4757 #endif
4758
4759     if (radioService[slotId]->mRadioResponse != NULL) {
4760         RadioResponseInfo responseInfo = {};
4761         populateResponseInfo(responseInfo, serial, responseType, e);
4762         Return<void> retStatus
4763                 = radioService[slotId]->mRadioResponse->setPreferredNetworkTypeResponse(
4764                 responseInfo);
4765         radioService[slotId]->checkReturnStatus(retStatus);
4766     } else {
4767         RLOGE("setPreferredNetworkTypeResponse: radioService[%d]->mRadioResponse == NULL",
4768                 slotId);
4769     }
4770
4771     return 0;
4772 }
4773
4774
4775 int radio::getPreferredNetworkTypeResponse(int slotId,
4776                                           int responseType, int serial, RIL_Errno e,
4777                                           void *response, size_t responseLen) {
4778 #if VDBG
4779     RLOGD("getPreferredNetworkTypeResponse: serial %d", serial);
4780 #endif
4781
4782     if (radioService[slotId]->mRadioResponse != NULL) {
4783         RadioResponseInfo responseInfo = {};
4784         int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
4785         Return<void> retStatus
4786                 = radioService[slotId]->mRadioResponse->getPreferredNetworkTypeResponse(
4787                 responseInfo, (PreferredNetworkType) ret);
4788         radioService[slotId]->checkReturnStatus(retStatus);
4789     } else {
4790         RLOGE("getPreferredNetworkTypeResponse: radioService[%d]->mRadioResponse == NULL",
4791                 slotId);
4792     }
4793
4794     return 0;
4795 }
4796
4797 int radio::getNeighboringCidsResponse(int slotId,
4798                                      int responseType, int serial, RIL_Errno e,
4799                                      void *response, size_t responseLen) {
4800 #if VDBG
4801     RLOGD("getNeighboringCidsResponse: serial %d", serial);
4802 #endif
4803
4804     if (radioService[slotId]->mRadioResponse != NULL) {
4805         RadioResponseInfo responseInfo = {};
4806         populateResponseInfo(responseInfo, serial, responseType, e);
4807         hidl_vec<NeighboringCell> cells;
4808
4809         if ((response == NULL && responseLen != 0)
4810                 || responseLen % sizeof(RIL_NeighboringCell *) != 0) {
4811             RLOGE("getNeighboringCidsResponse Invalid response: NULL");
4812             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4813         } else {
4814             int num = responseLen / sizeof(RIL_NeighboringCell *);
4815             cells.resize(num);
4816             for (int i = 0 ; i < num; i++) {
4817                 RIL_NeighboringCell *resp = ((RIL_NeighboringCell **) response)[i];
4818                 cells[i].cid = convertCharPtrToHidlString(resp->cid);
4819                 cells[i].rssi = resp->rssi;
4820             }
4821         }
4822
4823         Return<void> retStatus
4824                 = radioService[slotId]->mRadioResponse->getNeighboringCidsResponse(responseInfo,
4825                 cells);
4826         radioService[slotId]->checkReturnStatus(retStatus);
4827     } else {
4828         RLOGE("getNeighboringCidsResponse: radioService[%d]->mRadioResponse == NULL",
4829                 slotId);
4830     }
4831
4832     return 0;
4833 }
4834
4835 int radio::setLocationUpdatesResponse(int slotId,
4836                                      int responseType, int serial, RIL_Errno e,
4837                                      void *response, size_t responseLen) {
4838 #if VDBG
4839     RLOGD("setLocationUpdatesResponse: serial %d", serial);
4840 #endif
4841
4842     if (radioService[slotId]->mRadioResponse != NULL) {
4843         RadioResponseInfo responseInfo = {};
4844         populateResponseInfo(responseInfo, serial, responseType, e);
4845         Return<void> retStatus
4846                 = radioService[slotId]->mRadioResponse->setLocationUpdatesResponse(responseInfo);
4847         radioService[slotId]->checkReturnStatus(retStatus);
4848     } else {
4849         RLOGE("setLocationUpdatesResponse: radioService[%d]->mRadioResponse == NULL",
4850                 slotId);
4851     }
4852
4853     return 0;
4854 }
4855
4856 int radio::setCdmaSubscriptionSourceResponse(int slotId,
4857                                  int responseType, int serial, RIL_Errno e,
4858                                  void *response, size_t responseLen) {
4859 #if VDBG
4860     RLOGD("setCdmaSubscriptionSourceResponse: serial %d", serial);
4861 #endif
4862
4863     if (radioService[slotId]->mRadioResponse != NULL) {
4864         RadioResponseInfo responseInfo = {};
4865         populateResponseInfo(responseInfo, serial, responseType, e);
4866         Return<void> retStatus
4867                 = radioService[slotId]->mRadioResponse->setCdmaSubscriptionSourceResponse(
4868                 responseInfo);
4869         radioService[slotId]->checkReturnStatus(retStatus);
4870     } else {
4871         RLOGE("setCdmaSubscriptionSourceResponse: radioService[%d]->mRadioResponse == NULL",
4872                 slotId);
4873     }
4874
4875     return 0;
4876 }
4877
4878 int radio::setCdmaRoamingPreferenceResponse(int slotId,
4879                                  int responseType, int serial, RIL_Errno e,
4880                                  void *response, size_t responseLen) {
4881 #if VDBG
4882     RLOGD("setCdmaRoamingPreferenceResponse: serial %d", serial);
4883 #endif
4884
4885     if (radioService[slotId]->mRadioResponse != NULL) {
4886         RadioResponseInfo responseInfo = {};
4887         populateResponseInfo(responseInfo, serial, responseType, e);
4888         Return<void> retStatus
4889                 = radioService[slotId]->mRadioResponse->setCdmaRoamingPreferenceResponse(
4890                 responseInfo);
4891         radioService[slotId]->checkReturnStatus(retStatus);
4892     } else {
4893         RLOGE("setCdmaRoamingPreferenceResponse: radioService[%d]->mRadioResponse == NULL",
4894                 slotId);
4895     }
4896
4897     return 0;
4898 }
4899
4900 int radio::getCdmaRoamingPreferenceResponse(int slotId,
4901                                            int responseType, int serial, RIL_Errno e,
4902                                            void *response, size_t responseLen) {
4903 #if VDBG
4904     RLOGD("getCdmaRoamingPreferenceResponse: serial %d", serial);
4905 #endif
4906
4907     if (radioService[slotId]->mRadioResponse != NULL) {
4908         RadioResponseInfo responseInfo = {};
4909         int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
4910         Return<void> retStatus
4911                 = radioService[slotId]->mRadioResponse->getCdmaRoamingPreferenceResponse(
4912                 responseInfo, (CdmaRoamingType) ret);
4913         radioService[slotId]->checkReturnStatus(retStatus);
4914     } else {
4915         RLOGE("getCdmaRoamingPreferenceResponse: radioService[%d]->mRadioResponse == NULL",
4916                 slotId);
4917     }
4918
4919     return 0;
4920 }
4921
4922 int radio::setTTYModeResponse(int slotId,
4923                              int responseType, int serial, RIL_Errno e,
4924                              void *response, size_t responseLen) {
4925 #if VDBG
4926     RLOGD("setTTYModeResponse: serial %d", serial);
4927 #endif
4928
4929     if (radioService[slotId]->mRadioResponse != NULL) {
4930         RadioResponseInfo responseInfo = {};
4931         populateResponseInfo(responseInfo, serial, responseType, e);
4932         Return<void> retStatus
4933                 = radioService[slotId]->mRadioResponse->setTTYModeResponse(responseInfo);
4934         radioService[slotId]->checkReturnStatus(retStatus);
4935     } else {
4936         RLOGE("setTTYModeResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4937     }
4938
4939     return 0;
4940 }
4941
4942 int radio::getTTYModeResponse(int slotId,
4943                              int responseType, int serial, RIL_Errno e,
4944                              void *response, size_t responseLen) {
4945 #if VDBG
4946     RLOGD("getTTYModeResponse: serial %d", serial);
4947 #endif
4948
4949     if (radioService[slotId]->mRadioResponse != NULL) {
4950         RadioResponseInfo responseInfo = {};
4951         int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
4952         Return<void> retStatus
4953                 = radioService[slotId]->mRadioResponse->getTTYModeResponse(responseInfo,
4954                 (TtyMode) ret);
4955         radioService[slotId]->checkReturnStatus(retStatus);
4956     } else {
4957         RLOGE("getTTYModeResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4958     }
4959
4960     return 0;
4961 }
4962
4963 int radio::setPreferredVoicePrivacyResponse(int slotId,
4964                                  int responseType, int serial, RIL_Errno e,
4965                                  void *response, size_t responseLen) {
4966 #if VDBG
4967     RLOGD("setPreferredVoicePrivacyResponse: serial %d", serial);
4968 #endif
4969
4970     if (radioService[slotId]->mRadioResponse != NULL) {
4971         RadioResponseInfo responseInfo = {};
4972         populateResponseInfo(responseInfo, serial, responseType, e);
4973         Return<void> retStatus
4974                 = radioService[slotId]->mRadioResponse->setPreferredVoicePrivacyResponse(
4975                 responseInfo);
4976         radioService[slotId]->checkReturnStatus(retStatus);
4977     } else {
4978         RLOGE("setPreferredVoicePrivacyResponse: radioService[%d]->mRadioResponse == NULL",
4979                 slotId);
4980     }
4981
4982     return 0;
4983 }
4984
4985 int radio::getPreferredVoicePrivacyResponse(int slotId,
4986                                            int responseType, int serial, RIL_Errno e,
4987                                            void *response, size_t responseLen) {
4988 #if VDBG
4989     RLOGD("getPreferredVoicePrivacyResponse: serial %d", serial);
4990 #endif
4991
4992     if (radioService[slotId]->mRadioResponse != NULL) {
4993         RadioResponseInfo responseInfo = {};
4994         populateResponseInfo(responseInfo, serial, responseType, e);
4995         bool enable = false;
4996         int numInts = responseLen / sizeof(int);
4997         if (response == NULL || numInts != 1) {
4998             RLOGE("getPreferredVoicePrivacyResponse Invalid response: NULL");
4999             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5000         } else {
5001             int *pInt = (int *) response;
5002             enable = pInt[0] == 1 ? true : false;
5003         }
5004         Return<void> retStatus
5005                 = radioService[slotId]->mRadioResponse->getPreferredVoicePrivacyResponse(
5006                 responseInfo, enable);
5007         radioService[slotId]->checkReturnStatus(retStatus);
5008     } else {
5009         RLOGE("getPreferredVoicePrivacyResponse: radioService[%d]->mRadioResponse == NULL",
5010                 slotId);
5011     }
5012
5013     return 0;
5014 }
5015
5016 int radio::sendCDMAFeatureCodeResponse(int slotId,
5017                                  int responseType, int serial, RIL_Errno e,
5018                                  void *response, size_t responseLen) {
5019 #if VDBG
5020     RLOGD("sendCDMAFeatureCodeResponse: serial %d", serial);
5021 #endif
5022
5023     if (radioService[slotId]->mRadioResponse != NULL) {
5024         RadioResponseInfo responseInfo = {};
5025         populateResponseInfo(responseInfo, serial, responseType, e);
5026         Return<void> retStatus
5027                 = radioService[slotId]->mRadioResponse->sendCDMAFeatureCodeResponse(responseInfo);
5028         radioService[slotId]->checkReturnStatus(retStatus);
5029     } else {
5030         RLOGE("sendCDMAFeatureCodeResponse: radioService[%d]->mRadioResponse == NULL",
5031                 slotId);
5032     }
5033
5034     return 0;
5035 }
5036
5037 int radio::sendBurstDtmfResponse(int slotId,
5038                                  int responseType, int serial, RIL_Errno e,
5039                                  void *response, size_t responseLen) {
5040 #if VDBG
5041     RLOGD("sendBurstDtmfResponse: serial %d", serial);
5042 #endif
5043
5044     if (radioService[slotId]->mRadioResponse != NULL) {
5045         RadioResponseInfo responseInfo = {};
5046         populateResponseInfo(responseInfo, serial, responseType, e);
5047         Return<void> retStatus
5048                 = radioService[slotId]->mRadioResponse->sendBurstDtmfResponse(responseInfo);
5049         radioService[slotId]->checkReturnStatus(retStatus);
5050     } else {
5051         RLOGE("sendBurstDtmfResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5052     }
5053
5054     return 0;
5055 }
5056
5057 int radio::sendCdmaSmsResponse(int slotId,
5058                               int responseType, int serial, RIL_Errno e, void *response,
5059                               size_t responseLen) {
5060 #if VDBG
5061     RLOGD("sendCdmaSmsResponse: serial %d", serial);
5062 #endif
5063
5064     if (radioService[slotId]->mRadioResponse != NULL) {
5065         RadioResponseInfo responseInfo = {};
5066         SendSmsResult result = makeSendSmsResult(responseInfo, serial, responseType, e, response,
5067                 responseLen);
5068
5069         Return<void> retStatus
5070                 = radioService[slotId]->mRadioResponse->sendCdmaSmsResponse(responseInfo, result);
5071         radioService[slotId]->checkReturnStatus(retStatus);
5072     } else {
5073         RLOGE("sendCdmaSmsResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5074     }
5075
5076     return 0;
5077 }
5078
5079 int radio::acknowledgeLastIncomingCdmaSmsResponse(int slotId,
5080                                                  int responseType, int serial, RIL_Errno e,
5081                                                  void *response, size_t responseLen) {
5082 #if VDBG
5083     RLOGD("acknowledgeLastIncomingCdmaSmsResponse: serial %d", serial);
5084 #endif
5085
5086     if (radioService[slotId]->mRadioResponse != NULL) {
5087         RadioResponseInfo responseInfo = {};
5088         populateResponseInfo(responseInfo, serial, responseType, e);
5089         Return<void> retStatus
5090                 = radioService[slotId]->mRadioResponse->acknowledgeLastIncomingCdmaSmsResponse(
5091                 responseInfo);
5092         radioService[slotId]->checkReturnStatus(retStatus);
5093     } else {
5094         RLOGE("acknowledgeLastIncomingCdmaSmsResponse: radioService[%d]->mRadioResponse "
5095                 "== NULL", slotId);
5096     }
5097
5098     return 0;
5099 }
5100
5101 int radio::getGsmBroadcastConfigResponse(int slotId,
5102                                         int responseType, int serial, RIL_Errno e,
5103                                         void *response, size_t responseLen) {
5104 #if VDBG
5105     RLOGD("getGsmBroadcastConfigResponse: serial %d", serial);
5106 #endif
5107
5108     if (radioService[slotId]->mRadioResponse != NULL) {
5109         RadioResponseInfo responseInfo = {};
5110         populateResponseInfo(responseInfo, serial, responseType, e);
5111         hidl_vec<GsmBroadcastSmsConfigInfo> configs;
5112
5113         if ((response == NULL && responseLen != 0)
5114                 || responseLen % sizeof(RIL_GSM_BroadcastSmsConfigInfo *) != 0) {
5115             RLOGE("getGsmBroadcastConfigResponse Invalid response: NULL");
5116             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5117         } else {
5118             int num = responseLen / sizeof(RIL_GSM_BroadcastSmsConfigInfo *);
5119             configs.resize(num);
5120             for (int i = 0 ; i < num; i++) {
5121                 RIL_GSM_BroadcastSmsConfigInfo *resp =
5122                         ((RIL_GSM_BroadcastSmsConfigInfo **) response)[i];
5123                 configs[i].fromServiceId = resp->fromServiceId;
5124                 configs[i].toServiceId = resp->toServiceId;
5125                 configs[i].fromCodeScheme = resp->fromCodeScheme;
5126                 configs[i].toCodeScheme = resp->toCodeScheme;
5127                 configs[i].selected = resp->selected == 1 ? true : false;
5128             }
5129         }
5130
5131         Return<void> retStatus
5132                 = radioService[slotId]->mRadioResponse->getGsmBroadcastConfigResponse(responseInfo,
5133                 configs);
5134         radioService[slotId]->checkReturnStatus(retStatus);
5135     } else {
5136         RLOGE("getGsmBroadcastConfigResponse: radioService[%d]->mRadioResponse == NULL",
5137                 slotId);
5138     }
5139
5140     return 0;
5141 }
5142
5143 int radio::setGsmBroadcastConfigResponse(int slotId,
5144                                         int responseType, int serial, RIL_Errno e,
5145                                         void *response, size_t responseLen) {
5146 #if VDBG
5147     RLOGD("setGsmBroadcastConfigResponse: serial %d", serial);
5148 #endif
5149
5150     if (radioService[slotId]->mRadioResponse != NULL) {
5151         RadioResponseInfo responseInfo = {};
5152         populateResponseInfo(responseInfo, serial, responseType, e);
5153         Return<void> retStatus
5154                 = radioService[slotId]->mRadioResponse->setGsmBroadcastConfigResponse(responseInfo);
5155         radioService[slotId]->checkReturnStatus(retStatus);
5156     } else {
5157         RLOGE("setGsmBroadcastConfigResponse: radioService[%d]->mRadioResponse == NULL",
5158                 slotId);
5159     }
5160
5161     return 0;
5162 }
5163
5164 int radio::setGsmBroadcastActivationResponse(int slotId,
5165                                             int responseType, int serial, RIL_Errno e,
5166                                             void *response, size_t responseLen) {
5167 #if VDBG
5168     RLOGD("setGsmBroadcastActivationResponse: serial %d", serial);
5169 #endif
5170
5171     if (radioService[slotId]->mRadioResponse != NULL) {
5172         RadioResponseInfo responseInfo = {};
5173         populateResponseInfo(responseInfo, serial, responseType, e);
5174         Return<void> retStatus
5175                 = radioService[slotId]->mRadioResponse->setGsmBroadcastActivationResponse(
5176                 responseInfo);
5177         radioService[slotId]->checkReturnStatus(retStatus);
5178     } else {
5179         RLOGE("setGsmBroadcastActivationResponse: radioService[%d]->mRadioResponse == NULL",
5180                 slotId);
5181     }
5182
5183     return 0;
5184 }
5185
5186 int radio::getCdmaBroadcastConfigResponse(int slotId,
5187                                          int responseType, int serial, RIL_Errno e,
5188                                          void *response, size_t responseLen) {
5189 #if VDBG
5190     RLOGD("getCdmaBroadcastConfigResponse: serial %d", serial);
5191 #endif
5192
5193     if (radioService[slotId]->mRadioResponse != NULL) {
5194         RadioResponseInfo responseInfo = {};
5195         populateResponseInfo(responseInfo, serial, responseType, e);
5196         hidl_vec<CdmaBroadcastSmsConfigInfo> configs;
5197
5198         if ((response == NULL && responseLen != 0)
5199                 || responseLen % sizeof(RIL_CDMA_BroadcastSmsConfigInfo *) != 0) {
5200             RLOGE("getCdmaBroadcastConfigResponse Invalid response: NULL");
5201             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5202         } else {
5203             int num = responseLen / sizeof(RIL_CDMA_BroadcastSmsConfigInfo *);
5204             configs.resize(num);
5205             for (int i = 0 ; i < num; i++) {
5206                 RIL_CDMA_BroadcastSmsConfigInfo *resp =
5207                         ((RIL_CDMA_BroadcastSmsConfigInfo **) response)[i];
5208                 configs[i].serviceCategory = resp->service_category;
5209                 configs[i].language = resp->language;
5210                 configs[i].selected = resp->selected == 1 ? true : false;
5211             }
5212         }
5213
5214         Return<void> retStatus
5215                 = radioService[slotId]->mRadioResponse->getCdmaBroadcastConfigResponse(responseInfo,
5216                 configs);
5217         radioService[slotId]->checkReturnStatus(retStatus);
5218     } else {
5219         RLOGE("getCdmaBroadcastConfigResponse: radioService[%d]->mRadioResponse == NULL",
5220                 slotId);
5221     }
5222
5223     return 0;
5224 }
5225
5226 int radio::setCdmaBroadcastConfigResponse(int slotId,
5227                                          int responseType, int serial, RIL_Errno e,
5228                                          void *response, size_t responseLen) {
5229 #if VDBG
5230     RLOGD("setCdmaBroadcastConfigResponse: serial %d", serial);
5231 #endif
5232
5233     if (radioService[slotId]->mRadioResponse != NULL) {
5234         RadioResponseInfo responseInfo = {};
5235         populateResponseInfo(responseInfo, serial, responseType, e);
5236         Return<void> retStatus
5237                 = radioService[slotId]->mRadioResponse->setCdmaBroadcastConfigResponse(
5238                 responseInfo);
5239         radioService[slotId]->checkReturnStatus(retStatus);
5240     } else {
5241         RLOGE("setCdmaBroadcastConfigResponse: radioService[%d]->mRadioResponse == NULL",
5242                 slotId);
5243     }
5244
5245     return 0;
5246 }
5247
5248 int radio::setCdmaBroadcastActivationResponse(int slotId,
5249                                              int responseType, int serial, RIL_Errno e,
5250                                              void *response, size_t responseLen) {
5251 #if VDBG
5252     RLOGD("setCdmaBroadcastActivationResponse: serial %d", serial);
5253 #endif
5254
5255     if (radioService[slotId]->mRadioResponse != NULL) {
5256         RadioResponseInfo responseInfo = {};
5257         populateResponseInfo(responseInfo, serial, responseType, e);
5258         Return<void> retStatus
5259                 = radioService[slotId]->mRadioResponse->setCdmaBroadcastActivationResponse(
5260                 responseInfo);
5261         radioService[slotId]->checkReturnStatus(retStatus);
5262     } else {
5263         RLOGE("setCdmaBroadcastActivationResponse: radioService[%d]->mRadioResponse == NULL",
5264                 slotId);
5265     }
5266
5267     return 0;
5268 }
5269
5270 int radio::getCDMASubscriptionResponse(int slotId,
5271                                       int responseType, int serial, RIL_Errno e, void *response,
5272                                       size_t responseLen) {
5273 #if VDBG
5274     RLOGD("getCDMASubscriptionResponse: serial %d", serial);
5275 #endif
5276
5277     if (radioService[slotId]->mRadioResponse != NULL) {
5278         RadioResponseInfo responseInfo = {};
5279         populateResponseInfo(responseInfo, serial, responseType, e);
5280
5281         int numStrings = responseLen / sizeof(char *);
5282         hidl_string emptyString;
5283         if (response == NULL || numStrings != 5) {
5284             RLOGE("getOperatorResponse Invalid response: NULL");
5285             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5286             Return<void> retStatus
5287                     = radioService[slotId]->mRadioResponse->getCDMASubscriptionResponse(
5288                     responseInfo, emptyString, emptyString, emptyString, emptyString, emptyString);
5289             radioService[slotId]->checkReturnStatus(retStatus);
5290         } else {
5291             char **resp = (char **) response;
5292             Return<void> retStatus
5293                     = radioService[slotId]->mRadioResponse->getCDMASubscriptionResponse(
5294                     responseInfo,
5295                     convertCharPtrToHidlString(resp[0]),
5296                     convertCharPtrToHidlString(resp[1]),
5297                     convertCharPtrToHidlString(resp[2]),
5298                     convertCharPtrToHidlString(resp[3]),
5299                     convertCharPtrToHidlString(resp[4]));
5300             radioService[slotId]->checkReturnStatus(retStatus);
5301         }
5302     } else {
5303         RLOGE("getCDMASubscriptionResponse: radioService[%d]->mRadioResponse == NULL",
5304                 slotId);
5305     }
5306
5307     return 0;
5308 }
5309
5310 int radio::writeSmsToRuimResponse(int slotId,
5311                                  int responseType, int serial, RIL_Errno e,
5312                                  void *response, size_t responseLen) {
5313 #if VDBG
5314     RLOGD("writeSmsToRuimResponse: serial %d", serial);
5315 #endif
5316
5317     if (radioService[slotId]->mRadioResponse != NULL) {
5318         RadioResponseInfo responseInfo = {};
5319         int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
5320         Return<void> retStatus
5321                 = radioService[slotId]->mRadioResponse->writeSmsToRuimResponse(responseInfo, ret);
5322         radioService[slotId]->checkReturnStatus(retStatus);
5323     } else {
5324         RLOGE("writeSmsToRuimResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5325     }
5326
5327     return 0;
5328 }
5329
5330 int radio::deleteSmsOnRuimResponse(int slotId,
5331                                   int responseType, int serial, RIL_Errno e,
5332                                   void *response, size_t responseLen) {
5333 #if VDBG
5334     RLOGD("deleteSmsOnRuimResponse: serial %d", serial);
5335 #endif
5336
5337     if (radioService[slotId]->mRadioResponse != NULL) {
5338         RadioResponseInfo responseInfo = {};
5339         populateResponseInfo(responseInfo, serial, responseType, e);
5340         Return<void> retStatus
5341                 = radioService[slotId]->mRadioResponse->deleteSmsOnRuimResponse(responseInfo);
5342         radioService[slotId]->checkReturnStatus(retStatus);
5343     } else {
5344         RLOGE("deleteSmsOnRuimResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5345     }
5346
5347     return 0;
5348 }
5349
5350 int radio::getDeviceIdentityResponse(int slotId,
5351                                     int responseType, int serial, RIL_Errno e, void *response,
5352                                     size_t responseLen) {
5353 #if VDBG
5354     RLOGD("getDeviceIdentityResponse: serial %d", serial);
5355 #endif
5356
5357     if (radioService[slotId]->mRadioResponse != NULL) {
5358         RadioResponseInfo responseInfo = {};
5359         populateResponseInfo(responseInfo, serial, responseType, e);
5360
5361         int numStrings = responseLen / sizeof(char *);
5362         hidl_string emptyString;
5363         if (response == NULL || numStrings != 4) {
5364             RLOGE("getDeviceIdentityResponse Invalid response: NULL");
5365             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5366             Return<void> retStatus
5367                     = radioService[slotId]->mRadioResponse->getDeviceIdentityResponse(responseInfo,
5368                     emptyString, emptyString, emptyString, emptyString);
5369             radioService[slotId]->checkReturnStatus(retStatus);
5370         } else {
5371             char **resp = (char **) response;
5372             Return<void> retStatus
5373                     = radioService[slotId]->mRadioResponse->getDeviceIdentityResponse(responseInfo,
5374                     convertCharPtrToHidlString(resp[0]),
5375                     convertCharPtrToHidlString(resp[1]),
5376                     convertCharPtrToHidlString(resp[2]),
5377                     convertCharPtrToHidlString(resp[3]));
5378             radioService[slotId]->checkReturnStatus(retStatus);
5379         }
5380     } else {
5381         RLOGE("getDeviceIdentityResponse: radioService[%d]->mRadioResponse == NULL",
5382                 slotId);
5383     }
5384
5385     return 0;
5386 }
5387
5388 int radio::exitEmergencyCallbackModeResponse(int slotId,
5389                                             int responseType, int serial, RIL_Errno e,
5390                                             void *response, size_t responseLen) {
5391 #if VDBG
5392     RLOGD("exitEmergencyCallbackModeResponse: serial %d", serial);
5393 #endif
5394
5395     if (radioService[slotId]->mRadioResponse != NULL) {
5396         RadioResponseInfo responseInfo = {};
5397         populateResponseInfo(responseInfo, serial, responseType, e);
5398         Return<void> retStatus
5399                 = radioService[slotId]->mRadioResponse->exitEmergencyCallbackModeResponse(
5400                 responseInfo);
5401         radioService[slotId]->checkReturnStatus(retStatus);
5402     } else {
5403         RLOGE("exitEmergencyCallbackModeResponse: radioService[%d]->mRadioResponse == NULL",
5404                 slotId);
5405     }
5406
5407     return 0;
5408 }
5409
5410 int radio::getSmscAddressResponse(int slotId,
5411                                   int responseType, int serial, RIL_Errno e,
5412                                   void *response, size_t responseLen) {
5413 #if VDBG
5414     RLOGD("getSmscAddressResponse: serial %d", serial);
5415 #endif
5416
5417     if (radioService[slotId]->mRadioResponse != NULL) {
5418         RadioResponseInfo responseInfo = {};
5419         populateResponseInfo(responseInfo, serial, responseType, e);
5420         Return<void> retStatus
5421                 = radioService[slotId]->mRadioResponse->getSmscAddressResponse(responseInfo,
5422                 convertCharPtrToHidlString((char *) response));
5423         radioService[slotId]->checkReturnStatus(retStatus);
5424     } else {
5425         RLOGE("getSmscAddressResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5426     }
5427
5428     return 0;
5429 }
5430
5431 int radio::setSmscAddressResponse(int slotId,
5432                                              int responseType, int serial, RIL_Errno e,
5433                                              void *response, size_t responseLen) {
5434 #if VDBG
5435     RLOGD("setSmscAddressResponse: serial %d", serial);
5436 #endif
5437
5438     if (radioService[slotId]->mRadioResponse != NULL) {
5439         RadioResponseInfo responseInfo = {};
5440         populateResponseInfo(responseInfo, serial, responseType, e);
5441         Return<void> retStatus
5442                 = radioService[slotId]->mRadioResponse->setSmscAddressResponse(responseInfo);
5443         radioService[slotId]->checkReturnStatus(retStatus);
5444     } else {
5445         RLOGE("setSmscAddressResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5446     }
5447
5448     return 0;
5449 }
5450
5451 int radio::reportSmsMemoryStatusResponse(int slotId,
5452                                         int responseType, int serial, RIL_Errno e,
5453                                         void *response, size_t responseLen) {
5454 #if VDBG
5455     RLOGD("reportSmsMemoryStatusResponse: serial %d", serial);
5456 #endif
5457
5458     if (radioService[slotId]->mRadioResponse != NULL) {
5459         RadioResponseInfo responseInfo = {};
5460         populateResponseInfo(responseInfo, serial, responseType, e);
5461         Return<void> retStatus
5462                 = radioService[slotId]->mRadioResponse->reportSmsMemoryStatusResponse(responseInfo);
5463         radioService[slotId]->checkReturnStatus(retStatus);
5464     } else {
5465         RLOGE("reportSmsMemoryStatusResponse: radioService[%d]->mRadioResponse == NULL",
5466                 slotId);
5467     }
5468
5469     return 0;
5470 }
5471
5472 int radio::reportStkServiceIsRunningResponse(int slotId,
5473                                              int responseType, int serial, RIL_Errno e,
5474                                              void *response, size_t responseLen) {
5475 #if VDBG
5476     RLOGD("reportStkServiceIsRunningResponse: serial %d", serial);
5477 #endif
5478
5479     if (radioService[slotId]->mRadioResponse != NULL) {
5480         RadioResponseInfo responseInfo = {};
5481         populateResponseInfo(responseInfo, serial, responseType, e);
5482         Return<void> retStatus = radioService[slotId]->mRadioResponse->
5483                 reportStkServiceIsRunningResponse(responseInfo);
5484         radioService[slotId]->checkReturnStatus(retStatus);
5485     } else {
5486         RLOGE("reportStkServiceIsRunningResponse: radioService[%d]->mRadioResponse == NULL",
5487                 slotId);
5488     }
5489
5490     return 0;
5491 }
5492
5493 int radio::getCdmaSubscriptionSourceResponse(int slotId,
5494                                             int responseType, int serial, RIL_Errno e,
5495                                             void *response, size_t responseLen) {
5496 #if VDBG
5497     RLOGD("getCdmaSubscriptionSourceResponse: serial %d", serial);
5498 #endif
5499
5500     if (radioService[slotId]->mRadioResponse != NULL) {
5501         RadioResponseInfo responseInfo = {};
5502         int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
5503         Return<void> retStatus
5504                 = radioService[slotId]->mRadioResponse->getCdmaSubscriptionSourceResponse(
5505                 responseInfo, (CdmaSubscriptionSource) ret);
5506         radioService[slotId]->checkReturnStatus(retStatus);
5507     } else {
5508         RLOGE("getCdmaSubscriptionSourceResponse: radioService[%d]->mRadioResponse == NULL",
5509                 slotId);
5510     }
5511
5512     return 0;
5513 }
5514
5515 int radio::requestIsimAuthenticationResponse(int slotId,
5516                                             int responseType, int serial, RIL_Errno e,
5517                                             void *response, size_t responseLen) {
5518 #if VDBG
5519     RLOGD("requestIsimAuthenticationResponse: serial %d", serial);
5520 #endif
5521
5522     if (radioService[slotId]->mRadioResponse != NULL) {
5523         RadioResponseInfo responseInfo = {};
5524         populateResponseInfo(responseInfo, serial, responseType, e);
5525         Return<void> retStatus
5526                 = radioService[slotId]->mRadioResponse->requestIsimAuthenticationResponse(
5527                 responseInfo,
5528                 convertCharPtrToHidlString((char *) response));
5529         radioService[slotId]->checkReturnStatus(retStatus);
5530     } else {
5531         RLOGE("requestIsimAuthenticationResponse: radioService[%d]->mRadioResponse == NULL",
5532                 slotId);
5533     }
5534
5535     return 0;
5536 }
5537
5538 int radio::acknowledgeIncomingGsmSmsWithPduResponse(int slotId,
5539                                                    int responseType,
5540                                                    int serial, RIL_Errno e, void *response,
5541                                                    size_t responseLen) {
5542 #if VDBG
5543     RLOGD("acknowledgeIncomingGsmSmsWithPduResponse: serial %d", serial);
5544 #endif
5545
5546     if (radioService[slotId]->mRadioResponse != NULL) {
5547         RadioResponseInfo responseInfo = {};
5548         populateResponseInfo(responseInfo, serial, responseType, e);
5549         Return<void> retStatus
5550                 = radioService[slotId]->mRadioResponse->acknowledgeIncomingGsmSmsWithPduResponse(
5551                 responseInfo);
5552         radioService[slotId]->checkReturnStatus(retStatus);
5553     } else {
5554         RLOGE("acknowledgeIncomingGsmSmsWithPduResponse: radioService[%d]->mRadioResponse "
5555                 "== NULL", slotId);
5556     }
5557
5558     return 0;
5559 }
5560
5561 int radio::sendEnvelopeWithStatusResponse(int slotId,
5562                                          int responseType, int serial, RIL_Errno e, void *response,
5563                                          size_t responseLen) {
5564 #if VDBG
5565     RLOGD("sendEnvelopeWithStatusResponse: serial %d", serial);
5566 #endif
5567
5568     if (radioService[slotId]->mRadioResponse != NULL) {
5569         RadioResponseInfo responseInfo = {};
5570         IccIoResult result = responseIccIo(responseInfo, serial, responseType, e,
5571                 response, responseLen);
5572
5573         Return<void> retStatus
5574                 = radioService[slotId]->mRadioResponse->sendEnvelopeWithStatusResponse(responseInfo,
5575                 result);
5576         radioService[slotId]->checkReturnStatus(retStatus);
5577     } else {
5578         RLOGE("sendEnvelopeWithStatusResponse: radioService[%d]->mRadioResponse == NULL",
5579                 slotId);
5580     }
5581
5582     return 0;
5583 }
5584
5585 int radio::getVoiceRadioTechnologyResponse(int slotId,
5586                                           int responseType, int serial, RIL_Errno e,
5587                                           void *response, size_t responseLen) {
5588 #if VDBG
5589     RLOGD("getVoiceRadioTechnologyResponse: serial %d", serial);
5590 #endif
5591
5592     if (radioService[slotId]->mRadioResponse != NULL) {
5593         RadioResponseInfo responseInfo = {};
5594         int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
5595         Return<void> retStatus
5596                 = radioService[slotId]->mRadioResponse->getVoiceRadioTechnologyResponse(
5597                 responseInfo, (RadioTechnology) ret);
5598         radioService[slotId]->checkReturnStatus(retStatus);
5599     } else {
5600         RLOGE("getVoiceRadioTechnologyResponse: radioService[%d]->mRadioResponse == NULL",
5601                 slotId);
5602     }
5603
5604     return 0;
5605 }
5606
5607 int radio::getCellInfoListResponse(int slotId,
5608                                    int responseType,
5609                                    int serial, RIL_Errno e, void *response,
5610                                    size_t responseLen) {
5611 #if VDBG
5612     RLOGD("getCellInfoListResponse: serial %d", serial);
5613 #endif
5614
5615     if (radioService[slotId]->mRadioResponse != NULL) {
5616         RadioResponseInfo responseInfo = {};
5617         populateResponseInfo(responseInfo, serial, responseType, e);
5618
5619         hidl_vec<CellInfo> ret;
5620         if ((response == NULL && responseLen != 0)
5621                 || responseLen % sizeof(RIL_CellInfo_v12) != 0) {
5622             RLOGE("getCellInfoListResponse: Invalid response");
5623             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5624         } else {
5625             convertRilCellInfoListToHal(response, responseLen, ret);
5626         }
5627
5628         Return<void> retStatus = radioService[slotId]->mRadioResponse->getCellInfoListResponse(
5629                 responseInfo, ret);
5630         radioService[slotId]->checkReturnStatus(retStatus);
5631     } else {
5632         RLOGE("getCellInfoListResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5633     }
5634
5635     return 0;
5636 }
5637
5638 int radio::setCellInfoListRateResponse(int slotId,
5639                                        int responseType,
5640                                        int serial, RIL_Errno e, void *response,
5641                                        size_t responseLen) {
5642 #if VDBG
5643     RLOGD("setCellInfoListRateResponse: serial %d", serial);
5644 #endif
5645
5646     if (radioService[slotId]->mRadioResponse != NULL) {
5647         RadioResponseInfo responseInfo = {};
5648         populateResponseInfo(responseInfo, serial, responseType, e);
5649         Return<void> retStatus
5650                 = radioService[slotId]->mRadioResponse->setCellInfoListRateResponse(responseInfo);
5651         radioService[slotId]->checkReturnStatus(retStatus);
5652     } else {
5653         RLOGE("setCellInfoListRateResponse: radioService[%d]->mRadioResponse == NULL",
5654                 slotId);
5655     }
5656
5657     return 0;
5658 }
5659
5660 int radio::setInitialAttachApnResponse(int slotId,
5661                                        int responseType, int serial, RIL_Errno e,
5662                                        void *response, size_t responseLen) {
5663 #if VDBG
5664     RLOGD("setInitialAttachApnResponse: serial %d", serial);
5665 #endif
5666
5667     if (radioService[slotId]->mRadioResponse != NULL) {
5668         RadioResponseInfo responseInfo = {};
5669         populateResponseInfo(responseInfo, serial, responseType, e);
5670         Return<void> retStatus
5671                 = radioService[slotId]->mRadioResponse->setInitialAttachApnResponse(responseInfo);
5672         radioService[slotId]->checkReturnStatus(retStatus);
5673     } else {
5674         RLOGE("setInitialAttachApnResponse: radioService[%d]->mRadioResponse == NULL",
5675                 slotId);
5676     }
5677
5678     return 0;
5679 }
5680
5681 int radio::getImsRegistrationStateResponse(int slotId,
5682                                            int responseType, int serial, RIL_Errno e,
5683                                            void *response, size_t responseLen) {
5684 #if VDBG
5685     RLOGD("getImsRegistrationStateResponse: serial %d", serial);
5686 #endif
5687
5688     if (radioService[slotId]->mRadioResponse != NULL) {
5689         RadioResponseInfo responseInfo = {};
5690         populateResponseInfo(responseInfo, serial, responseType, e);
5691         bool isRegistered = false;
5692         int ratFamily = 0;
5693         int numInts = responseLen / sizeof(int);
5694         if (response == NULL || numInts != 2) {
5695             RLOGE("getImsRegistrationStateResponse Invalid response: NULL");
5696             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5697         } else {
5698             int *pInt = (int *) response;
5699             isRegistered = pInt[0] == 1 ? true : false;
5700             ratFamily = pInt[1];
5701         }
5702         Return<void> retStatus
5703                 = radioService[slotId]->mRadioResponse->getImsRegistrationStateResponse(
5704                 responseInfo, isRegistered, (RadioTechnologyFamily) ratFamily);
5705         radioService[slotId]->checkReturnStatus(retStatus);
5706     } else {
5707         RLOGE("getImsRegistrationStateResponse: radioService[%d]->mRadioResponse == NULL",
5708                 slotId);
5709     }
5710
5711     return 0;
5712 }
5713
5714 int radio::sendImsSmsResponse(int slotId,
5715                               int responseType, int serial, RIL_Errno e, void *response,
5716                               size_t responseLen) {
5717 #if VDBG
5718     RLOGD("sendImsSmsResponse: serial %d", serial);
5719 #endif
5720
5721     if (radioService[slotId]->mRadioResponse != NULL) {
5722         RadioResponseInfo responseInfo = {};
5723         SendSmsResult result = makeSendSmsResult(responseInfo, serial, responseType, e, response,
5724                 responseLen);
5725
5726         Return<void> retStatus
5727                 = radioService[slotId]->mRadioResponse->sendImsSmsResponse(responseInfo, result);
5728         radioService[slotId]->checkReturnStatus(retStatus);
5729     } else {
5730         RLOGE("sendSmsResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5731     }
5732
5733     return 0;
5734 }
5735
5736 int radio::iccTransmitApduBasicChannelResponse(int slotId,
5737                                                int responseType, int serial, RIL_Errno e,
5738                                                void *response, size_t responseLen) {
5739 #if VDBG
5740     RLOGD("iccTransmitApduBasicChannelResponse: serial %d", serial);
5741 #endif
5742
5743     if (radioService[slotId]->mRadioResponse != NULL) {
5744         RadioResponseInfo responseInfo = {};
5745         IccIoResult result = responseIccIo(responseInfo, serial, responseType, e, response,
5746                 responseLen);
5747
5748         Return<void> retStatus
5749                 = radioService[slotId]->mRadioResponse->iccTransmitApduBasicChannelResponse(
5750                 responseInfo, result);
5751         radioService[slotId]->checkReturnStatus(retStatus);
5752     } else {
5753         RLOGE("iccTransmitApduBasicChannelResponse: radioService[%d]->mRadioResponse "
5754                 "== NULL", slotId);
5755     }
5756
5757     return 0;
5758 }
5759
5760 int radio::iccOpenLogicalChannelResponse(int slotId,
5761                                          int responseType, int serial, RIL_Errno e, void *response,
5762                                          size_t responseLen) {
5763 #if VDBG
5764     RLOGD("iccOpenLogicalChannelResponse: serial %d", serial);
5765 #endif
5766
5767     if (radioService[slotId]->mRadioResponse != NULL) {
5768         RadioResponseInfo responseInfo = {};
5769         populateResponseInfo(responseInfo, serial, responseType, e);
5770         int channelId = -1;
5771         hidl_vec<int8_t> selectResponse;
5772         int numInts = responseLen / sizeof(int);
5773         if (response == NULL || responseLen % sizeof(int) != 0) {
5774             RLOGE("iccOpenLogicalChannelResponse Invalid response: NULL");
5775             if (response != NULL) {
5776                 if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5777             }
5778         } else {
5779             int *pInt = (int *) response;
5780             channelId = pInt[0];
5781             selectResponse.resize(numInts - 1);
5782             for (int i = 1; i < numInts; i++) {
5783                 selectResponse[i - 1] = (int8_t) pInt[i];
5784             }
5785         }
5786         Return<void> retStatus
5787                 = radioService[slotId]->mRadioResponse->iccOpenLogicalChannelResponse(responseInfo,
5788                 channelId, selectResponse);
5789         radioService[slotId]->checkReturnStatus(retStatus);
5790     } else {
5791         RLOGE("iccOpenLogicalChannelResponse: radioService[%d]->mRadioResponse == NULL",
5792                 slotId);
5793     }
5794
5795     return 0;
5796 }
5797
5798 int radio::iccCloseLogicalChannelResponse(int slotId,
5799                                           int responseType, int serial, RIL_Errno e,
5800                                           void *response, size_t responseLen) {
5801 #if VDBG
5802     RLOGD("iccCloseLogicalChannelResponse: serial %d", serial);
5803 #endif
5804
5805     if (radioService[slotId]->mRadioResponse != NULL) {
5806         RadioResponseInfo responseInfo = {};
5807         populateResponseInfo(responseInfo, serial, responseType, e);
5808         Return<void> retStatus
5809                 = radioService[slotId]->mRadioResponse->iccCloseLogicalChannelResponse(
5810                 responseInfo);
5811         radioService[slotId]->checkReturnStatus(retStatus);
5812     } else {
5813         RLOGE("iccCloseLogicalChannelResponse: radioService[%d]->mRadioResponse == NULL",
5814                 slotId);
5815     }
5816
5817     return 0;
5818 }
5819
5820 int radio::iccTransmitApduLogicalChannelResponse(int slotId,
5821                                                  int responseType, int serial, RIL_Errno e,
5822                                                  void *response, size_t responseLen) {
5823 #if VDBG
5824     RLOGD("iccTransmitApduLogicalChannelResponse: serial %d", serial);
5825 #endif
5826
5827     if (radioService[slotId]->mRadioResponse != NULL) {
5828         RadioResponseInfo responseInfo = {};
5829         IccIoResult result = responseIccIo(responseInfo, serial, responseType, e, response,
5830                 responseLen);
5831
5832         Return<void> retStatus
5833                 = radioService[slotId]->mRadioResponse->iccTransmitApduLogicalChannelResponse(
5834                 responseInfo, result);
5835         radioService[slotId]->checkReturnStatus(retStatus);
5836     } else {
5837         RLOGE("iccTransmitApduLogicalChannelResponse: radioService[%d]->mRadioResponse "
5838                 "== NULL", slotId);
5839     }
5840
5841     return 0;
5842 }
5843
5844 int radio::nvReadItemResponse(int slotId,
5845                               int responseType, int serial, RIL_Errno e,
5846                               void *response, size_t responseLen) {
5847 #if VDBG
5848     RLOGD("nvReadItemResponse: serial %d", serial);
5849 #endif
5850
5851     if (radioService[slotId]->mRadioResponse != NULL) {
5852         RadioResponseInfo responseInfo = {};
5853         populateResponseInfo(responseInfo, serial, responseType, e);
5854         Return<void> retStatus = radioService[slotId]->mRadioResponse->nvReadItemResponse(
5855                 responseInfo,
5856                 convertCharPtrToHidlString((char *) response));
5857         radioService[slotId]->checkReturnStatus(retStatus);
5858     } else {
5859         RLOGE("nvReadItemResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5860     }
5861
5862     return 0;
5863 }
5864
5865 int radio::nvWriteItemResponse(int slotId,
5866                                int responseType, int serial, RIL_Errno e,
5867                                void *response, size_t responseLen) {
5868 #if VDBG
5869     RLOGD("nvWriteItemResponse: serial %d", serial);
5870 #endif
5871
5872     if (radioService[slotId]->mRadioResponse != NULL) {
5873         RadioResponseInfo responseInfo = {};
5874         populateResponseInfo(responseInfo, serial, responseType, e);
5875         Return<void> retStatus
5876                 = radioService[slotId]->mRadioResponse->nvWriteItemResponse(responseInfo);
5877         radioService[slotId]->checkReturnStatus(retStatus);
5878     } else {
5879         RLOGE("nvWriteItemResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5880     }
5881
5882     return 0;
5883 }
5884
5885 int radio::nvWriteCdmaPrlResponse(int slotId,
5886                                   int responseType, int serial, RIL_Errno e,
5887                                   void *response, size_t responseLen) {
5888 #if VDBG
5889     RLOGD("nvWriteCdmaPrlResponse: serial %d", serial);
5890 #endif
5891
5892     if (radioService[slotId]->mRadioResponse != NULL) {
5893         RadioResponseInfo responseInfo = {};
5894         populateResponseInfo(responseInfo, serial, responseType, e);
5895         Return<void> retStatus
5896                 = radioService[slotId]->mRadioResponse->nvWriteCdmaPrlResponse(responseInfo);
5897         radioService[slotId]->checkReturnStatus(retStatus);
5898     } else {
5899         RLOGE("nvWriteCdmaPrlResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5900     }
5901
5902     return 0;
5903 }
5904
5905 int radio::nvResetConfigResponse(int slotId,
5906                                  int responseType, int serial, RIL_Errno e,
5907                                  void *response, size_t responseLen) {
5908 #if VDBG
5909     RLOGD("nvResetConfigResponse: serial %d", serial);
5910 #endif
5911
5912     if (radioService[slotId]->mRadioResponse != NULL) {
5913         RadioResponseInfo responseInfo = {};
5914         populateResponseInfo(responseInfo, serial, responseType, e);
5915         Return<void> retStatus
5916                 = radioService[slotId]->mRadioResponse->nvResetConfigResponse(responseInfo);
5917         radioService[slotId]->checkReturnStatus(retStatus);
5918     } else {
5919         RLOGE("nvResetConfigResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5920     }
5921
5922     return 0;
5923 }
5924
5925 int radio::setUiccSubscriptionResponse(int slotId,
5926                                        int responseType, int serial, RIL_Errno e,
5927                                        void *response, size_t responseLen) {
5928 #if VDBG
5929     RLOGD("setUiccSubscriptionResponse: serial %d", serial);
5930 #endif
5931
5932     if (radioService[slotId]->mRadioResponse != NULL) {
5933         RadioResponseInfo responseInfo = {};
5934         populateResponseInfo(responseInfo, serial, responseType, e);
5935         Return<void> retStatus
5936                 = radioService[slotId]->mRadioResponse->setUiccSubscriptionResponse(responseInfo);
5937         radioService[slotId]->checkReturnStatus(retStatus);
5938     } else {
5939         RLOGE("setUiccSubscriptionResponse: radioService[%d]->mRadioResponse == NULL",
5940                 slotId);
5941     }
5942
5943     return 0;
5944 }
5945
5946 int radio::setDataAllowedResponse(int slotId,
5947                                   int responseType, int serial, RIL_Errno e,
5948                                   void *response, size_t responseLen) {
5949 #if VDBG
5950     RLOGD("setDataAllowedResponse: serial %d", serial);
5951 #endif
5952
5953     if (radioService[slotId]->mRadioResponse != NULL) {
5954         RadioResponseInfo responseInfo = {};
5955         populateResponseInfo(responseInfo, serial, responseType, e);
5956         Return<void> retStatus
5957                 = radioService[slotId]->mRadioResponse->setDataAllowedResponse(responseInfo);
5958         radioService[slotId]->checkReturnStatus(retStatus);
5959     } else {
5960         RLOGE("setDataAllowedResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5961     }
5962
5963     return 0;
5964 }
5965
5966 int radio::getHardwareConfigResponse(int slotId,
5967                                      int responseType, int serial, RIL_Errno e,
5968                                      void *response, size_t responseLen) {
5969 #if VDBG
5970     RLOGD("getHardwareConfigResponse: serial %d", serial);
5971 #endif
5972
5973     if (radioService[slotId]->mRadioResponse != NULL) {
5974         RadioResponseInfo responseInfo = {};
5975         populateResponseInfo(responseInfo, serial, responseType, e);
5976
5977         hidl_vec<HardwareConfig> result;
5978         if ((response == NULL && responseLen != 0)
5979                 || responseLen % sizeof(RIL_HardwareConfig) != 0) {
5980             RLOGE("hardwareConfigChangedInd: invalid response");
5981             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5982         } else {
5983             convertRilHardwareConfigListToHal(response, responseLen, result);
5984         }
5985
5986         Return<void> retStatus = radioService[slotId]->mRadioResponse->getHardwareConfigResponse(
5987                 responseInfo, result);
5988         radioService[slotId]->checkReturnStatus(retStatus);
5989     } else {
5990         RLOGE("getHardwareConfigResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5991     }
5992
5993     return 0;
5994 }
5995
5996 int radio::requestIccSimAuthenticationResponse(int slotId,
5997                                                int responseType, int serial, RIL_Errno e,
5998                                                void *response, size_t responseLen) {
5999 #if VDBG
6000     RLOGD("requestIccSimAuthenticationResponse: serial %d", serial);
6001 #endif
6002
6003     if (radioService[slotId]->mRadioResponse != NULL) {
6004         RadioResponseInfo responseInfo = {};
6005         IccIoResult result = responseIccIo(responseInfo, serial, responseType, e, response,
6006                 responseLen);
6007
6008         Return<void> retStatus
6009                 = radioService[slotId]->mRadioResponse->requestIccSimAuthenticationResponse(
6010                 responseInfo, result);
6011         radioService[slotId]->checkReturnStatus(retStatus);
6012     } else {
6013         RLOGE("requestIccSimAuthenticationResponse: radioService[%d]->mRadioResponse "
6014                 "== NULL", slotId);
6015     }
6016
6017     return 0;
6018 }
6019
6020 int radio::setDataProfileResponse(int slotId,
6021                                   int responseType, int serial, RIL_Errno e,
6022                                   void *response, size_t responseLen) {
6023 #if VDBG
6024     RLOGD("setDataProfileResponse: serial %d", serial);
6025 #endif
6026
6027     if (radioService[slotId]->mRadioResponse != NULL) {
6028         RadioResponseInfo responseInfo = {};
6029         populateResponseInfo(responseInfo, serial, responseType, e);
6030         Return<void> retStatus
6031                 = radioService[slotId]->mRadioResponse->setDataProfileResponse(responseInfo);
6032         radioService[slotId]->checkReturnStatus(retStatus);
6033     } else {
6034         RLOGE("setDataProfileResponse: radioService[%d]->mRadioResponse == NULL", slotId);
6035     }
6036
6037     return 0;
6038 }
6039
6040 int radio::requestShutdownResponse(int slotId,
6041                                   int responseType, int serial, RIL_Errno e,
6042                                   void *response, size_t responseLen) {
6043 #if VDBG
6044     RLOGD("requestShutdownResponse: serial %d", serial);
6045 #endif
6046
6047     if (radioService[slotId]->mRadioResponse != NULL) {
6048         RadioResponseInfo responseInfo = {};
6049         populateResponseInfo(responseInfo, serial, responseType, e);
6050         Return<void> retStatus
6051                 = radioService[slotId]->mRadioResponse->requestShutdownResponse(responseInfo);
6052         radioService[slotId]->checkReturnStatus(retStatus);
6053     } else {
6054         RLOGE("requestShutdownResponse: radioService[%d]->mRadioResponse == NULL", slotId);
6055     }
6056
6057     return 0;
6058 }
6059
6060 void responseRadioCapability(RadioResponseInfo& responseInfo, int serial,
6061         int responseType, RIL_Errno e, void *response, size_t responseLen, RadioCapability& rc) {
6062     populateResponseInfo(responseInfo, serial, responseType, e);
6063
6064     if (response == NULL || responseLen != sizeof(RIL_RadioCapability)) {
6065         RLOGE("responseRadioCapability: Invalid response");
6066         if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
6067         rc.logicalModemUuid = hidl_string();
6068     } else {
6069         convertRilRadioCapabilityToHal(response, responseLen, rc);
6070     }
6071 }
6072
6073 int radio::getRadioCapabilityResponse(int slotId,
6074                                      int responseType, int serial, RIL_Errno e,
6075                                      void *response, size_t responseLen) {
6076 #if VDBG
6077     RLOGD("getRadioCapabilityResponse: serial %d", serial);
6078 #endif
6079
6080     if (radioService[slotId]->mRadioResponse != NULL) {
6081         RadioResponseInfo responseInfo = {};
6082         RadioCapability result = {};
6083         responseRadioCapability(responseInfo, serial, responseType, e, response, responseLen,
6084                 result);
6085         Return<void> retStatus = radioService[slotId]->mRadioResponse->getRadioCapabilityResponse(
6086                 responseInfo, result);
6087         radioService[slotId]->checkReturnStatus(retStatus);
6088     } else {
6089         RLOGE("getRadioCapabilityResponse: radioService[%d]->mRadioResponse == NULL", slotId);
6090     }
6091
6092     return 0;
6093 }
6094
6095 int radio::setRadioCapabilityResponse(int slotId,
6096                                      int responseType, int serial, RIL_Errno e,
6097                                      void *response, size_t responseLen) {
6098 #if VDBG
6099     RLOGD("setRadioCapabilityResponse: serial %d", serial);
6100 #endif
6101
6102     if (radioService[slotId]->mRadioResponse != NULL) {
6103         RadioResponseInfo responseInfo = {};
6104         RadioCapability result = {};
6105         responseRadioCapability(responseInfo, serial, responseType, e, response, responseLen,
6106                 result);
6107         Return<void> retStatus = radioService[slotId]->mRadioResponse->setRadioCapabilityResponse(
6108                 responseInfo, result);
6109         radioService[slotId]->checkReturnStatus(retStatus);
6110     } else {
6111         RLOGE("setRadioCapabilityResponse: radioService[%d]->mRadioResponse == NULL", slotId);
6112     }
6113
6114     return 0;
6115 }
6116
6117 LceStatusInfo responseLceStatusInfo(RadioResponseInfo& responseInfo, int serial, int responseType,
6118                                     RIL_Errno e, void *response, size_t responseLen) {
6119     populateResponseInfo(responseInfo, serial, responseType, e);
6120     LceStatusInfo result = {};
6121
6122     if (response == NULL || responseLen != sizeof(RIL_LceStatusInfo)) {
6123         RLOGE("Invalid response: NULL");
6124         if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
6125     } else {
6126         RIL_LceStatusInfo *resp = (RIL_LceStatusInfo *) response;
6127         result.lceStatus = (LceStatus) resp->lce_status;
6128         result.actualIntervalMs = (uint8_t) resp->actual_interval_ms;
6129     }
6130     return result;
6131 }
6132
6133 int radio::startLceServiceResponse(int slotId,
6134                                    int responseType, int serial, RIL_Errno e,
6135                                    void *response, size_t responseLen) {
6136 #if VDBG
6137     RLOGD("startLceServiceResponse: serial %d", serial);
6138 #endif
6139
6140     if (radioService[slotId]->mRadioResponse != NULL) {
6141         RadioResponseInfo responseInfo = {};
6142         LceStatusInfo result = responseLceStatusInfo(responseInfo, serial, responseType, e,
6143                 response, responseLen);
6144
6145         Return<void> retStatus
6146                 = radioService[slotId]->mRadioResponse->startLceServiceResponse(responseInfo,
6147                 result);
6148         radioService[slotId]->checkReturnStatus(retStatus);
6149     } else {
6150         RLOGE("startLceServiceResponse: radioService[%d]->mRadioResponse == NULL", slotId);
6151     }
6152
6153     return 0;
6154 }
6155
6156 int radio::stopLceServiceResponse(int slotId,
6157                                   int responseType, int serial, RIL_Errno e,
6158                                   void *response, size_t responseLen) {
6159 #if VDBG
6160     RLOGD("stopLceServiceResponse: serial %d", serial);
6161 #endif
6162
6163     if (radioService[slotId]->mRadioResponse != NULL) {
6164         RadioResponseInfo responseInfo = {};
6165         LceStatusInfo result = responseLceStatusInfo(responseInfo, serial, responseType, e,
6166                 response, responseLen);
6167
6168         Return<void> retStatus
6169                 = radioService[slotId]->mRadioResponse->stopLceServiceResponse(responseInfo,
6170                 result);
6171         radioService[slotId]->checkReturnStatus(retStatus);
6172     } else {
6173         RLOGE("stopLceServiceResponse: radioService[%d]->mRadioResponse == NULL", slotId);
6174     }
6175
6176     return 0;
6177 }
6178
6179 int radio::pullLceDataResponse(int slotId,
6180                                int responseType, int serial, RIL_Errno e,
6181                                void *response, size_t responseLen) {
6182 #if VDBG
6183     RLOGD("pullLceDataResponse: serial %d", serial);
6184 #endif
6185
6186     if (radioService[slotId]->mRadioResponse != NULL) {
6187         RadioResponseInfo responseInfo = {};
6188         populateResponseInfo(responseInfo, serial, responseType, e);
6189
6190         LceDataInfo result = {};
6191         if (response == NULL || responseLen != sizeof(RIL_LceDataInfo)) {
6192             RLOGE("pullLceDataResponse: Invalid response");
6193             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
6194         } else {
6195             convertRilLceDataInfoToHal(response, responseLen, result);
6196         }
6197
6198         Return<void> retStatus = radioService[slotId]->mRadioResponse->pullLceDataResponse(
6199                 responseInfo, result);
6200         radioService[slotId]->checkReturnStatus(retStatus);
6201     } else {
6202         RLOGE("pullLceDataResponse: radioService[%d]->mRadioResponse == NULL", slotId);
6203     }
6204
6205     return 0;
6206 }
6207
6208 int radio::getModemActivityInfoResponse(int slotId,
6209                                         int responseType, int serial, RIL_Errno e,
6210                                         void *response, size_t responseLen) {
6211 #if VDBG
6212     RLOGD("getModemActivityInfoResponse: serial %d", serial);
6213 #endif
6214
6215     if (radioService[slotId]->mRadioResponse != NULL) {
6216         RadioResponseInfo responseInfo = {};
6217         populateResponseInfo(responseInfo, serial, responseType, e);
6218         ActivityStatsInfo info;
6219         if (response == NULL || responseLen != sizeof(RIL_ActivityStatsInfo)) {
6220             RLOGE("getModemActivityInfoResponse Invalid response: NULL");
6221             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
6222         } else {
6223             RIL_ActivityStatsInfo *resp = (RIL_ActivityStatsInfo *)response;
6224             info.sleepModeTimeMs = resp->sleep_mode_time_ms;
6225             info.idleModeTimeMs = resp->idle_mode_time_ms;
6226             for(int i = 0; i < RIL_NUM_TX_POWER_LEVELS; i++) {
6227                 info.txmModetimeMs[i] = resp->tx_mode_time_ms[i];
6228             }
6229             info.rxModeTimeMs = resp->rx_mode_time_ms;
6230         }
6231
6232         Return<void> retStatus
6233                 = radioService[slotId]->mRadioResponse->getModemActivityInfoResponse(responseInfo,
6234                 info);
6235         radioService[slotId]->checkReturnStatus(retStatus);
6236     } else {
6237         RLOGE("getModemActivityInfoResponse: radioService[%d]->mRadioResponse == NULL",
6238                 slotId);
6239     }
6240
6241     return 0;
6242 }
6243
6244 int radio::setAllowedCarriersResponse(int slotId,
6245                                       int responseType, int serial, RIL_Errno e,
6246                                       void *response, size_t responseLen) {
6247 #if VDBG
6248     RLOGD("setAllowedCarriersResponse: serial %d", serial);
6249 #endif
6250
6251     if (radioService[slotId]->mRadioResponse != NULL) {
6252         RadioResponseInfo responseInfo = {};
6253         int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
6254         Return<void> retStatus
6255                 = radioService[slotId]->mRadioResponse->setAllowedCarriersResponse(responseInfo,
6256                 ret);
6257         radioService[slotId]->checkReturnStatus(retStatus);
6258     } else {
6259         RLOGE("setAllowedCarriersResponse: radioService[%d]->mRadioResponse == NULL",
6260                 slotId);
6261     }
6262
6263     return 0;
6264 }
6265
6266 int radio::getAllowedCarriersResponse(int slotId,
6267                                       int responseType, int serial, RIL_Errno e,
6268                                       void *response, size_t responseLen) {
6269 #if VDBG
6270     RLOGD("getAllowedCarriersResponse: serial %d", serial);
6271 #endif
6272
6273     if (radioService[slotId]->mRadioResponse != NULL) {
6274         RadioResponseInfo responseInfo = {};
6275         populateResponseInfo(responseInfo, serial, responseType, e);
6276         CarrierRestrictions carrierInfo = {};
6277         bool allAllowed = true;
6278         if (response == NULL) {
6279 #if VDBG
6280             RLOGD("getAllowedCarriersResponse response is NULL: all allowed");
6281 #endif
6282             carrierInfo.allowedCarriers.resize(0);
6283             carrierInfo.excludedCarriers.resize(0);
6284         } else if (responseLen != sizeof(RIL_CarrierRestrictions)) {
6285             RLOGE("getAllowedCarriersResponse Invalid response");
6286             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
6287         } else {
6288             RIL_CarrierRestrictions *pCr = (RIL_CarrierRestrictions *)response;
6289             if (pCr->len_allowed_carriers > 0 || pCr->len_excluded_carriers > 0) {
6290                 allAllowed = false;
6291             }
6292
6293             carrierInfo.allowedCarriers.resize(pCr->len_allowed_carriers);
6294             for(int i = 0; i < pCr->len_allowed_carriers; i++) {
6295                 RIL_Carrier *carrier = pCr->allowed_carriers + i;
6296                 carrierInfo.allowedCarriers[i].mcc = convertCharPtrToHidlString(carrier->mcc);
6297                 carrierInfo.allowedCarriers[i].mnc = convertCharPtrToHidlString(carrier->mnc);
6298                 carrierInfo.allowedCarriers[i].matchType = (CarrierMatchType) carrier->match_type;
6299                 carrierInfo.allowedCarriers[i].matchData =
6300                         convertCharPtrToHidlString(carrier->match_data);
6301             }
6302
6303             carrierInfo.excludedCarriers.resize(pCr->len_excluded_carriers);
6304             for(int i = 0; i < pCr->len_excluded_carriers; i++) {
6305                 RIL_Carrier *carrier = pCr->excluded_carriers + i;
6306                 carrierInfo.excludedCarriers[i].mcc = convertCharPtrToHidlString(carrier->mcc);
6307                 carrierInfo.excludedCarriers[i].mnc = convertCharPtrToHidlString(carrier->mnc);
6308                 carrierInfo.excludedCarriers[i].matchType = (CarrierMatchType) carrier->match_type;
6309                 carrierInfo.excludedCarriers[i].matchData =
6310                         convertCharPtrToHidlString(carrier->match_data);
6311             }
6312         }
6313
6314         Return<void> retStatus
6315                 = radioService[slotId]->mRadioResponse->getAllowedCarriersResponse(responseInfo,
6316                 allAllowed, carrierInfo);
6317         radioService[slotId]->checkReturnStatus(retStatus);
6318     } else {
6319         RLOGE("getAllowedCarriersResponse: radioService[%d]->mRadioResponse == NULL",
6320                 slotId);
6321     }
6322
6323     return 0;
6324 }
6325
6326 int radio::sendDeviceStateResponse(int slotId,
6327                               int responseType, int serial, RIL_Errno e,
6328                               void *response, size_t responselen) {
6329 #if VDBG
6330     RLOGD("sendDeviceStateResponse: serial %d", serial);
6331 #endif
6332
6333     if (radioService[slotId]->mRadioResponse != NULL) {
6334         RadioResponseInfo responseInfo = {};
6335         populateResponseInfo(responseInfo, serial, responseType, e);
6336         Return<void> retStatus
6337                 = radioService[slotId]->mRadioResponse->sendDeviceStateResponse(responseInfo);
6338         radioService[slotId]->checkReturnStatus(retStatus);
6339     } else {
6340         RLOGE("sendDeviceStateResponse: radioService[%d]->mRadioResponse == NULL", slotId);
6341     }
6342
6343     return 0;
6344 }
6345
6346 int radio::setIndicationFilterResponse(int slotId,
6347                               int responseType, int serial, RIL_Errno e,
6348                               void *response, size_t responselen) {
6349 #if VDBG
6350     RLOGD("setIndicationFilterResponse: serial %d", serial);
6351 #endif
6352
6353     if (radioService[slotId]->mRadioResponse != NULL) {
6354         RadioResponseInfo responseInfo = {};
6355         populateResponseInfo(responseInfo, serial, responseType, e);
6356         Return<void> retStatus
6357                 = radioService[slotId]->mRadioResponse->setIndicationFilterResponse(responseInfo);
6358         radioService[slotId]->checkReturnStatus(retStatus);
6359     } else {
6360         RLOGE("setIndicationFilterResponse: radioService[%d]->mRadioResponse == NULL",
6361                 slotId);
6362     }
6363
6364     return 0;
6365 }
6366
6367
6368 int radio::setSimCardPowerResponse(int slotId,
6369                                    int responseType, int serial, RIL_Errno e,
6370                                    void *response, size_t responseLen) {
6371 #if VDBG
6372     RLOGD("setSimCardPowerResponse: serial %d", serial);
6373 #endif
6374
6375     if (radioService[slotId]->mRadioResponse != NULL) {
6376         RadioResponseInfo responseInfo = {};
6377         populateResponseInfo(responseInfo, serial, responseType, e);
6378         Return<void> retStatus
6379                 = radioService[slotId]->mRadioResponse->setSimCardPowerResponse(responseInfo);
6380         radioService[slotId]->checkReturnStatus(retStatus);
6381     } else {
6382         RLOGE("setSimCardPowerResponse: radioService[%d]->mRadioResponse == NULL", slotId);
6383     }
6384
6385     return 0;
6386 }
6387
6388 int radio::sendRequestRawResponse(int slotId,
6389                                   int responseType, int serial, RIL_Errno e,
6390                                   void *response, size_t responseLen) {
6391 #if VDBG
6392    RLOGD("sendRequestRawResponse: serial %d", serial);
6393 #endif
6394
6395     if (oemHookService[slotId]->mOemHookResponse != NULL) {
6396         RadioResponseInfo responseInfo = {};
6397         populateResponseInfo(responseInfo, serial, responseType, e);
6398         hidl_vec<uint8_t> data;
6399
6400         if (response == NULL) {
6401             RLOGE("sendRequestRawResponse: Invalid response");
6402             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
6403         } else {
6404             data.setToExternal((uint8_t *) response, responseLen);
6405         }
6406         Return<void> retStatus = oemHookService[slotId]->mOemHookResponse->
6407                 sendRequestRawResponse(responseInfo, data);
6408         checkReturnStatus(slotId, retStatus, false);
6409     } else {
6410         RLOGE("sendRequestRawResponse: oemHookService[%d]->mOemHookResponse == NULL",
6411                 slotId);
6412     }
6413
6414     return 0;
6415 }
6416
6417 int radio::sendRequestStringsResponse(int slotId,
6418                                       int responseType, int serial, RIL_Errno e,
6419                                       void *response, size_t responseLen) {
6420 #if VDBG
6421     RLOGD("sendRequestStringsResponse: serial %d", serial);
6422 #endif
6423
6424     if (oemHookService[slotId]->mOemHookResponse != NULL) {
6425         RadioResponseInfo responseInfo = {};
6426         populateResponseInfo(responseInfo, serial, responseType, e);
6427         hidl_vec<hidl_string> data;
6428
6429         if ((response == NULL && responseLen != 0) || responseLen % sizeof(char *) != 0) {
6430             RLOGE("sendRequestStringsResponse Invalid response: NULL");
6431             if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
6432         } else {
6433             char **resp = (char **) response;
6434             int numStrings = responseLen / sizeof(char *);
6435             data.resize(numStrings);
6436             for (int i = 0; i < numStrings; i++) {
6437                 data[i] = convertCharPtrToHidlString(resp[i]);
6438             }
6439         }
6440         Return<void> retStatus
6441                 = oemHookService[slotId]->mOemHookResponse->sendRequestStringsResponse(
6442                 responseInfo, data);
6443         checkReturnStatus(slotId, retStatus, false);
6444     } else {
6445         RLOGE("sendRequestStringsResponse: oemHookService[%d]->mOemHookResponse == "
6446                 "NULL", slotId);
6447     }
6448
6449     return 0;
6450 }
6451
6452 // Radio Indication functions
6453
6454 RadioIndicationType convertIntToRadioIndicationType(int indicationType) {
6455     return indicationType == RESPONSE_UNSOLICITED ? (RadioIndicationType::UNSOLICITED) :
6456             (RadioIndicationType::UNSOLICITED_ACK_EXP);
6457 }
6458
6459 int radio::radioStateChangedInd(int slotId,
6460                                  int indicationType, int token, RIL_Errno e, void *response,
6461                                  size_t responseLen) {
6462     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6463         RadioState radioState =
6464                 (RadioState) CALL_ONSTATEREQUEST(slotId);
6465         RLOGD("radioStateChangedInd: radioState %d", radioState);
6466         Return<void> retStatus = radioService[slotId]->mRadioIndication->radioStateChanged(
6467                 convertIntToRadioIndicationType(indicationType), radioState);
6468         radioService[slotId]->checkReturnStatus(retStatus);
6469     } else {
6470         RLOGE("radioStateChangedInd: radioService[%d]->mRadioIndication == NULL", slotId);
6471     }
6472
6473     return 0;
6474 }
6475
6476 int radio::callStateChangedInd(int slotId,
6477                                int indicationType, int token, RIL_Errno e, void *response,
6478                                size_t responseLen) {
6479     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6480 #if VDBG
6481         RLOGD("callStateChangedInd");
6482 #endif
6483         Return<void> retStatus = radioService[slotId]->mRadioIndication->callStateChanged(
6484                 convertIntToRadioIndicationType(indicationType));
6485         radioService[slotId]->checkReturnStatus(retStatus);
6486     } else {
6487         RLOGE("callStateChangedInd: radioService[%d]->mRadioIndication == NULL", slotId);
6488     }
6489
6490     return 0;
6491 }
6492
6493 int radio::networkStateChangedInd(int slotId,
6494                                   int indicationType, int token, RIL_Errno e, void *response,
6495                                   size_t responseLen) {
6496     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6497 #if VDBG
6498         RLOGD("networkStateChangedInd");
6499 #endif
6500         Return<void> retStatus = radioService[slotId]->mRadioIndication->networkStateChanged(
6501                 convertIntToRadioIndicationType(indicationType));
6502         radioService[slotId]->checkReturnStatus(retStatus);
6503     } else {
6504         RLOGE("networkStateChangedInd: radioService[%d]->mRadioIndication == NULL",
6505                 slotId);
6506     }
6507
6508     return 0;
6509 }
6510
6511 uint8_t hexCharToInt(uint8_t c) {
6512     if (c >= '0' && c <= '9') return (c - '0');
6513     if (c >= 'A' && c <= 'F') return (c - 'A' + 10);
6514     if (c >= 'a' && c <= 'f') return (c - 'a' + 10);
6515
6516     return INVALID_HEX_CHAR;
6517 }
6518
6519 uint8_t * convertHexStringToBytes(void *response, size_t responseLen) {
6520     if (responseLen % 2 != 0) {
6521         return NULL;
6522     }
6523
6524     uint8_t *bytes = (uint8_t *)calloc(responseLen/2, sizeof(uint8_t));
6525     if (bytes == NULL) {
6526         RLOGE("convertHexStringToBytes: cannot allocate memory for bytes string");
6527         return NULL;
6528     }
6529     uint8_t *hexString = (uint8_t *)response;
6530
6531     for (size_t i = 0; i < responseLen; i += 2) {
6532         uint8_t hexChar1 = hexCharToInt(hexString[i]);
6533         uint8_t hexChar2 = hexCharToInt(hexString[i + 1]);
6534
6535         if (hexChar1 == INVALID_HEX_CHAR || hexChar2 == INVALID_HEX_CHAR) {
6536             RLOGE("convertHexStringToBytes: invalid hex char %d %d",
6537                     hexString[i], hexString[i + 1]);
6538             free(bytes);
6539             return NULL;
6540         }
6541         bytes[i/2] = ((hexChar1 << 4) | hexChar2);
6542     }
6543
6544     return bytes;
6545 }
6546
6547 int radio::newSmsInd(int slotId, int indicationType,
6548                      int token, RIL_Errno e, void *response, size_t responseLen) {
6549     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6550         if (response == NULL || responseLen == 0) {
6551             RLOGE("newSmsInd: invalid response");
6552             return 0;
6553         }
6554
6555         uint8_t *bytes = convertHexStringToBytes(response, responseLen);
6556         if (bytes == NULL) {
6557             RLOGE("newSmsInd: convertHexStringToBytes failed");
6558             return 0;
6559         }
6560
6561         hidl_vec<uint8_t> pdu;
6562         pdu.setToExternal(bytes, responseLen/2);
6563 #if VDBG
6564         RLOGD("newSmsInd");
6565 #endif
6566         Return<void> retStatus = radioService[slotId]->mRadioIndication->newSms(
6567                 convertIntToRadioIndicationType(indicationType), pdu);
6568         radioService[slotId]->checkReturnStatus(retStatus);
6569         free(bytes);
6570     } else {
6571         RLOGE("newSmsInd: radioService[%d]->mRadioIndication == NULL", slotId);
6572     }
6573
6574     return 0;
6575 }
6576
6577 int radio::newSmsStatusReportInd(int slotId,
6578                                  int indicationType, int token, RIL_Errno e, void *response,
6579                                  size_t responseLen) {
6580     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6581         if (response == NULL || responseLen == 0) {
6582             RLOGE("newSmsStatusReportInd: invalid response");
6583             return 0;
6584         }
6585
6586         uint8_t *bytes = convertHexStringToBytes(response, responseLen);
6587         if (bytes == NULL) {
6588             RLOGE("newSmsStatusReportInd: convertHexStringToBytes failed");
6589             return 0;
6590         }
6591
6592         hidl_vec<uint8_t> pdu;
6593         pdu.setToExternal(bytes, responseLen/2);
6594 #if VDBG
6595         RLOGD("newSmsStatusReportInd");
6596 #endif
6597         Return<void> retStatus = radioService[slotId]->mRadioIndication->newSmsStatusReport(
6598                 convertIntToRadioIndicationType(indicationType), pdu);
6599         radioService[slotId]->checkReturnStatus(retStatus);
6600         free(bytes);
6601     } else {
6602         RLOGE("newSmsStatusReportInd: radioService[%d]->mRadioIndication == NULL", slotId);
6603     }
6604
6605     return 0;
6606 }
6607
6608 int radio::newSmsOnSimInd(int slotId, int indicationType,
6609                           int token, RIL_Errno e, void *response, size_t responseLen) {
6610     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6611         if (response == NULL || responseLen != sizeof(int)) {
6612             RLOGE("newSmsOnSimInd: invalid response");
6613             return 0;
6614         }
6615         int32_t recordNumber = ((int32_t *) response)[0];
6616 #if VDBG
6617         RLOGD("newSmsOnSimInd: slotIndex %d", recordNumber);
6618 #endif
6619         Return<void> retStatus = radioService[slotId]->mRadioIndication->newSmsOnSim(
6620                 convertIntToRadioIndicationType(indicationType), recordNumber);
6621         radioService[slotId]->checkReturnStatus(retStatus);
6622     } else {
6623         RLOGE("newSmsOnSimInd: radioService[%d]->mRadioIndication == NULL", slotId);
6624     }
6625
6626     return 0;
6627 }
6628
6629 int radio::onUssdInd(int slotId, int indicationType,
6630                      int token, RIL_Errno e, void *response, size_t responseLen) {
6631     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6632         if (response == NULL || responseLen != 2 * sizeof(char *)) {
6633             RLOGE("onUssdInd: invalid response");
6634             return 0;
6635         }
6636         char **strings = (char **) response;
6637         char *mode = strings[0];
6638         hidl_string msg = convertCharPtrToHidlString(strings[1]);
6639         UssdModeType modeType = (UssdModeType) atoi(mode);
6640 #if VDBG
6641         RLOGD("onUssdInd: mode %s", mode);
6642 #endif
6643         Return<void> retStatus = radioService[slotId]->mRadioIndication->onUssd(
6644                 convertIntToRadioIndicationType(indicationType), modeType, msg);
6645         radioService[slotId]->checkReturnStatus(retStatus);
6646     } else {
6647         RLOGE("onUssdInd: radioService[%d]->mRadioIndication == NULL", slotId);
6648     }
6649
6650     return 0;
6651 }
6652
6653 int radio::nitzTimeReceivedInd(int slotId,
6654                                int indicationType, int token, RIL_Errno e, void *response,
6655                                size_t responseLen) {
6656     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6657         if (response == NULL || responseLen == 0) {
6658             RLOGE("nitzTimeReceivedInd: invalid response");
6659             return 0;
6660         }
6661         hidl_string nitzTime = convertCharPtrToHidlString((char *) response);
6662         int64_t timeReceived = android::elapsedRealtime();
6663 #if VDBG
6664         RLOGD("nitzTimeReceivedInd: nitzTime %s receivedTime %" PRId64, nitzTime.c_str(),
6665                 timeReceived);
6666 #endif
6667         Return<void> retStatus = radioService[slotId]->mRadioIndication->nitzTimeReceived(
6668                 convertIntToRadioIndicationType(indicationType), nitzTime, timeReceived);
6669         radioService[slotId]->checkReturnStatus(retStatus);
6670     } else {
6671         RLOGE("nitzTimeReceivedInd: radioService[%d]->mRadioIndication == NULL", slotId);
6672         return -1;
6673     }
6674
6675     return 0;
6676 }
6677
6678 void convertRilSignalStrengthToHal(void *response, size_t responseLen,
6679         SignalStrength& signalStrength) {
6680     RIL_SignalStrength_v10 *rilSignalStrength = (RIL_SignalStrength_v10 *) response;
6681
6682     // Fixup LTE for backwards compatibility
6683     // signalStrength: -1 -> 99
6684     if (rilSignalStrength->LTE_SignalStrength.signalStrength == -1) {
6685         rilSignalStrength->LTE_SignalStrength.signalStrength = 99;
6686     }
6687     // rsrp: -1 -> INT_MAX all other negative value to positive.
6688     // So remap here
6689     if (rilSignalStrength->LTE_SignalStrength.rsrp == -1) {
6690         rilSignalStrength->LTE_SignalStrength.rsrp = INT_MAX;
6691     } else if (rilSignalStrength->LTE_SignalStrength.rsrp < -1) {
6692         rilSignalStrength->LTE_SignalStrength.rsrp = -rilSignalStrength->LTE_SignalStrength.rsrp;
6693     }
6694     // rsrq: -1 -> INT_MAX
6695     if (rilSignalStrength->LTE_SignalStrength.rsrq == -1) {
6696         rilSignalStrength->LTE_SignalStrength.rsrq = INT_MAX;
6697     }
6698     // Not remapping rssnr is already using INT_MAX
6699     // cqi: -1 -> INT_MAX
6700     if (rilSignalStrength->LTE_SignalStrength.cqi == -1) {
6701         rilSignalStrength->LTE_SignalStrength.cqi = INT_MAX;
6702     }
6703
6704     signalStrength.gw.signalStrength = rilSignalStrength->GW_SignalStrength.signalStrength;
6705     signalStrength.gw.bitErrorRate = rilSignalStrength->GW_SignalStrength.bitErrorRate;
6706     signalStrength.cdma.dbm = rilSignalStrength->CDMA_SignalStrength.dbm;
6707     signalStrength.cdma.ecio = rilSignalStrength->CDMA_SignalStrength.ecio;
6708     signalStrength.evdo.dbm = rilSignalStrength->EVDO_SignalStrength.dbm;
6709     signalStrength.evdo.ecio = rilSignalStrength->EVDO_SignalStrength.ecio;
6710     signalStrength.evdo.signalNoiseRatio =
6711             rilSignalStrength->EVDO_SignalStrength.signalNoiseRatio;
6712     signalStrength.lte.signalStrength = rilSignalStrength->LTE_SignalStrength.signalStrength;
6713     signalStrength.lte.rsrp = rilSignalStrength->LTE_SignalStrength.rsrp;
6714     signalStrength.lte.rsrq = rilSignalStrength->LTE_SignalStrength.rsrq;
6715     signalStrength.lte.rssnr = rilSignalStrength->LTE_SignalStrength.rssnr;
6716     signalStrength.lte.cqi = rilSignalStrength->LTE_SignalStrength.cqi;
6717     signalStrength.lte.timingAdvance = rilSignalStrength->LTE_SignalStrength.timingAdvance;
6718     signalStrength.tdScdma.rscp = rilSignalStrength->TD_SCDMA_SignalStrength.rscp;
6719 }
6720
6721 int radio::currentSignalStrengthInd(int slotId,
6722                                     int indicationType, int token, RIL_Errno e,
6723                                     void *response, size_t responseLen) {
6724     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6725         if (response == NULL || responseLen != sizeof(RIL_SignalStrength_v10)) {
6726             RLOGE("currentSignalStrengthInd: invalid response");
6727             return 0;
6728         }
6729
6730         SignalStrength signalStrength = {};
6731         convertRilSignalStrengthToHal(response, responseLen, signalStrength);
6732
6733 #if VDBG
6734         RLOGD("currentSignalStrengthInd");
6735 #endif
6736         Return<void> retStatus = radioService[slotId]->mRadioIndication->currentSignalStrength(
6737                 convertIntToRadioIndicationType(indicationType), signalStrength);
6738         radioService[slotId]->checkReturnStatus(retStatus);
6739     } else {
6740         RLOGE("currentSignalStrengthInd: radioService[%d]->mRadioIndication == NULL",
6741                 slotId);
6742     }
6743
6744     return 0;
6745 }
6746
6747 void convertRilDataCallToHal(RIL_Data_Call_Response_v11 *dcResponse,
6748         SetupDataCallResult& dcResult) {
6749     dcResult.status = (DataCallFailCause) dcResponse->status;
6750     dcResult.suggestedRetryTime = dcResponse->suggestedRetryTime;
6751     dcResult.cid = dcResponse->cid;
6752     dcResult.active = dcResponse->active;
6753     dcResult.type = convertCharPtrToHidlString(dcResponse->type);
6754     dcResult.ifname = convertCharPtrToHidlString(dcResponse->ifname);
6755     dcResult.addresses = convertCharPtrToHidlString(dcResponse->addresses);
6756     dcResult.dnses = convertCharPtrToHidlString(dcResponse->dnses);
6757     dcResult.gateways = convertCharPtrToHidlString(dcResponse->gateways);
6758     dcResult.pcscf = convertCharPtrToHidlString(dcResponse->pcscf);
6759     dcResult.mtu = dcResponse->mtu;
6760 }
6761
6762 void convertRilDataCallListToHal(void *response, size_t responseLen,
6763         hidl_vec<SetupDataCallResult>& dcResultList) {
6764     int num = responseLen / sizeof(RIL_Data_Call_Response_v11);
6765
6766     RIL_Data_Call_Response_v11 *dcResponse = (RIL_Data_Call_Response_v11 *) response;
6767     dcResultList.resize(num);
6768     for (int i = 0; i < num; i++) {
6769         convertRilDataCallToHal(&dcResponse[i], dcResultList[i]);
6770     }
6771 }
6772
6773 int radio::dataCallListChangedInd(int slotId,
6774                                   int indicationType, int token, RIL_Errno e, void *response,
6775                                   size_t responseLen) {
6776     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6777         if ((response == NULL && responseLen != 0)
6778                 || responseLen % sizeof(RIL_Data_Call_Response_v11) != 0) {
6779             RLOGE("dataCallListChangedInd: invalid response");
6780             return 0;
6781         }
6782         hidl_vec<SetupDataCallResult> dcList;
6783         convertRilDataCallListToHal(response, responseLen, dcList);
6784 #if VDBG
6785         RLOGD("dataCallListChangedInd");
6786 #endif
6787         Return<void> retStatus = radioService[slotId]->mRadioIndication->dataCallListChanged(
6788                 convertIntToRadioIndicationType(indicationType), dcList);
6789         radioService[slotId]->checkReturnStatus(retStatus);
6790     } else {
6791         RLOGE("dataCallListChangedInd: radioService[%d]->mRadioIndication == NULL", slotId);
6792     }
6793
6794     return 0;
6795 }
6796
6797 int radio::suppSvcNotifyInd(int slotId, int indicationType,
6798                             int token, RIL_Errno e, void *response, size_t responseLen) {
6799     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6800         if (response == NULL || responseLen != sizeof(RIL_SuppSvcNotification)) {
6801             RLOGE("suppSvcNotifyInd: invalid response");
6802             return 0;
6803         }
6804
6805         SuppSvcNotification suppSvc = {};
6806         RIL_SuppSvcNotification *ssn = (RIL_SuppSvcNotification *) response;
6807         suppSvc.isMT = ssn->notificationType;
6808         suppSvc.code = ssn->code;
6809         suppSvc.index = ssn->index;
6810         suppSvc.type = ssn->type;
6811         suppSvc.number = convertCharPtrToHidlString(ssn->number);
6812
6813 #if VDBG
6814         RLOGD("suppSvcNotifyInd: isMT %d code %d index %d type %d",
6815                 suppSvc.isMT, suppSvc.code, suppSvc.index, suppSvc.type);
6816 #endif
6817         Return<void> retStatus = radioService[slotId]->mRadioIndication->suppSvcNotify(
6818                 convertIntToRadioIndicationType(indicationType), suppSvc);
6819         radioService[slotId]->checkReturnStatus(retStatus);
6820     } else {
6821         RLOGE("suppSvcNotifyInd: radioService[%d]->mRadioIndication == NULL", slotId);
6822     }
6823
6824     return 0;
6825 }
6826
6827 int radio::stkSessionEndInd(int slotId, int indicationType,
6828                             int token, RIL_Errno e, void *response, size_t responseLen) {
6829     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6830 #if VDBG
6831         RLOGD("stkSessionEndInd");
6832 #endif
6833         Return<void> retStatus = radioService[slotId]->mRadioIndication->stkSessionEnd(
6834                 convertIntToRadioIndicationType(indicationType));
6835         radioService[slotId]->checkReturnStatus(retStatus);
6836     } else {
6837         RLOGE("stkSessionEndInd: radioService[%d]->mRadioIndication == NULL", slotId);
6838     }
6839
6840     return 0;
6841 }
6842
6843 int radio::stkProactiveCommandInd(int slotId,
6844                                   int indicationType, int token, RIL_Errno e, void *response,
6845                                   size_t responseLen) {
6846     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6847         if (response == NULL || responseLen == 0) {
6848             RLOGE("stkProactiveCommandInd: invalid response");
6849             return 0;
6850         }
6851 #if VDBG
6852         RLOGD("stkProactiveCommandInd");
6853 #endif
6854         Return<void> retStatus = radioService[slotId]->mRadioIndication->stkProactiveCommand(
6855                 convertIntToRadioIndicationType(indicationType),
6856                 convertCharPtrToHidlString((char *) response));
6857         radioService[slotId]->checkReturnStatus(retStatus);
6858     } else {
6859         RLOGE("stkProactiveCommandInd: radioService[%d]->mRadioIndication == NULL", slotId);
6860     }
6861
6862     return 0;
6863 }
6864
6865 int radio::stkEventNotifyInd(int slotId, int indicationType,
6866                              int token, RIL_Errno e, void *response, size_t responseLen) {
6867     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6868         if (response == NULL || responseLen == 0) {
6869             RLOGE("stkEventNotifyInd: invalid response");
6870             return 0;
6871         }
6872 #if VDBG
6873         RLOGD("stkEventNotifyInd");
6874 #endif
6875         Return<void> retStatus = radioService[slotId]->mRadioIndication->stkEventNotify(
6876                 convertIntToRadioIndicationType(indicationType),
6877                 convertCharPtrToHidlString((char *) response));
6878         radioService[slotId]->checkReturnStatus(retStatus);
6879     } else {
6880         RLOGE("stkEventNotifyInd: radioService[%d]->mRadioIndication == NULL", slotId);
6881     }
6882
6883     return 0;
6884 }
6885
6886 int radio::stkCallSetupInd(int slotId, int indicationType,
6887                            int token, RIL_Errno e, void *response, size_t responseLen) {
6888     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6889         if (response == NULL || responseLen != sizeof(int)) {
6890             RLOGE("stkCallSetupInd: invalid response");
6891             return 0;
6892         }
6893         int32_t timeout = ((int32_t *) response)[0];
6894 #if VDBG
6895         RLOGD("stkCallSetupInd: timeout %d", timeout);
6896 #endif
6897         Return<void> retStatus = radioService[slotId]->mRadioIndication->stkCallSetup(
6898                 convertIntToRadioIndicationType(indicationType), timeout);
6899         radioService[slotId]->checkReturnStatus(retStatus);
6900     } else {
6901         RLOGE("stkCallSetupInd: radioService[%d]->mRadioIndication == NULL", slotId);
6902     }
6903
6904     return 0;
6905 }
6906
6907 int radio::simSmsStorageFullInd(int slotId,
6908                                 int indicationType, int token, RIL_Errno e, void *response,
6909                                 size_t responseLen) {
6910     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6911 #if VDBG
6912         RLOGD("simSmsStorageFullInd");
6913 #endif
6914         Return<void> retStatus = radioService[slotId]->mRadioIndication->simSmsStorageFull(
6915                 convertIntToRadioIndicationType(indicationType));
6916         radioService[slotId]->checkReturnStatus(retStatus);
6917     } else {
6918         RLOGE("simSmsStorageFullInd: radioService[%d]->mRadioIndication == NULL", slotId);
6919     }
6920
6921     return 0;
6922 }
6923
6924 int radio::simRefreshInd(int slotId, int indicationType,
6925                          int token, RIL_Errno e, void *response, size_t responseLen) {
6926     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6927         if (response == NULL || responseLen != sizeof(RIL_SimRefreshResponse_v7)) {
6928             RLOGE("simRefreshInd: invalid response");
6929             return 0;
6930         }
6931
6932         SimRefreshResult refreshResult = {};
6933         RIL_SimRefreshResponse_v7 *simRefreshResponse = ((RIL_SimRefreshResponse_v7 *) response);
6934         refreshResult.type =
6935                 (android::hardware::radio::V1_0::SimRefreshType) simRefreshResponse->result;
6936         refreshResult.efId = simRefreshResponse->ef_id;
6937         refreshResult.aid = convertCharPtrToHidlString(simRefreshResponse->aid);
6938
6939 #if VDBG
6940         RLOGD("simRefreshInd: type %d efId %d", refreshResult.type, refreshResult.efId);
6941 #endif
6942         Return<void> retStatus = radioService[slotId]->mRadioIndication->simRefresh(
6943                 convertIntToRadioIndicationType(indicationType), refreshResult);
6944         radioService[slotId]->checkReturnStatus(retStatus);
6945     } else {
6946         RLOGE("simRefreshInd: radioService[%d]->mRadioIndication == NULL", slotId);
6947     }
6948
6949     return 0;
6950 }
6951
6952 void convertRilCdmaSignalInfoRecordToHal(RIL_CDMA_SignalInfoRecord *signalInfoRecord,
6953         CdmaSignalInfoRecord& record) {
6954     record.isPresent = signalInfoRecord->isPresent;
6955     record.signalType = signalInfoRecord->signalType;
6956     record.alertPitch = signalInfoRecord->alertPitch;
6957     record.signal = signalInfoRecord->signal;
6958 }
6959
6960 int radio::callRingInd(int slotId, int indicationType,
6961                        int token, RIL_Errno e, void *response, size_t responseLen) {
6962     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6963         bool isGsm;
6964         CdmaSignalInfoRecord record = {};
6965         if (response == NULL || responseLen == 0) {
6966             isGsm = true;
6967         } else {
6968             isGsm = false;
6969             if (responseLen != sizeof (RIL_CDMA_SignalInfoRecord)) {
6970                 RLOGE("callRingInd: invalid response");
6971                 return 0;
6972             }
6973             convertRilCdmaSignalInfoRecordToHal((RIL_CDMA_SignalInfoRecord *) response, record);
6974         }
6975
6976 #if VDBG
6977         RLOGD("callRingInd: isGsm %d", isGsm);
6978 #endif
6979         Return<void> retStatus = radioService[slotId]->mRadioIndication->callRing(
6980                 convertIntToRadioIndicationType(indicationType), isGsm, record);
6981         radioService[slotId]->checkReturnStatus(retStatus);
6982     } else {
6983         RLOGE("callRingInd: radioService[%d]->mRadioIndication == NULL", slotId);
6984     }
6985
6986     return 0;
6987 }
6988
6989 int radio::simStatusChangedInd(int slotId,
6990                                int indicationType, int token, RIL_Errno e, void *response,
6991                                size_t responseLen) {
6992     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6993 #if VDBG
6994         RLOGD("simStatusChangedInd");
6995 #endif
6996         Return<void> retStatus = radioService[slotId]->mRadioIndication->simStatusChanged(
6997                 convertIntToRadioIndicationType(indicationType));
6998         radioService[slotId]->checkReturnStatus(retStatus);
6999     } else {
7000         RLOGE("simStatusChangedInd: radioService[%d]->mRadioIndication == NULL", slotId);
7001     }
7002
7003     return 0;
7004 }
7005
7006 int radio::cdmaNewSmsInd(int slotId, int indicationType,
7007                          int token, RIL_Errno e, void *response, size_t responseLen) {
7008     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7009         if (response == NULL || responseLen != sizeof(RIL_CDMA_SMS_Message)) {
7010             RLOGE("cdmaNewSmsInd: invalid response");
7011             return 0;
7012         }
7013
7014         CdmaSmsMessage msg = {};
7015         RIL_CDMA_SMS_Message *rilMsg = (RIL_CDMA_SMS_Message *) response;
7016         msg.teleserviceId = rilMsg->uTeleserviceID;
7017         msg.isServicePresent = rilMsg->bIsServicePresent;
7018         msg.serviceCategory = rilMsg->uServicecategory;
7019         msg.address.digitMode =
7020                 (android::hardware::radio::V1_0::CdmaSmsDigitMode) rilMsg->sAddress.digit_mode;
7021         msg.address.numberMode =
7022                 (android::hardware::radio::V1_0::CdmaSmsNumberMode) rilMsg->sAddress.number_mode;
7023         msg.address.numberType =
7024                 (android::hardware::radio::V1_0::CdmaSmsNumberType) rilMsg->sAddress.number_type;
7025         msg.address.numberPlan =
7026                 (android::hardware::radio::V1_0::CdmaSmsNumberPlan) rilMsg->sAddress.number_plan;
7027
7028         int digitLimit = MIN((rilMsg->sAddress.number_of_digits), RIL_CDMA_SMS_ADDRESS_MAX);
7029         msg.address.digits.setToExternal(rilMsg->sAddress.digits, digitLimit);
7030
7031         msg.subAddress.subaddressType = (android::hardware::radio::V1_0::CdmaSmsSubaddressType)
7032                 rilMsg->sSubAddress.subaddressType;
7033         msg.subAddress.odd = rilMsg->sSubAddress.odd;
7034
7035         digitLimit= MIN((rilMsg->sSubAddress.number_of_digits), RIL_CDMA_SMS_SUBADDRESS_MAX);
7036         msg.subAddress.digits.setToExternal(rilMsg->sSubAddress.digits, digitLimit);
7037
7038         digitLimit = MIN((rilMsg->uBearerDataLen), RIL_CDMA_SMS_BEARER_DATA_MAX);
7039         msg.bearerData.setToExternal(rilMsg->aBearerData, digitLimit);
7040
7041 #if VDBG
7042         RLOGD("cdmaNewSmsInd");
7043 #endif
7044         Return<void> retStatus = radioService[slotId]->mRadioIndication->cdmaNewSms(
7045                 convertIntToRadioIndicationType(indicationType), msg);
7046         radioService[slotId]->checkReturnStatus(retStatus);
7047     } else {
7048         RLOGE("cdmaNewSmsInd: radioService[%d]->mRadioIndication == NULL", slotId);
7049     }
7050
7051     return 0;
7052 }
7053
7054 int radio::newBroadcastSmsInd(int slotId,
7055                               int indicationType, int token, RIL_Errno e, void *response,
7056                               size_t responseLen) {
7057     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7058         if (response == NULL || responseLen == 0) {
7059             RLOGE("newBroadcastSmsInd: invalid response");
7060             return 0;
7061         }
7062
7063         hidl_vec<uint8_t> data;
7064         data.setToExternal((uint8_t *) response, responseLen);
7065 #if VDBG
7066         RLOGD("newBroadcastSmsInd");
7067 #endif
7068         Return<void> retStatus = radioService[slotId]->mRadioIndication->newBroadcastSms(
7069                 convertIntToRadioIndicationType(indicationType), data);
7070         radioService[slotId]->checkReturnStatus(retStatus);
7071     } else {
7072         RLOGE("newBroadcastSmsInd: radioService[%d]->mRadioIndication == NULL", slotId);
7073     }
7074
7075     return 0;
7076 }
7077
7078 int radio::cdmaRuimSmsStorageFullInd(int slotId,
7079                                      int indicationType, int token, RIL_Errno e, void *response,
7080                                      size_t responseLen) {
7081     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7082 #if VDBG
7083         RLOGD("cdmaRuimSmsStorageFullInd");
7084 #endif
7085         Return<void> retStatus = radioService[slotId]->mRadioIndication->cdmaRuimSmsStorageFull(
7086                 convertIntToRadioIndicationType(indicationType));
7087         radioService[slotId]->checkReturnStatus(retStatus);
7088     } else {
7089         RLOGE("cdmaRuimSmsStorageFullInd: radioService[%d]->mRadioIndication == NULL",
7090                 slotId);
7091     }
7092
7093     return 0;
7094 }
7095
7096 int radio::restrictedStateChangedInd(int slotId,
7097                                      int indicationType, int token, RIL_Errno e, void *response,
7098                                      size_t responseLen) {
7099     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7100         if (response == NULL || responseLen != sizeof(int)) {
7101             RLOGE("restrictedStateChangedInd: invalid response");
7102             return 0;
7103         }
7104         int32_t state = ((int32_t *) response)[0];
7105 #if VDBG
7106         RLOGD("restrictedStateChangedInd: state %d", state);
7107 #endif
7108         Return<void> retStatus = radioService[slotId]->mRadioIndication->restrictedStateChanged(
7109                 convertIntToRadioIndicationType(indicationType), (PhoneRestrictedState) state);
7110         radioService[slotId]->checkReturnStatus(retStatus);
7111     } else {
7112         RLOGE("restrictedStateChangedInd: radioService[%d]->mRadioIndication == NULL",
7113                 slotId);
7114     }
7115
7116     return 0;
7117 }
7118
7119 int radio::enterEmergencyCallbackModeInd(int slotId,
7120                                          int indicationType, int token, RIL_Errno e, void *response,
7121                                          size_t responseLen) {
7122     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7123 #if VDBG
7124         RLOGD("enterEmergencyCallbackModeInd");
7125 #endif
7126         Return<void> retStatus = radioService[slotId]->mRadioIndication->enterEmergencyCallbackMode(
7127                 convertIntToRadioIndicationType(indicationType));
7128         radioService[slotId]->checkReturnStatus(retStatus);
7129     } else {
7130         RLOGE("enterEmergencyCallbackModeInd: radioService[%d]->mRadioIndication == NULL",
7131                 slotId);
7132     }
7133
7134     return 0;
7135 }
7136
7137 int radio::cdmaCallWaitingInd(int slotId,
7138                               int indicationType, int token, RIL_Errno e, void *response,
7139                               size_t responseLen) {
7140     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7141         if (response == NULL || responseLen != sizeof(RIL_CDMA_CallWaiting_v6)) {
7142             RLOGE("cdmaCallWaitingInd: invalid response");
7143             return 0;
7144         }
7145
7146         CdmaCallWaiting callWaitingRecord = {};
7147         RIL_CDMA_CallWaiting_v6 *callWaitingRil = ((RIL_CDMA_CallWaiting_v6 *) response);
7148         callWaitingRecord.number = convertCharPtrToHidlString(callWaitingRil->number);
7149         callWaitingRecord.numberPresentation =
7150                 (CdmaCallWaitingNumberPresentation) callWaitingRil->numberPresentation;
7151         callWaitingRecord.name = convertCharPtrToHidlString(callWaitingRil->name);
7152         convertRilCdmaSignalInfoRecordToHal(&callWaitingRil->signalInfoRecord,
7153                 callWaitingRecord.signalInfoRecord);
7154         callWaitingRecord.numberType = (CdmaCallWaitingNumberType) callWaitingRil->number_type;
7155         callWaitingRecord.numberPlan = (CdmaCallWaitingNumberPlan) callWaitingRil->number_plan;
7156
7157 #if VDBG
7158         RLOGD("cdmaCallWaitingInd");
7159 #endif
7160         Return<void> retStatus = radioService[slotId]->mRadioIndication->cdmaCallWaiting(
7161                 convertIntToRadioIndicationType(indicationType), callWaitingRecord);
7162         radioService[slotId]->checkReturnStatus(retStatus);
7163     } else {
7164         RLOGE("cdmaCallWaitingInd: radioService[%d]->mRadioIndication == NULL", slotId);
7165     }
7166
7167     return 0;
7168 }
7169
7170 int radio::cdmaOtaProvisionStatusInd(int slotId,
7171                                      int indicationType, int token, RIL_Errno e, void *response,
7172                                      size_t responseLen) {
7173     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7174         if (response == NULL || responseLen != sizeof(int)) {
7175             RLOGE("cdmaOtaProvisionStatusInd: invalid response");
7176             return 0;
7177         }
7178         int32_t status = ((int32_t *) response)[0];
7179 #if VDBG
7180         RLOGD("cdmaOtaProvisionStatusInd: status %d", status);
7181 #endif
7182         Return<void> retStatus = radioService[slotId]->mRadioIndication->cdmaOtaProvisionStatus(
7183                 convertIntToRadioIndicationType(indicationType), (CdmaOtaProvisionStatus) status);
7184         radioService[slotId]->checkReturnStatus(retStatus);
7185     } else {
7186         RLOGE("cdmaOtaProvisionStatusInd: radioService[%d]->mRadioIndication == NULL",
7187                 slotId);
7188     }
7189
7190     return 0;
7191 }
7192
7193 int radio::cdmaInfoRecInd(int slotId,
7194                           int indicationType, int token, RIL_Errno e, void *response,
7195                           size_t responseLen) {
7196     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7197         if (response == NULL || responseLen != sizeof(RIL_CDMA_InformationRecords)) {
7198             RLOGE("cdmaInfoRecInd: invalid response");
7199             return 0;
7200         }
7201
7202         CdmaInformationRecords records = {};
7203         RIL_CDMA_InformationRecords *recordsRil = (RIL_CDMA_InformationRecords *) response;
7204
7205         char* string8 = NULL;
7206         int num = MIN(recordsRil->numberOfInfoRecs, RIL_CDMA_MAX_NUMBER_OF_INFO_RECS);
7207         if (recordsRil->numberOfInfoRecs > RIL_CDMA_MAX_NUMBER_OF_INFO_RECS) {
7208             RLOGE("cdmaInfoRecInd: received %d recs which is more than %d, dropping "
7209                     "additional ones", recordsRil->numberOfInfoRecs,
7210                     RIL_CDMA_MAX_NUMBER_OF_INFO_RECS);
7211         }
7212         records.infoRec.resize(num);
7213         for (int i = 0 ; i < num ; i++) {
7214             CdmaInformationRecord *record = &records.infoRec[i];
7215             RIL_CDMA_InformationRecord *infoRec = &recordsRil->infoRec[i];
7216             record->name = (CdmaInfoRecName) infoRec->name;
7217             // All vectors should be size 0 except one which will be size 1. Set everything to
7218             // size 0 initially.
7219             record->display.resize(0);
7220             record->number.resize(0);
7221             record->signal.resize(0);
7222             record->redir.resize(0);
7223             record->lineCtrl.resize(0);
7224             record->clir.resize(0);
7225             record->audioCtrl.resize(0);
7226             switch (infoRec->name) {
7227                 case RIL_CDMA_DISPLAY_INFO_REC:
7228                 case RIL_CDMA_EXTENDED_DISPLAY_INFO_REC: {
7229                     if (infoRec->rec.display.alpha_len > CDMA_ALPHA_INFO_BUFFER_LENGTH) {
7230                         RLOGE("cdmaInfoRecInd: invalid display info response length %d "
7231                                 "expected not more than %d", (int) infoRec->rec.display.alpha_len,
7232                                 CDMA_ALPHA_INFO_BUFFER_LENGTH);
7233                         return 0;
7234                     }
7235                     string8 = (char*) malloc((infoRec->rec.display.alpha_len + 1) * sizeof(char));
7236                     if (string8 == NULL) {
7237                         RLOGE("cdmaInfoRecInd: Memory allocation failed for "
7238                                 "responseCdmaInformationRecords");
7239                         return 0;
7240                     }
7241                     memcpy(string8, infoRec->rec.display.alpha_buf, infoRec->rec.display.alpha_len);
7242                     string8[(int)infoRec->rec.display.alpha_len] = '\0';
7243
7244                     record->display.resize(1);
7245                     record->display[0].alphaBuf = string8;
7246                     free(string8);
7247                     string8 = NULL;
7248                     break;
7249                 }
7250
7251                 case RIL_CDMA_CALLED_PARTY_NUMBER_INFO_REC:
7252                 case RIL_CDMA_CALLING_PARTY_NUMBER_INFO_REC:
7253                 case RIL_CDMA_CONNECTED_NUMBER_INFO_REC: {
7254                     if (infoRec->rec.number.len > CDMA_NUMBER_INFO_BUFFER_LENGTH) {
7255                         RLOGE("cdmaInfoRecInd: invalid display info response length %d "
7256                                 "expected not more than %d", (int) infoRec->rec.number.len,
7257                                 CDMA_NUMBER_INFO_BUFFER_LENGTH);
7258                         return 0;
7259                     }
7260                     string8 = (char*) malloc((infoRec->rec.number.len + 1) * sizeof(char));
7261                     if (string8 == NULL) {
7262                         RLOGE("cdmaInfoRecInd: Memory allocation failed for "
7263                                 "responseCdmaInformationRecords");
7264                         return 0;
7265                     }
7266                     memcpy(string8, infoRec->rec.number.buf, infoRec->rec.number.len);
7267                     string8[(int)infoRec->rec.number.len] = '\0';
7268
7269                     record->number.resize(1);
7270                     record->number[0].number = string8;
7271                     free(string8);
7272                     string8 = NULL;
7273                     record->number[0].numberType = infoRec->rec.number.number_type;
7274                     record->number[0].numberPlan = infoRec->rec.number.number_plan;
7275                     record->number[0].pi = infoRec->rec.number.pi;
7276                     record->number[0].si = infoRec->rec.number.si;
7277                     break;
7278                 }
7279
7280                 case RIL_CDMA_SIGNAL_INFO_REC: {
7281                     record->signal.resize(1);
7282                     record->signal[0].isPresent = infoRec->rec.signal.isPresent;
7283                     record->signal[0].signalType = infoRec->rec.signal.signalType;
7284                     record->signal[0].alertPitch = infoRec->rec.signal.alertPitch;
7285                     record->signal[0].signal = infoRec->rec.signal.signal;
7286                     break;
7287                 }
7288
7289                 case RIL_CDMA_REDIRECTING_NUMBER_INFO_REC: {
7290                     if (infoRec->rec.redir.redirectingNumber.len >
7291                                                   CDMA_NUMBER_INFO_BUFFER_LENGTH) {
7292                         RLOGE("cdmaInfoRecInd: invalid display info response length %d "
7293                                 "expected not more than %d\n",
7294                                 (int)infoRec->rec.redir.redirectingNumber.len,
7295                                 CDMA_NUMBER_INFO_BUFFER_LENGTH);
7296                         return 0;
7297                     }
7298                     string8 = (char*) malloc((infoRec->rec.redir.redirectingNumber.len + 1) *
7299                             sizeof(char));
7300                     if (string8 == NULL) {
7301                         RLOGE("cdmaInfoRecInd: Memory allocation failed for "
7302                                 "responseCdmaInformationRecords");
7303                         return 0;
7304                     }
7305                     memcpy(string8, infoRec->rec.redir.redirectingNumber.buf,
7306                             infoRec->rec.redir.redirectingNumber.len);
7307                     string8[(int)infoRec->rec.redir.redirectingNumber.len] = '\0';
7308
7309                     record->redir.resize(1);
7310                     record->redir[0].redirectingNumber.number = string8;
7311                     free(string8);
7312                     string8 = NULL;
7313                     record->redir[0].redirectingNumber.numberType =
7314                             infoRec->rec.redir.redirectingNumber.number_type;
7315                     record->redir[0].redirectingNumber.numberPlan =
7316                             infoRec->rec.redir.redirectingNumber.number_plan;
7317                     record->redir[0].redirectingNumber.pi = infoRec->rec.redir.redirectingNumber.pi;
7318                     record->redir[0].redirectingNumber.si = infoRec->rec.redir.redirectingNumber.si;
7319                     record->redir[0].redirectingReason =
7320                             (CdmaRedirectingReason) infoRec->rec.redir.redirectingReason;
7321                     break;
7322                 }
7323
7324                 case RIL_CDMA_LINE_CONTROL_INFO_REC: {
7325                     record->lineCtrl.resize(1);
7326                     record->lineCtrl[0].lineCtrlPolarityIncluded =
7327                             infoRec->rec.lineCtrl.lineCtrlPolarityIncluded;
7328                     record->lineCtrl[0].lineCtrlToggle = infoRec->rec.lineCtrl.lineCtrlToggle;
7329                     record->lineCtrl[0].lineCtrlReverse = infoRec->rec.lineCtrl.lineCtrlReverse;
7330                     record->lineCtrl[0].lineCtrlPowerDenial =
7331                             infoRec->rec.lineCtrl.lineCtrlPowerDenial;
7332                     break;
7333                 }
7334
7335                 case RIL_CDMA_T53_CLIR_INFO_REC: {
7336                     record->clir.resize(1);
7337                     record->clir[0].cause = infoRec->rec.clir.cause;
7338                     break;
7339                 }
7340
7341                 case RIL_CDMA_T53_AUDIO_CONTROL_INFO_REC: {
7342                     record->audioCtrl.resize(1);
7343                     record->audioCtrl[0].upLink = infoRec->rec.audioCtrl.upLink;
7344                     record->audioCtrl[0].downLink = infoRec->rec.audioCtrl.downLink;
7345                     break;
7346                 }
7347
7348                 case RIL_CDMA_T53_RELEASE_INFO_REC:
7349                     RLOGE("cdmaInfoRecInd: RIL_CDMA_T53_RELEASE_INFO_REC: INVALID");
7350                     return 0;
7351
7352                 default:
7353                     RLOGE("cdmaInfoRecInd: Incorrect name value");
7354                     return 0;
7355             }
7356         }
7357
7358 #if VDBG
7359         RLOGD("cdmaInfoRecInd");
7360 #endif
7361         Return<void> retStatus = radioService[slotId]->mRadioIndication->cdmaInfoRec(
7362                 convertIntToRadioIndicationType(indicationType), records);
7363         radioService[slotId]->checkReturnStatus(retStatus);
7364     } else {
7365         RLOGE("cdmaInfoRecInd: radioService[%d]->mRadioIndication == NULL", slotId);
7366     }
7367
7368     return 0;
7369 }
7370
7371 int radio::indicateRingbackToneInd(int slotId,
7372                                    int indicationType, int token, RIL_Errno e, void *response,
7373                                    size_t responseLen) {
7374     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7375         if (response == NULL || responseLen != sizeof(int)) {
7376             RLOGE("indicateRingbackToneInd: invalid response");
7377             return 0;
7378         }
7379         bool start = ((int32_t *) response)[0];
7380 #if VDBG
7381         RLOGD("indicateRingbackToneInd: start %d", start);
7382 #endif
7383         Return<void> retStatus = radioService[slotId]->mRadioIndication->indicateRingbackTone(
7384                 convertIntToRadioIndicationType(indicationType), start);
7385         radioService[slotId]->checkReturnStatus(retStatus);
7386     } else {
7387         RLOGE("indicateRingbackToneInd: radioService[%d]->mRadioIndication == NULL", slotId);
7388     }
7389
7390     return 0;
7391 }
7392
7393 int radio::resendIncallMuteInd(int slotId,
7394                                int indicationType, int token, RIL_Errno e, void *response,
7395                                size_t responseLen) {
7396     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7397 #if VDBG
7398         RLOGD("resendIncallMuteInd");
7399 #endif
7400         Return<void> retStatus = radioService[slotId]->mRadioIndication->resendIncallMute(
7401                 convertIntToRadioIndicationType(indicationType));
7402         radioService[slotId]->checkReturnStatus(retStatus);
7403     } else {
7404         RLOGE("resendIncallMuteInd: radioService[%d]->mRadioIndication == NULL", slotId);
7405     }
7406
7407     return 0;
7408 }
7409
7410 int radio::cdmaSubscriptionSourceChangedInd(int slotId,
7411                                             int indicationType, int token, RIL_Errno e,
7412                                             void *response, size_t responseLen) {
7413     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7414         if (response == NULL || responseLen != sizeof(int)) {
7415             RLOGE("cdmaSubscriptionSourceChangedInd: invalid response");
7416             return 0;
7417         }
7418         int32_t cdmaSource = ((int32_t *) response)[0];
7419 #if VDBG
7420         RLOGD("cdmaSubscriptionSourceChangedInd: cdmaSource %d", cdmaSource);
7421 #endif
7422         Return<void> retStatus = radioService[slotId]->mRadioIndication->
7423                 cdmaSubscriptionSourceChanged(convertIntToRadioIndicationType(indicationType),
7424                 (CdmaSubscriptionSource) cdmaSource);
7425         radioService[slotId]->checkReturnStatus(retStatus);
7426     } else {
7427         RLOGE("cdmaSubscriptionSourceChangedInd: radioService[%d]->mRadioIndication == NULL",
7428                 slotId);
7429     }
7430
7431     return 0;
7432 }
7433
7434 int radio::cdmaPrlChangedInd(int slotId,
7435                              int indicationType, int token, RIL_Errno e, void *response,
7436                              size_t responseLen) {
7437     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7438         if (response == NULL || responseLen != sizeof(int)) {
7439             RLOGE("cdmaPrlChangedInd: invalid response");
7440             return 0;
7441         }
7442         int32_t version = ((int32_t *) response)[0];
7443 #if VDBG
7444         RLOGD("cdmaPrlChangedInd: version %d", version);
7445 #endif
7446         Return<void> retStatus = radioService[slotId]->mRadioIndication->cdmaPrlChanged(
7447                 convertIntToRadioIndicationType(indicationType), version);
7448         radioService[slotId]->checkReturnStatus(retStatus);
7449     } else {
7450         RLOGE("cdmaPrlChangedInd: radioService[%d]->mRadioIndication == NULL", slotId);
7451     }
7452
7453     return 0;
7454 }
7455
7456 int radio::exitEmergencyCallbackModeInd(int slotId,
7457                                         int indicationType, int token, RIL_Errno e, void *response,
7458                                         size_t responseLen) {
7459     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7460 #if VDBG
7461         RLOGD("exitEmergencyCallbackModeInd");
7462 #endif
7463         Return<void> retStatus = radioService[slotId]->mRadioIndication->exitEmergencyCallbackMode(
7464                 convertIntToRadioIndicationType(indicationType));
7465         radioService[slotId]->checkReturnStatus(retStatus);
7466     } else {
7467         RLOGE("exitEmergencyCallbackModeInd: radioService[%d]->mRadioIndication == NULL",
7468                 slotId);
7469     }
7470
7471     return 0;
7472 }
7473
7474 int radio::rilConnectedInd(int slotId,
7475                            int indicationType, int token, RIL_Errno e, void *response,
7476                            size_t responseLen) {
7477     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7478         RLOGD("rilConnectedInd");
7479         Return<void> retStatus = radioService[slotId]->mRadioIndication->rilConnected(
7480                 convertIntToRadioIndicationType(indicationType));
7481         radioService[slotId]->checkReturnStatus(retStatus);
7482     } else {
7483         RLOGE("rilConnectedInd: radioService[%d]->mRadioIndication == NULL", slotId);
7484     }
7485
7486     return 0;
7487 }
7488
7489 int radio::voiceRadioTechChangedInd(int slotId,
7490                                     int indicationType, int token, RIL_Errno e, void *response,
7491                                     size_t responseLen) {
7492     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7493         if (response == NULL || responseLen != sizeof(int)) {
7494             RLOGE("voiceRadioTechChangedInd: invalid response");
7495             return 0;
7496         }
7497         int32_t rat = ((int32_t *) response)[0];
7498 #if VDBG
7499         RLOGD("voiceRadioTechChangedInd: rat %d", rat);
7500 #endif
7501         Return<void> retStatus = radioService[slotId]->mRadioIndication->voiceRadioTechChanged(
7502                 convertIntToRadioIndicationType(indicationType), (RadioTechnology) rat);
7503         radioService[slotId]->checkReturnStatus(retStatus);
7504     } else {
7505         RLOGE("voiceRadioTechChangedInd: radioService[%d]->mRadioIndication == NULL",
7506                 slotId);
7507     }
7508
7509     return 0;
7510 }
7511
7512 void convertRilCellInfoListToHal(void *response, size_t responseLen, hidl_vec<CellInfo>& records) {
7513     int num = responseLen / sizeof(RIL_CellInfo_v12);
7514     records.resize(num);
7515
7516     RIL_CellInfo_v12 *rillCellInfo = (RIL_CellInfo_v12 *) response;
7517     for (int i = 0; i < num; i++) {
7518         records[i].cellInfoType = (CellInfoType) rillCellInfo->cellInfoType;
7519         records[i].registered = rillCellInfo->registered;
7520         records[i].timeStampType = (TimeStampType) rillCellInfo->timeStampType;
7521         records[i].timeStamp = rillCellInfo->timeStamp;
7522         // All vectors should be size 0 except one which will be size 1. Set everything to
7523         // size 0 initially.
7524         records[i].gsm.resize(0);
7525         records[i].wcdma.resize(0);
7526         records[i].cdma.resize(0);
7527         records[i].lte.resize(0);
7528         records[i].tdscdma.resize(0);
7529         switch(rillCellInfo->cellInfoType) {
7530             case RIL_CELL_INFO_TYPE_GSM: {
7531                 records[i].gsm.resize(1);
7532                 CellInfoGsm *cellInfoGsm = &records[i].gsm[0];
7533                 cellInfoGsm->cellIdentityGsm.mcc =
7534                         std::to_string(rillCellInfo->CellInfo.gsm.cellIdentityGsm.mcc);
7535                 cellInfoGsm->cellIdentityGsm.mnc =
7536                         std::to_string(rillCellInfo->CellInfo.gsm.cellIdentityGsm.mnc);
7537                 cellInfoGsm->cellIdentityGsm.lac =
7538                         rillCellInfo->CellInfo.gsm.cellIdentityGsm.lac;
7539                 cellInfoGsm->cellIdentityGsm.cid =
7540                         rillCellInfo->CellInfo.gsm.cellIdentityGsm.cid;
7541                 cellInfoGsm->cellIdentityGsm.arfcn =
7542                         rillCellInfo->CellInfo.gsm.cellIdentityGsm.arfcn;
7543                 cellInfoGsm->cellIdentityGsm.bsic =
7544                         rillCellInfo->CellInfo.gsm.cellIdentityGsm.bsic;
7545                 cellInfoGsm->signalStrengthGsm.signalStrength =
7546                         rillCellInfo->CellInfo.gsm.signalStrengthGsm.signalStrength;
7547                 cellInfoGsm->signalStrengthGsm.bitErrorRate =
7548                         rillCellInfo->CellInfo.gsm.signalStrengthGsm.bitErrorRate;
7549                 cellInfoGsm->signalStrengthGsm.timingAdvance =
7550                         rillCellInfo->CellInfo.gsm.signalStrengthGsm.timingAdvance;
7551                 break;
7552             }
7553
7554             case RIL_CELL_INFO_TYPE_WCDMA: {
7555                 records[i].wcdma.resize(1);
7556                 CellInfoWcdma *cellInfoWcdma = &records[i].wcdma[0];
7557                 cellInfoWcdma->cellIdentityWcdma.mcc =
7558                         std::to_string(rillCellInfo->CellInfo.wcdma.cellIdentityWcdma.mcc);
7559                 cellInfoWcdma->cellIdentityWcdma.mnc =
7560                         std::to_string(rillCellInfo->CellInfo.wcdma.cellIdentityWcdma.mnc);
7561                 cellInfoWcdma->cellIdentityWcdma.lac =
7562                         rillCellInfo->CellInfo.wcdma.cellIdentityWcdma.lac;
7563                 cellInfoWcdma->cellIdentityWcdma.cid =
7564                         rillCellInfo->CellInfo.wcdma.cellIdentityWcdma.cid;
7565                 cellInfoWcdma->cellIdentityWcdma.psc =
7566                         rillCellInfo->CellInfo.wcdma.cellIdentityWcdma.psc;
7567                 cellInfoWcdma->cellIdentityWcdma.uarfcn =
7568                         rillCellInfo->CellInfo.wcdma.cellIdentityWcdma.uarfcn;
7569                 cellInfoWcdma->signalStrengthWcdma.signalStrength =
7570                         rillCellInfo->CellInfo.wcdma.signalStrengthWcdma.signalStrength;
7571                 cellInfoWcdma->signalStrengthWcdma.bitErrorRate =
7572                         rillCellInfo->CellInfo.wcdma.signalStrengthWcdma.bitErrorRate;
7573                 break;
7574             }
7575
7576             case RIL_CELL_INFO_TYPE_CDMA: {
7577                 records[i].cdma.resize(1);
7578                 CellInfoCdma *cellInfoCdma = &records[i].cdma[0];
7579                 cellInfoCdma->cellIdentityCdma.networkId =
7580                         rillCellInfo->CellInfo.cdma.cellIdentityCdma.networkId;
7581                 cellInfoCdma->cellIdentityCdma.systemId =
7582                         rillCellInfo->CellInfo.cdma.cellIdentityCdma.systemId;
7583                 cellInfoCdma->cellIdentityCdma.baseStationId =
7584                         rillCellInfo->CellInfo.cdma.cellIdentityCdma.basestationId;
7585                 cellInfoCdma->cellIdentityCdma.longitude =
7586                         rillCellInfo->CellInfo.cdma.cellIdentityCdma.longitude;
7587                 cellInfoCdma->cellIdentityCdma.latitude =
7588                         rillCellInfo->CellInfo.cdma.cellIdentityCdma.latitude;
7589                 cellInfoCdma->signalStrengthCdma.dbm =
7590                         rillCellInfo->CellInfo.cdma.signalStrengthCdma.dbm;
7591                 cellInfoCdma->signalStrengthCdma.ecio =
7592                         rillCellInfo->CellInfo.cdma.signalStrengthCdma.ecio;
7593                 cellInfoCdma->signalStrengthEvdo.dbm =
7594                         rillCellInfo->CellInfo.cdma.signalStrengthEvdo.dbm;
7595                 cellInfoCdma->signalStrengthEvdo.ecio =
7596                         rillCellInfo->CellInfo.cdma.signalStrengthEvdo.ecio;
7597                 cellInfoCdma->signalStrengthEvdo.signalNoiseRatio =
7598                         rillCellInfo->CellInfo.cdma.signalStrengthEvdo.signalNoiseRatio;
7599                 break;
7600             }
7601
7602             case RIL_CELL_INFO_TYPE_LTE: {
7603                 records[i].lte.resize(1);
7604                 CellInfoLte *cellInfoLte = &records[i].lte[0];
7605                 cellInfoLte->cellIdentityLte.mcc =
7606                         std::to_string(rillCellInfo->CellInfo.lte.cellIdentityLte.mcc);
7607                 cellInfoLte->cellIdentityLte.mnc =
7608                         std::to_string(rillCellInfo->CellInfo.lte.cellIdentityLte.mnc);
7609                 cellInfoLte->cellIdentityLte.ci =
7610                         rillCellInfo->CellInfo.lte.cellIdentityLte.ci;
7611                 cellInfoLte->cellIdentityLte.pci =
7612                         rillCellInfo->CellInfo.lte.cellIdentityLte.pci;
7613                 cellInfoLte->cellIdentityLte.tac =
7614                         rillCellInfo->CellInfo.lte.cellIdentityLte.tac;
7615                 cellInfoLte->cellIdentityLte.earfcn =
7616                         rillCellInfo->CellInfo.lte.cellIdentityLte.earfcn;
7617                 cellInfoLte->signalStrengthLte.signalStrength =
7618                         rillCellInfo->CellInfo.lte.signalStrengthLte.signalStrength;
7619                 cellInfoLte->signalStrengthLte.rsrp =
7620                         rillCellInfo->CellInfo.lte.signalStrengthLte.rsrp;
7621                 cellInfoLte->signalStrengthLte.rsrq =
7622                         rillCellInfo->CellInfo.lte.signalStrengthLte.rsrq;
7623                 cellInfoLte->signalStrengthLte.rssnr =
7624                         rillCellInfo->CellInfo.lte.signalStrengthLte.rssnr;
7625                 cellInfoLte->signalStrengthLte.cqi =
7626                         rillCellInfo->CellInfo.lte.signalStrengthLte.cqi;
7627                 cellInfoLte->signalStrengthLte.timingAdvance =
7628                         rillCellInfo->CellInfo.lte.signalStrengthLte.timingAdvance;
7629                 break;
7630             }
7631
7632             case RIL_CELL_INFO_TYPE_TD_SCDMA: {
7633                 records[i].tdscdma.resize(1);
7634                 CellInfoTdscdma *cellInfoTdscdma = &records[i].tdscdma[0];
7635                 cellInfoTdscdma->cellIdentityTdscdma.mcc =
7636                         std::to_string(rillCellInfo->CellInfo.tdscdma.cellIdentityTdscdma.mcc);
7637                 cellInfoTdscdma->cellIdentityTdscdma.mnc =
7638                         std::to_string(rillCellInfo->CellInfo.tdscdma.cellIdentityTdscdma.mnc);
7639                 cellInfoTdscdma->cellIdentityTdscdma.lac =
7640                         rillCellInfo->CellInfo.tdscdma.cellIdentityTdscdma.lac;
7641                 cellInfoTdscdma->cellIdentityTdscdma.cid =
7642                         rillCellInfo->CellInfo.tdscdma.cellIdentityTdscdma.cid;
7643                 cellInfoTdscdma->cellIdentityTdscdma.cpid =
7644                         rillCellInfo->CellInfo.tdscdma.cellIdentityTdscdma.cpid;
7645                 cellInfoTdscdma->signalStrengthTdscdma.rscp =
7646                         rillCellInfo->CellInfo.tdscdma.signalStrengthTdscdma.rscp;
7647                 break;
7648             }
7649             default: {
7650                 break;
7651             }
7652         }
7653         rillCellInfo += 1;
7654     }
7655 }
7656
7657 int radio::cellInfoListInd(int slotId,
7658                            int indicationType, int token, RIL_Errno e, void *response,
7659                            size_t responseLen) {
7660     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7661         if ((response == NULL && responseLen != 0) || responseLen % sizeof(RIL_CellInfo_v12) != 0) {
7662             RLOGE("cellInfoListInd: invalid response");
7663             return 0;
7664         }
7665
7666         hidl_vec<CellInfo> records;
7667         convertRilCellInfoListToHal(response, responseLen, records);
7668
7669 #if VDBG
7670         RLOGD("cellInfoListInd");
7671 #endif
7672         Return<void> retStatus = radioService[slotId]->mRadioIndication->cellInfoList(
7673                 convertIntToRadioIndicationType(indicationType), records);
7674         radioService[slotId]->checkReturnStatus(retStatus);
7675     } else {
7676         RLOGE("cellInfoListInd: radioService[%d]->mRadioIndication == NULL", slotId);
7677     }
7678
7679     return 0;
7680 }
7681
7682 int radio::imsNetworkStateChangedInd(int slotId,
7683                                      int indicationType, int token, RIL_Errno e, void *response,
7684                                      size_t responseLen) {
7685     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7686 #if VDBG
7687         RLOGD("imsNetworkStateChangedInd");
7688 #endif
7689         Return<void> retStatus = radioService[slotId]->mRadioIndication->imsNetworkStateChanged(
7690                 convertIntToRadioIndicationType(indicationType));
7691         radioService[slotId]->checkReturnStatus(retStatus);
7692     } else {
7693         RLOGE("imsNetworkStateChangedInd: radioService[%d]->mRadioIndication == NULL",
7694                 slotId);
7695     }
7696
7697     return 0;
7698 }
7699
7700 int radio::subscriptionStatusChangedInd(int slotId,
7701                                         int indicationType, int token, RIL_Errno e, void *response,
7702                                         size_t responseLen) {
7703     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7704         if (response == NULL || responseLen != sizeof(int)) {
7705             RLOGE("subscriptionStatusChangedInd: invalid response");
7706             return 0;
7707         }
7708         bool activate = ((int32_t *) response)[0];
7709 #if VDBG
7710         RLOGD("subscriptionStatusChangedInd: activate %d", activate);
7711 #endif
7712         Return<void> retStatus = radioService[slotId]->mRadioIndication->subscriptionStatusChanged(
7713                 convertIntToRadioIndicationType(indicationType), activate);
7714         radioService[slotId]->checkReturnStatus(retStatus);
7715     } else {
7716         RLOGE("subscriptionStatusChangedInd: radioService[%d]->mRadioIndication == NULL",
7717                 slotId);
7718     }
7719
7720     return 0;
7721 }
7722
7723 int radio::srvccStateNotifyInd(int slotId,
7724                                int indicationType, int token, RIL_Errno e, void *response,
7725                                size_t responseLen) {
7726     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7727         if (response == NULL || responseLen != sizeof(int)) {
7728             RLOGE("srvccStateNotifyInd: invalid response");
7729             return 0;
7730         }
7731         int32_t state = ((int32_t *) response)[0];
7732 #if VDBG
7733         RLOGD("srvccStateNotifyInd: rat %d", state);
7734 #endif
7735         Return<void> retStatus = radioService[slotId]->mRadioIndication->srvccStateNotify(
7736                 convertIntToRadioIndicationType(indicationType), (SrvccState) state);
7737         radioService[slotId]->checkReturnStatus(retStatus);
7738     } else {
7739         RLOGE("srvccStateNotifyInd: radioService[%d]->mRadioIndication == NULL", slotId);
7740     }
7741
7742     return 0;
7743 }
7744
7745 void convertRilHardwareConfigListToHal(void *response, size_t responseLen,
7746         hidl_vec<HardwareConfig>& records) {
7747     int num = responseLen / sizeof(RIL_HardwareConfig);
7748     records.resize(num);
7749
7750     RIL_HardwareConfig *rilHardwareConfig = (RIL_HardwareConfig *) response;
7751     for (int i = 0; i < num; i++) {
7752         records[i].type = (HardwareConfigType) rilHardwareConfig[i].type;
7753         records[i].uuid = convertCharPtrToHidlString(rilHardwareConfig[i].uuid);
7754         records[i].state = (HardwareConfigState) rilHardwareConfig[i].state;
7755         switch (rilHardwareConfig[i].type) {
7756             case RIL_HARDWARE_CONFIG_MODEM: {
7757                 records[i].modem.resize(1);
7758                 records[i].sim.resize(0);
7759                 HardwareConfigModem *hwConfigModem = &records[i].modem[0];
7760                 hwConfigModem->rat = rilHardwareConfig[i].cfg.modem.rat;
7761                 hwConfigModem->maxVoice = rilHardwareConfig[i].cfg.modem.maxVoice;
7762                 hwConfigModem->maxData = rilHardwareConfig[i].cfg.modem.maxData;
7763                 hwConfigModem->maxStandby = rilHardwareConfig[i].cfg.modem.maxStandby;
7764                 break;
7765             }
7766
7767             case RIL_HARDWARE_CONFIG_SIM: {
7768                 records[i].sim.resize(1);
7769                 records[i].modem.resize(0);
7770                 records[i].sim[0].modemUuid =
7771                         convertCharPtrToHidlString(rilHardwareConfig[i].cfg.sim.modemUuid);
7772                 break;
7773             }
7774         }
7775     }
7776 }
7777
7778 int radio::hardwareConfigChangedInd(int slotId,
7779                                     int indicationType, int token, RIL_Errno e, void *response,
7780                                     size_t responseLen) {
7781     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7782         if ((response == NULL && responseLen != 0)
7783                 || responseLen % sizeof(RIL_HardwareConfig) != 0) {
7784             RLOGE("hardwareConfigChangedInd: invalid response");
7785             return 0;
7786         }
7787
7788         hidl_vec<HardwareConfig> configs;
7789         convertRilHardwareConfigListToHal(response, responseLen, configs);
7790
7791 #if VDBG
7792         RLOGD("hardwareConfigChangedInd");
7793 #endif
7794         Return<void> retStatus = radioService[slotId]->mRadioIndication->hardwareConfigChanged(
7795                 convertIntToRadioIndicationType(indicationType), configs);
7796         radioService[slotId]->checkReturnStatus(retStatus);
7797     } else {
7798         RLOGE("hardwareConfigChangedInd: radioService[%d]->mRadioIndication == NULL",
7799                 slotId);
7800     }
7801
7802     return 0;
7803 }
7804
7805 void convertRilRadioCapabilityToHal(void *response, size_t responseLen, RadioCapability& rc) {
7806     RIL_RadioCapability *rilRadioCapability = (RIL_RadioCapability *) response;
7807     rc.session = rilRadioCapability->session;
7808     rc.phase = (android::hardware::radio::V1_0::RadioCapabilityPhase) rilRadioCapability->phase;
7809     rc.raf = rilRadioCapability->rat;
7810     rc.logicalModemUuid = convertCharPtrToHidlString(rilRadioCapability->logicalModemUuid);
7811     rc.status = (android::hardware::radio::V1_0::RadioCapabilityStatus) rilRadioCapability->status;
7812 }
7813
7814 int radio::radioCapabilityIndicationInd(int slotId,
7815                                         int indicationType, int token, RIL_Errno e, void *response,
7816                                         size_t responseLen) {
7817     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7818         if (response == NULL || responseLen != sizeof(RIL_RadioCapability)) {
7819             RLOGE("radioCapabilityIndicationInd: invalid response");
7820             return 0;
7821         }
7822
7823         RadioCapability rc = {};
7824         convertRilRadioCapabilityToHal(response, responseLen, rc);
7825
7826 #if VDBG
7827         RLOGD("radioCapabilityIndicationInd");
7828 #endif
7829         Return<void> retStatus = radioService[slotId]->mRadioIndication->radioCapabilityIndication(
7830                 convertIntToRadioIndicationType(indicationType), rc);
7831         radioService[slotId]->checkReturnStatus(retStatus);
7832     } else {
7833         RLOGE("radioCapabilityIndicationInd: radioService[%d]->mRadioIndication == NULL",
7834                 slotId);
7835     }
7836
7837     return 0;
7838 }
7839
7840 bool isServiceTypeCfQuery(RIL_SsServiceType serType, RIL_SsRequestType reqType) {
7841     if ((reqType == SS_INTERROGATION) &&
7842         (serType == SS_CFU ||
7843          serType == SS_CF_BUSY ||
7844          serType == SS_CF_NO_REPLY ||
7845          serType == SS_CF_NOT_REACHABLE ||
7846          serType == SS_CF_ALL ||
7847          serType == SS_CF_ALL_CONDITIONAL)) {
7848         return true;
7849     }
7850     return false;
7851 }
7852
7853 int radio::onSupplementaryServiceIndicationInd(int slotId,
7854                                                int indicationType, int token, RIL_Errno e,
7855                                                void *response, size_t responseLen) {
7856     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7857         if (response == NULL || responseLen != sizeof(RIL_StkCcUnsolSsResponse)) {
7858             RLOGE("onSupplementaryServiceIndicationInd: invalid response");
7859             return 0;
7860         }
7861
7862         RIL_StkCcUnsolSsResponse *rilSsResponse = (RIL_StkCcUnsolSsResponse *) response;
7863         StkCcUnsolSsResult ss = {};
7864         ss.serviceType = (SsServiceType) rilSsResponse->serviceType;
7865         ss.requestType = (SsRequestType) rilSsResponse->requestType;
7866         ss.teleserviceType = (SsTeleserviceType) rilSsResponse->teleserviceType;
7867         ss.serviceClass = rilSsResponse->serviceClass;
7868         ss.result = (RadioError) rilSsResponse->result;
7869
7870         if (isServiceTypeCfQuery(rilSsResponse->serviceType, rilSsResponse->requestType)) {
7871 #if VDBG
7872             RLOGD("onSupplementaryServiceIndicationInd CF type, num of Cf elements %d",
7873                     rilSsResponse->cfData.numValidIndexes);
7874 #endif
7875             if (rilSsResponse->cfData.numValidIndexes > NUM_SERVICE_CLASSES) {
7876                 RLOGE("onSupplementaryServiceIndicationInd numValidIndexes is greater than "
7877                         "max value %d, truncating it to max value", NUM_SERVICE_CLASSES);
7878                 rilSsResponse->cfData.numValidIndexes = NUM_SERVICE_CLASSES;
7879             }
7880
7881             ss.cfData.resize(1);
7882             ss.ssInfo.resize(0);
7883
7884             /* number of call info's */
7885             ss.cfData[0].cfInfo.resize(rilSsResponse->cfData.numValidIndexes);
7886
7887             for (int i = 0; i < rilSsResponse->cfData.numValidIndexes; i++) {
7888                  RIL_CallForwardInfo cf = rilSsResponse->cfData.cfInfo[i];
7889                  CallForwardInfo *cfInfo = &ss.cfData[0].cfInfo[i];
7890
7891                  cfInfo->status = (CallForwardInfoStatus) cf.status;
7892                  cfInfo->reason = cf.reason;
7893                  cfInfo->serviceClass = cf.serviceClass;
7894                  cfInfo->toa = cf.toa;
7895                  cfInfo->number = convertCharPtrToHidlString(cf.number);
7896                  cfInfo->timeSeconds = cf.timeSeconds;
7897 #if VDBG
7898                  RLOGD("onSupplementaryServiceIndicationInd: "
7899                         "Data: %d,reason=%d,cls=%d,toa=%d,num=%s,tout=%d],", cf.status,
7900                         cf.reason, cf.serviceClass, cf.toa, (char*)cf.number, cf.timeSeconds);
7901 #endif
7902             }
7903         } else {
7904             ss.ssInfo.resize(1);
7905             ss.cfData.resize(0);
7906
7907             /* each int */
7908             ss.ssInfo[0].ssInfo.resize(SS_INFO_MAX);
7909             for (int i = 0; i < SS_INFO_MAX; i++) {
7910 #if VDBG
7911                  RLOGD("onSupplementaryServiceIndicationInd: Data: %d",
7912                         rilSsResponse->ssInfo[i]);
7913 #endif
7914                  ss.ssInfo[0].ssInfo[i] = rilSsResponse->ssInfo[i];
7915             }
7916         }
7917
7918 #if VDBG
7919         RLOGD("onSupplementaryServiceIndicationInd");
7920 #endif
7921         Return<void> retStatus = radioService[slotId]->mRadioIndication->
7922                 onSupplementaryServiceIndication(convertIntToRadioIndicationType(indicationType),
7923                 ss);
7924         radioService[slotId]->checkReturnStatus(retStatus);
7925     } else {
7926         RLOGE("onSupplementaryServiceIndicationInd: "
7927                 "radioService[%d]->mRadioIndication == NULL", slotId);
7928     }
7929
7930     return 0;
7931 }
7932
7933 int radio::stkCallControlAlphaNotifyInd(int slotId,
7934                                         int indicationType, int token, RIL_Errno e, void *response,
7935                                         size_t responseLen) {
7936     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7937         if (response == NULL || responseLen == 0) {
7938             RLOGE("stkCallControlAlphaNotifyInd: invalid response");
7939             return 0;
7940         }
7941 #if VDBG
7942         RLOGD("stkCallControlAlphaNotifyInd");
7943 #endif
7944         Return<void> retStatus = radioService[slotId]->mRadioIndication->stkCallControlAlphaNotify(
7945                 convertIntToRadioIndicationType(indicationType),
7946                 convertCharPtrToHidlString((char *) response));
7947         radioService[slotId]->checkReturnStatus(retStatus);
7948     } else {
7949         RLOGE("stkCallControlAlphaNotifyInd: radioService[%d]->mRadioIndication == NULL",
7950                 slotId);
7951     }
7952
7953     return 0;
7954 }
7955
7956 void convertRilLceDataInfoToHal(void *response, size_t responseLen, LceDataInfo& lce) {
7957     RIL_LceDataInfo *rilLceDataInfo = (RIL_LceDataInfo *)response;
7958     lce.lastHopCapacityKbps = rilLceDataInfo->last_hop_capacity_kbps;
7959     lce.confidenceLevel = rilLceDataInfo->confidence_level;
7960     lce.lceSuspended = rilLceDataInfo->lce_suspended;
7961 }
7962
7963 int radio::lceDataInd(int slotId,
7964                       int indicationType, int token, RIL_Errno e, void *response,
7965                       size_t responseLen) {
7966     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7967         if (response == NULL || responseLen != sizeof(RIL_LceDataInfo)) {
7968             RLOGE("lceDataInd: invalid response");
7969             return 0;
7970         }
7971
7972         LceDataInfo lce = {};
7973         convertRilLceDataInfoToHal(response, responseLen, lce);
7974 #if VDBG
7975         RLOGD("lceDataInd");
7976 #endif
7977         Return<void> retStatus = radioService[slotId]->mRadioIndication->lceData(
7978                 convertIntToRadioIndicationType(indicationType), lce);
7979         radioService[slotId]->checkReturnStatus(retStatus);
7980     } else {
7981         RLOGE("lceDataInd: radioService[%d]->mRadioIndication == NULL", slotId);
7982     }
7983
7984     return 0;
7985 }
7986
7987 int radio::pcoDataInd(int slotId,
7988                       int indicationType, int token, RIL_Errno e, void *response,
7989                       size_t responseLen) {
7990     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7991         if (response == NULL || responseLen != sizeof(RIL_PCO_Data)) {
7992             RLOGE("pcoDataInd: invalid response");
7993             return 0;
7994         }
7995
7996         PcoDataInfo pco = {};
7997         RIL_PCO_Data *rilPcoData = (RIL_PCO_Data *)response;
7998         pco.cid = rilPcoData->cid;
7999         pco.bearerProto = convertCharPtrToHidlString(rilPcoData->bearer_proto);
8000         pco.pcoId = rilPcoData->pco_id;
8001         pco.contents.setToExternal((uint8_t *) rilPcoData->contents, rilPcoData->contents_length);
8002
8003 #if VDBG
8004         RLOGD("pcoDataInd");
8005 #endif
8006         Return<void> retStatus = radioService[slotId]->mRadioIndication->pcoData(
8007                 convertIntToRadioIndicationType(indicationType), pco);
8008         radioService[slotId]->checkReturnStatus(retStatus);
8009     } else {
8010         RLOGE("pcoDataInd: radioService[%d]->mRadioIndication == NULL", slotId);
8011     }
8012
8013     return 0;
8014 }
8015
8016 int radio::modemResetInd(int slotId,
8017                          int indicationType, int token, RIL_Errno e, void *response,
8018                          size_t responseLen) {
8019     if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
8020         if (response == NULL || responseLen == 0) {
8021             RLOGE("modemResetInd: invalid response");
8022             return 0;
8023         }
8024 #if VDBG
8025         RLOGD("modemResetInd");
8026 #endif
8027         Return<void> retStatus = radioService[slotId]->mRadioIndication->modemReset(
8028                 convertIntToRadioIndicationType(indicationType),
8029                 convertCharPtrToHidlString((char *) response));
8030         radioService[slotId]->checkReturnStatus(retStatus);
8031     } else {
8032         RLOGE("modemResetInd: radioService[%d]->mRadioIndication == NULL", slotId);
8033     }
8034
8035     return 0;
8036 }
8037
8038 int radio::oemHookRawInd(int slotId,
8039                          int indicationType, int token, RIL_Errno e, void *response,
8040                          size_t responseLen) {
8041     if (oemHookService[slotId] != NULL && oemHookService[slotId]->mOemHookIndication != NULL) {
8042         if (response == NULL || responseLen == 0) {
8043             RLOGE("oemHookRawInd: invalid response");
8044             return 0;
8045         }
8046
8047         hidl_vec<uint8_t> data;
8048         data.setToExternal((uint8_t *) response, responseLen);
8049 #if VDBG
8050         RLOGD("oemHookRawInd");
8051 #endif
8052         Return<void> retStatus = oemHookService[slotId]->mOemHookIndication->oemHookRaw(
8053                 convertIntToRadioIndicationType(indicationType), data);
8054         checkReturnStatus(slotId, retStatus, false);
8055     } else {
8056         RLOGE("oemHookRawInd: oemHookService[%d]->mOemHookIndication == NULL", slotId);
8057     }
8058
8059     return 0;
8060 }
8061
8062 void radio::registerService(RIL_RadioFunctions *callbacks, CommandInfo *commands) {
8063     using namespace android::hardware;
8064     int simCount = 1;
8065     const char *serviceNames[] = {
8066             android::RIL_getServiceName()
8067             #if (SIM_COUNT >= 2)
8068             , RIL2_SERVICE_NAME
8069             #if (SIM_COUNT >= 3)
8070             , RIL3_SERVICE_NAME
8071             #if (SIM_COUNT >= 4)
8072             , RIL4_SERVICE_NAME
8073             #endif
8074             #endif
8075             #endif
8076             };
8077
8078     #if (SIM_COUNT >= 2)
8079     simCount = SIM_COUNT;
8080     #endif
8081
8082     configureRpcThreadpool(1, true /* callerWillJoin */);
8083     for (int i = 0; i < simCount; i++) {
8084         pthread_rwlock_t *radioServiceRwlockPtr = getRadioServiceRwlock(i);
8085         int ret = pthread_rwlock_wrlock(radioServiceRwlockPtr);
8086         assert(ret == 0);
8087
8088         radioService[i] = new RadioImpl;
8089         radioService[i]->mSlotId = i;
8090         oemHookService[i] = new OemHookImpl;
8091         oemHookService[i]->mSlotId = i;
8092         RLOGD("registerService: starting IRadio %s", serviceNames[i]);
8093         android::status_t status = radioService[i]->registerAsService(serviceNames[i]);
8094         status = oemHookService[i]->registerAsService(serviceNames[i]);
8095
8096         ret = pthread_rwlock_unlock(radioServiceRwlockPtr);
8097         assert(ret == 0);
8098     }
8099
8100     s_vendorFunctions = callbacks;
8101     s_commands = commands;
8102 }
8103
8104 void rilc_thread_pool() {
8105     joinRpcThreadpool();
8106 }
8107
8108 pthread_rwlock_t * radio::getRadioServiceRwlock(int slotId) {
8109     pthread_rwlock_t *radioServiceRwlockPtr = &radioServiceRwlock;
8110
8111     #if (SIM_COUNT >= 2)
8112     if (slotId == 2) radioServiceRwlockPtr = &radioServiceRwlock2;
8113     #if (SIM_COUNT >= 3)
8114     if (slotId == 3) radioServiceRwlockPtr = &radioServiceRwlock3;
8115     #if (SIM_COUNT >= 4)
8116     if (slotId == 4) radioServiceRwlockPtr = &radioServiceRwlock4;
8117     #endif
8118     #endif
8119     #endif
8120
8121     return radioServiceRwlockPtr;
8122 }