OSDN Git Service

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