ril_service.cpp revision 93f603c4e168c30fbc1366c1b6c6ac0f0a4b4b6e
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            2, 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 % sizeof(RIL_Call *)) != 0) {
2978            RLOGE("getCurrentCallsResponse: Invalid response");
2979            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
2980        } else {
2981            int num = responseLen / sizeof(RIL_Call *);
2982            calls.resize(num);
2983
2984            for (int i = 0 ; i < num ; i++) {
2985                RIL_Call *p_cur = ((RIL_Call **) response)[i];
2986                /* each call info */
2987                calls[i].state = (CallState) p_cur->state;
2988                calls[i].index = p_cur->index;
2989                calls[i].toa = p_cur->toa;
2990                calls[i].isMpty = p_cur->isMpty;
2991                calls[i].isMT = p_cur->isMT;
2992                calls[i].als = p_cur->als;
2993                calls[i].isVoice = p_cur->isVoice;
2994                calls[i].isVoicePrivacy = p_cur->isVoicePrivacy;
2995                calls[i].number = convertCharPtrToHidlString(p_cur->number);
2996                calls[i].numberPresentation = (CallPresentation) p_cur->numberPresentation;
2997                calls[i].name = convertCharPtrToHidlString(p_cur->name);
2998                calls[i].namePresentation = (CallPresentation) p_cur->namePresentation;
2999                if (p_cur->uusInfo != NULL && p_cur->uusInfo->uusData != NULL) {
3000                    RIL_UUS_Info *uusInfo = p_cur->uusInfo;
3001                    calls[i].uusInfo[0].uusType = (UusType) uusInfo->uusType;
3002                    calls[i].uusInfo[0].uusDcs = (UusDcs) uusInfo->uusDcs;
3003                    // convert uusInfo->uusData to a null-terminated string
3004                    char *nullTermStr = strndup(uusInfo->uusData, uusInfo->uusLength);
3005                    calls[i].uusInfo[0].uusData = nullTermStr;
3006                    free(nullTermStr);
3007                }
3008            }
3009        }
3010
3011        Return<void> retStatus = radioService[slotId]->mRadioResponse->
3012                getCurrentCallsResponse(responseInfo, calls);
3013        radioService[slotId]->checkReturnStatus(retStatus);
3014    } else {
3015        RLOGE("getCurrentCallsResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3016    }
3017
3018    return 0;
3019}
3020
3021int radio::dialResponse(int slotId,
3022                       int responseType, int serial, RIL_Errno e, void *response,
3023                       size_t responseLen) {
3024#if VDBG
3025    RLOGD("dialResponse: serial %d", serial);
3026#endif
3027
3028    if (radioService[slotId]->mRadioResponse != NULL) {
3029        RadioResponseInfo responseInfo = {};
3030        populateResponseInfo(responseInfo, serial, responseType, e);
3031        Return<void> retStatus = radioService[slotId]->mRadioResponse->dialResponse(responseInfo);
3032        radioService[slotId]->checkReturnStatus(retStatus);
3033    } else {
3034        RLOGE("dialResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3035    }
3036
3037    return 0;
3038}
3039
3040int radio::getIMSIForAppResponse(int slotId,
3041                                int responseType, int serial, RIL_Errno e, void *response,
3042                                size_t responseLen) {
3043#if VDBG
3044    RLOGD("getIMSIForAppResponse: serial %d", serial);
3045#endif
3046
3047    if (radioService[slotId]->mRadioResponse != NULL) {
3048        RadioResponseInfo responseInfo = {};
3049        populateResponseInfo(responseInfo, serial, responseType, e);
3050        Return<void> retStatus = radioService[slotId]->mRadioResponse->getIMSIForAppResponse(
3051                responseInfo, convertCharPtrToHidlString((char *) response));
3052        radioService[slotId]->checkReturnStatus(retStatus);
3053    } else {
3054        RLOGE("getIMSIForAppResponse: radioService[%d]->mRadioResponse == NULL",
3055                slotId);
3056    }
3057
3058    return 0;
3059}
3060
3061int radio::hangupConnectionResponse(int slotId,
3062                                   int responseType, int serial, RIL_Errno e,
3063                                   void *response, size_t responseLen) {
3064#if VDBG
3065    RLOGD("hangupConnectionResponse: serial %d", serial);
3066#endif
3067
3068    if (radioService[slotId]->mRadioResponse != NULL) {
3069        RadioResponseInfo responseInfo = {};
3070        populateResponseInfo(responseInfo, serial, responseType, e);
3071        Return<void> retStatus = radioService[slotId]->mRadioResponse->hangupConnectionResponse(
3072                responseInfo);
3073        radioService[slotId]->checkReturnStatus(retStatus);
3074    } else {
3075        RLOGE("hangupConnectionResponse: radioService[%d]->mRadioResponse == NULL",
3076                slotId);
3077    }
3078
3079    return 0;
3080}
3081
3082int radio::hangupWaitingOrBackgroundResponse(int slotId,
3083                                            int responseType, int serial, RIL_Errno e,
3084                                            void *response, size_t responseLen) {
3085#if VDBG
3086    RLOGD("hangupWaitingOrBackgroundResponse: serial %d", serial);
3087#endif
3088
3089    if (radioService[slotId]->mRadioResponse != NULL) {
3090        RadioResponseInfo responseInfo = {};
3091        populateResponseInfo(responseInfo, serial, responseType, e);
3092        Return<void> retStatus =
3093                radioService[slotId]->mRadioResponse->hangupWaitingOrBackgroundResponse(
3094                responseInfo);
3095        radioService[slotId]->checkReturnStatus(retStatus);
3096    } else {
3097        RLOGE("hangupWaitingOrBackgroundResponse: radioService[%d]->mRadioResponse == NULL",
3098                slotId);
3099    }
3100
3101    return 0;
3102}
3103
3104int radio::hangupForegroundResumeBackgroundResponse(int slotId, int responseType, int serial,
3105                                                    RIL_Errno e, void *response,
3106                                                    size_t responseLen) {
3107#if VDBG
3108    RLOGD("hangupWaitingOrBackgroundResponse: serial %d", serial);
3109#endif
3110
3111    if (radioService[slotId]->mRadioResponse != NULL) {
3112        RadioResponseInfo responseInfo = {};
3113        populateResponseInfo(responseInfo, serial, responseType, e);
3114        Return<void> retStatus =
3115                radioService[slotId]->mRadioResponse->hangupWaitingOrBackgroundResponse(
3116                responseInfo);
3117        radioService[slotId]->checkReturnStatus(retStatus);
3118    } else {
3119        RLOGE("hangupWaitingOrBackgroundResponse: radioService[%d]->mRadioResponse == NULL",
3120                slotId);
3121    }
3122
3123    return 0;
3124}
3125
3126int radio::switchWaitingOrHoldingAndActiveResponse(int slotId, int responseType, int serial,
3127                                                   RIL_Errno e, void *response,
3128                                                   size_t responseLen) {
3129#if VDBG
3130    RLOGD("switchWaitingOrHoldingAndActiveResponse: serial %d", serial);
3131#endif
3132
3133    if (radioService[slotId]->mRadioResponse != NULL) {
3134        RadioResponseInfo responseInfo = {};
3135        populateResponseInfo(responseInfo, serial, responseType, e);
3136        Return<void> retStatus =
3137                radioService[slotId]->mRadioResponse->switchWaitingOrHoldingAndActiveResponse(
3138                responseInfo);
3139        radioService[slotId]->checkReturnStatus(retStatus);
3140    } else {
3141        RLOGE("switchWaitingOrHoldingAndActiveResponse: radioService[%d]->mRadioResponse "
3142                "== NULL", slotId);
3143    }
3144
3145    return 0;
3146}
3147
3148int radio::conferenceResponse(int slotId, int responseType,
3149                             int serial, RIL_Errno e, void *response, size_t responseLen) {
3150#if VDBG
3151    RLOGD("conferenceResponse: serial %d", serial);
3152#endif
3153
3154    if (radioService[slotId]->mRadioResponse != NULL) {
3155        RadioResponseInfo responseInfo = {};
3156        populateResponseInfo(responseInfo, serial, responseType, e);
3157        Return<void> retStatus = radioService[slotId]->mRadioResponse->conferenceResponse(
3158                responseInfo);
3159        radioService[slotId]->checkReturnStatus(retStatus);
3160    } else {
3161        RLOGE("conferenceResponse: radioService[%d]->mRadioResponse == NULL",
3162                slotId);
3163    }
3164
3165    return 0;
3166}
3167
3168int radio::rejectCallResponse(int slotId, int responseType,
3169                             int serial, RIL_Errno e, void *response, size_t responseLen) {
3170#if VDBG
3171    RLOGD("rejectCallResponse: serial %d", serial);
3172#endif
3173
3174    if (radioService[slotId]->mRadioResponse != NULL) {
3175        RadioResponseInfo responseInfo = {};
3176        populateResponseInfo(responseInfo, serial, responseType, e);
3177        Return<void> retStatus = radioService[slotId]->mRadioResponse->rejectCallResponse(
3178                responseInfo);
3179        radioService[slotId]->checkReturnStatus(retStatus);
3180    } else {
3181        RLOGE("rejectCallResponse: radioService[%d]->mRadioResponse == NULL",
3182                slotId);
3183    }
3184
3185    return 0;
3186}
3187
3188int radio::getLastCallFailCauseResponse(int slotId,
3189                                       int responseType, int serial, RIL_Errno e, void *response,
3190                                       size_t responseLen) {
3191#if VDBG
3192    RLOGD("getLastCallFailCauseResponse: serial %d", serial);
3193#endif
3194
3195    if (radioService[slotId]->mRadioResponse != NULL) {
3196        RadioResponseInfo responseInfo = {};
3197        populateResponseInfo(responseInfo, serial, responseType, e);
3198
3199        LastCallFailCauseInfo info = {};
3200        info.vendorCause = hidl_string();
3201        if (response == NULL) {
3202            RLOGE("getCurrentCallsResponse Invalid response: NULL");
3203            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3204        } else if (responseLen == sizeof(int)) {
3205            int *pInt = (int *) response;
3206            info.causeCode = (LastCallFailCause) pInt[0];
3207        } else if (responseLen == sizeof(RIL_LastCallFailCauseInfo))  {
3208            RIL_LastCallFailCauseInfo *pFailCauseInfo = (RIL_LastCallFailCauseInfo *) response;
3209            info.causeCode = (LastCallFailCause) pFailCauseInfo->cause_code;
3210            info.vendorCause = convertCharPtrToHidlString(pFailCauseInfo->vendor_cause);
3211        } else {
3212            RLOGE("getCurrentCallsResponse Invalid response: NULL");
3213            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3214        }
3215
3216        Return<void> retStatus = radioService[slotId]->mRadioResponse->getLastCallFailCauseResponse(
3217                responseInfo, info);
3218        radioService[slotId]->checkReturnStatus(retStatus);
3219    } else {
3220        RLOGE("getLastCallFailCauseResponse: radioService[%d]->mRadioResponse == NULL",
3221                slotId);
3222    }
3223
3224    return 0;
3225}
3226
3227int radio::getSignalStrengthResponse(int slotId,
3228                                     int responseType, int serial, RIL_Errno e,
3229                                     void *response, size_t responseLen) {
3230#if VDBG
3231    RLOGD("getSignalStrengthResponse: serial %d", serial);
3232#endif
3233
3234    if (radioService[slotId]->mRadioResponse != NULL) {
3235        RadioResponseInfo responseInfo = {};
3236        populateResponseInfo(responseInfo, serial, responseType, e);
3237        SignalStrength signalStrength = {};
3238        if (response == NULL || responseLen != sizeof(RIL_SignalStrength_v10)) {
3239            RLOGE("getSignalStrengthResponse: Invalid response");
3240            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3241        } else {
3242            convertRilSignalStrengthToHal(response, responseLen, signalStrength);
3243        }
3244
3245        Return<void> retStatus = radioService[slotId]->mRadioResponse->getSignalStrengthResponse(
3246                responseInfo, signalStrength);
3247        radioService[slotId]->checkReturnStatus(retStatus);
3248    } else {
3249        RLOGE("getSignalStrengthResponse: radioService[%d]->mRadioResponse == NULL",
3250                slotId);
3251    }
3252
3253    return 0;
3254}
3255
3256RIL_CellInfoType getCellInfoTypeRadioTechnology(char *rat) {
3257    if (rat == NULL) {
3258        return RIL_CELL_INFO_TYPE_NONE;
3259    }
3260
3261    int radioTech = atoi(rat);
3262
3263    switch(radioTech) {
3264
3265        case RADIO_TECH_GPRS:
3266        case RADIO_TECH_EDGE:
3267        case RADIO_TECH_GSM: {
3268            return RIL_CELL_INFO_TYPE_GSM;
3269        }
3270
3271        case RADIO_TECH_UMTS:
3272        case RADIO_TECH_HSDPA:
3273        case RADIO_TECH_HSUPA:
3274        case RADIO_TECH_HSPA:
3275        case RADIO_TECH_HSPAP: {
3276            return RIL_CELL_INFO_TYPE_WCDMA;
3277        }
3278
3279        case RADIO_TECH_IS95A:
3280        case RADIO_TECH_IS95B:
3281        case RADIO_TECH_1xRTT:
3282        case RADIO_TECH_EVDO_0:
3283        case RADIO_TECH_EVDO_A:
3284        case RADIO_TECH_EVDO_B:
3285        case RADIO_TECH_EHRPD: {
3286            return RIL_CELL_INFO_TYPE_CDMA;
3287        }
3288
3289        case RADIO_TECH_LTE:
3290        case RADIO_TECH_LTE_CA: {
3291            return RIL_CELL_INFO_TYPE_LTE;
3292        }
3293
3294        case RADIO_TECH_TD_SCDMA: {
3295            return RIL_CELL_INFO_TYPE_TD_SCDMA;
3296        }
3297
3298        default: {
3299            break;
3300        }
3301    }
3302
3303    return RIL_CELL_INFO_TYPE_NONE;
3304
3305}
3306
3307void fillCellIdentityResponse(CellIdentity &cellIdentity, RIL_CellIdentity_v16 &rilCellIdentity) {
3308
3309    cellIdentity.cellIdentityGsm.resize(0);
3310    cellIdentity.cellIdentityWcdma.resize(0);
3311    cellIdentity.cellIdentityCdma.resize(0);
3312    cellIdentity.cellIdentityTdscdma.resize(0);
3313    cellIdentity.cellIdentityLte.resize(0);
3314    cellIdentity.cellInfoType = (CellInfoType)rilCellIdentity.cellInfoType;
3315    switch(rilCellIdentity.cellInfoType) {
3316
3317        case RIL_CELL_INFO_TYPE_GSM: {
3318            cellIdentity.cellIdentityGsm.resize(1);
3319            cellIdentity.cellIdentityGsm[0].mcc =
3320                    std::to_string(rilCellIdentity.cellIdentityGsm.mcc);
3321            cellIdentity.cellIdentityGsm[0].mnc =
3322                    std::to_string(rilCellIdentity.cellIdentityGsm.mnc);
3323            cellIdentity.cellIdentityGsm[0].lac = rilCellIdentity.cellIdentityGsm.lac;
3324            cellIdentity.cellIdentityGsm[0].cid = rilCellIdentity.cellIdentityGsm.cid;
3325            cellIdentity.cellIdentityGsm[0].arfcn = rilCellIdentity.cellIdentityGsm.arfcn;
3326            cellIdentity.cellIdentityGsm[0].bsic = rilCellIdentity.cellIdentityGsm.bsic;
3327            break;
3328        }
3329
3330        case RIL_CELL_INFO_TYPE_WCDMA: {
3331            cellIdentity.cellIdentityWcdma.resize(1);
3332            cellIdentity.cellIdentityWcdma[0].mcc =
3333                    std::to_string(rilCellIdentity.cellIdentityWcdma.mcc);
3334            cellIdentity.cellIdentityWcdma[0].mnc =
3335                    std::to_string(rilCellIdentity.cellIdentityWcdma.mnc);
3336            cellIdentity.cellIdentityWcdma[0].lac = rilCellIdentity.cellIdentityWcdma.lac;
3337            cellIdentity.cellIdentityWcdma[0].cid = rilCellIdentity.cellIdentityWcdma.cid;
3338            cellIdentity.cellIdentityWcdma[0].psc = rilCellIdentity.cellIdentityWcdma.psc;
3339            cellIdentity.cellIdentityWcdma[0].uarfcn = rilCellIdentity.cellIdentityWcdma.uarfcn;
3340            break;
3341        }
3342
3343        case RIL_CELL_INFO_TYPE_CDMA: {
3344            cellIdentity.cellIdentityCdma.resize(1);
3345            cellIdentity.cellIdentityCdma[0].networkId = rilCellIdentity.cellIdentityCdma.networkId;
3346            cellIdentity.cellIdentityCdma[0].systemId = rilCellIdentity.cellIdentityCdma.systemId;
3347            cellIdentity.cellIdentityCdma[0].baseStationId =
3348                    rilCellIdentity.cellIdentityCdma.basestationId;
3349            cellIdentity.cellIdentityCdma[0].longitude = rilCellIdentity.cellIdentityCdma.longitude;
3350            cellIdentity.cellIdentityCdma[0].latitude = rilCellIdentity.cellIdentityCdma.latitude;
3351            break;
3352        }
3353
3354        case RIL_CELL_INFO_TYPE_LTE: {
3355            cellIdentity.cellIdentityLte.resize(1);
3356            cellIdentity.cellIdentityLte[0].mcc =
3357                    std::to_string(rilCellIdentity.cellIdentityLte.mcc);
3358            cellIdentity.cellIdentityLte[0].mnc =
3359                    std::to_string(rilCellIdentity.cellIdentityLte.mnc);
3360            cellIdentity.cellIdentityLte[0].ci = rilCellIdentity.cellIdentityLte.ci;
3361            cellIdentity.cellIdentityLte[0].pci = rilCellIdentity.cellIdentityLte.pci;
3362            cellIdentity.cellIdentityLte[0].tac = rilCellIdentity.cellIdentityLte.tac;
3363            cellIdentity.cellIdentityLte[0].earfcn = rilCellIdentity.cellIdentityLte.earfcn;
3364            break;
3365        }
3366
3367        case RIL_CELL_INFO_TYPE_TD_SCDMA: {
3368            cellIdentity.cellIdentityTdscdma.resize(1);
3369            cellIdentity.cellIdentityTdscdma[0].mcc =
3370                    std::to_string(rilCellIdentity.cellIdentityTdscdma.mcc);
3371            cellIdentity.cellIdentityTdscdma[0].mnc =
3372                    std::to_string(rilCellIdentity.cellIdentityTdscdma.mnc);
3373            cellIdentity.cellIdentityTdscdma[0].lac = rilCellIdentity.cellIdentityTdscdma.lac;
3374            cellIdentity.cellIdentityTdscdma[0].cid = rilCellIdentity.cellIdentityTdscdma.cid;
3375            cellIdentity.cellIdentityTdscdma[0].cpid = rilCellIdentity.cellIdentityTdscdma.cpid;
3376            break;
3377        }
3378
3379        default: {
3380            break;
3381        }
3382    }
3383}
3384
3385int convertResponseStringEntryToInt(char **response, int index, int numStrings) {
3386    if ((response != NULL) &&  (numStrings > index) && (response[index] != NULL)) {
3387        return atoi(response[index]);
3388    }
3389
3390    return -1;
3391}
3392
3393void fillCellIdentityFromVoiceRegStateResponseString(CellIdentity &cellIdentity,
3394        int numStrings, char** response) {
3395
3396    RIL_CellIdentity_v16 rilCellIdentity;
3397    memset(&rilCellIdentity, -1, sizeof(RIL_CellIdentity_v16));
3398
3399    rilCellIdentity.cellInfoType = getCellInfoTypeRadioTechnology(response[3]);
3400    switch(rilCellIdentity.cellInfoType) {
3401
3402        case RIL_CELL_INFO_TYPE_GSM: {
3403            rilCellIdentity.cellIdentityGsm.lac =
3404                    convertResponseStringEntryToInt(response, 1, numStrings);
3405            rilCellIdentity.cellIdentityGsm.cid =
3406                    convertResponseStringEntryToInt(response, 2, numStrings);
3407            break;
3408        }
3409
3410        case RIL_CELL_INFO_TYPE_WCDMA: {
3411            rilCellIdentity.cellIdentityWcdma.lac =
3412                    convertResponseStringEntryToInt(response, 1, numStrings);
3413            rilCellIdentity.cellIdentityWcdma.cid =
3414                    convertResponseStringEntryToInt(response, 2, numStrings);
3415            rilCellIdentity.cellIdentityWcdma.psc =
3416                    convertResponseStringEntryToInt(response, 14, numStrings);
3417            break;
3418        }
3419
3420        case RIL_CELL_INFO_TYPE_TD_SCDMA:{
3421            rilCellIdentity.cellIdentityTdscdma.lac =
3422                    convertResponseStringEntryToInt(response, 1, numStrings);
3423            rilCellIdentity.cellIdentityTdscdma.cid =
3424                    convertResponseStringEntryToInt(response, 2, numStrings);
3425            break;
3426        }
3427
3428        case RIL_CELL_INFO_TYPE_CDMA:{
3429            rilCellIdentity.cellIdentityCdma.basestationId =
3430                    convertResponseStringEntryToInt(response, 4, numStrings);
3431            rilCellIdentity.cellIdentityCdma.longitude =
3432                    convertResponseStringEntryToInt(response, 5, numStrings);
3433            rilCellIdentity.cellIdentityCdma.latitude =
3434                    convertResponseStringEntryToInt(response, 6, numStrings);
3435            rilCellIdentity.cellIdentityCdma.systemId =
3436                    convertResponseStringEntryToInt(response, 8, numStrings);
3437            rilCellIdentity.cellIdentityCdma.networkId =
3438                    convertResponseStringEntryToInt(response, 9, numStrings);
3439            break;
3440        }
3441
3442        case RIL_CELL_INFO_TYPE_LTE:{
3443            rilCellIdentity.cellIdentityLte.tac =
3444                    convertResponseStringEntryToInt(response, 1, numStrings);
3445            rilCellIdentity.cellIdentityLte.ci =
3446                    convertResponseStringEntryToInt(response, 2, numStrings);
3447            break;
3448        }
3449
3450        default: {
3451            break;
3452        }
3453    }
3454
3455    fillCellIdentityResponse(cellIdentity, rilCellIdentity);
3456}
3457
3458void fillCellIdentityFromDataRegStateResponseString(CellIdentity &cellIdentity,
3459        int numStrings, char** response) {
3460
3461    RIL_CellIdentity_v16 rilCellIdentity;
3462    memset(&rilCellIdentity, -1, sizeof(RIL_CellIdentity_v16));
3463
3464    rilCellIdentity.cellInfoType = getCellInfoTypeRadioTechnology(response[3]);
3465    switch(rilCellIdentity.cellInfoType) {
3466        case RIL_CELL_INFO_TYPE_GSM: {
3467            rilCellIdentity.cellIdentityGsm.lac =
3468                    convertResponseStringEntryToInt(response, 1, numStrings);
3469            rilCellIdentity.cellIdentityGsm.cid =
3470                    convertResponseStringEntryToInt(response, 2, numStrings);
3471            break;
3472        }
3473        case RIL_CELL_INFO_TYPE_WCDMA: {
3474            rilCellIdentity.cellIdentityWcdma.lac =
3475                    convertResponseStringEntryToInt(response, 1, numStrings);
3476            rilCellIdentity.cellIdentityWcdma.cid =
3477                    convertResponseStringEntryToInt(response, 2, numStrings);
3478            break;
3479        }
3480        case RIL_CELL_INFO_TYPE_TD_SCDMA:{
3481            rilCellIdentity.cellIdentityTdscdma.lac =
3482                    convertResponseStringEntryToInt(response, 1, numStrings);
3483            rilCellIdentity.cellIdentityTdscdma.cid =
3484                    convertResponseStringEntryToInt(response, 2, numStrings);
3485            break;
3486        }
3487        case RIL_CELL_INFO_TYPE_LTE: {
3488            rilCellIdentity.cellIdentityLte.tac =
3489                    convertResponseStringEntryToInt(response, 6, numStrings);
3490            rilCellIdentity.cellIdentityLte.pci =
3491                    convertResponseStringEntryToInt(response, 7, numStrings);
3492            rilCellIdentity.cellIdentityLte.ci =
3493                    convertResponseStringEntryToInt(response, 8, numStrings);
3494            break;
3495        }
3496        default: {
3497            break;
3498        }
3499    }
3500
3501    fillCellIdentityResponse(cellIdentity, rilCellIdentity);
3502}
3503
3504int radio::getVoiceRegistrationStateResponse(int slotId,
3505                                            int responseType, int serial, RIL_Errno e,
3506                                            void *response, size_t responseLen) {
3507#if VDBG
3508    RLOGD("getVoiceRegistrationStateResponse: serial %d", serial);
3509#endif
3510
3511    if (radioService[slotId]->mRadioResponse != NULL) {
3512        RadioResponseInfo responseInfo = {};
3513        populateResponseInfo(responseInfo, serial, responseType, e);
3514
3515        VoiceRegStateResult voiceRegResponse = {};
3516        int numStrings = responseLen / sizeof(char *);
3517        if (response == NULL) {
3518               RLOGE("getVoiceRegistrationStateResponse Invalid response: NULL");
3519               if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3520        } else if (s_vendorFunctions->version <= 14) {
3521            if (numStrings != 15) {
3522                RLOGE("getVoiceRegistrationStateResponse Invalid response: NULL");
3523                if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3524            } else {
3525                char **resp = (char **) response;
3526                voiceRegResponse.regState = (RegState) ATOI_NULL_HANDLED_DEF(resp[0], 4);
3527                voiceRegResponse.rat = ATOI_NULL_HANDLED(resp[3]);
3528                voiceRegResponse.cssSupported = ATOI_NULL_HANDLED_DEF(resp[7], 0);
3529                voiceRegResponse.roamingIndicator = ATOI_NULL_HANDLED(resp[10]);
3530                voiceRegResponse.systemIsInPrl = ATOI_NULL_HANDLED_DEF(resp[11], 0);
3531                voiceRegResponse.defaultRoamingIndicator = ATOI_NULL_HANDLED_DEF(resp[12], 0);
3532                voiceRegResponse.reasonForDenial = ATOI_NULL_HANDLED_DEF(resp[13], 0);
3533                fillCellIdentityFromVoiceRegStateResponseString(voiceRegResponse.cellIdentity,
3534                        numStrings, resp);
3535            }
3536        } else {
3537            RIL_VoiceRegistrationStateResponse *voiceRegState =
3538                    (RIL_VoiceRegistrationStateResponse *)response;
3539
3540            if (responseLen != sizeof(RIL_VoiceRegistrationStateResponse)) {
3541                RLOGE("getVoiceRegistrationStateResponse Invalid response: NULL");
3542                if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3543            } else {
3544                voiceRegResponse.regState = (RegState) voiceRegState->regState;
3545                voiceRegResponse.rat = voiceRegState->rat;;
3546                voiceRegResponse.cssSupported = voiceRegState->cssSupported;
3547                voiceRegResponse.roamingIndicator = voiceRegState->roamingIndicator;
3548                voiceRegResponse.systemIsInPrl = voiceRegState->systemIsInPrl;
3549                voiceRegResponse.defaultRoamingIndicator = voiceRegState->defaultRoamingIndicator;
3550                voiceRegResponse.reasonForDenial = voiceRegState->reasonForDenial;
3551                fillCellIdentityResponse(voiceRegResponse.cellIdentity,
3552                        voiceRegState->cellIdentity);
3553            }
3554        }
3555
3556        Return<void> retStatus =
3557                radioService[slotId]->mRadioResponse->getVoiceRegistrationStateResponse(
3558                responseInfo, voiceRegResponse);
3559        radioService[slotId]->checkReturnStatus(retStatus);
3560    } else {
3561        RLOGE("getVoiceRegistrationStateResponse: radioService[%d]->mRadioResponse == NULL",
3562                slotId);
3563    }
3564
3565    return 0;
3566}
3567
3568int radio::getDataRegistrationStateResponse(int slotId,
3569                                           int responseType, int serial, RIL_Errno e,
3570                                           void *response, size_t responseLen) {
3571#if VDBG
3572    RLOGD("getDataRegistrationStateResponse: serial %d", serial);
3573#endif
3574
3575    if (radioService[slotId]->mRadioResponse != NULL) {
3576        RadioResponseInfo responseInfo = {};
3577        populateResponseInfo(responseInfo, serial, responseType, e);
3578        DataRegStateResult dataRegResponse = {};
3579        if (response == NULL) {
3580            RLOGE("getDataRegistrationStateResponse Invalid response: NULL");
3581            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3582        } else if (s_vendorFunctions->version <= 14) {
3583            int numStrings = responseLen / sizeof(char *);
3584            if ((numStrings != 6) && (numStrings != 11)) {
3585                RLOGE("getDataRegistrationStateResponse Invalid response: NULL");
3586                if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3587            } else {
3588                char **resp = (char **) response;
3589                dataRegResponse.regState = (RegState) ATOI_NULL_HANDLED_DEF(resp[0], 4);
3590                dataRegResponse.rat =  ATOI_NULL_HANDLED_DEF(resp[3], 0);
3591                dataRegResponse.reasonDataDenied =  ATOI_NULL_HANDLED(resp[4]);
3592                dataRegResponse.maxDataCalls =  ATOI_NULL_HANDLED_DEF(resp[5], 1);
3593                fillCellIdentityFromDataRegStateResponseString(dataRegResponse.cellIdentity,
3594                        numStrings, resp);
3595            }
3596        } else {
3597            RIL_DataRegistrationStateResponse *dataRegState =
3598                    (RIL_DataRegistrationStateResponse *)response;
3599
3600            if (responseLen != sizeof(RIL_DataRegistrationStateResponse)) {
3601                RLOGE("getDataRegistrationStateResponse Invalid response: NULL");
3602                if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3603            } else {
3604                dataRegResponse.regState = (RegState) dataRegState->regState;
3605                dataRegResponse.rat = dataRegState->rat;;
3606                dataRegResponse.reasonDataDenied = dataRegState->reasonDataDenied;
3607                dataRegResponse.maxDataCalls = dataRegState->maxDataCalls;
3608                fillCellIdentityResponse(dataRegResponse.cellIdentity, dataRegState->cellIdentity);
3609            }
3610        }
3611
3612        Return<void> retStatus =
3613                radioService[slotId]->mRadioResponse->getDataRegistrationStateResponse(responseInfo,
3614                dataRegResponse);
3615        radioService[slotId]->checkReturnStatus(retStatus);
3616    } else {
3617        RLOGE("getDataRegistrationStateResponse: radioService[%d]->mRadioResponse == NULL",
3618                slotId);
3619    }
3620
3621    return 0;
3622}
3623
3624int radio::getOperatorResponse(int slotId,
3625                              int responseType, int serial, RIL_Errno e, void *response,
3626                              size_t responseLen) {
3627#if VDBG
3628    RLOGD("getOperatorResponse: serial %d", serial);
3629#endif
3630
3631    if (radioService[slotId]->mRadioResponse != NULL) {
3632        RadioResponseInfo responseInfo = {};
3633        populateResponseInfo(responseInfo, serial, responseType, e);
3634        hidl_string longName;
3635        hidl_string shortName;
3636        hidl_string numeric;
3637        int numStrings = responseLen / sizeof(char *);
3638        if (response == NULL || numStrings != 3) {
3639            RLOGE("getOperatorResponse Invalid response: NULL");
3640            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3641
3642        } else {
3643            char **resp = (char **) response;
3644            longName = convertCharPtrToHidlString(resp[0]);
3645            shortName = convertCharPtrToHidlString(resp[1]);
3646            numeric = convertCharPtrToHidlString(resp[2]);
3647        }
3648        Return<void> retStatus = radioService[slotId]->mRadioResponse->getOperatorResponse(
3649                responseInfo, longName, shortName, numeric);
3650        radioService[slotId]->checkReturnStatus(retStatus);
3651    } else {
3652        RLOGE("getOperatorResponse: radioService[%d]->mRadioResponse == NULL",
3653                slotId);
3654    }
3655
3656    return 0;
3657}
3658
3659int radio::setRadioPowerResponse(int slotId,
3660                                int responseType, int serial, RIL_Errno e, void *response,
3661                                size_t responseLen) {
3662    RLOGD("setRadioPowerResponse: serial %d", serial);
3663
3664    if (radioService[slotId]->mRadioResponse != NULL) {
3665        RadioResponseInfo responseInfo = {};
3666        populateResponseInfo(responseInfo, serial, responseType, e);
3667        Return<void> retStatus = radioService[slotId]->mRadioResponse->setRadioPowerResponse(
3668                responseInfo);
3669        radioService[slotId]->checkReturnStatus(retStatus);
3670    } else {
3671        RLOGE("setRadioPowerResponse: radioService[%d]->mRadioResponse == NULL",
3672                slotId);
3673    }
3674
3675    return 0;
3676}
3677
3678int radio::sendDtmfResponse(int slotId,
3679                           int responseType, int serial, RIL_Errno e, void *response,
3680                           size_t responseLen) {
3681#if VDBG
3682    RLOGD("sendDtmfResponse: serial %d", serial);
3683#endif
3684
3685    if (radioService[slotId]->mRadioResponse != NULL) {
3686        RadioResponseInfo responseInfo = {};
3687        populateResponseInfo(responseInfo, serial, responseType, e);
3688        Return<void> retStatus = radioService[slotId]->mRadioResponse->sendDtmfResponse(
3689                responseInfo);
3690        radioService[slotId]->checkReturnStatus(retStatus);
3691    } else {
3692        RLOGE("sendDtmfResponse: radioService[%d]->mRadioResponse == NULL",
3693                slotId);
3694    }
3695
3696    return 0;
3697}
3698
3699SendSmsResult makeSendSmsResult(RadioResponseInfo& responseInfo, int serial, int responseType,
3700                                RIL_Errno e, void *response, size_t responseLen) {
3701    populateResponseInfo(responseInfo, serial, responseType, e);
3702    SendSmsResult result = {};
3703
3704    if (response == NULL || responseLen != sizeof(RIL_SMS_Response)) {
3705        RLOGE("Invalid response: NULL");
3706        if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3707        result.ackPDU = hidl_string();
3708    } else {
3709        RIL_SMS_Response *resp = (RIL_SMS_Response *) response;
3710        result.messageRef = resp->messageRef;
3711        result.ackPDU = convertCharPtrToHidlString(resp->ackPDU);
3712        result.errorCode = resp->errorCode;
3713    }
3714    return result;
3715}
3716
3717int radio::sendSmsResponse(int slotId,
3718                          int responseType, int serial, RIL_Errno e, void *response,
3719                          size_t responseLen) {
3720#if VDBG
3721    RLOGD("sendSmsResponse: serial %d", serial);
3722#endif
3723
3724    if (radioService[slotId]->mRadioResponse != NULL) {
3725        RadioResponseInfo responseInfo = {};
3726        SendSmsResult result = makeSendSmsResult(responseInfo, serial, responseType, e, response,
3727                responseLen);
3728
3729        Return<void> retStatus = radioService[slotId]->mRadioResponse->sendSmsResponse(responseInfo,
3730                result);
3731        radioService[slotId]->checkReturnStatus(retStatus);
3732    } else {
3733        RLOGE("sendSmsResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3734    }
3735
3736    return 0;
3737}
3738
3739int radio::sendSMSExpectMoreResponse(int slotId,
3740                                    int responseType, int serial, RIL_Errno e, void *response,
3741                                    size_t responseLen) {
3742#if VDBG
3743    RLOGD("sendSMSExpectMoreResponse: serial %d", serial);
3744#endif
3745
3746    if (radioService[slotId]->mRadioResponse != NULL) {
3747        RadioResponseInfo responseInfo = {};
3748        SendSmsResult result = makeSendSmsResult(responseInfo, serial, responseType, e, response,
3749                responseLen);
3750
3751        Return<void> retStatus = radioService[slotId]->mRadioResponse->sendSMSExpectMoreResponse(
3752                responseInfo, result);
3753        radioService[slotId]->checkReturnStatus(retStatus);
3754    } else {
3755        RLOGE("sendSMSExpectMoreResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3756    }
3757
3758    return 0;
3759}
3760
3761int radio::setupDataCallResponse(int slotId,
3762                                 int responseType, int serial, RIL_Errno e, void *response,
3763                                 size_t responseLen) {
3764#if VDBG
3765    RLOGD("setupDataCallResponse: serial %d", serial);
3766#endif
3767
3768    if (radioService[slotId]->mRadioResponse != NULL) {
3769        RadioResponseInfo responseInfo = {};
3770        populateResponseInfo(responseInfo, serial, responseType, e);
3771
3772        SetupDataCallResult result = {};
3773        if (response == NULL || responseLen != sizeof(RIL_Data_Call_Response_v11)) {
3774            RLOGE("setupDataCallResponse: Invalid response");
3775            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3776            result.status = DataCallFailCause::ERROR_UNSPECIFIED;
3777            result.type = hidl_string();
3778            result.ifname = hidl_string();
3779            result.addresses = hidl_string();
3780            result.dnses = hidl_string();
3781            result.gateways = hidl_string();
3782            result.pcscf = hidl_string();
3783        } else {
3784            convertRilDataCallToHal((RIL_Data_Call_Response_v11 *) response, result);
3785        }
3786
3787        Return<void> retStatus = radioService[slotId]->mRadioResponse->setupDataCallResponse(
3788                responseInfo, result);
3789        radioService[slotId]->checkReturnStatus(retStatus);
3790    } else {
3791        RLOGE("setupDataCallResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3792    }
3793
3794    return 0;
3795}
3796
3797IccIoResult responseIccIo(RadioResponseInfo& responseInfo, int serial, int responseType,
3798                           RIL_Errno e, void *response, size_t responseLen) {
3799    populateResponseInfo(responseInfo, serial, responseType, e);
3800    IccIoResult result = {};
3801
3802    if (response == NULL || responseLen != sizeof(RIL_SIM_IO_Response)) {
3803        RLOGE("Invalid response: NULL");
3804        if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3805        result.simResponse = hidl_string();
3806    } else {
3807        RIL_SIM_IO_Response *resp = (RIL_SIM_IO_Response *) response;
3808        result.sw1 = resp->sw1;
3809        result.sw2 = resp->sw2;
3810        result.simResponse = convertCharPtrToHidlString(resp->simResponse);
3811    }
3812    return result;
3813}
3814
3815int radio::iccIOForAppResponse(int slotId,
3816                      int responseType, int serial, RIL_Errno e, void *response,
3817                      size_t responseLen) {
3818#if VDBG
3819    RLOGD("iccIOForAppResponse: serial %d", serial);
3820#endif
3821
3822    if (radioService[slotId]->mRadioResponse != NULL) {
3823        RadioResponseInfo responseInfo = {};
3824        IccIoResult result = responseIccIo(responseInfo, serial, responseType, e, response,
3825                responseLen);
3826
3827        Return<void> retStatus = radioService[slotId]->mRadioResponse->iccIOForAppResponse(
3828                responseInfo, result);
3829        radioService[slotId]->checkReturnStatus(retStatus);
3830    } else {
3831        RLOGE("iccIOForAppResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3832    }
3833
3834    return 0;
3835}
3836
3837int radio::sendUssdResponse(int slotId,
3838                           int responseType, int serial, RIL_Errno e, void *response,
3839                           size_t responseLen) {
3840#if VDBG
3841    RLOGD("sendUssdResponse: serial %d", serial);
3842#endif
3843
3844    if (radioService[slotId]->mRadioResponse != NULL) {
3845        RadioResponseInfo responseInfo = {};
3846        populateResponseInfo(responseInfo, serial, responseType, e);
3847        Return<void> retStatus = radioService[slotId]->mRadioResponse->sendUssdResponse(
3848                responseInfo);
3849        radioService[slotId]->checkReturnStatus(retStatus);
3850    } else {
3851        RLOGE("sendUssdResponse: radioService[%d]->mRadioResponse == NULL",
3852                slotId);
3853    }
3854
3855    return 0;
3856}
3857
3858int radio::cancelPendingUssdResponse(int slotId,
3859                                    int responseType, int serial, RIL_Errno e, void *response,
3860                                    size_t responseLen) {
3861#if VDBG
3862    RLOGD("cancelPendingUssdResponse: serial %d", serial);
3863#endif
3864
3865    if (radioService[slotId]->mRadioResponse != NULL) {
3866        RadioResponseInfo responseInfo = {};
3867        populateResponseInfo(responseInfo, serial, responseType, e);
3868        Return<void> retStatus = radioService[slotId]->mRadioResponse->cancelPendingUssdResponse(
3869                responseInfo);
3870        radioService[slotId]->checkReturnStatus(retStatus);
3871    } else {
3872        RLOGE("cancelPendingUssdResponse: radioService[%d]->mRadioResponse == NULL",
3873                slotId);
3874    }
3875
3876    return 0;
3877}
3878
3879int radio::getClirResponse(int slotId,
3880                              int responseType, int serial, RIL_Errno e, void *response,
3881                              size_t responseLen) {
3882#if VDBG
3883    RLOGD("getClirResponse: serial %d", serial);
3884#endif
3885
3886    if (radioService[slotId]->mRadioResponse != NULL) {
3887        RadioResponseInfo responseInfo = {};
3888        populateResponseInfo(responseInfo, serial, responseType, e);
3889        int n = -1, m = -1;
3890        int numInts = responseLen / sizeof(int);
3891        if (response == NULL || numInts != 2) {
3892            RLOGE("getClirResponse Invalid response: NULL");
3893            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3894        } else {
3895            int *pInt = (int *) response;
3896            n = pInt[0];
3897            m = pInt[1];
3898        }
3899        Return<void> retStatus = radioService[slotId]->mRadioResponse->getClirResponse(responseInfo,
3900                n, m);
3901        radioService[slotId]->checkReturnStatus(retStatus);
3902    } else {
3903        RLOGE("getClirResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3904    }
3905
3906    return 0;
3907}
3908
3909int radio::setClirResponse(int slotId,
3910                          int responseType, int serial, RIL_Errno e, void *response,
3911                          size_t responseLen) {
3912#if VDBG
3913    RLOGD("setClirResponse: serial %d", serial);
3914#endif
3915
3916    if (radioService[slotId]->mRadioResponse != NULL) {
3917        RadioResponseInfo responseInfo = {};
3918        populateResponseInfo(responseInfo, serial, responseType, e);
3919        Return<void> retStatus = radioService[slotId]->mRadioResponse->setClirResponse(
3920                responseInfo);
3921        radioService[slotId]->checkReturnStatus(retStatus);
3922    } else {
3923        RLOGE("setClirResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3924    }
3925
3926    return 0;
3927}
3928
3929int radio::getCallForwardStatusResponse(int slotId,
3930                                       int responseType, int serial, RIL_Errno e,
3931                                       void *response, size_t responseLen) {
3932#if VDBG
3933    RLOGD("getCallForwardStatusResponse: serial %d", serial);
3934#endif
3935
3936    if (radioService[slotId]->mRadioResponse != NULL) {
3937        RadioResponseInfo responseInfo = {};
3938        populateResponseInfo(responseInfo, serial, responseType, e);
3939        hidl_vec<CallForwardInfo> callForwardInfos;
3940
3941        if (response == NULL || responseLen % sizeof(RIL_CallForwardInfo *) != 0) {
3942            RLOGE("getCallForwardStatusResponse Invalid response: NULL");
3943            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
3944        } else {
3945            int num = responseLen / sizeof(RIL_CallForwardInfo *);
3946            callForwardInfos.resize(num);
3947            for (int i = 0 ; i < num; i++) {
3948                RIL_CallForwardInfo *resp = ((RIL_CallForwardInfo **) response)[i];
3949                callForwardInfos[i].status = (CallForwardInfoStatus) resp->status;
3950                callForwardInfos[i].reason = resp->reason;
3951                callForwardInfos[i].serviceClass = resp->serviceClass;
3952                callForwardInfos[i].toa = resp->toa;
3953                callForwardInfos[i].number = convertCharPtrToHidlString(resp->number);
3954                callForwardInfos[i].timeSeconds = resp->timeSeconds;
3955            }
3956        }
3957
3958        Return<void> retStatus = radioService[slotId]->mRadioResponse->getCallForwardStatusResponse(
3959                responseInfo, callForwardInfos);
3960        radioService[slotId]->checkReturnStatus(retStatus);
3961    } else {
3962        RLOGE("getCallForwardStatusResponse: radioService[%d]->mRadioResponse == NULL",
3963                slotId);
3964    }
3965
3966    return 0;
3967}
3968
3969int radio::setCallForwardResponse(int slotId,
3970                                 int responseType, int serial, RIL_Errno e, void *response,
3971                                 size_t responseLen) {
3972#if VDBG
3973    RLOGD("setCallForwardResponse: serial %d", serial);
3974#endif
3975
3976    if (radioService[slotId]->mRadioResponse != NULL) {
3977        RadioResponseInfo responseInfo = {};
3978        populateResponseInfo(responseInfo, serial, responseType, e);
3979        Return<void> retStatus = radioService[slotId]->mRadioResponse->setCallForwardResponse(
3980                responseInfo);
3981        radioService[slotId]->checkReturnStatus(retStatus);
3982    } else {
3983        RLOGE("setCallForwardResponse: radioService[%d]->mRadioResponse == NULL", slotId);
3984    }
3985
3986    return 0;
3987}
3988
3989int radio::getCallWaitingResponse(int slotId,
3990                                 int responseType, int serial, RIL_Errno e, void *response,
3991                                 size_t responseLen) {
3992#if VDBG
3993    RLOGD("getCallWaitingResponse: serial %d", serial);
3994#endif
3995
3996    if (radioService[slotId]->mRadioResponse != NULL) {
3997        RadioResponseInfo responseInfo = {};
3998        populateResponseInfo(responseInfo, serial, responseType, e);
3999        bool enable = false;
4000        int serviceClass = -1;
4001        int numInts = responseLen / sizeof(int);
4002        if (response == NULL || numInts != 2) {
4003            RLOGE("getCallWaitingResponse Invalid response: NULL");
4004            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4005        } else {
4006            int *pInt = (int *) response;
4007            enable = pInt[0] == 1 ? true : false;
4008            serviceClass = pInt[1];
4009        }
4010        Return<void> retStatus = radioService[slotId]->mRadioResponse->getCallWaitingResponse(
4011                responseInfo, enable, serviceClass);
4012        radioService[slotId]->checkReturnStatus(retStatus);
4013    } else {
4014        RLOGE("getCallWaitingResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4015    }
4016
4017    return 0;
4018}
4019
4020int radio::setCallWaitingResponse(int slotId,
4021                                 int responseType, int serial, RIL_Errno e, void *response,
4022                                 size_t responseLen) {
4023#if VDBG
4024    RLOGD("setCallWaitingResponse: serial %d", serial);
4025#endif
4026
4027    if (radioService[slotId]->mRadioResponse != NULL) {
4028        RadioResponseInfo responseInfo = {};
4029        populateResponseInfo(responseInfo, serial, responseType, e);
4030        Return<void> retStatus = radioService[slotId]->mRadioResponse->setCallWaitingResponse(
4031                responseInfo);
4032        radioService[slotId]->checkReturnStatus(retStatus);
4033    } else {
4034        RLOGE("setCallWaitingResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4035    }
4036
4037    return 0;
4038}
4039
4040int radio::acknowledgeLastIncomingGsmSmsResponse(int slotId,
4041                                                int responseType, int serial, RIL_Errno e,
4042                                                void *response, size_t responseLen) {
4043#if VDBG
4044    RLOGD("acknowledgeLastIncomingGsmSmsResponse: serial %d", serial);
4045#endif
4046
4047    if (radioService[slotId]->mRadioResponse != NULL) {
4048        RadioResponseInfo responseInfo = {};
4049        populateResponseInfo(responseInfo, serial, responseType, e);
4050        Return<void> retStatus =
4051                radioService[slotId]->mRadioResponse->acknowledgeLastIncomingGsmSmsResponse(
4052                responseInfo);
4053        radioService[slotId]->checkReturnStatus(retStatus);
4054    } else {
4055        RLOGE("acknowledgeLastIncomingGsmSmsResponse: radioService[%d]->mRadioResponse "
4056                "== NULL", slotId);
4057    }
4058
4059    return 0;
4060}
4061
4062int radio::acceptCallResponse(int slotId,
4063                             int responseType, int serial, RIL_Errno e,
4064                             void *response, size_t responseLen) {
4065#if VDBG
4066    RLOGD("acceptCallResponse: serial %d", serial);
4067#endif
4068
4069    if (radioService[slotId]->mRadioResponse != NULL) {
4070        RadioResponseInfo responseInfo = {};
4071        populateResponseInfo(responseInfo, serial, responseType, e);
4072        Return<void> retStatus = radioService[slotId]->mRadioResponse->acceptCallResponse(
4073                responseInfo);
4074        radioService[slotId]->checkReturnStatus(retStatus);
4075    } else {
4076        RLOGE("acceptCallResponse: radioService[%d]->mRadioResponse == NULL",
4077                slotId);
4078    }
4079
4080    return 0;
4081}
4082
4083int radio::deactivateDataCallResponse(int slotId,
4084                                                int responseType, int serial, RIL_Errno e,
4085                                                void *response, size_t responseLen) {
4086#if VDBG
4087    RLOGD("deactivateDataCallResponse: serial %d", serial);
4088#endif
4089
4090    if (radioService[slotId]->mRadioResponse != NULL) {
4091        RadioResponseInfo responseInfo = {};
4092        populateResponseInfo(responseInfo, serial, responseType, e);
4093        Return<void> retStatus = radioService[slotId]->mRadioResponse->deactivateDataCallResponse(
4094                responseInfo);
4095        radioService[slotId]->checkReturnStatus(retStatus);
4096    } else {
4097        RLOGE("deactivateDataCallResponse: radioService[%d]->mRadioResponse == NULL",
4098                slotId);
4099    }
4100
4101    return 0;
4102}
4103
4104int radio::getFacilityLockForAppResponse(int slotId,
4105                                        int responseType, int serial, RIL_Errno e,
4106                                        void *response, size_t responseLen) {
4107#if VDBG
4108    RLOGD("getFacilityLockForAppResponse: serial %d", serial);
4109#endif
4110
4111    if (radioService[slotId]->mRadioResponse != NULL) {
4112        RadioResponseInfo responseInfo = {};
4113        int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
4114        Return<void> retStatus = radioService[slotId]->mRadioResponse->
4115                getFacilityLockForAppResponse(responseInfo, ret);
4116        radioService[slotId]->checkReturnStatus(retStatus);
4117    } else {
4118        RLOGE("getFacilityLockForAppResponse: radioService[%d]->mRadioResponse == NULL",
4119                slotId);
4120    }
4121
4122    return 0;
4123}
4124
4125int radio::setFacilityLockForAppResponse(int slotId,
4126                                      int responseType, int serial, RIL_Errno e,
4127                                      void *response, size_t responseLen) {
4128#if VDBG
4129    RLOGD("setFacilityLockForAppResponse: serial %d", serial);
4130#endif
4131
4132    if (radioService[slotId]->mRadioResponse != NULL) {
4133        RadioResponseInfo responseInfo = {};
4134        int ret = responseIntOrEmpty(responseInfo, serial, responseType, e, response, responseLen);
4135        Return<void> retStatus
4136                = radioService[slotId]->mRadioResponse->setFacilityLockForAppResponse(responseInfo,
4137                ret);
4138        radioService[slotId]->checkReturnStatus(retStatus);
4139    } else {
4140        RLOGE("setFacilityLockForAppResponse: radioService[%d]->mRadioResponse == NULL",
4141                slotId);
4142    }
4143
4144    return 0;
4145}
4146
4147int radio::setBarringPasswordResponse(int slotId,
4148                             int responseType, int serial, RIL_Errno e,
4149                             void *response, size_t responseLen) {
4150#if VDBG
4151    RLOGD("acceptCallResponse: serial %d", serial);
4152#endif
4153
4154    if (radioService[slotId]->mRadioResponse != NULL) {
4155        RadioResponseInfo responseInfo = {};
4156        populateResponseInfo(responseInfo, serial, responseType, e);
4157        Return<void> retStatus
4158                = radioService[slotId]->mRadioResponse->setBarringPasswordResponse(responseInfo);
4159        radioService[slotId]->checkReturnStatus(retStatus);
4160    } else {
4161        RLOGE("setBarringPasswordResponse: radioService[%d]->mRadioResponse == NULL",
4162                slotId);
4163    }
4164
4165    return 0;
4166}
4167
4168int radio::getNetworkSelectionModeResponse(int slotId,
4169                                          int responseType, int serial, RIL_Errno e, void *response,
4170                                          size_t responseLen) {
4171#if VDBG
4172    RLOGD("getNetworkSelectionModeResponse: serial %d", serial);
4173#endif
4174
4175    if (radioService[slotId]->mRadioResponse != NULL) {
4176        RadioResponseInfo responseInfo = {};
4177        populateResponseInfo(responseInfo, serial, responseType, e);
4178        bool manual = false;
4179        int serviceClass;
4180        if (response == NULL || responseLen != sizeof(int)) {
4181            RLOGE("getNetworkSelectionModeResponse Invalid response: NULL");
4182            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4183        } else {
4184            int *pInt = (int *) response;
4185            manual = pInt[0] == 1 ? true : false;
4186        }
4187        Return<void> retStatus
4188                = radioService[slotId]->mRadioResponse->getNetworkSelectionModeResponse(
4189                responseInfo,
4190                manual);
4191        radioService[slotId]->checkReturnStatus(retStatus);
4192    } else {
4193        RLOGE("getNetworkSelectionModeResponse: radioService[%d]->mRadioResponse == NULL",
4194                slotId);
4195    }
4196
4197    return 0;
4198}
4199
4200int radio::setNetworkSelectionModeAutomaticResponse(int slotId, int responseType, int serial,
4201                                                    RIL_Errno e, void *response,
4202                                                    size_t responseLen) {
4203#if VDBG
4204    RLOGD("setNetworkSelectionModeAutomaticResponse: serial %d", serial);
4205#endif
4206
4207    if (radioService[slotId]->mRadioResponse != NULL) {
4208        RadioResponseInfo responseInfo = {};
4209        populateResponseInfo(responseInfo, serial, responseType, e);
4210        Return<void> retStatus
4211                = radioService[slotId]->mRadioResponse->setNetworkSelectionModeAutomaticResponse(
4212                responseInfo);
4213        radioService[slotId]->checkReturnStatus(retStatus);
4214    } else {
4215        RLOGE("setNetworkSelectionModeAutomaticResponse: radioService[%d]->mRadioResponse "
4216                "== NULL", slotId);
4217    }
4218
4219    return 0;
4220}
4221
4222int radio::setNetworkSelectionModeManualResponse(int slotId,
4223                             int responseType, int serial, RIL_Errno e,
4224                             void *response, size_t responseLen) {
4225#if VDBG
4226    RLOGD("setNetworkSelectionModeManualResponse: serial %d", serial);
4227#endif
4228
4229    if (radioService[slotId]->mRadioResponse != NULL) {
4230        RadioResponseInfo responseInfo = {};
4231        populateResponseInfo(responseInfo, serial, responseType, e);
4232        Return<void> retStatus
4233                = radioService[slotId]->mRadioResponse->setNetworkSelectionModeManualResponse(
4234                responseInfo);
4235        radioService[slotId]->checkReturnStatus(retStatus);
4236    } else {
4237        RLOGE("acceptCallResponse: radioService[%d]->setNetworkSelectionModeManualResponse "
4238                "== NULL", slotId);
4239    }
4240
4241    return 0;
4242}
4243
4244int convertOperatorStatusToInt(const char *str) {
4245    if (strncmp("unknown", str, 9) == 0) {
4246        return (int) OperatorStatus::UNKNOWN;
4247    } else if (strncmp("available", str, 9) == 0) {
4248        return (int) OperatorStatus::AVAILABLE;
4249    } else if (strncmp("current", str, 9) == 0) {
4250        return (int) OperatorStatus::CURRENT;
4251    } else if (strncmp("forbidden", str, 9) == 0) {
4252        return (int) OperatorStatus::FORBIDDEN;
4253    } else {
4254        return -1;
4255    }
4256}
4257
4258int radio::getAvailableNetworksResponse(int slotId,
4259                              int responseType, int serial, RIL_Errno e, void *response,
4260                              size_t responseLen) {
4261#if VDBG
4262    RLOGD("getAvailableNetworksResponse: serial %d", serial);
4263#endif
4264
4265    if (radioService[slotId]->mRadioResponse != NULL) {
4266        RadioResponseInfo responseInfo = {};
4267        populateResponseInfo(responseInfo, serial, responseType, e);
4268        hidl_vec<OperatorInfo> networks;
4269        if (response == NULL || responseLen % (4 * sizeof(char *))!= 0) {
4270            RLOGE("getAvailableNetworksResponse Invalid response: NULL");
4271            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4272        } else {
4273            char **resp = (char **) response;
4274            int numStrings = responseLen / sizeof(char *);
4275            networks.resize(numStrings/4);
4276            for (int i = 0, j = 0; i < numStrings; i = i + 4, j++) {
4277                networks[j].alphaLong = convertCharPtrToHidlString(resp[i]);
4278                networks[j].alphaShort = convertCharPtrToHidlString(resp[i + 1]);
4279                networks[j].operatorNumeric = convertCharPtrToHidlString(resp[i + 2]);
4280                int status = convertOperatorStatusToInt(resp[i + 3]);
4281                if (status == -1) {
4282                    if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4283                } else {
4284                    networks[j].status = (OperatorStatus) status;
4285                }
4286            }
4287        }
4288        Return<void> retStatus
4289                = radioService[slotId]->mRadioResponse->getAvailableNetworksResponse(responseInfo,
4290                networks);
4291        radioService[slotId]->checkReturnStatus(retStatus);
4292    } else {
4293        RLOGE("getAvailableNetworksResponse: radioService[%d]->mRadioResponse == NULL",
4294                slotId);
4295    }
4296
4297    return 0;
4298}
4299
4300int radio::startDtmfResponse(int slotId,
4301                            int responseType, int serial, RIL_Errno e,
4302                            void *response, size_t responseLen) {
4303#if VDBG
4304    RLOGD("startDtmfResponse: serial %d", serial);
4305#endif
4306
4307    if (radioService[slotId]->mRadioResponse != NULL) {
4308        RadioResponseInfo responseInfo = {};
4309        populateResponseInfo(responseInfo, serial, responseType, e);
4310        Return<void> retStatus
4311                = radioService[slotId]->mRadioResponse->startDtmfResponse(responseInfo);
4312        radioService[slotId]->checkReturnStatus(retStatus);
4313    } else {
4314        RLOGE("startDtmfResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4315    }
4316
4317    return 0;
4318}
4319
4320int radio::stopDtmfResponse(int slotId,
4321                           int responseType, int serial, RIL_Errno e,
4322                           void *response, size_t responseLen) {
4323#if VDBG
4324    RLOGD("stopDtmfResponse: serial %d", serial);
4325#endif
4326
4327    if (radioService[slotId]->mRadioResponse != NULL) {
4328        RadioResponseInfo responseInfo = {};
4329        populateResponseInfo(responseInfo, serial, responseType, e);
4330        Return<void> retStatus
4331                = radioService[slotId]->mRadioResponse->stopDtmfResponse(responseInfo);
4332        radioService[slotId]->checkReturnStatus(retStatus);
4333    } else {
4334        RLOGE("stopDtmfResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4335    }
4336
4337    return 0;
4338}
4339
4340int radio::getBasebandVersionResponse(int slotId,
4341                                     int responseType, int serial, RIL_Errno e,
4342                                     void *response, size_t responseLen) {
4343#if VDBG
4344    RLOGD("getBasebandVersionResponse: serial %d", serial);
4345#endif
4346
4347    if (radioService[slotId]->mRadioResponse != NULL) {
4348        RadioResponseInfo responseInfo = {};
4349        populateResponseInfo(responseInfo, serial, responseType, e);
4350        Return<void> retStatus
4351                = radioService[slotId]->mRadioResponse->getBasebandVersionResponse(responseInfo,
4352                convertCharPtrToHidlString((char *) response));
4353        radioService[slotId]->checkReturnStatus(retStatus);
4354    } else {
4355        RLOGE("getBasebandVersionResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4356    }
4357
4358    return 0;
4359}
4360
4361int radio::separateConnectionResponse(int slotId,
4362                                     int responseType, int serial, RIL_Errno e,
4363                                     void *response, size_t responseLen) {
4364#if VDBG
4365    RLOGD("separateConnectionResponse: serial %d", serial);
4366#endif
4367
4368    if (radioService[slotId]->mRadioResponse != NULL) {
4369        RadioResponseInfo responseInfo = {};
4370        populateResponseInfo(responseInfo, serial, responseType, e);
4371        Return<void> retStatus
4372                = radioService[slotId]->mRadioResponse->separateConnectionResponse(responseInfo);
4373        radioService[slotId]->checkReturnStatus(retStatus);
4374    } else {
4375        RLOGE("separateConnectionResponse: radioService[%d]->mRadioResponse == NULL",
4376                slotId);
4377    }
4378
4379    return 0;
4380}
4381
4382int radio::setMuteResponse(int slotId,
4383                          int responseType, int serial, RIL_Errno e,
4384                          void *response, size_t responseLen) {
4385#if VDBG
4386    RLOGD("setMuteResponse: serial %d", serial);
4387#endif
4388
4389    if (radioService[slotId]->mRadioResponse != NULL) {
4390        RadioResponseInfo responseInfo = {};
4391        populateResponseInfo(responseInfo, serial, responseType, e);
4392        Return<void> retStatus
4393                = radioService[slotId]->mRadioResponse->setMuteResponse(responseInfo);
4394        radioService[slotId]->checkReturnStatus(retStatus);
4395    } else {
4396        RLOGE("setMuteResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4397    }
4398
4399    return 0;
4400}
4401
4402int radio::getMuteResponse(int slotId,
4403                          int responseType, int serial, RIL_Errno e, void *response,
4404                          size_t responseLen) {
4405#if VDBG
4406    RLOGD("getMuteResponse: serial %d", serial);
4407#endif
4408
4409    if (radioService[slotId]->mRadioResponse != NULL) {
4410        RadioResponseInfo responseInfo = {};
4411        populateResponseInfo(responseInfo, serial, responseType, e);
4412        bool enable = false;
4413        int serviceClass;
4414        if (response == NULL || responseLen != sizeof(int)) {
4415            RLOGE("getMuteResponse Invalid response: NULL");
4416            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4417        } else {
4418            int *pInt = (int *) response;
4419            enable = pInt[0] == 1 ? true : false;
4420        }
4421        Return<void> retStatus = radioService[slotId]->mRadioResponse->getMuteResponse(responseInfo,
4422                enable);
4423        radioService[slotId]->checkReturnStatus(retStatus);
4424    } else {
4425        RLOGE("getMuteResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4426    }
4427
4428    return 0;
4429}
4430
4431int radio::getClipResponse(int slotId,
4432                          int responseType, int serial, RIL_Errno e,
4433                          void *response, size_t responseLen) {
4434#if VDBG
4435    RLOGD("getClipResponse: serial %d", serial);
4436#endif
4437
4438    if (radioService[slotId]->mRadioResponse != NULL) {
4439        RadioResponseInfo responseInfo = {};
4440        int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
4441        Return<void> retStatus = radioService[slotId]->mRadioResponse->getClipResponse(responseInfo,
4442                (ClipStatus) ret);
4443        radioService[slotId]->checkReturnStatus(retStatus);
4444    } else {
4445        RLOGE("getClipResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4446    }
4447
4448    return 0;
4449}
4450
4451int radio::getDataCallListResponse(int slotId,
4452                                   int responseType, int serial, RIL_Errno e,
4453                                   void *response, size_t responseLen) {
4454#if VDBG
4455    RLOGD("getDataCallListResponse: serial %d", serial);
4456#endif
4457
4458    if (radioService[slotId]->mRadioResponse != NULL) {
4459        RadioResponseInfo responseInfo = {};
4460        populateResponseInfo(responseInfo, serial, responseType, e);
4461
4462        hidl_vec<SetupDataCallResult> ret;
4463        if (response == NULL || responseLen % sizeof(RIL_Data_Call_Response_v11) != 0) {
4464            RLOGE("getDataCallListResponse: invalid response");
4465            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4466        } else {
4467            convertRilDataCallListToHal(response, responseLen, ret);
4468        }
4469
4470        Return<void> retStatus = radioService[slotId]->mRadioResponse->getDataCallListResponse(
4471                responseInfo, ret);
4472        radioService[slotId]->checkReturnStatus(retStatus);
4473    } else {
4474        RLOGE("getDataCallListResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4475    }
4476
4477    return 0;
4478}
4479
4480int radio::setSuppServiceNotificationsResponse(int slotId,
4481                                              int responseType, int serial, RIL_Errno e,
4482                                              void *response, size_t responseLen) {
4483#if VDBG
4484    RLOGD("setSuppServiceNotificationsResponse: serial %d", serial);
4485#endif
4486
4487    if (radioService[slotId]->mRadioResponse != NULL) {
4488        RadioResponseInfo responseInfo = {};
4489        populateResponseInfo(responseInfo, serial, responseType, e);
4490        Return<void> retStatus
4491                = radioService[slotId]->mRadioResponse->setSuppServiceNotificationsResponse(
4492                responseInfo);
4493        radioService[slotId]->checkReturnStatus(retStatus);
4494    } else {
4495        RLOGE("setSuppServiceNotificationsResponse: radioService[%d]->mRadioResponse "
4496                "== NULL", slotId);
4497    }
4498
4499    return 0;
4500}
4501
4502int radio::deleteSmsOnSimResponse(int slotId,
4503                                 int responseType, int serial, RIL_Errno e,
4504                                 void *response, size_t responseLen) {
4505#if VDBG
4506    RLOGD("deleteSmsOnSimResponse: serial %d", serial);
4507#endif
4508
4509    if (radioService[slotId]->mRadioResponse != NULL) {
4510        RadioResponseInfo responseInfo = {};
4511        populateResponseInfo(responseInfo, serial, responseType, e);
4512        Return<void> retStatus
4513                = radioService[slotId]->mRadioResponse->deleteSmsOnSimResponse(responseInfo);
4514        radioService[slotId]->checkReturnStatus(retStatus);
4515    } else {
4516        RLOGE("deleteSmsOnSimResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4517    }
4518
4519    return 0;
4520}
4521
4522int radio::setBandModeResponse(int slotId,
4523                              int responseType, int serial, RIL_Errno e,
4524                              void *response, size_t responseLen) {
4525#if VDBG
4526    RLOGD("setBandModeResponse: serial %d", serial);
4527#endif
4528
4529    if (radioService[slotId]->mRadioResponse != NULL) {
4530        RadioResponseInfo responseInfo = {};
4531        populateResponseInfo(responseInfo, serial, responseType, e);
4532        Return<void> retStatus
4533                = radioService[slotId]->mRadioResponse->setBandModeResponse(responseInfo);
4534        radioService[slotId]->checkReturnStatus(retStatus);
4535    } else {
4536        RLOGE("setBandModeResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4537    }
4538
4539    return 0;
4540}
4541
4542int radio::writeSmsToSimResponse(int slotId,
4543                                int responseType, int serial, RIL_Errno e,
4544                                void *response, size_t responseLen) {
4545#if VDBG
4546    RLOGD("writeSmsToSimResponse: serial %d", serial);
4547#endif
4548
4549    if (radioService[slotId]->mRadioResponse != NULL) {
4550        RadioResponseInfo responseInfo = {};
4551        int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
4552        Return<void> retStatus
4553                = radioService[slotId]->mRadioResponse->writeSmsToSimResponse(responseInfo, ret);
4554        radioService[slotId]->checkReturnStatus(retStatus);
4555    } else {
4556        RLOGE("writeSmsToSimResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4557    }
4558
4559    return 0;
4560}
4561
4562int radio::getAvailableBandModesResponse(int slotId,
4563                                        int responseType, int serial, RIL_Errno e, void *response,
4564                                        size_t responseLen) {
4565#if VDBG
4566    RLOGD("getAvailableBandModesResponse: serial %d", serial);
4567#endif
4568
4569    if (radioService[slotId]->mRadioResponse != NULL) {
4570        RadioResponseInfo responseInfo = {};
4571        populateResponseInfo(responseInfo, serial, responseType, e);
4572        hidl_vec<RadioBandMode> modes;
4573        if (response == NULL || responseLen % sizeof(int) != 0) {
4574            RLOGE("getAvailableBandModesResponse Invalid response: NULL");
4575            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4576        } else {
4577            int *pInt = (int *) response;
4578            int numInts = responseLen / sizeof(int);
4579            modes.resize(numInts);
4580            for (int i = 0; i < numInts; i++) {
4581                modes[i] = (RadioBandMode) pInt[i];
4582            }
4583        }
4584        Return<void> retStatus
4585                = radioService[slotId]->mRadioResponse->getAvailableBandModesResponse(responseInfo,
4586                modes);
4587        radioService[slotId]->checkReturnStatus(retStatus);
4588    } else {
4589        RLOGE("getAvailableBandModesResponse: radioService[%d]->mRadioResponse == NULL",
4590                slotId);
4591    }
4592
4593    return 0;
4594}
4595
4596int radio::sendEnvelopeResponse(int slotId,
4597                               int responseType, int serial, RIL_Errno e,
4598                               void *response, size_t responseLen) {
4599#if VDBG
4600    RLOGD("sendEnvelopeResponse: serial %d", serial);
4601#endif
4602
4603    if (radioService[slotId]->mRadioResponse != NULL) {
4604        RadioResponseInfo responseInfo = {};
4605        populateResponseInfo(responseInfo, serial, responseType, e);
4606        Return<void> retStatus
4607                = radioService[slotId]->mRadioResponse->sendEnvelopeResponse(responseInfo,
4608                convertCharPtrToHidlString((char *) response));
4609        radioService[slotId]->checkReturnStatus(retStatus);
4610    } else {
4611        RLOGE("sendEnvelopeResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4612    }
4613
4614    return 0;
4615}
4616
4617int radio::sendTerminalResponseToSimResponse(int slotId,
4618                                            int responseType, int serial, RIL_Errno e,
4619                                            void *response, size_t responseLen) {
4620#if VDBG
4621    RLOGD("sendTerminalResponseToSimResponse: serial %d", serial);
4622#endif
4623
4624    if (radioService[slotId]->mRadioResponse != NULL) {
4625        RadioResponseInfo responseInfo = {};
4626        populateResponseInfo(responseInfo, serial, responseType, e);
4627        Return<void> retStatus
4628                = radioService[slotId]->mRadioResponse->sendTerminalResponseToSimResponse(
4629                responseInfo);
4630        radioService[slotId]->checkReturnStatus(retStatus);
4631    } else {
4632        RLOGE("sendTerminalResponseToSimResponse: radioService[%d]->mRadioResponse == NULL",
4633                slotId);
4634    }
4635
4636    return 0;
4637}
4638
4639int radio::handleStkCallSetupRequestFromSimResponse(int slotId,
4640                                                   int responseType, int serial,
4641                                                   RIL_Errno e, void *response,
4642                                                   size_t responseLen) {
4643#if VDBG
4644    RLOGD("handleStkCallSetupRequestFromSimResponse: serial %d", serial);
4645#endif
4646
4647    if (radioService[slotId]->mRadioResponse != NULL) {
4648        RadioResponseInfo responseInfo = {};
4649        populateResponseInfo(responseInfo, serial, responseType, e);
4650        Return<void> retStatus
4651                = radioService[slotId]->mRadioResponse->handleStkCallSetupRequestFromSimResponse(
4652                responseInfo);
4653        radioService[slotId]->checkReturnStatus(retStatus);
4654    } else {
4655        RLOGE("handleStkCallSetupRequestFromSimResponse: radioService[%d]->mRadioResponse "
4656                "== NULL", slotId);
4657    }
4658
4659    return 0;
4660}
4661
4662int radio::explicitCallTransferResponse(int slotId,
4663                                       int responseType, int serial, RIL_Errno e,
4664                                       void *response, size_t responseLen) {
4665#if VDBG
4666    RLOGD("explicitCallTransferResponse: serial %d", serial);
4667#endif
4668
4669    if (radioService[slotId]->mRadioResponse != NULL) {
4670        RadioResponseInfo responseInfo = {};
4671        populateResponseInfo(responseInfo, serial, responseType, e);
4672        Return<void> retStatus
4673                = radioService[slotId]->mRadioResponse->explicitCallTransferResponse(responseInfo);
4674        radioService[slotId]->checkReturnStatus(retStatus);
4675    } else {
4676        RLOGE("explicitCallTransferResponse: radioService[%d]->mRadioResponse == NULL",
4677                slotId);
4678    }
4679
4680    return 0;
4681}
4682
4683int radio::setPreferredNetworkTypeResponse(int slotId,
4684                                 int responseType, int serial, RIL_Errno e,
4685                                 void *response, size_t responseLen) {
4686#if VDBG
4687    RLOGD("setPreferredNetworkTypeResponse: serial %d", serial);
4688#endif
4689
4690    if (radioService[slotId]->mRadioResponse != NULL) {
4691        RadioResponseInfo responseInfo = {};
4692        populateResponseInfo(responseInfo, serial, responseType, e);
4693        Return<void> retStatus
4694                = radioService[slotId]->mRadioResponse->setPreferredNetworkTypeResponse(
4695                responseInfo);
4696        radioService[slotId]->checkReturnStatus(retStatus);
4697    } else {
4698        RLOGE("setPreferredNetworkTypeResponse: radioService[%d]->mRadioResponse == NULL",
4699                slotId);
4700    }
4701
4702    return 0;
4703}
4704
4705
4706int radio::getPreferredNetworkTypeResponse(int slotId,
4707                                          int responseType, int serial, RIL_Errno e,
4708                                          void *response, size_t responseLen) {
4709#if VDBG
4710    RLOGD("getPreferredNetworkTypeResponse: serial %d", serial);
4711#endif
4712
4713    if (radioService[slotId]->mRadioResponse != NULL) {
4714        RadioResponseInfo responseInfo = {};
4715        int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
4716        Return<void> retStatus
4717                = radioService[slotId]->mRadioResponse->getPreferredNetworkTypeResponse(
4718                responseInfo, (PreferredNetworkType) ret);
4719        radioService[slotId]->checkReturnStatus(retStatus);
4720    } else {
4721        RLOGE("getPreferredNetworkTypeResponse: radioService[%d]->mRadioResponse == NULL",
4722                slotId);
4723    }
4724
4725    return 0;
4726}
4727
4728int radio::getNeighboringCidsResponse(int slotId,
4729                                     int responseType, int serial, RIL_Errno e,
4730                                     void *response, size_t responseLen) {
4731#if VDBG
4732    RLOGD("getNeighboringCidsResponse: serial %d", serial);
4733#endif
4734
4735    if (radioService[slotId]->mRadioResponse != NULL) {
4736        RadioResponseInfo responseInfo = {};
4737        populateResponseInfo(responseInfo, serial, responseType, e);
4738        hidl_vec<NeighboringCell> cells;
4739
4740        if (response == NULL || responseLen % sizeof(RIL_NeighboringCell *) != 0) {
4741            RLOGE("getNeighboringCidsResponse Invalid response: NULL");
4742            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4743        } else {
4744            int num = responseLen / sizeof(RIL_NeighboringCell *);
4745            cells.resize(num);
4746            for (int i = 0 ; i < num; i++) {
4747                RIL_NeighboringCell *resp = ((RIL_NeighboringCell **) response)[i];
4748                cells[i].cid = convertCharPtrToHidlString(resp->cid);
4749                cells[i].rssi = resp->rssi;
4750            }
4751        }
4752
4753        Return<void> retStatus
4754                = radioService[slotId]->mRadioResponse->getNeighboringCidsResponse(responseInfo,
4755                cells);
4756        radioService[slotId]->checkReturnStatus(retStatus);
4757    } else {
4758        RLOGE("getNeighboringCidsResponse: radioService[%d]->mRadioResponse == NULL",
4759                slotId);
4760    }
4761
4762    return 0;
4763}
4764
4765int radio::setLocationUpdatesResponse(int slotId,
4766                                     int responseType, int serial, RIL_Errno e,
4767                                     void *response, size_t responseLen) {
4768#if VDBG
4769    RLOGD("setLocationUpdatesResponse: serial %d", serial);
4770#endif
4771
4772    if (radioService[slotId]->mRadioResponse != NULL) {
4773        RadioResponseInfo responseInfo = {};
4774        populateResponseInfo(responseInfo, serial, responseType, e);
4775        Return<void> retStatus
4776                = radioService[slotId]->mRadioResponse->setLocationUpdatesResponse(responseInfo);
4777        radioService[slotId]->checkReturnStatus(retStatus);
4778    } else {
4779        RLOGE("setLocationUpdatesResponse: radioService[%d]->mRadioResponse == NULL",
4780                slotId);
4781    }
4782
4783    return 0;
4784}
4785
4786int radio::setCdmaSubscriptionSourceResponse(int slotId,
4787                                 int responseType, int serial, RIL_Errno e,
4788                                 void *response, size_t responseLen) {
4789#if VDBG
4790    RLOGD("setCdmaSubscriptionSourceResponse: serial %d", serial);
4791#endif
4792
4793    if (radioService[slotId]->mRadioResponse != NULL) {
4794        RadioResponseInfo responseInfo = {};
4795        populateResponseInfo(responseInfo, serial, responseType, e);
4796        Return<void> retStatus
4797                = radioService[slotId]->mRadioResponse->setCdmaSubscriptionSourceResponse(
4798                responseInfo);
4799        radioService[slotId]->checkReturnStatus(retStatus);
4800    } else {
4801        RLOGE("setCdmaSubscriptionSourceResponse: radioService[%d]->mRadioResponse == NULL",
4802                slotId);
4803    }
4804
4805    return 0;
4806}
4807
4808int radio::setCdmaRoamingPreferenceResponse(int slotId,
4809                                 int responseType, int serial, RIL_Errno e,
4810                                 void *response, size_t responseLen) {
4811#if VDBG
4812    RLOGD("setCdmaRoamingPreferenceResponse: serial %d", serial);
4813#endif
4814
4815    if (radioService[slotId]->mRadioResponse != NULL) {
4816        RadioResponseInfo responseInfo = {};
4817        populateResponseInfo(responseInfo, serial, responseType, e);
4818        Return<void> retStatus
4819                = radioService[slotId]->mRadioResponse->setCdmaRoamingPreferenceResponse(
4820                responseInfo);
4821        radioService[slotId]->checkReturnStatus(retStatus);
4822    } else {
4823        RLOGE("setCdmaRoamingPreferenceResponse: radioService[%d]->mRadioResponse == NULL",
4824                slotId);
4825    }
4826
4827    return 0;
4828}
4829
4830int radio::getCdmaRoamingPreferenceResponse(int slotId,
4831                                           int responseType, int serial, RIL_Errno e,
4832                                           void *response, size_t responseLen) {
4833#if VDBG
4834    RLOGD("getCdmaRoamingPreferenceResponse: serial %d", serial);
4835#endif
4836
4837    if (radioService[slotId]->mRadioResponse != NULL) {
4838        RadioResponseInfo responseInfo = {};
4839        int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
4840        Return<void> retStatus
4841                = radioService[slotId]->mRadioResponse->getCdmaRoamingPreferenceResponse(
4842                responseInfo, (CdmaRoamingType) ret);
4843        radioService[slotId]->checkReturnStatus(retStatus);
4844    } else {
4845        RLOGE("getCdmaRoamingPreferenceResponse: radioService[%d]->mRadioResponse == NULL",
4846                slotId);
4847    }
4848
4849    return 0;
4850}
4851
4852int radio::setTTYModeResponse(int slotId,
4853                             int responseType, int serial, RIL_Errno e,
4854                             void *response, size_t responseLen) {
4855#if VDBG
4856    RLOGD("setTTYModeResponse: serial %d", serial);
4857#endif
4858
4859    if (radioService[slotId]->mRadioResponse != NULL) {
4860        RadioResponseInfo responseInfo = {};
4861        populateResponseInfo(responseInfo, serial, responseType, e);
4862        Return<void> retStatus
4863                = radioService[slotId]->mRadioResponse->setTTYModeResponse(responseInfo);
4864        radioService[slotId]->checkReturnStatus(retStatus);
4865    } else {
4866        RLOGE("setTTYModeResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4867    }
4868
4869    return 0;
4870}
4871
4872int radio::getTTYModeResponse(int slotId,
4873                             int responseType, int serial, RIL_Errno e,
4874                             void *response, size_t responseLen) {
4875#if VDBG
4876    RLOGD("getTTYModeResponse: serial %d", serial);
4877#endif
4878
4879    if (radioService[slotId]->mRadioResponse != NULL) {
4880        RadioResponseInfo responseInfo = {};
4881        int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
4882        Return<void> retStatus
4883                = radioService[slotId]->mRadioResponse->getTTYModeResponse(responseInfo,
4884                (TtyMode) ret);
4885        radioService[slotId]->checkReturnStatus(retStatus);
4886    } else {
4887        RLOGE("getTTYModeResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4888    }
4889
4890    return 0;
4891}
4892
4893int radio::setPreferredVoicePrivacyResponse(int slotId,
4894                                 int responseType, int serial, RIL_Errno e,
4895                                 void *response, size_t responseLen) {
4896#if VDBG
4897    RLOGD("setPreferredVoicePrivacyResponse: serial %d", serial);
4898#endif
4899
4900    if (radioService[slotId]->mRadioResponse != NULL) {
4901        RadioResponseInfo responseInfo = {};
4902        populateResponseInfo(responseInfo, serial, responseType, e);
4903        Return<void> retStatus
4904                = radioService[slotId]->mRadioResponse->setPreferredVoicePrivacyResponse(
4905                responseInfo);
4906        radioService[slotId]->checkReturnStatus(retStatus);
4907    } else {
4908        RLOGE("setPreferredVoicePrivacyResponse: radioService[%d]->mRadioResponse == NULL",
4909                slotId);
4910    }
4911
4912    return 0;
4913}
4914
4915int radio::getPreferredVoicePrivacyResponse(int slotId,
4916                                           int responseType, int serial, RIL_Errno e,
4917                                           void *response, size_t responseLen) {
4918#if VDBG
4919    RLOGD("getPreferredVoicePrivacyResponse: serial %d", serial);
4920#endif
4921
4922    if (radioService[slotId]->mRadioResponse != NULL) {
4923        RadioResponseInfo responseInfo = {};
4924        populateResponseInfo(responseInfo, serial, responseType, e);
4925        bool enable = false;
4926        int numInts = responseLen / sizeof(int);
4927        if (response == NULL || numInts != 1) {
4928            RLOGE("getPreferredVoicePrivacyResponse Invalid response: NULL");
4929            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
4930        } else {
4931            int *pInt = (int *) response;
4932            enable = pInt[0] == 1 ? true : false;
4933        }
4934        Return<void> retStatus
4935                = radioService[slotId]->mRadioResponse->getPreferredVoicePrivacyResponse(
4936                responseInfo, enable);
4937        radioService[slotId]->checkReturnStatus(retStatus);
4938    } else {
4939        RLOGE("getPreferredVoicePrivacyResponse: radioService[%d]->mRadioResponse == NULL",
4940                slotId);
4941    }
4942
4943    return 0;
4944}
4945
4946int radio::sendCDMAFeatureCodeResponse(int slotId,
4947                                 int responseType, int serial, RIL_Errno e,
4948                                 void *response, size_t responseLen) {
4949#if VDBG
4950    RLOGD("sendCDMAFeatureCodeResponse: serial %d", serial);
4951#endif
4952
4953    if (radioService[slotId]->mRadioResponse != NULL) {
4954        RadioResponseInfo responseInfo = {};
4955        populateResponseInfo(responseInfo, serial, responseType, e);
4956        Return<void> retStatus
4957                = radioService[slotId]->mRadioResponse->sendCDMAFeatureCodeResponse(responseInfo);
4958        radioService[slotId]->checkReturnStatus(retStatus);
4959    } else {
4960        RLOGE("sendCDMAFeatureCodeResponse: radioService[%d]->mRadioResponse == NULL",
4961                slotId);
4962    }
4963
4964    return 0;
4965}
4966
4967int radio::sendBurstDtmfResponse(int slotId,
4968                                 int responseType, int serial, RIL_Errno e,
4969                                 void *response, size_t responseLen) {
4970#if VDBG
4971    RLOGD("sendBurstDtmfResponse: serial %d", serial);
4972#endif
4973
4974    if (radioService[slotId]->mRadioResponse != NULL) {
4975        RadioResponseInfo responseInfo = {};
4976        populateResponseInfo(responseInfo, serial, responseType, e);
4977        Return<void> retStatus
4978                = radioService[slotId]->mRadioResponse->sendBurstDtmfResponse(responseInfo);
4979        radioService[slotId]->checkReturnStatus(retStatus);
4980    } else {
4981        RLOGE("sendBurstDtmfResponse: radioService[%d]->mRadioResponse == NULL", slotId);
4982    }
4983
4984    return 0;
4985}
4986
4987int radio::sendCdmaSmsResponse(int slotId,
4988                              int responseType, int serial, RIL_Errno e, void *response,
4989                              size_t responseLen) {
4990#if VDBG
4991    RLOGD("sendCdmaSmsResponse: serial %d", serial);
4992#endif
4993
4994    if (radioService[slotId]->mRadioResponse != NULL) {
4995        RadioResponseInfo responseInfo = {};
4996        SendSmsResult result = makeSendSmsResult(responseInfo, serial, responseType, e, response,
4997                responseLen);
4998
4999        Return<void> retStatus
5000                = radioService[slotId]->mRadioResponse->sendCdmaSmsResponse(responseInfo, result);
5001        radioService[slotId]->checkReturnStatus(retStatus);
5002    } else {
5003        RLOGE("sendCdmaSmsResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5004    }
5005
5006    return 0;
5007}
5008
5009int radio::acknowledgeLastIncomingCdmaSmsResponse(int slotId,
5010                                                 int responseType, int serial, RIL_Errno e,
5011                                                 void *response, size_t responseLen) {
5012#if VDBG
5013    RLOGD("acknowledgeLastIncomingCdmaSmsResponse: serial %d", serial);
5014#endif
5015
5016    if (radioService[slotId]->mRadioResponse != NULL) {
5017        RadioResponseInfo responseInfo = {};
5018        populateResponseInfo(responseInfo, serial, responseType, e);
5019        Return<void> retStatus
5020                = radioService[slotId]->mRadioResponse->acknowledgeLastIncomingCdmaSmsResponse(
5021                responseInfo);
5022        radioService[slotId]->checkReturnStatus(retStatus);
5023    } else {
5024        RLOGE("acknowledgeLastIncomingCdmaSmsResponse: radioService[%d]->mRadioResponse "
5025                "== NULL", slotId);
5026    }
5027
5028    return 0;
5029}
5030
5031int radio::getGsmBroadcastConfigResponse(int slotId,
5032                                        int responseType, int serial, RIL_Errno e,
5033                                        void *response, size_t responseLen) {
5034#if VDBG
5035    RLOGD("getGsmBroadcastConfigResponse: serial %d", serial);
5036#endif
5037
5038    if (radioService[slotId]->mRadioResponse != NULL) {
5039        RadioResponseInfo responseInfo = {};
5040        populateResponseInfo(responseInfo, serial, responseType, e);
5041        hidl_vec<GsmBroadcastSmsConfigInfo> configs;
5042
5043        if (response == NULL || responseLen % sizeof(RIL_GSM_BroadcastSmsConfigInfo *) != 0) {
5044            RLOGE("getGsmBroadcastConfigResponse Invalid response: NULL");
5045            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5046        } else {
5047            int num = responseLen / sizeof(RIL_GSM_BroadcastSmsConfigInfo *);
5048            configs.resize(num);
5049            for (int i = 0 ; i < num; i++) {
5050                RIL_GSM_BroadcastSmsConfigInfo *resp =
5051                        ((RIL_GSM_BroadcastSmsConfigInfo **) response)[i];
5052                configs[i].fromServiceId = resp->fromServiceId;
5053                configs[i].toServiceId = resp->toServiceId;
5054                configs[i].fromCodeScheme = resp->fromCodeScheme;
5055                configs[i].toCodeScheme = resp->toCodeScheme;
5056                configs[i].selected = resp->selected == 1 ? true : false;
5057            }
5058        }
5059
5060        Return<void> retStatus
5061                = radioService[slotId]->mRadioResponse->getGsmBroadcastConfigResponse(responseInfo,
5062                configs);
5063        radioService[slotId]->checkReturnStatus(retStatus);
5064    } else {
5065        RLOGE("getGsmBroadcastConfigResponse: radioService[%d]->mRadioResponse == NULL",
5066                slotId);
5067    }
5068
5069    return 0;
5070}
5071
5072int radio::setGsmBroadcastConfigResponse(int slotId,
5073                                        int responseType, int serial, RIL_Errno e,
5074                                        void *response, size_t responseLen) {
5075#if VDBG
5076    RLOGD("setGsmBroadcastConfigResponse: serial %d", serial);
5077#endif
5078
5079    if (radioService[slotId]->mRadioResponse != NULL) {
5080        RadioResponseInfo responseInfo = {};
5081        populateResponseInfo(responseInfo, serial, responseType, e);
5082        Return<void> retStatus
5083                = radioService[slotId]->mRadioResponse->setGsmBroadcastConfigResponse(responseInfo);
5084        radioService[slotId]->checkReturnStatus(retStatus);
5085    } else {
5086        RLOGE("setGsmBroadcastConfigResponse: radioService[%d]->mRadioResponse == NULL",
5087                slotId);
5088    }
5089
5090    return 0;
5091}
5092
5093int radio::setGsmBroadcastActivationResponse(int slotId,
5094                                            int responseType, int serial, RIL_Errno e,
5095                                            void *response, size_t responseLen) {
5096#if VDBG
5097    RLOGD("setGsmBroadcastActivationResponse: serial %d", serial);
5098#endif
5099
5100    if (radioService[slotId]->mRadioResponse != NULL) {
5101        RadioResponseInfo responseInfo = {};
5102        populateResponseInfo(responseInfo, serial, responseType, e);
5103        Return<void> retStatus
5104                = radioService[slotId]->mRadioResponse->setGsmBroadcastActivationResponse(
5105                responseInfo);
5106        radioService[slotId]->checkReturnStatus(retStatus);
5107    } else {
5108        RLOGE("setGsmBroadcastActivationResponse: radioService[%d]->mRadioResponse == NULL",
5109                slotId);
5110    }
5111
5112    return 0;
5113}
5114
5115int radio::getCdmaBroadcastConfigResponse(int slotId,
5116                                         int responseType, int serial, RIL_Errno e,
5117                                         void *response, size_t responseLen) {
5118#if VDBG
5119    RLOGD("getCdmaBroadcastConfigResponse: serial %d", serial);
5120#endif
5121
5122    if (radioService[slotId]->mRadioResponse != NULL) {
5123        RadioResponseInfo responseInfo = {};
5124        populateResponseInfo(responseInfo, serial, responseType, e);
5125        hidl_vec<CdmaBroadcastSmsConfigInfo> configs;
5126
5127        if (response == NULL || responseLen % sizeof(RIL_CDMA_BroadcastSmsConfigInfo *) != 0) {
5128            RLOGE("getCdmaBroadcastConfigResponse Invalid response: NULL");
5129            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5130        } else {
5131            int num = responseLen / sizeof(RIL_CDMA_BroadcastSmsConfigInfo *);
5132            configs.resize(num);
5133            for (int i = 0 ; i < num; i++) {
5134                RIL_CDMA_BroadcastSmsConfigInfo *resp =
5135                        ((RIL_CDMA_BroadcastSmsConfigInfo **) response)[i];
5136                configs[i].serviceCategory = resp->service_category;
5137                configs[i].language = resp->language;
5138                configs[i].selected = resp->selected == 1 ? true : false;
5139            }
5140        }
5141
5142        Return<void> retStatus
5143                = radioService[slotId]->mRadioResponse->getCdmaBroadcastConfigResponse(responseInfo,
5144                configs);
5145        radioService[slotId]->checkReturnStatus(retStatus);
5146    } else {
5147        RLOGE("getCdmaBroadcastConfigResponse: radioService[%d]->mRadioResponse == NULL",
5148                slotId);
5149    }
5150
5151    return 0;
5152}
5153
5154int radio::setCdmaBroadcastConfigResponse(int slotId,
5155                                         int responseType, int serial, RIL_Errno e,
5156                                         void *response, size_t responseLen) {
5157#if VDBG
5158    RLOGD("setCdmaBroadcastConfigResponse: serial %d", serial);
5159#endif
5160
5161    if (radioService[slotId]->mRadioResponse != NULL) {
5162        RadioResponseInfo responseInfo = {};
5163        populateResponseInfo(responseInfo, serial, responseType, e);
5164        Return<void> retStatus
5165                = radioService[slotId]->mRadioResponse->setCdmaBroadcastConfigResponse(
5166                responseInfo);
5167        radioService[slotId]->checkReturnStatus(retStatus);
5168    } else {
5169        RLOGE("setCdmaBroadcastConfigResponse: radioService[%d]->mRadioResponse == NULL",
5170                slotId);
5171    }
5172
5173    return 0;
5174}
5175
5176int radio::setCdmaBroadcastActivationResponse(int slotId,
5177                                             int responseType, int serial, RIL_Errno e,
5178                                             void *response, size_t responseLen) {
5179#if VDBG
5180    RLOGD("setCdmaBroadcastActivationResponse: serial %d", serial);
5181#endif
5182
5183    if (radioService[slotId]->mRadioResponse != NULL) {
5184        RadioResponseInfo responseInfo = {};
5185        populateResponseInfo(responseInfo, serial, responseType, e);
5186        Return<void> retStatus
5187                = radioService[slotId]->mRadioResponse->setCdmaBroadcastActivationResponse(
5188                responseInfo);
5189        radioService[slotId]->checkReturnStatus(retStatus);
5190    } else {
5191        RLOGE("setCdmaBroadcastActivationResponse: radioService[%d]->mRadioResponse == NULL",
5192                slotId);
5193    }
5194
5195    return 0;
5196}
5197
5198int radio::getCDMASubscriptionResponse(int slotId,
5199                                      int responseType, int serial, RIL_Errno e, void *response,
5200                                      size_t responseLen) {
5201#if VDBG
5202    RLOGD("getCDMASubscriptionResponse: serial %d", serial);
5203#endif
5204
5205    if (radioService[slotId]->mRadioResponse != NULL) {
5206        RadioResponseInfo responseInfo = {};
5207        populateResponseInfo(responseInfo, serial, responseType, e);
5208
5209        int numStrings = responseLen / sizeof(char *);
5210        hidl_string emptyString;
5211        if (response == NULL || numStrings != 5) {
5212            RLOGE("getOperatorResponse Invalid response: NULL");
5213            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5214            Return<void> retStatus
5215                    = radioService[slotId]->mRadioResponse->getCDMASubscriptionResponse(
5216                    responseInfo, emptyString, emptyString, emptyString, emptyString, emptyString);
5217            radioService[slotId]->checkReturnStatus(retStatus);
5218        } else {
5219            char **resp = (char **) response;
5220            Return<void> retStatus
5221                    = radioService[slotId]->mRadioResponse->getCDMASubscriptionResponse(
5222                    responseInfo,
5223                    convertCharPtrToHidlString(resp[0]),
5224                    convertCharPtrToHidlString(resp[1]),
5225                    convertCharPtrToHidlString(resp[2]),
5226                    convertCharPtrToHidlString(resp[3]),
5227                    convertCharPtrToHidlString(resp[4]));
5228            radioService[slotId]->checkReturnStatus(retStatus);
5229        }
5230    } else {
5231        RLOGE("getCDMASubscriptionResponse: radioService[%d]->mRadioResponse == NULL",
5232                slotId);
5233    }
5234
5235    return 0;
5236}
5237
5238int radio::writeSmsToRuimResponse(int slotId,
5239                                 int responseType, int serial, RIL_Errno e,
5240                                 void *response, size_t responseLen) {
5241#if VDBG
5242    RLOGD("writeSmsToRuimResponse: serial %d", serial);
5243#endif
5244
5245    if (radioService[slotId]->mRadioResponse != NULL) {
5246        RadioResponseInfo responseInfo = {};
5247        int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
5248        Return<void> retStatus
5249                = radioService[slotId]->mRadioResponse->writeSmsToRuimResponse(responseInfo, ret);
5250        radioService[slotId]->checkReturnStatus(retStatus);
5251    } else {
5252        RLOGE("writeSmsToRuimResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5253    }
5254
5255    return 0;
5256}
5257
5258int radio::deleteSmsOnRuimResponse(int slotId,
5259                                  int responseType, int serial, RIL_Errno e,
5260                                  void *response, size_t responseLen) {
5261#if VDBG
5262    RLOGD("deleteSmsOnRuimResponse: serial %d", serial);
5263#endif
5264
5265    if (radioService[slotId]->mRadioResponse != NULL) {
5266        RadioResponseInfo responseInfo = {};
5267        populateResponseInfo(responseInfo, serial, responseType, e);
5268        Return<void> retStatus
5269                = radioService[slotId]->mRadioResponse->deleteSmsOnRuimResponse(responseInfo);
5270        radioService[slotId]->checkReturnStatus(retStatus);
5271    } else {
5272        RLOGE("deleteSmsOnRuimResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5273    }
5274
5275    return 0;
5276}
5277
5278int radio::getDeviceIdentityResponse(int slotId,
5279                                    int responseType, int serial, RIL_Errno e, void *response,
5280                                    size_t responseLen) {
5281#if VDBG
5282    RLOGD("getDeviceIdentityResponse: serial %d", serial);
5283#endif
5284
5285    if (radioService[slotId]->mRadioResponse != NULL) {
5286        RadioResponseInfo responseInfo = {};
5287        populateResponseInfo(responseInfo, serial, responseType, e);
5288
5289        int numStrings = responseLen / sizeof(char *);
5290        hidl_string emptyString;
5291        if (response == NULL || numStrings != 4) {
5292            RLOGE("getDeviceIdentityResponse Invalid response: NULL");
5293            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5294            Return<void> retStatus
5295                    = radioService[slotId]->mRadioResponse->getDeviceIdentityResponse(responseInfo,
5296                    emptyString, emptyString, emptyString, emptyString);
5297            radioService[slotId]->checkReturnStatus(retStatus);
5298        } else {
5299            char **resp = (char **) response;
5300            Return<void> retStatus
5301                    = radioService[slotId]->mRadioResponse->getDeviceIdentityResponse(responseInfo,
5302                    convertCharPtrToHidlString(resp[0]),
5303                    convertCharPtrToHidlString(resp[1]),
5304                    convertCharPtrToHidlString(resp[2]),
5305                    convertCharPtrToHidlString(resp[3]));
5306            radioService[slotId]->checkReturnStatus(retStatus);
5307        }
5308    } else {
5309        RLOGE("getDeviceIdentityResponse: radioService[%d]->mRadioResponse == NULL",
5310                slotId);
5311    }
5312
5313    return 0;
5314}
5315
5316int radio::exitEmergencyCallbackModeResponse(int slotId,
5317                                            int responseType, int serial, RIL_Errno e,
5318                                            void *response, size_t responseLen) {
5319#if VDBG
5320    RLOGD("exitEmergencyCallbackModeResponse: serial %d", serial);
5321#endif
5322
5323    if (radioService[slotId]->mRadioResponse != NULL) {
5324        RadioResponseInfo responseInfo = {};
5325        populateResponseInfo(responseInfo, serial, responseType, e);
5326        Return<void> retStatus
5327                = radioService[slotId]->mRadioResponse->exitEmergencyCallbackModeResponse(
5328                responseInfo);
5329        radioService[slotId]->checkReturnStatus(retStatus);
5330    } else {
5331        RLOGE("exitEmergencyCallbackModeResponse: radioService[%d]->mRadioResponse == NULL",
5332                slotId);
5333    }
5334
5335    return 0;
5336}
5337
5338int radio::getSmscAddressResponse(int slotId,
5339                                  int responseType, int serial, RIL_Errno e,
5340                                  void *response, size_t responseLen) {
5341#if VDBG
5342    RLOGD("getSmscAddressResponse: serial %d", serial);
5343#endif
5344
5345    if (radioService[slotId]->mRadioResponse != NULL) {
5346        RadioResponseInfo responseInfo = {};
5347        populateResponseInfo(responseInfo, serial, responseType, e);
5348        Return<void> retStatus
5349                = radioService[slotId]->mRadioResponse->getSmscAddressResponse(responseInfo,
5350                convertCharPtrToHidlString((char *) response));
5351        radioService[slotId]->checkReturnStatus(retStatus);
5352    } else {
5353        RLOGE("getSmscAddressResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5354    }
5355
5356    return 0;
5357}
5358
5359int radio::setSmscAddressResponse(int slotId,
5360                                             int responseType, int serial, RIL_Errno e,
5361                                             void *response, size_t responseLen) {
5362#if VDBG
5363    RLOGD("setSmscAddressResponse: serial %d", serial);
5364#endif
5365
5366    if (radioService[slotId]->mRadioResponse != NULL) {
5367        RadioResponseInfo responseInfo = {};
5368        populateResponseInfo(responseInfo, serial, responseType, e);
5369        Return<void> retStatus
5370                = radioService[slotId]->mRadioResponse->setSmscAddressResponse(responseInfo);
5371        radioService[slotId]->checkReturnStatus(retStatus);
5372    } else {
5373        RLOGE("setSmscAddressResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5374    }
5375
5376    return 0;
5377}
5378
5379int radio::reportSmsMemoryStatusResponse(int slotId,
5380                                        int responseType, int serial, RIL_Errno e,
5381                                        void *response, size_t responseLen) {
5382#if VDBG
5383    RLOGD("reportSmsMemoryStatusResponse: serial %d", serial);
5384#endif
5385
5386    if (radioService[slotId]->mRadioResponse != NULL) {
5387        RadioResponseInfo responseInfo = {};
5388        populateResponseInfo(responseInfo, serial, responseType, e);
5389        Return<void> retStatus
5390                = radioService[slotId]->mRadioResponse->reportSmsMemoryStatusResponse(responseInfo);
5391        radioService[slotId]->checkReturnStatus(retStatus);
5392    } else {
5393        RLOGE("reportSmsMemoryStatusResponse: radioService[%d]->mRadioResponse == NULL",
5394                slotId);
5395    }
5396
5397    return 0;
5398}
5399
5400int radio::reportStkServiceIsRunningResponse(int slotId,
5401                                             int responseType, int serial, RIL_Errno e,
5402                                             void *response, size_t responseLen) {
5403#if VDBG
5404    RLOGD("reportStkServiceIsRunningResponse: serial %d", serial);
5405#endif
5406
5407    if (radioService[slotId]->mRadioResponse != NULL) {
5408        RadioResponseInfo responseInfo = {};
5409        populateResponseInfo(responseInfo, serial, responseType, e);
5410        Return<void> retStatus = radioService[slotId]->mRadioResponse->
5411                reportStkServiceIsRunningResponse(responseInfo);
5412        radioService[slotId]->checkReturnStatus(retStatus);
5413    } else {
5414        RLOGE("reportStkServiceIsRunningResponse: radioService[%d]->mRadioResponse == NULL",
5415                slotId);
5416    }
5417
5418    return 0;
5419}
5420
5421int radio::getCdmaSubscriptionSourceResponse(int slotId,
5422                                            int responseType, int serial, RIL_Errno e,
5423                                            void *response, size_t responseLen) {
5424#if VDBG
5425    RLOGD("getCdmaSubscriptionSourceResponse: serial %d", serial);
5426#endif
5427
5428    if (radioService[slotId]->mRadioResponse != NULL) {
5429        RadioResponseInfo responseInfo = {};
5430        int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
5431        Return<void> retStatus
5432                = radioService[slotId]->mRadioResponse->getCdmaSubscriptionSourceResponse(
5433                responseInfo, (CdmaSubscriptionSource) ret);
5434        radioService[slotId]->checkReturnStatus(retStatus);
5435    } else {
5436        RLOGE("getCdmaSubscriptionSourceResponse: radioService[%d]->mRadioResponse == NULL",
5437                slotId);
5438    }
5439
5440    return 0;
5441}
5442
5443int radio::requestIsimAuthenticationResponse(int slotId,
5444                                            int responseType, int serial, RIL_Errno e,
5445                                            void *response, size_t responseLen) {
5446#if VDBG
5447    RLOGD("requestIsimAuthenticationResponse: serial %d", serial);
5448#endif
5449
5450    if (radioService[slotId]->mRadioResponse != NULL) {
5451        RadioResponseInfo responseInfo = {};
5452        populateResponseInfo(responseInfo, serial, responseType, e);
5453        Return<void> retStatus
5454                = radioService[slotId]->mRadioResponse->requestIsimAuthenticationResponse(
5455                responseInfo,
5456                convertCharPtrToHidlString((char *) response));
5457        radioService[slotId]->checkReturnStatus(retStatus);
5458    } else {
5459        RLOGE("requestIsimAuthenticationResponse: radioService[%d]->mRadioResponse == NULL",
5460                slotId);
5461    }
5462
5463    return 0;
5464}
5465
5466int radio::acknowledgeIncomingGsmSmsWithPduResponse(int slotId,
5467                                                   int responseType,
5468                                                   int serial, RIL_Errno e, void *response,
5469                                                   size_t responseLen) {
5470#if VDBG
5471    RLOGD("acknowledgeIncomingGsmSmsWithPduResponse: serial %d", serial);
5472#endif
5473
5474    if (radioService[slotId]->mRadioResponse != NULL) {
5475        RadioResponseInfo responseInfo = {};
5476        populateResponseInfo(responseInfo, serial, responseType, e);
5477        Return<void> retStatus
5478                = radioService[slotId]->mRadioResponse->acknowledgeIncomingGsmSmsWithPduResponse(
5479                responseInfo);
5480        radioService[slotId]->checkReturnStatus(retStatus);
5481    } else {
5482        RLOGE("acknowledgeIncomingGsmSmsWithPduResponse: radioService[%d]->mRadioResponse "
5483                "== NULL", slotId);
5484    }
5485
5486    return 0;
5487}
5488
5489int radio::sendEnvelopeWithStatusResponse(int slotId,
5490                                         int responseType, int serial, RIL_Errno e, void *response,
5491                                         size_t responseLen) {
5492#if VDBG
5493    RLOGD("sendEnvelopeWithStatusResponse: serial %d", serial);
5494#endif
5495
5496    if (radioService[slotId]->mRadioResponse != NULL) {
5497        RadioResponseInfo responseInfo = {};
5498        IccIoResult result = responseIccIo(responseInfo, serial, responseType, e,
5499                response, responseLen);
5500
5501        Return<void> retStatus
5502                = radioService[slotId]->mRadioResponse->sendEnvelopeWithStatusResponse(responseInfo,
5503                result);
5504        radioService[slotId]->checkReturnStatus(retStatus);
5505    } else {
5506        RLOGE("sendEnvelopeWithStatusResponse: radioService[%d]->mRadioResponse == NULL",
5507                slotId);
5508    }
5509
5510    return 0;
5511}
5512
5513int radio::getVoiceRadioTechnologyResponse(int slotId,
5514                                          int responseType, int serial, RIL_Errno e,
5515                                          void *response, size_t responseLen) {
5516#if VDBG
5517    RLOGD("getVoiceRadioTechnologyResponse: serial %d", serial);
5518#endif
5519
5520    if (radioService[slotId]->mRadioResponse != NULL) {
5521        RadioResponseInfo responseInfo = {};
5522        int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
5523        Return<void> retStatus
5524                = radioService[slotId]->mRadioResponse->getVoiceRadioTechnologyResponse(
5525                responseInfo, (RadioTechnology) ret);
5526        radioService[slotId]->checkReturnStatus(retStatus);
5527    } else {
5528        RLOGE("getVoiceRadioTechnologyResponse: radioService[%d]->mRadioResponse == NULL",
5529                slotId);
5530    }
5531
5532    return 0;
5533}
5534
5535int radio::getCellInfoListResponse(int slotId,
5536                                   int responseType,
5537                                   int serial, RIL_Errno e, void *response,
5538                                   size_t responseLen) {
5539#if VDBG
5540    RLOGD("getCellInfoListResponse: serial %d", serial);
5541#endif
5542
5543    if (radioService[slotId]->mRadioResponse != NULL) {
5544        RadioResponseInfo responseInfo = {};
5545        populateResponseInfo(responseInfo, serial, responseType, e);
5546
5547        hidl_vec<CellInfo> ret;
5548        if (response == NULL || responseLen % sizeof(RIL_CellInfo_v12) != 0) {
5549            RLOGE("getCellInfoListResponse: Invalid response");
5550            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5551        } else {
5552            convertRilCellInfoListToHal(response, responseLen, ret);
5553        }
5554
5555        Return<void> retStatus = radioService[slotId]->mRadioResponse->getCellInfoListResponse(
5556                responseInfo, ret);
5557        radioService[slotId]->checkReturnStatus(retStatus);
5558    } else {
5559        RLOGE("getCellInfoListResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5560    }
5561
5562    return 0;
5563}
5564
5565int radio::setCellInfoListRateResponse(int slotId,
5566                                       int responseType,
5567                                       int serial, RIL_Errno e, void *response,
5568                                       size_t responseLen) {
5569#if VDBG
5570    RLOGD("setCellInfoListRateResponse: serial %d", serial);
5571#endif
5572
5573    if (radioService[slotId]->mRadioResponse != NULL) {
5574        RadioResponseInfo responseInfo = {};
5575        populateResponseInfo(responseInfo, serial, responseType, e);
5576        Return<void> retStatus
5577                = radioService[slotId]->mRadioResponse->setCellInfoListRateResponse(responseInfo);
5578        radioService[slotId]->checkReturnStatus(retStatus);
5579    } else {
5580        RLOGE("setCellInfoListRateResponse: radioService[%d]->mRadioResponse == NULL",
5581                slotId);
5582    }
5583
5584    return 0;
5585}
5586
5587int radio::setInitialAttachApnResponse(int slotId,
5588                                       int responseType, int serial, RIL_Errno e,
5589                                       void *response, size_t responseLen) {
5590#if VDBG
5591    RLOGD("setInitialAttachApnResponse: serial %d", serial);
5592#endif
5593
5594    if (radioService[slotId]->mRadioResponse != NULL) {
5595        RadioResponseInfo responseInfo = {};
5596        populateResponseInfo(responseInfo, serial, responseType, e);
5597        Return<void> retStatus
5598                = radioService[slotId]->mRadioResponse->setInitialAttachApnResponse(responseInfo);
5599        radioService[slotId]->checkReturnStatus(retStatus);
5600    } else {
5601        RLOGE("setInitialAttachApnResponse: radioService[%d]->mRadioResponse == NULL",
5602                slotId);
5603    }
5604
5605    return 0;
5606}
5607
5608int radio::getImsRegistrationStateResponse(int slotId,
5609                                           int responseType, int serial, RIL_Errno e,
5610                                           void *response, size_t responseLen) {
5611#if VDBG
5612    RLOGD("getImsRegistrationStateResponse: serial %d", serial);
5613#endif
5614
5615    if (radioService[slotId]->mRadioResponse != NULL) {
5616        RadioResponseInfo responseInfo = {};
5617        populateResponseInfo(responseInfo, serial, responseType, e);
5618        bool isRegistered = false;
5619        int ratFamily = 0;
5620        int numInts = responseLen / sizeof(int);
5621        if (response == NULL || numInts != 2) {
5622            RLOGE("getImsRegistrationStateResponse Invalid response: NULL");
5623            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5624        } else {
5625            int *pInt = (int *) response;
5626            isRegistered = pInt[0] == 1 ? true : false;
5627            ratFamily = pInt[1];
5628        }
5629        Return<void> retStatus
5630                = radioService[slotId]->mRadioResponse->getImsRegistrationStateResponse(
5631                responseInfo, isRegistered, (RadioTechnologyFamily) ratFamily);
5632        radioService[slotId]->checkReturnStatus(retStatus);
5633    } else {
5634        RLOGE("getImsRegistrationStateResponse: radioService[%d]->mRadioResponse == NULL",
5635                slotId);
5636    }
5637
5638    return 0;
5639}
5640
5641int radio::sendImsSmsResponse(int slotId,
5642                              int responseType, int serial, RIL_Errno e, void *response,
5643                              size_t responseLen) {
5644#if VDBG
5645    RLOGD("sendImsSmsResponse: serial %d", serial);
5646#endif
5647
5648    if (radioService[slotId]->mRadioResponse != NULL) {
5649        RadioResponseInfo responseInfo = {};
5650        SendSmsResult result = makeSendSmsResult(responseInfo, serial, responseType, e, response,
5651                responseLen);
5652
5653        Return<void> retStatus
5654                = radioService[slotId]->mRadioResponse->sendImsSmsResponse(responseInfo, result);
5655        radioService[slotId]->checkReturnStatus(retStatus);
5656    } else {
5657        RLOGE("sendSmsResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5658    }
5659
5660    return 0;
5661}
5662
5663int radio::iccTransmitApduBasicChannelResponse(int slotId,
5664                                               int responseType, int serial, RIL_Errno e,
5665                                               void *response, size_t responseLen) {
5666#if VDBG
5667    RLOGD("iccTransmitApduBasicChannelResponse: serial %d", serial);
5668#endif
5669
5670    if (radioService[slotId]->mRadioResponse != NULL) {
5671        RadioResponseInfo responseInfo = {};
5672        IccIoResult result = responseIccIo(responseInfo, serial, responseType, e, response,
5673                responseLen);
5674
5675        Return<void> retStatus
5676                = radioService[slotId]->mRadioResponse->iccTransmitApduBasicChannelResponse(
5677                responseInfo, result);
5678        radioService[slotId]->checkReturnStatus(retStatus);
5679    } else {
5680        RLOGE("iccTransmitApduBasicChannelResponse: radioService[%d]->mRadioResponse "
5681                "== NULL", slotId);
5682    }
5683
5684    return 0;
5685}
5686
5687int radio::iccOpenLogicalChannelResponse(int slotId,
5688                                         int responseType, int serial, RIL_Errno e, void *response,
5689                                         size_t responseLen) {
5690#if VDBG
5691    RLOGD("iccOpenLogicalChannelResponse: serial %d", serial);
5692#endif
5693
5694    if (radioService[slotId]->mRadioResponse != NULL) {
5695        RadioResponseInfo responseInfo = {};
5696        populateResponseInfo(responseInfo, serial, responseType, e);
5697        int channelId = -1;
5698        hidl_vec<int8_t> selectResponse;
5699        int numInts = responseLen / sizeof(int);
5700        if (response == NULL || responseLen % sizeof(int) != 0) {
5701            RLOGE("iccOpenLogicalChannelResponse Invalid response: NULL");
5702            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5703        } else {
5704            int *pInt = (int *) response;
5705            channelId = pInt[0];
5706            selectResponse.resize(numInts - 1);
5707            for (int i = 1; i < numInts; i++) {
5708                selectResponse[i - 1] = (int8_t) pInt[i];
5709            }
5710        }
5711        Return<void> retStatus
5712                = radioService[slotId]->mRadioResponse->iccOpenLogicalChannelResponse(responseInfo,
5713                channelId, selectResponse);
5714        radioService[slotId]->checkReturnStatus(retStatus);
5715    } else {
5716        RLOGE("iccOpenLogicalChannelResponse: radioService[%d]->mRadioResponse == NULL",
5717                slotId);
5718    }
5719
5720    return 0;
5721}
5722
5723int radio::iccCloseLogicalChannelResponse(int slotId,
5724                                          int responseType, int serial, RIL_Errno e,
5725                                          void *response, size_t responseLen) {
5726#if VDBG
5727    RLOGD("iccCloseLogicalChannelResponse: serial %d", serial);
5728#endif
5729
5730    if (radioService[slotId]->mRadioResponse != NULL) {
5731        RadioResponseInfo responseInfo = {};
5732        populateResponseInfo(responseInfo, serial, responseType, e);
5733        Return<void> retStatus
5734                = radioService[slotId]->mRadioResponse->iccCloseLogicalChannelResponse(
5735                responseInfo);
5736        radioService[slotId]->checkReturnStatus(retStatus);
5737    } else {
5738        RLOGE("iccCloseLogicalChannelResponse: radioService[%d]->mRadioResponse == NULL",
5739                slotId);
5740    }
5741
5742    return 0;
5743}
5744
5745int radio::iccTransmitApduLogicalChannelResponse(int slotId,
5746                                                 int responseType, int serial, RIL_Errno e,
5747                                                 void *response, size_t responseLen) {
5748#if VDBG
5749    RLOGD("iccTransmitApduLogicalChannelResponse: serial %d", serial);
5750#endif
5751
5752    if (radioService[slotId]->mRadioResponse != NULL) {
5753        RadioResponseInfo responseInfo = {};
5754        IccIoResult result = responseIccIo(responseInfo, serial, responseType, e, response,
5755                responseLen);
5756
5757        Return<void> retStatus
5758                = radioService[slotId]->mRadioResponse->iccTransmitApduLogicalChannelResponse(
5759                responseInfo, result);
5760        radioService[slotId]->checkReturnStatus(retStatus);
5761    } else {
5762        RLOGE("iccTransmitApduLogicalChannelResponse: radioService[%d]->mRadioResponse "
5763                "== NULL", slotId);
5764    }
5765
5766    return 0;
5767}
5768
5769int radio::nvReadItemResponse(int slotId,
5770                              int responseType, int serial, RIL_Errno e,
5771                              void *response, size_t responseLen) {
5772#if VDBG
5773    RLOGD("nvReadItemResponse: serial %d", serial);
5774#endif
5775
5776    if (radioService[slotId]->mRadioResponse != NULL) {
5777        RadioResponseInfo responseInfo = {};
5778        populateResponseInfo(responseInfo, serial, responseType, e);
5779        Return<void> retStatus = radioService[slotId]->mRadioResponse->nvReadItemResponse(
5780                responseInfo,
5781                convertCharPtrToHidlString((char *) response));
5782        radioService[slotId]->checkReturnStatus(retStatus);
5783    } else {
5784        RLOGE("nvReadItemResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5785    }
5786
5787    return 0;
5788}
5789
5790int radio::nvWriteItemResponse(int slotId,
5791                               int responseType, int serial, RIL_Errno e,
5792                               void *response, size_t responseLen) {
5793#if VDBG
5794    RLOGD("nvWriteItemResponse: serial %d", serial);
5795#endif
5796
5797    if (radioService[slotId]->mRadioResponse != NULL) {
5798        RadioResponseInfo responseInfo = {};
5799        populateResponseInfo(responseInfo, serial, responseType, e);
5800        Return<void> retStatus
5801                = radioService[slotId]->mRadioResponse->nvWriteItemResponse(responseInfo);
5802        radioService[slotId]->checkReturnStatus(retStatus);
5803    } else {
5804        RLOGE("nvWriteItemResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5805    }
5806
5807    return 0;
5808}
5809
5810int radio::nvWriteCdmaPrlResponse(int slotId,
5811                                  int responseType, int serial, RIL_Errno e,
5812                                  void *response, size_t responseLen) {
5813#if VDBG
5814    RLOGD("nvWriteCdmaPrlResponse: serial %d", serial);
5815#endif
5816
5817    if (radioService[slotId]->mRadioResponse != NULL) {
5818        RadioResponseInfo responseInfo = {};
5819        populateResponseInfo(responseInfo, serial, responseType, e);
5820        Return<void> retStatus
5821                = radioService[slotId]->mRadioResponse->nvWriteCdmaPrlResponse(responseInfo);
5822        radioService[slotId]->checkReturnStatus(retStatus);
5823    } else {
5824        RLOGE("nvWriteCdmaPrlResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5825    }
5826
5827    return 0;
5828}
5829
5830int radio::nvResetConfigResponse(int slotId,
5831                                 int responseType, int serial, RIL_Errno e,
5832                                 void *response, size_t responseLen) {
5833#if VDBG
5834    RLOGD("nvResetConfigResponse: serial %d", serial);
5835#endif
5836
5837    if (radioService[slotId]->mRadioResponse != NULL) {
5838        RadioResponseInfo responseInfo = {};
5839        populateResponseInfo(responseInfo, serial, responseType, e);
5840        Return<void> retStatus
5841                = radioService[slotId]->mRadioResponse->nvResetConfigResponse(responseInfo);
5842        radioService[slotId]->checkReturnStatus(retStatus);
5843    } else {
5844        RLOGE("nvResetConfigResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5845    }
5846
5847    return 0;
5848}
5849
5850int radio::setUiccSubscriptionResponse(int slotId,
5851                                       int responseType, int serial, RIL_Errno e,
5852                                       void *response, size_t responseLen) {
5853#if VDBG
5854    RLOGD("setUiccSubscriptionResponse: serial %d", serial);
5855#endif
5856
5857    if (radioService[slotId]->mRadioResponse != NULL) {
5858        RadioResponseInfo responseInfo = {};
5859        populateResponseInfo(responseInfo, serial, responseType, e);
5860        Return<void> retStatus
5861                = radioService[slotId]->mRadioResponse->setUiccSubscriptionResponse(responseInfo);
5862        radioService[slotId]->checkReturnStatus(retStatus);
5863    } else {
5864        RLOGE("setUiccSubscriptionResponse: radioService[%d]->mRadioResponse == NULL",
5865                slotId);
5866    }
5867
5868    return 0;
5869}
5870
5871int radio::setDataAllowedResponse(int slotId,
5872                                  int responseType, int serial, RIL_Errno e,
5873                                  void *response, size_t responseLen) {
5874#if VDBG
5875    RLOGD("setDataAllowedResponse: serial %d", serial);
5876#endif
5877
5878    if (radioService[slotId]->mRadioResponse != NULL) {
5879        RadioResponseInfo responseInfo = {};
5880        populateResponseInfo(responseInfo, serial, responseType, e);
5881        Return<void> retStatus
5882                = radioService[slotId]->mRadioResponse->setDataAllowedResponse(responseInfo);
5883        radioService[slotId]->checkReturnStatus(retStatus);
5884    } else {
5885        RLOGE("setDataAllowedResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5886    }
5887
5888    return 0;
5889}
5890
5891int radio::getHardwareConfigResponse(int slotId,
5892                                     int responseType, int serial, RIL_Errno e,
5893                                     void *response, size_t responseLen) {
5894#if VDBG
5895    RLOGD("getHardwareConfigResponse: serial %d", serial);
5896#endif
5897
5898    if (radioService[slotId]->mRadioResponse != NULL) {
5899        RadioResponseInfo responseInfo = {};
5900        populateResponseInfo(responseInfo, serial, responseType, e);
5901
5902        hidl_vec<HardwareConfig> result;
5903        if (response == NULL || responseLen % sizeof(RIL_HardwareConfig) != 0) {
5904            RLOGE("hardwareConfigChangedInd: invalid response");
5905            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5906        } else {
5907            convertRilHardwareConfigListToHal(response, responseLen, result);
5908        }
5909
5910        Return<void> retStatus = radioService[slotId]->mRadioResponse->getHardwareConfigResponse(
5911                responseInfo, result);
5912        radioService[slotId]->checkReturnStatus(retStatus);
5913    } else {
5914        RLOGE("getHardwareConfigResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5915    }
5916
5917    return 0;
5918}
5919
5920int radio::requestIccSimAuthenticationResponse(int slotId,
5921                                               int responseType, int serial, RIL_Errno e,
5922                                               void *response, size_t responseLen) {
5923#if VDBG
5924    RLOGD("requestIccSimAuthenticationResponse: serial %d", serial);
5925#endif
5926
5927    if (radioService[slotId]->mRadioResponse != NULL) {
5928        RadioResponseInfo responseInfo = {};
5929        IccIoResult result = responseIccIo(responseInfo, serial, responseType, e, response,
5930                responseLen);
5931
5932        Return<void> retStatus
5933                = radioService[slotId]->mRadioResponse->requestIccSimAuthenticationResponse(
5934                responseInfo, result);
5935        radioService[slotId]->checkReturnStatus(retStatus);
5936    } else {
5937        RLOGE("requestIccSimAuthenticationResponse: radioService[%d]->mRadioResponse "
5938                "== NULL", slotId);
5939    }
5940
5941    return 0;
5942}
5943
5944int radio::setDataProfileResponse(int slotId,
5945                                  int responseType, int serial, RIL_Errno e,
5946                                  void *response, size_t responseLen) {
5947#if VDBG
5948    RLOGD("setDataProfileResponse: serial %d", serial);
5949#endif
5950
5951    if (radioService[slotId]->mRadioResponse != NULL) {
5952        RadioResponseInfo responseInfo = {};
5953        populateResponseInfo(responseInfo, serial, responseType, e);
5954        Return<void> retStatus
5955                = radioService[slotId]->mRadioResponse->setDataProfileResponse(responseInfo);
5956        radioService[slotId]->checkReturnStatus(retStatus);
5957    } else {
5958        RLOGE("setDataProfileResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5959    }
5960
5961    return 0;
5962}
5963
5964int radio::requestShutdownResponse(int slotId,
5965                                  int responseType, int serial, RIL_Errno e,
5966                                  void *response, size_t responseLen) {
5967#if VDBG
5968    RLOGD("requestShutdownResponse: serial %d", serial);
5969#endif
5970
5971    if (radioService[slotId]->mRadioResponse != NULL) {
5972        RadioResponseInfo responseInfo = {};
5973        populateResponseInfo(responseInfo, serial, responseType, e);
5974        Return<void> retStatus
5975                = radioService[slotId]->mRadioResponse->requestShutdownResponse(responseInfo);
5976        radioService[slotId]->checkReturnStatus(retStatus);
5977    } else {
5978        RLOGE("requestShutdownResponse: radioService[%d]->mRadioResponse == NULL", slotId);
5979    }
5980
5981    return 0;
5982}
5983
5984void responseRadioCapability(RadioResponseInfo& responseInfo, int serial,
5985        int responseType, RIL_Errno e, void *response, size_t responseLen, RadioCapability& rc) {
5986    populateResponseInfo(responseInfo, serial, responseType, e);
5987
5988    if (response == NULL || responseLen != sizeof(RIL_RadioCapability)) {
5989        RLOGE("responseRadioCapability: Invalid response");
5990        if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
5991        rc.logicalModemUuid = hidl_string();
5992    } else {
5993        convertRilRadioCapabilityToHal(response, responseLen, rc);
5994    }
5995}
5996
5997int radio::getRadioCapabilityResponse(int slotId,
5998                                     int responseType, int serial, RIL_Errno e,
5999                                     void *response, size_t responseLen) {
6000#if VDBG
6001    RLOGD("getRadioCapabilityResponse: serial %d", serial);
6002#endif
6003
6004    if (radioService[slotId]->mRadioResponse != NULL) {
6005        RadioResponseInfo responseInfo = {};
6006        RadioCapability result = {};
6007        responseRadioCapability(responseInfo, serial, responseType, e, response, responseLen,
6008                result);
6009        Return<void> retStatus = radioService[slotId]->mRadioResponse->getRadioCapabilityResponse(
6010                responseInfo, result);
6011        radioService[slotId]->checkReturnStatus(retStatus);
6012    } else {
6013        RLOGE("getRadioCapabilityResponse: radioService[%d]->mRadioResponse == NULL", slotId);
6014    }
6015
6016    return 0;
6017}
6018
6019int radio::setRadioCapabilityResponse(int slotId,
6020                                     int responseType, int serial, RIL_Errno e,
6021                                     void *response, size_t responseLen) {
6022#if VDBG
6023    RLOGD("setRadioCapabilityResponse: serial %d", serial);
6024#endif
6025
6026    if (radioService[slotId]->mRadioResponse != NULL) {
6027        RadioResponseInfo responseInfo = {};
6028        RadioCapability result = {};
6029        responseRadioCapability(responseInfo, serial, responseType, e, response, responseLen,
6030                result);
6031        Return<void> retStatus = radioService[slotId]->mRadioResponse->setRadioCapabilityResponse(
6032                responseInfo, result);
6033        radioService[slotId]->checkReturnStatus(retStatus);
6034    } else {
6035        RLOGE("setRadioCapabilityResponse: radioService[%d]->mRadioResponse == NULL", slotId);
6036    }
6037
6038    return 0;
6039}
6040
6041LceStatusInfo responseLceStatusInfo(RadioResponseInfo& responseInfo, int serial, int responseType,
6042                                    RIL_Errno e, void *response, size_t responseLen) {
6043    populateResponseInfo(responseInfo, serial, responseType, e);
6044    LceStatusInfo result = {};
6045
6046    if (response == NULL || responseLen != sizeof(RIL_LceStatusInfo)) {
6047        RLOGE("Invalid response: NULL");
6048        if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
6049    } else {
6050        RIL_LceStatusInfo *resp = (RIL_LceStatusInfo *) response;
6051        result.lceStatus = (LceStatus) resp->lce_status;
6052        result.actualIntervalMs = (uint8_t) resp->actual_interval_ms;
6053    }
6054    return result;
6055}
6056
6057int radio::startLceServiceResponse(int slotId,
6058                                   int responseType, int serial, RIL_Errno e,
6059                                   void *response, size_t responseLen) {
6060#if VDBG
6061    RLOGD("startLceServiceResponse: serial %d", serial);
6062#endif
6063
6064    if (radioService[slotId]->mRadioResponse != NULL) {
6065        RadioResponseInfo responseInfo = {};
6066        LceStatusInfo result = responseLceStatusInfo(responseInfo, serial, responseType, e,
6067                response, responseLen);
6068
6069        Return<void> retStatus
6070                = radioService[slotId]->mRadioResponse->startLceServiceResponse(responseInfo,
6071                result);
6072        radioService[slotId]->checkReturnStatus(retStatus);
6073    } else {
6074        RLOGE("startLceServiceResponse: radioService[%d]->mRadioResponse == NULL", slotId);
6075    }
6076
6077    return 0;
6078}
6079
6080int radio::stopLceServiceResponse(int slotId,
6081                                  int responseType, int serial, RIL_Errno e,
6082                                  void *response, size_t responseLen) {
6083#if VDBG
6084    RLOGD("stopLceServiceResponse: serial %d", serial);
6085#endif
6086
6087    if (radioService[slotId]->mRadioResponse != NULL) {
6088        RadioResponseInfo responseInfo = {};
6089        LceStatusInfo result = responseLceStatusInfo(responseInfo, serial, responseType, e,
6090                response, responseLen);
6091
6092        Return<void> retStatus
6093                = radioService[slotId]->mRadioResponse->stopLceServiceResponse(responseInfo,
6094                result);
6095        radioService[slotId]->checkReturnStatus(retStatus);
6096    } else {
6097        RLOGE("stopLceServiceResponse: radioService[%d]->mRadioResponse == NULL", slotId);
6098    }
6099
6100    return 0;
6101}
6102
6103int radio::pullLceDataResponse(int slotId,
6104                               int responseType, int serial, RIL_Errno e,
6105                               void *response, size_t responseLen) {
6106#if VDBG
6107    RLOGD("pullLceDataResponse: serial %d", serial);
6108#endif
6109
6110    if (radioService[slotId]->mRadioResponse != NULL) {
6111        RadioResponseInfo responseInfo = {};
6112        populateResponseInfo(responseInfo, serial, responseType, e);
6113
6114        LceDataInfo result = {};
6115        if (response == NULL || responseLen != sizeof(RIL_LceDataInfo)) {
6116            RLOGE("pullLceDataResponse: Invalid response");
6117            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
6118        } else {
6119            convertRilLceDataInfoToHal(response, responseLen, result);
6120        }
6121
6122        Return<void> retStatus = radioService[slotId]->mRadioResponse->pullLceDataResponse(
6123                responseInfo, result);
6124        radioService[slotId]->checkReturnStatus(retStatus);
6125    } else {
6126        RLOGE("pullLceDataResponse: radioService[%d]->mRadioResponse == NULL", slotId);
6127    }
6128
6129    return 0;
6130}
6131
6132int radio::getModemActivityInfoResponse(int slotId,
6133                                        int responseType, int serial, RIL_Errno e,
6134                                        void *response, size_t responseLen) {
6135#if VDBG
6136    RLOGD("getModemActivityInfoResponse: serial %d", serial);
6137#endif
6138
6139    if (radioService[slotId]->mRadioResponse != NULL) {
6140        RadioResponseInfo responseInfo = {};
6141        populateResponseInfo(responseInfo, serial, responseType, e);
6142        ActivityStatsInfo info;
6143        if (response == NULL || responseLen != sizeof(RIL_ActivityStatsInfo)) {
6144            RLOGE("getModemActivityInfoResponse Invalid response: NULL");
6145            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
6146        } else {
6147            RIL_ActivityStatsInfo *resp = (RIL_ActivityStatsInfo *)response;
6148            info.sleepModeTimeMs = resp->sleep_mode_time_ms;
6149            info.idleModeTimeMs = resp->idle_mode_time_ms;
6150            for(int i = 0; i < RIL_NUM_TX_POWER_LEVELS; i++) {
6151                info.txmModetimeMs[i] = resp->tx_mode_time_ms[i];
6152            }
6153            info.rxModeTimeMs = resp->rx_mode_time_ms;
6154        }
6155
6156        Return<void> retStatus
6157                = radioService[slotId]->mRadioResponse->getModemActivityInfoResponse(responseInfo,
6158                info);
6159        radioService[slotId]->checkReturnStatus(retStatus);
6160    } else {
6161        RLOGE("getModemActivityInfoResponse: radioService[%d]->mRadioResponse == NULL",
6162                slotId);
6163    }
6164
6165    return 0;
6166}
6167
6168int radio::setAllowedCarriersResponse(int slotId,
6169                                      int responseType, int serial, RIL_Errno e,
6170                                      void *response, size_t responseLen) {
6171#if VDBG
6172    RLOGD("setAllowedCarriersResponse: serial %d", serial);
6173#endif
6174
6175    if (radioService[slotId]->mRadioResponse != NULL) {
6176        RadioResponseInfo responseInfo = {};
6177        int ret = responseInt(responseInfo, serial, responseType, e, response, responseLen);
6178        Return<void> retStatus
6179                = radioService[slotId]->mRadioResponse->setAllowedCarriersResponse(responseInfo,
6180                ret);
6181        radioService[slotId]->checkReturnStatus(retStatus);
6182    } else {
6183        RLOGE("setAllowedCarriersResponse: radioService[%d]->mRadioResponse == NULL",
6184                slotId);
6185    }
6186
6187    return 0;
6188}
6189
6190int radio::getAllowedCarriersResponse(int slotId,
6191                                      int responseType, int serial, RIL_Errno e,
6192                                      void *response, size_t responseLen) {
6193#if VDBG
6194    RLOGD("getAllowedCarriersResponse: serial %d", serial);
6195#endif
6196
6197    if (radioService[slotId]->mRadioResponse != NULL) {
6198        RadioResponseInfo responseInfo = {};
6199        populateResponseInfo(responseInfo, serial, responseType, e);
6200        CarrierRestrictions carrierInfo = {};
6201        bool allAllowed = true;
6202        if (response == NULL) {
6203#if VDBG
6204            RLOGD("getAllowedCarriersResponse response is NULL: all allowed");
6205#endif
6206            carrierInfo.allowedCarriers.resize(0);
6207            carrierInfo.excludedCarriers.resize(0);
6208        } else if (responseLen != sizeof(RIL_CarrierRestrictions)) {
6209            RLOGE("getAllowedCarriersResponse Invalid response");
6210            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
6211        } else {
6212            RIL_CarrierRestrictions *pCr = (RIL_CarrierRestrictions *)response;
6213            if (pCr->len_allowed_carriers > 0 || pCr->len_excluded_carriers > 0) {
6214                allAllowed = false;
6215            }
6216
6217            carrierInfo.allowedCarriers.resize(pCr->len_allowed_carriers);
6218            for(int i = 0; i < pCr->len_allowed_carriers; i++) {
6219                RIL_Carrier *carrier = pCr->allowed_carriers + i;
6220                carrierInfo.allowedCarriers[i].mcc = convertCharPtrToHidlString(carrier->mcc);
6221                carrierInfo.allowedCarriers[i].mnc = convertCharPtrToHidlString(carrier->mnc);
6222                carrierInfo.allowedCarriers[i].matchType = (CarrierMatchType) carrier->match_type;
6223                carrierInfo.allowedCarriers[i].matchData =
6224                        convertCharPtrToHidlString(carrier->match_data);
6225            }
6226
6227            carrierInfo.excludedCarriers.resize(pCr->len_excluded_carriers);
6228            for(int i = 0; i < pCr->len_excluded_carriers; i++) {
6229                RIL_Carrier *carrier = pCr->excluded_carriers + i;
6230                carrierInfo.excludedCarriers[i].mcc = convertCharPtrToHidlString(carrier->mcc);
6231                carrierInfo.excludedCarriers[i].mnc = convertCharPtrToHidlString(carrier->mnc);
6232                carrierInfo.excludedCarriers[i].matchType = (CarrierMatchType) carrier->match_type;
6233                carrierInfo.excludedCarriers[i].matchData =
6234                        convertCharPtrToHidlString(carrier->match_data);
6235            }
6236        }
6237
6238        Return<void> retStatus
6239                = radioService[slotId]->mRadioResponse->getAllowedCarriersResponse(responseInfo,
6240                allAllowed, carrierInfo);
6241        radioService[slotId]->checkReturnStatus(retStatus);
6242    } else {
6243        RLOGE("getAllowedCarriersResponse: radioService[%d]->mRadioResponse == NULL",
6244                slotId);
6245    }
6246
6247    return 0;
6248}
6249
6250int radio::sendDeviceStateResponse(int slotId,
6251                              int responseType, int serial, RIL_Errno e,
6252                              void *response, size_t responselen) {
6253#if VDBG
6254    RLOGD("sendDeviceStateResponse: serial %d", serial);
6255#endif
6256
6257    if (radioService[slotId]->mRadioResponse != NULL) {
6258        RadioResponseInfo responseInfo = {};
6259        populateResponseInfo(responseInfo, serial, responseType, e);
6260        Return<void> retStatus
6261                = radioService[slotId]->mRadioResponse->sendDeviceStateResponse(responseInfo);
6262        radioService[slotId]->checkReturnStatus(retStatus);
6263    } else {
6264        RLOGE("sendDeviceStateResponse: radioService[%d]->mRadioResponse == NULL", slotId);
6265    }
6266
6267    return 0;
6268}
6269
6270int radio::setIndicationFilterResponse(int slotId,
6271                              int responseType, int serial, RIL_Errno e,
6272                              void *response, size_t responselen) {
6273#if VDBG
6274    RLOGD("setIndicationFilterResponse: serial %d", serial);
6275#endif
6276
6277    if (radioService[slotId]->mRadioResponse != NULL) {
6278        RadioResponseInfo responseInfo = {};
6279        populateResponseInfo(responseInfo, serial, responseType, e);
6280        Return<void> retStatus
6281                = radioService[slotId]->mRadioResponse->setIndicationFilterResponse(responseInfo);
6282        radioService[slotId]->checkReturnStatus(retStatus);
6283    } else {
6284        RLOGE("setIndicationFilterResponse: radioService[%d]->mRadioResponse == NULL",
6285                slotId);
6286    }
6287
6288    return 0;
6289}
6290
6291
6292int radio::setSimCardPowerResponse(int slotId,
6293                                   int responseType, int serial, RIL_Errno e,
6294                                   void *response, size_t responseLen) {
6295#if VDBG
6296    RLOGD("setSimCardPowerResponse: serial %d", serial);
6297#endif
6298
6299    if (radioService[slotId]->mRadioResponse != NULL) {
6300        RadioResponseInfo responseInfo = {};
6301        populateResponseInfo(responseInfo, serial, responseType, e);
6302        Return<void> retStatus
6303                = radioService[slotId]->mRadioResponse->setSimCardPowerResponse(responseInfo);
6304        radioService[slotId]->checkReturnStatus(retStatus);
6305    } else {
6306        RLOGE("setSimCardPowerResponse: radioService[%d]->mRadioResponse == NULL", slotId);
6307    }
6308
6309    return 0;
6310}
6311
6312int radio::sendRequestRawResponse(int slotId,
6313                                  int responseType, int serial, RIL_Errno e,
6314                                  void *response, size_t responseLen) {
6315#if VDBG
6316   RLOGD("sendRequestRawResponse: serial %d", serial);
6317#endif
6318
6319    if (oemHookService[slotId]->mOemHookResponse != NULL) {
6320        RadioResponseInfo responseInfo = {};
6321        populateResponseInfo(responseInfo, serial, responseType, e);
6322        hidl_vec<uint8_t> data;
6323
6324        if (response == NULL) {
6325            RLOGE("sendRequestRawResponse: Invalid response");
6326            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
6327        } else {
6328            data.setToExternal((uint8_t *) response, responseLen);
6329        }
6330        Return<void> retStatus = oemHookService[slotId]->mOemHookResponse->
6331                sendRequestRawResponse(responseInfo, data);
6332        checkReturnStatus(slotId, retStatus, false);
6333    } else {
6334        RLOGE("sendRequestRawResponse: oemHookService[%d]->mOemHookResponse == NULL",
6335                slotId);
6336    }
6337
6338    return 0;
6339}
6340
6341int radio::sendRequestStringsResponse(int slotId,
6342                                      int responseType, int serial, RIL_Errno e,
6343                                      void *response, size_t responseLen) {
6344#if VDBG
6345    RLOGD("sendRequestStringsResponse: serial %d", serial);
6346#endif
6347
6348    if (oemHookService[slotId]->mOemHookResponse != NULL) {
6349        RadioResponseInfo responseInfo = {};
6350        populateResponseInfo(responseInfo, serial, responseType, e);
6351        hidl_vec<hidl_string> data;
6352
6353        if (response == NULL || responseLen % sizeof(char *) != 0) {
6354            RLOGE("sendRequestStringsResponse Invalid response: NULL");
6355            if (e == RIL_E_SUCCESS) responseInfo.error = RadioError::INVALID_RESPONSE;
6356        } else {
6357            char **resp = (char **) response;
6358            int numStrings = responseLen / sizeof(char *);
6359            data.resize(numStrings);
6360            for (int i = 0; i < numStrings; i++) {
6361                data[i] = convertCharPtrToHidlString(resp[i]);
6362            }
6363        }
6364        Return<void> retStatus
6365                = oemHookService[slotId]->mOemHookResponse->sendRequestStringsResponse(
6366                responseInfo, data);
6367        checkReturnStatus(slotId, retStatus, false);
6368    } else {
6369        RLOGE("sendRequestStringsResponse: oemHookService[%d]->mOemHookResponse == "
6370                "NULL", slotId);
6371    }
6372
6373    return 0;
6374}
6375
6376// Radio Indication functions
6377
6378RadioIndicationType convertIntToRadioIndicationType(int indicationType) {
6379    return indicationType == RESPONSE_UNSOLICITED ? (RadioIndicationType::UNSOLICITED) :
6380            (RadioIndicationType::UNSOLICITED_ACK_EXP);
6381}
6382
6383int radio::radioStateChangedInd(int slotId,
6384                                 int indicationType, int token, RIL_Errno e, void *response,
6385                                 size_t responseLen) {
6386    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6387        RadioState radioState =
6388                (RadioState) CALL_ONSTATEREQUEST(slotId);
6389        RLOGD("radioStateChangedInd: radioState %d", radioState);
6390        Return<void> retStatus = radioService[slotId]->mRadioIndication->radioStateChanged(
6391                convertIntToRadioIndicationType(indicationType), radioState);
6392        radioService[slotId]->checkReturnStatus(retStatus);
6393    } else {
6394        RLOGE("radioStateChangedInd: radioService[%d]->mRadioIndication == NULL", slotId);
6395    }
6396
6397    return 0;
6398}
6399
6400int radio::callStateChangedInd(int slotId,
6401                               int indicationType, int token, RIL_Errno e, void *response,
6402                               size_t responseLen) {
6403    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6404#if VDBG
6405        RLOGD("callStateChangedInd");
6406#endif
6407        Return<void> retStatus = radioService[slotId]->mRadioIndication->callStateChanged(
6408                convertIntToRadioIndicationType(indicationType));
6409        radioService[slotId]->checkReturnStatus(retStatus);
6410    } else {
6411        RLOGE("callStateChangedInd: radioService[%d]->mRadioIndication == NULL", slotId);
6412    }
6413
6414    return 0;
6415}
6416
6417int radio::networkStateChangedInd(int slotId,
6418                                  int indicationType, int token, RIL_Errno e, void *response,
6419                                  size_t responseLen) {
6420    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6421#if VDBG
6422        RLOGD("networkStateChangedInd");
6423#endif
6424        Return<void> retStatus = radioService[slotId]->mRadioIndication->networkStateChanged(
6425                convertIntToRadioIndicationType(indicationType));
6426        radioService[slotId]->checkReturnStatus(retStatus);
6427    } else {
6428        RLOGE("networkStateChangedInd: radioService[%d]->mRadioIndication == NULL",
6429                slotId);
6430    }
6431
6432    return 0;
6433}
6434
6435uint8_t hexCharToInt(uint8_t c) {
6436    if (c >= '0' && c <= '9') return (c - '0');
6437    if (c >= 'A' && c <= 'F') return (c - 'A' + 10);
6438    if (c >= 'a' && c <= 'f') return (c - 'a' + 10);
6439
6440    return INVALID_HEX_CHAR;
6441}
6442
6443uint8_t * convertHexStringToBytes(void *response, size_t responseLen) {
6444    if (responseLen % 2 != 0) {
6445        return NULL;
6446    }
6447
6448    uint8_t *bytes = (uint8_t *)calloc(responseLen/2, sizeof(uint8_t));
6449    if (bytes == NULL) {
6450        RLOGE("convertHexStringToBytes: cannot allocate memory for bytes string");
6451        return NULL;
6452    }
6453    uint8_t *hexString = (uint8_t *)response;
6454
6455    for (size_t i = 0; i < responseLen; i += 2) {
6456        uint8_t hexChar1 = hexCharToInt(hexString[i]);
6457        uint8_t hexChar2 = hexCharToInt(hexString[i + 1]);
6458
6459        if (hexChar1 == INVALID_HEX_CHAR || hexChar2 == INVALID_HEX_CHAR) {
6460            RLOGE("convertHexStringToBytes: invalid hex char %d %d",
6461                    hexString[i], hexString[i + 1]);
6462            free(bytes);
6463            return NULL;
6464        }
6465        bytes[i/2] = ((hexChar1 << 4) | hexChar2);
6466    }
6467
6468    return bytes;
6469}
6470
6471int radio::newSmsInd(int slotId, int indicationType,
6472                     int token, RIL_Errno e, void *response, size_t responseLen) {
6473    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6474        if (response == NULL || responseLen == 0) {
6475            RLOGE("newSmsInd: invalid response");
6476            return 0;
6477        }
6478
6479        uint8_t *bytes = convertHexStringToBytes(response, responseLen);
6480        if (bytes == NULL) {
6481            RLOGE("newSmsInd: convertHexStringToBytes failed");
6482            return 0;
6483        }
6484
6485        hidl_vec<uint8_t> pdu;
6486        pdu.setToExternal(bytes, responseLen/2);
6487#if VDBG
6488        RLOGD("newSmsInd");
6489#endif
6490        Return<void> retStatus = radioService[slotId]->mRadioIndication->newSms(
6491                convertIntToRadioIndicationType(indicationType), pdu);
6492        radioService[slotId]->checkReturnStatus(retStatus);
6493        free(bytes);
6494    } else {
6495        RLOGE("newSmsInd: radioService[%d]->mRadioIndication == NULL", slotId);
6496    }
6497
6498    return 0;
6499}
6500
6501int radio::newSmsStatusReportInd(int slotId,
6502                                 int indicationType, int token, RIL_Errno e, void *response,
6503                                 size_t responseLen) {
6504    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6505        if (response == NULL || responseLen == 0) {
6506            RLOGE("newSmsStatusReportInd: invalid response");
6507            return 0;
6508        }
6509
6510        uint8_t *bytes = convertHexStringToBytes(response, responseLen);
6511        if (bytes == NULL) {
6512            RLOGE("newSmsStatusReportInd: convertHexStringToBytes failed");
6513            return 0;
6514        }
6515
6516        hidl_vec<uint8_t> pdu;
6517        pdu.setToExternal(bytes, responseLen/2);
6518#if VDBG
6519        RLOGD("newSmsStatusReportInd");
6520#endif
6521        Return<void> retStatus = radioService[slotId]->mRadioIndication->newSmsStatusReport(
6522                convertIntToRadioIndicationType(indicationType), pdu);
6523        radioService[slotId]->checkReturnStatus(retStatus);
6524        free(bytes);
6525    } else {
6526        RLOGE("newSmsStatusReportInd: radioService[%d]->mRadioIndication == NULL", slotId);
6527    }
6528
6529    return 0;
6530}
6531
6532int radio::newSmsOnSimInd(int slotId, int indicationType,
6533                          int token, RIL_Errno e, void *response, size_t responseLen) {
6534    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6535        if (response == NULL || responseLen != sizeof(int)) {
6536            RLOGE("newSmsOnSimInd: invalid response");
6537            return 0;
6538        }
6539        int32_t recordNumber = ((int32_t *) response)[0];
6540#if VDBG
6541        RLOGD("newSmsOnSimInd: slotIndex %d", recordNumber);
6542#endif
6543        Return<void> retStatus = radioService[slotId]->mRadioIndication->newSmsOnSim(
6544                convertIntToRadioIndicationType(indicationType), recordNumber);
6545        radioService[slotId]->checkReturnStatus(retStatus);
6546    } else {
6547        RLOGE("newSmsOnSimInd: radioService[%d]->mRadioIndication == NULL", slotId);
6548    }
6549
6550    return 0;
6551}
6552
6553int radio::onUssdInd(int slotId, int indicationType,
6554                     int token, RIL_Errno e, void *response, size_t responseLen) {
6555    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6556        if (response == NULL || responseLen != 2 * sizeof(char *)) {
6557            RLOGE("onUssdInd: invalid response");
6558            return 0;
6559        }
6560        char **strings = (char **) response;
6561        char *mode = strings[0];
6562        hidl_string msg = convertCharPtrToHidlString(strings[1]);
6563        UssdModeType modeType = (UssdModeType) atoi(mode);
6564#if VDBG
6565        RLOGD("onUssdInd: mode %s", mode);
6566#endif
6567        Return<void> retStatus = radioService[slotId]->mRadioIndication->onUssd(
6568                convertIntToRadioIndicationType(indicationType), modeType, msg);
6569        radioService[slotId]->checkReturnStatus(retStatus);
6570    } else {
6571        RLOGE("onUssdInd: radioService[%d]->mRadioIndication == NULL", slotId);
6572    }
6573
6574    return 0;
6575}
6576
6577int radio::nitzTimeReceivedInd(int slotId,
6578                               int indicationType, int token, RIL_Errno e, void *response,
6579                               size_t responseLen) {
6580    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6581        if (response == NULL || responseLen == 0) {
6582            RLOGE("nitzTimeReceivedInd: invalid response");
6583            return 0;
6584        }
6585        hidl_string nitzTime = convertCharPtrToHidlString((char *) response);
6586        int64_t timeReceived = android::elapsedRealtime();
6587#if VDBG
6588        RLOGD("nitzTimeReceivedInd: nitzTime %s receivedTime %" PRId64, nitzTime.c_str(),
6589                timeReceived);
6590#endif
6591        Return<void> retStatus = radioService[slotId]->mRadioIndication->nitzTimeReceived(
6592                convertIntToRadioIndicationType(indicationType), nitzTime, timeReceived);
6593        radioService[slotId]->checkReturnStatus(retStatus);
6594    } else {
6595        RLOGE("nitzTimeReceivedInd: radioService[%d]->mRadioIndication == NULL", slotId);
6596        return -1;
6597    }
6598
6599    return 0;
6600}
6601
6602void convertRilSignalStrengthToHal(void *response, size_t responseLen,
6603        SignalStrength& signalStrength) {
6604    RIL_SignalStrength_v10 *rilSignalStrength = (RIL_SignalStrength_v10 *) response;
6605
6606    // Fixup LTE for backwards compatibility
6607    // signalStrength: -1 -> 99
6608    if (rilSignalStrength->LTE_SignalStrength.signalStrength == -1) {
6609        rilSignalStrength->LTE_SignalStrength.signalStrength = 99;
6610    }
6611    // rsrp: -1 -> INT_MAX all other negative value to positive.
6612    // So remap here
6613    if (rilSignalStrength->LTE_SignalStrength.rsrp == -1) {
6614        rilSignalStrength->LTE_SignalStrength.rsrp = INT_MAX;
6615    } else if (rilSignalStrength->LTE_SignalStrength.rsrp < -1) {
6616        rilSignalStrength->LTE_SignalStrength.rsrp = -rilSignalStrength->LTE_SignalStrength.rsrp;
6617    }
6618    // rsrq: -1 -> INT_MAX
6619    if (rilSignalStrength->LTE_SignalStrength.rsrq == -1) {
6620        rilSignalStrength->LTE_SignalStrength.rsrq = INT_MAX;
6621    }
6622    // Not remapping rssnr is already using INT_MAX
6623    // cqi: -1 -> INT_MAX
6624    if (rilSignalStrength->LTE_SignalStrength.cqi == -1) {
6625        rilSignalStrength->LTE_SignalStrength.cqi = INT_MAX;
6626    }
6627
6628    signalStrength.gw.signalStrength = rilSignalStrength->GW_SignalStrength.signalStrength;
6629    signalStrength.gw.bitErrorRate = rilSignalStrength->GW_SignalStrength.bitErrorRate;
6630    signalStrength.cdma.dbm = rilSignalStrength->CDMA_SignalStrength.dbm;
6631    signalStrength.cdma.ecio = rilSignalStrength->CDMA_SignalStrength.ecio;
6632    signalStrength.evdo.dbm = rilSignalStrength->EVDO_SignalStrength.dbm;
6633    signalStrength.evdo.ecio = rilSignalStrength->EVDO_SignalStrength.ecio;
6634    signalStrength.evdo.signalNoiseRatio =
6635            rilSignalStrength->EVDO_SignalStrength.signalNoiseRatio;
6636    signalStrength.lte.signalStrength = rilSignalStrength->LTE_SignalStrength.signalStrength;
6637    signalStrength.lte.rsrp = rilSignalStrength->LTE_SignalStrength.rsrp;
6638    signalStrength.lte.rsrq = rilSignalStrength->LTE_SignalStrength.rsrq;
6639    signalStrength.lte.rssnr = rilSignalStrength->LTE_SignalStrength.rssnr;
6640    signalStrength.lte.cqi = rilSignalStrength->LTE_SignalStrength.cqi;
6641    signalStrength.lte.timingAdvance = rilSignalStrength->LTE_SignalStrength.timingAdvance;
6642    signalStrength.tdScdma.rscp = rilSignalStrength->TD_SCDMA_SignalStrength.rscp;
6643}
6644
6645int radio::currentSignalStrengthInd(int slotId,
6646                                    int indicationType, int token, RIL_Errno e,
6647                                    void *response, size_t responseLen) {
6648    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6649        if (response == NULL || responseLen != sizeof(RIL_SignalStrength_v10)) {
6650            RLOGE("currentSignalStrengthInd: invalid response");
6651            return 0;
6652        }
6653
6654        SignalStrength signalStrength = {};
6655        convertRilSignalStrengthToHal(response, responseLen, signalStrength);
6656
6657#if VDBG
6658        RLOGD("currentSignalStrengthInd");
6659#endif
6660        Return<void> retStatus = radioService[slotId]->mRadioIndication->currentSignalStrength(
6661                convertIntToRadioIndicationType(indicationType), signalStrength);
6662        radioService[slotId]->checkReturnStatus(retStatus);
6663    } else {
6664        RLOGE("currentSignalStrengthInd: radioService[%d]->mRadioIndication == NULL",
6665                slotId);
6666    }
6667
6668    return 0;
6669}
6670
6671void convertRilDataCallToHal(RIL_Data_Call_Response_v11 *dcResponse,
6672        SetupDataCallResult& dcResult) {
6673    dcResult.status = (DataCallFailCause) dcResponse->status;
6674    dcResult.suggestedRetryTime = dcResponse->suggestedRetryTime;
6675    dcResult.cid = dcResponse->cid;
6676    dcResult.active = dcResponse->active;
6677    dcResult.type = convertCharPtrToHidlString(dcResponse->type);
6678    dcResult.ifname = convertCharPtrToHidlString(dcResponse->ifname);
6679    dcResult.addresses = convertCharPtrToHidlString(dcResponse->addresses);
6680    dcResult.dnses = convertCharPtrToHidlString(dcResponse->dnses);
6681    dcResult.gateways = convertCharPtrToHidlString(dcResponse->gateways);
6682    dcResult.pcscf = convertCharPtrToHidlString(dcResponse->pcscf);
6683    dcResult.mtu = dcResponse->mtu;
6684}
6685
6686void convertRilDataCallListToHal(void *response, size_t responseLen,
6687        hidl_vec<SetupDataCallResult>& dcResultList) {
6688    int num = responseLen / sizeof(RIL_Data_Call_Response_v11);
6689
6690    RIL_Data_Call_Response_v11 *dcResponse = (RIL_Data_Call_Response_v11 *) response;
6691    dcResultList.resize(num);
6692    for (int i = 0; i < num; i++) {
6693        convertRilDataCallToHal(&dcResponse[i], dcResultList[i]);
6694    }
6695}
6696
6697int radio::dataCallListChangedInd(int slotId,
6698                                  int indicationType, int token, RIL_Errno e, void *response,
6699                                  size_t responseLen) {
6700    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6701        if (response == NULL || responseLen % sizeof(RIL_Data_Call_Response_v11) != 0) {
6702            RLOGE("dataCallListChangedInd: invalid response");
6703            return 0;
6704        }
6705        hidl_vec<SetupDataCallResult> dcList;
6706        convertRilDataCallListToHal(response, responseLen, dcList);
6707#if VDBG
6708        RLOGD("dataCallListChangedInd");
6709#endif
6710        Return<void> retStatus = radioService[slotId]->mRadioIndication->dataCallListChanged(
6711                convertIntToRadioIndicationType(indicationType), dcList);
6712        radioService[slotId]->checkReturnStatus(retStatus);
6713    } else {
6714        RLOGE("dataCallListChangedInd: radioService[%d]->mRadioIndication == NULL", slotId);
6715    }
6716
6717    return 0;
6718}
6719
6720int radio::suppSvcNotifyInd(int slotId, int indicationType,
6721                            int token, RIL_Errno e, void *response, size_t responseLen) {
6722    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6723        if (response == NULL || responseLen != sizeof(RIL_SuppSvcNotification)) {
6724            RLOGE("suppSvcNotifyInd: invalid response");
6725            return 0;
6726        }
6727
6728        SuppSvcNotification suppSvc = {};
6729        RIL_SuppSvcNotification *ssn = (RIL_SuppSvcNotification *) response;
6730        suppSvc.isMT = ssn->notificationType;
6731        suppSvc.code = ssn->code;
6732        suppSvc.index = ssn->index;
6733        suppSvc.type = ssn->type;
6734        suppSvc.number = convertCharPtrToHidlString(ssn->number);
6735
6736#if VDBG
6737        RLOGD("suppSvcNotifyInd: isMT %d code %d index %d type %d",
6738                suppSvc.isMT, suppSvc.code, suppSvc.index, suppSvc.type);
6739#endif
6740        Return<void> retStatus = radioService[slotId]->mRadioIndication->suppSvcNotify(
6741                convertIntToRadioIndicationType(indicationType), suppSvc);
6742        radioService[slotId]->checkReturnStatus(retStatus);
6743    } else {
6744        RLOGE("suppSvcNotifyInd: radioService[%d]->mRadioIndication == NULL", slotId);
6745    }
6746
6747    return 0;
6748}
6749
6750int radio::stkSessionEndInd(int slotId, int indicationType,
6751                            int token, RIL_Errno e, void *response, size_t responseLen) {
6752    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6753#if VDBG
6754        RLOGD("stkSessionEndInd");
6755#endif
6756        Return<void> retStatus = radioService[slotId]->mRadioIndication->stkSessionEnd(
6757                convertIntToRadioIndicationType(indicationType));
6758        radioService[slotId]->checkReturnStatus(retStatus);
6759    } else {
6760        RLOGE("stkSessionEndInd: radioService[%d]->mRadioIndication == NULL", slotId);
6761    }
6762
6763    return 0;
6764}
6765
6766int radio::stkProactiveCommandInd(int slotId,
6767                                  int indicationType, int token, RIL_Errno e, void *response,
6768                                  size_t responseLen) {
6769    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6770        if (response == NULL || responseLen == 0) {
6771            RLOGE("stkProactiveCommandInd: invalid response");
6772            return 0;
6773        }
6774#if VDBG
6775        RLOGD("stkProactiveCommandInd");
6776#endif
6777        Return<void> retStatus = radioService[slotId]->mRadioIndication->stkProactiveCommand(
6778                convertIntToRadioIndicationType(indicationType),
6779                convertCharPtrToHidlString((char *) response));
6780        radioService[slotId]->checkReturnStatus(retStatus);
6781    } else {
6782        RLOGE("stkProactiveCommandInd: radioService[%d]->mRadioIndication == NULL", slotId);
6783    }
6784
6785    return 0;
6786}
6787
6788int radio::stkEventNotifyInd(int slotId, int indicationType,
6789                             int token, RIL_Errno e, void *response, size_t responseLen) {
6790    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6791        if (response == NULL || responseLen == 0) {
6792            RLOGE("stkEventNotifyInd: invalid response");
6793            return 0;
6794        }
6795#if VDBG
6796        RLOGD("stkEventNotifyInd");
6797#endif
6798        Return<void> retStatus = radioService[slotId]->mRadioIndication->stkEventNotify(
6799                convertIntToRadioIndicationType(indicationType),
6800                convertCharPtrToHidlString((char *) response));
6801        radioService[slotId]->checkReturnStatus(retStatus);
6802    } else {
6803        RLOGE("stkEventNotifyInd: radioService[%d]->mRadioIndication == NULL", slotId);
6804    }
6805
6806    return 0;
6807}
6808
6809int radio::stkCallSetupInd(int slotId, int indicationType,
6810                           int token, RIL_Errno e, void *response, size_t responseLen) {
6811    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6812        if (response == NULL || responseLen != sizeof(int)) {
6813            RLOGE("stkCallSetupInd: invalid response");
6814            return 0;
6815        }
6816        int32_t timeout = ((int32_t *) response)[0];
6817#if VDBG
6818        RLOGD("stkCallSetupInd: timeout %d", timeout);
6819#endif
6820        Return<void> retStatus = radioService[slotId]->mRadioIndication->stkCallSetup(
6821                convertIntToRadioIndicationType(indicationType), timeout);
6822        radioService[slotId]->checkReturnStatus(retStatus);
6823    } else {
6824        RLOGE("stkCallSetupInd: radioService[%d]->mRadioIndication == NULL", slotId);
6825    }
6826
6827    return 0;
6828}
6829
6830int radio::simSmsStorageFullInd(int slotId,
6831                                int indicationType, int token, RIL_Errno e, void *response,
6832                                size_t responseLen) {
6833    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6834#if VDBG
6835        RLOGD("simSmsStorageFullInd");
6836#endif
6837        Return<void> retStatus = radioService[slotId]->mRadioIndication->simSmsStorageFull(
6838                convertIntToRadioIndicationType(indicationType));
6839        radioService[slotId]->checkReturnStatus(retStatus);
6840    } else {
6841        RLOGE("simSmsStorageFullInd: radioService[%d]->mRadioIndication == NULL", slotId);
6842    }
6843
6844    return 0;
6845}
6846
6847int radio::simRefreshInd(int slotId, int indicationType,
6848                         int token, RIL_Errno e, void *response, size_t responseLen) {
6849    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6850        if (response == NULL || responseLen != sizeof(RIL_SimRefreshResponse_v7)) {
6851            RLOGE("simRefreshInd: invalid response");
6852            return 0;
6853        }
6854
6855        SimRefreshResult refreshResult = {};
6856        RIL_SimRefreshResponse_v7 *simRefreshResponse = ((RIL_SimRefreshResponse_v7 *) response);
6857        refreshResult.type =
6858                (android::hardware::radio::V1_0::SimRefreshType) simRefreshResponse->result;
6859        refreshResult.efId = simRefreshResponse->ef_id;
6860        refreshResult.aid = convertCharPtrToHidlString(simRefreshResponse->aid);
6861
6862#if VDBG
6863        RLOGD("simRefreshInd: type %d efId %d", refreshResult.type, refreshResult.efId);
6864#endif
6865        Return<void> retStatus = radioService[slotId]->mRadioIndication->simRefresh(
6866                convertIntToRadioIndicationType(indicationType), refreshResult);
6867        radioService[slotId]->checkReturnStatus(retStatus);
6868    } else {
6869        RLOGE("simRefreshInd: radioService[%d]->mRadioIndication == NULL", slotId);
6870    }
6871
6872    return 0;
6873}
6874
6875void convertRilCdmaSignalInfoRecordToHal(RIL_CDMA_SignalInfoRecord *signalInfoRecord,
6876        CdmaSignalInfoRecord& record) {
6877    record.isPresent = signalInfoRecord->isPresent;
6878    record.signalType = signalInfoRecord->signalType;
6879    record.alertPitch = signalInfoRecord->alertPitch;
6880    record.signal = signalInfoRecord->signal;
6881}
6882
6883int radio::callRingInd(int slotId, int indicationType,
6884                       int token, RIL_Errno e, void *response, size_t responseLen) {
6885    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6886        bool isGsm;
6887        CdmaSignalInfoRecord record = {};
6888        if (response == NULL || responseLen == 0) {
6889            isGsm = true;
6890        } else {
6891            isGsm = false;
6892            if (responseLen != sizeof (RIL_CDMA_SignalInfoRecord)) {
6893                RLOGE("callRingInd: invalid response");
6894                return 0;
6895            }
6896            convertRilCdmaSignalInfoRecordToHal((RIL_CDMA_SignalInfoRecord *) response, record);
6897        }
6898
6899#if VDBG
6900        RLOGD("callRingInd: isGsm %d", isGsm);
6901#endif
6902        Return<void> retStatus = radioService[slotId]->mRadioIndication->callRing(
6903                convertIntToRadioIndicationType(indicationType), isGsm, record);
6904        radioService[slotId]->checkReturnStatus(retStatus);
6905    } else {
6906        RLOGE("callRingInd: radioService[%d]->mRadioIndication == NULL", slotId);
6907    }
6908
6909    return 0;
6910}
6911
6912int radio::simStatusChangedInd(int slotId,
6913                               int indicationType, int token, RIL_Errno e, void *response,
6914                               size_t responseLen) {
6915    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6916#if VDBG
6917        RLOGD("simStatusChangedInd");
6918#endif
6919        Return<void> retStatus = radioService[slotId]->mRadioIndication->simStatusChanged(
6920                convertIntToRadioIndicationType(indicationType));
6921        radioService[slotId]->checkReturnStatus(retStatus);
6922    } else {
6923        RLOGE("simStatusChangedInd: radioService[%d]->mRadioIndication == NULL", slotId);
6924    }
6925
6926    return 0;
6927}
6928
6929int radio::cdmaNewSmsInd(int slotId, int indicationType,
6930                         int token, RIL_Errno e, void *response, size_t responseLen) {
6931    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6932        if (response == NULL || responseLen != sizeof(RIL_CDMA_SMS_Message)) {
6933            RLOGE("cdmaNewSmsInd: invalid response");
6934            return 0;
6935        }
6936
6937        CdmaSmsMessage msg = {};
6938        RIL_CDMA_SMS_Message *rilMsg = (RIL_CDMA_SMS_Message *) response;
6939        msg.teleserviceId = rilMsg->uTeleserviceID;
6940        msg.isServicePresent = rilMsg->bIsServicePresent;
6941        msg.serviceCategory = rilMsg->uServicecategory;
6942        msg.address.digitMode =
6943                (android::hardware::radio::V1_0::CdmaSmsDigitMode) rilMsg->sAddress.digit_mode;
6944        msg.address.numberMode =
6945                (android::hardware::radio::V1_0::CdmaSmsNumberMode) rilMsg->sAddress.number_mode;
6946        msg.address.numberType =
6947                (android::hardware::radio::V1_0::CdmaSmsNumberType) rilMsg->sAddress.number_type;
6948        msg.address.numberPlan =
6949                (android::hardware::radio::V1_0::CdmaSmsNumberPlan) rilMsg->sAddress.number_plan;
6950
6951        int digitLimit = MIN((rilMsg->sAddress.number_of_digits), RIL_CDMA_SMS_ADDRESS_MAX);
6952        msg.address.digits.setToExternal(rilMsg->sAddress.digits, digitLimit);
6953
6954        msg.subAddress.subaddressType = (android::hardware::radio::V1_0::CdmaSmsSubaddressType)
6955                rilMsg->sSubAddress.subaddressType;
6956        msg.subAddress.odd = rilMsg->sSubAddress.odd;
6957
6958        digitLimit= MIN((rilMsg->sSubAddress.number_of_digits), RIL_CDMA_SMS_SUBADDRESS_MAX);
6959        msg.subAddress.digits.setToExternal(rilMsg->sSubAddress.digits, digitLimit);
6960
6961        digitLimit = MIN((rilMsg->uBearerDataLen), RIL_CDMA_SMS_BEARER_DATA_MAX);
6962        msg.bearerData.setToExternal(rilMsg->aBearerData, digitLimit);
6963
6964#if VDBG
6965        RLOGD("cdmaNewSmsInd");
6966#endif
6967        Return<void> retStatus = radioService[slotId]->mRadioIndication->cdmaNewSms(
6968                convertIntToRadioIndicationType(indicationType), msg);
6969        radioService[slotId]->checkReturnStatus(retStatus);
6970    } else {
6971        RLOGE("cdmaNewSmsInd: radioService[%d]->mRadioIndication == NULL", slotId);
6972    }
6973
6974    return 0;
6975}
6976
6977int radio::newBroadcastSmsInd(int slotId,
6978                              int indicationType, int token, RIL_Errno e, void *response,
6979                              size_t responseLen) {
6980    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
6981        if (response == NULL || responseLen == 0) {
6982            RLOGE("newBroadcastSmsInd: invalid response");
6983            return 0;
6984        }
6985
6986        hidl_vec<uint8_t> data;
6987        data.setToExternal((uint8_t *) response, responseLen);
6988#if VDBG
6989        RLOGD("newBroadcastSmsInd");
6990#endif
6991        Return<void> retStatus = radioService[slotId]->mRadioIndication->newBroadcastSms(
6992                convertIntToRadioIndicationType(indicationType), data);
6993        radioService[slotId]->checkReturnStatus(retStatus);
6994    } else {
6995        RLOGE("newBroadcastSmsInd: radioService[%d]->mRadioIndication == NULL", slotId);
6996    }
6997
6998    return 0;
6999}
7000
7001int radio::cdmaRuimSmsStorageFullInd(int slotId,
7002                                     int indicationType, int token, RIL_Errno e, void *response,
7003                                     size_t responseLen) {
7004    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7005#if VDBG
7006        RLOGD("cdmaRuimSmsStorageFullInd");
7007#endif
7008        Return<void> retStatus = radioService[slotId]->mRadioIndication->cdmaRuimSmsStorageFull(
7009                convertIntToRadioIndicationType(indicationType));
7010        radioService[slotId]->checkReturnStatus(retStatus);
7011    } else {
7012        RLOGE("cdmaRuimSmsStorageFullInd: radioService[%d]->mRadioIndication == NULL",
7013                slotId);
7014    }
7015
7016    return 0;
7017}
7018
7019int radio::restrictedStateChangedInd(int slotId,
7020                                     int indicationType, int token, RIL_Errno e, void *response,
7021                                     size_t responseLen) {
7022    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7023        if (response == NULL || responseLen != sizeof(int)) {
7024            RLOGE("restrictedStateChangedInd: invalid response");
7025            return 0;
7026        }
7027        int32_t state = ((int32_t *) response)[0];
7028#if VDBG
7029        RLOGD("restrictedStateChangedInd: state %d", state);
7030#endif
7031        Return<void> retStatus = radioService[slotId]->mRadioIndication->restrictedStateChanged(
7032                convertIntToRadioIndicationType(indicationType), (PhoneRestrictedState) state);
7033        radioService[slotId]->checkReturnStatus(retStatus);
7034    } else {
7035        RLOGE("restrictedStateChangedInd: radioService[%d]->mRadioIndication == NULL",
7036                slotId);
7037    }
7038
7039    return 0;
7040}
7041
7042int radio::enterEmergencyCallbackModeInd(int slotId,
7043                                         int indicationType, int token, RIL_Errno e, void *response,
7044                                         size_t responseLen) {
7045    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7046#if VDBG
7047        RLOGD("enterEmergencyCallbackModeInd");
7048#endif
7049        Return<void> retStatus = radioService[slotId]->mRadioIndication->enterEmergencyCallbackMode(
7050                convertIntToRadioIndicationType(indicationType));
7051        radioService[slotId]->checkReturnStatus(retStatus);
7052    } else {
7053        RLOGE("enterEmergencyCallbackModeInd: radioService[%d]->mRadioIndication == NULL",
7054                slotId);
7055    }
7056
7057    return 0;
7058}
7059
7060int radio::cdmaCallWaitingInd(int slotId,
7061                              int indicationType, int token, RIL_Errno e, void *response,
7062                              size_t responseLen) {
7063    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7064        if (response == NULL || responseLen != sizeof(RIL_CDMA_CallWaiting_v6)) {
7065            RLOGE("cdmaCallWaitingInd: invalid response");
7066            return 0;
7067        }
7068
7069        CdmaCallWaiting callWaitingRecord = {};
7070        RIL_CDMA_CallWaiting_v6 *callWaitingRil = ((RIL_CDMA_CallWaiting_v6 *) response);
7071        callWaitingRecord.number = convertCharPtrToHidlString(callWaitingRil->number);
7072        callWaitingRecord.numberPresentation =
7073                (CdmaCallWaitingNumberPresentation) callWaitingRil->numberPresentation;
7074        callWaitingRecord.name = convertCharPtrToHidlString(callWaitingRil->name);
7075        convertRilCdmaSignalInfoRecordToHal(&callWaitingRil->signalInfoRecord,
7076                callWaitingRecord.signalInfoRecord);
7077        callWaitingRecord.numberType = (CdmaCallWaitingNumberType) callWaitingRil->number_type;
7078        callWaitingRecord.numberPlan = (CdmaCallWaitingNumberPlan) callWaitingRil->number_plan;
7079
7080#if VDBG
7081        RLOGD("cdmaCallWaitingInd");
7082#endif
7083        Return<void> retStatus = radioService[slotId]->mRadioIndication->cdmaCallWaiting(
7084                convertIntToRadioIndicationType(indicationType), callWaitingRecord);
7085        radioService[slotId]->checkReturnStatus(retStatus);
7086    } else {
7087        RLOGE("cdmaCallWaitingInd: radioService[%d]->mRadioIndication == NULL", slotId);
7088    }
7089
7090    return 0;
7091}
7092
7093int radio::cdmaOtaProvisionStatusInd(int slotId,
7094                                     int indicationType, int token, RIL_Errno e, void *response,
7095                                     size_t responseLen) {
7096    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7097        if (response == NULL || responseLen != sizeof(int)) {
7098            RLOGE("cdmaOtaProvisionStatusInd: invalid response");
7099            return 0;
7100        }
7101        int32_t status = ((int32_t *) response)[0];
7102#if VDBG
7103        RLOGD("cdmaOtaProvisionStatusInd: status %d", status);
7104#endif
7105        Return<void> retStatus = radioService[slotId]->mRadioIndication->cdmaOtaProvisionStatus(
7106                convertIntToRadioIndicationType(indicationType), (CdmaOtaProvisionStatus) status);
7107        radioService[slotId]->checkReturnStatus(retStatus);
7108    } else {
7109        RLOGE("cdmaOtaProvisionStatusInd: radioService[%d]->mRadioIndication == NULL",
7110                slotId);
7111    }
7112
7113    return 0;
7114}
7115
7116int radio::cdmaInfoRecInd(int slotId,
7117                          int indicationType, int token, RIL_Errno e, void *response,
7118                          size_t responseLen) {
7119    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7120        if (response == NULL || responseLen != sizeof(RIL_CDMA_InformationRecords)) {
7121            RLOGE("cdmaInfoRecInd: invalid response");
7122            return 0;
7123        }
7124
7125        CdmaInformationRecords records = {};
7126        RIL_CDMA_InformationRecords *recordsRil = (RIL_CDMA_InformationRecords *) response;
7127
7128        char* string8 = NULL;
7129        int num = MIN(recordsRil->numberOfInfoRecs, RIL_CDMA_MAX_NUMBER_OF_INFO_RECS);
7130        if (recordsRil->numberOfInfoRecs > RIL_CDMA_MAX_NUMBER_OF_INFO_RECS) {
7131            RLOGE("cdmaInfoRecInd: received %d recs which is more than %d, dropping "
7132                    "additional ones", recordsRil->numberOfInfoRecs,
7133                    RIL_CDMA_MAX_NUMBER_OF_INFO_RECS);
7134        }
7135        records.infoRec.resize(num);
7136        for (int i = 0 ; i < num ; i++) {
7137            CdmaInformationRecord *record = &records.infoRec[i];
7138            RIL_CDMA_InformationRecord *infoRec = &recordsRil->infoRec[i];
7139            record->name = (CdmaInfoRecName) infoRec->name;
7140            // All vectors should be size 0 except one which will be size 1. Set everything to
7141            // size 0 initially.
7142            record->display.resize(0);
7143            record->number.resize(0);
7144            record->signal.resize(0);
7145            record->redir.resize(0);
7146            record->lineCtrl.resize(0);
7147            record->clir.resize(0);
7148            record->audioCtrl.resize(0);
7149            switch (infoRec->name) {
7150                case RIL_CDMA_DISPLAY_INFO_REC:
7151                case RIL_CDMA_EXTENDED_DISPLAY_INFO_REC: {
7152                    if (infoRec->rec.display.alpha_len > CDMA_ALPHA_INFO_BUFFER_LENGTH) {
7153                        RLOGE("cdmaInfoRecInd: invalid display info response length %d "
7154                                "expected not more than %d", (int) infoRec->rec.display.alpha_len,
7155                                CDMA_ALPHA_INFO_BUFFER_LENGTH);
7156                        return 0;
7157                    }
7158                    string8 = (char*) malloc((infoRec->rec.display.alpha_len + 1) * sizeof(char));
7159                    if (string8 == NULL) {
7160                        RLOGE("cdmaInfoRecInd: Memory allocation failed for "
7161                                "responseCdmaInformationRecords");
7162                        return 0;
7163                    }
7164                    memcpy(string8, infoRec->rec.display.alpha_buf, infoRec->rec.display.alpha_len);
7165                    string8[(int)infoRec->rec.display.alpha_len] = '\0';
7166
7167                    record->display.resize(1);
7168                    record->display[0].alphaBuf = string8;
7169                    free(string8);
7170                    string8 = NULL;
7171                    break;
7172                }
7173
7174                case RIL_CDMA_CALLED_PARTY_NUMBER_INFO_REC:
7175                case RIL_CDMA_CALLING_PARTY_NUMBER_INFO_REC:
7176                case RIL_CDMA_CONNECTED_NUMBER_INFO_REC: {
7177                    if (infoRec->rec.number.len > CDMA_NUMBER_INFO_BUFFER_LENGTH) {
7178                        RLOGE("cdmaInfoRecInd: invalid display info response length %d "
7179                                "expected not more than %d", (int) infoRec->rec.number.len,
7180                                CDMA_NUMBER_INFO_BUFFER_LENGTH);
7181                        return 0;
7182                    }
7183                    string8 = (char*) malloc((infoRec->rec.number.len + 1) * sizeof(char));
7184                    if (string8 == NULL) {
7185                        RLOGE("cdmaInfoRecInd: Memory allocation failed for "
7186                                "responseCdmaInformationRecords");
7187                        return 0;
7188                    }
7189                    memcpy(string8, infoRec->rec.number.buf, infoRec->rec.number.len);
7190                    string8[(int)infoRec->rec.number.len] = '\0';
7191
7192                    record->number.resize(1);
7193                    record->number[0].number = string8;
7194                    free(string8);
7195                    string8 = NULL;
7196                    record->number[0].numberType = infoRec->rec.number.number_type;
7197                    record->number[0].numberPlan = infoRec->rec.number.number_plan;
7198                    record->number[0].pi = infoRec->rec.number.pi;
7199                    record->number[0].si = infoRec->rec.number.si;
7200                    break;
7201                }
7202
7203                case RIL_CDMA_SIGNAL_INFO_REC: {
7204                    record->signal.resize(1);
7205                    record->signal[0].isPresent = infoRec->rec.signal.isPresent;
7206                    record->signal[0].signalType = infoRec->rec.signal.signalType;
7207                    record->signal[0].alertPitch = infoRec->rec.signal.alertPitch;
7208                    record->signal[0].signal = infoRec->rec.signal.signal;
7209                    break;
7210                }
7211
7212                case RIL_CDMA_REDIRECTING_NUMBER_INFO_REC: {
7213                    if (infoRec->rec.redir.redirectingNumber.len >
7214                                                  CDMA_NUMBER_INFO_BUFFER_LENGTH) {
7215                        RLOGE("cdmaInfoRecInd: invalid display info response length %d "
7216                                "expected not more than %d\n",
7217                                (int)infoRec->rec.redir.redirectingNumber.len,
7218                                CDMA_NUMBER_INFO_BUFFER_LENGTH);
7219                        return 0;
7220                    }
7221                    string8 = (char*) malloc((infoRec->rec.redir.redirectingNumber.len + 1) *
7222                            sizeof(char));
7223                    if (string8 == NULL) {
7224                        RLOGE("cdmaInfoRecInd: Memory allocation failed for "
7225                                "responseCdmaInformationRecords");
7226                        return 0;
7227                    }
7228                    memcpy(string8, infoRec->rec.redir.redirectingNumber.buf,
7229                            infoRec->rec.redir.redirectingNumber.len);
7230                    string8[(int)infoRec->rec.redir.redirectingNumber.len] = '\0';
7231
7232                    record->redir.resize(1);
7233                    record->redir[0].redirectingNumber.number = string8;
7234                    free(string8);
7235                    string8 = NULL;
7236                    record->redir[0].redirectingNumber.numberType =
7237                            infoRec->rec.redir.redirectingNumber.number_type;
7238                    record->redir[0].redirectingNumber.numberPlan =
7239                            infoRec->rec.redir.redirectingNumber.number_plan;
7240                    record->redir[0].redirectingNumber.pi = infoRec->rec.redir.redirectingNumber.pi;
7241                    record->redir[0].redirectingNumber.si = infoRec->rec.redir.redirectingNumber.si;
7242                    record->redir[0].redirectingReason =
7243                            (CdmaRedirectingReason) infoRec->rec.redir.redirectingReason;
7244                    break;
7245                }
7246
7247                case RIL_CDMA_LINE_CONTROL_INFO_REC: {
7248                    record->lineCtrl.resize(1);
7249                    record->lineCtrl[0].lineCtrlPolarityIncluded =
7250                            infoRec->rec.lineCtrl.lineCtrlPolarityIncluded;
7251                    record->lineCtrl[0].lineCtrlToggle = infoRec->rec.lineCtrl.lineCtrlToggle;
7252                    record->lineCtrl[0].lineCtrlReverse = infoRec->rec.lineCtrl.lineCtrlReverse;
7253                    record->lineCtrl[0].lineCtrlPowerDenial =
7254                            infoRec->rec.lineCtrl.lineCtrlPowerDenial;
7255                    break;
7256                }
7257
7258                case RIL_CDMA_T53_CLIR_INFO_REC: {
7259                    record->clir.resize(1);
7260                    record->clir[0].cause = infoRec->rec.clir.cause;
7261                    break;
7262                }
7263
7264                case RIL_CDMA_T53_AUDIO_CONTROL_INFO_REC: {
7265                    record->audioCtrl.resize(1);
7266                    record->audioCtrl[0].upLink = infoRec->rec.audioCtrl.upLink;
7267                    record->audioCtrl[0].downLink = infoRec->rec.audioCtrl.downLink;
7268                    break;
7269                }
7270
7271                case RIL_CDMA_T53_RELEASE_INFO_REC:
7272                    RLOGE("cdmaInfoRecInd: RIL_CDMA_T53_RELEASE_INFO_REC: INVALID");
7273                    return 0;
7274
7275                default:
7276                    RLOGE("cdmaInfoRecInd: Incorrect name value");
7277                    return 0;
7278            }
7279        }
7280
7281#if VDBG
7282        RLOGD("cdmaInfoRecInd");
7283#endif
7284        Return<void> retStatus = radioService[slotId]->mRadioIndication->cdmaInfoRec(
7285                convertIntToRadioIndicationType(indicationType), records);
7286        radioService[slotId]->checkReturnStatus(retStatus);
7287    } else {
7288        RLOGE("cdmaInfoRecInd: radioService[%d]->mRadioIndication == NULL", slotId);
7289    }
7290
7291    return 0;
7292}
7293
7294int radio::indicateRingbackToneInd(int slotId,
7295                                   int indicationType, int token, RIL_Errno e, void *response,
7296                                   size_t responseLen) {
7297    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7298        if (response == NULL || responseLen != sizeof(int)) {
7299            RLOGE("indicateRingbackToneInd: invalid response");
7300            return 0;
7301        }
7302        bool start = ((int32_t *) response)[0];
7303#if VDBG
7304        RLOGD("indicateRingbackToneInd: start %d", start);
7305#endif
7306        Return<void> retStatus = radioService[slotId]->mRadioIndication->indicateRingbackTone(
7307                convertIntToRadioIndicationType(indicationType), start);
7308        radioService[slotId]->checkReturnStatus(retStatus);
7309    } else {
7310        RLOGE("indicateRingbackToneInd: radioService[%d]->mRadioIndication == NULL", slotId);
7311    }
7312
7313    return 0;
7314}
7315
7316int radio::resendIncallMuteInd(int slotId,
7317                               int indicationType, int token, RIL_Errno e, void *response,
7318                               size_t responseLen) {
7319    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7320#if VDBG
7321        RLOGD("resendIncallMuteInd");
7322#endif
7323        Return<void> retStatus = radioService[slotId]->mRadioIndication->resendIncallMute(
7324                convertIntToRadioIndicationType(indicationType));
7325        radioService[slotId]->checkReturnStatus(retStatus);
7326    } else {
7327        RLOGE("resendIncallMuteInd: radioService[%d]->mRadioIndication == NULL", slotId);
7328    }
7329
7330    return 0;
7331}
7332
7333int radio::cdmaSubscriptionSourceChangedInd(int slotId,
7334                                            int indicationType, int token, RIL_Errno e,
7335                                            void *response, size_t responseLen) {
7336    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7337        if (response == NULL || responseLen != sizeof(int)) {
7338            RLOGE("cdmaSubscriptionSourceChangedInd: invalid response");
7339            return 0;
7340        }
7341        int32_t cdmaSource = ((int32_t *) response)[0];
7342#if VDBG
7343        RLOGD("cdmaSubscriptionSourceChangedInd: cdmaSource %d", cdmaSource);
7344#endif
7345        Return<void> retStatus = radioService[slotId]->mRadioIndication->
7346                cdmaSubscriptionSourceChanged(convertIntToRadioIndicationType(indicationType),
7347                (CdmaSubscriptionSource) cdmaSource);
7348        radioService[slotId]->checkReturnStatus(retStatus);
7349    } else {
7350        RLOGE("cdmaSubscriptionSourceChangedInd: radioService[%d]->mRadioIndication == NULL",
7351                slotId);
7352    }
7353
7354    return 0;
7355}
7356
7357int radio::cdmaPrlChangedInd(int slotId,
7358                             int indicationType, int token, RIL_Errno e, void *response,
7359                             size_t responseLen) {
7360    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7361        if (response == NULL || responseLen != sizeof(int)) {
7362            RLOGE("cdmaPrlChangedInd: invalid response");
7363            return 0;
7364        }
7365        int32_t version = ((int32_t *) response)[0];
7366#if VDBG
7367        RLOGD("cdmaPrlChangedInd: version %d", version);
7368#endif
7369        Return<void> retStatus = radioService[slotId]->mRadioIndication->cdmaPrlChanged(
7370                convertIntToRadioIndicationType(indicationType), version);
7371        radioService[slotId]->checkReturnStatus(retStatus);
7372    } else {
7373        RLOGE("cdmaPrlChangedInd: radioService[%d]->mRadioIndication == NULL", slotId);
7374    }
7375
7376    return 0;
7377}
7378
7379int radio::exitEmergencyCallbackModeInd(int slotId,
7380                                        int indicationType, int token, RIL_Errno e, void *response,
7381                                        size_t responseLen) {
7382    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7383#if VDBG
7384        RLOGD("exitEmergencyCallbackModeInd");
7385#endif
7386        Return<void> retStatus = radioService[slotId]->mRadioIndication->exitEmergencyCallbackMode(
7387                convertIntToRadioIndicationType(indicationType));
7388        radioService[slotId]->checkReturnStatus(retStatus);
7389    } else {
7390        RLOGE("exitEmergencyCallbackModeInd: radioService[%d]->mRadioIndication == NULL",
7391                slotId);
7392    }
7393
7394    return 0;
7395}
7396
7397int radio::rilConnectedInd(int slotId,
7398                           int indicationType, int token, RIL_Errno e, void *response,
7399                           size_t responseLen) {
7400    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7401        RLOGD("rilConnectedInd");
7402        Return<void> retStatus = radioService[slotId]->mRadioIndication->rilConnected(
7403                convertIntToRadioIndicationType(indicationType));
7404        radioService[slotId]->checkReturnStatus(retStatus);
7405    } else {
7406        RLOGE("rilConnectedInd: radioService[%d]->mRadioIndication == NULL", slotId);
7407    }
7408
7409    return 0;
7410}
7411
7412int radio::voiceRadioTechChangedInd(int slotId,
7413                                    int indicationType, int token, RIL_Errno e, void *response,
7414                                    size_t responseLen) {
7415    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7416        if (response == NULL || responseLen != sizeof(int)) {
7417            RLOGE("voiceRadioTechChangedInd: invalid response");
7418            return 0;
7419        }
7420        int32_t rat = ((int32_t *) response)[0];
7421#if VDBG
7422        RLOGD("voiceRadioTechChangedInd: rat %d", rat);
7423#endif
7424        Return<void> retStatus = radioService[slotId]->mRadioIndication->voiceRadioTechChanged(
7425                convertIntToRadioIndicationType(indicationType), (RadioTechnology) rat);
7426        radioService[slotId]->checkReturnStatus(retStatus);
7427    } else {
7428        RLOGE("voiceRadioTechChangedInd: radioService[%d]->mRadioIndication == NULL",
7429                slotId);
7430    }
7431
7432    return 0;
7433}
7434
7435void convertRilCellInfoListToHal(void *response, size_t responseLen, hidl_vec<CellInfo>& records) {
7436    int num = responseLen / sizeof(RIL_CellInfo_v12);
7437    records.resize(num);
7438
7439    RIL_CellInfo_v12 *rillCellInfo = (RIL_CellInfo_v12 *) response;
7440    for (int i = 0; i < num; i++) {
7441        records[i].cellInfoType = (CellInfoType) rillCellInfo->cellInfoType;
7442        records[i].registered = rillCellInfo->registered;
7443        records[i].timeStampType = (TimeStampType) rillCellInfo->timeStampType;
7444        records[i].timeStamp = rillCellInfo->timeStamp;
7445        // All vectors should be size 0 except one which will be size 1. Set everything to
7446        // size 0 initially.
7447        records[i].gsm.resize(0);
7448        records[i].wcdma.resize(0);
7449        records[i].cdma.resize(0);
7450        records[i].lte.resize(0);
7451        records[i].tdscdma.resize(0);
7452        switch(rillCellInfo->cellInfoType) {
7453            case RIL_CELL_INFO_TYPE_GSM: {
7454                records[i].gsm.resize(1);
7455                CellInfoGsm *cellInfoGsm = &records[i].gsm[0];
7456                cellInfoGsm->cellIdentityGsm.mcc =
7457                        std::to_string(rillCellInfo->CellInfo.gsm.cellIdentityGsm.mcc);
7458                cellInfoGsm->cellIdentityGsm.mnc =
7459                        std::to_string(rillCellInfo->CellInfo.gsm.cellIdentityGsm.mnc);
7460                cellInfoGsm->cellIdentityGsm.lac =
7461                        rillCellInfo->CellInfo.gsm.cellIdentityGsm.lac;
7462                cellInfoGsm->cellIdentityGsm.cid =
7463                        rillCellInfo->CellInfo.gsm.cellIdentityGsm.cid;
7464                cellInfoGsm->cellIdentityGsm.arfcn =
7465                        rillCellInfo->CellInfo.gsm.cellIdentityGsm.arfcn;
7466                cellInfoGsm->cellIdentityGsm.bsic =
7467                        rillCellInfo->CellInfo.gsm.cellIdentityGsm.bsic;
7468                cellInfoGsm->signalStrengthGsm.signalStrength =
7469                        rillCellInfo->CellInfo.gsm.signalStrengthGsm.signalStrength;
7470                cellInfoGsm->signalStrengthGsm.bitErrorRate =
7471                        rillCellInfo->CellInfo.gsm.signalStrengthGsm.bitErrorRate;
7472                cellInfoGsm->signalStrengthGsm.timingAdvance =
7473                        rillCellInfo->CellInfo.gsm.signalStrengthGsm.timingAdvance;
7474                break;
7475            }
7476
7477            case RIL_CELL_INFO_TYPE_WCDMA: {
7478                records[i].wcdma.resize(1);
7479                CellInfoWcdma *cellInfoWcdma = &records[i].wcdma[0];
7480                cellInfoWcdma->cellIdentityWcdma.mcc =
7481                        std::to_string(rillCellInfo->CellInfo.wcdma.cellIdentityWcdma.mcc);
7482                cellInfoWcdma->cellIdentityWcdma.mnc =
7483                        std::to_string(rillCellInfo->CellInfo.wcdma.cellIdentityWcdma.mnc);
7484                cellInfoWcdma->cellIdentityWcdma.lac =
7485                        rillCellInfo->CellInfo.wcdma.cellIdentityWcdma.lac;
7486                cellInfoWcdma->cellIdentityWcdma.cid =
7487                        rillCellInfo->CellInfo.wcdma.cellIdentityWcdma.cid;
7488                cellInfoWcdma->cellIdentityWcdma.psc =
7489                        rillCellInfo->CellInfo.wcdma.cellIdentityWcdma.psc;
7490                cellInfoWcdma->cellIdentityWcdma.uarfcn =
7491                        rillCellInfo->CellInfo.wcdma.cellIdentityWcdma.uarfcn;
7492                cellInfoWcdma->signalStrengthWcdma.signalStrength =
7493                        rillCellInfo->CellInfo.wcdma.signalStrengthWcdma.signalStrength;
7494                cellInfoWcdma->signalStrengthWcdma.bitErrorRate =
7495                        rillCellInfo->CellInfo.wcdma.signalStrengthWcdma.bitErrorRate;
7496                break;
7497            }
7498
7499            case RIL_CELL_INFO_TYPE_CDMA: {
7500                records[i].cdma.resize(1);
7501                CellInfoCdma *cellInfoCdma = &records[i].cdma[0];
7502                cellInfoCdma->cellIdentityCdma.networkId =
7503                        rillCellInfo->CellInfo.cdma.cellIdentityCdma.networkId;
7504                cellInfoCdma->cellIdentityCdma.systemId =
7505                        rillCellInfo->CellInfo.cdma.cellIdentityCdma.systemId;
7506                cellInfoCdma->cellIdentityCdma.baseStationId =
7507                        rillCellInfo->CellInfo.cdma.cellIdentityCdma.basestationId;
7508                cellInfoCdma->cellIdentityCdma.longitude =
7509                        rillCellInfo->CellInfo.cdma.cellIdentityCdma.longitude;
7510                cellInfoCdma->cellIdentityCdma.latitude =
7511                        rillCellInfo->CellInfo.cdma.cellIdentityCdma.latitude;
7512                cellInfoCdma->signalStrengthCdma.dbm =
7513                        rillCellInfo->CellInfo.cdma.signalStrengthCdma.dbm;
7514                cellInfoCdma->signalStrengthCdma.ecio =
7515                        rillCellInfo->CellInfo.cdma.signalStrengthCdma.ecio;
7516                cellInfoCdma->signalStrengthEvdo.dbm =
7517                        rillCellInfo->CellInfo.cdma.signalStrengthEvdo.dbm;
7518                cellInfoCdma->signalStrengthEvdo.ecio =
7519                        rillCellInfo->CellInfo.cdma.signalStrengthEvdo.ecio;
7520                cellInfoCdma->signalStrengthEvdo.signalNoiseRatio =
7521                        rillCellInfo->CellInfo.cdma.signalStrengthEvdo.signalNoiseRatio;
7522                break;
7523            }
7524
7525            case RIL_CELL_INFO_TYPE_LTE: {
7526                records[i].lte.resize(1);
7527                CellInfoLte *cellInfoLte = &records[i].lte[0];
7528                cellInfoLte->cellIdentityLte.mcc =
7529                        std::to_string(rillCellInfo->CellInfo.lte.cellIdentityLte.mcc);
7530                cellInfoLte->cellIdentityLte.mnc =
7531                        std::to_string(rillCellInfo->CellInfo.lte.cellIdentityLte.mnc);
7532                cellInfoLte->cellIdentityLte.ci =
7533                        rillCellInfo->CellInfo.lte.cellIdentityLte.ci;
7534                cellInfoLte->cellIdentityLte.pci =
7535                        rillCellInfo->CellInfo.lte.cellIdentityLte.pci;
7536                cellInfoLte->cellIdentityLte.tac =
7537                        rillCellInfo->CellInfo.lte.cellIdentityLte.tac;
7538                cellInfoLte->cellIdentityLte.earfcn =
7539                        rillCellInfo->CellInfo.lte.cellIdentityLte.earfcn;
7540                cellInfoLte->signalStrengthLte.signalStrength =
7541                        rillCellInfo->CellInfo.lte.signalStrengthLte.signalStrength;
7542                cellInfoLte->signalStrengthLte.rsrp =
7543                        rillCellInfo->CellInfo.lte.signalStrengthLte.rsrp;
7544                cellInfoLte->signalStrengthLte.rsrq =
7545                        rillCellInfo->CellInfo.lte.signalStrengthLte.rsrq;
7546                cellInfoLte->signalStrengthLte.rssnr =
7547                        rillCellInfo->CellInfo.lte.signalStrengthLte.rssnr;
7548                cellInfoLte->signalStrengthLte.cqi =
7549                        rillCellInfo->CellInfo.lte.signalStrengthLte.cqi;
7550                cellInfoLte->signalStrengthLte.timingAdvance =
7551                        rillCellInfo->CellInfo.lte.signalStrengthLte.timingAdvance;
7552                break;
7553            }
7554
7555            case RIL_CELL_INFO_TYPE_TD_SCDMA: {
7556                records[i].tdscdma.resize(1);
7557                CellInfoTdscdma *cellInfoTdscdma = &records[i].tdscdma[0];
7558                cellInfoTdscdma->cellIdentityTdscdma.mcc =
7559                        std::to_string(rillCellInfo->CellInfo.tdscdma.cellIdentityTdscdma.mcc);
7560                cellInfoTdscdma->cellIdentityTdscdma.mnc =
7561                        std::to_string(rillCellInfo->CellInfo.tdscdma.cellIdentityTdscdma.mnc);
7562                cellInfoTdscdma->cellIdentityTdscdma.lac =
7563                        rillCellInfo->CellInfo.tdscdma.cellIdentityTdscdma.lac;
7564                cellInfoTdscdma->cellIdentityTdscdma.cid =
7565                        rillCellInfo->CellInfo.tdscdma.cellIdentityTdscdma.cid;
7566                cellInfoTdscdma->cellIdentityTdscdma.cpid =
7567                        rillCellInfo->CellInfo.tdscdma.cellIdentityTdscdma.cpid;
7568                cellInfoTdscdma->signalStrengthTdscdma.rscp =
7569                        rillCellInfo->CellInfo.tdscdma.signalStrengthTdscdma.rscp;
7570                break;
7571            }
7572            default: {
7573                break;
7574            }
7575        }
7576        rillCellInfo += 1;
7577    }
7578}
7579
7580int radio::cellInfoListInd(int slotId,
7581                           int indicationType, int token, RIL_Errno e, void *response,
7582                           size_t responseLen) {
7583    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7584        if (response == NULL || responseLen % sizeof(RIL_CellInfo_v12) != 0) {
7585            RLOGE("cellInfoListInd: invalid response");
7586            return 0;
7587        }
7588
7589        hidl_vec<CellInfo> records;
7590        convertRilCellInfoListToHal(response, responseLen, records);
7591
7592#if VDBG
7593        RLOGD("cellInfoListInd");
7594#endif
7595        Return<void> retStatus = radioService[slotId]->mRadioIndication->cellInfoList(
7596                convertIntToRadioIndicationType(indicationType), records);
7597        radioService[slotId]->checkReturnStatus(retStatus);
7598    } else {
7599        RLOGE("cellInfoListInd: radioService[%d]->mRadioIndication == NULL", slotId);
7600    }
7601
7602    return 0;
7603}
7604
7605int radio::imsNetworkStateChangedInd(int slotId,
7606                                     int indicationType, int token, RIL_Errno e, void *response,
7607                                     size_t responseLen) {
7608    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7609#if VDBG
7610        RLOGD("imsNetworkStateChangedInd");
7611#endif
7612        Return<void> retStatus = radioService[slotId]->mRadioIndication->imsNetworkStateChanged(
7613                convertIntToRadioIndicationType(indicationType));
7614        radioService[slotId]->checkReturnStatus(retStatus);
7615    } else {
7616        RLOGE("imsNetworkStateChangedInd: radioService[%d]->mRadioIndication == NULL",
7617                slotId);
7618    }
7619
7620    return 0;
7621}
7622
7623int radio::subscriptionStatusChangedInd(int slotId,
7624                                        int indicationType, int token, RIL_Errno e, void *response,
7625                                        size_t responseLen) {
7626    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7627        if (response == NULL || responseLen != sizeof(int)) {
7628            RLOGE("subscriptionStatusChangedInd: invalid response");
7629            return 0;
7630        }
7631        bool activate = ((int32_t *) response)[0];
7632#if VDBG
7633        RLOGD("subscriptionStatusChangedInd: activate %d", activate);
7634#endif
7635        Return<void> retStatus = radioService[slotId]->mRadioIndication->subscriptionStatusChanged(
7636                convertIntToRadioIndicationType(indicationType), activate);
7637        radioService[slotId]->checkReturnStatus(retStatus);
7638    } else {
7639        RLOGE("subscriptionStatusChangedInd: radioService[%d]->mRadioIndication == NULL",
7640                slotId);
7641    }
7642
7643    return 0;
7644}
7645
7646int radio::srvccStateNotifyInd(int slotId,
7647                               int indicationType, int token, RIL_Errno e, void *response,
7648                               size_t responseLen) {
7649    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7650        if (response == NULL || responseLen != sizeof(int)) {
7651            RLOGE("srvccStateNotifyInd: invalid response");
7652            return 0;
7653        }
7654        int32_t state = ((int32_t *) response)[0];
7655#if VDBG
7656        RLOGD("srvccStateNotifyInd: rat %d", state);
7657#endif
7658        Return<void> retStatus = radioService[slotId]->mRadioIndication->srvccStateNotify(
7659                convertIntToRadioIndicationType(indicationType), (SrvccState) state);
7660        radioService[slotId]->checkReturnStatus(retStatus);
7661    } else {
7662        RLOGE("srvccStateNotifyInd: radioService[%d]->mRadioIndication == NULL", slotId);
7663    }
7664
7665    return 0;
7666}
7667
7668void convertRilHardwareConfigListToHal(void *response, size_t responseLen,
7669        hidl_vec<HardwareConfig>& records) {
7670    int num = responseLen / sizeof(RIL_HardwareConfig);
7671    records.resize(num);
7672
7673    RIL_HardwareConfig *rilHardwareConfig = (RIL_HardwareConfig *) response;
7674    for (int i = 0; i < num; i++) {
7675        records[i].type = (HardwareConfigType) rilHardwareConfig[i].type;
7676        records[i].uuid = convertCharPtrToHidlString(rilHardwareConfig[i].uuid);
7677        records[i].state = (HardwareConfigState) rilHardwareConfig[i].state;
7678        switch (rilHardwareConfig[i].type) {
7679            case RIL_HARDWARE_CONFIG_MODEM: {
7680                records[i].modem.resize(1);
7681                records[i].sim.resize(0);
7682                HardwareConfigModem *hwConfigModem = &records[i].modem[0];
7683                hwConfigModem->rat = rilHardwareConfig[i].cfg.modem.rat;
7684                hwConfigModem->maxVoice = rilHardwareConfig[i].cfg.modem.maxVoice;
7685                hwConfigModem->maxData = rilHardwareConfig[i].cfg.modem.maxData;
7686                hwConfigModem->maxStandby = rilHardwareConfig[i].cfg.modem.maxStandby;
7687                break;
7688            }
7689
7690            case RIL_HARDWARE_CONFIG_SIM: {
7691                records[i].sim.resize(1);
7692                records[i].modem.resize(0);
7693                records[i].sim[0].modemUuid =
7694                        convertCharPtrToHidlString(rilHardwareConfig[i].cfg.sim.modemUuid);
7695                break;
7696            }
7697        }
7698    }
7699}
7700
7701int radio::hardwareConfigChangedInd(int slotId,
7702                                    int indicationType, int token, RIL_Errno e, void *response,
7703                                    size_t responseLen) {
7704    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7705        if (response == NULL || responseLen % sizeof(RIL_HardwareConfig) != 0) {
7706            RLOGE("hardwareConfigChangedInd: invalid response");
7707            return 0;
7708        }
7709
7710        hidl_vec<HardwareConfig> configs;
7711        convertRilHardwareConfigListToHal(response, responseLen, configs);
7712
7713#if VDBG
7714        RLOGD("hardwareConfigChangedInd");
7715#endif
7716        Return<void> retStatus = radioService[slotId]->mRadioIndication->hardwareConfigChanged(
7717                convertIntToRadioIndicationType(indicationType), configs);
7718        radioService[slotId]->checkReturnStatus(retStatus);
7719    } else {
7720        RLOGE("hardwareConfigChangedInd: radioService[%d]->mRadioIndication == NULL",
7721                slotId);
7722    }
7723
7724    return 0;
7725}
7726
7727void convertRilRadioCapabilityToHal(void *response, size_t responseLen, RadioCapability& rc) {
7728    RIL_RadioCapability *rilRadioCapability = (RIL_RadioCapability *) response;
7729    rc.session = rilRadioCapability->session;
7730    rc.phase = (android::hardware::radio::V1_0::RadioCapabilityPhase) rilRadioCapability->phase;
7731    rc.raf = rilRadioCapability->rat;
7732    rc.logicalModemUuid = convertCharPtrToHidlString(rilRadioCapability->logicalModemUuid);
7733    rc.status = (android::hardware::radio::V1_0::RadioCapabilityStatus) rilRadioCapability->status;
7734}
7735
7736int radio::radioCapabilityIndicationInd(int slotId,
7737                                        int indicationType, int token, RIL_Errno e, void *response,
7738                                        size_t responseLen) {
7739    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7740        if (response == NULL || responseLen != sizeof(RIL_RadioCapability)) {
7741            RLOGE("radioCapabilityIndicationInd: invalid response");
7742            return 0;
7743        }
7744
7745        RadioCapability rc = {};
7746        convertRilRadioCapabilityToHal(response, responseLen, rc);
7747
7748#if VDBG
7749        RLOGD("radioCapabilityIndicationInd");
7750#endif
7751        Return<void> retStatus = radioService[slotId]->mRadioIndication->radioCapabilityIndication(
7752                convertIntToRadioIndicationType(indicationType), rc);
7753        radioService[slotId]->checkReturnStatus(retStatus);
7754    } else {
7755        RLOGE("radioCapabilityIndicationInd: radioService[%d]->mRadioIndication == NULL",
7756                slotId);
7757    }
7758
7759    return 0;
7760}
7761
7762bool isServiceTypeCfQuery(RIL_SsServiceType serType, RIL_SsRequestType reqType) {
7763    if ((reqType == SS_INTERROGATION) &&
7764        (serType == SS_CFU ||
7765         serType == SS_CF_BUSY ||
7766         serType == SS_CF_NO_REPLY ||
7767         serType == SS_CF_NOT_REACHABLE ||
7768         serType == SS_CF_ALL ||
7769         serType == SS_CF_ALL_CONDITIONAL)) {
7770        return true;
7771    }
7772    return false;
7773}
7774
7775int radio::onSupplementaryServiceIndicationInd(int slotId,
7776                                               int indicationType, int token, RIL_Errno e,
7777                                               void *response, size_t responseLen) {
7778    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7779        if (response == NULL || responseLen != sizeof(RIL_StkCcUnsolSsResponse)) {
7780            RLOGE("onSupplementaryServiceIndicationInd: invalid response");
7781            return 0;
7782        }
7783
7784        RIL_StkCcUnsolSsResponse *rilSsResponse = (RIL_StkCcUnsolSsResponse *) response;
7785        StkCcUnsolSsResult ss = {};
7786        ss.serviceType = (SsServiceType) rilSsResponse->serviceType;
7787        ss.requestType = (SsRequestType) rilSsResponse->requestType;
7788        ss.teleserviceType = (SsTeleserviceType) rilSsResponse->teleserviceType;
7789        ss.serviceClass = rilSsResponse->serviceClass;
7790        ss.result = (RadioError) rilSsResponse->result;
7791
7792        if (isServiceTypeCfQuery(rilSsResponse->serviceType, rilSsResponse->requestType)) {
7793#if VDBG
7794            RLOGD("onSupplementaryServiceIndicationInd CF type, num of Cf elements %d",
7795                    rilSsResponse->cfData.numValidIndexes);
7796#endif
7797            if (rilSsResponse->cfData.numValidIndexes > NUM_SERVICE_CLASSES) {
7798                RLOGE("onSupplementaryServiceIndicationInd numValidIndexes is greater than "
7799                        "max value %d, truncating it to max value", NUM_SERVICE_CLASSES);
7800                rilSsResponse->cfData.numValidIndexes = NUM_SERVICE_CLASSES;
7801            }
7802
7803            ss.cfData.resize(1);
7804            ss.ssInfo.resize(0);
7805
7806            /* number of call info's */
7807            ss.cfData[0].cfInfo.resize(rilSsResponse->cfData.numValidIndexes);
7808
7809            for (int i = 0; i < rilSsResponse->cfData.numValidIndexes; i++) {
7810                 RIL_CallForwardInfo cf = rilSsResponse->cfData.cfInfo[i];
7811                 CallForwardInfo *cfInfo = &ss.cfData[0].cfInfo[i];
7812
7813                 cfInfo->status = (CallForwardInfoStatus) cf.status;
7814                 cfInfo->reason = cf.reason;
7815                 cfInfo->serviceClass = cf.serviceClass;
7816                 cfInfo->toa = cf.toa;
7817                 cfInfo->number = convertCharPtrToHidlString(cf.number);
7818                 cfInfo->timeSeconds = cf.timeSeconds;
7819#if VDBG
7820                 RLOGD("onSupplementaryServiceIndicationInd: "
7821                        "Data: %d,reason=%d,cls=%d,toa=%d,num=%s,tout=%d],", cf.status,
7822                        cf.reason, cf.serviceClass, cf.toa, (char*)cf.number, cf.timeSeconds);
7823#endif
7824            }
7825        } else {
7826            ss.ssInfo.resize(1);
7827            ss.cfData.resize(0);
7828
7829            /* each int */
7830            ss.ssInfo[0].ssInfo.resize(SS_INFO_MAX);
7831            for (int i = 0; i < SS_INFO_MAX; i++) {
7832#if VDBG
7833                 RLOGD("onSupplementaryServiceIndicationInd: Data: %d",
7834                        rilSsResponse->ssInfo[i]);
7835#endif
7836                 ss.ssInfo[0].ssInfo[i] = rilSsResponse->ssInfo[i];
7837            }
7838        }
7839
7840#if VDBG
7841        RLOGD("onSupplementaryServiceIndicationInd");
7842#endif
7843        Return<void> retStatus = radioService[slotId]->mRadioIndication->
7844                onSupplementaryServiceIndication(convertIntToRadioIndicationType(indicationType),
7845                ss);
7846        radioService[slotId]->checkReturnStatus(retStatus);
7847    } else {
7848        RLOGE("onSupplementaryServiceIndicationInd: "
7849                "radioService[%d]->mRadioIndication == NULL", slotId);
7850    }
7851
7852    return 0;
7853}
7854
7855int radio::stkCallControlAlphaNotifyInd(int slotId,
7856                                        int indicationType, int token, RIL_Errno e, void *response,
7857                                        size_t responseLen) {
7858    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7859        if (response == NULL || responseLen == 0) {
7860            RLOGE("stkCallControlAlphaNotifyInd: invalid response");
7861            return 0;
7862        }
7863#if VDBG
7864        RLOGD("stkCallControlAlphaNotifyInd");
7865#endif
7866        Return<void> retStatus = radioService[slotId]->mRadioIndication->stkCallControlAlphaNotify(
7867                convertIntToRadioIndicationType(indicationType),
7868                convertCharPtrToHidlString((char *) response));
7869        radioService[slotId]->checkReturnStatus(retStatus);
7870    } else {
7871        RLOGE("stkCallControlAlphaNotifyInd: radioService[%d]->mRadioIndication == NULL",
7872                slotId);
7873    }
7874
7875    return 0;
7876}
7877
7878void convertRilLceDataInfoToHal(void *response, size_t responseLen, LceDataInfo& lce) {
7879    RIL_LceDataInfo *rilLceDataInfo = (RIL_LceDataInfo *)response;
7880    lce.lastHopCapacityKbps = rilLceDataInfo->last_hop_capacity_kbps;
7881    lce.confidenceLevel = rilLceDataInfo->confidence_level;
7882    lce.lceSuspended = rilLceDataInfo->lce_suspended;
7883}
7884
7885int radio::lceDataInd(int slotId,
7886                      int indicationType, int token, RIL_Errno e, void *response,
7887                      size_t responseLen) {
7888    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7889        if (response == NULL || responseLen != sizeof(RIL_LceDataInfo)) {
7890            RLOGE("lceDataInd: invalid response");
7891            return 0;
7892        }
7893
7894        LceDataInfo lce = {};
7895        convertRilLceDataInfoToHal(response, responseLen, lce);
7896#if VDBG
7897        RLOGD("lceDataInd");
7898#endif
7899        Return<void> retStatus = radioService[slotId]->mRadioIndication->lceData(
7900                convertIntToRadioIndicationType(indicationType), lce);
7901        radioService[slotId]->checkReturnStatus(retStatus);
7902    } else {
7903        RLOGE("lceDataInd: radioService[%d]->mRadioIndication == NULL", slotId);
7904    }
7905
7906    return 0;
7907}
7908
7909int radio::pcoDataInd(int slotId,
7910                      int indicationType, int token, RIL_Errno e, void *response,
7911                      size_t responseLen) {
7912    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7913        if (response == NULL || responseLen != sizeof(RIL_PCO_Data)) {
7914            RLOGE("pcoDataInd: invalid response");
7915            return 0;
7916        }
7917
7918        PcoDataInfo pco = {};
7919        RIL_PCO_Data *rilPcoData = (RIL_PCO_Data *)response;
7920        pco.cid = rilPcoData->cid;
7921        pco.bearerProto = convertCharPtrToHidlString(rilPcoData->bearer_proto);
7922        pco.pcoId = rilPcoData->pco_id;
7923        pco.contents.setToExternal((uint8_t *) rilPcoData->contents, rilPcoData->contents_length);
7924
7925#if VDBG
7926        RLOGD("pcoDataInd");
7927#endif
7928        Return<void> retStatus = radioService[slotId]->mRadioIndication->pcoData(
7929                convertIntToRadioIndicationType(indicationType), pco);
7930        radioService[slotId]->checkReturnStatus(retStatus);
7931    } else {
7932        RLOGE("pcoDataInd: radioService[%d]->mRadioIndication == NULL", slotId);
7933    }
7934
7935    return 0;
7936}
7937
7938int radio::modemResetInd(int slotId,
7939                         int indicationType, int token, RIL_Errno e, void *response,
7940                         size_t responseLen) {
7941    if (radioService[slotId] != NULL && radioService[slotId]->mRadioIndication != NULL) {
7942        if (response == NULL || responseLen == 0) {
7943            RLOGE("modemResetInd: invalid response");
7944            return 0;
7945        }
7946#if VDBG
7947        RLOGD("modemResetInd");
7948#endif
7949        Return<void> retStatus = radioService[slotId]->mRadioIndication->modemReset(
7950                convertIntToRadioIndicationType(indicationType),
7951                convertCharPtrToHidlString((char *) response));
7952        radioService[slotId]->checkReturnStatus(retStatus);
7953    } else {
7954        RLOGE("modemResetInd: radioService[%d]->mRadioIndication == NULL", slotId);
7955    }
7956
7957    return 0;
7958}
7959
7960int radio::oemHookRawInd(int slotId,
7961                         int indicationType, int token, RIL_Errno e, void *response,
7962                         size_t responseLen) {
7963    if (oemHookService[slotId] != NULL && oemHookService[slotId]->mOemHookIndication != NULL) {
7964        if (response == NULL || responseLen == 0) {
7965            RLOGE("oemHookRawInd: invalid response");
7966            return 0;
7967        }
7968
7969        hidl_vec<uint8_t> data;
7970        data.setToExternal((uint8_t *) response, responseLen);
7971#if VDBG
7972        RLOGD("oemHookRawInd");
7973#endif
7974        Return<void> retStatus = oemHookService[slotId]->mOemHookIndication->oemHookRaw(
7975                convertIntToRadioIndicationType(indicationType), data);
7976        checkReturnStatus(slotId, retStatus, false);
7977    } else {
7978        RLOGE("oemHookRawInd: oemHookService[%d]->mOemHookIndication == NULL", slotId);
7979    }
7980
7981    return 0;
7982}
7983
7984void radio::registerService(RIL_RadioFunctions *callbacks, CommandInfo *commands) {
7985    using namespace android::hardware;
7986    int simCount = 1;
7987    const char *serviceNames[] = {
7988            android::RIL_getServiceName()
7989            #if (SIM_COUNT >= 2)
7990            , RIL2_SERVICE_NAME
7991            #if (SIM_COUNT >= 3)
7992            , RIL3_SERVICE_NAME
7993            #if (SIM_COUNT >= 4)
7994            , RIL4_SERVICE_NAME
7995            #endif
7996            #endif
7997            #endif
7998            };
7999
8000    #if (SIM_COUNT >= 2)
8001    simCount = SIM_COUNT;
8002    #endif
8003
8004    configureRpcThreadpool(1, true /* callerWillJoin */);
8005    for (int i = 0; i < simCount; i++) {
8006        pthread_rwlock_t *radioServiceRwlockPtr = getRadioServiceRwlock(i);
8007        int ret = pthread_rwlock_wrlock(radioServiceRwlockPtr);
8008        assert(ret == 0);
8009
8010        radioService[i] = new RadioImpl;
8011        radioService[i]->mSlotId = i;
8012        oemHookService[i] = new OemHookImpl;
8013        oemHookService[i]->mSlotId = i;
8014        RLOGD("registerService: starting IRadio %s", serviceNames[i]);
8015        android::status_t status = radioService[i]->registerAsService(serviceNames[i]);
8016        status = oemHookService[i]->registerAsService(serviceNames[i]);
8017
8018        ret = pthread_rwlock_unlock(radioServiceRwlockPtr);
8019        assert(ret == 0);
8020    }
8021
8022    s_vendorFunctions = callbacks;
8023    s_commands = commands;
8024}
8025
8026void rilc_thread_pool() {
8027    joinRpcThreadpool();
8028}
8029
8030pthread_rwlock_t * radio::getRadioServiceRwlock(int slotId) {
8031    pthread_rwlock_t *radioServiceRwlockPtr = &radioServiceRwlock;
8032
8033    #if (SIM_COUNT >= 2)
8034    if (slotId == 2) radioServiceRwlockPtr = &radioServiceRwlock2;
8035    #if (SIM_COUNT >= 3)
8036    if (slotId == 3) radioServiceRwlockPtr = &radioServiceRwlock3;
8037    #if (SIM_COUNT >= 4)
8038    if (slotId == 4) radioServiceRwlockPtr = &radioServiceRwlock4;
8039    #endif
8040    #endif
8041    #endif
8042
8043    return radioServiceRwlockPtr;
8044}
8045