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