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