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