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