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