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